
    E#i                    T   S SK Jr  S SKJr  S SKrS SKrS SKJrJrJ	r	J
r
JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJ r J!r!  S SK"J#r#  S SK$J%r%  S SK$J&r&  S SK$J'r'  S S	K$J(r(  S S
K$J)r)  S SK$J*r*  S SK$J+r+  S SK,J-r-  S SK,J.r.  S SK,J/r/  S SK,J0r0  S SK1J2r2   " S S\\5      r3g)    )Exchange)ImplicitAPIN)AnyBalances
CurrenciesCurrencyIntLedgerEntryMarginModificationMarketNumOrder	OrderBookOrderRequestCancellationRequest	OrderSide	OrderTypePositionStrStringsTickerTickersFundingRateFundingRatesTradeTradingFeeInterfaceTransactionMarketInterfaceTransferEntry)List)ExchangeError)ArgumentsRequired)
BadRequest)InsufficientFunds)InvalidOrder)OrderNotFound)NotSupported)ROUND)DECIMAL_PLACES)SIGNIFICANT_DIGITS)	TICK_SIZE)Precisec                   	  ^  \ rS rSrS\4U 4S jjrU 4S jrS\S\4U 4S jjr	SS\
S	\S
\
S\
S\4
U 4S jjjr0 4S\4S jjr0 4S\\   4S jjr0 4S\\   4S jjr0 4S\\   4S jjrS\S\S\4S jr0 4S\\   4S jjrS	\S\4S jr0 4S\4S jjrS0 4S\S\S\4S jjrS0 4S\S\4S jjrS0 4S\S\ 4S jjr!SS	\S\"4S jjr#SS\S	\S\$4S jjr%S SS0 4S\S!\S"\S\S\\&   4
S# jjr'SS	\S\&4S$ jjr(SS0 4S\
S"\S\4S% jjr)S& r*S\S\4S' jr+S( r,S) r-S* r.SS+ jr/S, r0SS\14S- jjr2S. r3S/ r4S0 r5S1 r6S2 r7S3 r8S4 r9S5\S6\4S7 jr:S8 r;S9 r<0 4S:\=4S; jjr>S0 4S\S<\?S=\@S>\S\A4
S? jjrB0 4S@\\C   4SA jjrDS0 4S\S<\?S=\@S>\S\
4
SB jjrE0 4S\4SC jjrFS0 4SD\S\
4SE jjrGS0 4SF\\   S\
4SG jjrHS0 4SF\\   S\
S\4SH jjrI0 4S@\\J   4SI jjrK0 4SJ\4SK jjrL0 4SL jrMSS0 4SD\S\S<\S=\S>\AS\A4SM jjrN0 4S@\\C   4SN jjrO0 4SO\SP\SQ\P4SR jjrQSSS0 4S\
S"\S\4SS jjrRSSS0 4S\
S"\S\S\\S   4ST jjrTSSS0 4S\
S"\S\S\\S   4SU jjrUSSS0 4S\
S"\S\S\\S   4SV jjrVSSS0 4S\
S"\S\S\\S   4SW jjrWSSS0 4S\
S"\S\S\\S   4SX jjrXS0 4SD\S\
4SY jjrYSSZ\S	\S\S4S[ jjrZS\\
4S] jr[S^ r\SSS0 4S\
S"\S\4S_ jjr]SS`\S	\S\^4Sa jjr_0 4S\4Sb jjr`S0 4S\S\\a   4Sc jjrbSSd\S	\4Se jjrcS0 4Sf\S\
4Sg jjrdS0 4Sh\PS\
4Si jjre0 4S\S>\S\f4Sj jjrg0 4S\S>\S\f4Sk jjrh0 4S\S\f4Sl jjriSSm\S	\S\f4Sn jjrj0 4So\S>\Sp\Sq\S\k4
Sr jjrlSSs\St\mS\k4Su jjrnS0 4So\S>\Sv\Sw\
S\o4
Sx jjrpSSy\St\mS\o4Sz jjrq0 4S\S\r4S{ jjrsSS|\S	\S\r4S} jjrtSSS0 4So\
S"\S\S\\u   4S~ jjrvSS\St\mS\u4S jjrwS rxSSS0 4So\
S"\S\4S jjrySSS0 4So\
S"\S\S\\o   4S jjrzS0 4S\4S jjr{0 4S\4S jjr|SS	\4S jjr}SSS0 4S\
S"\S\4S jjr~SS	\4S jjr0 4S\AS\4S jjr/ 4S jrSSv\
4S jjrS\S\4S jrS\
4S jrSo\PS\S\S\S\S\4S jrSS0 SS4S jr0 4S jrS0 4SD\
S\S<\?S=\@S>\S\A4S jjrSrU =r$ )hyperliquid   returnc                   > U R                  [        [        U ]  5       0 SS_SS_S/ _SS_SS	_S
S_SS_SS_S0 SS _SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_S S_0 S!S_S"S_S#S_S$S_S%S_S&S_S'S_S(S_S)S_S*S_S+S_S,S_S-S_S.S_S/S_S0S_S1S_E0 S2S_S3S_S4S_S5S_S6S_S7S_S8S_S9S:_S;S_S<S_S=S_S>S_S?S_S@S_SAS_SBS_SCS_E0 SDS_SES_SFS_SGS _SHS_SIS_SJS_SKS_SLS_SMS_SNS_SOS_SPS_SQS_SRS_SSS_STS_E0 SUS_SVS_SWS_SXS_SYS_SZS_S[S:_S\S_S]S_S^S_S_S_S`S_SaS_SbS_ScS_SdS_SeS_ESSSSSSSSSf.E_SgShSiSjSkSlSmSnSoSpSqSrSsStSuSv._SwSx_SySzS{S{S|.S}S}S|.S~SSSS._SSSSSSSSSSSS.S.00SSS00S|._SU R	                  S5      U R	                  S5      S.U R	                  S5      U R	                  S5      S.S._SSSSSS._S0 0 S[
        _S[
        _S[        _S[
        _S[
        _S[
        _S[
        _S[
        _S[        _S[
        _S[
        _S[
        _S[
        _S[        _S[        _S[        _S._S[        _0 SSSSSSSSSSSSSSSSS.S/ SQ0S.SSSS SSSSSSS.SSSS.SSSSS.SSSSSSSS.SS0SSS S SS.SSSSS.SSSSSS.SSS S SSSS.SSS S S SSSS.SS0S.	SS0SSSS S.S.SS0SS0S.SS0SS0S.S.S.E5      $ )Nidr.   nameHyperliquid	countriesversionv1	rateLimit2   	certifiedTprodexhasCORSspotmarginFswapfutureoption	addMarginborrowCrossMarginborrowIsolatedMargincancelAllOrderscancelAllOrdersAftercancelOrdercancelOrderscancelOrdersForSymbolscloseAllPositionsclosePositioncreateMarketBuyOrderWithCostcreateMarketOrderWithCostcreateMarketSellOrderWithCostcreateOrdercreateOrders$createOrderWithTakeProfitAndStopLosscreateReduceOnlyOrdercreateStopOrdercreateTriggerOrder	editOrder
editOrdersfetchAccountsfetchBalancefetchBorrowInterestfetchBorrowRateHistoriesfetchBorrowRateHistoryfetchCanceledAndClosedOrdersfetchCanceledOrdersfetchClosedOrdersfetchCrossBorrowRatefetchCrossBorrowRatesfetchCurrenciesfetchDepositAddressfetchDepositAddressesfetchDepositsfetchDepositWithdrawFeeemulatedfetchDepositWithdrawFeesfetchFundingHistoryfetchFundingRatefetchFundingRateHistoryfetchFundingRatesfetchIndexOHLCVfetchIsolatedBorrowRatefetchIsolatedBorrowRatesfetchLedgerfetchLeveragefetchLeverageTiersfetchLiquidationsfetchMarginModefetchMarketLeverageTiersfetchMarketsfetchMarkOHLCVfetchMyLiquidationsfetchMyTrades
fetchOHLCVfetchOpenInterestfetchOpenInterestHistoryfetchOpenInterestsfetchOpenOrders
fetchOrderfetchOrderBookfetchOrdersfetchOrderTradesfetchPositionfetchPositionModefetchPositionsfetchPositionsRiskfetchPremiumIndexOHLCVfetchTickerfetchTickers	fetchTimefetchTradesfetchTradingFeefetchTradingFeesfetchTransferfetchTransfersfetchWithdrawalfetchWithdrawalsreduceMargin)repayCrossMarginrepayIsolatedMarginsandboxsetLeveragesetMarginModesetPositionModetransferwithdraw
timeframes1m3m5m15m30m1h2h4h8h12h1d3d1w1M)r   r   r   r   r   r   r   r   r   r   r   r   r   r   hostnamezhyperliquid.xyzurlszQhttps://github.com/ccxt/ccxt/assets/43336371/b371bc6c-4a8c-489f-87f4-20a913dd8d4bzhttps://api.{hostname})publicprivatez#https://api.hyperliquid-testnet.xyzzhttps://hyperliquid.xyzzBhttps://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/apiz<https://hyperliquid.gitbook.io/hyperliquid-docs/trading/feeszhttps://app.hyperliquid.xyz/)logoapitestwwwdocfeesreferralr   postinfo         )l2BookallMidsclearinghouseStateorderStatusspotClearinghouseStateexchangeStatuscandleSnapshot)costbyTypeexchange   r   z0.00045z0.00015)takermakerz0.0007z0.0004)rA   r?   requiredCredentials)apiKeysecretwalletAddress
privateKey
exceptionsz%Price must be divisible by tick size.z$Order must have minimum value of $10z#Insufficient margin to place order.z*Reduce only order would increase position.z/Post only order would have immediately matched,z=Order could not immediately match against any resting orders.zInvalid TP/SL price.z(No liquidity available for market order.z4Order was never placed, already canceled, or filled.zUser or API Wallet zOrder has invalid sizezAOrder price cannot be more than 80% away from the reference pricezOrder has zero size.zInsufficient spot balance assetz#Insufficient balance for withdrawalz'Insufficient balance for token transfer)exactbroadprecisionModeg?*0x00000000000000000000000000000000000000002ZBONKBTCETHFARTCOINPENGUPUMPSOLSPXUSDTXAUTXPL)UDZUBONKUBTCUETHUFARTHPENGUUPUMPUSOLUUUSPXUSDT0XAUT0UXPLtypes)r?   rA   hip3)defaultTypesandboxModedefaultSlippagezeroAddressspotCurrencyMappingrw   )lastmarkindex)triggerPriceTypetriggerPricetypeprice)IOCFOKPOGTD)
marginModer   r   triggerDirectionstopLossPricetakeProfitPriceattachedStopLossTakeProfittimeInForcehedgedtrailingleveragemarketBuyByCostmarketBuyRequiresPriceselfTradePreventionicebergmax  i  )r   limitdaysBack	untilDayssymbolRequired)r   triggerr  r  )r   r  r  r  r  )r   r  r  r  r  r  r  )r   r  r  daysBackCanceledr  r  r  r  r  i  )	r   rQ   rR   rz   r   r   r   r`   r{   extendsdefault)r   r   r   )r  rQ   forPerps)linearinverse)r  r?   r  rA   rB   )commonCurrenciesoptionsfeatures)	deep_extendsuperr.   describeparse_numberr%   r$   r&   r+   )self	__class__s    X/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/async_support/hyperliquid.pyr  hyperliquid.describe   s	   k4 A C OF
-OF
MOF
 OF
 t	OF

 OF
 OF
 4OF
 4OF
  ^^^ %^ 	^
 $^ %^ T^ $U^ '^ "5^ '^ t^ ^ )$^ $U^   !^" /#^$ ,U%^& 0'^( t)^* +^, 7-^. (/^0 "41^2 %d3^4 T5^6 d7^8  9^: ;^< &u=^> +E?^@ )%A^B /C^D &tE^F $TG^H 'I^J (K^L "4M^N &uO^P (Q^R  S^T *:U^V +EW^X &tY^Z #E[^\ *4]^^ $T_^` "5a^b *5c^d +Ee^f tg^h  i^j %ek^l $Um^n "4o^p +Eq^r s^t !%u^v &uw^x  y^z d{^| $T}^~ +E^@ %dA^B "4C^D dE^F !$G^H tI^J #EK^L  M^N $UO^P !$Q^R %eS^T )%U^V zW^X Y^Z U[^\ t]^^ "4_^` #Ea^b  c^d !%e^f "5g^h #Di^j k^l %*',#!%#(  {^OF
P QOF
p )qOF
r k67
 DD 1[V:sOF
P $&*++,67/0:;2323'!" "A#QOF
~ !..y9!..y9
 "..x8!..x8	OF
R "!%"	$SOF
^ ;\:L :;L A,	
 F| TUa +L ? KM *< -l XYe +L 67H :;L  >?P!_OF
J YKOF
L!  &$#'K#!!'%#!###!( 5!':  $&+(-,0,1).+0 ).(-).1
 -1$(%)	7 $(#("&#(	( #($)$)+027/4#(;$@ t% ',!%$(%)*.& ',#($)*.	# ',!%#($)*.( ',!%$(%)#($)*.$ ',!%$(,0%)#($)*.	*  #WN` y  ))-+/6:$ ": ": 	 ": ": 	GkGOF
 O O	    c                 H   > [         [        U ]  U5        XR                  S'   g )Nr   )r  r.   set_sandbox_moder  )r  enabledr  s     r  r#  hyperliquid.set_sandbox_moden  s    k41':&-]#r!  symbolc                   > U R                   c  [        U R                  S-   5      eXR                   ;   a  U R                   U   nUS   (       a  U R                  US5      nU R	                  U R
                  S0 5      nX4;   aX  U R                  XC5      nU R                  US5      nU R                  U5      S-   U-   nXpR                   ;   a  U R                   U   $ [        [        U ]'  U5      nU$ )Nz markets not loadedr?   baseNamer   quote/)
marketsr!   r2   safe_string	safe_dictr  safe_currency_coder  r.   market)
r  r&  r/  r(  r   unifiedBaseNamer)  	newSymbolresr  s
            r  r/  hyperliquid.marketr  s    <<*? ?@@\\!\\&)Ff~++FJ?&*nnT\\CXZ\&]#2&*&6&67J&UO ,,VW=E $ 7 7 H3 NQV VI LL0#||I66K-f5
r!  NmarketIdr/  	delimiter
marketTypec                   > Ub  U R                   b  XR                   ;   a  U R                   U   n[        U5      nUS:X  a  US   $ US:  a  [        U R                  S-   U-   5      eUS   nUS   nU R	                  US5      U R	                  US5      :w  a  [        U R                  S-   U-   5      eU R	                  US5      n	U R                  U R                  S0 5      n
X;   a  U$ U$ [        [        U ]'  XX45      $ )	Nr   r   r   zB safeMarket() found more than two markets with the same market id r   zG safeMarket() found two different market types with the same market id baser   )
markets_by_idlenr!   r2   r,  r-  r  r  r.   safe_market)r  r4  r/  r5  r6  r+  
numMarketsfirstMarketsecondMarketbaseCurrencyr   r  s              r  r;  hyperliquid.safe_market  s(   "".XASAS5S,,X6 \
?"1:%!A~+DGG6z,z  ~F  -F  G  G")!*K#*1:L''V<@P@PQ]_e@ff+DGG6,  CK  -K  L  L#'#3#3K#HL*...G\^`*a'#:++&&[$3Hi\\r!  c                   #    U R                  S5      (       a  U R                  5       I Sh  vN   SS0nU R                  U R                  X!5      5      I Sh  vN nU R	                  US/ 5      n0 n[        S[        U5      5       Hc  nU R                  XF0 5      nUnU R                  US5      n	U R                  U	5      n
U R                  UU	U
SUSSSSSSSSS	.SSS	.S
.S.5      XZ'   Me     U$  N N7f)a0  
fetches all available currencies on an exchange

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-metadata

:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: an associative dictionary of currencies
FNr   metauniverser   r3   cryptominr	  )amountr   )r2   r3   code	precisionr   activedepositr   networksfeer   limits)check_required_credentialsinitialize_clientpublicPostInfoextend	safe_listranger:  r-  r,  r.  safe_currency_structure)r  paramsrequestresponserB  resultidatar2   r3   rH  s              r  fetch_currencieshyperliquid.fetch_currencies  s#     **511((***F
 ,,T[[-IJJ ~~h
B7q#d)$A>>$2.DB##D&1D**40D77!     $#
  $#!	9 FL %8 c + Ks"   *C4C0+C4C2BC42C4c                 >  #    U R                  U R                  S0 5      nU R                  US5      n/ n[        S[	        U5      5       H  nX5   nUS:X  a"  UR                  U R                  U5      5        M/  US:X  a"  UR                  U R                  U5      5        MW  US:X  d  M_  UR                  U R                  U5      5        M     [        R                  " U6 I Sh  vN n/ n[        S[	        U5      5       H  nU R                  XU   5      nM     U$  N87f)a  
retrieves data on all markets for hyperliquid

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-asset-contexts-includes-mark-price-current-funding-open-interest-etc
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-spot-asset-contexts

:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: an array of objects representing market data
rw   r   r   rA   r?   r   N)r-  r  rS  rT  r:  appendfetch_swap_marketsfetch_spot_marketsfetch_hip3_marketsasynciogatherarray_concat)	r  rV  r  r   rawPromisesrZ  r6  promisesrY  s	            r  fetch_marketshyperliquid.fetch_markets  s      ..~rBw0q#e*%AJV#""4#:#:6#BCv%""4#:#:6#BCv%""4#:#:6#BC & !55q#h-(A&&v{;F )	 6s   B"D(:D"D#9Dc           
      r  #    U R                  SS05      I Sh  vN n0 n[        S[        U5      5       H  nX$   nSUS-
  S-  -   nXcUS   '   M     / nU R                  U R                  S0 5      nU R                  US	0 5      n	U R                  U	S
S5      n
[        U5      nX:  aL  U R                  U	S/ 5      n[        U5      S:X  a'  [        U R                  S-   [        U
5      -   S-   5      eUnOQ[        S[        U5      5       H8  nU R                  X$0 5      nU R                  US5      nUR                  U5        M:     / n[        S[        U5      5       HF  nSU R                  Xt5      S.nUR                  U R                  U R                  X5      5      5        MH     [        R                  " U6 I Sh  vN n/ n[        S[        U5      5       H  nXt   nX=   nUU   nU R                  US0 5      nU R                  US/ 5      nU R                  US/ 5      n/ n[        S[        U5      5       HP  nU R                  U R                  UU0 5      U R                  UU0 5      5      nUU-   US'   UR                  U5        MR     U R                  UU R!                  U5      5      nM     U$  GN N7f)a  
retrieves data on all hip3 markets for hyperliquid

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-all-perpetual-dexs
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-asset-contexts-includes-mark-price-current-funding-open-interest-etc

:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: an array of objects representing market data
r   perpDexsNr   i '  r3   rw   r   r     r<   r   z fetchHip3Markets() Too many DEXes found. Please specify a list of DEXes in the exchange.options["fetchMarkets"]["hip3"]["dex"] parameter to fetch markets from those DEXes only. The limit is set to z DEXes by default.metaAndAssetCtxs)r   r<   rC  baseId)rQ  rT  r:  r-  r  safe_integerrS  r"   r2   strr,  r_  rR  rc  rd  re  parse_markets)r  rV  
fetchDexesperpDexesOffsetrZ  r<   offsetfetchDexesListr  r   defaultLimitdexesLengthdefaultDexesdexNamerf  rW  rg  r+  rX  rB  rC  	assetCtxsrY  jr[  s                            r  rb  hyperliquid.fetch_hip3_markets  s      ..J0
  
* !#q#j/*A-Cq1uo-F+1CK(	 +
 ..~rB~~gvr2((w:*o&>>$r:L< A%'  3{  ){  ~A  BN  ~O  )O  Rf  )f  g  g!-1c*o.nnZB7**37%%g. / q#n-.A*'':G t224;;w3OPQ / !55q#h-(A$'G$-F{H>>(Ar2D~~dJ;HxB7IF1c(m,{{NN8Q3NN9a4 "#VXd# - ''1C1CF1KLG )^ Gd 6s#   J7J2F!J7;J5<C7J75J7c           	        #    SS0nU R                  U R                  X!5      5      I Sh  vN nU R                  US0 5      nU R                  US/ 5      nU R                  US/ 5      n/ n[	        S[        U5      5       HJ  nU R                  U R                  XX0 5      U R                  Xh0 5      5      n	XS'   UR                  U	5        ML     U R                  U5      $  N7f)av  
retrieves data on all swap markets for hyperliquid

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-asset-contexts-includes-mark-price-current-funding-open-interest-etc

:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: an array of objects representing market data
r   rn  Nr   rC  r   ro  )rQ  rR  r-  rS  rT  r:  r_  rr  )
r  rV  rW  rX  rB  rC  r{  rY  rZ  r[  s
             r  r`  hyperliquid.fetch_swap_markets_  s      &
 ,,T[[-IJJ@ ~~h2.>>$
B7NN8Q3	q#h-(A;;xB/yR0D NMM$ ) !!&))W Ks   (C CB4C r   amountPrecisionmaxDecimalsc                    SnU R                  U5      nUc  gUR                  S5      n[        R                  " US5      (       a  SnSn[	        X2-
  Xx-
  5      nO[        R
                  " US5      (       a{  [        R                  " US5      (       a_  SnU R                  USS5      n	Sn
U
[        U	5      ::  a&  X   S:X  a  U
S-   n
U
[        U	5      ::  a
  X   S:X  a  M  X-   n[	        X2-
  U5      nOBU R                  USS5      n[        S[        U5      5      n[	        X2-
  U[        U5      -
  5      nU R                  U5      $ )a>  
Helper function to calculate the Hyperliquid DECIMAL_PLACES price precision
:param float price: the price to use in the calculation
:param int amountPrecision: the amountPrecision to use in the calculation
:param int maxDecimals: the maxDecimals to use in the calculation
:returns int: The calculated price precision
r   .0rm  1r    )number_to_stringsplitr,   	string_eqrF  	string_gt	string_ltr,  r:  r	  parse_to_int)r  r   r  r  pricePrecisionpriceStrpriceSplittedsignificantDigitsintegerDigitsdecimalPartleadingZerosintegerParts               r  calculate_price_precision%hyperliquid.calculate_price_precision  sO    ((/ s+Xs++ !M !>@Q@abNx--'2C2CHc2R2R !**=!R@KL3{#33+:SWZ:Z+a/  3{#33+:SWZ:Z *=N !>ON **=!R@K #As;'7 8 !>@QTWXcTd@deN  00r!  c                   #    SS0nU R                  U R                  X!5      5      I Sh  vN nU R                  US0 5      nU R                  US/ 5      nU R                  US/ 5      nU R                  US/ 5      n/ n[	        S[        U5      5       GHG  n	U R                  Xi0 5      n
U R                  U
S5      nU R                  X[0 5      nU R                  U
S	5      nU R                  U R                  S
0 5      nU R                  US5      nU R                  US5      nU R                  U
S/ 5      nU R                  US5      nU R                  US5      nU R                  UU0 5      nU R                  UU0 5      nU R                  US	5      nU R                  US	5      nU R                  U R                  S0 5      nU R                  UUU5      nU R                  UUU5      nU R                  U5      nU R                  U5      nUS-   U-   nU R                  USU5      nU R                  US5      n[        U5      n U R                  US5      n!Sn"U!b  U R                  U!U S5      n"U R                  U"5      n#U R                  US-   5      n$0 SU_SU_SU_SU_SS_SU$_SU_SU_SS_SS
_S
S_SS_SS_S S!_S"S!_S#S!_S$S_S!SSUUSSSSSU R                  U R!                  U5      5      U R                  U R!                  U#5      5      S%.SSS&.SSS&.SSS&.U R                  S'5      SS&.S(.SU R                  X5      S).En%UR#                  U R%                  U%5      5        U R                  U5      n&U R                  U5      n'U R                  0 U%5      n(U&S-   U'-   n)U)U:w  d  GM  U)U(S'   U&U(S'   U'U(S'   UU(S'   UR#                  U R%                  U(5      5        GMJ     U$  GN7f)*a4  
retrieves data on all spot markets for hyperliquid

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-spot-asset-contexts

:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: an array of objects representing market data
r   spotMetaAndAssetCtxsNr   r   rC  tokensr   r3   r?   r   r   r   r*  spec
szDecimalsmidPx   rl  r2   r&  r8  r)  settlero  r(  quoteIdsettleIdTsubTyper@   rA   FrB   rC   rJ  rG  r   rE  10r  rG  r   r   )contractr  r  r   r   contractSizeexpiryexpiryDatetimestrike
optionTyperI  rN  createdr   )rQ  rR  r-  rS  rT  r:  rp  r,  r   safe_numberr  r.  intr  r  r  parse_precisionr_  safe_market_structure)*r  rV  rW  rX  firstsecondrB  r  r+  rZ  r/  r   	extraData
marketNamer   r   r   	tokensPosbaseTokenPosquoteTokenPosbaseTokenInfoquoteTokenInfor(  r  r   mappedBaseNamemappedQuoteId
mappedBasemappedQuotemappedSymbolinnerBaseTokenInfoamountPrecisionStrr  r   r  pricePrecisionStrro  entryr8  r)  newEntryr&  s*                                             r  ra  hyperliquid.fetch_spot_markets  s     *
 ,,T[[-IJJZ xB/!R0~~eZ4x4q#d)$A^^DR0F%%fg6Evb9I))&&9J >>$))VR8D$$T73E$$T73Evx<I,,Y:L --i;M NN6<DM!^^FM2FN''v>H&&~v>G"&..?TVX"Y!--.A8XVN ,,-@'7SM00@J11-@K%+k9L!%v}!U!%!1!12Dl!S!"45O$$Y8EN !%!?!?XY!Z $ 5 5n E**55=9F4j4,4 
4 	4
 $4 &4 H4 74 D4 4 4 44 $4 4 %4  %!4" $#4$ " $"&""//0D0DEW0XY!..t/C/CDU/VW  $#!
  $#
  $#
  $006#$  I6g4Ej NN455e<=**84D++G4E{{2u-HCZ%'F%%+"#' $)!'/$t99(CDQ %R w Ks   (O#O M5O#%<O#c                 0   SnU R                  US5      nU R                  U5      nU R                  U5      nU R                  US5      nSnU R                  U5      nUR                  SS5      S-   U-   n	Sn
SnU
(       a  U(       a  U	S-   U-   n	U R                  U R                  S0 5      nU R                  US	5      nU R                  US
5      nU R                  US5      n[        U5      nU R                  USS5      nSnUb  U R                  UUS5      nU R                  U5      nU R                  US5      nSnUb  U(       + nU R                  0 SU_SU	_SU_SU_SU_SU_SU_SU_SU_SS_SS_SS _SU_SS_SS_SU_SU
_SSUUU R                  S 5      S S S S U R                  U R                  U5      5      U R                  U R                  U5      5      S!.S U R                  US"5      S#.S S S#.S S S#.U R                  S$5      S S#.S%.S US&.E5      $ )'NUSDCr3   ro  :-r*  TrA   r   r   r  markPxr      
isDelistedr2   r&  r8  r)  r  r(  r  r  r   r?   Fr@   rB   rC   rJ  r  r  r  maxLeveragerE  r  r  )r  r  r   r   r  r  r  r  r  rI  rN  r  r   )r,  r.  replacer-  r   r  r  r  r  	safe_boolr  r  r  rp  )r  r/  r  r(  r8  r)  ro  r  r  r&  r  rA   r   r   r   r  r  r   r  r  r  rJ  s                         r  parse_markethyperliquid.parse_marketk  s   * ##FF3&&x0''0!!&(3((2c3'#-5#.~~dii4  w/  w/!--flC01  15!;;E?TUVN 11.A^^FL9
!#^F)) 3+
&3+
f3+
 D3+
 U	3+

 f3+
 f3+
 3+
 w3+
 3+
 F3+
 E3+
 d3+
 D3+
 e3+
 e3+
  f!3+
" #3+
$  --c2"++D,@,@AS,TU**4+?+?@Q+RS  ,,V]C
  
  
  ,,T2$ e3+
 3 3	r!  c                   #    SnU R                  SU5      u  p!SnU R                  SSU5      u  p1SnU R                  SU5      u  pAUS:H  nU(       a  SOSUS.nU R                  U R	                  Xa5      5      I Sh  vN nU R                  US5      nUb  SU0n	[        S	[        U5      5       Hh  n
X   nU R                  U R                  US
5      5      nU R                  5       nU R                  US5      nU R                  US5      nXS'   XS'   XU'   Mj     U R                  U	5      $ U R                  US0 5      nSU R                  US5      0nUb  US:X  a  U R                  US5      US'   OU R                  US5      US'   UUS.nU R                  US5      nUUS'   U R                  U5      US'   U R                  U5      $  GNL7f)a}  
query for balance and get the amount of funds available for trading or funds locked in orders

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-a-users-token-balances
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-users-perpetuals-account-summary

:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.user]: user address, will default to self.walletAddress if not provided
:param str [params.type]: wallet type, ['spot', 'swap'], defaults to swap
:param str [params.marginMode]: 'cross' or 'isolated', for margin trading, uses self.options.defaultMarginMode if not passed, defaults to None/None/None
:param str [params.subAccountAddress]: sub account user address
:returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
NrZ   r?   r   r   r   userbalancesr   r   cointotalholdusedmarginSummaryaccountValueisolatedwithdrawablefreetotalMarginUsed)r   r  time	timestampdatetime)handle_public_addresshandle_market_type_and_paramshandle_margin_mode_and_paramsrQ  rR  rS  rT  r:  r.  r,  accountsafe_balancer-  r  rp  iso8601)r  rV  userAddressr   r   isSpotrW  rX  r  spotBalancesrZ  balancerH  r  r  r  r[  usdcBalancerY  r  s                       r  fetch_balancehyperliquid.fetch_balance  s     "88P99.$PVW
!??PVW
&.17,>R
 ,,T[[-IJJH >>(J7"((!3L1c(m,"+..t/?/?/PQ,,.((':''8#( "&%,T" - $$\22~~h<T%%dN;
 "z)A"&"2"28^"LK"&"2"249J"KK
 %%h7	'{!\\)4z  ((A Ks   A:G<G=EGr  c           	        #    U R                  5       I Sh  vN   U R                  U5      nSUS   (       a  US   OUS   S.nU R                  U R                  XS5      5      I Sh  vN nU R	                  US/ 5      nU R	                  US/ 5      U R	                  US	/ 5      S
.nU R                  US5      n	U R                  XS   U	SSSS5      $  N Nk7f)a'  
fetches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#l2-book-snapshot

:param str symbol: unified symbol of the market to fetch the order book for
:param int [limit]: the maximum amount of order book entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
Nr   rA   r(  r2   r   r  levelsr   r   )bidsasksr  r&  r  r  pxsz)load_marketsr/  rQ  rR  rS  rp  parse_order_book)
r  r&  r  rV  r/  rW  rX  r[  rY  r  s
             r  fetch_order_bookhyperliquid.fetch_order_book+  s      !!!V$*0.F:&fTl
 ,,T[[-IJJ. ~~h"5NN4B/NN4B/
 %%h7	$$VH-=y&RXZ^`deeG 	" Ks#   CCAC&C'A*CCsymbolsc                 ,  #    U R                  5       I Sh  vN   U R                  U5      n/ nU R                  US5      nU R                  US5      nUS:X  a  U R	                  U5      I Sh  vN nO9US:X  a  U R                  U5      I Sh  vN nOU R                  U5      I Sh  vN n0 n[        S[        U5      5       H3  nX6   nUS   nU R                  X5      n	U R                  U	S5      n
XU
'   M5     U R                  USU5      $  N N N Nk7f)a  
fetches price tickers for multiple markets, statistical information calculated over the past 24 hours for each market

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-asset-contexts-includes-mark-price-current-funding-open-interest-etc
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-spot-asset-contexts

:param str[] [symbols]: unified symbols of the markets to fetch the ticker for, all market tickers are returned if not assigned
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.type]: 'spot' or 'swap', by default fetches both
:returns dict: a dictionary of `ticker structures <https://docs.ccxt.com/#/?id=ticker-structure>`
Nr   r?   rA   r   r   r&  )r  market_symbolsr,  omitra  r`  rh  rT  r:  parse_tickerfilter_by_array_tickers)r  r  rV  rX  r   rY  rZ  r/  r   tickerr&  s              r  fetch_tickershyperliquid.fetch_tickers[  s     !!!%%g./66*6>!44V<<HV^!44V<<H!//77Hq#h-(A[F&>D&&t4F%%fh7F#6N ) ++FHgFF) 	" =<7sG   DDAD,D-DDD&D'A&DDDDc           	        #    SS0nU R                  U R                  X25      5      I Sh  vN nU R                  US0 5      nU R                  US/ 5      nU R                  US/ 5      n/ n[	        S[        U5      5       HF  n	U R                  U R                  Xi0 5      U R                  Xy0 5      5      n
UR                  U
5        MH     U R                  X5      $  N7f)a  
retrieves data on all swap markets for hyperliquid

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-asset-contexts-includes-mark-price-current-funding-open-interest-etc

:param str[] [symbols]: list of unified market symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: an array of objects representing market data
r   rn  Nr   rC  r   )rQ  rR  r-  rS  rT  r:  r_  parse_funding_rates)r  r  rV  rW  rX  rB  rC  r{  rY  rZ  r[  s              r  fetch_funding_rateshyperliquid.fetch_funding_rates}  s      &
 ,,T[[-IJJ@ ~~h2.>>$
B7NN8Q3	q#h-(A;;xB/yR0D MM$ ) ''88U Ks   (CCB0Cc                    U R                  US5      nU R                  U5      nU R                  XB5      nU R                  US5      nU R                  US5      nU R                  US5      n[	        [
        R                  " U R                  5       5      S-  S-  S-  5      S-   S-  S-  S-  n	0 SU_S	U_S
U_SU_SS _SS _SS _SS _SU_SU	_SU R                  U	5      _SS _SS _SS _SS _SS _SS _SS0E$ )Nr3   fundingr  oraclePx<   r
  r   r   r&  	markPrice
indexPriceinterestRateestimatedSettlePricer  r  fundingRatefundingTimestampfundingDatetimenextFundingRatenextFundingTimestampnextFundingDatetimepreviousFundingRatepreviousFundingTimestamppreviousFundingDatetimeintervalr   )	r,  coin_to_market_idsafe_symbolr  r  mathfloormillisecondsr  )
r  r   r/  r8  r4  r&  r  r  r  r  s
             r  parse_funding_ratehyperliquid.parse_funding_rate  s   * f-))$/!!(3""43!!$1##D*5

4+<+<+> ?" Dr ID PQTUUY[[^``cgg
D
f
 
 (	

 D
 #D
 
 
 7
  0
 t||,<=
 t
 #D
 "4
 "4
  '!
" &t#
$ %
 	
r!  r  c                    U R                  US5      nU R                  US   S S U R                  US5      U R                  US5      U R                  US5      U R                  US5      U R                  US5      US.	U5      $ )	N	impactPxsr&  	prevDayPxr  r   r   	dayNtlVlm)	r&  r  r  previousCloseclosebidaskquoteVolumer   )rS  safe_tickerr  )r  r  r/  bidAsks       r  r  hyperliquid.parse_ticker  s      4X&!--fkB%%fg6##FA.##FA.++FK@
!
 
 
	r!  r   	timeframesincec                 6  #    U R                  5       I Sh  vN   U R                  U5      nU R                  USU R                  5       5      nUSL nUn	Uc;  Ub6  U R	                  U5      S-  n
U R                  XzU-  S-  5      nUS:  a  SnSnOSnU R                  US/5      nSUS   (       a  US	   OUS
   U R                  U R                  X"5      UUS.S.nU R                  U R                  X5      5      I Sh  vN nU R                  XX)XH5      $  N N7f)a  
fetches historical candlestick data containing the open, high, low, and close price, and the volume of a market

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#candle-snapshot

:param str symbol: unified symbol of the market to fetch OHLCV data for
:param str timeframe: the length of time each candle represents, support '1m', '15m', '1h', '1d'
:param int [since]: timestamp in ms of the earliest candle to fetch
:param int [limit]: the maximum amount of candles to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: timestamp in ms of the latest candle to fetch
:returns int[][]: A list of candles ordered, open, high, low, close, volume
Nuntilr
  r   Fr   rA   r(  r2   )r  r  	startTimeendTime)r   req)r  r/  rp  r  parse_timeframesumr  r,  r   rQ  rR  parse_ohlcvs)r  r&  r+  r,  r  rV  r/  r.  useTailoriginalSincetimeframeInMillisecondsrW  rX  s                r  fetch_ohlcvhyperliquid.fetch_ohlcv  s1     !!!V$!!&'43D3D3FG4-= *.*>*>y*ID*P'%(G"(LM19E6G9-$.4Vnz*&, ,,T__iS" 	
 ,,T[[-IJJ"   9U\\U 	"2 Ks"   DDC&D=D>DDc           	          U R                  US5      U R                  US5      U R                  US5      U R                  US5      U R                  US5      U R                  US5      /$ )Ntohlcv)rp  r  )r  ohlcvr/  s      r  parse_ohlcvhyperliquid.parse_ohlcv>  sn      eS)UC(UC(UC(UC(UC(
 	
r!  c                   #    SnU R                  SU5      u  pTU R                  5       I Sh  vN   U R                  U5      nSU0nUb
  SUS'   X'S'   OSUS'   U R                  US5      nU R	                  US5      nUb  XS	'   U R                  U R                  Xt5      5      I Sh  vN n	U R                  XX#5      $  N N7f)
ag  
get the list of most recent trades for a particular symbol

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#retrieve-a-users-fills
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#retrieve-a-users-fills-by-time

:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch trades for
:param int [limit]: the maximum number of trades structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: timestamp in ms of the latest trade
:param str [params.address]: wallet address that made trades
:param str [params.user]: wallet address that made trades
:param str [params.subAccountAddress]: sub account user address
:returns Trade[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
Nr   r  userFillsByTimer   r0  	userFillsr.  r1  r  r  r;  rp  r  rQ  rR  parse_trades
r  r&  r,  r  rV  r  r/  rW  r.  rX  s
             r  fetch_tradeshyperliquid.fetch_tradesV  s     " "88O!!!!!&)K
 /GFO#(K )GFO!!&'267+!&I,,T[[-IJJ*   5@@G 	" K"   *CB=A9C&B?'C?Cc                     U R                  U5      nU R                  U[        US   S   U R                  U R                  5      $ )NrI  rG  )r/  decimal_to_precisionr(   r   paddingMode)r  r&  rG  r/  s       r  amount_to_precisionhyperliquid.amount_to_precision  sA    V$(({8KH8UW[WiWikok{k{||r!  c                    U R                  U5      nU R                  U5      nUR                  S5      S   n[        S[	        U5      5      nU R                  U[        U[        U R                  5      nUS   (       a  SOSnXR                  U R                  US   S5      5      -
  n	U R                  U[        U	[        U R                  5      $ )	Nr  r   rm  r?   r  r  rI  rG  )r/  r  r  r	  r:  rO  r(   r*   rP  precision_from_stringr,  r)   )
r  r&  r   r/  r  r  r  rY  r  subtractedValues
             r  price_to_precisionhyperliquid.price_to_precision  s    V$((/nnS)!,3{#34**5%9JL^`d`p`pq!&>aq%(B(B4CSCSTZ[fTgiqCr(ss((Y]YiYijjr!  c                 .    SU R                  USS5      -   $ )N0xkeccakhex)hash)r  messages     r  hash_messagehyperliquid.hash_message  s    dii5999r!  c                     U R                  USS  USS  SS 5      nSUS   -   SUS   -   U R                  SUS   5      S.$ )	N	secp256k1rY  rs   rA  )rc  rd  rA  )ecdsar4  )r  r\  r   	signatures       r  	sign_hashhyperliquid.sign_hash  sW    JJtCDz:cd+;[$O		#&	#&"in-
 	
r!  c                 J    U R                  U R                  U5      USS  5      $ )Nra  )rh  r^  )r  r]  r   s      r  sign_messagehyperliquid.sign_message  s%    ~~d//8*ST:JKKr!  c                 "    U(       a  SOSnUUS.$ )Nba)sourceconnectionId )r  r\  	isTestnetrp  s       r  construct_phantom_agent#hyperliquid.construct_phantom_agent  s    " 
 	
r!  c                     U R                  U5      nU R                  U5      nUnUSU R                  U5      -   -  nUc  US-  nO	US-  nXb-  nU R                  U R	                  U5      SS5      $ )N000000001rZ  binary)packbbinary_to_base16int_to_base16r\  base16_to_binary)r  actionvaultAddressnonce
dataBinarydataHexr[  s          r  action_hashhyperliquid.action_hash  s}    ZZ'
''
3$,,U333DLDDLD Dyy..t4hIIr!  c                 B   U R                  XU5      nU R                  U R                  SS5      nU R                  XE5      nU R	                  U R                  S5      nSnUSUSS.n	SS	S
S.SSS./0n
U R                  XU5      nU R                  XR                  5      nU$ )Nr   Fr   i9  r   r  chainIdr3   verifyingContractr6   Agentrp  stringr3   r   rq  bytes32)r  r  r  rt  r,  eth_encode_structured_datark  r   )r  r  r  vaultAdressr\  rs  phantomAgentr   r  domainmessageTypesmsgrg  s                r  sign_l1_actionhyperliquid.sign_l1_action  s    U;NN4<<F	33DD. &&t||]C!,	
 !84';
 --fLQ%%c??;	r!  c                     U R                  U R                  S5      nSnUSUSS.nU R                  XQU5      nU R                  X`R                  5      nU$ )Nr   in HyperliquidSignTransactionr  r  )r,  r  r  rk  r   )r  r  r]  r   r  r  r  rg  s           r  sign_user_signed_action#hyperliquid.sign_user_signed_action  s^    &&t||]C0!,	
 --fGL%%c??;	r!  c                 L    SSSS.SSS.SSS.SSS./0nU R                  X!5      $ )	NzHyperliquidTransaction:UsdSendhyperliquidChainr  r  destinationrG  r  uint64r  r  r]  r  s      r  build_usd_send_sighyperliquid.build_usd_send_sig  sD    ,+X>&9!842	/
 ++LBBr!  c                 L    SSSS.SSS.SSS.SS	S./0nU R                  X!5      $ )
Nz'HyperliquidTransaction:UsdClassTransferr  r  r  rG  toPerpboolr  r  r  r  s      r  build_usd_class_send_sig$hyperliquid.build_usd_class_send_sig  sD    5+X>!84!62 (3	8
 ++LBBr!  c                 L    SSSS.SSS.SSS.SSS./0nU R                  X!5      $ )	NzHyperliquidTransaction:Withdrawr  r  r  r  rG  r  r  r  r  s      r  build_withdraw_sighyperliquid.build_withdraw_sig  sD    -+X>&9!842	0
 ++LBBr!  c                 L    SSSS.SSS.SSS.S	S
S./0nU R                  X!5      $ )Nz)HyperliquidTransaction:UserDexAbstractionr  r  r  r  addressr$  r  r  r  r  r  s      r  build_user_dex_abstraction_sig*hyperliquid.build_user_dex_abstraction_sig  sD    7+X>3"F3 (3	:
 ++LBBr!  c                 L    SSSS.SSS.SSS.SS	S./0nU R                  X!5      $ )
Nz(HyperliquidTransaction:ApproveBuilderFeer  r  r  
maxFeeRatebuilderr  r  r  r  r  s      r  build_approve_builder_fee_sig)hyperliquid.build_approve_builder_fee_sig"  sD    6+X>%x8"I6 (3	9
 ++LBBr!  c                 p  #    U R                  U R                  SS5      (       a  gSU R                  S'   SU R                  U R                  SS5      S.nU R                  5       nU R	                  X5      nUUUS.nS n U R                  U5      I S h  vN nU$  N! [         a  nS n S nAU$ S nAff = f7f)	NrefSetFTsetReferrerrefCCXT1)r   rH  r  r  rg  )r  r  r,  r  r  privatePostExchange	Exception)r  r  r  rg  rW  rX  es          r  set_refhyperliquid.set_ref-  s     >>$,,%88!%X!$$T\\5'B
 !!#''6	"

 	!55g>>HO ? 	H	sB   A<B6?B BB B6B 
B3&B.(B6.B33B6r  r  c                   #    U R                  5       nU R                  U R                  SS5      nU(       a  SOSUUUS.nU R                  U5      nUS   SUS   US	   US
S.nUUUS S.nU R	                  U5      I S h  vN $  N7f)Nr   FTestnetMainnet)r  r  r  r  r  0x66eeer  r  approveBuilderFee)r  signatureChainIdr  r  r  r   r  r  rg  r  )r  r  r  r  r  )	r  r  r  r  isSandboxModepayloadsigr  rW  s	            r  approve_builder_feehyperliquid.approve_builder_feeD  s     !!#t||]EJ-:		$	
 009 '(: ; )!,/y)'
  	
 --g6666s   A<B>B?Bc                    #     [         R                  " U R                  5       U R                  5       /6 I S h  vN   g N! [         a
  n S nAgS nAff = f7f)NFT)rc  rd  handle_builder_fee_approvalr  r  )r  r  s     r  rP  hyperliquid.initialize_clientf  sM     	..4#C#C#Et||~"VWWW  X 		s5   A4A  >A  AA   
A
AAAc                   #    U R                  U R                  SS5      nU(       d  gU R                  U R                  SS5      nU(       a  g U R                  U R                  SS5      nU R                  U R                  SS5      nU R                  X45      I S h  vN   SU R                  S'   g N! [         a  nSU R                  S'    S nAgS nAff = f7f)	N
builderFeeTFapprovedBuilderFeer  *0x6530512A6c89C7cfCEbC3BA7fcD9aDa5f30827a6feeRatez0.01%)r  r  r,  r  r  )r  buildFeer  r  r  r  s         r  r  'hyperliquid.handle_builder_fee_approvalm  s     >>$,,dC!^^DLL:NPUV	/&&t||Y@lmG))$,,	7KJ**7???15DLL-. 	 @ 	/).DLL&	/sC   ACAB3 B1B3 0C1B3 3
C=CCCCr$  c                 :  #    S nU R                  SU5      u  p2U R                  5       nU R                  U R                  SS5      nU(       a  SOSUUUS.nU R	                  U5      nUS   SUS	   US
   USS.nUUUS S.n	U R                  U	5      I S h  vN $  N7f)NenableUserDexAbstractionr   Fr  r  )r  r  r$  r  r  r  r$  r  userDexAbstraction)r  r  r$  r  r  r   r  )r  r  r  r  r  r  )
r  r$  rV  r  r  r  r  r  r  rW  s
             r  enable_user_dex_abstraction'hyperliquid.enable_user_dex_abstraction}  s     "889SU[\!!#t||]EJ-:			
 11': '(: ; )y)FO(
  	
 --g6666s   BBBBr   siderG  c           	         #    U R                  5       I Sh  vN   U R                  SXX4XV5      u  pxU R                  U/U5      I Sh  vN n	U	S   $  N: N7f)a  
create a trade order

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order

:param str symbol: unified symbol of the market to create an order in
:param str type: 'market' or 'limit'
:param str side: 'buy' or 'sell'
:param float amount: how much of currency you want to trade in units of base currency
:param float [price]: the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.timeInForce]: 'Gtc', 'Ioc', 'Alo'
:param bool [params.postOnly]: True or False whether the order is post-only
:param bool [params.reduceOnly]: True or False whether the order is reduce-only
:param float [params.triggerPrice]: The price at which a trigger order is triggered at
:param str [params.clientOrderId]: client order id,(optional 128 bit hex string e.g. 0x1234567890abcdef1234567890abcdef)
:param str [params.slippage]: the slippage for market order
:param str [params.vaultAddress]: the vault address for order
:param str [params.subAccountAddress]: sub account user address
:returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
Nr   )r  parse_create_edit_order_argscreate_orders)
r  r&  r   r  rG  r   rV  orderglobalParamsorderss
             r  create_orderhyperliquid.create_order  s]     , !!!"??fTXbgp))5'<@@ay 	"@s!   AA0AA
AAr  c                   #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   U R                  X5      nU R                  U5      I Sh  vN nU R	                  US0 5      nU R	                  US0 5      nU R                  US/ 5      n/ n[        S[        U5      5       H3  n	Xy   n
U
S:X  a  UR                  SU
05        M"  UR                  U
5        M5     U R                  US5      $  N N N7f)a  
create a list of trade orders

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order

:param Array orders: list of orders to create, each object should contain the parameters required by createOrder, namely symbol, type, side, amount, price and params
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
NrX  r[  statusesr   waitingForTriggerstatus)
r  rP  create_orders_requestr  r-  rS  rT  r:  r_  parse_orders)r  r  rV  rW  rX  responseObjr[  r  ordersToBeParsedrZ  r  s              r  r  hyperliquid.create_orders  s      !!!$$&&&,,V<11'::$ nnXz2>~~k626>>$
B7q#h-(AKE++ ''5(9: ''. )   !1488? 	"&:s2   C<C6C<C8)C<C:BC<8C<:C<c                    U R                  U5      nUR                  5       nUR                  5       nUS:H  nUS:H  n	U R                  USS5      n
U R                  US5      nU(       a  SOSnU R	                  USS	5      nU(       a  S
nU R                  USU5      nU R                  U5      nU R                  USS5      nU R                  USU5      nU R                  US5      nU=(       d    UnS nU(       a  Uc  [        U R                  S-   5      eU	(       a,  [        R                  " U[        R                  " SU5      5      O+[        R                  " U[        R                  " SU5      5      nU R                  UU5      nOU R                  X5      nU R                  X5      nU R	                  USS	5      n0 nU(       a?  S	nUb  U R                  UU5      nSnOU R                  UU5      nUUU(       a  SOSS.US'   OSU0US'   U R                  U/ SQ5      nU R!                  US   5      U	UUUUS.nU
b  U
US'   U$ )NMARKETBUYclientOrderId	client_idslippageiocgtcpostOnlyFalor  r   	stopPricer   r   zy  market orders require price to calculate the max slippage price. Default slippage can be set in options(default is 5%).r  
reduceOnlyTtpslisMarket	triggerPxtpslr  tifr  )
r  r  r   r  r   r   r  r  r  r  ro  ro  rn  prd  rc  r<  r@  )r/  uppersafe_string_2r,  r  safe_string_lower
capitalizer"   r2   r,   
string_mul
string_add
string_subrV  rQ  r  r  )r  r&  r   r  rG  r   rV  r/  r  isBuyr  r  defaultTimeInForcer  r  r   r   r   	isTriggerr  r  r  	orderTypeisTporderObjs                            r  create_order_request hyperliquid.create_order_request  sx   V$zz|zz|H$**6?KP##FJ7'/Ue>>&*e<!&,,V]DVWook2))&.+N((,O**63DE"5o	}'  3n  )n  o  oRW##E7+=+=c8+LM^e^p^pqvx  yK  yK  LO  QY  yZ  _[B((4B((7B%%f5^^FL%@
	D*#66vO#66v}M$)!%D$Ii  {"Ig 6  $x  y""6(#34
 $)HSMr!  c                 Z   U R                  5         U R                  U R                  S5      nU R                  USU5      nSn[        S[	        U5      5       H4  nX   nU R                  US0 5      nU R                  USS5      nUc  M2  S	nM6     U(       aa  [        S[	        U5      5       HH  nX   nU R                  US0 5      nU R                  USS5      nUb  M2  [        U R                  S
-   5      e   U R                  U/ SQ5      nU R                  5       n	/ n
Sn[        S[	        U5      5       GH.  nX   nU R                  US5      nU R                  U5      nUS   nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US0 5      nU R                  USU5      nUUS'   U R                  US5      nU R                  US5      nU=(       d    UnU R                  USS/5      nU R                  XUUUU5      nU
R                  U5        U(       d  GM  U R!                  USS/5      nU R                  USS5      nU R!                  USS/U5      nU R!                  USS/5      nU R                  USS5      nU R!                  USS/U5      nSnU R                  USS/5      nSnUS:X  a  SnOSnUb:  U R                  UUUUUU R#                  UUS	S.5      5      nU
R                  U5        Uc  GM  U R                  UUUUUU R#                  UUS	S.5      5      nU
R                  U5        GM1     Sn U R%                  US S!5      u  n nU R'                  U 5      n S"U
US#.n!U R)                  U R                  S$S5      (       a@  U R+                  U R                  S%S&5      n"U"U R-                  U R                  S'S(5      S).U!S%'   U R/                  U!U	U 5      n#U!U	U#S*.n$U b  U R                  US!5      nU U$S!'   U$$ )+a{  
create a list of trade orders
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order
:param Array orders: list of orders to create, each object should contain the parameters required by createOrder, namely symbol, type, side, amount, price and params
:returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
r   r  Fr   rV  r  r  NTzV createOrders() all orders must have clientOrderId if at least one has a clientOrderId	r  r  r  r  r   r  r   r   r  nar&  r   r  rG  r   stopLoss
takeProfitr   r  r  r   r   
normalTpslr  r  sellbuy)r   r  )r   r  rQ   r  r  )r   r  groupingr  r  r  feeInt
   )rn  fr  )rO  r,  r  rT  r:  r-  r  r"   r2   r  r  r/  safe_string_upper
safe_valuer  r_  safe_string_nrR  handle_option_and_paramsformat_vault_addressr  r  rp  r  )%r  r  rV  r   hasClientOrderIdrZ  rawOrderorderParamsr  r  orderReqr  r4  r/  r&  r   r  rG  r   r  r  r  r	  mainOrderObjstopLossOrderTriggerPricestopLossOrderTypestopLossOrderLimitPricetakeProfitOrderTriggerPricetakeProfitOrderTypetakeProfitOrderLimitPricetriggerOrderSider  r  orderActionwalletrg  rW  s%                                        r  r  !hyperliquid.create_orders_request"  s    	'')**4<<9JK**6:O q#f+&AyH..8R@K ..{O[YM(#'  ' 1c&k*!9"nnXxD $ 2 2;Q\ ] (+DGG  7O  -O  P  P + 6  $j  k!!#q#f+&AyH''(;H[[*FH%F))(F;D))(F;D%%h9F$$Xw7E..8R@K''ZQH&.K
#{J?HlCJ!/ZI))K*l1KLK!%!:!:6vW\^i!jLOOL)y,0,>,>x.ZeIf,g)$($4$4Xvw$O!*.*<*<XQ`Gac|*}'.2.@.@n^iMj.k+&*&6&6z67&S#,0,>,>zGUfKg  jE  -F)'"iij,5OP#% 5='-$',$)%)%>%>vGZ\lnt  wP  RV  R]  R]  ^i+F&*k R &H OOH-'%)%>%>vGXZjlr  uL  NR  NY  NY  Ze)B&*g N &H OOH-Y 'Z #<<V]Tbcf00> 

 >>$,,(<eDD++DLL)EqrF+18I8I$,,X`bd8e%fK	"''ULI	!"
 #YYv~6F&2GN#r!  r2   c                 h   #    U R                  U/X#5      I Sh  vN nU R                  US5      $  N7f)a  
cancels an open order

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid

:param str id: order id
:param str symbol: unified symbol of the market the order was made in
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.clientOrderId]: client order id,(optional 128 bit hex string e.g. 0x1234567890abcdef1234567890abcdef)
:param str [params.vaultAddress]: the vault address for order
:param str [params.subAccountAddress]: sub account user address
:returns dict: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
Nr   )cancel_ordersr-  )r  r2   r&  rV  r  s        r  cancel_orderhyperliquid.cancel_order  s5      ))2$??~~fa(( @s   202idsc                   #    U R                  5         Uc  [        U R                  S-   5      eU R                  5       I Sh  vN   U R	                  5       I Sh  vN   U R                  XU5      nU R                  U5      I Sh  vN nU R                  US5      nU R                  US5      nU R                  US5      n/ n	[        S[        U5      5       H*  n
X   nU	R                  U R                  UUS.5      5        M,     U	$  N N N7f)a  
cancel multiple orders

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid

:param str[] ids: order ids
:param str [symbol]: unified market symbol
:param dict [params]: extra parameters specific to the exchange API endpoint
:param string|str[] [params.clientOrderId]: client order ids,(optional 128 bit hex string e.g. 0x1234567890abcdef1234567890abcdef)
:param str [params.vaultAddress]: the vault address
:param str [params.subAccountAddress]: sub account user address
:returns dict: an list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Nz* cancelOrders() requires a symbol argumentrX  r[  r  r   r   r  )rO  r"   r2   r  rP  cancel_orders_requestr  r-  rS  rT  r:  r_  
safe_order)r  r3  r&  rV  rW  rX  innerResponser[  r  r  rZ  r  s               r  r0  hyperliquid.cancel_orders  s      	'')>#DGG.Z$Z[[!!!$$&&&,,S&A11':: x<~~mV4>>$
3q#h-(A[FMM$// +   ) 7 	"&:s6   ?DDDD*DD
BDD
Dc                    U R                  U5      nU R                  USS5      nU R                  USS/5      nU R                  5       nSU0n/ nS/ S.n	U R	                  US   5      n
UbP  [        U[        5      (       d  U/nSU	S	'   [        S
[        U5      5       H  nUR                  U
X[   S.5        M     OFSU	S	'   [        S
[        U5      5       H(  nUR                  U
U R	                  X   5      S.5        M*     XS'   SnU R                  USSS5      u  pU R                  U5      nU R                  XU5      nXS'   XS'   Ub  U R                  US5      nXS'   U$ )a  
build the request payload for cancelling multiple orders
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid
:param str[] ids: order ids
:param str symbol: unified market symbol
:param dict [params]:
:returns dict: the raw request object to be sent to the exchange
r  r  r  r  r   cancelsro  NcancelByCloidr   r   )assetcloidcancel)ro  r=  r<  rJ   r  subAccountAddressr  rg  )r/  safe_value_2r  r  parse_to_numeric
isinstancelistrT  r:  r_  handle_option_and_params_2r  r  )r  r3  r&  rV  r/  r  r  rW  	cancelReqcancelActionro  rZ  r  rg  s                 r  r6  !hyperliquid.cancel_orders_request  s    V$))&/;O6O[#AB!!#U
 	
 &&vh'78$mT22!.#2L 1c-01  #*-"  2 $,L 1c#h'  ..sv6"  (
 #,Y#>>v~Wegz{00>''\J	((#YYv~6F&2N#r!  c                   #    U R                  5         U R                  5       I Sh  vN   U R                  5       I Sh  vN   U R                  5       nSU0n/ nS/ S.nSn[	        S[        U5      5       H  nX   n	U R                  U	S5      n
U
b  SnU R                  U	S	5      nU R                  U	S
5      nUc  [        U R                  S-   5      eUb  U(       a  [        U R                  S-   5      eU(       a  SOSnU(       a  SOSnU R                  U5      n0 nU R                  US   5      UU'   U(       a  U
OU R                  U5      UU'   UR                  U5        M     U(       a  SOSUS'   XVS'   SnU R                  USSS5      u  nnU R                  U5      nU R                  XcU5      nXdS'   UUS'   Ub  U R!                  US5      nUUS'   U R#                  U5      I Sh  vN nU R%                  SU05      /$  GN GN N7f)a  
cancel multiple orders for multiple symbols

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid

:param CancellationRequest[] orders: each order should contain the parameters required by cancelOrder namely id and symbol, example [{"id": "a", "symbol": "BTC/USDT"}, {"id": "b", "symbol": "ETH/USDT"}]
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.vaultAddress]: the vault address
:param str [params.subAccountAddress]: sub account user address
:returns dict: an list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Nr  r  r;  Fr   r  Tr2   r&  zB cancelOrdersForSymbols() requires a symbol argument in each orderzI cancelOrdersForSymbols() all orders must have either id or clientOrderIdr>  ro  r?  r=  ro  r=  r@  r   r<  rK   r  rA  r  rg  r   )rO  r  rP  r  rT  r:  r,  r"   r2   r#   r/  rC  r_  rF  r  r  r  r  r7  )r  r  rV  r  rW  rG  rH  r=  rZ  r  r  r2   r&  assetKeyidKeyr/  	cancelObjr  rg  rX  s                       r  cancel_orders_for_symbols%hyperliquid.cancel_orders_for_symbols  s8     	'')!!!$$&&&!!#U
 	
 q#f+&AIE ,,UODM( $!!%.B%%eX6F~'2v(vww~- +v!vww"/wSH,G#E[[(F I"&"7"7x8H"IIh0=}4CXCXY[C\IeY'# '$ 3@XV"+Y#>>vG_ao  rE   Ff00>''\J	((#YYv~6F&2GN#11'::  2344q 	"&R ;s2   $HHHHF,H+H
,HH
Htimeoutc                   #    U R                  5         U R                  5       I Sh  vN   U R                  5       I Sh  vN   U R                  USS/5      nU R	                  5       nSU0nSX1-   S.nSnU R                  USSS	5      u  pbU R                  U5      nU R                  XSU5      nXTS
'   XtS'   Ub  U R                  US5      nXdS'   U R                  U5      I Sh  vN nU$  N N N
7f)ah  
dead man's switch, cancel all orders after the given timeout
:param number timeout: time in milliseconds, 0 represents cancel the timer
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.vaultAddress]: the vault address
:param str [params.subAccountAddress]: sub account user address
:returns dict: the api result
Nr  r  r  scheduleCancel)r   r  rH   r  rA  r  rg  )	rO  r  rP  r  r  rF  r  r  r  )	r  rP  rV  r  rW  rH  r  rg  rX  s	            r  cancel_all_orders_after#hyperliquid.cancel_all_orders_after?  s     	'')!!!$$&&&6O[#AB!!#U

 %O
 #>>vG]_m  pC   D00>''\J	((#YYv~6F&2N#11':: 9 	"&( ;s2   $C/C)C/C+B#C/"C-#C/+C/-C/c                    U R                  5         Sn[        S[        U5      5       H4  nX   nU R                  US0 5      nU R	                  USS5      nUc  M2  SnM6     U(       aa  [        S[        U5      5       HH  nX   nU R                  US0 5      nU R	                  USS5      nUb  M2  [        U R                  S-   5      e   U R                  U/ SQ5      n/ n[        S[        U5      5       GH  nX   nU R                  US	5      n	U R                  US
5      n
U R                  U
5      nUS
   nU R                  US5      nUS:H  nU R                  US5      nUS:H  nU R                  US5      nU R                  US5      nU R                  US0 5      nU R                  U R                  S5      nU R                  USU5      nU(       a  SOSnU R                  USS5      nU(       a  SnU R                  USU5      nU R                  U5      nU R	                  USS5      nU R	                  USS5      nU R                  USU5      nU R                  US5      nU=(       d    UnU R                  USS5      nU R                  U/ SQ5      nU R                  U5      nU(       ar  U(       a,  [         R"                  " U[         R$                  " SU5      5      O+[         R"                  " U[         R&                  " SU5      5      nU R)                  UU5      nOU R)                  UU5      nU R+                  UU5      n0 nU(       a?  Sn Ub  U R)                  UU5      nSn OU R)                  UU5      nUUU (       a  SOS S!.US"'   OS#U0US$'   Uc  S%nU R-                  US&   5      UUUUUS'.n!Ub  UU!S('   U R-                  U	5      U!S).n"UR/                  U"5        GM     U R1                  5       n#S*US+.n$S n%U R3                  US,S-5      u  n%nU R5                  U%5      n%U R7                  U$U#U%5      n&U$U#U&S..n'U%b  U%U'S-'   U'$ )/NFr   rV  r  r  TzT editOrders() all orders must have clientOrderId if at least one has a clientOrderIdr  r2   r&  r   r  r  r  rG  r   r   r  r  r  r  r  r  r   r  r   r   r  )	r  r  r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  ro  r  r@  )oidr  batchModify)r   modifiesrW   r  r  )rO  rT  r:  r-  r  r"   r2   r  r,  r/  r  r  r  r  r  r  r,   r  r  r  rV  rQ  r  r_  r  r  r  r  )(r  r  rV  r   rZ  r!  r"  r  rX  r2   r4  r/  r&  r   r  r  r  rG  r   r   r  r  r  r  r   r   r   r	  r  r  r  r
  r  r#  	modifyReqr  modifyActionr  rg  rW  s(                                           r  edit_orders_requesthyperliquid.edit_orders_requestg  s   '') q#f+&AyH..8R@K ..{O[YM(#'  ' 1c&k*!9"nnXxD $ 2 2;Q\ ] (+DGG  7M  -M  N  N + 6  $j  kq#f+&AyH!!(D1B''(;H[[*FH%F))(F;D(H))(F;DU]E%%h9F$$Xw7E..8R@K"..t||=NOO''ZQH+3%~~k:uEH%*"00mM_`K//+6K ..{O[YM--k>;WL ,,[/<XM"..{<MNO&9/I\5IJ))K  2y  zK&&u-BSXW''G,>,>sH,MN_f_q_qrtv}  wI  wI  JM  OW  wX  `Y,,VR8,,VR8))&&9B I".#'#:#:6?#SLD#'#:#:6=#QL (!-%)D(	)$ ;&	'" #"&&vh'78H ( -((,!I OOI&K 'L !!#! 
 #<<V[R`af00>''e\J	""
 #&2GN#r!  c           	         #    U R                  5       I Sh  vN   Uc  [        U R                  S-   5      eU R                  XX4XVU5      u  pU R	                  U/U	5      I Sh  vN n
U
S   $  NU N7f)a  
edit a trade order

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders

:param str id: cancel order id
:param str symbol: unified symbol of the market to create an order in
:param str type: 'market' or 'limit'
:param str side: 'buy' or 'sell'
:param float amount: how much of currency you want to trade in units of base currency
:param float [price]: the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.timeInForce]: 'Gtc', 'Ioc', 'Alo'
:param bool [params.postOnly]: True or False whether the order is post-only
:param bool [params.reduceOnly]: True or False whether the order is reduce-only
:param float [params.triggerPrice]: The price at which a trigger order is triggered at
:param str [params.clientOrderId]: client order id,(optional 128 bit hex string e.g. 0x1234567890abcdef1234567890abcdef)
:param str [params.vaultAddress]: the vault address for order
:param str [params.subAccountAddress]: sub account user address
:returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
Nz$ editOrder() requires an id argumentr   )r  r"   r2   r  edit_orders)r  r2   r&  r   r  rG  r   rV  r  r  r  s              r  
edit_orderhyperliquid.edit_order  sy     , !!!:#DGG.T$TUU"??DX^gmn''>>ay 	" ?s"   A0A,AA0"A.#
A0.A0c                 ^  #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   U R                  X5      nU R                  U5      I Sh  vN nU R	                  US0 5      nU R	                  US0 5      nU R                  US/ 5      nU R                  U5      $  N Nz NR7f)a  
edit a list of trade orders

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders

:param Array orders: list of orders to create, each object should contain the parameters required by createOrder, namely symbol, type, side, amount, price and params
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
NrX  r[  r  )r  rP  r[  r  r-  rS  r  )r  r  rV  rW  rX  responseObject
dataObjectr  s           r  r^  hyperliquid.edit_orders  s      !!!$$&&&**6:11'::H *bA^^NFB?
>>*j"=  **U 	"&:s2   B-B'B-B))B-B+AB-)B-+B-r3   description
initialUsdc                   #    U R                  5         U R                  5       I Sh  vN   U R                  5       nSU0nU R                  [        R
                  " U R                  U5      S5      5      nSUUUUS.nU R                  X5      n	XS'   XS'   U R                  U R                  Xd5      5      I Sh  vN n
U
$  N N7f)a  
creates a value
:param str name: The name of the vault
:param str description: The description of the vault
:param number initialUsd: The initialUsd of the vault
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: the api result
Nr  1000000createVault)r   r3   re  rf  r  r  rg  )
rO  r  r  r  r,   r  r  r  r  rR  )r  r3   re  rf  rV  r  rW  usdr  rg  rX  s              r  create_vaulthyperliquid.create_vault%	  s      	'')!!!!!#U
  2 243H3H3TV_ `a!&
 ''6	"(11$++g2NOO 5 	"  Ps"   $CB?BC8C9CCc           
        #    U R                  5       I Sh  vN   Uc  [        U R                  S-   5      eU R                  U5      nSUS   S.nUb  X&S'   O&Uc  SOUnU R	                  5       US-  S-  S	-  -
  US'   U R                  US
5      nU R                  US
5      nUb  XS'   U R                  U R                  Xd5      5      I Sh  vN n	/ n
[        S[        U	5      5       H_  nX   nU R                  US5      nU
R                  UU R                  SU5      U R                  US5      UU R                  U5      S.5        Ma     U R                  U
S5      nU R!                  XX#5      $  GNX N7f)a  
fetches historical funding rate prices

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-historical-funding-rates

:param str symbol: unified symbol of the market to fetch the funding rate history for
:param int [since]: timestamp in ms of the earliest funding rate to fetch
:param int [limit]: the maximum amount of `funding rate structures <https://docs.ccxt.com/#/?id=funding-rate-history-structure>` to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: timestamp in ms of the latest funding rate
:returns dict[]: a list of `funding rate structures <https://docs.ccxt.com/#/?id=funding-rate-history-structure>`
Nz5 fetchFundingRateHistory() requires a symbol argumentfundingHistoryr(  r  r0  i  r	  r
  r.  r1  r   r  r  )r   r&  r  r  r  r  )r  r"   r2   r/  r  rp  r  rQ  rR  rT  r:  r_  r  r  r  sort_byfilter_by_symbol_since_limit)r  r&  r,  r  rV  r/  rW  maxLimitr.  rX  rY  rZ  r  r  sorteds                  r  fetch_funding_rate_history&hyperliquid.fetch_funding_rate_historyK	  s     !!!>#DGG.e$effV$$:&
 #(K $}s5H#'#4#4#6B9Kd9R#RGK !!&'267+!&I,,T[[-IJJ q#h-(AKE))%8IMM**48#//}E& LL3  ) fk200NNQ 	"" Ks#   E3E.B4E3E1B#E31E3c                   #    SnU R                  SU5      u  pTSnU R                  USSS5      u  pdU R                  5       I Sh  vN   UUS.nSnUb[  U R                  U5      nU R	                  USS5      n	U	R                  S5      n
[        U
5      nUS	:  a  U R	                  U
S
5      US'   U R                  U R                  Xt5      5      I Sh  vN n/ n[        S
[        U5      5       HB  nX   n0 nU R	                  US5      c  SUS'   UR                  U R                  UU5      5        MD     U R                  XX#5      $  GN Nv7f)a3  
fetch all unfilled currently open orders

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#retrieve-a-users-open-orders

:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch open orders for
:param int [limit]: the maximum number of open orders structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.user]: user address, will default to self.walletAddress if not provided
:param str [params.method]: 'openOrders' or 'frontendOpenOrders' default is 'frontendOpenOrders'
:param str [params.subAccountAddress]: sub account user address
:param str [params.dex]: perp dex name. default is None
:returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Nr   methodfrontendOpenOrdersr  r(  r  r  r   r   r<   r  open
ccxtStatus)r  r  r  r/  r,  r  r:  rQ  rR  rT  r_  r  )r  r&  r,  r  rV  r  rv  rW  r/  r(  partpartsLengthrX  orderWithStatusrZ  r  extendOrders                    r  fetch_open_ordershyperliquid.fetch_open_orders	  s_      "889JFS66v?PRZ\pq!!!
 [[(F''
B?H>>#&Dd)KQ!%!1!1$!:,,T[[-IJJ q#h-(AKEKx08,2L)""4;;uk#BC )   %GGG 	" Ks&   AE
EBE
EA4E
E
c                    #    U R                  5       I Sh  vN   U R                  USSU5      I Sh  vN nU R                  USS/S5      nU R                  XaX#5      $  NG N-7f)a  
fetch all unfilled currently closed orders
:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch open orders for
:param int [limit]: the maximum number of open orders structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.user]: user address, will default to self.walletAddress if not provided
:returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Nr  closedFr  fetch_ordersfilter_by_arrayrp  r  r&  r,  r  rV  r  closedOrderss          r  fetch_closed_ordershyperliquid.fetch_closed_orders	  se      !!!((tVDD++FHxj%P00uTT 	"D   A"AA"A ,A" A"c                    #    U R                  5       I Sh  vN   U R                  USSU5      I Sh  vN nU R                  USS/S5      nU R                  XaX#5      $  NG N-7f)a  
fetch all canceled orders
:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch open orders for
:param int [limit]: the maximum number of open orders structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.user]: user address, will default to self.walletAddress if not provided
:returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Nr  canceledFr  r  s          r  fetch_canceled_orders!hyperliquid.fetch_canceled_orders	  se      !!!((tVDD++FHzlER00uTT 	"Dr  c                    #    U R                  5       I Sh  vN   U R                  USSU5      I Sh  vN nU R                  US/ SQS5      nU R                  XaX#5      $  NH N.7f)a  
fetch all closed and canceled orders
:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch open orders for
:param int [limit]: the maximum number of open orders structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.user]: user address, will default to self.walletAddress if not provided
:returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Nr  )r  r  rejectedFr  r  s          r   fetch_canceled_and_closed_orders,hyperliquid.fetch_canceled_and_closed_orders	  se      !!!((tVDD++FH>`bgh00uTT 	"Ds   A#AA#A!-A#!A#c                   #    SnU R                  SU5      u  pTU R                  5       I Sh  vN   U R                  U5      nSUS.nU R                  U R	                  Xt5      5      I Sh  vN nU R                  XX#5      $  NT N7f)a  
fetch all orders
:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch open orders for
:param int [limit]: the maximum number of open orders structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.user]: user address, will default to self.walletAddress if not provided
:param str [params.subAccountAddress]: sub account user address
:returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Nr   historicalOrdersr  )r  r  r;  rQ  rR  r  )	r  r&  r,  r  rV  r  r/  rW  rX  s	            r  r  hyperliquid.fetch_orders	  s      "88O!!!!!&)&
 ,,T[[-IJJ   5@@) 	" Ks!   *BB=B*B+BBc                   #    SnU R                  SU5      u  pCU R                  5       I Sh  vN   U R                  U5      nU R                  US5      nSUS.nUb  U R	                  US5      nXgS'   O+[        U5      S:  nU(       a  UOU R                  U5      US'   U R                  U R                  Xs5      5      I Sh  vN n	U R                  U	S5      n
U R                  X5      $  N N)7f)	a  
fetches information on an order made by the user

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#query-order-status-by-oid-or-cloid

:param str id: order id
:param str symbol: unified symbol of the market the order was made in
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.clientOrderId]: client order id,(optional 128 bit hex string e.g. 0x1234567890abcdef1234567890abcdef)
:param str [params.user]: user address, will default to self.walletAddress if not provided
:param str [params.subAccountAddress]: sub account user address
:returns dict: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
Nr   r  r   r  rV  "   r  )r  r  r;  r,  r  r:  rC  rQ  rR  r-  parse_order)r  r2   r&  rV  r  r/  r  rW  isClientOrderIdrX  r[  s              r  fetch_orderhyperliquid.fetch_order
  s      "88vN!!!!!&)((A!

 $YYv7F*EN!"gmO#2R8M8Mb8QGEN,,T[[-IJJ8 ~~h0--W 	" Ks"   *C-C)BC-C+(C-+C-r  c           
         U R                  US5      nUb  U R                  USS.5      $ U R                  U/ SQ5      nUc  UnU R                  US5      nS nUb  U R                  U5      nU R                  US5      c  U R	                  US 5      nOU R	                  Xb5      nUS   nU R                  US5      nU R                  US	S
5      n	U R                  US
/5      nU R                  US5      n
U
b
  U
S:X  a  SOSn
U R                  USS5      nU R                  US5      nU R                  US5      nS nUb  US:H  nU R                  0 SU_SU R                  US5      _SU R                  US5      _SU_SU R                  U5      _SS _SU R                  US5      _SU_SU R                  U R                  US5      5      _SU_SU_S U R                  US 5      _SU
_S!U R                  US"5      _S#U R                  US$5      (       a  U R                  US%5      OS _S&U_S'S _U R                  US(5      [        R                  " X5      UU R!                  U	5      S S S).EU5      $ )*Nerrorr  r5  )r  restingfilledr  r2   r&  r  r  ry  r  Ar  r  origSztotalSzr  r  ALOr   rV  r  r?  r  lastTradeTimestamplastUpdateTimestampstatusTimestampr   r
  r  r  r  r   limitPxr   r	  r  rG  r   avgPx)averager  	remainingr  rM  trades)r,  r7  safe_dict_nr  r;  rp  r  r  r  r  parse_order_typer  r  r  r,   r  parse_order_status)r  r  r/  r  r  r  r4  r&  r  r  r  totalAmountr  r  r  s                  r  r  hyperliquid.parse_orderH
  s   @   0??$$     (FG=Ev.--d3HE4(0%%h5F%%h7F!%%e[9	##E8\B		%,0v."ck6D(()D$$UD1	$$UE2?uH  
E 
$""5%0 
 T--eW= 
 	 

 Y/ 
 !$ 
 "4#4#4U<M#N 
 f 
 D))$*@*@*TU 
 3 
  
 $..= 
 D 
 T%%eY7 
 DNNSXZeDfDfD,,UK@lp 
  k! 
" D# 
$ ''w7((@"--f5/ 
0 1 	r!  r  c                     Uc  g SSSSSSS.nUR                  S5      (       a  gUR                  S5      (       a  gU R                  X!U5      $ )Nrx  r  r  r  )	triggeredr  rx  r  r  marginCanceledRejectedCanceled)endswithr,  r  r  r  s      r  r  hyperliquid.parse_order_status
  s\    >""(
 ??:&&??:&&&99r!  c                 0    SSS.nU R                  X!U5      $ )Nr  r/  )z
stop limitzstop marketr,  r  s      r  r  hyperliquid.parse_order_type
  s$    !#
 &99r!  c                   #    SnU R                  SU5      u  pTU R                  5       I Sh  vN   U R                  U5      nSU0nUb
  SUS'   X'S'   OSUS'   U R                  US5      nU R	                  US5      nUb  XS	'   U R                  U R                  Xt5      5      I Sh  vN n	U R                  XX#5      $  N N7f)
a  
fetch all trades made by the user

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#retrieve-a-users-fills
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#retrieve-a-users-fills-by-time

:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch trades for
:param int [limit]: the maximum number of trades structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: timestamp in ms of the latest trade
:param str [params.subAccountAddress]: sub account user address
:returns Trade[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
Nrz   r  rF  r   r0  rG  r.  r1  rH  rJ  s
             r  fetch_my_tradeshyperliquid.fetch_my_trades
  s      "88&Q!!!!!&)K
 /GFO#(K )GFO!!&'267+!&I,,T[[-IJJ,   5@@I 	" KrM  tradec                 R   U R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  U5      nU R                  US 5      nUS   nU R                  US5      n	U R                  US5      n
U
b
  U
S:X  a  S	OS
n
U R                  US5      nS nU R	                  US5      nUb  U(       a  SOSnU R                  UUU R                  U5      UU	U R                  US5      S U
UUUS UU R                  US5      S S.S.U5      $ )Nr  r  r  r  r&  tidr  r  r  r  rM  crossedr   r   rV  feeToken)r   currencyrate)r   r  r  r&  r2   r  r   r  takerOrMakerr   rG  r   rM  )rp  r,  r  r;  r  
safe_trader  )r  r  r/  r  r   rG  r  r4  r&  r2   r  rM  r  r  s                 r  parse_tradehyperliquid.parse_trade/  sU   & %%eV4	  -!!%.v.))$/!!(D1!eU+v."ck6Due,..	2&-77L"Y/%%eU3( ,,UJ? 
$ % 	r!  c                 j   #    U R                  U/U5      I Sh  vN nU R                  US0 5      $  N7f)a  
fetch data on an open position

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-users-perpetuals-account-summary

:param str symbol: unified market symbol of the market the position is held in
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.user]: user address, will default to self.walletAddress if not provided
:returns dict: a `position structure <https://docs.ccxt.com/#/?id=position-structure>`
Nr   )fetch_positionsr-  )r  r&  rV  	positionss       r  fetch_positionhyperliquid.fetch_positionf  s7      ..x@@	~~iB// As   313c                   #    U R                  5       I Sh  vN   SnU R                  SU5      u  p2U R                  U5      nSUS.nU R                  U R	                  XB5      5      I Sh  vN nU R                  US/ 5      n/ n[        S[        U5      5       H&  nUR                  U R                  Xh   S5      5        M(     U R                  USUS5      $  N Nn7f)	a"  
fetch all open positions

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-users-perpetuals-account-summary

:param str[] [symbols]: list of unified market symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.user]: user address, will default to self.walletAddress if not provided
:param str [params.subAccountAddress]: sub account user address
:returns dict[]: a list of `position structure <https://docs.ccxt.com/#/?id=position-structure>`
Nr   r   r  assetPositionsr   r&  F)r  r  r  rQ  rR  rS  rT  r:  r_  parse_positionfilter_by_array_positions)	r  r  rV  r  rW  rX  r[  rY  rZ  s	            r  r  hyperliquid.fetch_positionst  s      !!!"889I6R%%g.(
 ,,T[[-IJJ\ ~~h(8"=q#d)$AMM$--dgt<= %--fhOOu 	" Ks#   CCAC*C+A-CCpositionc                 L   U R                  US0 5      nU R                  US5      nU R                  U5      nU R                  US 5      nUS   nU R                  US0 5      nU R                  US5      nUS:H  n	U R                  US5      n
U
nS nUb6  [        R
                  " U
S5      (       a  S	OS
n[        R                  " U5      nU R                  US5      n[        R                  " U5      nU R                  US5      nS nU	(       a  [        R                  " X5      nOUn[        R                  " [        R                  " X5      S5      nU R                  0 SU_SS _SU_SS _SS _SU	_SS _SU_SU R                  U5      _SS _SU R                  US5      _SS _SU R                  US5      _SU R                  US5      _SU R                  U5      _SU R                  U5      _SS _S S U R                  U5      U R                  US5      UU R                  U5      S .E5      $ )!Nr  r  r&  r  r   r  szir  longshortunrealizedPnl
marginUsed100r   r2   r  r  r  r  	contractsr  
entryPriceentryPxr
  notionalpositionValuevalue
collateralinitialMarginmaintenanceMarginliquidationPx)initialMarginPercentagemaintenanceMarginPercentager  liquidationPricer   
percentage)r-  r,  r  r;  r,   r  
string_absr  r  
string_divsafe_positionr  r  )r  r  r/  r  r  r4  r&  r  r   
isIsolatedrawSizesizer  rawUnrealizedPnlabsRawUnrealizedPnlr  r  r  s                     r  r  hyperliquid.parse_position  s   6 xR8v.))$/!!(D1!>>%R8%%h7
 J.
""5%0$..w<<6'D%%d+D++E?C%001AB%%e\:
#..zLM&M''(:(:;N([]bc
!! #
H#
$#
 f#
 	#

 #
 
#
 d#
 D#
 **40#
 D#
 $**5)<#
 #
 ((@#
 ((7;#
 $++J7#
  T..}=!#
"  ##
$ (,+/!../?@ $ 0 0 H$++J7/#
  	r!  r   c                 v  #    Uc  [        U R                  S-   5      eU R                  5       I Sh  vN   U R                  U5      nU R	                  US5      nUc  [        U R                  S-   5      eU R                  US   5      nUS:H  nU R                  5       nU R                  US/5      nSUUUS.n	Sn
U R                  US	S
S5      u  pU
b(  U
R                  S5      (       a  U
R                  SS5      n
U R                  XU
5      nU	UUS.nU
b  XS
'   U R                  U5      I Sh  vN nU$  GN N	7f)a  
set margin mode(symbol)
:param str marginMode: margin mode must be either [isolated, cross]
:param str symbol: unified market symbol of the market the position is held in, default is None
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.leverage]: the rate of leverage, is required if setting trade mode(symbol)
:param str [params.vaultAddress]: the vault address
:param str [params.subAccountAddress]: sub account user address
:returns dict: response from the exchange
Nz+ setMarginMode() requires a symbol argumentr  z. setMarginMode() requires a leverage parameterro  crossupdateLeverager   r>  isCrossr  r   r  rA  rY  r  r  )r"   r2   r  r/  rp  r  r  r  rF  
startswithr  r  r  )r  r   r&  rV  r/  r  r>  r  r  updateActionr  rg  rW  rX  s                 r  set_margin_modehyperliquid.set_margin_mode  sb     >#DGG.[$[\\!!!V$$$VZ8#DGG.^$^__!!&"23(!!#6J<0$ 	
 #>>vXfh{|#&&t,,+33D"=''\J	""
 #&2N#11':: M 	": ;s"   /D9D4C;D9-D7.D97D9r  c                 .  #    Uc  [        U R                  S-   5      eU R                  5       I Sh  vN   U R                  U5      nU R	                  USS5      nUS:H  nU R                  US   5      nU R                  5       nU R                  US5      nSUUUS.n	Sn
U R                  USS	S
5      u  pU R                  U
5      n
U R                  XU
5      nU	UUS.nU
b  U R                  US	5      nXS	'   U R                  U5      I Sh  vN nU$  N N7f)aS  
set the level of leverage for a market
:param float leverage: the rate of leverage
:param str symbol: unified market symbol
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.marginMode]: margin mode must be either [isolated, cross], default is cross
:returns dict: response from the exchange
Nz) setLeverage() requires a symbol argumentr   r  ro  r  r  r   r  rA  r  )r"   r2   r  r/  r,  r  r  r  rF  r  r  r  )r  r  r&  rV  r/  r   r  r>  r  r  r  rg  rW  rX  s                 r  set_leveragehyperliquid.set_leverage=  s>     >#DGG.Y$YZZ!!!V$%%flGD
(!!&"23!!#6<0$ 	
 #>>v}Vdfyz00>''\J	""
 #YYv~6F&2N#11':: G 	"4 ;s"   /DDCD
DDDc                 D   #    U R                  XSU5      I Sh  vN $  N7f)a  
add margin

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin

:param str symbol: unified market symbol
:param float amount: amount of margin to add
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.vaultAddress]: the vault address
:param str [params.subAccountAddress]: sub account user address
:returns dict: a `margin structure <https://docs.ccxt.com/#/?id=add-margin-structure>`
addNmodify_margin_helperr  r&  rG  rV  s       r  
add_marginhyperliquid.add_marginm  s"      ..vufMMMM     c                 D   #    U R                  XSU5      I Sh  vN $  N7f)a  

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin

remove margin from a position
:param str symbol: unified market symbol
:param float amount: the amount of margin to remove
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.vaultAddress]: the vault address
:param str [params.subAccountAddress]: sub account user address
:returns dict: a `margin structure <https://docs.ccxt.com/#/?id=reduce-margin-structure>`
reduceNr   r  s       r  reduce_marginhyperliquid.reduce_margin|  s"      ..vxPPPPr  c                 `  #    U R                  5       I S h  vN   U R                  U5      nU R                  US   5      nU R                  [        R                  " U R                  X5      S5      5      nUS:X  a  U* nU R                  5       nSUSUS.n	S n
U R                  USSS	5      u  pU R                  U
5      n
U R                  XU
5      nU	UUS
.nU
b  XS'   U R                  U5      I S h  vN nU R                  U R                  X5      SU R                  US5      05      $  GN N:7f)Nro  rh  r  updateIsolatedMarginT)r   r>  r  ntlimodifyMarginr  rA  r  rH  r  )r  r/  r  r,   r  rQ  r  rF  r  r  r  rR  parse_margin_modificationr,  )r  r&  rG  r   rV  r/  r>  r  r  r  r  rg  rW  rX  s                 r  r   hyperliquid.modify_margin_helper  sM    !!!V$!!&"23w11$2J2J62Z\efg8B!!#*	
 #>>v~Wegz{00>''\J	""
 #&2N#11':: {{499(KD$$Xx8N
  	E 	"2 ;s"   D.D)CD.1D,28D.,D.r[  c                 \    UU R                  S U5      S SS S U R                  US5      S S S S.
$ )Nr  r  )
r   r&  r   r   rG  r  rH  r  r  r  )r  r,  )r  r[  r/  s      r  r  %hyperliquid.parse_margin_modification  sF     &&tV4$$$VX6
 	
r!  rH  fromAccount	toAccountc                   #    U R                  5         U R                  5       I Sh  vN   U R                  U R                  S5      nU R	                  5       nU R                  U/ SQ5      (       a  U R                  U/ SQ5      (       d  [        U R                  S-   5      eU R                  U5      nU R                  USS5      n	U	b  U R                  U	5      n	US-   U	-   nUS:H  =(       d    US	:H  n
U(       a  S
OSUU
US.nU R                  U5      nUS   SSUU
US.UUS.nU R                  U5      I Sh  vN nU$ SnSnUS:X  a  UnSnO!US:X  a  UnO[        U R                  S-   5      eU R                  U5        Ub  UR                  5       S:X  a~  U R                  [         R"                  " U R                  U5      S5      5      nSUUUS.nU R%                  UU5      nUUUS.nU R                  U5      I Sh  vN nU R'                  U5      $ U R)                  U5      nSUUUU R                  U5      S.nU R%                  UU5      nUUUS.nU R                  U5      I Sh  vN nU R'                  U5      $  GNX GNJ N N7f)a^  
transfer currency internally between wallets on the same account

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-usdc-transfer

:param str code: unified currency code
:param float amount: amount to transfer
:param str fromAccount: account to transfer from *spot, swap*
:param str toAccount: account to transfer to *swap, spot or address*
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.vaultAddress]: the vault address for order
:returns dict: a `transfer structure <https://docs.ccxt.com/#/?id=transfer-structure>`
Nr   )r?   rA   perpz. transfer() only support spot <> swap transferr  rA  z subaccount:r  rA   r  r  )r  rG  r  r  r  r  usdClassTransfer)r  r  r   rG  r  r  r  FmainTz4 transfer() only support main <> subaccount transferr  rh  subAccountTransfer)r   subAccountUser	isDepositrj  subAccountSpotTransfer)r   r  r  tokenrG  )rO  r  r  r  r  in_arrayr'   r2   r  r  r  r  r  check_addressr   r  r,   r  r  parse_transferr&  )r  rH  rG  r  r  rV  r  r  	strAmountr  r  transferPayloadtransferSigtransferRequesttransferResponser  rA  rj  r  r  rW  rX  r&  s                          r  r   hyperliquid.transfer  s     	'')!!!t||]C!!#==&>??==,DEE"477-]#]^^--f5I--fnFYZL'#88F%6E	6)CyF/BF1>II# 	%O 77HK )88J(K(1.'$" (%O &*%=%=o%NN##	 &  )I&  +tww)__``,-<4::<61##G$6$6t7L7LV7TV_$`aC,"3&	F %%fe4C  G
 "55g>>H &&x00 [[&F0"3&//7F %%fe4C  G
 "55g>>H&&x00e 	"@  O8 ?* ?sH   $JI>DJ7J8CJ>J?A)J(J)JJJJr   r  c           
          US S S S S S S SS.	$ )Nok)	r   r2   r  r  r  rG  r  r  r  rr  )r  r   r  s      r  r  hyperliquid.parse_transfer(  s)    
 

 
	
r!  r  tagc                   #    U R                  5         U R                  5       I Sh  vN   U R                  U5        Ub.  UR                  5       nUS:w  a  [	        U R
                  S-   5      eSnU R                  USS5      u  peU R                  U5      nU R                  US5      nU R                  5       n0 nSn	Ub  SSU-   SUS	.nU R                  X5      n	O\U R                  U R                  S
S5      n
U
(       a  SOSU[        U5      US.nU R                  U5      n	US   SU[        U5      USS.nUUU	S.nU R                  U5      I Sh  vN nU R!                  U5      $  GN@ N7f)a  
make a withdrawal(only support USDC)

https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request
https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-or-withdraw-from-a-vault

:param str code: unified currency code
:param float amount: the amount to withdraw
:param str address: the address to withdraw to
:param str tag:
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.vaultAddress]: vault address withdraw from
:returns dict: a `transaction structure <https://docs.ccxt.com/#/?id=transaction-structure>`
Nr  z withdraw() only support USDCr   r  vaultTransferrY  F)r   r  r  rj  r   r  r  )r  r  rG  r  r  r  	withdraw3)r  r  r  rG  r  r   r  )rO  r  r  r   r'   r2   r  r  r  r  r  r  r  rq  r  r  parse_transaction)r  rH  rG  r  r)  rV  r  r  r  r  r  r  rW  rX  s                 r  r   hyperliquid.withdraw8  s     	'')!!!7#::<Dv~"477-L#LMM#<<VZQ_`00>6>2!!##' $| 3"	F %%f4C NN4<<NM1>II&f+	G ))'2C$+,>$?$-&f+#F 

 11'::%%h//Y 	"V ;s"   $E+E&D)E+E)E+)E+transactionc                    U R                  US5      nU R                  US0 5      nS nU R                  US5      nUb  SUS.nS nU R                  US5      nUb  US:H  n0 SU_S	S _S
U R                  US5      _SU_SU R                  U5      _SS _SS _SU R                  US5      _SU R                  US5      _SS _SS _SS _SS _SU R	                  US5      _SS _SU R                  US5      _SS _S UUS.E$ )Nr  deltarM  r  r  r   r   internalTransferr   r2   txidr\  r  r  networkr  	addressTor  addressFromr  r)  tagTotagFromrG  usdcr  r  updated)commentinternalrM  )rp  r-  r,  r  r  )	r  r/  r  r  r1  rM  feeCostr=  r   s	            r  r-  hyperliquid.parse_transactionv  s    %%k6:	{GR8##E51"C v. 22H
K
$
 D$$[&9
 	

 Y/
 t
 t
 ))%?
 4++E6:
 4
 T
 t
 D
 d&&uf5
 
  d&&{H=!
" t#
$  )
 	
r!  c                 V  #    U R                  5       I Sh  vN   SnU R                  SU5      u  p2U R                  U5      nSUS.nU R                  U R	                  XR5      5      I Sh  vN nU R                  US5      U R                  US5      S.nU R                  Xt5      $  N N<7f)a  
fetch the trading fees for a market
:param str symbol: unified market symbol
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.user]: user address, will default to self.walletAddress if not provided
:param str [params.subAccountAddress]: sub account user address
:returns dict: a `fee structure <https://docs.ccxt.com/#/?id=fee-structure>`
Nr   userFeesr  userCrossRateuserAddRate)rB  rC  )r  r  r/  rQ  rR  r,  parse_trading_fee)r  r&  rV  r  r/  rW  rX  r[  s           r  fetch_trading_feehyperliquid.fetch_trading_fee  s      !!!"889JFSV$
 ,,T[[-IJJJ "--hH++HmD
 %%d33a 	" Ks"   B)B%AB)*B'+;B)'B)rM  c                 x    U R                  S U5      nUUU R                  US5      U R                  US5      S S S.$ )NrC  rB  )r   r&  r   r   r  	tierBased)r  r  )r  rM  r/  r&  s       r  rD  hyperliquid.parse_trading_fee  sM    H !!$/%%c=9%%c?;
 	
r!  c                 T  #    U R                  5       I Sh  vN   SnU R                  SU5      u  pTSUS.nUb  X&S'   U R                  US5      nUb  XvS'   U R                  US/5      nU R	                  U R                  Xd5      5      I Sh  vN nU R                  USX#5      $  N N7f)a,  
fetch the history of changes, actions done by the user or operations that altered the balance of the user
:param str [code]: unified currency code
:param int [since]: timestamp in ms of the earliest ledger entry
:param int [limit]: max number of ledger entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: timestamp in ms of the latest ledger entry
:param str [params.subAccountAddress]: sub account user address
:returns dict: a `ledger structure <https://docs.ccxt.com/#/?id=ledger>`
Nrq   userNonFundingLedgerUpdatesr  r0  r.  r1  )r  r  rp  r  rQ  rR  parse_ledger)	r  rH  r,  r  rV  r  rW  r.  rX  s	            r  fetch_ledgerhyperliquid.fetch_ledger  s      !!!"88O1
 #(K !!&'2!&IYYvy1F,,T[[-IJJ   4>>7 	" Ks"   B(B$A5B(B&B(&B(itemc                    U R                  US5      nU R                  US0 5      nS nU R                  US5      nUb  SUS.nU R                  US5      nU R                  US5      nU R                  UU R                  US5      S S U R                  US	5      U R                  US5      U R	                  U5      S U R                  U5      UU R                  U5      S S S US
.U5      $ )Nr  r1  rM  r  r2  r   r:  r\  r  )r   r2   	directionr  referenceAccountreferenceIdr   r  rG  r  r  beforeafterr  rM  )rp  r-  r,  safe_ledger_entryparse_ledger_entry_typer  r  )	r  rO  r  r  r1  rM  r>  r   rG  s	            r  parse_ledger_entryhyperliquid.parse_ledger_entry:  s    %%dF3	tWb1##E51"C v.!!%0%%""40 $ 0 0 ?++D&9006''/"Y/'
  ! 	r!  c                 0    SSS.nU R                  X!U5      $ )Nr   )r3  accountClassTransferr  )r  r   
ledgerTypes      r  rW  #hyperliquid.parse_ledger_entry_typec  s$     *$.

 
$77r!  c                   #    U R                  5       I Sh  vN   SnU R                  SU5      u  pTSUS.nUb  X&S'   U R                  US5      nUb2  Uc  [        U R                  S-   5      eXvS'   U R                  US/5      nU R                  U R                  Xd5      5      I Sh  vN nU R                  U5      n	Sn
U R                  USS	5      u  pU R                  U
5      n
/ nU
b]  [        S
[        U	5      5       HC  nX   nUS   S:X  d  M  U R                  US5      nUS   SU
-   :X  d  M2  UR                  U5        ME     OU R                  U	SS/S5      nU R!                  USX#5      $  GNY N7f)aI  
fetch all deposits made to an account
:param str code: unified currency code
:param int [since]: the earliest time in ms to fetch deposits for
:param int [limit]: the maximum number of deposits structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: the latest time in ms to fetch withdrawals for
:param str [params.subAccountAddress]: sub account user address
:param str [params.vaultAddress]: vault address
:returns dict[]: a list of `transaction structures <https://docs.ccxt.com/#/?id=transaction-structure>`
NfetchDepositsWithdrawalsrK  r  r0  r.  z0 fetchDeposits requires since while until is setr1  r  r   r   vaultDepositr1  vaultrY  rK  F)r  r  rp  r"   r2   r  rQ  rR  extract_type_from_deltar  r  rT  r:  r-  r_  r  parse_transactions)r  rH  r,  r  rV  r  rW  r.  rX  recordsr  depositsrZ  recordr1  s                  r  fetch_depositshyperliquid.fetch_depositsj  s     !!!"889SU[\1
 #(K !!&'2}'2d(dee!&IYYvy1F,,T[[-IJJ ..x8#<<VE_aop00>#1c'l+ &>^3 NN67;EW~)<< / , ++GVi[%PH&&xuDDW 	" Ks/   E4E/BE4'E2(A%E4E41?E42E4c                   #    U R                  5       I Sh  vN   SnU R                  SU5      u  pTSUS.nUb  X&S'   U R                  US5      nUb  XvS'   U R                  US/5      nU R	                  U R                  Xd5      5      I Sh  vN nU R                  U5      n	Sn
U R                  USS5      u  pU R                  U
5      n
/ nU
b]  [        S	[        U	5      5       HC  nX   nUS
   S:X  d  M  U R                  US5      nUS   SU
-   :X  d  M2  UR                  U5        ME     OU R                  U	S
S/S5      nU R                  USX#5      $  GN> N7f)aT  
fetch all withdrawals made from an account
:param str code: unified currency code
:param int [since]: the earliest time in ms to fetch withdrawals for
:param int [limit]: the maximum number of withdrawals structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: the latest time in ms to fetch withdrawals for
:param str [params.subAccountAddress]: sub account user address
:param str [params.vaultAddress]: vault address
:returns dict[]: a list of `transaction structures <https://docs.ccxt.com/#/?id=transaction-structure>`
Nr_  rK  r  r0  r.  r1  r  r   r   vaultWithdrawr1  ra  rY  r   F)r  r  rp  r  rQ  rR  rb  r  r  rT  r:  r-  r_  r  rc  )r  rH  r,  r  rV  r  rW  r.  rX  rd  r  withdrawalsrZ  rf  r1  s                  r  fetch_withdrawalshyperliquid.fetch_withdrawals  s}     !!!"889SU[\1
 #(K !!&'2!&IYYvy1F,,T[[-IJJ ..x8#<<VE_aop00>#1c'l+ &>_4 NN67;EW~)<<#**62 , ..weTK&&{D%GGS 	" Ks/   EEA5EEA%E6E?EEc                    #    U R                  5       I Sh  vN   U R                  U5      nU R                  5       I Sh  vN nU R                  X15      $  N> N7f)z
Retrieves the open interest for a list of symbols
:param str[] [symbols]: Unified CCXT market symbol
:param dict [params]: exchange specific parameters
:returns dict} an open interest structure{@link https://docs.ccxt.com/#/?id=open-interest-structure:
N)r  r  r`  parse_open_interests)r  r  rV  swapMarketss       r  fetch_open_interests hyperliquid.fetch_open_interests  sS      !!!%%g. 3355((>> 	"5s    AA(AA AAc                    #    U R                  U5      nU R                  5       I Sh  vN   U R                  U/U5      I Sh  vN nX1   $  N# N
7f)z
retrieves the open interest of a contract trading pair
:param str symbol: unified CCXT market symbol
:param dict [params]: exchange specific parameters
:returns dict: an `open interest structure <https://docs.ccxt.com/#/?id=open-interest-structure>`
N)r&  r  rq  )r  r&  rV  oiss       r  fetch_open_interesthyperliquid.fetch_open_interest  sN      V$!!!--vh??{ 	"?s!   %AAAA	AAc           	          U R                  US0 5      nU R                  US5      nS nUb  U R                  U5      nU R                  U R	                  U5      U R                  US5      S S S US.U5      $ )Nr   r3   openInterest)r&  openInterestAmountopenInterestValuer  r  r   )r-  r,  r  safe_open_interestr  r  )r  interestr/  r  r4  s        r  parse_open_interesthyperliquid.parse_open_interest  s    & >>(FB7&1--d3H&&&&x0"&"2"28^"L!%(
  	r!  c                 |  #    U R                  5       I Sh  vN   SnUb  U R                  U5      nSnU R                  SU5      u  pdUSS.nUb  X'S'   U R                  US5      nU R	                  US5      nUb  XS'   U R                  U R                  Xt5      5      I Sh  vN n	U R                  XX#5      $  N N7f)a   
fetch the history of funding payments paid and received on self account
:param str [symbol]: unified market symbol
:param int [since]: the earliest time in ms to fetch funding history for
:param int [limit]: the maximum number of funding history structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.subAccountAddress]: sub account user address
:returns dict: a `funding history structure <https://docs.ccxt.com/#/?id=funding-history-structure>`
Nrj   userFunding)r  r   r0  r.  r1  )r  r/  r  rp  r  rQ  rR  parse_incomes)
r  r&  r,  r  rV  r/  r  rW  r.  rX  s
             r  fetch_funding_history!hyperliquid.fetch_funding_history  s      !!![[(F"889NPVW!
 #(K !!&'267+!&I,,T[[-IJJ" !!(EAAC 	"  Ks"   B<B8B
B<!B:"B<:B<c           	         U R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nUS-   nU R                  U5      nUS   nU R                  US5      n	U R	                  S5      n
U R                  US	5      nUUU
UU R                  U5      UU R                  U	5      US
.$ )Nr\  r  r1  r  
/USDC:USDCr&  r:  r  r  )r   r&  rH  r  r  r2   rG  r  )r,  rp  r-  r;  r.  r  r  r  )r  incomer/  r2   r  r1  ro  marketSymbolr&  rG  rH  r  s               r  parse_incomehyperliquid.parse_income@  s     ff-%%ff5	vw/!!%0,!!,/!!!%0&&v.}5"Y/''/	
 		
r!  weightc                    #    U R                  5       nSU0nSUS.nU R                  XS5      nXTS'   XdS'   U R                  U R                  XB5      5      I Sh  vN nU$  N7f)a|  
Instead of trading to increase the address based rate limits, self action allows reserving additional actions for 0.0005 USDC per request. The cost is paid from the Perps balance.
:param number weight: the weight to reserve, 1 weight = 1 action, 0.0005 USDC per action
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a response object
r  reserveRequestWeight)r   r  r  rg  N)r  r  r  rR  )r  r  rV  r  rW  r  rg  rX  s           r  reserve_request_weight"hyperliquid.reserve_request_weightd  sy      !!#U
 +
 ''6	"(11$++g2NOO Ps   AA!AA!c                     / n[        S[        U5      5       H#  nX   nUS   S   US'   UR                  U5        M%     U$ )Nr   r1  r   )rT  r:  r_  )r  r[  rd  rZ  rf  s        r  rb  #hyperliquid.extract_type_from_deltay  sH    q#d)$AWF#G_V4F6NNN6" % r!  c                 ^    Uc  g UR                  S5      (       a  UR                  SS5      $ U$ )NrY  r  )r  r  )r  r  s     r  r   hyperliquid.format_vault_address  s1    ?d##??4,,r!  
methodNamerV  c                    S nU R                  X!SS5      u  p2UnU R                  X!SU5      u  pBUb	  US:w  a  XB/$ U R                  b  U R                  S:w  a  U R                  U/$ [        U R                  S-   U-   S-   5      e)Nr  rA  r  r   zF() requires a user parameter inside 'params' or the wallet address set)rF  r  r   r"   r2   )r  r  rV  userAuxr  s        r  r  !hyperliquid.handle_public_address  s    99&fVij44VT[\42:>!*1C1Cr1I&&//#
 :  >H  !H  I  	Ir!  r  c                     UR                  S5      S:  d  UR                  S5      S:  a  U$ UR                  S5      S:  a  UR                  SS5      nU R                  U5      S-   $ )Nr*  r/  @r  r  r  )findr  r.  )r  r  s     r  r  hyperliquid.coin_to_market_id  s\    99S>B$))C.2"5K99S>B<<S)D&&t,|;;r!  reasonurlrv  headersbodyc
                    U(       d  g U R                  USS5      n
U R                  US5      nS nU
S:X  a  U R                  US5      nOU
S:X  a  [        U R                  S-   U-   5      eUb  UnOoU R                  US0 5      nU R                  US0 5      nU R	                  US	/ 5      n[        S
[        U5      5       H  nU R                  UU   S5      nUc  M    O   U R                  S-   U-   nUS L=(       a    US:g  nU(       a@  U R                  U R                  S   UU5        U R                  U R                  S   UU5        U(       a  [        U5      eg )Nr  r  r  errrX  
unknownOidr  r[  r  r   r   r   )r,  r&   r2   r-  rS  rT  r:  throw_exactly_matched_exceptionr   throw_broadly_matched_exceptionr!   )r  rH  r  r  rv  r  r  rX  requestHeadersrequestBodyr  r  r]  responsePayloadr[  r  rZ  feedbacknonEmptyMessages                      r  handle_errorshyperliquid.handle_errors  sV    !!(Hb9  73U?&&x<G|## 455G"nnXz2FO>>/62>D~~dJ;H1c(m,**8A;@& - 77S=4'#4/Dgm001I7T\]001I7T\]))r!  r   GETc                     U R                  U R                  S   U   5      S-   U-   nUS:X  a  SS0nU R                  U5      nXsXeS.$ )Nr   r*  POSTzContent-Typezapplication/json)r  rv  r  r  )implode_hostnamer   json)r  pathr   rv  rV  r  r  r  s           r  signhyperliquid.sign  sZ    ##DIIe$4S$9:S@4GV 2G 99V$DdOOr!  c                 f    SU;   a  SU;   a  US   nUS   nXg;   a  Xv   $ U R                  USS5      $ )Nr   r   r   r   )r  )r  r   rv  r  rV  configr   r   s           r  calculate_rate_limiter_cost'hyperliquid.calculate_rate_limiter_cost  sD    Vv%5&>DH%F~|#vvq11r!  c                     U R                  U5      nS n	U R                  USSS5      u  pU R                  U	5      n	US   nUUUUUUS.n
0 nU	b  XS'   Ub  XS'   X/$ )NrQ   r  rA  r&  )r&  r   r  rG  r   rV  r2   )r/  rF  r  )r  r2   r&  r   r  rG  r   rV  r/  r  r  r  s               r  r  (hyperliquid.parse_create_edit_order_args  s    V$#>>v}Vdfyz00>!
 #+7(>$K$$r!  rr  )NNNN)N)T)__name__
__module____qualname____firstlineno__r   r  r#  rq  r   r/  r   r   r;  r   r\  r    rh  rb  r`  floatr  ra  dictr  r   r  r	   r   r  r   r   r   r   r  r   r  r   r  rE  r9  rC  rK  rQ  rV  r^  rh  rk  rt  r  objectr  r  r  r  r  r  r  r  r  rP  r  r  r  r   r   r   r  r   r  r  r  r1  r0  r6  r   rN  rS  r[  r_  r^  r  rk  rs  r   r~  r  r  r  r  r  r  r  r  r  r   r  r  r   r  r  r  r  r   r  r  r  r  r   r   r   r  r   r   r-  r   rE  rD  r
   rM  rX  rW  rg  rl  rq  ru  r}  r  r  r  rb  r  r  r  r  r  r  r  __static_attributes____classcell__)r  s   @r  r.   r.      sr
   P# Pd
.S _ "]C ] ]RU ]jm ]  zI ] ]* -/ ;: ;z *, V 6 /1 mT&\ m^ /1 7*T&\ 7*r(1u (1u (1[` (1T /1 gT&\ gRc4 cF cJ *, Y) Y)v @DB .fS .f .fS\ .f` 6:"  G7  G  GD <@ 79 79| 79r/
v /
 /
b4  6 : ?CQUdhqs 8] 8] 8]3 8]^a 8]x|  ~B  yC 8]t
 
4 
0 <@d[] 6A 6AS 6A 6Ap}k k k:
L

J* *X	C	C	C	C	C. 7  7#  7D  GI "7 "7H mqy{  I Y X] fi 6 FH )9$|*< )9V mqy{ 93 9i 9y 9Z] 9fi 9v 46 ^$ ^@ 9=R )S )# )$ AER -tCy -# -^ CGr 2c 2C 2VZ 2h Y[ F5d;N6O F5P BD &S &P 24 iV Z^lpy{ 3  3 c SV fi : DF 4+\(: 4+l WY $s $ $# $L >BPTcgpr 5Os 5O# 5O]` 5On 59tZ^gi 7Hc 7H 7HTW 7Hnrsxny 7Hr 7;\`ik U U3 UVY Uptuzp{ U 9=4^bkm U# US UX[ Urvw|r} U DHVZimvx US UPS Ucf U  ~B  CH  ~I U 04$UYbd !A !AC !As !Aimnsit !AF 8<B ;.C ;. ;.zV Vv V Vp: :": 37TX\eg 5AC 5As 5ARU 5An5 5v 5 5n 8: 03 0 8<B FPW FP4PX> FPPJt JV JX DHPR 3 3S 3j ?C2 .3 . .` CE Ns NE NI[ N FH Q# Qu QL^ Q LN % %Rd %N
d 
F 
N` 
& ac a13 a1 a1C a1TW a1gt a1F
t 
x 
= 
  QU]_ <03 <0 <0 <0# <0do <0|1
T 1
X 1
Q\ 1
f ;= 94c 94AT 94v,
T ,
6 ,
EX ,
\ .2SW`b &?s &?# &?S &?gklwgx &?P't 'x '; 'R8 04$UYbd 7E 7EC 7Es 7Er 37TX\eg 5HC 5Hs 5HRU 5Hlpq|l} 5Hn =A 
?' 
? =? 
 
F B 9=4^bkm +B# +BS +BX[ +BZ"
6 "
H @B 3 d * ,. C 	I 	IT 	I<c <%# %s % %c %TX %`c %N &eBSW P MO 2 @D  MO %s %C %y %Xa %kp %y| % %r!  r.   )4 ccxt.async_support.base.exchanger   ccxt.abstract.hyperliquidr   rc  r  ccxt.base.typesr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   typingr    ccxt.base.errorsr!   r"   r#   r$   r%   r&   r'   ccxt.base.decimal_to_precisionr(   r)   r*   r+   ccxt.base.preciser,   r.   rr  r!  r  <module>r     sz    6 1   K  K  K  K  K  K  K  K  * . ' . ) * ) 0 9 = 4 %K?%(K K?%r!  