
    E#i@                         S SK rS SKJrJr  S SK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\R                   R"                  5      rg)	    N)
ArrayCacheArrayCacheByTimestamp)AnyInt	OrderBookTrade)Client)List)ExchangeErrorc            
       <  ^  \ rS rSrS\4U 4S jjrSS0 4S\S\S\S\\	   4S jjr
0 4S\S\4S	 jjrS
 rSS jrS0 4S\S\S\4S jjr0 4S\S\4S jjrS\4S jrS rSSS0 4S\S\S\S\\   4S jjrS0 4S\S\4S jjrS\4S jrS\4S jrS\4S jrSrU =r$ )dydx   returnc                 r   > U R                  [        [        U ]  5       SSSSSSSS.SS0SS0S.0 0 0 S.5      $ )	NTF)wswatchBalancewatchTickerwatchTickerswatchTradeswatchOrderBook
watchOHLCVr   z+wss://indexer.v4testnet.dydx.exchange/v4/wszwss://indexer.dydx.trade/v4/ws)testapi)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describe)self	__class__s    G/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/dydx.pyr!   dydx.describe   sk    dD : < %$ %#"&" G :	 )?
  	    Nsymbolsincelimitc                 j  #    U R                  5       I Sh  vN   U R                  S   S   nU R                  U5      nSUS   -   nSSUS   S	.nU R                  XWU R	                  X5      U5      I Sh  vN n	U R
                  (       a  U	R                  X5      nU R                  XUS
S5      $  N N<7f)a  
get the list of most recent trades for a particular symbol

https://docs.dydx.xyz/indexer-client/websockets#trades

:param str symbol: unified symbol of the market to fetch trades for
:param int [since]: timestamp in ms of the earliest trade to fetch
:param int [limit]: the maximum amount of trades to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://github.com/ccxt/ccxt/wiki/Manual#public-trades>`
Nr   r   trade:r'   	subscribe	v4_tradesidtypechannelr.   	timestampT)load_marketsr   marketwatchextend
newUpdatesgetLimitfilter_by_since_limit)
r"   r'   r(   r)   paramsurlr4   messageHashrequesttradess
             r$   watch_tradesdydx.watch_trades(   s      !!!iit$V$!11",

 zz#DKK4PR]^^??OOF2E))&TRR 	" _s"   B3B/AB34B15;B31B3c                    #    U R                  5       I Sh  vN   U R                  S   S   nU R                  U5      nSUS   -   nSSUS   S	.nU R                  X5U R	                  Xb5      U5      I Sh  vN $  Nb N7f)
aJ  
unsubscribes from the trades channel

https://docs.dydx.xyz/indexer-client/websockets#trades

:param str symbol: unified symbol of the market to fetch trades for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
Nr   r   r+   r'   unsubscriber-   r.   r/   r3   r   r4   r5   r6   r"   r'   r:   r;   r4   r<   r=   s          r$   un_watch_tradesdydx.un_watch_tradesB   s      !!!iit$V$!11!",

 ZZ$++g2NP[\\\ 	" ]"   A=A9AA=4A;5A=;A=c                    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 R
                  U5      nUc6  U R                  U R                  SS5      n	[        U	5      nXR
                  U'   U R                  Xt5      n
[        S[        U
5      5       H  nX   nUR                  U5        M     SU-   nUR                  X5        g )	Nr.   r'   contentsr>   tradesLimit  r   r+   )safe_stringsafe_market	safe_dict	safe_list
safe_valuer>   safe_integerr   r   parse_tradesrangelenappendresolve)r"   clientmessagemarketIdr4   r'   content	rawTradesstoredr)   parsedTradesiparsedr<   s                 r$   handle_tradesdydx.handle_tradesW   s    . ##GT2!!(+!..*5NN7Hb9	f5>%%dllM4HE&F"(KK((;q#l+,A!_FMM&! - $f,v+r&   c                 b   U R                  U R                  US5      5      nU R                  U R                  US5      UUU R                  U5      U R                  US5      S U R	                  US5      U R	                  US5      S U R                  US5      U R                  US5      S S S.U5      $ )	N	createdAtr.   r'   r0   sidepricesize)r.   infor2   datetimer'   orderr0   rd   takerOrMakerre   amountcostfee)	parse8601rL   
safe_tradeiso8601safe_string_lower)r"   trader4   r2   s       r$   parse_ws_tradedydx.parse_ws_trade   s     NN4#3#3E;#GH	""5$/"Y/&&vx8**5&9**5&9 %%eW5&&uf5 
  	r&   c                   #    U R                  5       I Sh  vN   U R                  S   S   nU R                  U5      nSUS   -   nSSUS   S	.nU R                  XFU R	                  Xs5      U5      I Sh  vN nUR                  5       $  Nr N7f)
a  
watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://docs.dydx.xyz/indexer-client/websockets#orders

: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   r   
orderbook:r'   r,   v4_orderbookr.   r/   )r3   r   r4   r5   r6   r)   )	r"   r'   r)   r:   r;   r4   r<   r=   	orderbooks	            r$   watch_order_bookdydx.watch_order_book   s      !!!iit$V$"VH%55%,

 **St{{77SU`aa	   	" bs"   BB	AB4B5BBc                    #    U R                  5       I Sh  vN   U R                  S   S   nU R                  U5      nSUS   -   nSSUS   S	.nU R                  X5U R	                  Xb5      U5      I Sh  vN $  Nb N7f)
a  
unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://docs.dydx.xyz/indexer-client/websockets#orders

:param str symbol: unified array of symbols
: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   r   rv   r'   rB   rw   r.   r/   rC   rD   s          r$   un_watch_order_bookdydx.un_watch_order_book   s      !!!iit$V$"VH%55!%,

 ZZ$++g2NP[\\\ 	" ]rG   rW   c                    U R                  US5      nU R                  U5      nUS   nU R                  US5      nU R                  U R                  U5      nUc  U R                  5       nXWS'   U R                  US/ 5      nU R                  US/ 5      n	U R                  US   U5        U R                  US   U	5        U R                  US5      US'   SU-   n
XpR                  U'   UR                  Xz5        g )	Nr.   r'   rI   asksbids
message_idnoncerv   )
rL   rM   rN   rP   
orderbooks
order_bookrO   handle_deltasrQ   rV   )r"   rW   rX   rY   r4   r'   rZ   rx   r   r   r<   s              r$   handle_order_bookdydx.handle_order_book   s    0 ##GT2!!(+!..*5OODOOV<	)I$(~~gvr2~~gvr29V,d39V,d3!..wE	'"V+"+y.r&   c                     [        U[        5      (       a6  U R                  US5      nU R                  US5      nUR                  X45        g U R	                  USS5      nUR                  U5        g )Nr      re   rf   )
isinstancelist
safe_floatstoreparse_bid_ask
storeArray)r"   booksidedeltare   rk   bidAsks         r$   handle_deltadydx.handle_delta   s]    eT""OOE1-E__UA.FNN5)''w?F'r&   1mc                   #    U R                  5       I Sh  vN   U R                  S   S   nU R                  U5      nSUS   -   nU R                  U R                  X"5      n	SSUS   S	-   U	-   S
.n
U R                  XhU R                  X5      U5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XUSS5      $  N N<7f)aK  
watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

https://docs.dydx.xyz/indexer-client/websockets#candles

: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   ohlcv:r'   r,   
v4_candlesr.   /r/   r   T)
r3   r   r4   rL   
timeframesr5   r6   r7   r8   r9   )r"   r'   	timeframer(   r)   r:   r;   r4   r<   
resolutionr=   ohlcvs               r$   watch_ohlcvdydx.watch_ohlcv   s      !!!iit$V$!11%%dooyL
#,$z1

 jj4;;w3OQ\]]??NN61E))%q$GG 	" ^s"   CCA?CC;CCc                 B  #    U R                  5       I Sh  vN   U R                  S   S   nU R                  U5      nSUS   -   nU R                  U R                  X"5      nSSUS   S	-   U-   S
.nU R                  XFU R                  X5      U5      I Sh  vN $  N N7f)aL  
unWatches historical candlestick data containing the open, high, low, and close price, and the volume of a market

https://docs.dydx.xyz/indexer-client/websockets#candles

: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
:param dict [params.timezone]: if provided, kline intervals are interpreted in that timezone instead of UTC, example '+08:00'
:returns int[][]: A list of candles ordered, open, high, low, close, volume
Nr   r   r   r'   rB   r   r.   r   r/   )r3   r   r4   rL   r   r5   r6   )	r"   r'   r   r:   r;   r4   r<   r   r=   s	            r$   un_watch_ohlcvdydx.un_watch_ohlcv  s      !!!iit$V$!11%%dooyL
!#,$z1

 ZZ$++g2NP[\\\ 	" ]s"   BBA?BBBBc                    U R                  US5      nUR                  S5      nU R                  US5      nU R                  U5      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SU	-   nU R	                  USU
5      nU R                  X5      nU R                  U R                  U	0 5      U R                  U	'   U R                  U R                  U	   U5      nUc9  U R                  U R                  S	S
5      n[        U5      nXR                  U	   U'   UR                  U5        UR                  X5        g )Nr.   r   r   r   r'   rI   candlesr   
OHLCVLimitrK   )rL   splitfind_timeframerM   rN   rO   parse_ohlcvrP   ohlcvsrQ   r   r   rU   rV   )r"   rW   rX   r.   partintervalr   rY   r4   r'   rZ   r   r<   r   r_   r\   r)   s                    r$   handle_ohlcvdydx.handle_ohlcv.  sF   h gt,xx}##D!,''1	##D!,!!(+!..*5..)4'w73!!%0"oodkk62FFV!4i@>%%dllL$GE*51F-3KK	*fv+r&   c                      U R                  US5      n[        U R                  S-   U-   5      e! [         a  nUR	                  U5         S nAgS nAff = f)NrX    T)rL   r   r.   	Exceptionreject)r"   rW   rX   msges        r$   handle_error_messagedydx.handle_error_messagew  sS    	""7I6C# 344 	MM!	s   -/ 
AAAc                    U R                  US5      nUS:X  a  U R                  X5        g UbT  U R                  US5      nU R                  U R                  U R                  S.nU R                  XT5      nUb	  U" X5        g g g )Nr0   errorr1   )r-   rw   r   )rL   r   r`   r   r   rP   )r"   rW   rX   r0   topicmethodsmethods          r$   handle_messagedydx.handle_message  s    07?%%f6$$Wi8E!// $ 6 6"//G
 __W4F!v' " r&    )N)__name__
__module____qualname____firstlineno__r   r!   strr   r
   r   r?   rE   r`   rs   r   ry   r|   r	   r   r   r   r   r   r   r   r   __static_attributes____classcell__)r#   s   @r$   r   r      sB   # 0 <@d[] S SS S Sbfglbm S4 9; ]C ]s ]*&,P: @DB !S ! !S\ !. =? ] ]3 ]*'/ '/R( 8<$]ajl H HC HWZ Hquvzq{ H8 ;?r ]3 ]c ]0G,6 G,R6  (V ( (r&   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   !ccxt.async_support.base.ws.clientr	   typingr
   ccxt.base.errorsr   async_supportr   r   r&   r$   <module>r      s6     N 6 6 4  *G(4"" G(r&   