Skip to content

Commit a23965e

Browse files
author
Louis Tao
committed
Version 0.1.18
1 parent 51a235f commit a23965e

File tree

4 files changed

+71
-21
lines changed

4 files changed

+71
-21
lines changed

examples/basic_order_modify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def main():
4242
print("Order status by oid:", order_status)
4343

4444
modify_result = exchange.modify_order(oid, "ETH", True, 0.1, 1105, {"limit": {"tif": "Gtc"}})
45-
print(modify_result)
45+
print("modify result:", modify_result)
4646

4747

4848
if __name__ == "__main__":

hyperliquid/exchange.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
ZERO_ADDRESS,
1414
CancelRequest,
1515
CancelByCloidRequest,
16+
ModifyRequest,
17+
ModifySpec,
1618
OrderRequest,
1719
OrderSpec,
1820
OrderType,
1921
float_to_usd_int,
2022
get_timestamp_ms,
23+
modify_spec_preprocessing,
24+
modify_spec_to_modify_wire,
2125
order_grouping_to_number,
2226
order_request_to_order_spec,
2327
order_spec_preprocessing,
@@ -154,40 +158,49 @@ def modify_order(
154158
reduce_only: bool = False,
155159
cloid: Optional[Cloid] = None,
156160
) -> Any:
157-
order: OrderRequest = {
158-
"coin": coin,
159-
"is_buy": is_buy,
160-
"sz": sz,
161-
"limit_px": limit_px,
162-
"order_type": order_type,
163-
"reduce_only": reduce_only,
161+
162+
modify: ModifyRequest = {
163+
"oid": oid,
164+
"order": {
165+
"coin": coin,
166+
"is_buy": is_buy,
167+
"sz": sz,
168+
"limit_px": limit_px,
169+
"order_type": order_type,
170+
"reduce_only": reduce_only,
171+
"cloid": cloid,
172+
},
164173
}
165-
if cloid:
166-
order["cloid"] = cloid
174+
return self.bulk_modify_orders([modify])
167175

168-
order_spec = order_request_to_order_spec(order, self.coin_to_asset[order["coin"]])
176+
def bulk_modify_orders(self, modify_requests: List[ModifyRequest]) -> Any:
177+
modify_specs: List[ModifySpec] = [
178+
{
179+
"oid": modify["oid"],
180+
"order": order_request_to_order_spec(modify["order"], self.coin_to_asset[modify["order"]["coin"]]),
181+
"orderType": modify["order"]["order_type"],
182+
}
183+
for modify in modify_requests
184+
]
169185

170186
timestamp = get_timestamp_ms()
171187

172-
if cloid:
173-
signature_types = ["uint64", "(uint32,bool,uint64,uint64,bool,uint8,uint64,bytes16)"]
174-
else:
175-
signature_types = ["uint64", "(uint32,bool,uint64,uint64,bool,uint8,uint64)"]
188+
signature_types = ["(uint64,uint32,bool,uint64,uint64,bool,uint8,uint64,bytes16)[]"]
176189

177190
signature = sign_l1_action(
178191
self.wallet,
179192
signature_types,
180-
[oid, order_spec_preprocessing(order_spec)],
193+
[[modify_spec_preprocessing(modify_spec) for modify_spec in modify_specs]],
181194
ZERO_ADDRESS if self.vault_address is None else self.vault_address,
182195
timestamp,
183196
self.base_url == MAINNET_API_URL,
197+
action_type_code=40,
184198
)
185199

186200
return self._post_action(
187201
{
188-
"type": "modify",
189-
"oid": oid,
190-
"order": order_spec_to_order_wire(order_spec),
202+
"type": "batchModify",
203+
"modifies": [modify_spec_to_modify_wire(modify_spec) for modify_spec in modify_specs],
191204
},
192205
signature,
193206
timestamp,

hyperliquid/utils/signing.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
},
2929
total=False,
3030
)
31+
ModifyRequest = TypedDict(
32+
"ModifyRequest",
33+
{
34+
"oid": int,
35+
"order": OrderRequest,
36+
},
37+
total=False,
38+
)
3139
CancelRequest = TypedDict("CancelRequest", {"coin": str, "oid": int})
3240
CancelByCloidRequest = TypedDict("CancelByCloidRequest", {"coin": str, "cloid": Cloid})
3341

@@ -71,6 +79,7 @@ def order_grouping_to_number(grouping: Grouping) -> int:
7179
"Order", {"asset": int, "isBuy": bool, "limitPx": float, "sz": float, "reduceOnly": bool, "cloid": Optional[Cloid]}
7280
)
7381
OrderSpec = TypedDict("OrderSpec", {"order": Order, "orderType": OrderType})
82+
ModifySpec = TypedDict("ModifySpec", {"oid": int, "order": OrderSpec, "orderType": OrderType})
7483

7584

7685
def order_spec_preprocessing(order_spec: OrderSpec) -> Any:
@@ -90,6 +99,15 @@ def order_spec_preprocessing(order_spec: OrderSpec) -> Any:
9099
return res
91100

92101

102+
def modify_spec_preprocessing(modify_spec: ModifySpec) -> Any:
103+
res: Any = (modify_spec["oid"],)
104+
res += order_spec_preprocessing(modify_spec["order"])
105+
order = modify_spec["order"]["order"]
106+
if "cloid" not in order or order["cloid"] is None:
107+
res += (bytearray(16),)
108+
return res
109+
110+
93111
OrderWire = TypedDict(
94112
"OrderWire",
95113
{
@@ -103,6 +121,14 @@ def order_spec_preprocessing(order_spec: OrderSpec) -> Any:
103121
},
104122
)
105123

124+
ModifyWire = TypedDict(
125+
"ModifyWire",
126+
{
127+
"oid": int,
128+
"order": OrderWire,
129+
},
130+
)
131+
106132

107133
def order_type_to_wire(order_type: OrderType) -> OrderTypeWire:
108134
if "limit" in order_type:
@@ -134,13 +160,20 @@ def order_spec_to_order_wire(order_spec: OrderSpec) -> OrderWire:
134160
}
135161

136162

163+
def modify_spec_to_modify_wire(modify_spec: ModifySpec) -> ModifyWire:
164+
return {
165+
"oid": modify_spec["oid"],
166+
"order": order_spec_to_order_wire(modify_spec["order"]),
167+
}
168+
169+
137170
def construct_phantom_agent(signature_types, signature_data, is_mainnet):
138171
connection_id = encode(signature_types, signature_data)
139172

140173
return {"source": "a" if is_mainnet else "b", "connectionId": keccak(connection_id)}
141174

142175

143-
def sign_l1_action(wallet, signature_types, signature_data, active_pool, nonce, is_mainnet):
176+
def sign_l1_action(wallet, signature_types, signature_data, active_pool, nonce, is_mainnet, action_type_code=None):
144177
signature_types.append("address")
145178
signature_types.append("uint64")
146179
if active_pool is None:
@@ -149,6 +182,10 @@ def sign_l1_action(wallet, signature_types, signature_data, active_pool, nonce,
149182
signature_data.append(active_pool)
150183
signature_data.append(nonce)
151184

185+
if action_type_code is not None:
186+
signature_types.append("uint16")
187+
signature_data.append(action_type_code)
188+
152189
phantom_agent = construct_phantom_agent(signature_types, signature_data, is_mainnet)
153190

154191
data = {

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
55

66
[tool.poetry]
77
name = "hyperliquid-python-sdk"
8-
version = "0.1.17"
8+
version = "0.1.18"
99
description = "SDK for Hyperliquid API trading with Python."
1010
readme = "README.md"
1111
authors = ["Hyperliquid <[email protected]>"]

0 commit comments

Comments
 (0)