55from hyperliquid .api import API
66from hyperliquid .info import Info
77from hyperliquid .utils .signing import (
8- OrderSpec ,
9- order_spec_preprocessing ,
10- order_grouping_to_number ,
11- sign_l1_action ,
128 ZERO_ADDRESS ,
9+ OrderSpec ,
1310 OrderType ,
14- order_spec_to_order_wire ,
11+ float_to_usd_int ,
1512 get_timestamp_ms ,
13+ order_grouping_to_number ,
14+ order_spec_preprocessing ,
15+ order_spec_to_order_wire ,
16+ sign_l1_action ,
1617)
17- from hyperliquid .utils .types import Meta , Any , Literal , Optional
18+ from hyperliquid .utils .types import Any , Literal , Meta , Optional
1819
1920
2021class Exchange (API ):
@@ -35,6 +36,16 @@ def __init__(
3536 self .meta = meta
3637 self .coin_to_asset = {asset_info ["name" ]: asset for (asset , asset_info ) in enumerate (self .meta ["universe" ])}
3738
39+ def _post_action (self , action , signature , nonce ):
40+ payload = {
41+ "action" : action ,
42+ "nonce" : nonce ,
43+ "signature" : signature ,
44+ "vaultAddress" : self .vault_address ,
45+ }
46+ logging .debug (payload )
47+ return self .post ("/exchange" , payload )
48+
3849 def order (
3950 self , coin : str , is_buy : bool , sz : float , limit_px : float , order_type : OrderType , reduce_only : bool = False
4051 ) -> Any :
@@ -58,18 +69,16 @@ def order(
5869 ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
5970 timestamp ,
6071 )
61- payload = {
62- "action" : {
72+
73+ return self ._post_action (
74+ {
6375 "type" : "order" ,
6476 "grouping" : grouping ,
6577 "orders" : [order_spec_to_order_wire (order_spec )],
6678 },
67- "nonce" : timestamp ,
68- "signature" : signature ,
69- "vaultAddress" : self .vault_address ,
70- }
71- logging .debug (payload )
72- return self .post ("/exchange" , payload )
79+ signature ,
80+ timestamp ,
81+ )
7382
7483 def cancel (self , coin : str , oid : int ) -> Any :
7584 timestamp = get_timestamp_ms ()
@@ -81,20 +90,59 @@ def cancel(self, coin: str, oid: int) -> Any:
8190 ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
8291 timestamp ,
8392 )
84- return self .post (
85- "/exchange" ,
93+ return self ._post_action (
94+ {
95+ "type" : "cancel" ,
96+ "cancels" : [
97+ {
98+ "asset" : asset ,
99+ "oid" : oid ,
100+ }
101+ ],
102+ },
103+ signature ,
104+ timestamp ,
105+ )
106+
107+ def update_leverage (self , leverage : int , coin : str , is_cross : bool = True ) -> Any :
108+ timestamp = get_timestamp_ms ()
109+ asset = self .coin_to_asset [coin ]
110+ signature = sign_l1_action (
111+ self .wallet ,
112+ ["uint32" , "bool" , "uint32" ],
113+ [asset , is_cross , leverage ],
114+ ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
115+ timestamp ,
116+ )
117+ return self ._post_action (
86118 {
87- "action" : {
88- "type" : "cancel" ,
89- "cancels" : [
90- {
91- "asset" : asset ,
92- "oid" : oid ,
93- }
94- ],
95- },
96- "nonce" : timestamp ,
97- "signature" : signature ,
98- "vaultAddress" : self .vault_address ,
119+ "type" : "updateLeverage" ,
120+ "asset" : asset ,
121+ "isCross" : is_cross ,
122+ "leverage" : leverage ,
99123 },
124+ signature ,
125+ timestamp ,
126+ )
127+
128+ def update_isolated_margin (self , amount : float , coin : str ) -> Any :
129+ timestamp = get_timestamp_ms ()
130+ asset = self .coin_to_asset [coin ]
131+ amount = float_to_usd_int (amount )
132+ signature = sign_l1_action (
133+ self .wallet ,
134+ ["uint32" , "bool" , "int64" ],
135+ [asset , True , amount ],
136+ ZERO_ADDRESS if self .vault_address is None else self .vault_address ,
137+ timestamp ,
138+ )
139+ return self ._post_action (
140+ {
141+ "type" : "updateIsolatedMargin" ,
142+ "asset" : asset ,
143+ "isBuy" : True ,
144+ "ntli" : amount ,
145+ },
146+ signature ,
147+ timestamp ,
100148 )
0 commit comments