
    ei                         d dl Zd dlmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ  G d dej        j                  ZdS )	    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyBoolIntMarketNumOrder	OrderBookOrderRequest	OrderSide	OrderTypeStrStringsTickerTickersTrade)Client)List)NotSupportedc                   z    e Zd Zdef fdZi fdee         fdZdi fdede	de
d	ed
ef
dZddi fdedededed	ed
efdZdi fdee         defdZdi fdedefdZdi fdededefdZi fdedefdZd Zi fdedefdZdi fdedefdZdi fdedefdZdddi fdedededee         fdZdi fdedefdZde fdZ!d?de"defdZ#de fd Z$ddi fdedededee         fd!Z%i fdedefd"Z&de fd#Z'd?d$e(de"defd%Z)d&ddi fded'edededee*         f
d(Z+d&i fded'edefd)Z,de fd*Z-de d+e(fd,Z.dddi fdedededee/         fd-Z0di fdedefd.Z1de fd/Z2de de3fd0Z4de d1e(fd2Z5de d1e(fd3Z6de d1e(fd4Z7de d1e(fd5Z8de d1e(fd6Z9de d1e(fd7Z:de fd8Z;de fd9Z<de fd:Z=de fd;Z>defd<Z?d=e(de(fd>Z@ xZAS )@hyperliquidreturnc           
      *   |                      t          t          |                                           i dddddddddddddd	d
ddddddddddddddd	dd	dddddddddddiidddiidi | j        ddddi iid          S )NwsTcancelOrderWscancelOrdersWscreateOrderWscreateOrdersWseditOrderWswatchBalanceFwatchMyTrades
watchOHLCVwatchOrderBookwatchOrderswatchTickerwatchTickerswatchTradeswatchTradesForSymbolswatchPositionunWatchOrderBook)unWatchTickersunWatchTradesunWatchOHLCVunWatchMyTradesunWatchOrderspubliczwss://api.hyperliquid.xyz/wsz$wss://api.hyperliquid-testnet.xyz/ws)apitesti N  )ping	keepAliveexact)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describer5   )self	__class__s    R/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/hyperliquid.pyr?   zhyperliquid.describe   s   k4 8 8 A A C Cd !$  	
 !$ t    d !$ t t  t (   !" #D#$ #'!% $#'!%-  4  "@  "H  	" 
  W1F
 1F
 1 1 1	    ordersc                   K   |                                   d{V  | j        d         d         d         }|                     ||          }|                     |          }|                     |di           }|                     |d          }|                     ||||           d{V }|                     |di           }	|                     |	di           }
|                     |
d	g           }|                     |d          S )
a  
        create a list of trade orders using WebSocket post request

        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>`
        Nr3   r   r2   request	requestIdresponsedatastatuses)	load_marketsr9   createOrdersRequestwrap_as_post_action	safe_dictsafe_stringwatch	safe_listparse_orders)r@   rD   paramsurlordersRequestwrappedrF   rG   rH   responseOjbrI   rJ   s               rB   create_orders_wszhyperliquid.create_orders_wsD   s      !!!!!!!!!it$X.00@@**=99..)R88$$Wk::	CGYGGGGGGGGnnXz2>>~~k6266>>$
B77  4000rC   Nsymboltypesideamountpricec           	        K   |                                   d{V  |                     d||||||          \  }}|                     |g|           d{V }	t          |	          }
|
dk    r|                     i           S |	d         }|S )a;  
        create a trade order using WebSocket post request

        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
        :returns dict: an `order structure <https://docs.ccxt.com/?id=order-structure>`
        Nr   )rK   parseCreateEditOrderArgsrX   len
safe_order)r@   rY   rZ   r[   r\   r]   rS   orderglobalParamsrD   ordersLengthparsedOrders               rB   create_order_wszhyperliquid.create_order_wsZ   s      * !!!!!!!!!";;D&$PTV\^cekll|,,eWlCCCCCCCC6{{1??2&&&QirC   idc           	        K   |                                   d{V  |                     |          }| j        d         d         d         }	|                     |||||||          \  }
}|                     |
g|          }|                     |          }|                     |di           }|                     |d          }|                     |	|||           d{V }|                     |di           }|                     |di           }| 	                    |d	g           }|                     |d
i           }| 
                    ||          }|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
        :returns dict: an `order structure <https://docs.ccxt.com/?id=order-structure>`
        Nr3   r   r2   rF   rG   rH   rI   rJ   r   )rK   marketr9   r_   editOrdersRequestrM   rN   rO   rP   rQ   parse_order)r@   rg   rY   rZ   r[   r\   r]   rS   ri   rT   rb   rc   postRequestrV   rF   rG   rH   responseObject
dataObjectrJ   firstre   s                         rB   edit_order_wszhyperliquid.edit_order_wsy   sf     * !!!!!!!!!V$$it$X.";;BdTZ\acijj|,,eWlCC**;77..)R88$$Wk::	CGYGGGGGGGG*bAA^^NFB??
>>*j"==xB//&&uf55rC   idsc                   K   |                                   |                                  d{V  |                     |||          }| j        d         d         d         }|                     |          }|                     |di           }|                     |d          }|                     ||||           d{V }	|                     |	di           }
|                     |
di           }|                     |d	g           }g }t          d
t          |                    D ]5}||         }|                    |                     ||d                     6|S )a  
        cancel multiple orders using WebSocket post request

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/post-requests

        :param str[] ids: list of order ids to cancel
        :param str symbol: unified symbol of the market the orders were made in
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str[] [params.clientOrderId]: list of client order ids to cancel instead of order ids
        :param str [params.vaultAddress]: the vault address for order cancellation
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        Nr3   r   r2   rF   rG   rH   rI   rJ   r   )infostatus)check_required_credentialsrK   cancelOrdersRequestr9   rM   rN   rO   rP   rQ   ranger`   appendra   )r@   rq   rY   rS   rF   rT   rV   	wsRequestrG   rH   responseObjrI   rJ   rD   irt   s                   rB   cancel_orders_wszhyperliquid.cancel_orders_ws   s      	'')))!!!!!!!!!**3??it$X.**733NN7Ir::	$$Wk::	CIyIIIIIIIInnXz2>>~~k6266>>$
B77q#h--(( 	 	Aa[FMM$// + +       rC   c                 n   K   |                      |g||           d{V }|                     |d          S )az  
        cancel a single order using WebSocket post request

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/post-requests

        :param str id: order id to cancel
        :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 to cancel instead of order id
        :param str [params.vaultAddress]: the vault address for order cancellation
        :returns dict: an `order structure <https://docs.ccxt.com/?id=order-structure>`
        Nr   )r|   rN   )r@   rg   rY   rS   rD   s        rB   cancel_order_wszhyperliquid.cancel_order_ws   sG       ,,bT66BBBBBBBB~~fa(((rC   limitc                 |  K   |                                   d{V  |                     |          }|d         }d|z   }| j        d         d         d         }dd|d	         r|d
         n|d         dd}|                     ||          }|                     ||||           d{V }	|	                                S )aV  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :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
        NrY   
orderbook:r3   r   r2   	subscribel2BookswapbaseNamerg   rZ   coinmethodsubscription)rK   ri   r9   extendrP   r   )
r@   rY   r   rS   ri   messageHashrT   rF   message	orderbooks
             rB   watch_order_bookzhyperliquid.watch_order_book   s       !!!!!!!!!V$$!"V+it$X.! .4VnNz**&, 
 
 ++gv..**S+wLLLLLLLL	   rC   c                   K   |                                   d{V  |                     |          }|d         }d|z   }d|z   }| j        d         d         d         }t          |                                           }|dd	|d
         r|d         n|d         dd}|                     ||          }	|                     |||	|           d{V S )a	  
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str symbol: unified symbol of the market to fetch the order book for
        :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
        NrY   r   unsubscribe:r3   r   r2   unsubscriber   r   r   rg   r   )rg   r   r   )rK   ri   r9   strnoncer   rP   )
r@   rY   rS   ri   subMessageHashr   rT   rg   rF   r   s
             rB   un_watch_order_bookzhyperliquid.un_watch_order_book   s       !!!!!!!!!V$$!%.$~5it$X.# .4VnNz**&, 
 
 ++gv..ZZ[';GGGGGGGGGrC   c           	      |   |                      |di           }|                     |d          }|                     |          }|                     |          }|d         }|                     |dg           }|                     |dg           |                     |dg           d}	|                     |d          }
|                     |	||
d	d
dd          }|| j        vr|                     |          }|| j        |<   | j        |         }|	                    |           d|z   }|
                    ||           d S )NrI   r   rY   levelsr      )bidsaskstimer   r   pxszr   )rN   rO   coinToMarketIdri   rQ   safe_integerparse_order_book
orderbooks
order_bookresetresolve)r@   clientr   entryr   marketIdri   rY   rawDatarI   	timestampsnapshotobr   r   s                  rB   handle_order_bookzhyperliquid.handle_order_book  sG   4 w33v..&&t,,X&&!.."55NN7Ar22NN7Ar22
 
 %%eV44	((vy&&RVX\]]$/))**B&(DOF#OF+	!!!"V+y+.....rC   c                   K   |                      |          }|d         }|                     |                     |di           d          }|r|                     |d|i          }|                     |g|           d{V }||         S )aG  

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.channel]: 'webData2' or 'allMids', default is 'webData2'
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        rY   rs   dexN)ri   rO   rN   r   watch_tickers)r@   rY   rS   ri   dexNametickerss         rB   watch_tickerzhyperliquid.watch_ticker5  s       V$$!""4>>&&"#E#EuMM 	;[[%)9::F**F8V<<<<<<<<vrC   symbolsc                   K   |                                   d{V  |                     |dd          }d}| j        d         d         d         }d}|                     |dd	|          \  }}d
|ddd}|                     |d          }|                     |d          }|D|                     |          }	|                     |                     |	di           d          }
|
|
}|1|                     |d          }d|z   }d|d         d<   ||d         d<   |                     ||| 	                    ||          |           d{V }| j
        r|                     |d|          S | j        S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str[] symbols: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.channel]: 'webData2' or 'allMids', default is 'webData2'
        :param str [params.dex]: for for hip3 tokens subscription, eg: 'xyz' or 'flx`, if symbols are provided we will infer it from the first symbol's market
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        NTr   r3   r   r2   webData2r(   channelr   *0x0000000000000000000000000000000000000000rZ   userr   r   r   rs   ztickers:allMidsr   rZ   rY   )rK   market_symbolsr9   handle_option_and_paramsrO   ri   rN   omitrP   r   
newUpdatesfilter_by_array_tickersr   )r@   r   rS   r   rT   r   rF   
defaultDexfirstSymbolri   r   r   s               rB   r   zhyperliquid.watch_tickersI  s      !!!!!!!!!%%gtT::it$X.77PY[bcc!D 
 
 %%fe44
&&w22"[[--F&&t~~ffb'I'I5QQG"$
!YYvu--F$z1K.7GN#F+-7GN#E*

3T[[&5Q5QS^________? 	L//7KKK|rC   c                 V  K   |                                   d{V  |                     |dd          }d}d}|                     |dd|          \  }}d|z   }| j        d         d	         d
         }d|ddd}|                     |||                     ||          |           d{V S )aY  
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str[] symbols: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.channel]: 'webData2' or 'allMids', default is 'webData2'
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        NTr   r   r-   r   r   r3   r   r2   r   r   r   r   )rK   r   r   r9   rP   r   )r@   r   rS   r   r   r   rT   rF   s           rB   un_watch_tickerszhyperliquid.un_watch_tickerss  s       !!!!!!!!!%%gtT::"77@PR[]dee$~5it$X.#D 
 
 ZZ[$++gv2N2NP[\\\\\\\\\rC   sincec                   K   d}|                      d|          \  }}|                                  d{V  d}||                     |          }|d|z   z  }| j        d         d         d         }dd	|d
d}|                     ||          }	|                     |||	|           d{V }
| j        r|
                    ||          }|                     |
|||d          S )a  
        watches information on multiple trades made by the user

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order 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 dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        Nr#   myTrades:r3   r   r2   r   	userFillsr   r   T)	handlePublicAddressrK   rY   r9   r   rP   r   getLimitfilter_by_symbol_since_limit)r@   rY   r   r   rS   userAddressr   rT   rF   r   tradess              rB   watch_my_tradeszhyperliquid.watch_my_trades  s!      "66OOV!!!!!!!!! [[((F3<'Kit$X.!## 
 
 ++gv..zz#{G[IIIIIIII? 	3OOFE22E00tTTTrC   c                 P  K   |                                   d{V  |t          | j        dz             d}|                     d|          \  }}d}| j        d         d         d         }dd	|d
d}|                     ||          }|                     ||||           d{V S )a  
        unWatches information on multiple trades made by the user

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str symbol: unified market symbol of the market orders were made 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 list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        NzR unWatchMyTrades does not support a symbol argument, unWatch from all markets onlyr0   zunsubscribe:myTradesr3   r   r2   r   r   r   r   )rK   r   rg   r   r9   r   rP   )r@   rY   rS   r   r   rT   rF   r   s           rB   un_watch_my_tradeszhyperliquid.un_watch_my_trades  s       !!!!!!!!!tw)}}~~~"667H&QQV,it$X.### 
 
 ++gv..ZZ[';GGGGGGGGGrC   r   c           	         |                      |d          }|dk    r#|                     |di           }|                     |di           }|t          |                                          }t	          dt          |                    D ]u}||         }|                     |          }	|                     |	d d d          }
|
d         }|                     d| 	                    ||          i|
          }|| j
        |<   vd	}|                      |d
          }||d|z   z  }|                    | j
        |           dS |                     |di           }|                     |dg           }g }t	          dt          |                    D ]}||         }|                      |d          }|                     |          }	|                     |	d d d          }
|
d         }|                     ||
          }|                    |           || j
        |<   |                     |di           }|                     |dg           }|                     |dg           }t	          dt          |                    D ]}|                     |                     ||i           |                     ||i                     }|                      |d          }|                     |          }	|                     |	d d d          }
|
d         }|                     ||
          }|| j
        |<   |                    |           |                     |d          }|                    |d	           dS )Nr   r   rI   midsr   r   rY   r]   r   r   r   TspotAssetCtxsr   spotmetauniverse	assetCtxsname)rO   rN   listkeysrw   r`   r   safe_marketparse_ws_tickersafe_numberr   r   rQ   rx   r   index_by)r@   r   r   r   rI   r   r   r{   r   r   ri   rY   tickerr   
dexMessager   
spotAssetsparsedTickersassetObjectr   r   r   r   r   s                           rB   handle_ws_tickerszhyperliquid.handle_ws_tickers  sw   z ""7I66i>>'6266D>>$33DDIIKK((q#d)),, 2 2A7D#22488H!--hdFKKF#H-F!11!1!1$!=!=3 F ,2DL(('!--dE::
)3#33Kt|[999t..&"55^^G_bAA
q#j//** 	* 	*A$Q-K##K88D**400H%%hdFCCFH%F))+v>>F  (((#)DL  ~~gvr22>>$
B77NN7K<<	q#h--(( 	) 	)A;;xB//y!R00 D ##D&11D**400H%%hdFCCFH%F))$77F#)DL   ((((--x88w	***trC   ri   c                 .    |                      ||          S N)parse_ticker)r@   	rawTickerri   s      rB   r   zhyperliquid.parse_ws_ticker?  s      F333rC   c                    |                      |di           }| j        0|                     | j        dd          }t	          |          | _        | j        }i }|                     |dg           }t          |          }|dk    rd S t          dt          |                    D ]A}	||	         }
|                     |
          }|d         }d||<   |	                    |           Bt          |                                          }t          dt          |                    D ]#}	d||	         z   }|                    ||           $d	}|                    ||           d S )
NrI   tradesLimit  fillsr   rY   Tz	myTrades:r   )rN   r   r   r:   r   rQ   r`   rw   parse_ws_traderx   r   r   r   )r@   r   r   r   r   r   r   rI   
dataLengthr{   rawTradeparsedrY   r   currentMessageHashr   s                   rB   handle_my_tradeszhyperliquid.handle_my_tradesB  s`   : w33= %%dlM4HHE2599DM~~eWb11YY
??Fq#d))$$ 	" 	"AAwH((22FH%F"GFOMM&!!!!GLLNN##q#d))$$ 	7 	7A!,tAw!6NN6#56666 v{+++++rC   c                   K   |                                   d{V  |                     |          }|d         }d|z   }| j        d         d         d         }dd|d	         r|d
         n|d         dd}|                     ||          }	|                     |||	|           d{V }
| j        r|
                    ||          }|                     |
||dd          S )aO  
        watches information on multiple trades made in a market

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str symbol: unified market symbol of the market trades were made in
        :param int [since]: the earliest time in ms to fetch trades for
        :param int [limit]: the maximum number of trade structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=trade-structure>`
        NrY   trade:r3   r   r2   r   r   r   r   rg   r   r   r   TrK   ri   r9   r   rP   r   r   filter_by_since_limit)r@   rY   r   r   rS   ri   r   rT   rF   r   r   s              rB   watch_tradeszhyperliquid.watch_tradesw  s      !!!!!!!!!V$$!'it$X.! .4VnNz**&, 
 
 ++gv..zz#{G[IIIIIIII? 	3OOFE22E))&%TRRRrC   c                 ^  K   |                                   d{V  |                     |          }|d         }d|z   }d|z   }| j        d         d         d         }dd	|d
         r|d         n|d         dd}|                     ||          }|                     ||||           d{V S )a  
        unWatches information on multiple trades made in a market

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str symbol: unified market symbol of the market trades were made in
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=trade-structure>`
        NrY   r   r   r3   r   r2   r   r   r   r   rg   r   r   rK   ri   r9   r   rP   )	r@   rY   rS   ri   r   r   rT   rF   r   s	            rB   un_watch_tradeszhyperliquid.un_watch_trades  s       !!!!!!!!!V$$!!F*$~5it$X.# .4VnNz**&, 
 
 ++gv..ZZ[';GGGGGGGGGrC   c                 ~   |                      |dg           }|                     |di           }|                     |d          }|                     |          }|                     |          }|d         }|| j        vr5|                     | j        dd          }	t          |	          }
|
| j        |<   | j        |         }t          dt          |                    D ]B}|                     ||          }|                     |          }|                    |           Cd|z   }|                    ||           d S )NrI   r   r   rY   r   r   r   )rQ   rN   rO   r   ri   r   r   r:   r   rw   r`   r   rx   r   )r@   r   r   r   ro   r   r   ri   rY   r   storedr   r{   rI   trader   s                   rB   handle_tradeszhyperliquid.handle_trades  s:   " w33ua,,v..&&t,,X&&!$+%%%%dlM4HHE&&F"(DKV$q#e**%% 	! 	!A>>%++D''--EMM%    'v{+++++rC   r   c                 P   |                      |d          }|                     |d          }|                     |d          }|                     |d          }|                     |          }|                     |d           }|d         }|                     |d          }	|                     |d          }
|

|
dk    rd	nd
}
|                     |d          }|                     |||                     |          ||	|                     |d          d |
d ||d |ddd|          S )Nr   r   r   r   rY   tidr[   AsellbuyfeeoidUSDC)costcurrency)rs   r   datetimerY   rg   rb   rZ   r[   takerOrMakerr]   r\   r  r  )r   rO   r   r   
safe_tradeiso8601)r@   r   ri   r   r]   r\   r   r   rY   rg   r[   r  s               rB   r   zhyperliquid.parse_ws_trade  sQ   D %%eV44	  --!!%..v..&&t,,!!(D11!eU++v.."ckk66Due,,"Y//%%eU33 V44 
  
   	rC   1m	timeframec                   K   |                                   d{V  |                     |          }|d         }| j        d         d         d         }dd|d         r|d	         n|d
         |dd}d|z   dz   |z   }	|                     ||          }
|                     ||	|
|	           d{V }| j        r|                    ||          }|                     |||dd          S )a  
        watches historical candlestick data containing the open, high, low, close price, and the volume of a market

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: the length of time each candle represents
        :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
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        NrY   r3   r   r2   r   candler   r   rg   rZ   r   intervalr   candles:r   r   Tr   )r@   rY   r  r   r   rS   ri   rT   rF   r   r   ohlcvs               rB   watch_ohlcvzhyperliquid.watch_ohlcv  s!      !!!!!!!!!V$$!it$X.! .4VnNz**&,% 
 
 !9,s2V;++gv..jjk7KHHHHHHHH? 	2NN6511E))%q$GGGrC   c                 l  K   |                                   d{V  |                     |          }|d         }| j        d         d         d         }dd|d         r|d	         n|d
         |dd}d|z   dz   |z   }d|z   }|                     ||          }	|                     |||	|           d{V S )a#  
        watches historical candlestick data containing the open, high, low, close price, and the volume of a market

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: the length of time each candle represents
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        NrY   r3   r   r2   r   r  r   r   rg   r  r   r  r   r   r   )
r@   rY   r  rS   ri   rT   rF   r   messagehashr   s
             rB   un_watch_ohlcvzhyperliquid.un_watch_ohlcv0  s       !!!!!!!!!V$$!it$X.# .4VnNz**&,% 
 
 $i/#5>$~5++gv..ZZ[';GGGGGGGGGrC   c                 V   |                      |di           }|                     |d          }|                     |          }|                     |          }|                     |d          }|| j        vr
i | j        |<   || j        |         vr;|                     | j        dd          }t          |          }	|	| j        |         |<   | j        |         |         }
|                     |          }|
	                    |           d|z   dz   |z   }|
                    |
|           d S )NrI   sr{   
OHLCVLimitr   r  r   )rN   rO   r   safe_symbolohlcvsr   r:   r   parse_ohlcvrx   r   )r@   r   r   rI   baser   rY   r  r   r   r  r   r   s                rB   handle_ohlcvzhyperliquid.handle_ohlcvL  s/   $ ~~gvr22c**&&t,,!!(++$$T3//	$+%%"$DKT[000%%dlL$GGE*511F-3DK	*F#I.!!$''V 9,s2V;uk*****rC   r   c                     |                      |d          }|                     |d          }|                      |d          }|                      |d          }|                    ||           d S )NrI   rg   rH   payload)rN   rO   r   )r@   r   r   rI   rg   rH   r#  s          rB   handle_ws_postzhyperliquid.handle_ws_posto  sl     ~~gv..dD))>>$
33..955w#####rC   c                   K   |                                   d{V  d}|                     d|          \  }}d}d}|%|                     |          }|d         }|dz   |z   }| j        d         d         d         }d	d
|dd}	|                     |	|          }
|                     |||
|           d{V }| j        r|                    ||          }|                     ||||d          S )a  
        watches information on multiple orders made by the user

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order 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 dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        Nr&   rb   rY   r   r3   r   r2   r   orderUpdatesr   r   T)	rK   r   ri   r9   r   rP   r   r   r   )r@   rY   r   r   rS   r   ri   r   rT   rF   r   rD   s               rB   watch_orderszhyperliquid.watch_orders  s1      !!!!!!!!!"66}fMMV[[((FH%F%+f4Kit$X.!&# 
 
 ++gv..zz#{G[IIIIIIII? 	3OOFE22E00tTTTrC   c                 P  K   |                                   d{V  |t          | j        dz             d}| j        d         d         d         }d}|                     d|          \  }}dd	|d
d}|                     ||          }|                     ||||           d{V S )a  
        unWatches information on multiple orders made by the user

        https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions

        :param str symbol: unified market symbol of the market orders were made 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 list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        NzR unWatchOrders() does not support a symbol argument, unWatch from all markets onlyzunsubscribe:orderr3   r   r2   r1   r   r&  r   r   )rK   r   rg   r9   r   r   rP   )r@   rY   rS   r   rT   r   rF   r   s           rB   un_watch_orderszhyperliquid.un_watch_orders  s       !!!!!!!!!tw)}}~~~)it$X."66OOV#&# 
 
 ++gv..ZZ[';GGGGGGGGGrC   c                    |                      |dg           }| j        0|                     | j        dd          }t	          |          | _        t          |          }|dk    rd S | j        }d}i }t          dt          |                    D ]O}	||	         }
|                     |
          }|                    |           | 	                    |d          }d||<   Pt          |                                          }t          dt          |                    D ](}	||	         }|dz   |z   }|                    ||           )|                    ||           d S )	NrI   ordersLimitr   r   rb   rY   Tr   )rQ   rD   r   r:   r   r`   rw   rk   rx   rO   r   r   r   )r@   r   r   rI   r   r   r   r   marketSymbolsr{   rawOrderrb   rY   r   innerMessageHashs                  rB   handle_orderzhyperliquid.handle_order  se   * ~~gvr22;%%dlM4HHE077DKYY
??F q#d))$$ 	) 	)AAwH$$X..EMM%   %%eX66F$(M&!!M&&(())q#d))$$ 	5 	5A!WF*S069NN6#34444v{+++++rC   c                    |                      |dd          }|dk    r;|                      |dd          }| j        dz   |z   }|                    |           dS |                     |di           }|                      |d          }||                      |d          }|                     |di           }|                     |d	i           }	|                      |	d
          }
|
>|
dk    r8| j        dz   |                     |	          z   }|                    ||           dS |                      |	d          }|dk    r8| j        dz   |                     |	          z   }|                    ||           dS 	 |                     ddddi |                     |	          |	i i 	  	         n.# t          $ r!}|                    ||           Y d }~dS d }~ww xY wdS )Nr    errorrI    Trg   rH   r#  rt   okrZ   r   F)rO   rg   rejectrN   jsonhandle_errors	Exception)r@   r   r   r   ret_msgerrorMsgrI   rg   rH   r#  rt   rZ   r2  es                 rB   handle_error_messagez hyperliquid.handle_error_message  s   < ""7Ir::g&&w;;Gw}w.HMM(###4~~gvr22gt,,:!!$--B>>$
B77..9b99!!'844&D..w}tyy'9'99HMM(B'''4007??GcMDIIg$6$66EMM%$$$4	q"b"b$))G2D2DgrSUVVVV 	 	 	MM!R   44444	 us   70F( (
G2GGr   c                     |                      |d          }|                     |          }|                     |          }d|z   }d|z   }|                     |||           || j        v r
| j        |= d S d S )Nr   r   r   )rO   r   r  clean_unsubscriptionr   r@   r   r   r   r   rY   r   r   s           rB    handle_order_book_unsubscriptionz,hyperliquid.handle_order_book_unsubscription&  s     f55&&t,,!!(++%.$~5!!&.+FFFT_$$''' %$rC   c                     |                      |d          }|                     |          }|                     |          }d|z   }d|z   }|                     |||           || j        v r
| j        |= d S d S )Nr   r   r   )rO   r   r  r>  r   r?  s           rB   handle_trades_unsubscriptionz(hyperliquid.handle_trades_unsubscription8  s    f55&&t,,!!(++!F*$~5!!&.+FFFT[  F### ! rC   c                     d}d|z   }|                      |||           t          | j                                                  }t	          dt          |                    D ]}| j        ||         = d S )Nr   r   r   )r>  r   r   r   rw   r`   )r@   r   r   r   r   r   r{   s          rB   handle_tickers_unsubscriptionz)hyperliquid.handle_tickers_unsubscriptionC  s~    "$~5!!&.+FFFt|((**++q#g,,'' 	) 	)AWQZ((	) 	)rC   c                 ~   |                      |d          }|                     |          }|                     |          }|                      |d          }|                     |          }d|z   dz   |z   }d|z   }	|                     |||	           || j        v r|| j        |         v r| j        |         |= d S d S d S )Nr   r  r  r   r   )rO   r   r  find_timeframer>  r  )
r@   r   r   r   r   rY   r  r  r   r   s
             rB   handle_ohlcv_unsubscriptionz'hyperliquid.handle_ohlcv_unsubscriptionL  s    f55&&t,,!!(++##L*==''11	#i/#5>$~5!!&.+FFFT[  DK///K'	222 ! //rC   c                 v    d}d|z   }|                      |||d           ddi}|                     |           d S )Nrb   r   TtopicrD   r>  clean_cacher@   r   r   subHash	unSubHashtopicStructures         rB   handle_order_unsubscriptionz'hyperliquid.handle_order_unsubscriptionY  sS    "W,	!!&'9dCCCX
 	(((((rC   c                 v    d}d|z   }|                      |||d           ddi}|                     |           d S )Nr   r   TrI  rJ  rL  s         rB   handle_my_trades_unsubscriptionz+hyperliquid.handle_my_trades_unsubscriptionb  sS    "W,	!!&'9dCCCZ
 	(((((rC   c                 2   |                      |di           }|                     |d          }|dk    r|                      |di           }|                     |d          }|dk    r|                     ||           d S |dk    r|                     ||           d S |dk    r|                     ||           d S |d	k    r|                     ||           d S |d
k    r|                     ||           d S |dk    r|                     ||           d S d S d S )NrI   r   r   r   rZ   r   r   r   r  r&  r   )rN   rO   r@  rB  rD  rG  rP  rR  )r@   r   r   rI   r   r   rZ   s          rB   handle_subscription_responsez(hyperliquid.handle_subscription_responsek  s]   2 ~~gvr22!!$11]"">>$CCL##L&99Dx55flKKKKK!!11&,GGGGG##226<HHHHH!!00FFFFF''00FFFFF$$44V\JJJJJ #" %$rC   c                 ,   |                      ||          rd S |                     |dd          }| j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
        d
}|                     ||          }| |||           d S t          |                                          }t          dt          |                    D ]@}||         }|                    ||                   dk    r||         }	 |	||            d S Ad S )Nr   r1  )
pongr   r   r  r&  r   r   r   postsubscriptionResponser   )r<  rO   handle_pongr   r   r!  r/  r   r   r$  rT  
safe_valuer   r   rw   r`   find)
r@   r   r   rI  methods
exacMethodr   r{   keyr   s
             rB   handle_messagezhyperliquid.handle_message  s;    $$VW55 	F  )R88$(,' -..-'$($E
 
 __We44
!Jvw'''FGLLNN##q#d))$$ 	 	Aq'Czz$q'""a'' vw''' (	 	rC   c                 
    ddiS )Nr   r5    )r@   r   s     rB   r5   zhyperliquid.ping  s    f
 	
rC   c                 b    |                      |d|                                           |_        |S )NrV  )r   millisecondslastPong)r@   r   r   s      rB   rY  zhyperliquid.handle_pong  s.     ++GVT=N=N=P=PQQrC   c                 z    |                      |                     | j        dd          d          }|| j        d<   |S )NrG   r   r   )sumr   r:   )r@   rG   s     rB   
request_idzhyperliquid.request_id  s;    HHT..t|[!LLaPP	$-[!rC   rF   c                 B    |                                  }|d|d|dddS )NrW  action)rZ   r#  )r   rg   rF   )rG   rF   )rg  )r@   rF   rG   s      rB   rM   zhyperliquid.wrap_as_post_action  sC    OO%%	" $&  

 

 
	
rC   r   )B__name__
__module____qualname__r   r?   r   r   rX   r   r   r   floatr
   rf   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   dictr   r   r  r  r!  r$  r   r'  r)  r/  r   r<  r@  rB  rD  rG  rP  rR  rT  r_  r5   rY  rg  rM   __classcell__)rA   s   @rB   r   r      s^       2# 2 2 2 2 2 2h IK 1 1T,-? 1 1 1 1, pt|~  C y 	 [` il    > ]aos|~ $ $c $3 $c $ $VY $il $ $ $ $L DHPR  $s) S    B <@ ) ) )S ) ) ) )  @DB ! !S ! !S\ ! ! ! !6 =? H H H3 H H H H8,/ ,/ ,/\ 68   F    ( 6:" ( (7 ( ( ( ( (T 9=R ] ]g ]C ] ] ] ]6 37TX\eg  U  UC  Us  URU  Ulpqvlw  U  U  U  UD 6:" H Hs H H H H H8q q q q qf4 4 46 4 4 4 43,v 3, 3, 3, 3,j <@d[] S S SS S Sbfglbm S S S S< 9; H HC Hs H H H H4!,F !, !, !, !,F< <D <& <E < < < <| ?CQUdhqs H H H H3 H^a Hx|  ~B  yC H H H H@ BFb H H3 H3 HUX H H H H8!+6 !+ !+ !+ !+F$V $d $ $ $ $  04$UYbd "U "U "UC "Us "Uimnsit "U "U "U "UH 37r H HC Hc H H H H8*,6 *, *, *, *,X96 9t 9 9 9 9v(v (T ( ( ( ($	$6 	$ 	$ 	$ 	$ 	$)F )$ ) ) ) )3& 3 3 3 3 3)& ) ) ) ) ))f )D ) ) ) ))K6 )K )K )K )KV(V ( ( ( (T
6 
 
 
 

&    E    

4 
D 
 
 
 
 
 
 
 
rC   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   async_supportr   ra  rC   rB   <module>rx     sw       f f f f f f f f f f T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  T 4 4 4 4 4 4       ) ) ) ) ) )Q
 Q
 Q
 Q
 Q
$$0 Q
 Q
 Q
 Q
 Q
rC   