1010from hyperliquid .info import Info
1111from hyperliquid .utils .constants import MAINNET_API_URL
1212from hyperliquid .utils .signing import (
13- ZERO_ADDRESS ,
1413 CancelRequest ,
1514 CancelByCloidRequest ,
1615 ModifyRequest ,
17- ModifySpec ,
1816 OrderRequest ,
19- OrderSpec ,
2017 OrderType ,
18+ OrderWire ,
2119 float_to_usd_int ,
2220 get_timestamp_ms ,
23- modify_spec_preprocessing ,
24- modify_spec_to_modify_wire ,
25- order_grouping_to_number ,
26- order_request_to_order_spec ,
27- order_spec_preprocessing ,
28- order_spec_to_order_wire ,
21+ order_request_to_order_wire ,
22+ order_wires_to_order_action ,
2923 sign_l1_action ,
3024 sign_usd_transfer_action ,
3125 sign_withdraw_from_bridge_action ,
3226 sign_agent ,
33- str_to_bytes16 ,
3427)
35- from hyperliquid .utils .types import Any , List , Literal , Meta , Optional , Tuple , Cloid
28+ from hyperliquid .utils .types import Any , List , Meta , Optional , Tuple , Cloid
3629
3730
3831class Exchange (API ):
@@ -108,43 +101,23 @@ def order(
108101 return self .bulk_orders ([order ])
109102
110103 def bulk_orders (self , order_requests : List [OrderRequest ]) -> Any :
111- order_specs : List [OrderSpec ] = [
112- order_request_to_order_spec (order , self .coin_to_asset [order ["coin" ]]) for order in order_requests
104+ order_wires : List [OrderWire ] = [
105+ order_request_to_order_wire (order , self .coin_to_asset [order ["coin" ]]) for order in order_requests
113106 ]
114-
115107 timestamp = get_timestamp_ms ()
116- grouping : Literal ["na" ] = "na"
117-
118- has_cloid = False
119- for order_spec in order_specs :
120- if "cloid" in order_spec ["order" ] and order_spec ["order" ]["cloid" ]:
121- has_cloid = True
122108
123- if has_cloid :
124- for order_spec in order_specs :
125- if "cloid" not in order_spec ["order" ] or not order_spec ["order" ]["cloid" ]:
126- raise ValueError ("all orders must have cloids if at least one has a cloid" )
127-
128- if has_cloid :
129- signature_types = ["(uint32,bool,uint64,uint64,bool,uint8,uint64,bytes16)[]" , "uint8" ]
130- else :
131- signature_types = ["(uint32,bool,uint64,uint64,bool,uint8,uint64)[]" , "uint8" ]
109+ order_action = order_wires_to_order_action (order_wires )
132110
133111 signature = sign_l1_action (
134112 self .wallet ,
135- signature_types ,
136- [[order_spec_preprocessing (order_spec ) for order_spec in order_specs ], order_grouping_to_number (grouping )],
137- ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
113+ order_action ,
114+ self .vault_address ,
138115 timestamp ,
139116 self .base_url == MAINNET_API_URL ,
140117 )
141118
142119 return self ._post_action (
143- {
144- "type" : "order" ,
145- "grouping" : grouping ,
146- "orders" : [order_spec_to_order_wire (order_spec ) for order_spec in order_specs ],
147- },
120+ order_action ,
148121 signature ,
149122 timestamp ,
150123 )
@@ -173,37 +146,33 @@ def modify_order(
173146 "cloid" : cloid ,
174147 },
175148 }
176- return self .bulk_modify_orders ([modify ])
149+ return self .bulk_modify_orders_new ([modify ])
177150
178- def bulk_modify_orders (self , modify_requests : List [ModifyRequest ]) -> Any :
179- modify_specs : List [ModifySpec ] = [
151+ def bulk_modify_orders_new (self , modify_requests : List [ModifyRequest ]) -> Any :
152+ timestamp = get_timestamp_ms ()
153+ modify_wires = [
180154 {
181155 "oid" : modify ["oid" ],
182- "order" : order_request_to_order_spec (modify ["order" ], self .coin_to_asset [modify ["order" ]["coin" ]]),
183- "orderType" : modify ["order" ]["order_type" ],
156+ "order" : order_request_to_order_wire (modify ["order" ], self .coin_to_asset [modify ["order" ]["coin" ]]),
184157 }
185158 for modify in modify_requests
186159 ]
187160
188- timestamp = get_timestamp_ms ()
189-
190- signature_types = ["(uint64,uint32,bool,uint64,uint64,bool,uint8,uint64,bytes16)[]" ]
161+ modify_action = {
162+ "type" : "batchModify" ,
163+ "modifies" : modify_wires ,
164+ }
191165
192166 signature = sign_l1_action (
193167 self .wallet ,
194- signature_types ,
195- [[modify_spec_preprocessing (modify_spec ) for modify_spec in modify_specs ]],
196- ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
168+ modify_action ,
169+ self .vault_address ,
197170 timestamp ,
198171 self .base_url == MAINNET_API_URL ,
199- action_type_code = 40 ,
200172 )
201173
202174 return self ._post_action (
203- {
204- "type" : "batchModify" ,
205- "modifies" : [modify_spec_to_modify_wire (modify_spec ) for modify_spec in modify_specs ],
206- },
175+ modify_action ,
207176 signature ,
208177 timestamp ,
209178 )
@@ -258,77 +227,75 @@ def cancel_by_cloid(self, coin: str, cloid: Cloid) -> Any:
258227
259228 def bulk_cancel (self , cancel_requests : List [CancelRequest ]) -> Any :
260229 timestamp = get_timestamp_ms ()
230+ cancel_action = {
231+ "type" : "cancel" ,
232+ "cancels" : [
233+ {
234+ "a" : self .coin_to_asset [cancel ["coin" ]],
235+ "o" : cancel ["oid" ],
236+ }
237+ for cancel in cancel_requests
238+ ],
239+ }
261240 signature = sign_l1_action (
262241 self .wallet ,
263- ["(uint32,uint64)[]" ],
264- [[(self .coin_to_asset [cancel ["coin" ]], cancel ["oid" ]) for cancel in cancel_requests ]],
265- ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
242+ cancel_action ,
243+ self .vault_address ,
266244 timestamp ,
267245 self .base_url == MAINNET_API_URL ,
268246 )
247+
269248 return self ._post_action (
270- {
271- "type" : "cancel" ,
272- "cancels" : [
273- {
274- "asset" : self .coin_to_asset [cancel ["coin" ]],
275- "oid" : cancel ["oid" ],
276- }
277- for cancel in cancel_requests
278- ],
279- },
249+ cancel_action ,
280250 signature ,
281251 timestamp ,
282252 )
283253
284254 def bulk_cancel_by_cloid (self , cancel_requests : List [CancelByCloidRequest ]) -> Any :
285255 timestamp = get_timestamp_ms ()
256+
257+ cancel_action = {
258+ "type" : "cancelByCloid" ,
259+ "cancels" : [
260+ {
261+ "asset" : self .coin_to_asset [cancel ["coin" ]],
262+ "cloid" : cancel ["cloid" ].to_raw (),
263+ }
264+ for cancel in cancel_requests
265+ ],
266+ }
286267 signature = sign_l1_action (
287268 self .wallet ,
288- ["(uint32,bytes16)[]" ],
289- [
290- [
291- (self .coin_to_asset [cancel ["coin" ]], str_to_bytes16 (cancel ["cloid" ].to_raw ()))
292- for cancel in cancel_requests
293- ]
294- ],
295- ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
269+ cancel_action ,
270+ self .vault_address ,
296271 timestamp ,
297272 self .base_url == MAINNET_API_URL ,
298273 )
274+
299275 return self ._post_action (
300- {
301- "type" : "cancelByCloid" ,
302- "cancels" : [
303- {
304- "asset" : self .coin_to_asset [cancel ["coin" ]],
305- "cloid" : cancel ["cloid" ].to_raw (),
306- }
307- for cancel in cancel_requests
308- ],
309- },
276+ cancel_action ,
310277 signature ,
311278 timestamp ,
312279 )
313280
314281 def update_leverage (self , leverage : int , coin : str , is_cross : bool = True ) -> Any :
315282 timestamp = get_timestamp_ms ()
316283 asset = self .coin_to_asset [coin ]
284+ update_leverage_action = {
285+ "type" : "updateLeverage" ,
286+ "asset" : asset ,
287+ "isCross" : is_cross ,
288+ "leverage" : leverage ,
289+ }
317290 signature = sign_l1_action (
318291 self .wallet ,
319- ["uint32" , "bool" , "uint32" ],
320- [asset , is_cross , leverage ],
321- ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
292+ update_leverage_action ,
293+ self .vault_address ,
322294 timestamp ,
323295 self .base_url == MAINNET_API_URL ,
324296 )
325297 return self ._post_action (
326- {
327- "type" : "updateLeverage" ,
328- "asset" : asset ,
329- "isCross" : is_cross ,
330- "leverage" : leverage ,
331- },
298+ update_leverage_action ,
332299 signature ,
333300 timestamp ,
334301 )
@@ -337,21 +304,21 @@ def update_isolated_margin(self, amount: float, coin: str) -> Any:
337304 timestamp = get_timestamp_ms ()
338305 asset = self .coin_to_asset [coin ]
339306 amount = float_to_usd_int (amount )
307+ update_isolated_margin_action = {
308+ "type" : "updateIsolatedMargin" ,
309+ "asset" : asset ,
310+ "isBuy" : True ,
311+ "ntli" : amount ,
312+ }
340313 signature = sign_l1_action (
341314 self .wallet ,
342- ["uint32" , "bool" , "int64" ],
343- [asset , True , amount ],
344- ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
315+ update_isolated_margin_action ,
316+ self .vault_address ,
345317 timestamp ,
346318 self .base_url == MAINNET_API_URL ,
347319 )
348320 return self ._post_action (
349- {
350- "type" : "updateIsolatedMargin" ,
351- "asset" : asset ,
352- "isBuy" : True ,
353- "ntli" : amount ,
354- },
321+ update_isolated_margin_action ,
355322 signature ,
356323 timestamp ,
357324 )
0 commit comments