
    ei@                         d dl Zd dlmZmZ d dl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ArrayCacheByTimestamp)AnyInt	OrderBookTrade)Client)List)ExchangeErrorc            
           e Zd Zdef 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	 Zdd
Zdi fdededefdZi fdedefdZ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i fdedefdZdefdZdefdZdefdZ xZS )dydxreturnc                     |                      t          t          |                                           ddddddddddiddidi i i d          S )	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    K/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/dydx.pyr    zdydx.describe   s    dD 1 1 : : < < %$ %#"&"  G :	  )?
 ?
   	    Nsymbolsincelimitc                   K   |                                   d{V  | j        d         d         }|                     |          }d|d         z   }dd|d         d	}|                     |||                     ||          |           d{V }	| j        r|	                    ||          }|                     |	||d
d          S )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urlr2   messageHashrequesttradess
             r#   watch_tradeszdydx.watch_trades(   s       !!!!!!!!!it$V$$!11",
 

 zz#{DKK4P4PR]^^^^^^^^? 	3OOFE22E))&%TRRRr$   c                   K   |                                   d{V  | j        d         d         }|                     |          }d|d         z   }dd|d         d	}|                     |||                     ||          |           d{V S )
ax  
        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-   r1   r   r2   r3   r4   r!   r%   r8   r9   r2   r:   r;   s          r#   un_watch_tradeszdydx.un_watch_tradesB   s       !!!!!!!!!it$V$$!11!",
 

 ZZ[$++gv2N2NP[\\\\\\\\\r$   c                 F   |                      |d          }|                     |          }|d         }|                     |d          }|                     |dg           }|                     | j        |          }|5|                     | j        dd          }	t          |	          }|| j        |<   | 	                    ||          }
t          dt          |
                    D ]}|
|         }|                    |            d|z   }|                    ||           d S )	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marketIdr2   r%   content	rawTradesstoredr'   parsedTradesiparsedr:   s                 r#   handle_tradeszdydx.handle_tradesW   s"   . ##GT22!!(++!..*55NN7Hb99	f55>%%dlM4HHE&&F"(DK((F;;q#l++,, 	" 	"A!!_FMM&!!!!#f,v{+++++r$   c                    |                      |                     |d                    }|                     |                     |d          |||                     |          |                     |d          d |                     |d          |                     |d          d |                     |d          |                     |d          d d d|          S )	N	createdAtr,   r%   r.   sidepricesize)r,   infor0   datetimer%   orderr.   r^   takerOrMakerr_   amountcostfee)	parse8601rG   
safe_tradeiso8601safe_string_lower)r!   trader2   r0   s       r#   parse_ws_tradezdydx.parse_ws_trade   s     NN4#3#3E;#G#GHH	""5$//"Y//&&vx88**5&99**5&99 %%eW55&&uf55 
  
   	r$   c                 D  K   |                                   d{V  | j        d         d         }|                     |          }d|d         z   }dd|d         d	}|                     |||                     ||          |           d{V }|                                S )
a2  
        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-   )r1   r   r2   r3   r4   r'   )	r!   r%   r'   r8   r9   r2   r:   r;   	orderbooks	            r#   watch_order_bookzdydx.watch_order_book   s       !!!!!!!!!it$V$$"VH%55%,
 

 **S+t{{7F7S7SU`aaaaaaaa	   r$   c                   K   |                                   d{V  | j        d         d         }|                     |          }d|d         z   }dd|d         d	}|                     |||                     ||          |           d{V S )
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   ro   r%   r?   rp   r,   r-   r@   rA   s          r#   un_watch_order_bookzdydx.un_watch_order_book   s       !!!!!!!!!it$V$$"VH%55!%,
 

 ZZ[$++gv2N2NP[\\\\\\\\\r$   rR   c                 L   |                      |d          }|                     |          }|d         }|                     |d          }|                     | j        |          }||                                 }||d<   |                     |dg           }|                     |dg           }	|                     |d         |           |                     |d         |	           |                     |d          |d<   d|z   }
|| j        |<   |	                    ||
           d S )	Nr,   r%   rD   asksbids
message_idnoncero   )
rG   rH   rI   rK   
orderbooks
order_bookrJ   handle_deltasrL   rQ   )r!   rR   rS   rT   r2   r%   rU   rq   rv   rw   r:   s              r#   handle_order_bookzdydx.handle_order_book   s'   0 ##GT22!!(++!..*55OODOV<<	))I$	(~~gvr22~~gvr229V,d3339V,d333!..wEE	'"V+"+y+.....r$   c                    t          |t                    rD|                     |d          }|                     |d          }|                    ||           d S |                     |dd          }|                    |           d S )Nr      r_   r`   )
isinstancelist
safe_floatstoreparse_bid_ask
storeArray)r!   booksidedeltar_   re   bidAsks         r#   handle_deltazdydx.handle_delta   s    eT"" 	(OOE1--E__UA..FNN5&)))))''w??F'''''r$   1mc                   K   |                                   d{V  | j        d         d         }|                     |          }d|d         z   }|                     | j        ||          }	dd|d         d	z   |	z   d
}
|                     |||                     |
|          |           d{V }| j        r|                    ||          }| 	                    |||dd          S )a  
        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)
r1   r   r2   rG   
timeframesr3   r4   r5   r6   r7   )r!   r%   	timeframer&   r'   r8   r9   r2   r:   
resolutionr;   ohlcvs               r#   watch_ohlcvzdydx.watch_ohlcv   s      !!!!!!!!!it$V$$!11%%doy)LL
#,$z1
 

 jjk4;;w3O3OQ\]]]]]]]]? 	2NN6511E))%q$GGGr$   c                 `  K   |                                   d{V  | j        d         d         }|                     |          }d|d         z   }|                     | j        ||          }dd|d         d	z   |z   d
}|                     |||                     ||          |           d{V S )a  
        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%   r?   r   r,   r   r-   )r1   r   r2   rG   r   r3   r4   )	r!   r%   r   r8   r9   r2   r:   r   r;   s	            r#   un_watch_ohlcvzdydx.un_watch_ohlcv  s       !!!!!!!!!it$V$$!11%%doy)LL
!#,$z1
 

 ZZ[$++gv2N2NP[\\\\\\\\\r$   c                 .   |                      |d          }|                    d          }|                      |d          }|                     |          }|                      |d          }|                     |          }|d         }	|                     |d          }
|                     |
d          }d|	z   }|                     |d|
          }|                     ||          }|                     | j        |	i           | j        |	<   |                     | j        |	         |          }|;| 	                    | j
        d	d
          }t          |          }|| j        |	         |<   |                    |           |                    ||           d S )Nr,   r   r   r   r%   rD   candlesr   
OHLCVLimitrF   )rG   splitfind_timeframerH   rI   rJ   parse_ohlcvrK   ohlcvsrL   r   r   rP   rQ   )r!   rR   rS   r,   partintervalr   rT   r2   r%   rU   r   r:   r   rZ   rW   r'   s                    r#   handle_ohlcvzdydx.handle_ohlcv.  s}   h gt,,xx}}##D!,,''11	##D!,,!!(++!..*55..)44'w733!!%00"oodk62FFFV!4i@@>%%dlL$GGE*511F-3DK	*fv{+++++r$   c                     	 |                      |d          }t          | j        dz   |z             # t          $ r}|                    |           Y d }~nd }~ww xY wdS )NrS    T)rG   r   r,   	Exceptionreject)r!   rR   rS   msges        r#   handle_error_messagezdydx.handle_error_messagew  sz    	""7I66C# 3444 	 	 	MM!	ts   02 
AAAc                    |                      |d          }|dk    r|                     ||           d S |Q|                      |d          }| j        | j        | j        d}|                     ||          }| |||           d S d S d S )Nr.   errorr/   )r+   rp   r   )rG   r   r[   r}   r   rK   )r!   rR   rS   r.   topicmethodsmethods          r#   handle_messagezdydx.handle_message  s    007??%%fg666F$$Wi88E!/ $ 6"/ G
 __We44F!vw'''''  "!r$   )N)__name__
__module____qualname__r   r    strr   r
   r   r=   rB   r[   rm   r   rr   rt   r	   r}   r   r   r   r   r   r   r   __classcell__)r"   s   @r#   r   r      s`       #      0 <@d[] S S SS S Sbfglbm S S S S4 9; ] ]C ]s ] ] ] ]*&, &, &,P   : @DB ! !S ! !S\ ! ! ! !. =? ] ] ]3 ] ] ] ]*'/ '/ '/ '/ '/R( ( ( 8<$]ajl H H HC HWZ Hquvzq{ H H H H8 ;?r ] ]3 ]c ] ] ] ]0G,6 G, G, G, 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#   <module>r      s        N N N N N N N N 6 6 6 6 6 6 6 6 6 6 6 6 4 4 4 4 4 4       * * * * * *G( G( G( G( G(4" G( G( G( G( G(r$   