
    ":h                         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  G d dej:                  j<                        Zy)    N)
ArrayCacheArrayCacheBySymbolById)
AnyBalancesIntOrder	OrderBookStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)
BadRequest)NotSupported)RateLimitExceeded)ExchangeNotAvailable)RequestTimeoutc                   4    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                    | j                  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.00000000001rD   )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    I/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/coinex.pyrd   zcoinex.describe   s   fd < > $!%# $#)-!%#"&,0# %   B E d     "-$& %H*-	#)6Z\ 0 0	
 ^ . ] 1 Z \ 0 0 ^ . ]  1!$ 'kJA
 J J	    c                 ~    | j                  | j                  | j                  dd      d      }|| j                  d<   |S )N	requestIdr      )sumsafe_integerr_   )re   rj   s     rg   
request_idzcoinex.request_idd   s9    HHT..t||[!LaP	$-[!rh   clientc                    | j                  | j                  d      }| j                  |di       }| j                  |dg       }g }t	        dt        |            D ]s  }||   }| j                  |d      }	| j                  |	d d |      }
| j                  |	d d |      }| j                  ||      }|| j                  |
<   |j                  |       u | j                  |d      }t	        dt        |            D ]}  }||   }|j                  d      }|d   }|j                  d	      }| j                  |d
|      }t        |j                               }t        |      }|dkD  sl|j!                  ||        |j!                  |d       y )NdefaultTypedata
state_listr   market	tickers::z::rk   ,symboltickers)safe_stringr_   	safe_dict	safe_listrangelensafe_symbolsafe_marketparse_ws_tickerrx   appendfind_message_hashessplitfilter_by_arraylistkeysresolve)re   ro   messagerq   rr   
rawTickers
newTickersientrymarketIdrw   rt   parsedTickermessageHashesmessageHashpartssymbolsStringsymbolsrx   tickersSymbols
numTickerss                        rg   handle_tickerzcoinex.handle_tickeri   s   p &&t||]C~~gvr2^^D,;

q#j/* 	,AqME''x8H%%hdKHF%%hdKHF//v>L#/DLL l+	, 00Eq#m,- 		5A'*K%%d+E!!HM#))#.G**:xIG!',,.1N^,JA~w4		5 	z9-rh   Nc           
      8   | j                  | j                  d      }| j                  |d      }| j                  i d| j                  ||d |      dd dd d| j                  |d      d| j                  |d      dd d	| j                  |d
      dd d| j                  |d      dd d| j                  |d      d| j                  |d      d| j                  |d      dd dd dd dd | j                  |d      | j                  |d      |d|      S )Nrq   rt   rw   	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)ry   r_   safe_tickerr~   )re   tickerrt   rq   r   s        rg   r   zcoinex.parse_ws_ticker   s   P &&t||]C##FH5 !
d&&x{K!
!
 !
 D$$VV4	!

 4##FE2!
 4!
 ))&,?!
 4!
 ))&-@!
 D!
 D$$VV4!
 T%%fg6!
 D$$VV4!
 T!
 d!
  $!!
" t#!
$ **68<++FG<)!
* + 	rh   c                   K   | j                          d{    d}| j                  dd|d      \  }}| j                  |       d{    | j                  d   d   |   }| j	                  | j
                  dd      }| j                  |d      }t        |j                               }|g }d	}|dk(  r|d
z  }n|dz  }dd|i| j                         d}| j                  ||      }	| j                  |||	|       d{   S 7 7 7 w)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   rn   rb   watch)
re   r   typeurlactiveCurrenciesactiveCurrenciesById
currenciesr   	subscriberequests
             rg   watch_balancezcoinex.watch_balance   s+     !!!99.$PVX^_f%%%iit$T*>>$*?*?4P#}}-=tD.3356
J 6>7"K7"K)!:.//#
	
 ""9f5ZZ[';GGG- 	" 	&& Hs4   D
D1D
D	B6D
?D D
D
D
c                 0   | j                   i | _         | j                  |di       }| j                  |dg       }|d   }| j                  |d      }| j	                  |d      }|d u}|d u}	d }
d }g }|r0d}t        dt        |            D ]  }| j                  ||      } |}
|	r0d}t        dt        |            D ]  }| j                  ||      } |}
t        dt        |            D ]  }||   }| j                  ||        d }|| j                  | j                   |      i | j                   |<   |
| j                   |   d<   | j                  | j                   |         | j                   |<   d	|z   }|j                  | j                   |   |       y y )
Nrr   balance_listr   
updated_atunrealized_pnlr*   r+   r   z	balances:)balancerz   r{   rm   ry   r|   r}   array_concatparse_ws_balance
safe_valuesafe_balancer   )re   ro   r   rr   r   
firstEntryupdatedunrealizedPnlisSpotisSwapr   rJ   rawBalancesr   r   r   s                   rg   handle_balancezcoinex.handle_balance  s   P <<DL~~gvr2>>$;a[
##J=((5EF%t+G1c(m, G"//XFGDG1c(m, G"//XFGDq#k*+ 	2ANE!!%1	2 t||W5=(*W%,0DLL!&)$($5$5dll76K$LDLL!%/KNN4<<0+> rh   c                 \   | j                         }| j                  |d      }| j                  |      }| j                  |d      |d<   | j                  |d      |d<   |>| j                  | j                  |      i | j                  |<   || j                  |   |<   y || j                  |<   y )Nccy	availablefreefrozenused)rJ   ry   safe_currency_coder   r   )re   r   accountTyperJ   
currencyIdcodes         rg   r   zcoinex.parse_ws_balanceg  s    0 ,,.%%gu5
&&z2**7K@**7H="t||[9A,.[).5DLL%d+!(DLLrh   rw   sincelimitc                 ^  K   | j                          d{    d}|| j                  |      }|d   }d}| j                  d||d      \  }}| j                  |       d{    | j                  d   d   |   }g }d}	||	d|z   z  }	|j                  |d	          n|dk(  r|	d
z  }	n|	dz  }	dd|i| j                         d}
| j                  |
|      }| j                  ||	||	       d{   }| j                  r|j                  ||      }| j                  ||||d      S 7 7 7 <w)aj  
        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>`
        Nrw   r#   r*   r)   r   myTrades:r   r   r   zuser_deals.subscribemarket_listr   T)r   rt   r   r   r^   r   rn   rb   r   
newUpdatesgetLimitfilter_by_symbol_since_limit)re   rw   r   r   r   rt   r   r   subscribedSymbolsr   r   r   tradess                rg   watch_my_tradeszcoinex.watch_my_trades  s`     !!![[(FH%F99/6SY[abf%%%iit$T* 3<'K$$VD\2v~w&w&,$&78//#

 ""7F3zz#{G[II??OOFE2E00tTT9 	" 	&$ Js5   D-D&AD-"D)#BD-.D+/8D-)D-+D-c                 l   | j                  |di       }| j                  |d      }|j                  j                  d      dkD  }|rdnd}| j	                  |d d |      }|d   }d|z   }	d|d   z   }
| j                  | j                  |      }|7| j                  | j                  d	d
      }t        |      }|| j                  |<   | j                  ||      }|j                  |       || j                  |<   |j                  | j                  |   |
       |j                  | j                  |   |	       y )Nrr   rt   r*   r+   rw   z	myTrades:r   tradesLimit  )rz   ry   r   findr   r   r   rm   r_   r   parse_ws_trader   r   )re   ro   r   rr   r   r   rq   rt   rw   r   messageWithTypestoredr   parseds                 rg   handle_my_tradeszcoinex.handle_my_trades  s#   ( ~~gvr2##D(3(2- &fF!!(D$D!!F*%v6f5>%%dllM4HE&F"(DKK$$T62f$Ft{{6*O<t{{6*K8rh   c                    | j                  |di       }| j                  |dg       }| j                  |d      }|j                  j	                  d      dkD  }|rdnd}| j                  |d d |      }|d   }	d|	z   }
| j                  | j                  |	      }|7| j                  | j                  d	d
      }t        |      }|| j                  |	<   t        dt        |            D ]*  }||   }| j                  ||      }|j                  |       , || j                  |	<   |j                  | j                  |	   |
       y )Nrr   	deal_listrt   r*   r   r+   rw   trades:r   r   r   )rz   r{   ry   r   r   r   r   r   rm   r_   r   r|   r}   r   r   r   )re   ro   r   rr   r   r   r   rq   rt   rw   r   r   r   r   trader   s                   rg   handle_tradeszcoinex.handle_trades  s6   P ~~gvr2k26##D(3(2- &fF!!(D$D!&(f5>%%dllM4HE&F"(DKKq#f+& 	"A1IE((7FMM&!	" %Ft{{6*K8rh   c                 f   | j                  |d      }d|v }|rdnd}| j                  |d      }| j                  ||d |      }i }| j                  | j                  |d            }|*| j                  |d|d         }	| j	                  |	      |d	}| j                  | j                  |d
      ||| j                  |      | j                  ||d |      | j                  |d      d | j                  |d      | j                  |d      | j                  |d      | j                  |d      d |d|      S )N
created_atmargin_marketr*   r+   rt   feefee_ccyquotecurrencycostdeal_idorder_idsiderolepriceamount)r   r   r   r   rw   orderr   r
  takerOrMakerr  r  r  r  )rm   ry   r   	omit_zeror   
safe_tradeiso8601r~   )
re   r   rt   r   r   rq   r   r  feeCostfeeCurrencyIds
             rg   r   zcoinex.parse_ws_trade  sT   L %%e\:	!U* &fF##E84!!(FD+F..!1!1%!?@ ,,UIvgOM 33MBC ""5)4"Y/&&x{K%%eZ8$$UF3 ,,UF;%%eW5&&uh7 
  	rh   c                    K   | j                          d{    | j                  |      }| j                  |g|       d{   }||d      S 7 87 w)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>`
        Nrw   )r   rt   watch_tickers)re   rw   r   rt   rx   s        rg   watch_tickerzcoinex.watch_ticker\  sX      !!!V$**F8V<<vh'(( 	"<s!   AA+AAAAr   c                 f  K   | j                          d{    | j                  |      }d}g }|du}|rHt        dt        |            D ]/  }||   }| j	                  |      }|j                  d|d   z          1 n|j                  d       d}	| j                  d||      \  }	}| j                  d   d   |	   }
d	g}d
d|i| j                         d}| j                  |
|| j                  ||      |       d{   }| j                  r|S | j                  | j                  d|      S 7 7 2w)a4  
        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   ru   rw   rx   r    r)   r   z
all@tickerzstate.subscriber   r   )r   
market_idsr|   r}   rt   r   r   r^   rn   watch_multiplerb   r   r   rx   )re   r   r   	marketIdsrt   r   symbolsDefinedr   rw   r   r   subscriptionHashesr   results                 rg   r  zcoinex.watch_tickersl  sP     !!!OOG,	!-1c'l+ E V,$$[6(3C%CDE
   +99.&RXYfiit$T**^'$i0//#
	
 **3t?O?OPY[a?bdvww??M##DLL(GDD1 	"* x"   D1D,C%D1<D/=0D1/D1c                 R   K   d|d<   | j                  |g|||       d{   S 7 w)ag  
        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)re   rw   r   r   r   s        rg   watch_tradeszcoinex.watch_trades  s2      &3!"22F8UE6RRRRs   '%'c                   K   | j                          d{    g }g }d}d}| j                  |dd      \  }}|du}	|	r\t        dt        |            D ]C  }
||
   }| j	                  |      }|j                  |d          |j                  d|d   z          E n|j                  d       d}| j                  |||      \  }}| j                  d	   d
   |   }dg}dd|i| j                         d}| j                  ||| j                  ||      |       d{   }| j                  r|S | j                  |||dd      S 7 +7 *w)ad  
        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   rw   r   r)   r   zdeals.subscriber   r   r   T)r   handle_param_stringr|   r}   rt   r   r   r^   rn   r  rb   r   filter_by_since_limit)re   r   r   r   r   r   r   rt   r!  r  r   rw   r   r   r  r   r   s                    rg   r"  zcoinex.watch_trades_for_symbols  s~     !!!#'#;#;FDVXo#p &!-1c'l+ C V,!((6$$Y1A%AB	C   *99:JFTZ[fiit$T*&Z'$&78//#
	
 **3t?O?OPY[a?bdvww??M))&%TRR7 	"0 xs"   EEDEE(EEc                   K   | j                          d{    i }g }d}d}d}| j                  |dd      \  }}| j                  |d|      \  }}| j                  | j                  di       }	| j                  |	dg       }
|| j                  |	dd      }| j                  ||
      s*t        | j                  dz   d	j                  |
      z         | j                  |	d
d      }| j                  |	dg       }| j                  |d|      }| j                  ||      s*t        | j                  dz   d	j                  |      z         | j                  |d      }|du}|rTt        dt        |            D ];  }||   }| j                  |      }|j!                  d|d   z          |d   ||dg||<   = n|j!                  d       t#        |j%                               }dd|i| j'                         d}| j)                  | j+                  | j-                  |            d      }| j.                  d   d   |   }| j1                  ||| j3                  ||      |       d{   }| j4                  r|S |j7                         S 7 l7 %w)aa  
        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%   rE   rF   r?   z1 watchOrderBookForSymbols() limit must be one of z, rH   rD   rG   aggregationz7 watchOrderBookForSymbols() aggregation must be one of r   z
orderbook:rw   r   T	orderbookzdepth.subscriber   r   sha256r)   r   )r   r%  r   rz   r_   r{   rm   in_arrayr   r   joinry   omitr|   r}   rt   r   r   valuesrn   hashencodejsonr^   r  rb   r   r   )re   r   r   r   watchOrderBookSubscriptionsr   rt   r   r!  r_   rE   rH   rG   r(  r  r   rw   
marketListr   r  r   
orderbookss                         rg   watch_order_book_for_symbolsz#coinex.watch_order_book_for_symbols  s     !!!,.##'#;#;FDVXr#s &99:JDRXYf../?D26=%%g~rBE}}UF+tww)\\_c_h_hio_ppqq!--g7KSQ~~g~rB&&v}>PQ}}[,7tww)bbeieneno{e||}}6=1!-1c'l+ _ V,$$\F84D%DE7=d|UKY]6^+F3	_   -5<<>?
'$j1//#
	
 "YYt{{499=X3Y'Z\deiit$T*..sM4CSCST]_eCfhz{{
??!!Q 	"J |s"   JJIJJ #JJc                 P   K   d|d<   | j                  |g||       d{   S 7 w)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)r5  )re   rw   r   r   s       rg   watch_order_bookzcoinex.watch_order_book  s0      &6!"66xOOOOs   &$&c                 L    | j                  |dd      }|j                  |       y )Nr   rk   )parse_bid_ask
storeArray)re   booksidedeltabidAsks       rg   handle_deltazcoinex.handle_delta  s$    ##E1a0F#rh   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )r|   r}   r>  )re   r;  deltasr   s       rg   handle_deltaszcoinex.handle_deltas  s0    q#f+& 	3Ahq	2	3rh   c                 Z   | j                  | j                  d      }| j                  |di       }| j                  |di       }| j                  |d      }| j                  |d d |      }|d   }d}	|	dz   |z   }
| j	                  |d      }| j                  | j                  |      }| j                  |d	d
      }|rU| j                  |||      }|| j                  |      | j                  |<   n| j                  |   }|j                  |       n}| j                  |dg       }| j                  |dg       }| j                  |d   |       | j                  |d   |       ||d<   ||d<   | j                  |      |d<   || j                  |<   |j                  | j                  |   |
       y )Nrq   rr   depthrt   rw   r)  r   r   is_fullFasksbidsnoncer   r   )ry   r_   rz   r   rm   r   r4  	safe_boolparse_order_book
order_bookresetr{   rA  r  r   )re   ro   r   rq   rr   rC  r   rt   rw   namer   r   currentOrderBookfullOrderBooksnapshotr)  rE  rF  s                     rg   handle_order_bookzcoinex.handle_order_book  s   8 &&t||]C~~gvr2tWb1##D(3!!(D$D!Sj6)%%e\:	??4??FCtY>,,UFIFH'*.//(*C' OOF3	)>>%4D>>%4D/7>/7>(1W%,5[)+/<<	+BZ(&6DOOF#tv.<rh   c                   K   | j                          d{    | j                  |dd      }| j                  |ddg      }d}d}d}|| j                  |      }|d   }d}	| j	                  d||d      \  }	}| j                  |	       d{    ||d   g}|d	|z   z  }ng }|	dk(  r|d
z  }n|dz  }d}
|rd}
nd}
|
d|i| j                         d}| j                  d   d   |	   }| j                  ||      }| j                  |||||       d{   }| j                  r|j                  ||      }| j                  ||||d      S 7 57 7 <w)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ordersrw   r$   r*   r   r   r   r   zstop.subscribezorder.subscriber   r   r)   r   T)r   safe_bool_2r-  rt   r   r   rn   r^   rb   r   r   r   r   )re   rw   r   r   r   rR  r   rt   r3  r   r   r   r   r   rT  s                  rg   watch_orderszcoinex.watch_ordersR  s     !!!""69f=6Iv#67
[[(FH%F99-QWY_`f%%% ,J3<'KJv~w&w&%F&F$j1//#

 iit$T*""7F3zz#{G['RR??OOFE2E00tTTI 	" 	&, Ss5   EEA6EEBEE8EEEc                    | j                  |di       }| j                  |ddi       }| j                  |      }|d   }| j                  |      }| j                  -| j                  | j                  dd      }t        |      | _        | j                  }	|	j                  |       d}
|
dz   |d	   z   }|j                  | j                  |       |
d|z   z  }
|j                  | j                  |
       y )
Nrr   r  rS  rw   ordersLimitr   rT  r   r   )
rz   safe_dict_2parse_ws_orderrt   rT  rm   r_   r   r   r   )re   ro   r   rr   r  parsedOrderrw   rt   r   rT  r   r   s               rg   handle_orderszcoinex.handle_orders  s    j ~~gvr2  w;))%0X&V$;;%%dllM4HE07DKk"%+fVn<t{{O4sV|#t{{K0rh   c           	         | j                  |d      }| j                  |d      }| j                  |d      }d|v }|rdnd}| j                  ||d |      }d }| j                  | j	                  |dd            }	|	*| j                  |d	|d
         }
| j                  |
      |	d}| j                  i d|d| j	                  |dd      d| j                  |d      d| j                  |      d|d| j                  |d      d|d   d| j                  |d      dd dd d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      d| j                  |d      d | j	                  |d!d"      d#| j	                  |d$d%      d d | j                  |      |d d&|      S )'Nr   rt   statusr  r*   r+   r  quote_ccy_feer  r  r  r   r   r	  stop_idclientOrderId	client_idr   r   lastTradeTimestampr   rw   r   timeInForcepostOnlyr
  r  	stopPricetrigger_pricetriggerPricer  filledfilled_amount
fill_value	remainingunfilled_amountunfill_amount)r  r   r^  r  r   )	rm   ry   r   r  safe_string_2r   
safe_orderr  parse_ws_order_status)re   r  rt   r   r   r^  r   rq   r  r  r  s              rg   rZ  zcoinex.parse_ws_order  sf   r %%e\:	##E84!!%2!U* &fF!!(FD+F..!3!3E5/!RS ,,UIvgOM 33MBC   
E 
$$$UJ	B 
 T--e[A 
 Y/	 

  
 !$"3"3E<"H 
 fX& 
 D$$UF3 
 4 
  
 D$$UF3 
 T%%eW5 
 ))%A 
 D,,UOD 
 d&&uh7 
  d((N! 
" ++E3DoV# 
$ 008- 
. / 	rh   c                 4    dddd}| j                  |||      S )Nr   canceled)active_successactive_failcancel)ry   )re   r^  statusess      rg   rq  zcoinex.parse_ws_order_status  s)    $% 

 &&99rh   c                 f  K   | j                          d{    | j                  |      }g }d}|du}|rHt        dt        |            D ]/  }||   }| j	                  |      }|j                  d|d   z          1 n|j                  d       d}	| j                  d||      \  }	}| j                  d   d   |	   }
d	g}d
d|i| j                         d}| j                  |
|| j                  ||      |       d{   }| j                  r|S | j                  | j                  d|      S 7 7 2w)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:rw   bidsasksr   r)   r   zall@bidsaskszbbo.subscriber   r   )r   r  r|   r}   rt   r   r   r^   rn   r  rb   r   r   rz  )re   r   r   r  r   rt   r  r   rw   r   r   r  r   r  s                 rg   watch_bids_askszcoinex.watch_bids_asks  sQ     !!!OOG,	!-1c'l+ E V,$$[6(3C%CDE
   ,99/6SYZfiit$T*,-%$i0//#
	
 **3t?O?OPY[a?bdvww??M##DMM8WEE1 	"* xr  c                     | j                  |di       }| j                  |      }|d   }|| j                  |<   d|z   }|j                  ||       y )Nrr   rw   ry  )rz   parse_ws_bid_askrz  r   )re   ro   r   rr   r   rw   r   s          rg   handle_bid_askzcoinex.handle_bid_ask  sV     ~~gvr2,,T2h' ,f!F*|[1rh   c                    | j                  | j                  d      }| j                  |d      }| j                  ||d |      }| j                  |d      }| j	                  | j                  ||d |      || j                  |      | j                  |d      | j                  |d      | j                  |d      | j                  |d      |d|      S )	Nrq   rt   r   best_ask_pricebest_ask_sizebest_bid_pricebest_bid_size)rw   r   r   r   r   r   r   r   )ry   r_   r   rm   r   r~   r  safe_number)re   r   rt   rq   r   r   s         rg   r}  zcoinex.parse_ws_bid_ask  s     &&t||]C##FH5!!(FD+F%%fl;	&&x{K"Y/##F,<=))&/B##F,<=))&/B	!
 	 		rh   c                    | j                  |d      }| j                  |d      }|2| j                  d d |j                  |d | j                  |      |d d 	       | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  d}| j                  ||      }|
 |||       y | j                  ||       y )Nr   r   )zstate.updatezbalance.updatezdeals.updatezuser_deals.updatezdepth.updatezorder.updatezstop.updatez
bbo.update)ry   handle_errorsr   r1  r   r   r   r   rP  r\  r~  r   handle_subscription_status)re   ro   r   r   errorhandlershandlers          rg   handle_messagezcoinex.handle_message  s    !!'84  )4tT6::vtTYYuEUW^`dfjk .."11 ..!%!6!6 22 ..----	
 //(F3FG$''8rh   r   reasonr   r   headersbodyc
                 @   |y | j                  |d      }
|
d uxr |
dk7  }| j                  |d      }|d uxr |dk7  }|s|r]| j                  dz   |z   }| j                  | j                  d   ||       | j                  | j                  d   |
|       t        |      y )Nr   okr   rD    r[   r\   )safe_string_lowerry   r   throw_exactly_matched_exceptionra   throw_broadly_matched_exceptionr   )re   r   r  r   r   r  r  responserequestHeadersrequestBodyr   isErrorMessage	errorCodeisErrorCodefeedbacks                  rg   r  zcoinex.handle_errors  s     ((9=!-DGtO$$Xv6	 ,D93C.ww}t+H001I9V^_001I7T\]))rh   c                 P   | j                  |d      }| j                  |d      }d}|dk(  s|dk(  r.| j                  |j                  |      }|j	                  d       y t        | j                  |            }|j                  ||       ||j                  v r|j                  |= y y )Nr   r   authenticatedr  rD   T)	r  ry   r   futuresr   r   r1  rejectsubscriptions)re   ro   r   r^  r  r   futurer  s           rg   handle_authentication_messagez$coinex.handle_authentication_message  s    $ '';$$Wf5	%dN	S 0__V^^[AFNN4 '		'(:;EMM%-f222((5 3rh   c                    | j                  |d      }| j                  |j                  |      }|O| j                  |d      }| j                  |j                  |      }||j                  d       |j                  |= y y )Nr   r  T)rm   r   r  ry   r  r   )re   ro   r   r   subscriptionfutureIndexr  s          rg   r  z!coinex.handle_subscription_status.  s{    w-v';';R@#**<BK__V^^[AF!t$$$R( $rh   r   c                 h  K   | j                   d   d   |   }| j                  |      }| j                         }t        |      }d}|j	                  |      }| j                  |j                  |      }|
| d {   S | j                         }	|	|d}
| j                  | j                  |      | j                  | j                        t        j                  d      }|	d| j                  |j                         |dd}| j                  ||||	|
       d	|j                  |<   | d {   S 7 7 w)
Nr)   r   r  )r   r  hexzserver.sign)	access_id
signed_strr   )r   r   r   T)r^   ro   millisecondsstrr  r   r  rn   hmacr0  secrethashlibr*  apiKeylowerr   )re   r   r   ro   timer   r   r  r  rj   r   r  r   s                rg   r   zcoinex.authenticate8  s"    iit$T*S!  "I	%{+(<(<kJ$<OO%	!
	 yyY/T[[1I7>>[`a#![["jjl!
 	

3WiC,0[)|%  $ s%   A8D2:D.;B.D2)D0*D20D2)N)3__name__
__module____qualname__r   rd   rn   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r  r   r  r   r   r  r#  r"  r	   r5  r7  r>  rA  rP  r   rV  r\  rZ  rq  r{  r~  r}  r  intdictr  r  r  r   __classcell__)rf   s   @rg   r   r      s   K# KZ
O.F O.b?B *,  H  HDH?V H?T")H 37TX\eg )UC )Us )URU )Ulpqvlw )UV%9v %9N:9F :9xAF 68 ) )F )  6:" #E7 #E #EJ <@d[] S SS S Sbfglbm S  OSaenp (Sd3i (S (S[^ (Suyz  vA (ST SW_a 4"$s) 4"C 4"fo 4"l @DB PS P PS\ P$38= 8=t 04$UYbd 2U 2UC 2Us 2Uimnsit 2UhC1F C1J~@: 8<B #FW #F7 #FJ2V 2,49V 9*# s  c TX `c &6F 6<) )s rh   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   async_supportr    rh   rg   <module>r     sN     O  f f f 4  * 0 ' ) . 1 +~T&& ~rh   