Skip to content

Commit c9681bf

Browse files
committed
misc
1 parent dfc5fcb commit c9681bf

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

lighter/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,4 @@
156156
from lighter.models.withdraw_history import WithdrawHistory
157157
from lighter.models.withdraw_history_item import WithdrawHistoryItem
158158
from lighter.models.zk_lighter_info import ZkLighterInfo
159+
from lighter.models.ws_account_assets import WSAccountAssets
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from __future__ import annotations
2+
import pprint
3+
import re # noqa: F401
4+
import json
5+
6+
from lighter.models.account_asset import AccountAsset
7+
from pydantic import BaseModel, ConfigDict, StrictInt, StrictStr, Field
8+
from typing import Any, ClassVar, Dict, List
9+
from typing import Optional, Set
10+
11+
class WSAccountAssets(BaseModel):
12+
type: StrictStr
13+
channel: StrictStr
14+
assets: Dict[StrictStr, AccountAsset]
15+
account_id: StrictInt
16+
17+
additional_properties: Dict[str, Any] = Field(default_factory=dict)
18+
__properties: ClassVar[List[str]] = ["type", "channel", "assets"]
19+
20+
model_config = ConfigDict(
21+
populate_by_name=True,
22+
validate_assignment=True,
23+
protected_namespaces=(),
24+
)
25+
26+
def to_str(self) -> str:
27+
return pprint.pformat(self.model_dump(by_alias=True))
28+
29+
def to_json(self) -> str:
30+
return json.dumps(self.to_dict())
31+
32+
@classmethod
33+
def from_json(cls, json_str: str) -> Optional["WSAccountAssets"]:
34+
return cls.from_dict(json.loads(json_str))
35+
36+
def to_dict(self) -> Dict[str, Any]:
37+
excluded_fields: Set[str] = {"additional_properties"}
38+
39+
# dump base fields
40+
_dict = self.model_dump(
41+
by_alias=True,
42+
exclude=excluded_fields,
43+
exclude_none=True,
44+
)
45+
46+
# add extra fields
47+
if self.additional_properties is not None:
48+
for _key, _value in self.additional_properties.items():
49+
_dict[_key] = _value
50+
51+
return _dict
52+
53+
@classmethod
54+
def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional["WSAccountAssets"]:
55+
if obj["type"] != "subscribed/account_all_assets" and obj["type"] != "update/account_all_assets":
56+
raise ValueError(f"invalid type {obj['type']} for WSAccountAssets")
57+
58+
if obj is None:
59+
return None
60+
61+
if not isinstance(obj, dict):
62+
return cls.model_validate(obj)
63+
64+
# parse inner assets dict into AccountAsset objects
65+
raw_assets = obj.get("assets") or {}
66+
parsed_assets: Dict[str, AccountAsset] = {
67+
k: AccountAsset.from_dict(v) for k, v in raw_assets.items()
68+
}
69+
70+
account_id = int(obj.get("channel").split(":")[1])
71+
72+
_obj = cls.model_validate(
73+
{
74+
"type": obj.get("type"),
75+
"channel": obj.get("channel"),
76+
"assets": parsed_assets,
77+
"account_id": account_id
78+
}
79+
)
80+
81+
# store additional fields
82+
for _key in obj.keys():
83+
if _key not in cls.__properties:
84+
_obj.additional_properties[_key] = obj.get(_key)
85+
86+
return _obj

0 commit comments

Comments
 (0)