
    E#ib                    L   S SK Jr  S SKJ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+J/r/  S SK0J1r1   " S S\\5      r2g)    )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    J/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/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_modem  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.marketq  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         SS0nU R                  U R                  X!5      5      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$ )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
Fr   metauniverser   r3   N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{{7'CD ~~h
B7q#d)$A>>$2.DB##D&1D**40D77!     $#
  $#!	9 FL %8 r!  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     Un/ n[        S[	        U5      5       H  nU R                  XU   5      nM     U$ )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   )
r-  r  rS  rT  r:  appendfetch_swap_marketsfetch_spot_marketsfetch_hip3_markets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 & q#h-(A&&v{;F )r!  c           
         U R                  SS05      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     U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$ )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   perpDexsr   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  parse_markets)r  rV  
fetchDexesperpDexesOffsetrZ  r<   offsetfetchDexesListr  r   defaultLimitdexesLengthdefaultDexesdexNamerd  rW  re  r+  rX  rB  rC  	assetCtxsrY  jr[  s                            r  rb  hyperliquid.fetch_hip3_markets  s    ((J*
 
* !#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 / 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 )^ r!  c           	         SS0nU R                  U R                  X!5      5      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      $ )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   rl  r   rC  r   rm  )rQ  rR  r-  rS  rT  r:  r_  rp  )
r  rV  rW  rX  rB  rC  ry  rY  rZ  r[  s
             r  r`  hyperliquid.fetch_swap_markets^  s     &
 &&t{{7'CD@ ~~h2.>>$
B7NN8Q3	q#h-(A;;xB/yR0D NMM$ ) !!&))r!  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   .0rk  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      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$ )*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   spotMetaAndAssetCtxsr   r   rC  tokensr   r3   r?   r   r   r   r*  spec
szDecimalsmidPxN   rj  r2   r&  r8  r)  settlerm  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:  rn  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  pricePrecisionStrrm  entryr8  r)  newEntryr&  s*                                             r  ra  hyperliquid.fetch_spot_markets  s    *
 &&t{{7'CD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 r!  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   rm  :-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  rn  )r  r/  r  r(  r8  r)  rm  r  r  r&  r  rA   r   r   r   r  r~  r   r  r  r  rJ  s                         r  parse_markethyperliquid.parse_marketj  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      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      $ )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  rn  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{{7'CD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  ((r!  r  c           	      |   U R                  5         U R                  U5      nSUS   (       a  US   OUS   S.nU R                  U R                  XS5      5      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      $ )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
r   rA   r(  r2   r   r  levelsr   r   )bidsasksr  r&  r  r  pxsz)load_marketsr/  rQ  rR  rS  rn  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{{7'CD. ~~h"5NN4B/NN4B/
 %%h7	$$VH-=y&RXZ^`deer!  symbolsc                    U R                  5         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      nO)US:X  a  U R                  U5      nOU R                  U5      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      $ )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>`
r   r?   rA   r   r   r&  )r  market_symbolsr,  omitra  r`  rf  rT  r:  parse_tickerfilter_by_array_tickers)r  r  rV  rX  r   rY  rZ  r/  r   tickerr&  s              r  fetch_tickershyperliquid.fetch_tickersZ  s     	%%g./66*6>..v6HV^..v6H))&1Hq#h-(A[F&>D&&t4F%%fh7F#6N ) ++FHgFFr!  c           	         SS0nU R                  U R                  X25      5      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      $ )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   rl  r   rC  r   )rQ  rR  r-  rS  rT  r:  r_  parse_funding_rates)r  r  rV  rW  rX  rB  rC  ry  rY  rZ  r[  s              r  fetch_funding_rateshyperliquid.fetch_funding_rates|  s     &
 &&t{{7'CD@ ~~h2.>>$
B7NN8Q3	q#h-(A;;xB/yR0D MM$ ) ''88r!  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                    U R                  5         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      nU R                  XX)XH5      $ )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
untilNr
  r   Fr   rA   r(  r2   )r  r  	startTimeendTime)r   req)r  r/  rn  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  s    	V$!!&'43D3D3FG4-= *.*>*>y*ID*P'%(G"(LM19E6G9-$.4Vnz*&, ,,T__iS" 	
 &&t{{7'CD"   9U\\r!  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)rn  r  )r  ohlcvr/  s      r  parse_ohlcvhyperliquid.parse_ohlcv=  sn      eS)UC(UC(UC(UC(UC(
 	
r!  c                 V   SnU R                  SU5      u  pTU R                  5         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      n	U R                  XX#5      $ )
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   r.  	userFillsr,  r/  r  r  r;  rn  r  rQ  rR  parse_trades
r  r&  r*  r  rV  r  r/  rW  r,  rX  s
             r  fetch_tradeshyperliquid.fetch_tradesU  s    " "88O!!&)K
 /GFO#(K )GFO!!&'267+!&I&&t{{7'CD*   5@@r!  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   rk  r?   r  r  rI  rG  )r/  r  r  r	  r:  rL  r(   r*   rM  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	secp256k1rV  rs   r?  )r`  ra  r?  )ecdsar2  )r  rY  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      $ )Nr^  )re  r[  )r  rZ  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  rY  	isTestnetrm  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01rW  binary)packbbinary_to_base16int_to_base16rY  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   Agentrm  stringr3   r   rn  bytes32)r  r  r  rq  r,  eth_encode_structured_datarh  r   )r  r|  r~  vaultAdressrY  rp  phantomAgentr   r  domainmessageTypesmsgrd  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  rh  r   )r  r  rZ  r   r  r  r  rd  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  rZ  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                 T   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      nU$ ! [         a  nS n S nAU$ S nAff = f)	NrefSetFTsetReferrerrefCCXT1)r   rH  r|  r~  rd  )r  r  r,  r  r  privatePostExchange	Exception)r  r|  r~  rd  rW  rX  es          r  set_refhyperliquid.set_ref,  s    >>$,,%88!%X!$$T\\5'B
 !!#''6	"

 	//8HO 	H	s   =B 
B'B""B'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      $ )Nr   FTestnetMainnet)r  r  r  r~  r  0x66eeer  r  approveBuilderFee)r  signatureChainIdr  r  r~  r   r|  r~  rd  r}  )r  r  r  r  r  )	r  r  r  r~  isSandboxModepayloadsigr|  rW  s	            r  approve_builder_feehyperliquid.approve_builder_feeC  s    !!#t||]EJ-:		$	
 009 '(: ; )!,/y)'
  	
 ''00r!  c                 t     U R                  5       U R                  5       /  g! [         a
  n S nAgS nAff = f)NFT)handle_builder_fee_approvalr  r  )r  r  s     r  rP  hyperliquid.initialize_cliente  s8    	--/@   		s    # 
77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        SU R                  S'   g! [         a  nSU R                  S'    S nAgS nAff = 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_approvall  s    >>$,,dC!^^DLL:NPUV	/&&t||Y@lmG))$,,	7KJ$$W915DLL-.   	/).DLL&	/s   AB' '
C
1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      $ )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(
  	
 ''00r!  r   siderG  c           	      ~    U R                  5         U R                  SXX4XV5      u  pxU R                  U/U5      n	U	S   $ )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  sI    , 	"??fTXbgp##UG\:ayr!  r  c                    U R                  5         U R                  5         U R                  X5      nU R                  U5      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      $ )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>`
rX  r[  statusesr   waitingForTriggerstatusN)
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<++G4$ nnXz2>~~k626>>$
B7q#h-(AKE++ ''5(9: ''. )   !1488r!  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  rm  rl  rk  pra  r`  r:  r>  )r/  uppersafe_string_2r,  r  safe_string_lower
capitalizer"   r2   r,   
string_mul
string_add
string_subrS  rN  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
   )rk  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  rn  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walletrd  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                 L    U R                  U/X#5      nU R                  US5      $ )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>`
r   )cancel_ordersr-  )r  r2   r&  rV  r  s        r  cancel_orderhyperliquid.cancel_order  s)     ##RD&9~~fa((r!  idsc                    U R                  5         Uc  [        U R                  S-   5      eU R                  5         U R	                  5         U R                  XU5      nU R                  U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       H*  n
X   nU	R                  U R                  UUS.5      5        M,     U	$ )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>`
z* 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  r0  r&  rV  rW  rX  innerResponser[  r  r  rZ  r  s               r  r-  hyperliquid.cancel_orders  s     	'')>#DGG.Z$Z[[ ,,S&A++G4 x<~~mV4>>$
3q#h-(A[FMM$// +   ) r!  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   cancelsrm  NcancelByCloidr   r   )assetcloidcancel)rl  r;  r9  rJ   r}  subAccountAddressr|  rd  )r/  safe_value_2r  r  parse_to_numeric
isinstancelistrT  r:  r_  handle_option_and_params_2r  r  )r  r0  r&  rV  r/  r  r~  rW  	cancelReqcancelActionrm  rZ  r}  rd  s                 r  r3  !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         U R                  5         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      nU R%                  SU05      /$ )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>`
r~  r  r8  Fr   r  NTr2   r&  zB cancelOrdersForSymbols() requires a symbol argument in each orderzI cancelOrdersForSymbols() all orders must have either id or clientOrderIdr;  rl  r<  r;  rm  r:  r=  r   r9  rK   r}  r>  r|  rd  r   )rO  r  rP  r  rT  r:  r,  r"   r2   r#   r/  r@  r_  rC  r  r  r  r  r4  )r  r  rV  r~  rW  rD  rE  r:  rZ  r  r  r2   r&  assetKeyidKeyr/  	cancelObjr}  rd  rX  s                       r  cancel_orders_for_symbols%hyperliquid.cancel_orders_for_symbols  s    	'') !!#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#++G4  2344r!  timeoutc                    U R                  5         U R                  5         U R                  5         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      nU$ )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
r  r  r~  scheduleCancel)r   r  NrH   r}  r>  r|  rd  )	rO  r  rP  r  r  rC  r  r  r  )	r  rM  rV  r~  rW  rE  r}  rd  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#++G4 r!  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  rm  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  rS  rN  r  r_  r  r  r  r  )(r  r  rV  r  rZ  r  r  r  rU  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}  rd  rW  s(                                           r  edit_orders_requesthyperliquid.edit_orders_requestf  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         Uc  [        U R                  S-   5      eU R                  XX4XVU5      u  pU R	                  U/U	5      n
U
S   $ )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>`
z$ 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  sc    , 	:#DGG.T$TUU"??DX^gmn!!5'<8ayr!  c                    U R                  5         U R                  5         U R                  X5      nU R                  U5      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      $ )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>`
rX  r[  r  )r  rP  rX  r  r-  rS  r  )r  r  rV  rW  rX  responseObject
dataObjectr  s           r  r[  hyperliquid.edit_orders  s     	 **6:++G4H *bA^^NFB?
>>*j"=  **r!  r3   description
initialUsdc                 Z   U R                  5         U R                  5         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      n
U
$ )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
r~  1000000createVault)r   r3   rb  rc  r~  r|  rd  )
rO  r  r  r  r,   r  r  r  r  rR  )r  r3   rb  rc  rV  r~  rW  usdr|  rd  rX  s              r  create_vaulthyperliquid.create_vault$	  s     	'')!!#U
  2 243H3H3TV_ `a!&
 ''6	"(++DKK,HI r!  c           
         U R                  5         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      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      $ )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  r.  i  r  r
  r,  r/  r   r  r  )r   r&  r  r  r  r  )r  r"   r2   r/  r  rn  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_historyJ	  sq    	>#DGG.e$effV$$:&
 #(K $}s5H#'#4#4#6B9Kd9R#RGK !!&'267+!&I&&t{{7'CD q#h-(AKE))%8IMM**48#//}E& LL3  ) fk200NNr!  c                 f   SnU R                  SU5      u  pTSnU R                  USSS5      u  pdU R                  5         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      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      $ )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  rs  rW  r/  r(  partpartsLengthrX  orderWithStatusrZ  r  extendOrders                    r  fetch_open_ordershyperliquid.fetch_open_orders	  sG     "889JFS66v?PRZ\pq
 [[(F''
B?H>>#&Dd)KQ!%!1!1$!:&&t{{7'CD q#h-(AKEKx08,2L)""4;;uk#BC )   %GGr!  c                     U R                  5         U R                  USSU5      nU R                  USS/S5      nU R                  XaX#5      $ )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_arrayrm  r  r&  r*  r  rV  r  closedOrderss          r  fetch_closed_ordershyperliquid.fetch_closed_orders	  sQ     	""64v>++FHxj%P00uTTr!  c                     U R                  5         U R                  USSU5      nU R                  USS/S5      nU R                  XaX#5      $ )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	  sQ     	""64v>++FHzlER00uTTr!  c                     U R                  5         U R                  USSU5      nU R                  US/ SQS5      nU R                  XaX#5      $ )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	  sQ     	""64v>++FH>`bgh00uTTr!  c                     SnU R                  SU5      u  pTU R                  5         U R                  U5      nSUS.nU R                  U R	                  Xt5      5      nU R                  XX#5      $ )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	  sw     "88O!!&)&
 &&t{{7'CD   5@@r!  c                    SnU R                  SU5      u  pCU R                  5         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      n	U R                  U	S5      n
U R                  X5      $ )	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  rS  "   r  )r  r  r;  r,  r  r:  r@  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{{7'CD8 ~~h0--r!  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  r2  )r  restingfilledr  r2   r&  r  r  rv  r  Ar  r  origSztotalSzr  r  ALOr   rS  r  r<  r  lastTradeTimestamplastUpdateTimestampstatusTimestampr   r  r  r  r  r   limitPxr   r  r  rG  r   avgPx)averager  	remainingr  rM  trades)r,  r4  safe_dict_nr  r;  rn  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_orderG
  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      $ )Nru  r~  r  r  )	triggeredr  ru  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                 V   SnU R                  SU5      u  pTU R                  5         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      n	U R                  XX#5      $ )
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  rD  r   r.  rE  r,  r/  rF  rH  s
             r  fetch_my_tradeshyperliquid.fetch_my_trades
  s     "88&Q!!&)K
 /GFO#(K )GFO!!&'267+!&I&&t{{7'CD,   5@@r!  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   rS  feeToken)r   currencyrate)r   r  r  r&  r2   r  r   r  takerOrMakerr   rG  r   rM  )rn  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                 N    U R                  U/U5      nU R                  US0 5      $ )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>`
r   )fetch_positionsr-  )r  r&  rV  	positionss       r  fetch_positionhyperliquid.fetch_positione  s+     ((&6:	~~iB//r!  c                    U R                  5         SnU R                  SU5      u  p2U R                  U5      nSUS.nU R                  U R	                  XB5      5      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      $ )	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_positionss  s     	"889I6R%%g.(
 &&t{{7'CD\ ~~h(8"=q#d)$AMM$--dgt<= %--fhOOr!  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                 D   Uc  [        U R                  S-   5      eU R                  5         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      nU$ )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 parameterrm  crossupdateLeverager   r;  isCrossr  r   r}  r>  rV  r  r  )r"   r2   r  r/  rn  r  r  r  rC  
startswithr  r  r  )r  r   r&  rV  r/  r  r;  r  r~  updateActionr}  rd  rW  rX  s                 r  set_margin_modehyperliquid.set_margin_mode  sK    >#DGG.[$[\\V$$$VZ8#DGG.^$^__!!&"23(!!#6J<0$ 	
 #>>vXfh{|#&&t,,+33D"=''\J	""
 #&2N#++G4 r!  r  c                    Uc  [        U R                  S-   5      eU R                  5         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      nU$ )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  rm  r  r  r   r}  r>  r  )r"   r2   r  r/  r,  r  r  r  rC  r  r  r  )r  r  r&  rV  r/  r   r  r;  r~  r  r}  rd  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#++G4 r!  c                 (    U R                  XSU5      $ )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>`
addmodify_margin_helperr  r&  rG  rV  s       r  
add_marginhyperliquid.add_marginl  s     ((GGr!  c                 (    U R                  XSU5      $ )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>`
reducer  r  s       r  reduce_marginhyperliquid.reduce_margin{  s     ((6JJr!  c                 .   U R                  5         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      nU R                  U R                  X5      SU R                  US5      05      $ )Nrm  re  r  updateIsolatedMarginT)r   r;  r  ntlimodifyMarginr}  r>  r  rH  r  )r  r/  r  r,   r  rN  r  rC  r  r  r  rR  parse_margin_modificationr,  )r  r&  rG  r   rV  r/  r;  r  r~  r  r}  rd  rW  rX  s                 r  r   hyperliquid.modify_margin_helper  s6   V$!!&"23w11$2J2J62Z\efg8B!!#*	
 #>>v~Wegz{00>''\J	""
 #&2N#++G4 {{499(KD$$Xx8N
  	r!  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         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      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  av  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      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      nU R'                  U5      $ )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>`
r   )r?   rA   perpz. transfer() only support spot <> swap transferr}  r>  Nz 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  re  subAccountTransfer)r   subAccountUser	isDepositrg  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  r>  rg  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  $77H##	 &  )I&  +tww)__``,-<4::<61##G$6$6t7L7LV7TV_$`aC,"3&	F %%fe4C  G
 //8H &&x00 [[&F0"3&//7F %%fe4C  G
 //8H&&x00r!  r   r  c           
          US S S S S S S SS.	$ )Nok)	r   r2   r  r  r  rG  r  r  r  ro  )r  r   r  s      r  r  hyperliquid.parse_transfer'  s)    
 

 
	
r!  r  tagc                    U R                  5         U R                  5         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      nU R!                  U5      $ )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}  vaultTransferrV  F)r   r}  r  rg  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  ro  r  r  parse_transaction)r  rH  rG  r  r$  rV  r}  r~  r|  r  r  r  rW  rX  s                 r  r   hyperliquid.withdraw7  sw    	'')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 

 ++G4%%h//r!  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   txidrY  r  r  networkr  	addressTor  addressFromr  r$  tagTotagFromrG  usdcr  r  updated)commentinternalrM  )rn  r-  r,  r  r  )	r  r*  r  r  r,  rM  feeCostr8  r   s	            r  r(  hyperliquid.parse_transactionu  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                 &   U R                  5         SnU R                  SU5      u  p2U R                  U5      nSUS.nU R                  U R	                  XR5      5      nU R                  US5      U R                  US5      S.nU R                  Xt5      $ )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)r=  r>  )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{{7'CDJ "--hH++HmD
 %%d33r!  rM  c                 x    U R                  S U5      nUUU R                  US5      U R                  US5      S S S.$ )Nr>  r=  )r   r&  r   r   r  	tierBased)r  r  )r  rM  r/  r&  s       r  r?  hyperliquid.parse_trading_fee  sM    H !!$/%%c=9%%c?;
 	
r!  c                 $   U R                  5         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      nU R                  USX#5      $ )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  r.  r,  r/  )r  r  rn  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{{7'CD   4>>r!  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  r,  rM  r  r-  r   r5  rY  r  )r   r2   	directionr  referenceAccountreferenceIdr   r  rG  r  r  beforeafterr  rM  )rn  r-  r,  safe_ledger_entryparse_ledger_entry_typer  r  )	r  rJ  r  r  r,  rM  r9  r   rG  s	            r  parse_ledger_entryhyperliquid.parse_ledger_entry9  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   )r.  accountClassTransferr  )r  r   
ledgerTypes      r  rR  #hyperliquid.parse_ledger_entry_typeb  s$     *$.

 
$77r!  c                    U R                  5         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      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      $ )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fetchDepositsWithdrawalsrF  r  r.  r,  z0 fetchDeposits requires since while until is setr/  r}  r   r   vaultDepositr,  vaultrV  rK  F)r  r  rn  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  recordr,  s                  r  fetch_depositshyperliquid.fetch_depositsi  s}    	"889SU[\1
 #(K !!&'2}'2d(dee!&IYYvy1F&&t{{7'CD ..x8#<<VE_aop00>#1c'l+ &>^3 NN67;EW~)<< / , ++GVi[%PH&&xuDDr!  c                    U R                  5         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      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      $ )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>`
NrZ  rF  r  r.  r,  r/  r}  r   r   vaultWithdrawr,  r\  rV  r   F)r  r  rn  r  rQ  rR  r]  r  r  rT  r:  r-  r_  r  r^  )r  rH  r*  r  rV  r  rW  r,  rX  r_  r}  withdrawalsrZ  ra  r,  s                  r  fetch_withdrawalshyperliquid.fetch_withdrawals  se    	"889SU[\1
 #(K !!&'2!&IYYvy1F&&t{{7'CD ..x8#<<VE_aop00>#1c'l+ &>_4 NN67;EW~)<<#**62 , ..weTK&&{D%GGr!  c                     U R                  5         U R                  U5      nU R                  5       nU R                  X15      $ )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:
)r  r  r`  parse_open_interests)r  r  rV  swapMarketss       r  fetch_open_interests hyperliquid.fetch_open_interests  s?     	%%g.--/((>>r!  c                 r    U R                  U5      nU R                  5         U R                  U/U5      nX1   $ )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>`
)r&  r  rl  )r  r&  rV  oiss       r  fetch_open_interesthyperliquid.fetch_open_interest  s:     V$''&9{r!  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                 L   U R                  5         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      n	U R                  XX#5      $ )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   r.  r,  r/  )r  r/  r  rn  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{{7'CD" !!(EAAr!  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
.$ )NrY  r  r,  r  
/USDC:USDCr&  r5  r  r  )r   r&  rH  r  r  r2   rG  r  )r,  rn  r-  r;  r.  r  r  r  )r  incomer/  r2   r  r,  rm  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      nU$ )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|  rd  )r  r  r  rR  )r  r  rV  r~  rW  r|  rd  rX  s           r  reserve_request_weight"hyperliquid.reserve_request_weightc  sm     !!#U
 +
 ''6	"(++DKK,HIr!  c                     / n[        S[        U5      5       H#  nX   nUS   S   US'   UR                  U5        M%     U$ )Nr   r,  r   )rT  r:  r_  )r  r[  r_  rZ  ra  s        r  r]  #hyperliquid.extract_type_from_deltax  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$ )NrV  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  r>  r  r   zF() requires a user parameter inside 'params' or the wallet address set)rC  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urlrs  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  rs  r  r  rX  requestHeadersrequestBodyr  r  rZ  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  rs  r  r  )implode_hostnamer   json)r  pathr   rs  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   rs  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}  r>  r&  )r&  r   r  rG  r   rV  r2   )r/  rC  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!  ro  )NNNN)N)T)__name__
__module____qualname____firstlineno__r   r  r#  ro  r   r/  r   r   r;  r   r\  r    rf  rb  r`  floatr  ra  dictr  r   r  r	   r   r  r   r   r  r   r  r   r  r   r  rB  r7  rA  rI  rN  rS  r[  re  rh  rq  r  objectr  r  r  r  r  r  r  r  r  rP  r  r  r  r   r   r   r  r   r  r
  r  r.  r-  r3  r   rK  rP  rX  r\  r[  r  rh  rp  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   r@  r?  r
   rH  rS  rR  rb  rg  rl  rp  rx  r}  r  r  r]  r  r  r  r  r  r  r  __static_attributes____classcell__)r  s   @r  r.   r.      sE
   P# Pd
.S _ "]C ] ]RU ]jm ]  zI ] ]* ') ;Z ;z $& $v, 6 )+ mtF| m^ )+ 7*tF| 7*r(1u (1u (1[` (1T )+ gtF| gRc4 cF cJ $& Y)( Y)v :>b .fs .f3 .fY .f` 04B  GW  G7  GD 6:" 797 79 79r/
v /
 /
b4  6 : 9=4^bkm 8]# 8]# 8]S 8]X[ 8]rvw{r| 8]t
 
4 
0 6:UW 6A3 6As 6A# 6Ap}k k k:
L

J* *X	C	C	C	C	C. 13  1C  1D  AC "14 "1H gksu 3 i y RW `c 6 @B )9D$6 )9V mqy{ 93 9i 9y 9Z] 9fi 9v 46 ^$ ^@ 37r )s )C )$ ;?r -c -C -^ CGr 2c 2C 2VZ 2h SU F55H0I F5P <> &s &P 24 iV TXfjsu S # S  S `c : >@ 4+$|"4 4+l QS $ $3 $C $L 8<$]ajl 5O 5OC 5OWZ 5On /3TXac 7H 7H3 7Hc 7Hhlmrhs 7Hr 154VZce U# US UPS Ujnotju U 37TX\eg UC Us URU Ulpqvlw U >BPTcgpr Us U# U]` Uw{  }B  xC U *.Dt\^ !A3 !Ac !A !Acghmcn !AF 26b ;.c ;.3 ;.zV Vv V Vp: :": -1tRV_a 5Ac 5A 5AC 5An5 5v 5 5n 24 0S 0 26b FPw FPT(^ FPPJt JV JX >B" 3# 3s 3j 9=R .S .# .` =? H He HCU H @B KC K KFX K FH %3 %L^ %N
d 
F 
N` 
& [] a1S a1% a1c a1c a1an a1F
t 
x 
= 
  KOWY <0S <0% <0# <0C <0^i <0|1
T 1
X 1
Q\ 1
f 57 94 94;N 94v,
T ,
6 ,
EX ,
\ (,$TZ\ &? &?C &?s &?aefqar &?P't 'x '; 'R8 *.Dt\^ 7E3 7Ec 7E 7Er -1tRV_a 5Hc 5H 5HC 5Hfjkvfw 5Hn 7;2 
?G 
? 79 
# 
F B 37TX\eg +BC +Bs +BRU +BZ"
6 "
H :< S  * ,. 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.   )3ccxt.base.exchanger   ccxt.abstract.hyperliquidr   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.   ro  r!  r  <module>r     sw    ( 1  K  K  K  K  K  K  K  K  * . ' . ) * ) 0 9 = 4 %K?%(K K?%r!  