
    E#ig                         S SK rS SKJrJrJrJr  S SKJrJ	r	J
r
JrJrJrJrJrJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr   " S	 S
\R:                  R<                  5      rg)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesBoolIntMarketOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)NotSupportedc            
       j  ^  \ rS rSrS\4U 4S jjrS\4S jrS\4S jrS\S\	4S jr
S	S	0 4S
\S\	S\	S\\   4S jjrS	S	0 4S\\   S\	S\	S\\   4S jjrS\4S jrS:S\S\S\4S jjrSS	S	0 4S
\S\	S\	S\\   4S jjrS	S	0 4S\\\      S\	S\	4S jjrS\4S jrS:S\4S jjr0 4S
\S\4S jjrS	0 4S\S\4S jjrS\4S jrS:S jrS	0 4S
\S\	S\ 4S jjr!S	0 4S\\   S\	S\ 4S jjr"S\4S  jr#S! r$S\4S" jr%S\S#\4S$ jr&0 4S\'4S% jjr(S	0 4S\S&\)4S' jjr*S\4S( jr+S) r,S	S	S	0 4S
\)S\	S\	S\\-   4S* jjr.S\4S+ jr/S:S, jr0S	S	S	0 4S
\)S\	S\	S\\   4S- jjr1S\4S. jr2S:S/ jr3S	S	S	0 4S\S\	S\	S\\4   4S0 jjr5S;S\S\4S1 jjr6S2 r7S3 r8S:S4 jr90 4S5 jr:0 4S6 jr;S7 r<S\S\=4S8 jr>S9r?U =r@$ )<toobit   returnc                    > U R                  [        [        U ]  5       SSSSSSSSSSSSS.SSSS000SSSS	S
SSSSSSSSSSS.SS0SS.0SU R                  S.SS0 00S.5      $ )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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   channeldepthO )
timeframesr#   listenKeyRefreshRateiX )	keepAlivepingexact)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describer@   )self	__class__s    I/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/toobit.pyrI   toobit.describe   s    fd < > $!%"(,"&,0## $#)-   "; """$$"""""$"""#" "7' -4)2 1		
  g9A
 9 9	    clientc                 &    SU R                  5       0$ )Nr@   )milliseconds)rJ   rO   s     rL   r@   toobit.pingN   s    D%%'
 	
rN   c                 v   U R                  US5      nU R                  X5      (       a  g U R                  US5      nUb  U R                  X5        g U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S.nU R                  XS5      nUb	  U" X5        g [        S[        U5      5       H7  nX'   nU R                  US5      n	U R                  XY5      n
U
c  M/  U
" X5        M9     g )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)rJ   rO   messagerT   pongTimestampmethodsmethodiitemeventmethod2s              rL   handle_messagetoobit.handle_messageS   s*   N   '2$$V55 ))'6:$%%f<''&&,,<<//#'#6#6+/+>+>#00'+'8'8..,0,A,A
 06# 1c'l+z((s3//'9&F) ,rN   rr   c                     X!l         g N)lastPong)rJ   rO   rr   s      rL   rd   toobit.handle_incoming_pong   s    'rN   Nsymbolsincelimitc                 F   #    U R                  U/X#U5      I Sh  vN $  N7f)a  
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)rJ   r   r   r   paramss        rL   watch_tradestoobit.watch_trades   s$      22F8U6RRRRs   !!symbolsc                   #    U R                  5       I Sh  vN   U R                  USS5      n/ n/ n[        S[        U5      5       HB  nX   nU R	                  U5      n	UR                  SU-   5        U	S   n
UR                  U
5        MD     U R                  U5      nU R                  S   S   S   S	-   nS
R                  U5      SSS.nU R                  XU R                  X5      U5      I Sh  vN nU R                  (       a6  U R                  US5      nU R                  US5      nUR                  UU5      nU R                  XUSS5      $  GN: Nb7f)aK  
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,rV   subr   rT   rw   r   	timestampT)load_marketsmarket_symbolsro   rp   marketappend
market_idsrC   joinwatch_multipleextend
newUpdatesrn   ra   getLimitfilter_by_since_limit)rJ   r   r   r   r   messageHashes	subParamsru   r   r   rawHash	marketIdsurlrequesttradesfirsttradeSymbols                    rL   r   toobit.watch_trades_for_symbols   sO     !!!%%gtU;	q#g,'AZF[[(F  V!34TlGW% ( OOG,	iit$X.?hhy)

 **3t{{7?[]jkk??OOFA.E**5(;KOOK7E))&TRR- 	"" ls#   EECE0E1A EEc                    U R                  US5      nU R                  U5      nUS   nXPR                  ;  a5  U R                  U R                  SS5      n[        U5      U R                  U'   U R                  U   nU R                  US/ 5      nU R                  X5      n	[        S[        U	5      5       H  n
X   nX[S'   UR                  U5        M     SU-   nUR                  X|5        g )Nr   tradesLimit  datar   r   )ra   safe_marketr   rc   rD   r   	safe_listparse_ws_tradesro   rp   r   resolve)rJ   rO   rq   marketIdr   r   r   storedr   parsedru   rV   messageHashs                rL   re   toobit.handle_trades   s    0 ##GX6!!(+!++%%%dllM4HE",U"3DKKV$~~gvr2%%d3q#f+&AIE$(OMM%  '  &(v+rN   rV   r   c                 $    U R                  X5      $ r|   )parse_trade)rJ   rV   r   s      rL   parse_ws_tradetoobit.parse_ws_trade   s    ..rN   r,   c                 `   #    SUS'   U R                  X//X4U5      I Sh  vN nXa   U   $  N7f)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)rJ   r   	timeframer   r   r   results          rL   watch_ohlcvtoobit.watch_ohlcv   sC      &2!"33f5H4I5Y_``~i(( as   .,.symbolsAndTimeframesc                 X  #    U R                  5       I Sh  vN   U R                  S   S   S   S-   n/ nU R                  U R                  S   S0 5      n/ nSn	[	        S[        U5      5       H  n
X   nU R                  US5      nU R                  U5      nUS   nU R                  US	S
5      nU R                  XU5      nU	b  U	U:w  a  [        U R                  S-   5      eUn	UR                  U5        UR                  SU-   S-   U-   5        M     SR                  U5      SU	-   SS.nU R                  XVU R                  UU5      U5      I Sh  vN u  nnnU R                  (       a  UR                  UU5      nU R!                  UX#SS5      nU R#                  UUU5      $  GN NU7f)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   rC   	safe_dictrD   ro   rp   ra   r   r   r   r   r   r   r   r   r   r   create_ohlcv_object)rJ   r   r   r   r   r   r   r=   r   selectedTimeframeru   r   	symbolStrr   r   unfiedTimeframerawTimeframer   r   r   r   filtereds                         rL   r   toobit.watch_ohlcv_for_symbols  s     !!!iit$X.?^^DLL$6bI
	!%q#234A'*D((q1I[[+Fd|H"..tQ=O++JYL ,1Bl1R"477-w#wxx$0!X&  Y!6!=!OP 5 hhy) 11

 +/*=*=cRVR]R]^egmRnp}*~$~!	6??OOFE2E--feAtL''	8DD9 	"0 %s#   F*F%D;F*F(AF*(F*c                    U R                  US5      nU R                  U5      nUS   nU R                  US0 5      nU R                  US5      nU R                  U5      nXPR                  ;  a  0 U R                  U'   XR                  U   ;  a;  U R                  U R                  S   SS5      n	[        U	5      U R                  U   U'   U R                  U   U   n
U R                  US/ 5      n[        S[        U5      5       H(  nU R                  X   U5      nU
R                  U5        M*     S	U-   S
-   U-   nXXU
/nUR                  X5        g )Nr   r   	klineTyper   
OHLCVLimitr   r   r   r   r   )ra   r   r   find_timeframeohlcvsrc   rD   r   r   ro   rp   parse_ws_ohlcvr   r   )rJ   rO   rq   r   r   r   r   timeframeIdr   r   r   r   ru   r   r   resolveDatas                   rL   rf   toobit.handle_ohlcv9  sD   4 ##GX6X&!26&&v{;''4	++%"$DKK[[00%%dll4&8,ME-B5-IDKK	*V$Y/~~gvr2q#d)$A((&9FMM&! %  &(4/);&1{0rN   c                 (    U R                  X5      nU$ r|   )parse_ohlcv)rJ   ohlcvr   r   s       rL   r   toobit.parse_ws_ohlcvg  s     !!%0rN   c                    #    U R                  5       I Sh  vN   U R                  U5      nU R                  U/U5      I Sh  vN nX1   $  N4 N
7f)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   r   watch_tickers)rJ   r   r   tickerss       rL   watch_tickertoobit.watch_tickerx  sN      !!!V$**F8V<< 	"<s!   AA+AA	AAc                 j  #    U R                  5       I Sh  vN   U R                  USS5      n/ n/ n[        S[        U5      5       HB  nX   nU R	                  U5      nUR                  SU-   5        US   nUR                  U5        MD     U R                  U5      n	U R                  S   S   S   S	-   n
S
R                  U	5      SSS.nU R                  XU R                  X5      U5      I Sh  vN nU R                  (       a  0 nXUS   '   U$ U R                  U R                  SU5      $  GN N@7f)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   rX   r   r   r   )r   r   ro   rp   r   r   r   rC   r   r   r   r   filter_by_arrayr   )rJ   r   r   r   r   ru   r   r   r   r   r   r   tickerr   s                 rL   r   toobit.watch_tickers  s6     !!!%%gtU;	q#g,'AZF[[(F  f!45TlGW% ( OOG,	iit$X.?hhy) 

 **3t{{7?[]jkk??F'-6(#$M##DLL(GDD- 	"" ls"   D3D.CD30D11>D31D3c                    U R                  US5      n0 n[        S[        U5      5       HE  nX5   nU R                  U5      nUS   nXpR                  U'   XtU'   SU-   n	UR                  Xy5        MG     UR                  US5        g )Nr   r   r   r   r   )r   ro   rp   parse_ws_tickerr   r   )
rJ   rO   rq   r   
newTickersru   r   r   r   r   s
             rL   rg   toobit.handle_tickers  s    H ~~gv.
q#d)$AWF))&1FH%F#)LL !'v$v-KNN6/ % 	z9-rN   c                 $    U R                  X5      $ r|   )parse_ticker)rJ   r   r   s      rL   r   toobit.parse_ws_ticker  s      00rN   c                 D   #    U R                  U/X#5      I Sh  vN $  N7f)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 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)rJ   r   r   r   s       rL   watch_order_booktoobit.watch_order_book  s"      66xOOOOs     c                 T  #    U R                  5       I Sh  vN   U R                  USS5      nSnU R                  USSS5      u  pC/ n/ n[        S[	        U5      5       HH  nX   nU R                  U5      n	UR                  SU-   S-   U-   5        U	S	   n
UR                  U
5        MJ     U R                  U5      nU R                  S
   S   S   S-   nSR                  U5      USS.nU R                  XU R                  X5      U5      I Sh  vN nUR                  5       $  GN N7f)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#   r:   r;   r   orderBook::r   r   r*   r   r+   r   r   r   r   )r   r   handle_option_and_paramsro   rp   r   r   r   rC   r   r   r   r   )rJ   r   r   r   r:   r   r   ru   r   r   r   r   r   r   	orderbooks                  rL   r   #toobit.watch_order_book_for_symbols  s6     !!!%%gtU;77@PR[]de	q#g,'AZF[[(F  !7$!>!HITlGW% ( OOG,	iit$X.?hhy)

 --c$++gB^`mnn	  ) 	"& os"   D(D#C7D(D&D(&D(c                    U R                  USS5      nU(       a  U R                  XS5        g U R                  US5      nU R                  U5      nUS   nU R	                  US/ 5      n[        S[        U5      5       H  nXx   n	SU-   S-   S-   n
X`R                  ;  a?  U R                  U R                  S	   S
S5      nU R                  0 U5      U R                  U'   U R                  U   nU R                  U	S5      nU R	                  U	S/ 5      nU R	                  U	S/ 5      nU R                  US   U5        U R                  US   U5        XS'   XR                  U'   UR                  X5        M     g )NfFrY   r   r   r   r   r   r   orderBookLimitr   tbaasksbidsr   )	safe_boolset_order_book_snapshotra   r   r   ro   rp   
orderbooksrc   rD   
order_bookhandle_deltasr   )rJ   rO   rq   
isSnapshotr   r   r   r   ru   entryr   r   	orderBookr   r   r   s                   rL   ri   toobit.handle_order_book
  sj   , ^^GS%8
((+F##GX6!!(+!~~gvr2q#d)$AGE'&047+EKoo-))$,,t*<>NPTU*.//"e*D'/I))%5I>>%b1D>>%b1Dy0$7y0$7%.k"&/OOF#NN92 %rN   c                 H    U R                  U5      nUR                  U5        g r|   )parse_bid_ask
storeArray)rJ   booksidedeltabidAsks       rL   handle_deltatoobit.handle_delta8  s     ##E*F#rN   c                 (    U R                  XS5        g )Nr;   )r   )rJ   rO   rq   s      rL   rh   )toobit.handle_order_book_partial_snapshot<  s    . 	$$Vg>rN   r:   c                 "   U R                  US/ 5      n[        U5      nUS:X  a  g [        SU5       H  nXF   nU R                  US5      nU R	                  U5      n	SU	-   S-   U-   n
XR
                  ;  a?  U R                  U R                  S   SS5      nU R                  0 U5      U R
                  U	'   U R
                  U	   nU R                  US	5      nU R                  XyUS
S5      nUR                  U5        UR                  X5        M     g )Nr   r   sr   r   r   r   r   r   r   r   )r   rp   ro   ra   safe_symbolr   rc   rD   r   parse_order_bookresetr   )rJ   rO   rq   r:   r   lengthru   r   r   r   r   r   r   r   snapshots                  rL   r   toobit.set_order_book_snapshotU  s   ~~gvr2TQ;q&!AGE''s3H%%h/F'&047'AKoo-))$,,t*<>NPTU*.//"e*D'/I))%5I,,UIsCPHOOH%NN92 "rN   c                   #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   SnU R                  SSU5      u  p!US:H  nU(       a  SOSnSnSnSnSnU(       a  UOUn	U(       a  UOUn
U R                  5       nU R	                  U5      nU R                  XX5        UR                  US-   5        U R                  XX5      I Sh  vN $  N N N7f)	aL  
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_urlrO   set_balance_cachefuturewatch)rJ   r   
marketTypeisSpottypespotSubHashswapSubHashspotMessageHashswapMessageHashr   subscriptionHashr   rO   s                rL   watch_balancetoobit.watch_balanceh  s      !!!!!!
!??PTV\]
&vZ$((,)/o_*0;k&&(S!v3CLd445ZZ&KKK! 	"! Ls2   CCCCB%CCCCCr&  c                     X1R                   ;   a  g US:X  a  SOSnUS-   nXaR                  ;  a/  UR                  U5        U R                  U R                  XU5        g g )Nr  r  r  )subscriptionsfuturesr  spawnload_balance_snapshot)rJ   rO   r  r&  r   r!  r   s          rL   r  toobit.set_balance_cache  s[    333$.vZ44~~-MM+&JJt116
S .rN   c                    U R                  US5      nU R                  US/ 5      nU R                  US5      nUS:X  a  SOSnX`R                  ;  a  0 U R                  U'   X@R                  U   S'   XPR                  U   S'   U R	                  U5      U R                  U   S	'   [        S
[        U5      5       Hy  nXG   nU R                  US5      n	U R                  U	5      n
U R                  5       nXS'   U R                  US5      US'   U R                  US5      US'   XR                  U   U
'   M{     U R                  U R                  U   5      U R                  U'   UR                  U R                  U   US-   5        g )Nr`   BEr[   r  r  infor   datetimer   r   lusedr   free:balance)ra   r   rc   balanceiso8601ro   rp   safe_currency_codeaccountsafe_balancer   )rJ   rO   rq   r:   r   r   r!  ru   r8  
currencyIdcoder;  s               rL   rj   toobit.handle_balance  sm   D ""7C0~~gsB/%%gs3	%)FFzV$!#DLL%)T6"*3T;')-i)@T:&q#d)$AgG))'37J**:6DllnG%FO"..w<GFO"..w<GFO'.LLt$ % "..t||D/ABTt||D)4*+<=rN   c                   #    U R                  SU05      I S h  vN nUS:X  a  SOSnU R                  X@R                  U R                  U0 5      5      U R                  U'   UR                  U   nUR                  5         UR                  U R                  U   US-   5        UR                  U R                  U   US-   5        g  N7f)Nr!  r  r  r  r7  )fetch_balancer   r   r8  r+  r   )rJ   rO   r   r  responser!  r  s          rL   r-  toobit.load_balance_snapshot  s     ++VZ,@AA$.vZ![[>>$,,PTVX3YZT,t||D)42I+IJt||D)4*+<= Bs   CCB,Cc                   #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   U R                  U5      nU R                  USU5      nSnUb  US-   U-   nU R	                  5       nU R                  XvXF5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XX#S5      $  N N N=7f)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>`
Nr   orders:T)	r   r  market_or_Nonera   r  r  r   r   filter_by_symbol_since_limit)	rJ   r   r   r   r   r   r   r   rE  s	            rL   watch_orderstoobit.watch_orders  s      !!!!!!$$V,!!&(F;%+f4K&&(zz#FHH??OOF2E00tTT 	"! Is2   C	CC	CAC		C
:C	C	C	c                 F   U R                   c-  U R                  U R                  SS5      n[        U5      U l         U R                   nU R	                  U5      nUR                  U5        SnUR                  XF5        SU R                  US5      -   nUR                  XF5        g )NordersLimitr   rE  zorders:r   )rE  rc   rD   r   parse_ws_orderr   r   ra   )rJ   rO   rq   r   rE  orderr   s          rL   rk   toobit.handle_order  s    @ ;;%%dllM4HE07DK##G,ev+$"2"25("CCv+rN   c                    U R                  US5      nU R                  US5      nU R                  XB5      nU R                  US5      nU R                  US5      nS nUS:X  a  SnOUnU R	                  US5      n	S n
U	b  U	S S.n
U R                  0 SU_S	U R                  US
5      _SU R                  US5      _SU_SU R                  U5      _SU R                  USS5      _SU_SU_SU R                  US5      _SS _SU R                  US5      _SU R                  US5      _SS _SS _SU R                  US5      _SS _S U R                  US!5      _U R                  US"5      S U R                  U R                  US#5      5      U
S S$.EU5      $ )%NOr  ptor   n)costcurrencyr2  r   ru   clientOrderIdcr   r3  lastUpdateTimestampUr1  r   r!  timeInForcer   postOnlysideSpriceL	stopPricetriggerPriceamountqrU  averagepzX)filled	remainingstatusfeer   )
rc   ra   r  safe_string_lowersafe_number
safe_orderr9  safe_integer_2safe_string_upperparse_order_status)rJ   rN  r   r   r   r   	priceTyperawOrderType	orderTypefeeCostrl  s              rL   rM  toobit.parse_ws_order  s.   %%eS1	##E3/!!(3**5$7	--eS9	  I$I""5#. C   
E 
$""5#. 
 T--eS9 
 	 

 Y/ 
 "4#6#6uc3#G 
 f 
 I 
 411%= 
  
 D**5#6 
 T%%eS1 
  
 D 
 d&&uc2 
  D! 
" t''s3# 
$ &&uc2--d.>.>uc.JK- 
. / 	rN   c                   #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   U R                  U5      nU R                  USU5      nSnUb  US-   U-   nU R	                  5       nU R                  XvXF5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XUSS5      $  N N N>7f)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>`
Nr   myTradesrF  r   T)	r   r  rG  ra   r  r  r   r   r   )	rJ   r   r   r   r   r   r   r   r   s	            rL   watch_my_tradestoobit.watch_my_trades<  s      !!!!!!$$V,!!&(F; %+f4K&&(zz#FHH??OOF2E))&TRR 	"! Is2   C
CC
CAC
	C
;C
C
C
c                    U R                   nUc(  U R                  U R                  SS5      n[        U5      nU R	                  U5      nUR                  U5        SUS   -   nUR                  X65        SnUR                  X65        g )Nr   r   z	myTrades:r   ry  )ry  rc   rD   r   parse_my_trader   r   )rJ   rO   rq   ry  r   rV   r   s          rL   rl   toobit.handle_my_tradeV  s|    " ==%%dllM4HE-e4H##G,!E(O3x- x-rN   c                    U R                  US5      nU R                  US5      nU R                  UU R                  US5      UU R                  U5      U R                  X25      U R                  US5      S U R	                  US5      U R                  US5      (       a  SOSU R                  US	5      U R                  US
5      S S S.U5      $ )Nr  r   TrS  r^  mmakertakerrf  rd  )r2  r   r   r3  r   rN  r!  r]  takerOrMakerr_  rc  rU  rl  )ra   
safe_trader9  r  rm  r   )rJ   rV   r   r   tss        rL   r}  toobit.parse_my_trader  s    ##E3/eS)""5#.R(&&x8%%eS1**5#6'+~~eS'A'AGw%%eS1&&uc2 
  	rN   c                   #    U R                  5       I Sh  vN   U R                  5       I Sh  vN   SnU R                  U5      (       d%  U R                  U5      nSSR	                  U5      -   nU R                  5       nU R                  U5      nU R                  U5      I Sh  vN   U R                  Xq5        U R                  nUc,  UR                  S5      I Sh  vN n	U R                  XX#S5      $ U R                  XeSU5      I Sh  vN n
U R                  (       a  U
$ U R                  XX#S5      $  GN! GN N N` N47f)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  rO   set_positions_cache	positionsr  filter_by_symbols_since_limitr  r   )rJ   r   r   r   r   r   r   rO   cacher  newPositionss              rL   watch_positionstoobit.watch_positions  s$     !!!!!!}}W%%))'2G'!22K&&(S!$$$  1=#]]+CDDH55hW[\\!ZZ$LL??11%%PTUU# 	"! 	% ELsV   ED7ED:A6E%D=&8ED?-EE+E:E=E?EEc                 6   U R                   c  0 U l         X R                   ;   a  g U R                  SSS5      nU(       aD  US-   nXaR                  ;  a/  UR                  U5        U R	                  U R
                  XX$5        g g [        5       U R                   U'   g )NwatchPositionsr  Fz:fetchPositionsSnapshot)r  handle_optionr+  r  r,  load_positions_snapshotr   )rJ   rO   r!  r   isPortfolioMarginr  r   s          rL   r  toobit.set_positions_cache  s    >>!DN>>!!%!3!34DF^`e!f!!::K>>1k*

477df 2 $<#=DNN4 rN   c                 b  #    SU0nU R                  S U5      I S h  vN n[        5       U R                  U'   U R                  U   n[        S[	        U5      5       H  nXW   nUR                  U5        M     UR                  U   n	U	R                  U5        UR                  XcS-   5        g  N7f)Nr!  r   
:positions)fetch_positionsr   r  ro   rp   r   r+  r   )
rJ   rO   r   r!  r   r  r  ru   positionr  s
             rL   r  toobit.load_positions_snapshot  s     D
 ..tV<<	79tt$q#i.)A |HLL" * ,uu\12 =s   B/B-BB/c                 >   [        UR                  R                  5       5      nUS   nU R                  c  0 U l        X@R                  ;  a  [	        5       U R                  U'   U R                  U   n/ n[        S[        U5      5       Hd  nX'   nU R                  U5      n	U R                  US5      n
XS'   U R                  U
5      U	S'   UR                  U	5        UR                  U	5        Mf     U R                  XS-   5      n[        S[        U5      5       Hl  nX   nUR                  S5      nUS   nUR                  S5      nU R                  US	US
5      nU R                  U5      (       a  MZ  UR                  UU5        Mn     UR                  XdS-   5        g )Nr   r1  r   r3  z:positions::r   r   r   r   Fr  )listr*  keysr  r   ro   rp   parse_ws_positionrc   r9  r   find_message_hashessplitr   r  r   )rJ   rO   rq   r*  accountTyper  r  ru   rawPositionr  r   r   r   partssymbolsStringr   r  s                    rL   rm   toobit.handle_positions  s}   2 V116689#A&>>!DN~~-*B*DDNN;'{+q#g,'A!*K--k:H))+s;I$-[!#'<<	#:HZ )LL" ( 00~9UVq#m,-A'*K%%d+E!!HM#))#.G,,\8WeTI==++y+6 . 	|<%?@rN   c                 ~   U R                  US5      nU R                  0 SU_SS _SU R                  US 5      _SU R                  U R                  US5      5      _SU R	                  US5      _S	U R                  US
5      _SU R                  US5      _SU R                  US5      _SU R                  US5      _SS _SS _SS _SU R                  US5      _SU R	                  US5      _SS _SS _SS _U R                  US5      S S U R                  U R                  US5      5      S U R                  US5      S S.E5      $ )Nr  r2  r   r   notionalpv
marginModemtliquidationPricer   
entryPricerf  unrealizedPnluprealizedPnlr
percentage	contractscontractSize	markPricempr]  r^  hedgedr   r3  mmr  v)maintenanceMarginmaintenanceMarginPercentage
collateralinitialMargininitialMarginPercentageleveragemarginRatio)ra   safe_positionr  	omit_zerorm  rn  )rJ   r  r   r   s       rL   r  toobit.parse_ws_position  s   ##Hc2!! #
H#
$#
 d&&x6#
 t'7'7$'GH	#

 $004@#
  0 03 ?#
 $**8S9#
 T--h=#
 4++Hc:#
 $#
 #
 D#
 ))(D9#
 D**8S9#
 d#
  !#
" ##
$ "&!1!1(D!A+/!^^D,<,<Xs,KL'+((371#
  	rN   c                   #    U R                  U R                  5       5      nSnUR                  U5      nU R                  UR                  U5      nUGc  U R                  5         U R                  5       nU R                  U R                  S   SS5      nU R                  U R                  S   SS5      nU R                  US5      n	Xg-
  U	:  a   SUR                  U'   U R                  U5      I S h  vN n
U R                  U
S	5      U R                  S   S	'   X`R                  S   S'   UR                  S5        U R                  XR                  U5        UI S h  vN $  Nn! [         aa  n[!        U R"                  S
-   U R%                  U5      -   5      nUR'                  X5        X2R                  ;   a  UR                  U	  S nANrS nAff = f Nt7f)Nauthenticatedr   lastAuthenticatedTimer   r>   r<   i'  T	listenKey )rO   r  reusableFuturern   r*  check_required_credentialsrQ   rc   rD   sumprivatePostApiV1UserDataStreamra   r   delaykeep_alive_listen_key	Exceptionr   r   jsonreject)rJ   r   rO   r   r  r  timer  r>   r  rB  r`   errs                rL   r  toobit.authenticate  s    T5578%&&{3(<(<kJ ++-$$&D$($5$5dll46HJacd$e!#'#4#4T\\$5GI_ah#i HH159E+e3>8<F((5%)%H%H%PPH6:6F6FxQ\6]DLL&{3BFLL&'>?NN4(JJ35O5OQWX |  Q
 ! >-dggmdiil.JKCMM#3"&:&::"00=	>
 sP   CG#E$ 3E"4A$E$ GGG"E$ $
G.AG
G
GGc                    #    U R                  U R                  S0 5      nU R                  US5      nUc  g  U R                  U5      I S h  vN nU R                  US5      U R                  S   S'   U R	                  5       U R                  S   S'   U R                  U R                  SS5      nU R                  XR                   U5        g  N! [
         a  nU R                  5       nU R                  U5      n[        UR                  R                  5       5      n[        S[        U5      5       H  n	X   n
UR                  XZ5        M     S U R                  S   S'   SU R                  S   S'    S nAg S nAff = f7f)Nr   r  r  r   r>   r<   )rn   rD   ra   r  rQ   r  r  rO   r  r+  r  ro   rp   r  rc   r  r  )rJ   r   rD   r  rB  errorr   rO   r   ru   r   r>   s               rL   r  toobit.keep_alive_listen_key,  s]    //$,,b9$$Wk:		!@@HHH.2.>.>x.UDLL{+:>:K:K:MDLL67  $00?UW^_

')C)CVL I  		**,C[[%F !4!4!67M1c-01+.e1 2 /3DLL{+:;DLL67		sB   4E>C CAC :E>C 
E;BE61E>6E;;E>c                 X    U R                   S   S   S   S-   U R                  S   S   -   $ )Nr*   r   r+   z/api/v1/ws/r  )rC   rD   )rJ   s    rL   r  toobit.get_user_stream_urlD  s4    yy%h/-?$,,tBTU`BaaarN   c                     U R                  US5      nUbG  U R                  US5      nU R                  S-   U-   S-   U-   n[        U5      nUR                  U5        gg)Nr>  descz code: z
 message: TF)ra   r   r   r  )rJ   rO   rq   r>  r  msg	exceptions          rL   rb   toobit.handle_error_messageG  sg     0##GV4D''I%,|;dBC%c*IMM)$rN   )rE  r  r|   )NF)A__name__
__module____qualname____firstlineno__r   rI   r   r@   ry   r
   rd   strr   r   r   r   re   dictr   r   r  r   r   rf   r   r   r   r   r   r   rg   r   r   r   r   ri   r	  rh   r   r   r'  r   r  rj   r-  r   rI  rk   rM  rz  rl   r}  r   r  r  r  rm   r  r  r  r  r	   rb   __static_attributes____classcell__)rK   s   @rL   r   r      s   :# :x
6 

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