
    eiط                         d dl Zd dlmZ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 d dl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j        j                  ZdS )    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesBoolIntMarketOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)NotSupportedc            
       4    e Zd Zdef fdZdefdZdefdZdedefdZ	ddi fd	e
d
ededee         fdZddi fdee
         d
ededee         fdZdefdZd9de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di fdeee
                  d
edefdZdefdZd9defdZi fd	e
defdZdi fdedefdZdefdZd9dZdi fd	e
dedefdZ di fdee
         dedefdZ!defdZ"d  Z#defd!Z$ded"e
fd#Z%i fde&fd$Z'd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efd*Z.d9d+Z/dddi fd	e(d
ededee         fd,Z0defd-Z1d9d.Z2dddi fded
ededee3         fd/Z4d:dedefd1Z5d2 Z6d3 Z7d9d4Z8i fd5Z9i fd6Z:d7 Z;dede<fd8Z= xZ>S );toobitreturnc                     |                      t          t          |                                           dddddddddddddddddiiidddd	d
dddddddddddddiddid| j        dddi iid          S )NT)wswatchBalancewatchMyTrades
watchOHLCVwatchOHLCVForSymbolswatchOrderBookwatchOrderBookForSymbolswatchOrderswatchTickerwatchTickerswatchTradeswatchTradesForSymbolsapir   commonzwss://stream.toobit.com1m3m5m15m30m1h2h4h6h8h12h1d1w1M)r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   channeldepthO )
timeframesr"   listenKeyRefreshRateiX )	keepAlivepingexact)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describer?   )self	__class__s    M/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/toobit.pyrH   ztoobit.describe   s   fd 3 3 < < > > $!%"(,"&,0## $#)-    "; """$$"""""$"""# #" "7' -4) 2 1	 
  g9A
 9A
 9 9 9	    clientc                 .    d|                                  iS )Nr?   )milliseconds)rI   rM   s     rK   r?   ztoobit.pingN   s    D%%''
 	
rL   c                 \   |                      |d          }|                     ||          rd S |                     |d          }||                     ||           d S | j        | j        | j        | j        | j        | j	        | j	        | j
        | j
        | j        | j        d}|                     ||          }| |||           d S t          dt          |                    D ]D}||         }|                      |d          }	|                     ||	          }
|
 |
||           Ed S )Ntopicpong)tradekline	realtimesr:   	diffDepthoutboundAccountInfooutboundContractAccountInfoexecutionReportcontractExecutionReport
ticketInfooutboundContractPositionInfor   e)safe_stringhandle_error_messagesafe_integerhandle_incoming_ponghandle_tradeshandle_ohlcvhandle_tickers"handle_order_book_partial_snapshothandle_order_bookhandle_balancehandle_orderhandle_my_tradehandle_positions
safe_valuerangelen)rI   rM   messagerQ   pongTimestampmethodsmethodiitemeventmethod2s              rK   handle_messageztoobit.handle_messageS   se   N   '22$$VW55 	F ))'6::$%%fm<<<F'&,</#'#6+/+>#0'+'8.,0,A
 
 %00F67##### 1c'll++ * *qz((s33//'599&GFD)))* *rL   ro   c                     ||_         d S N)lastPong)rI   rM   ro   s      rK   ra   ztoobit.handle_incoming_pong   s    'rL   Nsymbolsincelimitc                 D   K   |                      |g|||           d{V S )a4  
        watches information on multiple trades made in a market

        https://toobit-docs.github.io/apidocs/spot/v1/en/#trade-streams

        :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>`
        N)watch_trades_for_symbols)rI   rz   r{   r|   paramss        rK   watch_tradesztoobit.watch_trades   s6       22F8UE6RRRRRRRRRrL   symbolsc                   K   |                                   d{V  |                     |dd          }g }g }t          dt          |                    D ]T}||         }|                     |          }	|                    d|z              |	d         }
|                    |
           U|                     |          }| j        d         d         d         d	z   }d
                    |          ddd}| 	                    ||| 
                    ||          |           d{V }| j        rB|                     |d          }|                     |d          }|                    ||          }|                     |||dd          S )a  
        get the list of most recent trades for a list of symbols

        https://toobit-docs.github.io/apidocs/spot/v1/en/#trade-streams

        :param str[] symbols: 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
        :param str [params.name]: the name of the method to call, 'trade' or 'aggTrade', default is 'trade'
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=public-trades>`
        NFr   trade::idr)   r   r*   /quote/ws/v1,rS   subrz   rQ   rt   rz   	timestampT)load_marketsmarket_symbolsrl   rm   marketappend
market_idsrB   joinwatch_multipleextend
newUpdatesrk   r^   getLimitfilter_by_since_limit)rI   r   r{   r|   r   messageHashes	subParamsrr   rz   r   rawHash	marketIdsurlrequesttradesfirsttradeSymbols                    rK   r~   ztoobit.watch_trades_for_symbols   s      !!!!!!!!!%%gtU;;	q#g,,'' 	& 	&AQZF[[((F  V!3444TlGW%%%%OOG,,	it$X.?hhy))
 

 **3t{{7TZ?[?[]jkkkkkkkk? 	8OOFA..E**5(;;KOOK77E))&%TRRRrL   c                    |                      |d          }|                     |          }|d         }|| j        vr3|                     | j        dd          }t          |          | j        |<   | j        |         }|                     |dg           }|                     ||          }	t          dt          |	                    D ]$}
|	|
         }||d<   |
                    |           %d|z   }|                    ||           d S )Nrz   tradesLimit  datar   r   )r^   safe_marketr   r`   rC   r   	safe_listparse_ws_tradesrl   rm   r   resolve)rI   rM   rn   marketIdr   rz   r|   storedr   parsedrr   rS   messageHashs                rK   rb   ztoobit.handle_trades   s   0 ##GX66!!(++!$+%%%%dlM4HHE",U"3"3DKV$~~gvr22%%dF33q#f++&& 	! 	!A1IE$E(OMM%    &(v{+++++rL   rS   r   c                 .    |                      ||          S rx   )parse_trade)rI   rS   r   s      rK   parse_ws_tradeztoobit.parse_ws_trade   s    v...rL   r+   c                 n   K   d|d<   |                      ||gg|||           d{V }||         |         S )a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://toobit-docs.github.io/apidocs/spot/v1/en/#kline-candlestick-streams

        :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
        r    callerMethodNameN)watch_ohlcv_for_symbols)rI   rz   	timeframer{   r|   r   results          rK   watch_ohlcvztoobit.watch_ohlcv   sW       &2!"33fi5H4I5RWY_````````f~i((rL   symbolsAndTimeframesc                   K   |                                   d{V  | j        d         d         d         dz   }g }|                     | j        d         di           }g }d}	t	          dt          |                    D ]}
||
         }|                     |d          }|                     |          }|d         }|                     |d	d
          }|                     |||          }|	|	|k    rt          | j	        dz             |}	|
                    |           |
                    d|z   dz   |z              d                    |          d|	z   d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://toobit-docs.github.io/apidocs/spot/v1/en/#kline-candlestick-streams

        :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 dict: A list of candles ordered, open, high, low, close, volume
        Nr)   r   r*   r   r<   r   r      r+   zH watchOHLCVForSymbols() only supports a single timeframe for all symbolsohlcv::::r   kline_r   r   T)r   rB   	safe_dictrC   rl   rm   r^   r   r   r   r   r   r   r   r   r   r   create_ohlcv_object)rI   r   r{   r|   r   r   r   r<   r   selectedTimeframerr   r   	symbolStrr   r   unfiedTimeframerawTimeframer   rz   r   r   filtereds                         rK   r   ztoobit.watch_ohlcv_for_symbols  s$      !!!!!!!!!it$X.?^^DL$6bII
	!%q#23344 	Q 	QA'*D((q11I[[++Fd|H"..tQ==O++JYYL ,1Bl1R1R"47-w#wxxx$0!X&&&  Y!6!=!OPPPPhhy)) 11
 

 +/*=*=c=RVR]R]^egmRnRnp}*~*~$~$~$~$~$~$~!	6? 	3OOFE22E--feUAtLL''	8DDDrL   c                    |                      |d          }|                     |          }|d         }|                     |di           }|                      |d          }|                     |          }|| j        vr
i | j        |<   || j        |         vr?|                     | j        d         dd          }	t          |	          | j        |         |<   | j        |         |         }
|                     |dg           }t          dt          |                    D ]3}|                     ||         |          }|
                    |           4d	|z   d
z   |z   }|||
g}|                    ||           d S )Nrz   r   	klineTyper   
OHLCVLimitr   r   r   r   r   )r^   r   r   find_timeframeohlcvsr`   rC   r   r   rl   rm   parse_ws_ohlcvr   r   )rI   rM   rn   r   r   rz   r   timeframeIdr   r|   r   r   rr   r   r   resolveDatas                   rK   rc   ztoobit.handle_ohlcv9  s   4 ##GX66X&&!266&&v{;;''44	$+%%"$DKT[000%%dl4&8,MME-B5-I-IDK	*V$Y/~~gvr22q#d))$$ 	" 	"A((a&99FMM&!!!!&(4/);y&1{K00000rL   c                 2    |                      ||          }|S rx   )parse_ohlcv)rI   ohlcvr   r   s       rK   r   ztoobit.parse_ws_ohlcvg  s     !!%00rL   c                    K   |                                   d{V  |                     |          }|                     |g|           d{V }||         S )a  

        https://toobit-docs.github.io/apidocs/spot/v1/en/#individual-symbol-ticker-streams

        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
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        N)r   rz   watch_tickers)rI   rz   r   tickerss       rK   watch_tickerztoobit.watch_tickerx  sp       !!!!!!!!!V$$**F8V<<<<<<<<vrL   c                   K   |                                   d{V  |                     |dd          }g }g }t          dt          |                    D ]T}||         }|                     |          }|                    d|z              |d         }|                    |           U|                     |          }	| j        d         d         d         d	z   }
d
                    |	          ddd}| 	                    |
|| 
                    ||          |           d{V }| j        ri }|||d         <   |S |                     | j        d|          S )a  

        https://toobit-docs.github.io/apidocs/spot/v1/en/#individual-symbol-ticker-streams

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list
        :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   ticker::r   r)   r   r*   r   r   rU   r   r   rz   )r   r   rl   rm   r   r   r   rB   r   r   r   r   filter_by_arrayr   )rI   r   r   r   r   rr   rz   r   r   r   r   r   tickerr   s                 rK   r   ztoobit.watch_tickers  s      !!!!!!!!!%%gtU;;	q#g,,'' 	& 	&AQZF[[((F  f!4555TlGW%%%%OOG,,	it$X.?hhy)) 
 

 **3t{{7TZ?[?[]jkkkkkkkk? 	F'-F6(#$M##DL(GDDDrL   c                 @   |                      |d          }i }t          dt          |                    D ]Q}||         }|                     |          }|d         }|| j        |<   |||<   d|z   }	|                    ||	           R|                    |d           d S )Nr   r   rz   r   r   )r   rl   rm   parse_ws_tickerr   r   )
rI   rM   rn   r   
newTickersrr   r   r   rz   r   s
             rK   rd   ztoobit.handle_tickers  s    H ~~gv..
q#d))$$ 	0 	0A!WF))&11FH%F#)DL !'Jv$v-KNN6;////z9-----rL   c                 .    |                      ||          S rx   )parse_ticker)rI   r   r   s      rK   r   ztoobit.parse_ws_ticker  s      000rL   c                 B   K   |                      |g||           d{V S )aI  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://toobit-docs.github.io/apidocs/spot/v1/en/#partial-book-depth-streams

        :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)rI   rz   r|   r   s       rK   watch_order_bookztoobit.watch_order_book  s4       66xOOOOOOOOOrL   c                   K   |                                   d{V  |                     |dd          }d}|                     |ddd          \  }}g }g }t          dt	          |                    D ]Z}||         }|                     |          }	|                    d|z   dz   |z              |	d	         }
|                    |
           [|                     |          }| j        d
         d         d         dz   }d	                    |          |dd}| 
                    |||                     ||          |           d{V }|                                S )a,  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://toobit-docs.github.io/apidocs/spot/v1/en/#partial-book-depth-streams

        :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
        NFr"   r9   r:   r   orderBook::r   r   r)   r   r*   r   r   r   r   )r   r   handle_option_and_paramsrl   rm   r   r   r   rB   r   r   r   r|   )rI   r   r|   r   r9   r   r   rr   rz   r   r   r   r   r   	orderbooks                  rK   r   z#toobit.watch_order_book_for_symbols  s      !!!!!!!!!%%gtU;;77@PR[]dee	q#g,,'' 	& 	&AQZF[[((F  !7$!>!HIIITlGW%%%%OOG,,	it$X.?hhy))
 

 --c=$++gW]B^B^`mnnnnnnnn	   rL   c                 V   |                      |dd          }|r|                     ||d           d S |                     |d          }|                     |          }|d         }|                     |dg           }t          dt          |                    D ]}||         }	d|z   dz   dz   }
|| j        vr@|                     | j	        d	         d
d          }| 
                    i |          | j        |<   | j        |         }|                     |	d          }|                     |	dg           }|                     |	dg           }|                     |d         |           |                     |d         |           ||d<   || j        |<   |                    ||
           d S )NfFrV   rz   r   r   r   r   r   orderBookLimitr   tbaasksbidsr   )	safe_boolset_order_book_snapshotr^   r   r   rl   rm   
orderbooksr`   rC   
order_bookhandle_deltasr   )rI   rM   rn   
isSnapshotr   r   rz   r   rr   entryr   r|   	orderBookr   r   r   s                   rK   rf   ztoobit.handle_order_book
  s   , ^^GS%88
 	((+FFFF##GX66!!(++!~~gvr22q#d))$$ 	3 	3AGE'&047+EKdo--))$,t*<>NPTUU*.//"e*D*D'/I))%55I>>%b11D>>%b11Dy0$777y0$777%.Ik"&/DOF#NN9k2222	3 	3rL   c                 Z    |                      |          }|                    |           d S rx   )parse_bid_ask
storeArray)rI   booksidedeltabidAsks       rK   handle_deltaztoobit.handle_delta8  s/    ##E**F#####rL   c                 4    |                      ||d           d S )Nr:   )r   )rI   rM   rn   s      rK   re   z)toobit.handle_order_book_partial_snapshot<  s"    . 	$$VWg>>>>>rL   r9   c                 d   |                      |dg           }t          |          }|dk    rd S t          d|          D ]}||         }|                     |d          }|                     |          }	d|	z   dz   |z   }
|	| j        vr@|                     | j        d         dd          }|                     i |          | j        |	<   | j        |	         }|                     |d	          }| 	                    ||	|d
d          }|
                    |           |                    ||
           d S )Nr   r   sr   r   r   r   r   r   r   r   )r   rm   rl   r^   safe_symbolr   r`   rC   r   parse_order_bookresetr   )rI   rM   rn   r9   r   lengthrr   r   r   rz   r   r|   r   r   snapshots                  rK   r   ztoobit.set_order_book_snapshotU  sD   ~~gvr22TQ;;Fq&!! 	3 	3AGE''s33H%%h//F'&047'AKdo--))$,t*<>NPTUU*.//"e*D*D'/I))%55I,,UFIsCPPHOOH%%%NN9k2222	3 	3rL   c                   K   |                                   d{V  |                                  d{V  d}|                     dd|          \  }}|dk    }|rdnd}d}d}d}d}|r|n|}	|r|n|}
|                                 }|                     |          }|                     |||
|           |                    |dz              |                     ||	||
           d{V S )	ar  
        query for balance and get the amount of funds available for trading or funds locked in orders

        https://toobit-docs.github.io/apidocs/spot/v1/en/#payload-account-update

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
        Nr   spotcontractzspot:balancezcontract:privatezcontract:balance:fetchBalanceSnapshot)r   authenticatehandle_market_type_and_paramsget_user_stream_urlrM   set_balance_cachefuturewatch)rI   r   
marketTypeisSpottypespotSubHashswapSubHashspotMessageHashswapMessageHashr   subscriptionHashr   rM   s                rK   watch_balanceztoobit.watch_balanceh  s<      !!!!!!!!!!!!!!!!!!
!??PTV\]]
F&/vvZ$((,)/Doo_*0A;;k&&((S!!vz3CVLLLd44555ZZ[&:JKKKKKKKKKrL   r  c                     ||j         v rd S |dk    rdnd}|dz   }||j        vr4|                    |           |                     | j        |||           d S d S )Nr   r   r  )subscriptionsfuturesr  spawnload_balance_snapshot)rI   rM   r  r  r   r
  r   s          rK   r  ztoobit.set_balance_cache  s{    v333F$..vvZ44v~--MM+&&&JJt16;
SSSSS .-rL   c                 @   |                      |d          }|                     |dg           }|                     |d          }|dk    rdnd}|| j        vr
i | j        |<   || j        |         d<   || j        |         d<   |                     |          | j        |         d	<   t          d
t          |                    D ]}||         }|                      |d          }	|                     |	          }
|                                 }||d<   |                      |d          |d<   |                      |d          |d<   || j        |         |
<   | 	                    | j        |                   | j        |<   |
                    | j        |         |dz              d S )Nr]   BErX   r   r   infor   datetimer   r   lusedr   free:balance)r^   r   r`   balanceiso8601rl   rm   safe_currency_codeaccountsafe_balancer   )rI   rM   rn   r9   r   r   r
  rr   r  
currencyIdcoder"  s               rK   rg   ztoobit.handle_balance  s   D ""7C00~~gsB//%%gs33	%)FFFzzV$$!#DL%)T6"*3T;')-i)@)@T:&q#d))$$ 	/ 	/A1gG))'377J**:66DllnnG%GFO"..w<<GFO"..w<<GFO'.DLt$$!..t|D/ABBTt|D)4*+<=====rL   c                   K   |                      d|i           d {V }|dk    rdnd}|                     ||                     | j        |i                     | j        |<   ||j        v rk|j        |         }|                                 |                    | j        |         |dz              |                    | j        |         |dz              d S d S )Nr
  r   r   r  r  )fetch_balancer   r   r  r  r   )rI   rM   r   r  responser
  r  s          rK   r  ztoobit.load_balance_snapshot  s      ++VZ,@AAAAAAAA$..vvZ![[4>>$,PTVX3Y3YZZT&.((^K0FNNNN4<-t6M/MNNNNN4<-tj/@AAAAA	 )(rL   c                   K   |                                   d{V  |                                  d{V  |                     |          }|                     |d|          }d}||dz   |z   }|                                 }|                     ||||           d{V }| j        r|                    ||          }|                     ||||d          S )a;  
        watches information on multiple orders made by the user

        https://toobit-docs.github.io/apidocs/spot/v1/en/#payload-order-update

        :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
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        Nrz   orders:T)	r   r  market_or_nullr^   r  r  r   r   filter_by_symbol_since_limit)	rI   rz   r{   r|   r   r   r   r   r*  s	            rK   watch_ordersztoobit.watch_orders  s      !!!!!!!!!!!!!!!!!!$$V,,!!&(F;;%+f4K&&((zz#{FKHHHHHHHH? 	3OOFE22E00tTTTrL   c                 d   | j         0|                     | j        dd          }t          |          | _         | j         }|                     |          }|                    |           d}|                    ||           d|                     |d          z   }|                    ||           d S )NordersLimitr   r*  zorders:rz   )r*  r`   rC   r   parse_ws_orderr   r   r^   )rI   rM   rn   r|   r*  orderr   s          rK   rh   ztoobit.handle_order  s    @ ;%%dlM4HHE077DK##G,,ev{+++$"2"25("C"CCv{+++++rL   c                    |                      |d          }|                     |d          }|                     ||          }|                     |d          }|                     |d          }d }|dk    rd}n|}|                     |d          }	d }
|	|	d d}
|                     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$|          S )%NOr   ptor   n)costcurrencyr  r   rr   clientOrderIdcr   r  lastUpdateTimestampUr  rz   r
  timeInForcer   postOnlysideSpriceL	stopPricetriggerPriceamountqr8  averagepzX)filled	remainingstatusfeer   )
r`   r^   r   safe_string_lowersafe_number
safe_orderr   safe_integer_2safe_string_upperparse_order_status)rI   r2  r   r   r   rz   	priceTyperawOrderType	orderTypefeeCostrO  s              rK   r1  ztoobit.parse_ws_order  s   %%eS11	##E3//!!(F33**5$77	--eS99	   II$I""5#..  C   
E 
$""5#.. 
 T--eS99 
 	 

 Y// 
 "4#6#6uc3#G#G 
 f 
 I 
 411%== 
  
 D**5#66 
 T%%eS11 
  
 D 
 d&&uc22 
  D! 
" t''s33# 
$ &&uc22--d.>.>uc.J.JKK- 
  
  
. /  	rL   c                   K   |                                   d{V  |                                  d{V  |                     |          }|                     |d|          }d}||dz   |z   }|                                 }|                     ||||           d{V }| j        r|                    ||          }|                     |||dd          S )a  
        watches information on multiple trades made by the user

        https://toobit-docs.github.io/apidocs/spot/v1/en/#payload-ticket-push

        :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
        :param boolean [params.unifiedMargin]: use unified margin account
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=trade-structure>`
        Nrz   myTradesr+  r   T)	r   r  r,  r^   r  r  r   r   r   )	rI   rz   r{   r|   r   r   r   r   r   s	            rK   watch_my_tradesztoobit.watch_my_trades=  s      !!!!!!!!!!!!!!!!!!$$V,,!!&(F;; %+f4K&&((zz#{FKHHHHHHHH? 	3OOFE22E))&%TRRRrL   c                 4   | j         }|+|                     | j        dd          }t          |          }|                     |          }|                    |           d|d         z   }|                    ||           d}|                    ||           d S )Nr   r   z	myTrades:rz   r[  )r[  r`   rC   r   parse_my_trader   r   )rI   rM   rn   r[  r|   rS   r   s          rK   ri   ztoobit.handle_my_tradeW  s    " =%%dlM4HHE-e44H##G,,!E(O3x--- x-----rL   c                    |                      |d          }|                      |d          }|                     ||                      |d          ||                     |          |                     ||          |                      |d          d |                     |d          |                     |d          rdnd|                      |d	          |                      |d
          d d d|          S )Nr   r   Tr6  rA  mmakertakerrI  rG  )r  r   r   r  rz   r2  r
  r@  takerOrMakerrB  rF  r8  rO  )r^   
safe_trader   r   rP  r   )rI   rS   r   r   tss        rK   r^  ztoobit.parse_my_trades  s    ##E3//eS))""5#..R((&&x88%%eS11**5#66'+~~eS'A'ANGGw%%eS11&&uc22 
  
   	rL   c                   K   |                                   d{V  |                                  d{V  d}|                     |          s-|                     |          }dd                    |          z   }|                                 }|                     |          }|                     |           d{V  |                     ||           | j        }|4|	                    d           d{V }	| 
                    |	|||d          S |                     ||d|           d{V }
| j        r|
S | 
                    ||||d          S )a  

        https://toobit-docs.github.io/apidocs/usdt_swap/v1/en/#event-position-update

        watch all open positions
        :param str[] [symbols]: list of unified market symbols
        :param int [since]: the earliest time in ms to fetch positions for
        :param int [limit]: the maximum number of positions to retrieve
        :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>`
        N r   r   fetchPositionsSnapshotT)r   r  is_emptyr   r   r  rM   set_positions_cache	positionsr  filter_by_symbols_since_limitr  r   )rI   r   r{   r|   r   r   r   rM   cacher   newPositionss              rK   watch_positionsztoobit.watch_positions  s      !!!!!!!!!!!!!!!!!!}}W%% 	3))'22G'!2!22K&&((S!!$$$$$$$$$  111=#]]+CDDDDDDDDH55hPUW[\\\!ZZ[$LLLLLLLL? 	 11%%PTUUUrL   Fc                     | j         i | _         || j         v rd S |                     ddd          }|rE|dz   }||j        vr5|                    |           |                     | j        ||||           d S d S t                      | j         |<   d S )NwatchPositionsri  Fz:fetchPositionsSnapshot)rl  handle_optionr  r  r  load_positions_snapshotr   )rI   rM   r
  r   isPortfolioMarginri  r   s          rK   rk  ztoobit.set_positions_cache  s    >!DN4>!!F!%!3!34DF^`e!f!f! 	>!::K6>11k***

47dTefffff 21 $<#=#=DN4   rL   c                   K   d|i}|                      d |           d {V }t                      | j        |<   | j        |         }t          dt	          |                    D ]}||         }|                    |            ||j        v r=|j        |         }	|	                    |           |                    ||dz              d S d S )Nr
  r   
:positions)fetch_positionsr   rl  rl   rm   r   r  r   )
rI   rM   r   r
  r   rl  rn  rr   positionr  s
             rK   rt  ztoobit.load_positions_snapshot  s      D
 ..tV<<<<<<<<	799tt$q#i..)) 	# 	#A |HLL""""&.((^K0FNN5!!!NN5$"566666 )(rL   c                    t          |j                                                  }|d         }| j        i | _        || j        vrt	                      | j        |<   | j        |         }g }t          dt          |                    D ]|}||         }|                     |          }	|                     |d          }
|
|	d<   | 	                    |
          |	d<   |
                    |	           |
                    |	           }|                     ||dz             }t          dt          |                    D ]}||         }|                    d          }|d         }|                    d          }|                     |d	|d
          }|                     |          s|                    ||           |                    ||dz              d S )Nr   r  r   r  z:positions::r   r   r   rz   Frw  )listr  keysrl  r   rl   rm   parse_ws_positionr`   r   r   find_message_hashessplitr   rj  r   )rI   rM   rn   r  accountTypern  ro  rr   rawPositionry  r   r   r   partssymbolsStringr   rl  s                    rK   rj   ztoobit.handle_positions  s   2 V1668899#A&>!DNt~--*B*D*DDN;'{+q#g,,'' 	# 	#A!!*K--k::H))+s;;I$-H[!#'<<	#:#:HZ )))LL""""00~9UVVq#m,,-- 	7 	7A'*K%%d++E!!HM#))#..G,,\8WeTTI==++ 7y+666|[<%?@@@@@rL   c                    |                      |d          }|                     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          S )Nr   r  r   rz   notionalpv
marginModemtliquidationPricer   
entryPricerI  unrealizedPnluprealizedPnlr
percentage	contractscontractSize	markPricempr@  rA  hedgedr   r  mmra  v)maintenanceMarginmaintenanceMarginPercentage
collateralinitialMargininitialMarginPercentageleveragemarginRatio)r^   safe_positionr   	omit_zerorP  rQ  )rI   ry  r   r   s       rK   r}  ztoobit.parse_ws_position  s   ##Hc22!! #
H#
$#
 d&&x66#
 t'7'7$'G'GHH	#

 $004@@#
  0 03 ? ?#
 $**8S99#
 T--h==#
 4++Hc::#
 $#
 #
 D#
 ))(D99#
 D**8S99#
 d#
  !#
" ##
$ "&!1!1(D!A!A+/!^^D,<,<Xs,K,KLL'+((3771#
 #
 #
   	rL   c                   K   |                      |                                           }d}|                    |          }|                     |j        |          }||                                  |                                 }|                     | j        d         dd          }|                     | j        d         dd          }| 	                    |d          }	||z
  |	k    r	 d|j        |<   | 
                    |           d {V }
|                     |
d	          | j        d         d	<   || j        d         d<   |                    d           |                     || j        |           nk# t          $ r^}t!          | j        d
z   |                     |          z             }|                    ||           ||j        v r|j        |= Y d }~nd }~ww xY w| d {V S )Nauthenticatedr   lastAuthenticatedTimer   r=   r;   i'  T	listenKey )rM   r  reusableFuturerk   r  check_required_credentialsrO   r`   rC   sumprivatePostApiV1UserDataStreamr^   r   delaykeep_alive_listen_key	Exceptionr   r   exception_messagereject)rI   r   rM   r   r  r  timer  r=   r  r(  r]   errs                rK   r  ztoobit.authenticate  s     T557788%&&{33(<kJJ ++---$$&&D$($5$5dl46HJacd$e$e!#'#4#4T\$5GI_ah#i#i HH1599E++e33>8<F(5%)%H%H%P%PPPPPPPH6:6F6FxQ\6]6]DL&{3BFDL&'>?NN4(((JJ3T5OQWXXXX  > > >-dgmd>T>TUV>W>W.WXXCMM#{333"f&:::"0=	>
 ||||||s   +B
E6 6
G AGGc                 B  K   |                      | j        di           }|                     |d          }|d S 	 |                     |           d {V }|                     |d          | j        d         d<   |                                 | j        d         d<   n# t
          $ r}|                                 }|                     |          }t          |j	        
                                          }t          dt          |                    D ] }	||	         }
|                    ||
           !d | j        d         d<   d| j        d         d<   Y d }~d S d }~ww xY w|                     | j        d         dd          }|                     || j        |           d S )Nr   r  r  r   r=   r;   )rk   rC   r^   r  rO   r  r  rM   r{  r  r|  rl   rm   r  r`   r  r  )rI   r   rC   r  r(  errorr   rM   r   rr   r   r=   s               rK   r  ztoobit.keep_alive_listen_key.  s     //$,b99$$Wk::	F	!@@HHHHHHHHH.2.>.>x.U.UDL{+:>:K:K:M:MDL677 		 		 		**,,C[[%%F !4!4!6!677M1c-0011 2 2+A.e[1111.2DL{+:;DL67FFFFF		  $00d1CE[]dee

')CVLLLLLs   A!B 
E&B-EEc                 b    | j         d         d         d         dz   | j        d         d         z   S )Nr)   r   r*   z/api/v1/ws/r  )rB   rC   )rI   s    rK   r  ztoobit.get_user_stream_urlF  s0    y%h/-?$,tBTU`BaaarL   c                     |                      |d          }|O|                      |d          }| j        dz   |z   dz   |z   }t          |          }|                    |           dS dS )Nr%  descz code: z
 message: TF)r^   r   r   r  )rI   rM   rn   r%  r  msg	exceptions          rK   r_   ztoobit.handle_error_messageI  sx     00##GV44D'I%,|;dBC%c**IMM)$$$4urL   rx   )NF)?__name__
__module____qualname__r   rH   r   r?   rv   r
   ra   strr   r   r   r~   rb   dictr   r   r{  r   r   rc   r   r   r   r   r   r   rd   r   r   r   r   rf   r   re   r   r   r  r   r  rg   r  r   r.  rh   r1  r\  ri   r^  r   rp  rk  rt  rj   r}  r  r  r  r	   r_   __classcell__)rJ   s   @rK   r   r      s       :# : : : : : :x
6 
 
 
 

H*V H* H* H* H*T(6 (# ( ( ( ( <@d[] S S SS S Sbfglbm S S S S OSaenp #S #Sd3i #S #S[^ #Suyz  vA #S #S #S #SJ&,F &, &, &, &,P/ /D /& /E / / / / 8<$]ajl ) ) )C )WZ )quvzq{ ) ) ) )" aesw  AC (E (E$tCy/ (EZ] (Emp (E (E (E (ET,16 ,1 ,1 ,1 ,1\ D    " 68   F     6:"  E  E7  E  E  E  E  ED..V .. .. .. ..`1 1 1 1 @DB P PS P PS\ P P P P SW_a ! !$s) !C !fo ! ! ! !B,3 ,3 ,3 ,3 ,3\$ $ $? ? ? ? ?23f 3 3 3 3 3& *, L L L L L L6 UYac T T Tc T T T T5>V 5> 5> 5> 5>n	B 	B 	B 04$UYbd U U UC Us Uimnsit U U U U2),6 ), ), ), ),V) ) ) )V 37TX\eg S SC Ss SRU Slpqvlw S S S S4.f . . . .8   & 8<$]ajl V VW VC VWZ Vquv~q V V V V>> >& > > > > >7 7 7 2A 2A 2Ah   : )+    4 24 M M M M0b b b6 t        rL   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   ccxt.base.typesr   r   r	   r
   r   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   async_supportr    rL   rK   <module>r     sS       A  A  A  A  A  A  A  A  A  A  A  A ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 ) ) ) ) ) )G G G G GT& G G G G GrL   