
    ":h                         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 d dlmZ d dlmZ d dlmZ d dlmZ  G d d	ej2                  j4                        Zy)
    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyBalancesIntOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)ArgumentsRequiredc            
           e Zd Zdef fdZi fdZi 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di fde	e	e      ded	efdZi fde	e	e      defdZdefdZi fdedef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efdZdi fdedefdZdefdZd-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di fde	e   ded	ede	e   fdZi fde	e   defdZ 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i fde	e   d	ede"fd!Z%i fde	e   defd"Z&defd#Z'i fd$Z(i fd%Z)i fd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)Z0dddi fdeded	ede	e1   fd*Z2d+ Z3defd,Z4 xZ5S ).defxreturnc                     | j                  t        t        |          ddddddddddddddddddiddd	did
dddddddddddddddidi i d      S )NTF)wswatchBalancewatchTickerwatchTickerswatchBidsAskswatchTradeswatchTradesForSymbolswatchMyTradeswatchOrderswatchOrderBookwatchOrderBookForSymbols
watchOHLCVwatchOHLCVForSymbolsr   z'wss://stream.testnet.defx.com/pricefeedzwss://ws.testnet.defx.com/user)publicprivatez'wss://marketfeed.api.defx.com/pricefeedz wss://userfeed.api.defx.com/user)testapi 6 
timeframes1m3m5m15m30m1h2h4h12h1d1w1M)r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   )listenKeyRefreshRater   )hasurlsoptions	streaming
exceptions)deep_extendsuperr   describe)self	__class__s    G/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/defx.pyr@   zdefx.describe   s    dD : < $# $!%#)-!&#"&,0"(," "K#C "K#E )0 """$$"""$"""#&g5?
 5 5	    c                    K   | j                          d {    | j                  d   d   d   }d|d}| j                  ||      }| j                  ||||       d {   S 7 L7 w)Nr)   r   r&   	SUBSCRIBEmethodtopicsload_marketsr:   extendwatch_multiplerA   rI   messageHashesparamsurlrequestmessages          rC   watch_publiczdefx.watch_publicI   su     !!!iit$X.!
 ++gv.((mWmTTT 	" U"   A'A#AA'A%A'%A'c                    K   | j                          d {    | j                  d   d   d   }d|d}| j                  ||      }| j                  ||||       d {   S 7 L7 w)Nr)   r   r&   UNSUBSCRIBErG   rJ   rN   s          rC   un_watch_publiczdefx.un_watch_publicS   su     !!!iit$X.#
 ++gv.((mWmTTT 	" UrU   r,   Nsymbolsincelimitc                 \   K   | j                  ||gg|||       d{   }||   |   S 7 w)a  
        watches historical candlestick data containing the open, high, low, close price, and the volume of a market

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :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
        N)watch_ohlcv_for_symbols)rA   rY   	timeframerZ   r[   rP   results          rC   watch_ohlcvzdefx.watch_ohlcv]   s@      33fi5H4I5RWY_``f~i(( as   ,*,c                 H   K   | j                  ||gg|       d{   S 7 w)a"  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :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
        N)un_watch_ohlcv_for_symbols)rA   rY   r^   rP   s       rC   un_watch_ohlcvzdefx.un_watch_ohlcvm   s)      44vy6I5JFSSSS   " "symbolsAndTimeframesc                   K   t        |      }|dk(  st        |d   t              st        | j                  dz         | j                          d{    g }g }t        dt        |            D ]  }||   }	| j                  |	d      }
| j                  |
      }| j                  |	d      }| j                  | j                  ||      }|j                  d|d   z   dz   |z          |j                  d|z   d	z   |d
   z           | j                  |||       d{   \  }}}| j                  r|j                  ||      }| j                  |||dd      }| j                  |||      S 7 7 Qw)a  
        watches historical candlestick data containing the open, high, low, close price, and the volume of a market

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str[][] symbolsAndTimeframes: array of arrays containing unified symbols and timeframes to fetch OHLCV data for, example [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]
        :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
        r   zu watchOHLCVForSymbols() requires a an array of symbols and timeframes, like  [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]N   symbol:id:ohlc:candles::rY   T)len
isinstancelistr   ri   rK   rangesafe_stringmarketr+   appendrT   
newUpdatesgetLimitfilter_by_since_limitcreate_ohlcv_object)rA   re   rZ   r[   rP   symbolsLengthrI   rO   isymbolAndTimeframemarketIdrr   tfintervalrY   r^   candlesfiltereds                     rC   r]   zdefx.watch_ohlcv_for_symbolsz   s     01AZ0DQ0G%N#DGG  /f  %f  g  g!!!q#234 	QA!5a!8''(:A>H[[*F!!"4a8B''R@HMM)fTl2X=HI  h!6!<vh?O!OP	Q ,0+<+<V]TZ+[%["	7??$$VU3E--gueQM''	8DD 	" &\s&   AE/E*C	E/E-AE/-E/c                 L  K   t        |      }|dk(  st        |d   t              st        | j                  dz         | j                          d{    g }g }t        dt        |            D ]  }||   }| j                  |d      }| j                  |      }	| j                  |d      }
| j                  | j                  |
|
      }|j                  d|	d   z   dz   |z          |j                  d|z   d	z   |	d
   z           | j                  |||       d{   S 7 7 w)a=  
        unWatches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str[][] symbolsAndTimeframes: array of arrays containing unified symbols and timeframes to fetch OHLCV data for, example [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        r   zw unWatchOHLCVForSymbols() requires a an array of symbols and timeframes, like  [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]Nrg   rh   ri   rj   rk   rl   rY   )rm   rn   ro   r   ri   rK   rp   rq   rr   r+   rs   rX   )rA   re   rP   rx   rI   rO   ry   rz   r{   rr   r|   r}   s               rC   rb   zdefx.un_watch_ohlcv_for_symbols   sC     01AZ0DQ0G%N#DGG  /h  %h  i  i!!!q#234 	QA!5a!8''(:A>H[[*F!!"4a8B''R@HMM)fTl2X=HI  h!6!<vh?O!OP	Q ))&-HHH 	" Is%   AD$D C	D$D"D$"D$clientc                    | j                  |di       }| j                  |d      }| j                  |      }|d   }| j                  |d      }|| j                  vri | j                  |<   || j                  |   vr:| j	                  | j
                  dd      }t        |      }	|	| j                  |   |<   | j                  |   |   }
| j                  |      }|
j                  |       d|z   dz   |z   }|j                  |||
g|       y )NdatarY   window
OHLCVLimit  rk   rl   )
	safe_dictrq   rr   ohlcvssafe_integerr;   r   parse_ohlcvrs   resolve)rA   r   rS   r   r{   rr   rY   r^   r[   storedohlcvparsedmessageHashs                rC   handle_ohlcvzdefx.handle_ohlcv   s
   0 ~~gvr2##D(3X&!$$T84	$++%"$DKKT[[00%%dllL$GE*51F-3DKK	*F#I.!!$'V 9,s2V;	51;?rD   c                    K   | j                          d{    | j                  |      }|d   }d|d   z   dz   }d|z   }| j                  |g|g|       d{   S 7 G7 w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        NrY   rh   ri   :24hrTickerticker:)rK   rr   rT   )rA   rY   rP   rr   topicr   s         rC   watch_tickerzdefx.watch_ticker   sw      !!!V$!F4L(=8&(&&wvFFF 	"
 Gs"   A"AAA"A A" A"c                 D   K   | j                  |g|       d{   S 7 w)a  
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :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]: the channel to subscribe to, tickers by default. Can be tickers, sprd-tickers, index-tickers, block-tickers
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        N)un_watch_tickersrA   rY   rP   s      rC   un_watch_tickerzdefx.un_watch_ticker   s#      **F8V<<<<     symbolsc                   K   | j                          d{    | j                  |dd      }g }g }t        dt        |            D ]C  }||   }| j	                  |      }|j                  d|z   dz          |j                  d|z          E | j                  |||       d{    | j                  | j                  d|      S 7 7 #w)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://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str[] [symbols]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        NFr   rh   r   r   rY   )	rK   market_symbolsrp   rm   	market_idrs   rT   filter_by_arraytickersrA   r   rP   rI   rO   ry   rY   r{   s           rC   watch_tickerszdefx.watch_tickers   s      !!!%%gtU;q#g,' 	5AQZF~~f-HMM)h.>?  V!34		5
 v>>>##DLL(GDD 	" 	?"   C	CBC	#C$"C	C	c                 \  K   | j                          d{    | j                  |dd      }g }g }t        dt        |            D ]C  }||   }| j	                  |      }|j                  d|z   dz          |j                  d|z          E | j                  |||       d{   S 7 7 w)a  
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str[] [symbols]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        NFr   rh   r   r   )rK   r   rp   rm   r   rs   rX   r   s           rC   r   zdefx.un_watch_tickers  s      !!!%%gtU;q#g,' 	5AQZF~~f-HMM)h.>?  V!34		5
 ))&-HHH 	" Is"   B,B(BB,#B*$B,*B,c                    | j                  ||       | j                  |di       }| j                  |      }|d   }| j                  |d      }||d<   | j	                  |      |d<   || j
                  |<   d|z   }|j                  ||       y )Nr   rY   	timestampdatetimer   )handle_bid_askr   parse_tickerr   iso8601r   r   rA   r   rS   r   parsedTickerrY   r   r   s           rC   handle_tickerzdefx.handle_ticker&  s    : 	FG,~~gvr2((.h'%%g{;	$-[!#'<<	#:Z +V&(|[1rD   c                   K   | j                          d{    | j                  |dd      }g }g }t        dt        |            D ]C  }||   }| j	                  |      }|j                  d|z   dz          |j                  d|z          E | j                  |||       d{    | j                  | j                  d|      S 7 7 #w)a  
        watches best bid & ask for symbols

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str[] symbols: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        NFr   rh   r   bidask:rY   )	rK   r   rp   rm   r   rs   rT   r   bidsasksr   s           rC   watch_bids_askszdefx.watch_bids_asksN  s      !!!%%gtU;q#g,' 	5AQZF~~f-HMM)h.>?  V!34		5
 v>>>##DMM8WEE 	" 	?r   c                     | j                  |di       }| j                  |      }|d   }| j                  |d      }||d<   | j                  |      |d<   || j                  |<   d|z   }|j                  ||       y )Nr   rY   r   r   r   )r   parse_ws_bid_askr   r   r   r   r   s           rC   r   zdefx.handle_bid_askd  s    ~~gvr2,,T2h'%%g{;	$-[!#'<<	#:Z  ,f&(|[1rD   c                 $   | j                  |d      }| j                  ||      }| j                  |d      }| j                  |d d | j                  |d      | j                  |d      | j                  |d      | j                  |d      |d|      S )NrY   bestAskPrice
bestAskQtybestBidPrice
bestBidQty)rY   r   r   ask	askVolumebid	bidVolumeinfo)rq   safe_marketsafe_ticker)rA   tickerrr   r{   rY   s        rC   r   zdefx.parse_ws_bid_asko  s    ##FH5!!(F3!!&(3##FN;))&,?##FN;))&,?	!
 	 		rD   c                 H   K   | j                  |g|||       d{   S 7 w)a@  
        watches information on multiple trades made in a market

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str symbol: unified symbol of the market to fetch the ticker for
        :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 `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        N)watch_trades_for_symbols)rA   rY   rZ   r[   rP   s        rC   watch_tradeszdefx.watch_trades~  s'      22F8UE6RRRRrd   c                 D   K   | j                  |g|       d{   S 7 w)a  
        unWatches from the stream channel

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :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>`
        N)un_watch_trades_for_symbolsr   s      rC   un_watch_tradeszdefx.un_watch_trades  s#      55vhGGGGr   c                 X  K   | j                          d{    | j                  |      }t        |      }|dk(  rt        | j                  dz         g }g }t        dt        |            D ]C  }||   }	| j                  |	      }
|j                  d|
z   dz          |j                  d|	z          E | j                  |||       d{   }| j                  r6| j                  |d      }| j                  |d      }|j                  ||      }| j                  |||dd	      S 7 7 ^w)
a?  
        watches information on multiple trades made in a market

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str[] symbols: unified symbol of the market to fetch trades for
        :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 `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr   z> watchTradesForSymbols() requires a non-empty array of symbolsrh   :tradestrade:rY   r   T)rK   r   rm   r   ri   rp   r   rs   rT   rt   
safe_valuerq   ru   rv   )rA   r   rZ   r[   rP   rx   rI   rO   ry   rY   r{   tradesfirsttradeSymbols                 rC   r   zdefx.watch_trades_for_symbols  s+     !!!%%g.GA#DGG.n$nooq#g,' 	4AQZF~~f-HMM)h.:;  F!23		4
 ((GG??OOFA.E**5(;KOOK7E))&%TRR# 	" Hs#   D*D%B2D*	D(
AD*(D*c                   K   | j                          d{    | j                  |      }t        |      }|dk(  rt        | j                  dz         g }g }t        dt        |            D ]C  }||   }| j                  |      }|j                  d|z   dz          |j                  d|z          E | j                  |||       d{   S 7 7 w)a  
        unWatches from the stream channel

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str[] symbols: 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   z@ unWatchTradesForSymbols() requires a non-empty array of symbolsrh   r   r   )	rK   r   rm   r   ri   rp   r   rs   rX   	rA   r   rP   rx   rI   rO   ry   rY   r{   s	            rC   r   z defx.un_watch_trades_for_symbols  s      !!!%%g.GA#DGG.p$pqqq#g,' 	4AQZF~~f-HMM)h.:;  F!23		4
 ))&-HHH 	" I"   CCB2C	C
CCc                 N   | j                  |di       }| j                  |      }|d   }|| j                  vr7| j                  | j                  dd      }t        |      }|| j                  |<   | j                  |   }|j                  |       d|z   }	|j                  ||	       y )Nr   rY   tradesLimitr   r   )r   parse_trader   r   r;   r   rs   r   )
rA   r   rS   r   parsedTraderY   r[   r   r   r   s
             rC   handle_tradeszdefx.handle_trades  s     ~~gvr2&&t,X&$++%%%dllM4HE&F"(DKKV$k"'v{+rD   c                 F   K   | j                  |g||       d{   S 7 w)aS  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :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
        N)watch_order_book_for_symbols)rA   rY   r[   rP   s       rC   watch_order_bookzdefx.watch_order_book  s%      66xOOOOs   !!c                 D   K   | j                  |g|       d{   S 7 w)a  
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :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
        N)un_watch_order_book_for_symbolsr   s      rC   un_watch_order_bookzdefx.un_watch_order_book  s#      996(FKKKKr   c                   K   | j                          d{    t        |      }|dk(  rt        | j                  dz         | j	                  |      }g }g }t        dt        |            D ]C  }||   }| j                  |      }	|j                  d|	z   dz          |j                  d|z          E | j                  |||       d{   }
|
j                         S 7 7 w)a6  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str[] symbols: unified array of symbols
        :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   zA watchOrderBookForSymbols() requires a non-empty array of symbolsrh   :depth:20:0.001
orderbook:)
rK   rm   r   ri   r   rp   r   rs   rT   r[   )rA   r   r[   rP   rx   rI   rO   ry   rY   r{   	orderbooks              rC   r   z!defx.watch_order_book_for_symbols  s      !!!GA#DGG.q$qrr%%g.q#g,' 	8AQZF~~f-HMM)h.1BBC  !67		8
 ++FM6JJ	   	" Ks"   C"CB2C"	C 
C" C"c                   K   | j                          d{    t        |      }|dk(  rt        | j                  dz         | j	                  |      }g }g }t        dt        |            D ]C  }||   }| j                  |      }|j                  d|z   dz          |j                  d|z          E | j                  |||       d{   S 7 7 w)a  
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://www.postman.com/defxcode/defx-public-apis/collection/667939a1b5d8069c13d614e9

        :param str[] symbols: 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   zC unWatchOrderBookForSymbols() requires a non-empty array of symbolsrh   r   r   )	rK   rm   r   ri   r   rp   r   rs   rX   r   s	            rC   r   z$defx.un_watch_order_book_for_symbols  s      !!!GA#DGG.s$stt%%g.q#g,' 	8AQZF~~f-HMM)h.1BBC  !67		8
 ))&-HHH 	" Ir   c           	         | j                  |di       }| j                  |d      }| j                  |      }|d   }| j                  |d      }| j	                  |||dddd      }|| j
                  vr | j                  |      }	|	| j
                  |<   | j
                  |   }
|
j                  |       d|z   }|j                  |
|       y )	Nr   rY   r   bidsaskspriceqtyr   )	r   rq   rr   r   parse_order_book
orderbooks
order_bookresetr   )rA   r   rS   r   r{   rr   rY   r   snapshotobr   r   s               rC   handle_order_bookzdefx.handle_order_book5  s    6 ~~gvr2##D(3X&!%%dK8	((vy&&RY[`a$//)*B&(DOOF#OOF+	!"V+y+.rD   c                 b  K   | j                  | j                  d      }|y 	 | j                  d|i       d {    | j                  | j                  dd	      }	| j                  |	| j                  |       y 7 ?# t        $ r}| j                  d   d   d   dz   |z   }| j                  |      }t        |j                  j                               }t        dt        |            D ]  }||   }|j                  ||        d | j                  d<   d| j                  d<   Y d }~y d }~ww xY ww)
N	listenKeyr)   r   r'   ?listenKey=r   lastAuthenticatedTimer8   r*   )rq   r;   -v1PrivatePutApiUsersSocketListenKeysListenKey	Exceptionr:   r   ro   futureskeysrp   rm   rejectr   delaykeep_alive_listen_key)
rA   rP   r   errorrQ   r   rO   jr   r8   s
             rC   r   zdefx.keep_alive_listen_key^  s)    $$T\\;?		DDkS\E]^^^  $00?UW^_

')C)CVL _ 		))E"4(3mCiOC[[%F !4!4!67M1c-01 2+A.e[12 )-DLL%45DLL01		s>    D/A; A9A; ;D/9A; ;	D,BD'"D/'D,,D/c                 |  K   | j                         }| j                  | j                  dd      }| j                  | j                  dd      }||z
  |kD  rd| j                          d {   }| j	                  |d      | j                  d<   || j                  d<   | j                  || j                  |       y y 7 Qw)Nr   r   r8   r*   r   )millisecondsr   r;   %v1PrivatePostApiUsersSocketListenKeysrq   r   r   )rA   rP   timer   r8   responses         rC   authenticatezdefx.authenticates  s       " $ 1 1$,,@WYZ [#00?UW^_''*>>!GGIIH(,(8(8;(ODLL%48DLL01JJ+T-G-GP	 ?Is   A&B<(B:)AB<c                   K   | j                          d{    | j                          d{    | j                  d   d   d   }d}|dz   | j                  d   z   }| j	                  ||d|       d{   S 7 d7 N7 w)a  
        query for balance and get the amount of funds available for trading or funds locked in orders

        https://www.postman.com/defxcode/defx-public-apis/ws-raw-request/667939b2f00f79161bb47809

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr)   r   r'   WALLET_BALANCE_UPDATEr   r   )rK   r   r:   r;   watch)rA   rP   baseUrlr   rQ   s        rC   watch_balancezdefx.watch_balance}  s      !!!!!!))E"4(3-%[(AAZZ[$DDD 	"! Es2   BA;BA=AB6A?7B=B?Bc                 p   | j                  |d      }| j                  |dg       }| j                  |d      }| j                  i | _        || j                  d<   || j                  d<   | j	                  |      | j                  d<   | j                  |d      }| j                  |      }|| j                  v r| j                  |   n| j                         }| j                  |d      |d<   || j                  |<   | j                  | j                        | _        |j                  | j                  |       y )	Neventr   r   r   r   assetbalancefree)	rq   r   r   r  r   safe_currency_codeaccountsafe_balancer   )	rA   r   rS   r   r   r   
currencyIdcoder  s	            rC   handle_balancezdefx.handle_balance  s    &&w8~~gvr2%%g{;	<<DL#V$-[!#'<<	#:Z %%dG4
&&z2)-)=$,,t$DLLN**4;$T((6t||[1rD   c                   K   | j                          d{    | j                          d{    | j                  d   d   d   }d}|| j                  |      }|d|d   z   z  }|dz   | j                  d	   z   }| j                  ||d|       d{   }	| j                  r|	j                  ||      }| j                  |	|||d
      S 7 7 7 ;w)aV  
        watches information on multiple orders made by the user

        https://www.postman.com/defxcode/defx-public-apis/ws-raw-request/667939b2f00f79161bb47809

        :param str [symbol]: unified market symbol of the market the 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
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr)   r   r'   ordersrl   rY   r   r   T)	rK   r   r:   rr   r;   r  rt   ru   filter_by_symbol_since_limit)
rA   rY   rZ   r[   rP   r  r   rr   rQ   r  s
             rC   watch_orderszdefx.watch_orders  s      !!!!!!))E"4(3[[(F3!111K%[(AAzz#{D+FF??OOFE2E00tTT 	"! Gs2   CCCCA%CC8CCCc                 Z   d}| j                  |di       }| j                  -| j                  | j                  dd      }t	        |      | _        | j                  }| j                  |      }|j                  |       |dz   |d   z   }|j                  ||       |j                  ||       y )Nr  r   ordersLimitr   rl   rY   )r   r  r   r;   r   parse_orderrs   r   )	rA   r   rS   channelr   r[   r  parsedOrderr   s	            rC   handle_orderzdefx.handle_order  s    B ~~gvr2;;%%dllM4HE07DK&&t,k"mk(&;;vw'v{+rD   c                 8  K   | j                          d{    | j                          d{    | j                  |      }| j                  d   d   d   }d}|dz   | j                  d   z   }d}|Ug }	t        dt        |            D ]  }
||
   }|	j                  |d	z   |z            | j                  ||	d|	       d{   }n| j                  ||d|       d{   }| j                  r|S | j                  | j                  |||d
      S 7 7 7 R7 7w)a  
        watch all open positions

        https://www.postman.com/defxcode/defx-public-apis/ws-raw-request/667939b2f00f79161bb47809

        :param str[]|None symbols: list of unified market symbols
        :param number [since]: since timestamp
        :param number [limit]: limit
        :param dict params: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
        Nr)   r   r'   	positionsr   r   r   rl   T)rK   r   r   r:   r;   rp   rm   rs   rM   r  rt   filter_by_symbols_since_limitr  )rA   r   rZ   r[   rP   r  r  rQ   newPositionrO   ry   rY   s               rC   watch_positionszdefx.watch_positions  s-     !!!!!!%%g.))E"4(3%[(AAM1c'l+ = $$Ws]V%;<= !% 3 3Cm \\K $

3w GGK??11$..'5RWY]^^# 	"! ]GsD   DDDDBDDD D!2DDDDc                 x   d}| j                  |di       }| j                  t               | _        | j                  }| j                  |      }| j	                  |d      }||d<   | j                  |      |d<   |j                  |       |dz   |d   z   }|j                  |g|       |j                  |g|       y )Nr  r   r   r   rl   rY   )r   r  r   parse_positionr   r   rs   r   )	rA   r   rS   r  r   cacheparsedPositionr   r   s	            rC   handle_positionszdefx.handle_positions  s    & ~~gvr2>>!35DN,,T2%%g{;	&/{#%)\\)%<z"^$mnX&>>'1'5rD   c                    | j                  |d      }|-| j                  |d      }t        | j                  dz   |z         | j                  |d      }|o| j                  | j                  | j
                  | j                  | j                  | j                  | j                  d}| j                  ||      }|
 |||       y y y )Nr  msg r  )ohlc
24hrTickerr   depthr  ORDER_UPDATEPOSITION_UPDATE)rq   r   ri   r   r   r   r   r  r  r#  r   )rA   r   rS   r   errorMsgr  methods
exacMethods           rC   handle_messagezdefx.handle_message-  s      &1''7H# 899  '2))"00,,//)-)<)< $ 1 1#'#8#8G %8J%67+ & rD   )N)6__name__
__module____qualname__r   r@   rT   rX   strr   r   ro   r`   rc   r]   rb   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r   r  r  r   r	   r  r  r   r  r#  r/  __classcell__)rB   s   @rC   r   r      s   6# 6p @B U CE U 8<$]ajl ) )C )WZ )quvzq{ )  ;?r T3 Tc T aesw  AC E$tCy/ EZ] Emp E@ ^` IT$s)_ Idg I4'@6 '@R 68 G GF G" 9; =C =s = 6:" E7 E E, 9=R Ig IC I*&2F &2P 8<B FW F7 F,	2V 	2 <@d[] S SS S Sbfglbm S 9; 
HC 
Hs 
H OSaenp Sd3i S S[^ Suyz  vA S> LN Ic IRU I0,F ,6 @DB PS P PS\ P =? 
L 
L3 
L SW_a !$s) !C !fo !4 PR IT#Y IVY I0'/ '/R 24 M* )+ Q *, E E 2V 24 04$UYbd U UC Us Uimnsit U2+,6 +,Z 8<$]ajl _W _C _WZ _quv~q _>6B,V ,rD   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   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   async_supportr    rD   rC   <module>r>     sB     f f p p p p 4  * .p,4"" p,rD   