
    ei                         d dl Zd dl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 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)
AnyBalancesIntOrder	OrderBookStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)ArgumentsRequired)
BadRequest)NotSupported)RateLimitExceeded)ExchangeNotAvailable)RequestTimeoutc                   X    e Zd Zdef fdZd ZdefdZd/dZi fde	fdZ
defd	Zd/d
Zdddi fdedededee         fdZdefdZdefdZd/dZi fdedefdZdi fdedefdZddi fdedededee         fdZddi fdee         dededee         fdZdi fdee         dedefdZdi fdededefdZd Z d Z!defdZ"dddi fdedededee#         fdZ$defdZ%d/dZ&d Z'di fdedefd Z(defd!Z)d/d"Z*defd#Z+d$e,d%ed&ed'ed(e-d)efd*Z.defd+Z/defd,Z0d-efd.Z1 xZ2S )0coinex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id
ddddddddddddddg ddg ddddi i dt          d t
          d!t          d"t          d#t          d$t          d%t          d&t          d't          d(t
          d)t          d*t          d+t          d,t          d-t          d.t          i d/d0          S )1NTF)wswatchBalancewatchBidsAskswatchTickerwatchTickerswatchTradeswatchTradesForSymbolswatchMyTradeswatchOrderswatchOrderBookwatchOrderBookForSymbols
watchOHLCVfetchOHLCVWsapir   z wss://socket.coinex.com/v2/spot/z#wss://socket.coinex.com/v2/futures/)spotswapgunzip<      i,  i  i  i  i   i@8  i`T  i  iQ i i:	 )1m3m5m15m30m1h2h4h6h12h1d3d1wr+   )   
      2   r@   )10001001010z0.1z0.01z0.001z0.0001z0.00001z0.000001z	0.0000001z
0.00000001z0.000000001z0.0000000001z0.00000000001rE   )limitsdefaultLimitaggregationsdefaultAggregation)r   
timeframesaccountr&   20001200022100121002230012300224001240023000130002310013100233001330023400134002)exactbroad)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describer   r   r   r   r   r   r   )self	__class__s    M/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/coinex.pyre   zcoinex.describe   s   fd 3 3 < < > > $!%# $#)-!%#"&,0# %    B E  d      "-oo$& %H  %H  %H*-	# #) 6Z\ 0 0	
 ^ . ] 1 Z \ 0 0 ^ . ]  1!$ ' kJA
 JA
 J J J	    c                     |                                   |                     |                     | j        dd          d          }|| j        d<   |                                  |S )N	requestIdr      )lock_idsumsafe_integerr`   	unlock_id)rf   rk   s     rh   
request_idzcoinex.request_ide   sX    HHT..t|[!LLaPP	$-[!ri   clientc                    |                      | j        d          }|                     |di           }|                     |dg           }i }t	          dt          |                    D ]u}||         }|                      |d          }	|                     |	d d |          }
|                     |	d d |          }|                     ||          }|| j	        |
<   |||
<   v| 
                    |d          }t	          dt          |                    D ]}||         }|                    d          }|d         }|                    d	          }|                     |d
|          }t          |                                          }t          |          }|dk    r|                    ||           |                    |d           d S )NdefaultTypedata
state_listr   market	tickers::z::rl   ,symboltickers)safe_stringr`   	safe_dict	safe_listrangelensafe_symbolsafe_marketparse_ws_tickerr{   find_message_hashessplitfilter_by_arraylistkeysresolve)rf   rr   messagert   ru   
rawTickers
newTickersientrymarketIdrz   rw   parsedTickermessageHashesmessageHashpartssymbolsStringsymbolsr{   tickersSymbols
numTickerss                        rh   handle_tickerzcoinex.handle_tickerl   s   p &&t|]CC~~gvr22^^D,;;

q#j//** 	. 	.AqME''x88H%%hdKHHF%%hdKHHF//v>>L#/DL !-Jv00EEq#m,,-- 		5 		5A'*K%%d++E!!HM#))#..G**:xIIG!',,..11N^,,JA~~w444z9-----ri   Nc           
         |                      | 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|          S )Nrt   rw   rz   	timestampdatetimehighlowbid	bidVolume
volume_buyask	askVolumevolume_sellvwapopencloselastpreviousClosechange
percentageaveragevolumevalue)
baseVolumequoteVolumeinfo)r|   r`   safe_tickerr   )rf   tickerrw   rt   r   s        rh   r   zcoinex.parse_ws_ticker   s   P &&t|]CC##FH55 !
d&&x{KK!
!
 !
 D$$VV44	!

 4##FE22!
 4!
 ))&,??!
 4!
 ))&-@@!
 D!
 D$$VV44!
 T%%fg66!
 D$$VV44!
 T!
 d!
  $!!
" t#!
$ **68<<++FG<<)!
 !
 !
* +  	ri   c                 L  K   |                                   d{V  d}|                     dd|d          \  }}|                     |           d{V  | j        d         d         |         }|                     | j        dd          }|                     |d          }t          |                                          }|g }d	}|dk    r|d
z  }n|dz  }dd|i| 	                                d}| 
                    ||          }	|                     |||	|           d{V S )a  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://docs.coinex.com/api/v2/assets/balance/ws/spot_balance
        https://docs.coinex.com/api/v2/assets/balance/ws/futures_balance

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
        Nr   r+   r*   r   activeTidbalances:spot:swapzbalance.subscribeccy_listmethodparamsr   )load_marketshandle_market_type_and_paramsauthenticater_   	filter_bycurrencies_by_idindex_byr   r   rq   rc   watch)
rf   r   typeurlactiveCurrenciesactiveCurrenciesById
currenciesr   	subscriberequests
             rh   watch_balancezcoinex.watch_balance   sh      !!!!!!!!!99.$PVX^__f%%%%%%%%%it$T*>>$*?4PP#}}-=tDD.335566
J 6>>7"KK7"K)!:.//##
 
	
 ""9f55ZZ[';GGGGGGGGGri   c                    | j         i | _         |                     |di           }|                     |dg           }|d         }|                     |d          }|                     |d          }|d u}|d u}	d }
d }g }|r:d}t          dt          |                    D ]}|                     ||          }|}
|	r:d}t          dt          |                    D ]}|                     ||          }|}
t          dt          |                    D ] }||         }|                     ||           !d }|| 	                    | j         |          
i | j         |<   |
| j         |         d<   | 
                    | j         |                   | j         |<   d	|z   }|                    | j         |         |           d S d S )
Nru   balance_listr   
updated_atunrealized_pnlr+   r,   r   z	balances:)balancer}   r~   ro   r|   r   r   array_concatparse_ws_balance
safe_valuesafe_balancer   )rf   rr   r   ru   r   
firstEntryupdatedunrealizedPnlisSpotisSwapr   rK   rawBalancesr   r   r   s                   rh   handle_balancezcoinex.handle_balance   s   P <DL~~gvr22>>$;;a[
##J==((5EFF%t+ 	G1c(mm,, G G"//XFFD 	G1c(mm,, G G"//XFFDq#k**++ 	2 	2ANE!!%1111t|W55=(*W%,0DL!&)$($5$5dl76K$L$LDL!%/KNN4<0+>>>>> ri   c                 n   |                                  }|                     |d          }|                     |          }|                     |d          |d<   |                     |d          |d<   |7|                     | j        |          
i | j        |<   || j        |         |<   d S || j        |<   d S )Nccy	availablefreefrozenused)rK   r|   safe_currency_coder   r   )rf   r   accountTyperK   
currencyIdcodes         rh   r   zcoinex.parse_ws_balancej  s    0 ,,..%%gu55
&&z22**7K@@**7H=="t|[99A,.[).5DL%d+++!(DLri   rz   sincelimitc                   K   |                                   d{V  d}||                     |          }|d         }d}|                     d||d          \  }}|                     |           d{V  | j        d         d         |         }g }d}	|$|	d|z   z  }	|                    |d	                    n|dk    r|	d
z  }	n|	dz  }	dd|i|                                 d}
|                     |
|          }|                     ||	||	           d{V }| j	        r|
                    ||          }|                     ||||d          S )ah  
        watches information on multiple trades made by the user

        https://docs.coinex.com/api/v2/spot/deal/ws/user-deals
        https://docs.coinex.com/api/v2/futures/deal/ws/user-deals

        :param str [symbol]: unified symbol of the market the trades were made in
        :param int [since]: the earliest time in ms to watch trades
        :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>`
        Nrz   r$   r+   r*   r   myTrades:r   r   r   zuser_deals.subscribemarket_listr   T)r   rw   r   r   r_   appendrq   rc   r   
newUpdatesgetLimitfilter_by_symbol_since_limit)rf   rz   r   r   r   rw   r   r   subscribedSymbolsr   r   r   tradess                rh   watch_my_tradeszcoinex.watch_my_trades  s      !!!!!!!!![[((FH%F99/6SY[abbf%%%%%%%%%it$T* 3<'K$$VD\2222v~~w&w&,$&78//##
 

 ""7F33zz#{G[IIIIIIII? 	3OOFE22E00tTTTri   c                    |                      |di           }|                     |d          }|j                            d          dk    }|rdnd}|                     |d d |          }|d         }d|z   }	d|d         z   }
|                     | j        |          }|5|                     | j        d	d
          }t          |          }|| j        |<   | 
                    ||          }|                    |           || j        |<   |                    | j        |         |
           |                    | j        |         |	           d S )Nru   rw   r+   r,   rz   z	myTrades:r   tradesLimit  )r}   r|   r   findr   r   r   ro   r`   r   parse_ws_trader   r   )rf   rr   r   ru   r   r   rt   rw   rz   r   messageWithTypestoredr   parseds                 rh   handle_my_tradeszcoinex.handle_my_trades  sJ   ( ~~gvr22##D(33((2- &2ffF!!(D$DD!!F*%v6f55>%%dlM4HHE&&F"(DK$$T622f$Ft{6*O<<<t{6*K88888ri   c                    |                      |di           }|                     |dg           }|                     |d          }|j                            d          dk    }|rdnd}|                     |d d |          }|d         }	d|	z   }
|                     | j        |	          }|5|                     | j	        d	d
          }t          |          }|| j        |	<   t          dt          |                    D ]5}||         }|                     ||          }|                    |           6|| j        |	<   |                    | j        |	         |
           d S )Nru   	deal_listrw   r+   r   r,   rz   trades:r   r   r   )r}   r~   r|   r   r   r   r   r   ro   r`   r   r   r   r   r   r   )rf   rr   r   ru   r   r   r   rt   rw   rz   r   r   r   r   trader   s                   rh   handle_tradeszcoinex.handle_trades  sb   P ~~gvr22k266##D(33((2- &2ffF!!(D$DD!&(f55>%%dlM4HHE&&F"(DKq#f++&& 	" 	"A1IE((77FMM&!!!!$Ft{6*K88888ri   c                    |                      |d          }d|v }|rdnd}|                     |d          }|                     ||d |          }i }|                     |                     |d                    }|5|                     |d|d                   }	|                     |	          |d	}|                     |                     |d
          |||                     |          |                     ||d |          |                     |d          d |                     |d          |                     |d          |                     |d          |                     |d          d |d|          S )N
created_atmargin_marketr+   r,   rw   feefee_ccyquotecurrencycostdeal_idorder_idsiderolepriceamount)r   r   r   r   rz   orderr   r  takerOrMakerr  r  r
  r  )ro   r|   r   	omit_zeror   
safe_tradeiso8601r   )
rf   r   rw   r   r   rt   r   r  feeCostfeeCurrencyIds
             rh   r   zcoinex.parse_ws_trade  s   L %%e\::	!U* &2ffF##E844!!(FD+FF..!1!1%!?!?@@ ,,UIvgOOM 33MBB C ""5)44"Y//&&x{KK%%eZ88$$UF33 ,,UF;;%%eW55&&uh77 
  
   	ri   c                    K   |                                   d{V  |                     |          }|                     |g|           d{V }||d                  S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://docs.coinex.com/api/v2/spot/market/ws/market
        https://docs.coinex.com/api/v2/futures/market/ws/market-state

        :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>`
        Nrz   )r   rw   watch_tickers)rf   rz   r   rw   r{   s        rh   watch_tickerzcoinex.watch_ticker_  sv       !!!!!!!!!V$$**F8V<<<<<<<<vh'((ri   r   c                   K   |                                   d{V  |                     |          }d}g }|du}|r\t          dt          |                    D ]=}||         }|                     |          }|                    d|d         z              >ng }|                    d           d}	|                     d||          \  }	}| j        d         d         |	         }
d	g}d
d|i|                                 d}| 	                    |
|| 
                    ||          |           d{V }| j        r|S |                     | j        d|          S )a2  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://docs.coinex.com/api/v2/spot/market/ws/market
        https://docs.coinex.com/api/v2/futures/market/ws/market-state

        :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 dictionary of `ticker structures <https://docs.ccxt.com/?id=ticker-structure>`
        Nr   rx   rz   r{   r!   r*   r   z
all@tickerzstate.subscriber   r   )r   
market_idsr   r   rw   r   r   r_   rq   watch_multiplerc   r   r   r{   )rf   r   r   	marketIdsrw   r   symbolsDefinedr   rz   r   r   subscriptionHashesr   results                 rh   r  zcoinex.watch_tickerso  s      !!!!!!!!!OOG,,	!- 	,1c'll++ E E V,,$$[6(3C%CDDDDE
 I  +++99.&RXYYfit$T**^'$i0//##
 
	
 **3t?O?OPY[a?b?bdvwwwwwwww? 	M##DL(GDDDri   c                 N   K   d|d<   |                      |g|||           d{V S )ae  
        get the list of most recent trades for a particular symbol

        https://docs.coinex.com/api/v2/spot/market/ws/market-deals
        https://docs.coinex.com/api/v2/futures/market/ws/market-deals

        :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>`
        r"   callerMethodNameN)watch_trades_for_symbols)rf   rz   r   r   r   s        rh   watch_tradeszcoinex.watch_trades  sA       &3!"22F8UE6RRRRRRRRRri   c                   K   |                                   d{V  g }g }d}d}|                     |dd          \  }}|du}	|	rwt          dt          |                    D ]X}
||
         }|                     |          }|                    |d                    |                    d|d         z              Yn|                    d           d}|                     |||          \  }}| j        d	         d
         |         }dd|i|                                 d}| 	                    ||| 
                    ||          |           d{V }| j        r|S |                     |||dd          S )ab  
        watch the most recent trades for a list of symbols

        https://docs.coinex.com/api/v2/spot/market/ws/market-deals
        https://docs.coinex.com/api/v2/futures/market/ws/market-deals

        :param str[] symbols: unified symbols of the markets 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>`
        Nr#  r#   r   r   r   rz   r   r*   r   zdeals.subscriber   r   r   T)r   handle_param_stringr   r   rw   r   r   r_   rq   r  rc   r   filter_by_since_limit)rf   r   r   r   r   r   r   rw   r#  r  r   rz   r   r   r   r   s                   rh   r$  zcoinex.watch_trades_for_symbols  s      !!!!!!!!!#'#;#;FDVXo#p#p &!- 	+1c'll++ C C V,,!((666$$Y1A%ABBBB	C   ***99:JFTZ[[fit$T* ($&78//##
 
	
 **3t?O?OPY[a?b?bdqrrrrrrrr? 	M))&%TRRRri   c                 d  K   |                                   d{V  i }g }d}d}d}|                     |dd          \  }}|                     | j        di           }	|                     |	dg           }
||                     |	dd          }|                     ||
          s-t          | j        dz   d		                    |
          z             | 
                    |	d
d          }|                     |	dg           }| 
                    |d|          }|                     ||          s-t          | j        dz   d		                    |          z             |                     |d          }|du}|st          | j        dz             t          dt          |                    D ]L}||         }|                     |          }|                    d|d         z              |d         ||dg||<   M|                     |||          \  }}t%          |                                          }dd|i|                                 d}| j        d         d         |         }|                     |||                     ||          |           d{V }| j        r|S |                                S )a_  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://docs.coinex.com/api/v2/spot/market/ws/market-depth
        https://docs.coinex.com/api/v2/futures/market/ws/market-depth

        :param str[] symbols: unified array of symbols
        :param int [limit]: the maximum amount of order book entries to return
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/?id=order-book-structure>` indexed by market symbols
        Nr#  r'   r&   rF   rG   r@   z1 watchOrderBookForSymbols() limit must be one of z, rI   rE   rH   aggregationz7 watchOrderBookForSymbols() aggregation must be one of z6 watchOrderBookForSymbols() requires a symbol argumentr   z
orderbook:rz   r   Tzdepth.subscriber   r   r*   r   )r   r'  r}   r`   r~   ro   in_arrayr   r   joinr|   omitr   r   r   rw   r   r   r   valuesrq   r_   r  rc   r   r   )rf   r   r   r   watchOrderBookSubscriptionsr   rw   r   r#  r`   rF   rI   rH   r*  r  r   rz   
marketListr   r   
orderbookss                        rh   watch_order_book_for_symbolsz#coinex.watch_order_book_for_symbols  s      !!!!!!!!!,.##'#;#;FDVXr#s#s &../?DD266=%%g~rBBE}}UF++ 	rtw)\\_c_h_hio_p_ppqqq!--g7KSQQ~~g~rBB&&v}>PQQ}}[,77 	~tw)bbeieneno{e|e||}}}6=11!- 	h#DG.f$fgggq#g,,'' 	[ 	[AQZF[[((F  x0@!@AAA39$<UY2Z'//99:JFTZ[[f5<<>>??
'$j1//##
 
	 it$T*..sM4CSCST]_eCfCfhuvvvvvvvv
? 	!!!ri   c                 L   K   d|d<   |                      |g||           d{V S )a|  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://docs.coinex.com/api/v2/spot/market/ws/market-depth
        https://docs.coinex.com/api/v2/futures/market/ws/market-depth

        :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
        r&   r#  N)r2  )rf   rz   r   r   s       rh   watch_order_bookzcoinex.watch_order_book  s?       &6!"66xOOOOOOOOOri   c                 ^    |                      |dd          }|                    |           d S )Nr   rl   )parse_bid_ask
storeArray)rf   booksidedeltabidAsks       rh   handle_deltazcoinex.handle_delta  s3    ##E1a00F#####ri   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )r   r   r;  )rf   r8  deltasr   s       rh   handle_deltaszcoinex.handle_deltas  sH    q#f++&& 	3 	3Ahq	2222	3 	3ri   c                    |j                             d          dk    }|rdnd}|                     |di           }|                     |di           }|                     |d          }|                     |d d |          }|d         }	d}
|
d	z   |	z   }|                     |d
          }|                     | j        |	          }|                     |dd          }|rZ| 	                    ||	|          }|| 
                    |          | j        |	<   n| j        |	         }|                    |           n|                     |dg           }|                     |dg           }|                     |d         |           |                     |d         |           ||d<   ||d<   |                     |          |d<   || j        |	<   |                    | j        |	         |           d S )Nr+   r   r,   ru   depthrw   rz   	orderbookr   r   is_fullFasksbidsnoncer   r   )r   r   r}   r|   r   ro   r   r1  	safe_boolparse_order_book
order_bookresetr~   r>  r  r   )rf   rr   r   r   rt   ru   r@  r   rw   rz   namer   r   currentOrderBookfullOrderBooksnapshotrA  rC  rD  s                      rh   handle_order_bookzcoinex.handle_order_book  s   8 ((2- &2ffF~~gvr22tWb11##D(33!!(D$DD!Sj6)%%e\::	??4?FCCtY>> 	7,,UFIFFH'*.//(*C*C'' OF3	))))>>%44D>>%44D/7>>>/7>>>(1W%,5[)+/<<	+B+BZ(&6DOF#tv.<<<<<ri   c                   K   |                                   d{V  |                     |dd          }|                     |ddg          }d}d}d}||                     |          }|d         }d}	|                     d||d          \  }	}|                     |	           d{V  ||d         g}|d	|z   z  }ng }|	dk    r|d
z  }n|dz  }d}
|rd}
nd}
|
d|i|                                 d}| j        d         d         |	         }|                     ||          }| 	                    |||||           d{V }| j
        r|                    ||          }|                     ||||d          S )a  
        watches information on multiple orders made by the user

        https://docs.coinex.com/api/v2/spot/order/ws/user-order
        https://docs.coinex.com/api/v2/futures/order/ws/user-order

        :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 bool [params.trigger]: if the orders to watch are trigger orders or not
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        Ntriggerstopordersrz   r%   r+   r   r   r   r   zstop.subscribezorder.subscriber   r   r*   r   T)r   safe_bool_2r-  rw   r   r   rq   r_   rc   r   r   r   r   )rf   rz   r   r   r   rP  r   rw   r0  r   r   r   r   r   rR  s                  rh   watch_orderszcoinex.watch_ordersV  s      !!!!!!!!!""69f==6Iv#677
[[((FH%F99-QWY_``f%%%%%%%%% ,J3<'KKJv~~w&w& 	'%FF&F$j1//##
 

 it$T*""7F33zz#{G['RRRRRRRR? 	3OOFE22E00tTTTri   c                 
   |                      |di           }|                     |ddi           }|                     |          }|d         }|                     |          }| j        0|                     | j        dd          }t          |          | _        | j        }	|	                    |           d}
|
dz   |d	         z   }|	                    | j        |           |
d|z   z  }
|	                    | j        |
           d S )
Nru   r  rQ  rz   ordersLimitr   rR  r   r   )
r}   safe_dict_2parse_ws_orderrw   rR  ro   r`   r   r   r   )rf   rr   r   ru   r  parsedOrderrz   rw   r   rR  r   r   s               rh   handle_orderszcoinex.handle_orders  s   j ~~gvr22  w;;))%00X&V$$;%%dlM4HHE077DKk"""%+fVn<t{O444sV|#t{K00000ri   c           	      H   |                      |d          }|                     |d          }|                     |d          }d|v }|rdnd}|                     ||d |          }d }|                     |                     |dd                    }	|	5|                     |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 |                     |          |d d&|          S )'Nr  rw   statusr  r+   r,   r  quote_ccy_feer  r  r  r   r   r  stop_idclientOrderId	client_idr   r   lastTradeTimestampr   rz   r   timeInForcepostOnlyr  r  	stopPricetrigger_pricetriggerPricer  filledfilled_amount
fill_value	remainingunfilled_amountunfill_amount)r
  r   r\  r  r   )	ro   r|   r   r  safe_string_2r   
safe_orderr  parse_ws_order_status)rf   r  rw   r   r   r\  r   rt   r  r  r  s              rh   rX  zcoinex.parse_ws_order  s   r %%e\::	##E844!!%22!U* &2ffF!!(FD+FF..!3!3E5/!R!RSS ,,UIvgOOM 33MBB C   
E 
$$$UJ	BB 
 T--e[AA 
 Y//	 

  
 !$"3"3E<"H"H 
 fX& 
 D$$UF33 
 4 
  
 D$$UF33 
 T%%eW55 
 ))%AA 
 D,,UODD 
 d&&uh77 
  d((NN! 
" ++E3DoVV# 
$ 0088- 
  
  
. /  	ri   c                 <    dddd}|                      |||          S )Nr   canceled)active_successactive_failcancel)r|   )rf   r\  statusess      rh   ro  zcoinex.parse_ws_order_status  s1    $% 
 

 &&999ri   c                   K   |                                   d{V  |                     |          }g }d}|du}|r\t          dt          |                    D ]=}||         }|                     |          }|                    d|d         z              >n|                    d           d}	|                     d||          \  }	}| j        d         d         |	         }
d	g}d
d|i|                                 d}| 	                    |
|| 
                    ||          |           d{V }| j        r|S |                     | j        d|          S )a  
        watches best bid & ask for symbols

        https://docs.coinex.com/api/v2/spot/market/ws/market-bbo
        https://docs.coinex.com/api/v2/futures/market/ws/market-bbo

        :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>`
        Nr   	bidsasks:rz   bidsasksr   r*   r   zall@bidsaskszbbo.subscriber   r   )r   r  r   r   rw   r   r   r_   rq   r  rc   r   r   rx  )rf   r   r   r  r   rw   r  r   rz   r   r   r   r   r!  s                 rh   watch_bids_askszcoinex.watch_bids_asks  s      !!!!!!!!!OOG,,	!- 	-1c'll++ E E V,,$$[6(3C%CDDDDE
   ,,,99/6SYZZfit$T*,-%$i0//##
 
	
 **3t?O?OPY[a?b?bdvwwwwwwww? 	M##DM8WEEEri   c                     |                      |di           }|                     |          }|d         }|| j        |<   d|z   }|                    ||           d S )Nru   rz   rw  )r}   parse_ws_bid_askrx  r   )rf   rr   r   ru   r   rz   r   s          rh   handle_bid_askzcoinex.handle_bid_ask  sf     ~~gvr22,,T22h' ,f!F*|[11111ri   c                    |                      | j        d          }|                      |d          }|                     ||d |          }|                     |d          }|                     |                     ||d |          ||                     |          |                     |d          |                     |d          |                     |d          |                     |d          |d|          S )	Nrt   rw   r   best_ask_pricebest_ask_sizebest_bid_pricebest_bid_size)rz   r   r   r   r   r   r   r   )r|   r`   r   ro   r   r   r  safe_number)rf   r   rw   rt   r   r   s         rh   r{  zcoinex.parse_ws_bid_ask  s     &&t|]CC##FH55!!(FD+FF%%fl;;	&&x{KK"Y//##F,<==))&/BB##F,<==))&/BB	!
 	!
 	 	 		ri   c                    |                      |d          }|                      |d          }|5|                     dd|j        |i |                     |          |i i 	  	         | j        | j        | j        | j        | j        | j	        | j	        | j
        d}|                     ||          }| |||           d S |                     ||           d S )Nr   r   rl    )zstate.updatezbalance.updatezdeals.updatezuser_deals.updatezdepth.updatezorder.updatezstop.updatez
bbo.update)r|   handle_errorsr   jsonr   r   r  r   rN  rZ  r|  r   handle_subscription_status)rf   rr   r   r   errorhandlershandlers          rh   handle_messagezcoinex.handle_message  s    !!'844  )44q"fj&"dii>N>NPWY[]_``` ."1 .!%!6 2 .--	
 	
 //(F33GFG$$$F''88888ri   r   reasonr   r   headersbodyc
                 V   |d S |                      |d          }
|
d uo|
dk    }|                     |d          }|d uo|dk    }|s|r`| j        dz   |z   }|                     | j        d         ||           |                     | j        d         |
|           t          |          d S )Nr   okr   rE    r\   r]   )safe_string_lowerr|   r   throw_exactly_matched_exceptionrb   throw_broadly_matched_exceptionr   )rf   r   r  r   r   r  r  responserequestHeadersrequestBodyr   isErrorMessage	errorCodeisErrorCodefeedbacks                  rh   r  zcoinex.handle_errors  s    4 ((9==!-DGtO$$Xv66	 ,D93C 	*. 	*w}t+H001I9V^___001I7T\]]])))tri   c                 t   |                      |d          }|                     |d          }d}|dk    s|dk    r2|                     |j        |          }|                    d           d S t          |                     |                    }|                    ||           ||j        v r
|j        |= d S d S )Nr   r   authenticatedr  rE   T)	r  r|   r   futuresr   r   r  rejectsubscriptions)rf   rr   r   r\  r  r   futurer  s           rh   handle_authentication_messagez$coinex.handle_authentication_message  s    $ '';;$$Wf55	%dNN	S 0 0__V^[AAFNN4     '		'(:(:;;EMM%---f222(555 32ri   c                    |                      |d          }|                     |j        |          }|R|                     |d          }|                     |j        |          }||                    d           |j        |= d S d S )Nr   r  T)ro   r   r  r|   r  r   )rf   rr   r   r   subscriptionfutureIndexr  s          rh   r  z!coinex.handle_subscription_status2  s    w--v';R@@#**<BBK__V^[AAF!t$$$$R((( $#ri   r   c                   K   | j         d         d         |         }|                     |          }|                                 }t          |          }d}|                    |          }|                     |j        |          }|| d {V S |                                 }	|	|d}
|                     | 	                    |          | 	                    | j
                  t          j        d          }|	d| j        |                                |dd}|                     ||||	|
           d	|j        |<   | d {V S )
Nr*   r   r  )r   r  hexzserver.sign)	access_id
signed_strr   )r   r   r   T)r_   rr   millisecondsstrreusableFuturer   r  rq   hmacencodesecrethashlibsha256apiKeylowerr   )rf   r   r   rr   timer   r   r  r  rk   r   r  r   s                rh   r   zcoinex.authenticate<  sP     it$T*S!!  ""II	%&&{33(<kJJ$<<<<<<OO%%	!
 
	 yyY//T[1I1I7>[`aa#!["jjll! 
 
 	

3WiCCC,0[)||||||ri   )N)3__name__
__module____qualname__r   re   rq   r   r   r   r   r   r   r   r
   r   r   r   r   r   r  r   r  r   r  r   r   r  r%  r$  r	   r2  r4  r;  r>  rN  r   rT  rZ  rX  ro  ry  r|  r{  r  intdictr  r  r  r   __classcell__)rg   s   @rh   r   r      s       K# K K K K K KZ  O.F O. O. O. O.b? ? ? ?B *,  H  H  H  H  H  HDH?V H? H? H? H?T") ") ") ")H 37TX\eg )U )UC )Us )URU )Ulpqvlw )U )U )U )UV%9v %9 %9 %9 %9N:9F :9 :9 :9 :9xA A A AF 68 ) ) )F ) ) ) )  6:" $E $E7 $E $E $E $E $EL <@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T SW_a 3" 3"$s) 3"C 3"fo 3" 3" 3" 3"j @DB P PS P PS\ P P P P$ $ $3 3 39= 9= 9= 9= 9=v 04$UYbd 2U 2U 2UC 2Us 2Uimnsit 2U 2U 2U 2UhC1F C1 C1 C1 C1J~ ~ ~ ~@: : : 8<B #F #FW #F7 #F #F #F #FJ2V 2 2 2 2,   49V 9 9 9 9*# s  c TX `c    &6F 6 6 6 6<) ) ) ) )s        ri   r   ) ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r  ccxt.base.typesr   r   r   r   r	   r
   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   r   r   r   r   async_supportr    ri   rh   <module>r     st       O O O O O O O O  f f f f f f f f f f f f f f f f f f f f f f f f 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 . . . . . . ' ' ' ' ' ' ) ) ) ) ) ) . . . . . . 1 1 1 1 1 1 + + + + + +A A A A AT& A A A A Ari   