20
20
get_timestamp_ms ,
21
21
order_request_to_order_wire ,
22
22
order_wires_to_order_action ,
23
+ sign_approve_builder_fee ,
23
24
sign_l1_action ,
25
+ sign_usd_class_transfer_action ,
24
26
sign_usd_transfer_action ,
25
27
sign_spot_transfer_action ,
26
28
sign_withdraw_from_bridge_action ,
27
29
sign_agent ,
28
30
)
29
- from hyperliquid .utils .types import Any , List , Meta , SpotMeta , Optional , Tuple , Cloid
31
+ from hyperliquid .utils .types import Any , List , Meta , SpotMeta , Optional , Tuple , Cloid , BuilderInfo
30
32
31
33
32
34
class Exchange (API ):
@@ -87,6 +89,7 @@ def order(
87
89
order_type : OrderType ,
88
90
reduce_only : bool = False ,
89
91
cloid : Optional [Cloid ] = None ,
92
+ builder : Optional [BuilderInfo ] = None ,
90
93
) -> Any :
91
94
order : OrderRequest = {
92
95
"coin" : name ,
@@ -98,15 +101,15 @@ def order(
98
101
}
99
102
if cloid :
100
103
order ["cloid" ] = cloid
101
- return self .bulk_orders ([order ])
104
+ return self .bulk_orders ([order ], builder )
102
105
103
- def bulk_orders (self , order_requests : List [OrderRequest ]) -> Any :
106
+ def bulk_orders (self , order_requests : List [OrderRequest ], builder : Optional [ BuilderInfo ] = None ) -> Any :
104
107
order_wires : List [OrderWire ] = [
105
108
order_request_to_order_wire (order , self .info .name_to_asset (order ["coin" ])) for order in order_requests
106
109
]
107
110
timestamp = get_timestamp_ms ()
108
111
109
- order_action = order_wires_to_order_action (order_wires )
112
+ order_action = order_wires_to_order_action (order_wires , builder )
110
113
111
114
signature = sign_l1_action (
112
115
self .wallet ,
@@ -184,11 +187,14 @@ def market_open(
184
187
px : Optional [float ] = None ,
185
188
slippage : float = DEFAULT_SLIPPAGE ,
186
189
cloid : Optional [Cloid ] = None ,
190
+ builder : Optional [BuilderInfo ] = None ,
187
191
) -> Any :
188
192
# Get aggressive Market Price
189
193
px = self ._slippage_price (name , is_buy , slippage , px )
190
194
# Market Order is an aggressive Limit Order IoC
191
- return self .order (name , is_buy , sz , px , order_type = {"limit" : {"tif" : "Ioc" }}, reduce_only = False , cloid = cloid )
195
+ return self .order (
196
+ name , is_buy , sz , px , order_type = {"limit" : {"tif" : "Ioc" }}, reduce_only = False , cloid = cloid , builder = builder
197
+ )
192
198
193
199
def market_close (
194
200
self ,
@@ -197,6 +203,7 @@ def market_close(
197
203
px : Optional [float ] = None ,
198
204
slippage : float = DEFAULT_SLIPPAGE ,
199
205
cloid : Optional [Cloid ] = None ,
206
+ builder : Optional [BuilderInfo ] = None ,
200
207
) -> Any :
201
208
address = self .wallet .address
202
209
if self .account_address :
@@ -215,7 +222,16 @@ def market_close(
215
222
# Get aggressive Market Price
216
223
px = self ._slippage_price (coin , is_buy , slippage , px )
217
224
# Market Order is an aggressive Limit Order IoC
218
- return self .order (coin , is_buy , sz , px , order_type = {"limit" : {"tif" : "Ioc" }}, reduce_only = True , cloid = cloid )
225
+ return self .order (
226
+ coin ,
227
+ is_buy ,
228
+ sz ,
229
+ px ,
230
+ order_type = {"limit" : {"tif" : "Ioc" }},
231
+ reduce_only = True ,
232
+ cloid = cloid ,
233
+ builder = builder ,
234
+ )
219
235
220
236
def cancel (self , name : str , oid : int ) -> Any :
221
237
return self .bulk_cancel ([{"coin" : name , "oid" : oid }])
@@ -384,6 +400,22 @@ def create_sub_account(self, name: str) -> Any:
384
400
timestamp ,
385
401
)
386
402
403
+ def usd_class_transfer (self , amount : float , to_perp : bool ) -> Any :
404
+ timestamp = get_timestamp_ms ()
405
+ action = {
406
+ "type" : "usdClassTransfer" ,
407
+ "amount" : str (amount ),
408
+ "toPerp" : to_perp ,
409
+ "nonce" : timestamp ,
410
+ }
411
+ signature = sign_usd_class_transfer_action (self .wallet , action , self .base_url == MAINNET_API_URL )
412
+ return self ._post_action (
413
+ action ,
414
+ signature ,
415
+ timestamp ,
416
+ )
417
+
418
+ # Deprecated in favor of usd_class_transfer
387
419
def user_spot_transfer (self , usdc : float , to_perp : bool ) -> Any :
388
420
usdc = int (round (usdc , 2 ) * 1e6 )
389
421
timestamp = get_timestamp_ms ()
@@ -427,22 +459,21 @@ def sub_account_transfer(self, sub_account_user: str, is_deposit: bool, usd: int
427
459
signature ,
428
460
timestamp ,
429
461
)
430
-
462
+
431
463
def vault_usd_transfer (self , vault_address : str , is_deposit : bool , usd : int ) -> Any :
432
464
timestamp = get_timestamp_ms ()
433
465
vault_transfer_action = {
434
466
"type" : "vaultTransfer" ,
435
467
"vaultAddress" : vault_address ,
436
468
"isDeposit" : is_deposit ,
437
- "usd" : usd }
469
+ "usd" : usd ,
470
+ }
438
471
is_mainnet = self .base_url == MAINNET_API_URL
439
472
signature = sign_l1_action (self .wallet , vault_transfer_action , None , timestamp , is_mainnet )
440
- return (
441
- self ._post_action (
442
- vault_transfer_action ,
443
- signature ,
444
- timestamp ,
445
- )
473
+ return self ._post_action (
474
+ vault_transfer_action ,
475
+ signature ,
476
+ timestamp ,
446
477
)
447
478
448
479
def usd_transfer (self , amount : float , destination : str ) -> Any :
@@ -458,8 +489,13 @@ def usd_transfer(self, amount: float, destination: str) -> Any:
458
489
459
490
def spot_transfer (self , amount : float , destination : str , token : str ) -> Any :
460
491
timestamp = get_timestamp_ms ()
461
- action = {"destination" : destination , "amount" : str (
462
- amount ), "token" : token , "time" : timestamp , "type" : "spotSend" }
492
+ action = {
493
+ "destination" : destination ,
494
+ "amount" : str (amount ),
495
+ "token" : token ,
496
+ "time" : timestamp ,
497
+ "type" : "spotSend" ,
498
+ }
463
499
is_mainnet = self .base_url == MAINNET_API_URL
464
500
signature = sign_spot_transfer_action (self .wallet , action , is_mainnet )
465
501
return self ._post_action (
@@ -502,3 +538,10 @@ def approve_agent(self, name: Optional[str] = None) -> Tuple[Any, str]:
502
538
),
503
539
agent_key ,
504
540
)
541
+
542
+ def approve_builder_fee (self , builder : str , max_fee_rate : str ) -> Any :
543
+ timestamp = get_timestamp_ms ()
544
+
545
+ action = {"maxFeeRate" : max_fee_rate , "builder" : builder , "nonce" : timestamp , "type" : "approveBuilderFee" }
546
+ signature = sign_approve_builder_fee (self .wallet , action , self .base_url == MAINNET_API_URL )
547
+ return self ._post_action (action , signature , timestamp )
0 commit comments