
    iP                         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 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)IntMarketOrder	OrderBookStrTrade)Client)List)ExchangeErrorc            
       `    e Zd Z fdZdi fdededefdZd Zdddi fde	dedede
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d
efdZd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
efdZdddi fde	dedede
e         fdZd
efdZd
efdZd
efdZd Zd
efdZ xZS )hyperliquidc                     |                      t          t          |                                           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 )NTF)
wswatchBalancewatchMyTrades
watchOHLCVwatchOrderBookwatchOrderswatchTickerwatchTickerswatchTradeswatchPositionr   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   describer   )self	__class__s    Z/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/hyperliquid.pyr)   zhyperliquid.describe   s    k4 8 8 A A C C %!%""&#$ %#!&   "@  "H  	" 
  ?%F
 %F
 % % %	    Nsymbollimitreturnc                 \  K   |                                   d{V  |                     |          }|d         }d|z   }| j        d         d         d         }dd|d	         d
d}|                     ||          }|                     ||||           d{V }	|	                                S )a  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :param str symbol: unified symbol of the market to fetch the order book for
        :param int [limit]: the maximum amount of order book entries to return
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        Nr.   
orderbook:r   r   r   	subscribel2Bookbasetypecoinmethodsubscription)load_marketsmarketr#   extendwatchr/   )
r*   r.   r/   paramsr=   messageHashurlrequestmessage	orderbooks
             r,   watch_order_bookzhyperliquid.watch_order_book8   s       !!!!!!!!!V$$!"V+it$X.! v 
 
 ++gv..**S+wLLLLLLLL	   r-   c           	      \   |                      |di           }|                     |d          }|dz   }|                     |          }|d         }|                     |dg           }|                     |dg           |                     |dg           d}	|                     |d	          }
|                     |	||
d
ddd          }|| j        vr|                     |          }|| j        |<   | j        |         }|                    |           d|z   }|	                    ||           d S )Ndatar8   
/USDC:USDCr.   levelsr      )bidsaskstimerL   rM   pxszr2   )
	safe_dictsafe_stringr=   	safe_listsafe_integerparse_order_book
orderbooks
order_bookresetresolve)r*   clientrD   entryr8   marketIdr=   r.   rawDatarH   	timestampsnapshotobrE   rA   s                  r,   handle_order_bookzhyperliquid.handle_order_bookP   s>   4 w33v..,&X&&!.."55NN7Ar22NN7Ar22
 
 %%eV44	((vy&&RVX\]]$/))**B&(DOF#OF+	!!!"V+y+.....r-   sincec                   K   d}|                      d|          \  }}|                                  d{V  d}||                     |          }|d|z   z  }| j        d         d         d         }dd	|d
d}|                     ||          }	|                     |||	|           d{V }
| j        r|
                    ||          }|                     |
|||d          S )aS  
        watches information on multiple trades made by the user
        :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]{@link https://docs.ccxt.com/#/?id=order-structure
        Nr   myTrades:r   r   r   r3   	userFillsr7   userr9   T)	handlePublicAddressr<   r.   r#   r>   r?   
newUpdatesgetLimitfilter_by_symbol_since_limit)r*   r.   rb   r/   r@   userAddressrA   rB   rC   rD   tradess              r,   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Tr-   rZ   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 )
NrH   tradesLimit  fillsr   r.   Tz	myTrades:rd   )rQ   rd   rT   r$   r   rS   lenrangeparse_ws_tradeappendlistkeysrY   )r*   rZ   rD   r[   r/   rn   symbolsrH   
dataLengthirawTradeparsedr.   ry   currentMessageHashrA   s                   r,   handle_my_tradeszhyperliquid.handle_my_trades   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{+++++r-   c                   K   |                                   d{V  |                     |          }|d         }d|z   }| j        d         d         d         }dd|d	         d
d}|                     ||          }	|                     |||	|           d{V }
| j        r|
                    ||          }|                     |
||dd          S )a  
        watches information on multiple trades made in a market
        :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]{@link https://docs.ccxt.com/#/?id=trade-structure
        Nr.   trade:r   r   r   r3   rn   r5   r6   r9   r^   Tr<   r=   r#   r>   r?   rj   rk   filter_by_since_limit)r*   r.   rb   r/   r@   r=   rA   rB   rC   rD   rn   s              r,   watch_tradeszhyperliquid.watch_trades   s      !!!!!!!!!V$$!'it$X.! v 
 
 ++gv..zz#{G[IIIIIIII? 	3OOFE22E))&%TRRRr-   c                 ^   |                      |dg           }|                     |di           }|                     |d          }|dz   }|                     |          }|d         }|| j        vr5|                     | j        dd          }	t          |	          }
|
| j        |<   | j        |         }t          dt          |                    D ]B}|                     ||          }| 
                    |          }|                    |           Cd|z   }|                    ||           d S )	NrH   r   r8   rI   r.   rq   rr   r   )rS   rQ   rR   r=   rn   rT   r$   r   ru   rt   rv   rw   rY   )r*   rZ   rD   r[   firstr8   r\   r=   r.   r/   storedrn   r|   rH   traderA   s                   r,   handle_tradeszhyperliquid.handle_trades   s1   " w33ua,,v..,&X&&!$+%%%%dlM4HHE&&F"(DKV$q#e**%% 	! 	!A>>%++D''--EMM%    'v{+++++r-   r=   c                    |                      |d          }|                     |d          }|                     |d          }|                     |d          }|dz   }|                     |d           }|d         }|                     |d          }	|                     |d          }
|

|
d	k    rd
nd}
|                     |d          }|                     |||                     |          ||	d d |
d ||d |ddd|          S )NrN   rO   rP   r8   rI   r.   tidsideAsellbuyfeeUSDC)costcurrency)infor^   datetimer.   idorderr7   r   takerOrMakerpriceamountr   r   )rT   rR   safe_market
safe_tradeiso8601)r*   r   r=   r^   r   r   r8   r\   r.   r   r   r   s               r,   rv   zhyperliquid.parse_ws_trade  s8   D %%eV44	  --!!%..v..,&!!(D11!eU++v.."ckk66Due,,"Y// V44 
  
   	r-   1mc                   K   |                                   d{V  |                     |          }|d         }| j        d         d         d         }dd|d         |d	d
}d|z   dz   |z   }	|                     ||          }
|                     ||	|
|	           d{V }| j        r|                    ||          }|                     |||dd          S )aM  
        watches historical candlestick data containing the open, high, low, close price, and the volume of a market
        :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
        Nr.   r   r   r   r3   candler5   )r7   r8   intervalr9   candles:re   r   Tr   )r*   r.   	timeframerb   r/   r@   r=   rB   rC   rA   rD   ohlcvs               r,   watch_ohlcvzhyperliquid.watch_ohlcvN  s      !!!!!!!!!V$$!it$X.! v% 
 
 !9,s2V;++gv..jjk7KHHHHHHHH? 	2NN6511E))%q$GGGr-   c                    |                      |di           }|                     |d          }|dz   }|                     |d          }|| j        vr
i | j        |<   || j        |         vr;|                     | j        dd          }t          |          }|| j        |         |<   | j        |         |         }	|                     |          }
|	                    |
           d|z   dz   |z   }|                    |	|           d S )	NrH   srI   r|   
OHLCVLimitrr   r   re   )	rQ   rR   ohlcvsrT   r$   r   parse_ohlcvrw   rY   )r*   rZ   rD   rH   r5   r.   r   r/   r   r   r~   rA   s               r,   handle_ohlcvzhyperliquid.handle_ohlcvk  s   $ ~~gvr22c**$$$T3//	$+%%"$DKT[000%%dlL$GGE*511F-3DK	*F#I.!!$''V 9,s2V;uk*****r-   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 )aS  
        watches information on multiple orders made by the user
        :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]{@link https://docs.ccxt.com/#/?id=order-structure
        Nr   r   r.   re   r   r   r   r3   orderUpdatesrg   r9   T)	r<   ri   r=   r#   r>   r?   rj   rk   rl   )r*   r.   rb   r/   r@   rm   r=   rA   rB   rC   rD   orderss               r,   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Tr-   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 )	NrH   ordersLimitrr   r   r   r.   Tre   )rS   r   rT   r$   r   rt   ru   parse_orderrw   rR   rx   ry   rY   )r*   rZ   rD   rH   r/   r{   r   rA   marketSymbolsr|   rawOrderr   r.   ry   innerMessageHashs                  r,   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{+++++r-   c                     |                      |dd          }|                      |dd          }|dk    rt          | j        dz   |z             dS )Nchannel rH   error F)rR   r   r   )r*   rZ   rD   r   ret_msgs        r,   handle_error_messagez hyperliquid.handle_error_message  s[     ""7Ir::""7FB77g# 78885r-   c                    |                      ||          rd S |                     |dd          }| j        | j        | j        | j        | j        | j        d}|                     ||          }| |||           d S t          |
                                          }t          dt          |                    D ]@}||         }|                    ||                   dk    r||         }	 |	||            d S Ad S )Nr   r   )pongrn   r4   r   r   rf   r   )r   rR   handle_pongr   ra   r   r   r   
safe_valuerx   ry   ru   rt   find)
r*   rZ   rD   topicmethods
exacMethodry   r|   keyr:   s
             r,   handle_messagezhyperliquid.handle_message  s!   $$VW55 	F  )R88$(,' -.
 
 __We44
!Jvw'''FGLLNN##q#d))$$ 	 	Aq'Czz$q'""a'' vw''' (	 	r-   c                 
    ddiS )Nr:   r    )r*   rZ   s     r,   r   zhyperliquid.ping   s    f
 	
r-   c                 <    |                      |d          |_        |S )Nr   )rT   lastPong)r*   rZ   rD   s      r,   r   zhyperliquid.handle_pong  s      ++GV<<r-   )N)__name__
__module____qualname__r)   strr   r	   rF   ra   r
   r   r   ro   r   r   r   r   r   rv   rx   r   r   r   r   r   r   r   r   r   __classcell__)r+   s   @r,   r   r      s       & & & & &P @DB ! !S ! !S\ ! ! ! !0,/ ,/ ,/\ 37TX\eg U UC Us URU Ulpqvlw U U U U>3,v 3, 3, 3, 3,j <@d[] S S SS S Sbfglbm S S S S6!,F !, !, !, !,F< <F <e < < < <| 8<$]ajl H H HC HWZ Hquvzq{ H H H H: +6  +  +  +  +D 04$UYbd U U UC Us Uimnsit U U U UB*,6 *, *, *, *,X6    V    0
 
 

&        r-   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r	   r
   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   async_supportr   r   r-   r,   <module>r      s        f f f f f f f f f f E E E E E E E E E E E E E E E E 4 4 4 4 4 4       * * * * * *~ ~ ~ ~ ~$$0 ~ ~ ~ ~ ~r-   