
    eix                        d dl Zd dlmZmZmZmZ d dl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 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" 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)ArgumentsRequired)
BadRequest)NotSupported)RateLimitExceeded)ChecksumError)UnsubscribeError)Precisec                       e Zd Zdef fdZd Zi fdZi fdZi fdZi fdZ	i fdZ
d	efd
Zi fdZi fdedededefdZdefdZdDdeded	e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	efdZdDdZdddi fdededededee         f
dZdi fdededefd Z d	efd!Z!dDde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&d	efd&Z'd	eded'efd(Z(d) Z)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-i fdedefd-Z.d	efd.Z/dDde+fd/Z0i fde1fd0Z2d	efd1Z3dddi fdedededee4         fd2Z5d	efd3Z6dDd4e7dede4fd5Z8dddi fdedededee9         fd6Z:defd7Z;d	efd8Z<dDd9Z=d	ede>fd:Z?d	efd;Z@d	efd<ZAd	efd=ZBd	efd>ZCd	efd?ZDd	efd@ZEd	efdAZFd	efdBZGd	efdCZH xZIS )E	coincatchreturnc                 |   |                      t          t          |                                           dddddddddddddddddddiid	d
dddddddddd	ddidd| j        idt
          t          t
          t          t          t          t          t          t          t          t
          t          di did          S )NTF)wswatchTradeswatchTradesForSymbolswatchOrderBookwatchOrderBookForSymbols
watchOHLCVwatchOHLCVForSymbolswatchOrderswatchMyTradeswatchTickerwatchTickerswatchBalancewatchPositionsapir#   z'wss://ws.coincatch.com/public/v1/streamz(wss://ws.coincatch.com/private/v1/stream)publicprivate     1m5m15m30m1H4H12H1D1W)	r5   r6   r7   r8   1h4h12h1d1wchecksum)tradesLimit
OHLCVLimittimeframesForWsr&   ping)300013000230003300043000530006300073001130012300133001430015)exactbroad)hasurlsoptions	streaming
exceptions)deep_extendsuperr    describerG   r   r   r   )self	__class__s    P/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/coincatch.pyr]   zcoincatch.describe   s0   i 6 6 ? ? A A#)-"&,0"(-#!&# $ $"&   "K#M   $!   
$ 
$ # & 	 !+!4!+!4!4!2!2!4!4!4!+!4    [@D
 @D
 @ @ @	    c                 P   |                      |d          }|                      |d          }|                     |          }|d         }|d         }d}|dk    r|dk    rd}nd	}|                     |          |                     |          z   |z   }|                     |          S )
NinstIdinstTypebaseIdquoteId_SPBLmcUSD_DMCBL_UMCBL)safe_stringparseSpotMarketIdsafe_currency_codesafeMarketCustom)	r^   entryrc   rd   baseAndQuotere   rf   suffixmarketIds	            r`   get_market_from_argzcoincatch.get_market_from_arg\   s    !!%22##E:66--f55h'y)t%!!**622T5L5LW5U5UUX^^$$X...ra   c                 x  K   |                                   | j        d         d         d         }|                     |          }d}|                    |          }|                     |j        |          }|t          |                                           }|dz   dz   }|                     | 	                    |          | 	                    | j
                  t          j        d          }	d}
|
| j        | j        ||	d	gd
}|                     ||          }|                     ||||           | d {V S )Nr0   r#   r2   authenticatedGETz/user/verifybase64login)apiKey
passphrase	timestampsignopargs)check_required_credentialsrW   clientreusableFuture
safe_valuesubscriptionsstrsecondshmacencodesecrethashlibsha256rz   passwordextendwatch)r^   paramsurlr   messageHashfuturerv   r|   auth	signature	operationrequestmessages                r`   authenticatezcoincatch.authenticatek   s?     '')))it$Y/S!!%&&{33(<kJJ DLLNN++Iu$~5D		$++d"3"3T[[5M5Mw~_ghhII #'+&*m%. )	 
 
G kk'622GJJsK+>>>||||||ra   c                    K   | j         d         d         d         }d|gd}|                     ||          }|                     ||||           d {V S Nr0   r#   r1   	subscriber~   rW   r   r   r^   r   subscribeHashr   r   r   r   r   s           r`   watch_publiczcoincatch.watch_public   so      it$X.F
 
 ++gv..ZZ['=IIIIIIIIIra   c                    K   | j         d         d         d         }d|gd}|                     ||          }|                     ||||           d {V S )Nr0   r#   r1   unsubscriber~   r   )r^   r   r   r   r   r   r   s          r`   un_watch_publiczcoincatch.un_watch_public   so      it$X.F
 
 ++gv..ZZ[';GGGGGGGGGra   c                    K   |                                   d {V  | j        d         d         d         }d|gd}|                     ||          }|                     ||||           d {V S Nr0   r#   r2   r   r~   )r   rW   r   r   r   s           r`   watch_privatezcoincatch.watch_private   s      !!!!!!!!!it$Y/F
 
 ++gv..ZZ['=IIIIIIIIIra   c                    K   |                                   d {V  | j        d         d         d         }d|d}|                     ||          }|                     ||||           d {V S r   )r   rW   r   watch_multiple)r^   messageHashessubscribeHashesr   r   r   r   r   s           r`   watch_private_multiplez coincatch.watch_private_multiple   s      !!!!!!!!!it$Y/
 
 ++gv..((mWoVVVVVVVVVra   r   c                 j    d}|                      |j        |          }|                    d           d S )Nrv   T)r   futuresresolve)r^   r   r   r   r   s        r`   handle_authenticatezcoincatch.handle_authenticate   s6     &==tra   c                    K   | j         d         d         d         }d|d}|                     ||          }|                     ||||           d {V S r   )rW   r   r   )r^   r   r   	argsArrayr   r   r   r   s           r`   watch_public_multiplezcoincatch.watch_public_multiple   so      it$X.
 
 ++gv..((mWoVVVVVVVVVra   symbolchannelmessageHashTopicc                    K   |                                   d {V  |                     |          }|                     |          \  }}d|z   dz   |z   }|||d}	|                     ||	|           d {V S )Nzunsubscribe::rd   r   rc   )load_marketsmarketget_public_inst_type_and_idr   )
r^   r   r   r   r   r   rd   rc   r   r   s
             r`   un_watch_channelzcoincatch.un_watch_channel   s      !!!!!!!!!V$$;;FCC&$'77#=F 
 

 ))+tVDDDDDDDDDra   r   c                     |d         |d         z   }d }|d         rd}n"|d         rd}nt          | j        dz             ||gS )Nre   rf   spotSPswapMCz$ supports only spot and swap markets)r   id)r^   r   rc   rd   s       r`   r   z%coincatch.get_public_inst_type_and_id   sd    !F9$55&> 	QHHF^ 	QHHtw)OOPPP&!!ra   Nhash	timeframec                 0   |d         }|                      ||          }|                     | j        |g           }d}|d|z   }t          dt	          |                    D ]4}	||	         }|d         }
||
z   |z   }|                     ||          r|c S 5|S )Nr    r   r   r   )find_message_hashes	safe_listmarkets_by_idrangelenin_array)r^   r   r   r   r   rs   r   marketsWithCurrentIdrr   ir   r   s               r`   %handle_dmcbl_market_by_message_hashesz/coincatch.handle_dmcbl_market_by_message_hashes   s    $<00>>  $~~d.@(BOO 9_Fq#23344 	 	A)!,FH%F-&0K}}[-88 ra   c                    K   |                                   d{V  |                     |          }|                     |          \  }}d}|dz   |z   }|||d}|                     ||||           d{V S )ar  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://coincatch.github.io/github.io/en/spot/#tickers-channel

        :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.instType]: the type of the instrument to fetch the ticker for, 'SP' for spot markets, 'MC' for futures markets(default is 'SP')
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        Ntickerr   r   )r   r   r   r   )	r^   r   r   r   rd   rc   r   r   r   s	            r`   watch_tickerzcoincatch.watch_ticker   s       !!!!!!!!!V$$;;FCC&mf, 
 

 &&{KvNNNNNNNNNra   c                 v   K   |                                   d{V  |                     |dd|           d{V S )aX  
        unsubscribe from the ticker channel

        https://coincatch.github.io/github.io/en/mix/#tickers-channel

        :param str symbol: unified symbol of the market to unwatch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns any: status of the unwatch request
        Nr   r   r   r^   r   r   s      r`   un_watch_tickerzcoincatch.un_watch_ticker   sX       !!!!!!!!!**68XvNNNNNNNNNra   symbolsc                   K   |                                   d{V  || j        }g }g }t          dt          |                    D ]j}||         }|                     |          }|                     |          \  }}	|d|	d}
|                    |
           |                    d|z              k|                     ||||           d{V }| j        ri }|||d         <   |S | 	                    | j
        d|          S )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://coincatch.github.io/github.io/en/mix/#tickers-channel

        :param str[] symbols: unified symbol of the market to watch the tickers for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        Nr   r   r   ticker:r   )r   r   r   r   r   r   appendr   
newUpdatesfilter_by_arraytickers)r^   r   r   topicsr   r   r   r   rd   rc   r   r   results                r`   watch_tickerszcoincatch.watch_tickers
  sB      !!!!!!!!!?lGq#g,,'' 
	5 
	5AQZF[[((F#??GGHf$#  D
 MM$  V!3444422=-QWY_````````? 	F(/F78$%M##DL(GDDDra   c                    |                      |di           }|                     |          }|d         }d}|                    d          dk    r|                     |||          }|                     |dg           }|                     |                      |di           |          }|d         }	|| j        |	<   ||	z   }
|                    | j        |	         |
           d S )Nargr   r   rj   r   datar   )	safe_dictrt   findr   r   parse_ws_tickerr   r   )r^   r   r   r   r   rs   r   r   r   r   r   s              r`   handle_tickerzcoincatch.handle_ticker+  s    2 nnWeR00))#..$<==""a''??fUUF~~gvr22%%dnnT1b&A&A6JJ!%VVmt|F+[99999ra   c           
      8   |                      |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t          j        |                      |d          d          dd |                     |d          |                     |d          |                      |d           |                      |d!          |d"|          S )#Nlastts
systemTimer   r|   datetimehighhigh24hlowlow24hbidbestBid	bidVolumebidSzaskbestAsk	askVolumeaskSzvwapopenopen24hopenUtcclosepreviousClosechange
percentagechgUTC100average
baseVolumequoteVolume
indexPrice	markPrice)r  r  r  r  info)rl   safe_integer_2safe_tickeriso8601safe_string_2r   
string_mulsafe_number)r^   r   r   r   r|   s        r`   r   zcoincatch.parse_ws_tickerQ  s   ^ //''lCC	 !
fX&!
!
 Y//!
 D$$VY77	!

 4##FH55!
 4##FI66!
 ))&'::!
 4##FI66!
 ))&'::!
 D!
 D&&vy)DD!
 T!
 D!
 T!
 d!
  ',T-=-=fh-O-OQVWW!!
" t#!
$ **6<@@++FMBB**6<@@))&+>>-!
 !
 !
. /  	ra   r5   sincelimitc                   K   |                                   d{V  |                     |          }| j        d         }d|                     ||          z   }|                     |          \  }	}
|	||
d}d|z   dz   |z   }|                     ||||           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://coincatch.github.io/github.io/en/spot/#candlesticks-channel

        :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(not including)
        :param int [limit]: the maximum amount of candles to fetch(not including)
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param bool [params.instType]: the type of the instrument to fetch the OHLCV data for, 'SP' for spot markets, 'MC' for futures markets(default is 'SP')
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        NrF   candler   ohlcv:r   r   T)	r   r   rX   rl   r   r   r   getLimitfilter_by_since_limit)r^   r   r   r  r  r   r   
timeframesr   rd   rc   r   r   ohlcvs                 r`   watch_ohlcvzcoincatch.watch_ohlcv  s      !!!!!!!!!V$$\"34
T--j)DDD;;FCC& 
 

 '#-	9''[$OOOOOOOO? 	2NN6511E))%q$GGGra   c                    K   |                                   d{V  | j        d         }|                     ||          }d|z   }|                     ||d|z   |           d{V S )a  
        unsubscribe from the ohlcv channel

        https://www.bitget.com/api-doc/spot/websocket/public/Candlesticks-Channel

        :param str symbol: unified symbol of the market to unwatch the ohlcv for
 @param timeframe
        :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
        NrF   r  r  )r   rX   rl   r   )r^   r   r   r   r  intervalr   s          r`   un_watch_ohlcvzcoincatch.un_watch_ohlcv  s       !!!!!!!!!\"34
##J	::X%**67Hx<OQWXXXXXXXXXra   c                 ^   |                      |di           }|                     |          }|d         }d}|                     |dg           }|                     |d          }|dd          }	|                     |	          }
|                    d          dk    r|                     ||||
          }|d	         }|| j        vr
i | j        |<   |
| j        |         vr9|                     | j	        d
d          }t          |          | j        |         |
<   | j        |         |
         }t          dt          |                    D ]D}|                     ||g           }|                     ||          }|                    |           E||z   dz   |
z   }|                    ||           d S )Nr   r   r  r   r      rj   r   r   rE   r3   r   )r   rt   r   rl   find_timeframer   r   ohlcvssafe_integerrX   r   r   r   parse_ws_ohlcvr   r   )r^   r   r   r   r   rs   r   r   r   	klineTyper   r   r  storedr   r  parsedr   s                     r`   handle_ohlcvzcoincatch.handle_ohlcv  s   $ nnWeR00))#..$<~~gvr22""3	22ABBK	''	22	==""a''??fV_``F!$+%%"$DKT[000%%dlL$GGE-B5-I-IDK	*V$Y/q#d))$$ 	" 	"A^^D!R00F((88FMM&!!!!Vmc)I5v{+++++ra   c           	         |                      |d          |                     |d          |                     |d          |                     |d          |                     |d          |                     |d          gS )Nr                  )r   r  )r^   r  r   s      r`   r!  zcoincatch.parse_ws_ohlcv  s     eQ''UA&&UA&&UA&&UA&&UA&&
 	
ra   c                 B   K   |                      |g||           d{V S )a8  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://coincatch.github.io/github.io/en/spot/#depth-channel

        :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)r^   r   r  r   s       r`   watch_order_bookzcoincatch.watch_order_book  s4       66xOOOOOOOOOra   c                   K   |                                   d{V  d}|                     |d          }|dk    s|dk    r(|                     |d          }|t          |          z  }|                     ||||           d{V S )a  
        unsubscribe from the orderbook channel

        https://coincatch.github.io/github.io/en/spot/#depth-channel

        :param str symbol: unified symbol of the market to fetch the order book for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param int [params.limit]: orderbook limit, default is None
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/?id=order-book-structure>` indexed by market symbols
        Nbooksr  r+     )r   r   omitr   r   )r^   r   r   r   r  s        r`   un_watch_order_bookzcoincatch.un_watch_order_book  s       !!!!!!!!!!!&'22QJJERKKYYvw//Fs5zz!G**67GVLLLLLLLLLra   c                   K   |                                   d{V  |                     |          }d}g }g }t          dt          |                    D ]m}||         }|                     |          }	|                     |	          \  }
}|
||d}|                    |           |                    |dz   |z              n|                     ||||           d{V }|                                S )a  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://coincatch.github.io/github.io/en/spot/#depth-channel

 @param 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
        Nr0  r   r   r   )	r   market_symbolsr   r   r   r   r   r   r  )r^   r   r  r   r   r   r   r   r   r   rd   rc   r   	orderbooks                 r`   r-  z&coincatch.watch_order_book_for_symbols(  s%      !!!!!!!!!%%g..q#g,,'' 
	9 
	9AQZF[[((F#??GGHf$"  D
 MM$  3!7888844]MSY[abbbbbbbb	   ra   c                    |                      |di           }|                     |          }|d         }d}|                    d          dk    r|                     |||          }|d         }|                     |d          }||z   }	|                     |dg           }
|                      |
d          }|                     |d	          }|}|rK|| j        vr$|                     i           }||d<   || j        |<   | j        |         }|                     |d
g           }|                     |dg           }| 	                    |d
         |           | 	                    |d         |           ||d<   | 
                    |          |d<   |                     | j        dd          }|                     |d          dk    }|sT|rQ|d
         }|d         }t          |          }t          |          }g }t          dd          D ]}||k     rN|                    ||         d         d                    |                    ||         d         d                    ||k     rN|                    ||         d         d                    |                    ||         d         d                    d                    |          }|                     |d          }|                     |d          }||k    r|                     | j        |||	           d S nK|                     i           }|                     |||          }|                    |           || j        |<   |                    | j        |         |	           d S )Nr   r   zbooks:rj   r   r   r   r   r   asksbidsr|   r   rC   Tactionsnapshot   r(  r'  r   )r   rt   r   r   rl   r   r   
orderbookscounted_order_bookhandle_deltasr  	safe_boolrX   r   r   r   joincrc32spawnhandle_check_sum_error
order_bookparse_order_bookresetr   )r^   r   r   r   r   rs   r   r   r   r   r   rawOrderBookr|   incrementalBookobstoredOrderBookr8  r9  rC   
isSnapshot
storedAsks
storedBids
asksLength
bidsLengthpayloadArrayr   payloadcalculatedChecksumresponseChecksumr6  parsedOrderbooks                                  r`   handle_order_bookzcoincatch.handle_order_bookF  s     nnWeR00))#..$<==""a''??fUUF!""3	22Vm~~gvr22~~dA..%%lD99	! %	0do--,,R00%8*,'"of5O>>,;;D>>,;;Dv6===v6===+4OK(*.,,y*A*AOJ'~~dlJEEH))'8<<
JJ ( ,V4
,V4
 __
 __
!q" A AA:~~$++JqM!,<Q,?@@@$++JqM!,<Q,?@@@:~~$++JqM!,<Q,?@@@$++JqM!,<Q,?@@@((<00%)ZZ%>%>"#'#4#4\:#N#N %)999JJt:FFKXXXF++I"33L&)TTOOOO,,,&/DOF#tv.<<<<<ra   r   c                    K   |                      |           d {V  t          | j        dz   |                     |          z             }|                    ||           d S )N )r3  r   r   orderbook_checksum_messagereject)r^   r   r   r   errors        r`   rD  z coincatch.handle_check_sum_error  sl      &&v.........dgmd.M.Mf.U.UUVVe[)))))ra   c                     |                      |dd          }|                    |           |                    |           d S )Nr   r'  )parse_bid_askr   
storeArray)r^   booksidedeltabidAsks       r`   handle_deltazcoincatch.handle_delta  sE    ##E1a00eF#####ra   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )r   r   rb  )r^   r_  deltasr   s       r`   r?  zcoincatch.handle_deltas  sH    q#f++&& 	3 	3Ahq	2222	3 	3ra   c                 D   K   |                      |g|||           d{V S )a"  
        get the list of most recent trades for a particular symbol

        https://coincatch.github.io/github.io/en/spot/#trades-channel

        :param str symbol: unified symbol of the market to fetch trades for
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=public-trades>`
        N)watch_trades_for_symbols)r^   r   r  r  r   s        r`   watch_tradeszcoincatch.watch_trades  s6       22F8UE6RRRRRRRRRra   c                   K   t          |          }|dk    rt          | j        dz             |                                  d{V  |                     |          }g }g }t          dt          |                    D ]j}||         }	|                     |	          }
|                     |
          \  }}|d|d}|                    |           |                    d|	z              k| 	                    ||||           d{V }| j
        rB|                     |d          }|                     |d          }|                    ||          }|                     |||dd	          S )
a  
        watches information on multiple trades made in a market

        https://coincatch.github.io/github.io/en/spot/#trades-channel

 @param symbols
        :param int [since]: the earliest time in ms to fetch orders 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>`
        r   z> watchTradesForSymbols() requires a non-empty array of symbolsNtrader   trade:r   r|   T)r   r   r   r   r5  r   r   r   r   r   r   r   rl   r  r  )r^   r   r  r  r   symbolsLengthr   r   r   r   r   rd   rc   r   tradesfirsttradeSymbols                    r`   rf  z"coincatch.watch_trades_for_symbols  s      GA#DG.n$nooo!!!!!!!!!%%g..q#g,,'' 
	4 
	4AQZF[[((F#??GGHf$"  D
 MM$  F!2333311-PVX^________? 	8NN61--E**5(;;KOOK77E))&%TRRRra   c                 v   K   |                                   d{V  |                     |dd|           d{V S )aX  
        unsubscribe from the trades channel

        https://coincatch.github.io/github.io/en/spot/#trades-channel

        :param str symbol: unified symbol of the market to unwatch the trades for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns any: status of the unwatch request
        Nri  r   r   s      r`   un_watch_tradeszcoincatch.un_watch_trades  sX       !!!!!!!!!**67GVLLLLLLLLLra   c                    |                      |di           }|                     |          }|d         }d}|                    d          dk    r|                     |||          }|d         }|| j        vr3|                     | j        dd          }t          |          | j        |<   | j        |         }	|                     |d	g           }
|
w| 	                    |
d          }
t          dt          |
                    D ]C}|                     |
|          }|                     ||          }|	                    |           D||z   }|                    |	|           d S )
Nr   r   rj  rj   r   r   rD   r3   r   )r   rt   r   r   rl  r   rX   r   r   sort_byr   r   parse_ws_trader   r   )r^   r   r   r   r   rs   r   r   r  r#  r   r   ri  r$  r   s                  r`   handle_tradeszcoincatch.handle_trades  se    nnWeR00))#..$<==""a''??fUUF!$+%%%%dlM4HHE",U"3"3DKV$~~gvr22<<a((D1c$ii(( & &tQ//,,UF;;f%%%%Vmv{+++++ra   c                     |                      |d          }|                     d ||                     |          |d         |                     |d          |                     |d          |                     |d          d d d d d |d|          S )Nr   r   r)  r'  r(  )r   r|   r   r   sidepriceamountcosttakerOrMakertypeorderfeer  )r   
safe_trader  safe_string_lowerrl   )r^   ri  r   r|   s       r`   rs  zcoincatch.parse_ws_trade  s     %%eQ//	"Y//X&**5!44%%eQ//&&ua00  
  
   	ra   c                   K   d}|                      dd|          \  }}d}|dk    rd}d}|                     |dd|          \  }}||dd	}d
|                                z   }|                     ||||           d{V S )aI  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://coincatch.github.io/github.io/en/spot/#account-channel
        https://coincatch.github.io/github.io/en/mix/#account-channel

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.type]: 'spot' or 'swap'(default is 'spot')
        :param str [params.instType]: *swap only* 'umcbl' or 'dmcbl'(default is 'umcbl')
        :returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
        Nr.   spblr   umcblaccountrd   defaultr   balance:)handle_market_type_and_paramshandle_option_and_paramslowerr   )r^   r   r{  rd   r   r   r   s          r`   watch_balancezcoincatch.watch_balance  s       99.$PVWWf6>>H88Q[]eff& 
 

 !8>>#3#33''[$OOOOOOOOOra   c                    |                      |dg           }t          dt          |                    D ]}||         }|                     |dd          }|                     |          }|| j        v r| j        |         n|                                 }d|v rdnd}	|                     ||	          |d<   |                     |d          |d	<   |                     |d
          |d<   || j        |<   |                     | j                  | _        | 	                    |d          }
| 
                    |
d          }d|z   }|                    | j        |           d S )Nr   r   coinName
marginCoinmaxTransferOut	availablefreeequitytotalfrozenusedr   rd   r  )r   r   r   r  rn   balancer  rl   safe_balancer   r  r   )r^   r   r   r   r   
rawBalance
currencyIdcoder  	freeQueryr   rd   r   s                r`   handle_balancezcoincatch.handle_balance,  sg   J ~~gvr22q#d))$$ 		) 		)AaJ++J
LQQJ**:66D-1T\-A-Adl4((G-=-K-K((R]I"..z9EEGFO#//
HEEGG"..z8DDGFO!(DL((66nnWe,,))#z:: 8+t|[11111ra   c                   K   d}|                                   d{V  d}d}|%|                     |          }|d         }|d         }d}|                     |||          \  }}d}	|}
|dk    r&|#t          | j        dz   |z   dz   |z   d	z             n=d
}
d}	||                     ||d|	          \  }	}n|                    d          dk    rd}	d}|                     |d          }|rd}|                     |d          }|	||
d}d}||d|z   z  }| 	                    ||||           d{V }| j
        r|                    ||          }|                     ||||d          S )a  
        watches information on multiple orders made by the user

        https://coincatch.github.io/github.io/en/spot/#order-channel
        https://coincatch.github.io/github.io/en/mix/#order-channel
        https://coincatch.github.io/github.io/en/mix/#plan-order-channel

        :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
        :param str [params.type]: 'spot' or 'swap'
        :param str [params.instType]: *swap only* 'umcbl' or 'dmcbl'(default is 'umcbl')
        :param bool [params.trigger]: *swap only* whether to watch trigger orders(default is False)
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        r*   Nr   r   r  r   rX  z"() requires a symbol argument for z	 markets.r  r  rd   rj   r   dmcblorderstrigger
ordersAlgor   r   T)r   r   r  r   r   r  r   r@  r2  r   r   r  filter_by_symbol_since_limit)r^   r   r  r  r   
methodNamer   rs   
marketTyperd   rc   r   	isTriggerr   r   r  s                   r`   watch_orderszcoincatch.watch_ordersb  s     " #
!!!!!!!!![[((FH%Fd|H
!??
FTZ[[
F~'#
(BEi(ilv(v  zE  )E  F  F  F  FH~#'#@#@U_ai#j#j &&==**a//&HNN6955	 	2"GYYvy11F 
 

 3<'K))+{D&QQQQQQQQ? 	3OOFE22E00tTTTra   c                    |                      |di           }|                     |d          }|                     |d          }d }|dk    rd}nd}|                     |dg           }| j        0|                     | j        dd	          }t          |          | _        d
}	| j        }
d }t          dt          |                    D ]}||         }|                     |d|          }| 	                    |d d |          }| 
                    ||          }|
                    |           |d         }d|z   }|                    |
|           |                    |
|	           d S )Nr   rd   rc   r  r   r   r   ordersLimitr3   r  r   r   zorders:)r   rl   r   r  r   rX   r   r   r   safe_marketparse_ws_orderr   r   )r^   r   r   r   rd   	argInstIdr  r   r  r   r#  r   r   r|  rs   r   r$  r   s                     r`   handle_orderzcoincatch.handle_order  sz   B nnWeR00##C44$$S(33	
vJJJ~~gvr22;%%dlM4HHE077DKq#d))$$ 	0 	0AGE''xCCH%%hdJGGF((77FMM&!!!H%F#f,KNN6;////vt$$$$$ra   r|  c                 P   |                      |d          }|                      |d          }|                     |||          }|                     |d          }|d         }|                      |d          }|                     |dg           }|                     |d          }	t          j        |                      |	d          d	          }
|                      |	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 |d&|          S )'Nrc   tgtCcycTimer   statusorderFeer   r}  z-1feeCcypxavgPxordTyper   ordIdclientOrderIdclOrdIdr|   r   lastTradeTimestamplastUpdateTimestampuTimer{  timeInForceforcerv  rw  r  rx  szfilled	accFillSz	remainingtriggerPricenotional)currencyry  r   )takeProfitPricestopLossPricery  rl  r}  
reduceOnlypostOnlyr  )rl   ro   r   r   r   r   r  	omit_zeror  
safe_orderr  parse_order_statusparseOrderTimeInForcer@  )r^   r|  r   rs   settleIdr|   r   	rawStatusr  r}  feeCostfeeCurrencyrw  priceAvgr{  s                  r`   r  zcoincatch.parse_ws_order  s!   ` ##E844##E844&&xBB%%eW55	!$$UH55	>>%R88nnXq))$T%5%5c5%A%A4HH&&sH55t//t<<==>>$"2"25'"B"BCC=E%%eY77  
$""5'22 
T--eY?? 
  
 Y//	 

 !$ 
 "4#4#4UG#D#D 
 d--i88 
 f 
 D 
 455d6L6LUT[6\6\]] 
 D**5&99 
 U 
 t''w77 
 d&&ud33 
 d&&uk:: 
  ! 
" D# 
$  $!$$UJ77'  ..669 
  
  
: ;  	ra   c                   K   |                                   d{V  |                     |d          }g }d}g }|t          dt          |                    D ]z}||         }	|                     |	          }
|                     |
          }|                     ||          s|                    |           |                    |dz   |	z              {nddg}|                    |           g }g }t          dt          |                    D ]@}||         }||dd	}|                    |dz   |z              |                    |           A|                     ||||           d{V }| j	        r|S | 
                    ||||d
          S )a  
        watch all open positions

        https://coincatch.github.io/github.io/en/mix/#positions-channel

        :param str[]|None symbols: list of unified market symbols
 @param since
 @param 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   	positionsr   ::r  r  r  r   T)r   r5  r   r   r   get_private_inst_typer   r   r   r   filter_by_symbols_since_limit)r^   r   r  r  r   r   r   	instTypesr   r   r   rd   r   r   r   newPositionss                   r`   watch_positionszcoincatch.watch_positionsU  s      !!!!!!!!!%%gv66	1c'll++ ; ; V,,55f==}}Xy99 /$$X...$$TD[6%9::::; !'*I  &&&q#i..)) 	 	A |H$# C
 ""4$;#9:::KK!88Y]_effffffff? 	 11,PUW[\\\ra   c                 L    |d         }|                     d          dk    rdS dS )Nr   rj   r   r  r  )r   )r^   r   rs   s      r`   r  zcoincatch.get_private_inst_type  s,    $<==""a''7wra   c                    | j         t                      | _         | j         }|                     |dg           }t          |          }|dk    rd S g }g }t	          dt          |                    D ]d}||         }	|                     |	          }
|                    |
d                    |                    |
           |                    |
           ed}|                     ||          }t	          dt          |                    D ]}||         }|                    d          }|d         }| 	                    ||          rag }t	          dt          |                    D ]+}||         }
|
d         |k    r|                    |
           ,|
                    ||           |
                    ||           d S )Nr   r   r   r  r  r'  )r  r   r   r   r   parse_ws_positionr   r   splitr   r   )r^   r   r   cacherawPositions
dataLengthr  r   r   rawPositionpositionr   r   r   partsr   positionsForSymboljs                     r`   handle_positionszcoincatch.handle_positions  s   F >!577DNw;;&&
??Fq#l++,, 	# 	#A&q/K--k::HNN8H-...)))LL""""00>>q#m,,-- 
	@ 
	@A'*K%%d++E1XF}}VW-- @%'"q#l"3"344 < <A+AH)V33*11(;;;1;???|T*****ra   c                 |   |                      |d          }|                      |d          }|                     |||          }|                     |d          }|                      |d          }|                     |ddd          }d }|                      |d          }	|	d	k    rd
}n|	dk    rd}|                      |d          }
t	          j        |
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 d d ||d(          S ))Nr   r  r  
marginModecrossisolated)crossedr  holdModedouble_holdTsingle_holdFuplRater  marginr   r|   r   	contractsr  contractSizerv  holdSider  leverageunrealizedPnluplrealizedPnlachievedProfits
collateral
entryPriceaverageOpenPricer  liquidationPriceliqPxhedgedkeepMarginRate
marginRater  )maintenanceMarginmaintenanceMarginPercentageinitialMargininitialMarginPercentagemarginRatior  	lastPricer  r  r   r  )
rl   ro   r   get_supported_mappingr   r  r  safe_positionr  r  )r^   r  r   rs   r  r|   marginModeIdr  isHedgedr  percentageDecimalr   r  s                r`   r  zcoincatch.parse_ws_position  s   8 ##Hh77##Hl;;&&xBB%%h88	'',??//"?
 ?
  
 ##Hj99}$$HH&&H ,,XyAA'(95AA
!!(H55!! #
fX&#
$#
 #
 Y//	#

 ))(G<<#
 D#
 D**8Z@@#
 #
 ))(J??#
 T--h>>#
 4++H6GHH#
 $#
 $**85GHH#
 ))(K@@#
  0 07 C C#
  *!#
" h##
$ "&+/+;+;HFV+W+W#'+++HlCC#'#4#4Xw#G#G!#$9#
 #
 #
   	ra   c                 p   |                      |d          }	 |dk    r|                      |d          }| j        dz   |                     |          z   }|                     | j        d         d         ||           |                      |dd          }|                     | j        d         d	         ||           t          |          d
S # t          $ r_}t          |t                    r*d}|
                    ||           ||j        v r|j        |= n|
                    |           Y d }~dS d }~ww xY w)Neventr[  r  rX  r#   rT   msgr   rU   Frv   T)rl   r   jsonthrow_exactly_matched_exceptionrZ   throw_broadly_matched_exceptionr   	Exception
isinstancer   rZ  r   )	r^   r   r   r  r  feedbackr  er   s	            r`   handle_error_messagezcoincatch.handle_error_message  sT      '22	''887S=499W+=+==44T_T5J75SUY[cddd&&wr::44T_T5J75SUXZbccc#H---5 	 	 	!011 !-a---&"666,[9a   44444	s   B2C 
D5AD00D5c                    |                      ||          rd S |                     |d          }|dk    r|                     ||           d S |dk    r|                     ||           d S |                     |d          }|dk    r|                     ||           d S |dk    r|                     ||           d S |dk    r|                     ||           d S |                     |di           }|                     |d          }|d	k    r|                     ||           |                    d
          dk    r| 	                    ||           |                    d          dk    r| 
                    ||           |dk    r|                     ||           |dk    r|                     ||           |dk    s|dk    r|                     ||           |dk    r|                     ||           d S d S )Nr   pongr  ry   r   r   r   r   r   r  r   r0  ri  r  r  r  r  )r  rl   handle_pongr   handle_subscription_statushandle_un_subscription_statusr   r   r   r%  rV  rt  r  r  r  )r^   r   r   contentr  r   r   s          r`   handle_messagezcoincatch.handle_message-  sH   $$VW55 	F""7I66fVW---FfVW---F  '22G$$VW555FK++FG<<<FM!!..vw???F~~gub11""433hvw///<<!!Q&&fg...<<  A%%""67333gvw///i000xW%<%<fg...k!!!!&'22222 "!ra   c                     dS )NrG    )r^   r   s     r`   rG   zcoincatch.pingS  s    vra   c                 8    |                                  |_        |S N)millisecondslastPongr^   r   r   s      r`   r  zcoincatch.handle_pongV  s    ++--ra   c                     |S r  r  r"  s      r`   r  z$coincatch.handle_subscription_statusZ  s    ra   c                    |                      |d          }||                     |di           g}t          dt          |                    D ]}||         }|                     |d          }|dk    r|                     ||           =|dk    r|                     ||           Z|dk    r|                     ||           w|                    d          r| 	                    ||           |S )	Nr   r   r   r   r0  ri  r   r  )
r   r   r   r   rl   !handle_order_book_un_subscriptionhandle_trades_un_subscriptionhandle_ticker_un_subscription
startswithhandle_ohlcv_un_subscription)r^   r   r   argsListr   r   r   s          r`   r  z'coincatch.handle_un_subscription_status]  s   >>'622wr::;Hq#h--(( 
	C 
	CA1+C&&sI66G'!!66vwGGGGG##2267CCCCH$$2267CCCC##H-- C11&'BBBra   c                    |                      |di           }|                     |d          }|dk    rdnd}|                     |d          }|                     |d d |          }|d         }d|d         z   }	d	|z   }
|| j        v r| j        |= |
|j        v r|j        |
= |	|j        v r|j        |	= t          | j        d
z   |z             }|                    ||
           |	                    d|	           d S )Nr   rd   spr   r   rc   r   zunsubscribe:orderbook:z
orderbook:z orderbook T)
r   r  rl   r  r=  r   r   r   rZ  r   r^   r   r   r   rd   r{  rc   r   r   r   subMessageHashr[  s               r`   r%  z+coincatch.handle_order_book_un_subscriptionn  s    nnWeR00))#z::"d**vv!!#x00!!&$d;;!.1AA%.T_$$'V111$^4&...$[1 =!86!ABBe^,,,t[)))))ra   c                    |                      |di           }|                     |d          }|dk    rdnd}|                     |d          }|                     |d d |          }|d         }d|d         z   }	d	|z   }
|| j        v r| j        |= |
|j        v r|j        |
= |	|j        v r|j        |	= t          | j        d
z   |z             }|                    ||
           |	                    d|	           d S )Nr   rd   r,  r   r   rc   r   zunsubscribe:trade:rj  z trades T)
r   r  rl   r  rl  r   r   r   rZ  r   r-  s               r`   r&  z'coincatch.handle_trades_un_subscription  s    nnWeR00))#z::"d**vv!!#x00!!&$d;;!*VH-==!F*T[  F#V111$^4&...$[1 :!5!>??e^,,,t[)))))ra   c                    |                      |di           }|                     |d          }|dk    rdnd}|                     |d          }|                     |d d |          }|d         }d|d         z   }	d	|z   }
|| j        v r| j        |= |
|j        v r|j        |
= |	|j        v r|j        |	= t          | j        d
z   |z             }|                    ||
           |	                    d|	           d S )Nr   rd   r,  r   r   rc   r   zunsubscribe:ticker:r   z ticker T)
r   r  rl   r  r   r   r   r   rZ  r   r-  s               r`   r'  z'coincatch.handle_ticker_un_subscription  s    nnWeR00))#z::"d**vv!!#x00!!&$d;;!+fX.>>"V+T\!!V$V111$^4&...$[1 :!5!>??e^,,,t[)))))ra   c                 L   |                      |di           }|                     |d          }|dk    rdnd}|                     |d          }|                     |d          }|                    dd	          }|                      | j        d
          }	|                     ||	          }
|                     |d d |          }|d         }d|
z   dz   |d         z   }d|z   dz   |
z   }|| j        v r|
| j        |         v r| j        |         |
= |                     |||           d S )Nr   rd   r,  r   r   rc   r   r  r   rF   r   zunsubscribe:ohlcv:r   r  )	r   r  rl   replacerX   r  r  r  clean_unsubscription)r^   r   r   r   rd   r{  rc   r   r  r  r   r   r   r   r.  s                  r`   r)  z&coincatch.handle_ohlcv_un_subscription  sC   nnWeR00))#z::"d**vv!!#x00""3	22??8R00^^DL2CDD
''*==	!!&$d;;!*Y6<vh?OO!F*S09<T[  DK///K'	2!!&.+FFFFFra   r  )J__name__
__module____qualname__r   r]   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   listr  r  r%  r!  r   r.  r3  r-  rV  rD  rb  r?  r   rg  rf  rp  rt  rs  r   r  r  r   r  r  dictr  r   r  r  r  r  r	   r  r  rG   r  r  r  r%  r&  r'  r)  __classcell__)r_   s   @r`   r    r       sc       A# A A A A A AF/ / / )+    6 KM J J J J ?A H H H H LN J J J J Y[ W W W W&     ]_ W W W W _a 
E 
ES 
E3 
ERU 
Eeh 
E 
E 
E 
E	"& 	" 	" 	" 	" F # W] jm    $ 68 O O OF O O O O. 9; O OC Os O O O O 6:" E E7 E E E E EB$:F $: $: $: $:LH H H HT ?CQUdhqs H H H H3 H^a Hx|  ~B  yC H H H H< BFb Y Y3 Y3 YUX Y Y Y Y"(,6 (, (, (, (,T
 
F 
d 
 
 
 
( @DB P PS P PS\ P P P P =? M M M3 M M M M& SW_a ! !$s) !C !fo ! ! ! !<C= C= C= C= C=J*6 *3 *UX * * * *
$ $ $
3 3 3 <@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 9; M MC Ms M M M M,F , , , ,< E    4 *, P P P P P P642V 42 42 42 42l 04$UYbd 8U 8U 8UC 8Us 8Uimnsit 8U 8U 8U 8UtY%6 Y% Y% Y% Y%v\ \D \& \E \ \ \ \| 8<$]ajl *] *]W *]C *]WZ *]quv~q *] *] *] *]XF    ?+v ?+ ?+ ?+ ?+BK K K KZ6 t    0$3V $3 $3 $3 $3L6    &        F    "* * * * *&*F * * * *&*F * * * *&G6 G G G G G G G Gra   r    )'ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   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   r   r   r   r   r   ccxt.base.preciser   async_supportr    r  ra   r`   <module>rC     s       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", `G `G `G `G `Gra   