
    eiT                         d dl Zd dl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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ArrayCacheByTimestamp)AnyBalancesBoolIntNumOrder	OrderBook	OrderSide	OrderTypeStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)
BadRequest)InvalidOrder)Precisec                       e Zd Zdef fdZi fdZdi fdedededefd	Z	i fdefd
Z
di fdedededededefdZdi fdedefdZdi fdee         defdZdi fdedee         fdZdefdZdddi fdededededee         f
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dede"fd!Z#dddi fdedededee         fd"Z$dddi fdedededee         fd#Z%i fde&fd$Z'd7defd%Z(defd&Z)defd'Z*d7d(Z+d) Z,d7d*Z-defd+Z.d7d,Z/defd-Z0defd.Z1defd/Z2d0 Z3defd1Z4defd2Z5defd3Z6dede7fd4Z8defd5Z9defd6Z: xZ;S )8poloniexreturnc                 H   |                      t          t          |                                           i ddddddddddddddd	dd
ddddddddddddddddddddddddddii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.| j        d/d0          S )1NwsT
watchOHLCVwatchOrderBookwatchTickerwatchTickerswatchTradeswatchTradesForSymbolswatchBalancewatchStatusFwatchOrderswatchMyTradescreateOrderWseditOrderWsfetchOpenOrdersWsfetchOrderWscancelOrderWscancelOrdersWs)cancelAllOrdersWsfetchTradesWsfetchBalanceWsapizwss://ws.poloniex.com/ws/publicz wss://ws.poloniex.com/ws/private)publicprivate  namebook_lv2i  i  candles_minute_1candles_minute_5candles_minute_10candles_minute_15candles_minute_30candles_hour_1candles_hour_2candles_hour_4candles_hour_6candles_hour_12candles_day_1candles_day_3candles_week_1candles_month_1)1m5m10m15m30m1h2h4h6h12h1d3d1w1M)!createMarketBuyOrderRequiresPricetradesLimitordersLimit
OHLCVLimitr!   connectionsLimitrequestsLimit
timeframesi:  )	keepAliveping)hasurlsoptions	streaming)deep_extendsuperr   describer]   )self	__class__s    O/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/poloniex.pyrd   zpoloniex.describe   s   h 5 5 > > @ @dd !$ t	
  t (  u t     u $U    !" !$#$ &*!&"')  . "C#E  6:##"J# %)!$,,...****,))*+  8 #	 u>C
 >C
 > > >	    c                 n  K   |                                   |                     |                                           }| j        d         d         d         }d}|                     |          }|                     |j        |          }|d}d|z   dz   |z   }|                     |                     |          |                     | j	                  t          j        d	          }	d
dg| j        ||	dddd}
|                     |
|          }|                     ||||           d{V }||j        |<   |S )z
 @ignore
        authenticates the user to access private web socket channels

        https://api-docs.poloniex.com/spot/websocket/authentication

        :returns dict: response from exchange
        r3   r   r5   authenticatedNz/wszGET
z
signTimestamp=base64	subscribeauth
HmacSHA2562)keysignTimestamp	signaturesignatureMethodsignatureVersion)eventchannelparams)check_required_credentialsnumber_to_stringmillisecondsr_   client
safe_valuesubscriptionshmacencodesecrethashlibsha256apiKeyextendwatch)re   rw   	timestampurlmessageHashr{   future
accessPathrequestStringrr   requestmessages               rg   authenticatezpoloniex.authenticateV   sQ      	'')))))$*;*;*=*=>>	it$Y/%S!!!5{CC>J#j03EE	QM		$++m"<"<dkk$+>V>VX_XfhpqqI$"8;%.!*'3(+ 
 
G kk'622G::c;MMMMMMMMF* 17F -rh   Nr7   r   	isPrivatesymbolsc                   K   |rdnd}| j         d         d         |         }d|gd}g }	|                     |          r|	                    d           n0|dz   d	                    |          z   }|                     |          }	|d
k    r|	|d<   |                     ||          }
|                     |||
|           d{V S )a  
 @ignore
        Connects to a websocket channel
        :param str name: name of the channel
        :param str messageHash: unique identifier for the message
        :param boolean isPrivate: True for the authenticated url, False for the public url
        :param str[] [symbols]: CCXT market symbols
        :param dict [params]: extra parameters specific to the poloniex api
        :returns dict: data from the websocket stream
        r5   r4   r3   r   rl   )ru   rv   all::,balancesr   N)r_   is_emptyappendjoin
market_idsr   r   )re   r7   r   r   r   rw   publicOrPrivater   rl   	marketIdsr   s              rg   rl   zpoloniex.subscribe   s       (1>))hit$_5 
 
	 	==!! 	1U####%,sxx/@/@@K00I:#,Ii ++i00ZZ[';GGGGGGGGGrh   c                    K   | j         d         d         d         }t          |                                           }|||d}|                     ||||           d{V S )z
 @ignore
        Connects to a websocket channel
        :param str name: name of the channel
        :param dict [params]: extra parameters specific to the poloniex api
        :returns dict: data from the websocket stream
        r3   r   r5   )idru   rw   N)r_   strnoncer   )re   r7   rw   r   r   rl   s         rg   trade_requestzpoloniex.trade_request   st       it$Y/$**,,''
 
	
 ZZ[)[IIIIIIIIIrh   symboltypesideamountpricec                   K   |                                   d{V  |                                  d{V  |                     |          }|                                }|                                }	|                     |dk    |dk    |          }
|
rd}|d         |                                |                                d}|dk    r|	dk    rd}d}|                     |dd	d          \  }}|                     |d
          }|                     |d
          }||                     ||          }n|ro|t          | j
        dz             |                     |          }|                     |          }t          j        ||          }|                     ||          }n|                     ||          }||d<   n:|                     |d         |          |d<   ||                     ||          |d<   |                     d|                     ||                     d{V }|                     |d          }|S )a  

        https://api-docs.poloniex.com/spot/websocket/trade-request#create-order

        create a trade order
        :param str symbol: unified symbol of the market to create an order in
        :param str type: 'market' or 'limit'
        :param str side: 'buy' or 'sell'
        :param float amount: how much of currency you want to trade in units of base currency
        :param float [price]: the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
        :param dict [params]: extra parameters specific to the poloniex api endpoint
        :param str [params.timeInForce]: GTC(default), IOC, FOK
        :param str [params.clientOrderId]: Maximum 64-character length.*
        :param float [params.cost]: *spot market buy only* the quote quantity that can be used alternative for the amount

 EXCHANGE SPECIFIC PARAMETERS
        :param str [params.amount]: quote units for the order
        :param boolean [params.allowBorrow]: allow order to be placed by borrowing funds(Default: False)
        :param str [params.stpMode]: self-trade prevention, defaults to expire_taker, none: enable self-trade; expire_taker: taker order will be canceled when self-trade happens
        :param str [params.slippageTolerance]: used to control the maximum slippage ratio, the value range is greater than 0 and less than 1
        :returns dict: an `order structure <https://github.com/ccxt/ccxt/wiki/Manual#order-structure>`
        NMARKETLIMIT_MAKERr   )r   r   r   BUYTcreateOrderrU   costa	   createOrder() requires the price argument for market buy orders to calculate the total cost to spend(amount * price), alternatively set the createMarketBuyOrderRequiresPrice option or param to False and pass the cost to spend(quote quantity) in the amount argumentr   r   quantityr   r   )load_marketsr   marketupperis_post_onlyhandle_option_and_paramssafe_numberomitcost_to_precisionr   r   ry   r   
string_mulamount_to_precisionprice_to_precisionr   r   	safe_dict)re   r   r   r   r   r   rw   r   uppercaseTypeuppercaseSide
isPostOnlyr   quoteAmountrU   r   amountStringpriceStringcostRequestordersorders                       rg   create_order_wszpoloniex.create_order_ws   s     . !!!!!!!!!!!!!!!!!!V$$



&&}'@-S`B`bhii
 	*)MTlJJLLJJLL
 

 X%%MU,B,BK04-8<8U8UV\^k  nQ  SW  9X  9X5-v##FF33DYYvv..F"44VTBB2 	E=&tw  2}  (}  ~  ~  ~#'#8#8#@#@L"&"7"7">">K")"4\;"O"OK"&"8"8"M"MKK"44VVDD +GH"&":":6(;KV"T"TGJ #'#:#:65#I#I ))-Wf9U9UVVVVVVVVvq))rh   r   c                   K   |                      |d          }|1|                     |dg           }|                     ||g          |d<   |                     |g||           d{V }|                     |d          }|S )a  

        https://api-docs.poloniex.com/spot/websocket/trade-request#cancel-multiple-orders

        cancel multiple orders
        :param str id: order id
        :param str [symbol]: unified market symbol
        :param dict [params]: extra parameters specific to the poloniex api endpoint
        :param str [params.clientOrderId]: client order id
        :returns dict: an list of `order structures <https://github.com/ccxt/ccxt/wiki/Manual#order-structure>`
        clientOrderIdNclientOrderIdsr   )safe_stringr|   array_concatcancel_orders_wsr   )re   r   r   rw   r   r   r   r   s           rg   cancel_order_wszpoloniex.cancel_order_ws   s       ((AA$!__V_bIIN'+'8'8-'Y'YF#$,,bT66BBBBBBBBvq))rh   idsc                    K   |                                   d{V  |                                  d{V  d|i}|                     d|                     ||                     d{V S )a  

        https://api-docs.poloniex.com/spot/websocket/trade-request#cancel-multiple-orders

        cancel multiple orders
        :param str[] ids: order ids
        :param str symbol: unified market symbol, default is None
        :param dict [params]: extra parameters specific to the poloniex api endpoint
        :param str[] [params.clientOrderIds]: client order ids
        :returns dict: an list of `order structures <https://github.com/ccxt/ccxt/wiki/Manual#order-structure>`
        NorderIdscancelOrders)r   r   r   r   )re   r   r   rw   r   s        rg   r   zpoloniex.cancel_orders_ws  s       !!!!!!!!!!!!!!!!!!
 ''GV8T8TUUUUUUUUUrh   c                    K   |                                   d{V  |                                  d{V  |                     d|           d{V S )a$  

        https://api-docs.poloniex.com/spot/websocket/trade-request#cancel-all-orders

        cancel all open orders of a type. Only applicable to Option in Portfolio Margin mode, and MMP privilege is required.
        :param str symbol: unified market symbol, only orders in the market of self symbol are cancelled when symbol is not None
        :param dict [params]: extra parameters specific to the poloniex api endpoint
        :returns dict[]: a list of `order structures <https://github.com/ccxt/ccxt/wiki/Manual#order-structure>`
        NcancelAllOrders)r   r   r   )re   r   rw   s      rg   cancel_all_orders_wszpoloniex.cancel_all_orders_ws"  sy       !!!!!!!!!!!!!!!!!!''(96BBBBBBBBBrh   r{   c                 4   |                      |d          }|                     |dg           }g }t          dt          |                    D ]4}||         }|                     |          }|                    |           5|                    ||           d S )Nr   datar   )r   r|   rangelenparse_ws_orderr   resolve)	re   r{   r   r   r   r   ir   parsedOrders	            rg   handle_order_requestzpoloniex.handle_order_request0  s     &&w55w33q#d))$$ 	' 	'AGE--e44KMM+&&&&v{+++++rh   rG   	timeframesincelimitc                   K   |                                   d{V  |                     | j        di           }|                     |||          }|t	          | j        dz   |z             |                     ||d|g|           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://api-docs.poloniex.com/spot/websocket/market-data#candlesticks

        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: the length of time each candle represents
        :param int [since]: timestamp in ms of the earliest candle to fetch
        :param int [limit]: the maximum amount of candles to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr[   z' watchOHLCV cannot take a timeframe of Fr   T)
r   r|   r`   r   r   r   rl   
newUpdatesgetLimitfilter_by_since_limit)	re   r   r   r   r   rw   r[   rv   ohlcvs	            rg   watch_ohlcvzpoloniex.watch_ohlcvE  s       !!!!!!!!!__T\<DD
"":y)DD?TW'PPS\\]]]nnWguvhOOOOOOOO? 	2NN6511E))%q$GGGrh   c                    K   |                                   d{V  |                     |          }|                     |g|           d{V }|                     ||          S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://api-docs.poloniex.com/spot/websocket/market-data#ticker

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        N)r   r   watch_tickersr|   )re   r   rw   tickerss       rg   watch_tickerzpoloniex.watch_ticker\  sz       !!!!!!!!!V$$**F8V<<<<<<<<w///rh   c                    K   |                                   d{V  d}|                     |          }|                     ||d||           d{V }| j        r|S |                     | j        d|          S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://api-docs.poloniex.com/spot/websocket/market-data#ticker

        :param str[] symbols:
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        NtickerFr   )r   market_symbolsrl   r   filter_by_arrayr   )re   r   rw   r7   
newTickerss        rg   r   zpoloniex.watch_tickersk  s       !!!!!!!!!%%g..>>$eWfMMMMMMMM
? 	##DL(GDDDrh   c                 D   K   |                      |g|||           d{V S )a$  
        get the list of most recent trades for a particular symbol

        https://api-docs.poloniex.com/spot/websocket/market-data#trades

        :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)re   r   r   r   rw   s        rg   watch_tradeszpoloniex.watch_trades}  s6       22F8UE6RRRRRRRRRrh   c                   K   |                                   d{V  |                     |dddd          }d}| j        d         d         d         }|                     |          }d|g|d	}|                     ||          }	g }
|At          d
t          |                    D ]#}|
                    |dz   ||         z              $|                     ||
|	|
           d{V }| j	        rB| 
                    |d
          }|                     |d          }|                    ||          }|                     |||dd          S )a%  
        get the list of most recent trades for a list of symbols

        https://api-docs.poloniex.com/spot/websocket/market-data#trades

        :param str[] symbols: unified symbol of the market to fetch trades for
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=public-trades>`
        NFTtradesr3   r   r4   rl   )ru   rv   r   r   r   r   r   )r   r   r_   r   r   r   r   r   watch_multipler   r|   r   r   r   )re   r   r   r   rw   r7   r   r   rl   r   messageHashesr   r   firsttradeSymbols                  rg   r   z!poloniex.watch_trades_for_symbols  s~      !!!!!!!!!%%gtUD$GGit$X.OOG,,	  !
 
	 ++i001c'll++ ? ?$$TD[71:%=>>>>**3wVVVVVVVV? 	8OOFA..E**5(;;KOOK77E))&%TRRRrh   c                 <  K   |                                   d{V  |                     | j        d          }|                     |dd          }|                     |dd|          \  }}|                     ||d|g|           d{V }|                                S )a2  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://api-docs.poloniex.com/spot/websocket/market-data#book-level-2

        :param str symbol: unified symbol of the market to fetch the order book for
        :param int [limit]: not used by poloniex watchOrderBook
        :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!   r7   r8   methodF)r   r|   r`   r   r   rl   r   )re   r   r   rw   watchOrderBookOptionsr7   	orderbooks          rg   watch_order_bookzpoloniex.watch_order_book  s       !!!!!!!!! $>N O O 5vzJJ44VXvtTTf..tUVHfMMMMMMMM	   rh   c                 X  K   |                                   d{V  d}|                                  d{V  ||                     |          }|dn|g}|                     ||d||           d{V }| j        r|                    ||          }|                     |||dd          S )a
  
        watches information on multiple orders made by the user

        https://api-docs.poloniex.com/spot/websocket/order

        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: not used by poloniex watchOrders
        :param int [limit]: not used by poloniex watchOrders
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        Nr   Tr   r   r   r   rl   r   r   r   )re   r   r   r   rw   r7   r   r   s           rg   watch_orderszpoloniex.watch_orders  s       !!!!!!!!!!!!!!!!!![[((F!>$$~~dD$HHHHHHHH? 	3OOFE22E))&%TRRRrh   c                 \  K   |                                   d{V  d}d}|                                  d{V  ||                     |          }|dn|g}|                     ||d||           d{V }| j        r|                    ||          }|                     |||dd          S )a  
        watches information on multiple trades made by the user using orders stream

        https://api-docs.poloniex.com/spot/websocket/order

        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: not used by poloniex watchMyTrades
        :param int [limit]: not used by poloniex watchMyTrades
        :param dict [params]: extra parameters specific to the poloniex strean
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=trade-structure>`
        Nr   myTradesTr   r   )	re   r   r   r   rw   r7   r   r   r   s	            rg   watch_my_tradeszpoloniex.watch_my_trades  s       !!!!!!!!! !!!!!!!!![[((F!>$$~~dKwOOOOOOOO? 	3OOFE22E))&%TRRRrh   c                    K   |                                   d{V  d}|                                  d{V  |                     ||dd|           d{V S )aZ  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://api-docs.poloniex.com/spot/websocket/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   T)r   r   rl   )re   rw   r7   s      rg   watch_balancezpoloniex.watch_balance  s       !!!!!!!!!!!!!!!!!!^^D$dFCCCCCCCCCrh   c           	         |                      |d          |                     |d          |                     |d          |                     |d          |                     |d          |                     |d          gS )N	startTimeopenhighlowcloser   )safe_integerr   )re   r   r   s      rg   parse_ws_ohlcvzpoloniex.parse_ws_ohlcv  s    " e[11UF++UF++UE**UG,,UJ//
 	
rh   c                    |                      |d          }|                      |d          }|                     |d          }|                     |d          }|                     |          }|                     |          }|                      | j        di           }|                     ||          }	|dz   |z   }
|                     ||          }|                      | j        |i           | j        |<   |                      | j        |         |	          }|h|;|                     | j        dd          }t          |          }|| j        |         |	<   |
                    |           |                    ||
           |S )	Nr   r   rv   r   r[   r   rX   r6   )r|   r   safe_symbolsafe_marketr`   find_timeframer	  ohlcvsr  r   r   r   )re   r{   r   r   rv   marketIdr   r   r[   r   r   parsedstoredr   s                 rg   handle_ohlcvzpoloniex.handle_ohlcv  sn   * w//tQ''""7I66##D(33!!(++!!&))__T\<DD
''<<	nv-$$T622"oodk62FFFV!4i@@~))$,dKK.u5517F#I.MM&!!!NN6;///rh   c                    |                      |dg           }t          dt          |                    D ]}||         }|                     |d          }||                     |          }|d         }d}	|	dz   |z   }
|                      | j        |          }|5|                     | j        dd          }t          |          }|| j        |<   |	                    |           |
                    ||
           |S )Nr   r   r   r   r   rV   r6   )r|   r   r   r   parse_ws_trader   r  r`   r   r   r   )re   r{   r   r   r   itemr  trader   r   r   tradesArrayrV   s                rg   handle_tradezpoloniex.handle_tradeA  s   $ w33q#d))$$ 	9 	9A7D''h77H#++D11x"TkF2"oodk6BB&"&"3"3DL-QU"V"VK",["9"9K*5DK'""5))){K888rh   c                    |                      |d          }|                     ||          }|                     |d          }|                     |dd          }|                     ||                     |dd          |                      |d          ||                     |          |                      |d          |                     |d          |                     |d	d
          ||                     |                     |dd                    |                     |                     |dd                    |                     |dd          d |                      |d          |                      |d          dd|          S )Nr   
createTime	matchRoletakerr   tradeIdorderIdr   	takerSider   
tradePricer   filledQuantityr   r   filledAmounttradeFeefeeCurrencyrater   currencyinfor   r   r   datetimer   r   r   takerOrMakerr   r   r   fee)	r   r  r  safe_string_lower_2
safe_tradesafe_string_2iso8601safe_string_lower	omit_zero)re   r  r   r  r   
takerMakers         rg   r  zpoloniex.parse_ws_tradee  s   V ##E844!!(F33%%e\::	--e['JJ
$$UD)<<&&vx88"Y//%%eY77**5&99,,UKHH&^^D$6$6ulG$T$TUUnnT%7%7?OQ[%\%\]]&&uhGG((
;; ,,UMBB  
  
$ %  	rh   c                 B    ddddddd}|                      |||          S )Nr  closedcanceled)NEWPARTIALLY_FILLEDFILLEDPENDING_CANCELPARTIALLY_CANCELEDCANCELED)r   )re   statusstatusess      rg   parse_statuszpoloniex.parse_status  s:     &$"("
 
 &&999rh   c                 `   |                      |d          }|                     |d          }|                     ||                     |d          |                     ||          ||                     |          |                     |d          |                     |d          |                     |d          |                     |d          |                     |d          |                     |d	          d d |                     |d
          |                     |d          dd|          S )N	tradeTimer   r  r  r   r   r  r   tradeAmountr#  r$  r%  r(  )r  r   r.  r  r0  r1  )re   r  r   r   r  s        rg   parse_ws_order_tradezpoloniex.parse_ws_order_trade  s6   : %%e[99	##E844""5)44&&x88"Y//%%eY77**5&99$$UF33 225+FF%%eW55&&um<<((
;; ,,UMBB  
  
$ %  	rh   c                 	   |                      |dg           }| j        }|1|                     | j        d          }t	          |          }|| _        g }t          dt          |                    D ]}|                      ||          }|                     |d          }	|                     |d          }
|	|                     |	          }|                     |d          }|                     |d          }|
dk    s|
d	k    r,| 	                    |          }|
                    |           n|                      |j        |i           }|                     |||          }|                     |          }|                     ||           |d
         g |d
<   |d
         
                    |           |d         |d<   d}d}|d
         }t          dt          |                    D ]j}||         }|                     |d                   }|                     |d                   }t!          j        ||          }t!          j        ||          }kt!          j        |d          r+|                     t!          j        ||                    |d<   |                     |          |d<   |d         |                     |d                   }|                     |d                   }t!          j        ||          }||d<   |d         F|                     |d                   }|                     t!          j        ||                    |d<   |d         d d|d         d         d|d<   |d         d         n|d         d         `|                     |d         d                   }|                     |d         d                   }t!          j        ||          |d         d<   |                     |d          }|                     |          }||d<   |
                    |           |
                    |	           t          dt          |                    D ]B}||         }	|                     |	          } | d         }d|z   }!|                    ||!           C|                    |d           |S )Nr   rW   r   r   	eventTyper  r   placer6  r   r   lastTradeTimestamp0r   r   averagefilled	remainingr,  r'  r%  stater=  zorders::r   )r|   r   r  r`   r   r   r   r   r  r   r   hashmapsafe_value_2r  handle_my_tradesry   r   
string_add	string_gtparse_number
string_div
string_subr?  r   r   )"re   r{   r   r   r   r   r   r   r   r  rE  r   r  r   r  previousOrderspreviousOrderr  	totalCosttotalAmountpreviousOrderTradesjpreviousOrderTrader   r   rB  previousOrderFilledpreviousOrderAmountstringOrderCoststringTradeCostrawStaterL  r   r   s"                                     rg   handle_orderzpoloniex.handle_order  s   H w33>%%dlMBBE+E22F DK	q#d))$$ 7	+ 7	+AOOD!,,E''x88H((<<I#))(33**5)<< $ 0 0 H H''9
+B+B!0077FMM&))))%)__V^VR%P%PN$($5$5ng}$]$]M //66E))&%888$X.624h/!(+225999:?:LM"67 #I"%K*7*A'"1c*=&>&>?? N N-@-C*#445G5OPP!%!6!67I(7S!T!T$+$6y$$G$G	&-&8f&M&M(c:: q373D3DWEWXacnEoEo3p3pi0,0,=,=i,H,HM&)$X.:&*&;&;E(O&L&L.2.C.CMRZD[.\.\+.5.@ATVa.b.b+2Eh/(2>262G2GV^H_2`2`/9=9J9J7K]^q  tG  LH  LH  :I  :IM+6$U+3$($%(-eZ(@0 0e,
 &e,V4@uU|TZG[Gg*.*?*?e@TU[@\*]*]*.*?*?eV@T*U*U7>7I/[j7k7ke,V4#//w??H --h77E.3M(+MM-000  ***q#i..)) 	0 	0A |H[[**FH%F$v-KNN6;////vx(((rh   c                    |                      |d          }|                      |d          }|                      |d          }|                      |d          }|                      |d          }|                      |d          }d }	t          j        |d          s,g }	|                     |          }
|	                    |
           |                     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          S )Nr  r   r   tsr"  rL  rH  r)  r   r   r*  rG  r   timeInForcepostOnlyr   r   	stopPricetriggerPricer   r   r   rI  remaining_sizer#  r$  r%  )rJ  rK  r=  r,  r   )	r   r   	string_eqrC  r   
safe_orderr  r0  r?  )re   r   r   r   r   r  r   r"  r=  r   r  s              rg   r   zpoloniex.parse_ws_orderS  sv   : eY//((@@##E844$$UD11	''~>>!!%11 s33 	!F--e44EMM%     
E 
d&&x88 
 " 
 ]	 

  
 Y// 
 !$ 
 D$$UF33 
 4 
  
 D$$UF33 
 T%%eW55 
  
 D 
 d&&uj99 
  D! 
" t# 
$ #))%1ABB''//((
;; ,,UMBB 
 5 
  
  
   	rh   c                    |                      |dg           }i }t          dt          |                    D ]N}||         }|                     |d          }|,|                     |          }|d         }	|| j        |	<   |||	<   O|                     |d          }
t          dt          |
                    D ]~}|
|         }|                    d          }|d         }|                    d          }|                     |d|          }| 	                    |          s|
                    ||           |
                    |d           |S )	Nr   r   r   zticker::r      r   r   )r|   r   r   r   parse_tickerr   find_message_hashessplitr   r   r   )re   r{   r   r   r   r   r  r  r   r   r   r   partssymbolsStringr   r   s                   rg   handle_tickerzpoloniex.handle_ticker  s]   . w33
q#d))$$ 	, 	,A7D''h77H#**400)'-V$%+
6"00DDq#m,,-- 	5 	5A'*K%%d++E!!HM#))#..G**:xIIG==)) 5w444z8,,,rh   c                 
   |                      |dg           }|                     |d          }|dk    }|dk    }t          dt          |                    D ]*}||         }|                     |d          }	|                     |	          }
|
d         }d}|dz   |z   }|                      |j        |i           }|                     |d	          }|                     |d
          }|                      |d          }|                      |d          }|s|ri|r|                     i |          | j        |<   | j        |         }|t          dt          |                    D ]b}|                      ||          }| 	                    |d          }| 	                    |d          }|d         }|
                    ||           c|t          dt          |                    D ]b}|                      ||          }| 	                    |d          }| 	                    |d          }|d         }|
                    ||           c||d<   ||d<   |                     |          |d<   |                    ||           ,d S )Nr   actionsnapshotupdater   r   r8   r   r   rc  asksbidsrl  r   r*  )r|   r   r   r   r  r}   r  
order_book
orderbooksr   storer0  r   )re   r{   r   r   r   ru  rv  r   r  r  r   r   r7   r   subscriptionr   r   rw  rx  r   rZ  bidr   r   bidsSideaskasksSides                              rg   handle_order_bookzpoloniex.handle_order_book  s   ` w3322:%!q#d))$$ !	7 !	7A7D''h77H%%h//FH%FD+.K??6+?bQQL%%lG<<E))$55I??400D??400D 76 7 I.2oob%.H.HDOF+ OF3	#"1c$ii00 6 6"oodA66 $ 0 0a 8 8!%!1!1#q!9!9#,V#4 uf5555#"1c$ii00 6 6"oodA66 $ 0 0a 8 8!%!1!1#q!9!9#,V#4 uf5555&,	(#)2	+&(,Y(?(?	*%y+666C!	7 !	7rh   c                     |                      |dg           }d}|                     |          | _        |                    | j        |           d S )Nr   r   )r|   parse_ws_balancebalancer   )re   r{   r   r   r   s        rg   handle_balancezpoloniex.handle_balance  sO    ( w33 ,,T22t|[11111rh   c                    |                      |di           }|                     |d          }|||                     |          d}t          dt	          |                    D ]}|                      ||          }|                     |d          }|                     |          }|                                 }	|                     |d          |	d<   |                     |d          |	d<   |	||<   |                     |          S )	Nr   rc  )r)  r   r*  r'  	availablefreeholdused)	r|   r  r0  r   r   r   safe_currency_codeaccountsafe_balance)
re   responsefirstBalancer   resultr   r  
currencyIdcode
newAccounts
             rg   r  zpoloniex.parse_ws_balance5  s   " xB77%%lD99	"Y//
 

 q#h--(( 	& 	&Aooh22G))':>>J**:66DJ!%!1!1';!G!GJv!%!1!1'6!B!BJv%F4LL  (((rh   c                 (   d}|d         }| j         0|                     | j        dd          }t          |          | _         | j         }|                    |           |                    ||           |dz   |z   }|                    ||           d S )Nr   r   rV   r6   :)r   r  r`   r   r   r   )re   r{   parsedTrader   r   r   r   symbolMessageHashs           rg   rO  zpoloniex.handle_my_tradesW  s     X&= %%dlM4HHE2599DMk"""v{+++'#-6v011111rh   c                 8    |                                  |_        d S N)rz   lastPongre   r{   s     rg   handle_pongzpoloniex.handle_pongd  s    ++--rh   c           	      J   |                      ||          rd S |                     |d          }|                     |d          }|dk    r|                                 |_        i d| j        d| j        d| j        d| j        d| j        d	| j        d
| j        d| j        d| j        d| j        d| j        d| j        d| j        d| j        d| j        d| j        d| j        | j        | j        | j	        | j
        | j
        | j
        | j        d}|                     ||          }|dk    r|                     ||           d S || 
                    ||           d S |                     |dg           }t          |          }|dk    r |||           d S d S )Nrv   ru   pongr9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   bookr8   r   )r   r   r   r   cancelOrderr   rm   rm   r   r   )handle_error_messager   rz   r  r  r  rr  r  ra  r  r   handle_authenticater|   r   )	re   r{   r   r   ru   methodsr   r   
dataLengths	            rg   handle_messagezpoloniex.handle_messageg  sE   $$VW55 	F33  '22F??"//11FO
 1
 1
  !2
  !2	

  !2
 d/
 d/
 d/
 d/
 t0
 T.
 T.
 d/
 t0
 D*
  .!
" d(#
$ ''+44#8,1
 
 
4 $//6>>$$VW55555\%%fg66666??7FB77DTJA~~vw''''' ~rh   c                    |                      |d          }|                      |d          }|                     |d          }|                     |d          }|                      |d          }|dk    s|dk    r	 |                      |d          }|                      |d	          }	| j        d
z   |                     |          z   }
|                     | j        d         |	|
           |                     | j        d         ||
           t          |
          # t          $ r`}t          |t                    r*d}|                    ||           ||j        v r|j        |= n|                    ||           Y d }~dS d }~ww xY wdS )Nr   ru   r   r   r  errorrH  r   r   exactbroadrj   TF)r   	safe_listr   r   jsonthrow_exactly_matched_exception
exceptionsthrow_broadly_matched_exceptionr   	Exception
isinstancer   rejectr}   )re   r{   r   r   ru   r   r   r  r  r  feedbacker   s                rg   r  zpoloniex.handle_error_message  s   < gt,,  '22~~gv..tQ''""5)44W'S..((	::''v667S=499W+=+==44T_W5MtU]^^^44T_W5MuV^___#H---   a!455 )"1KMM![111"f&:::"0=MM!R(((ttttt us   =BD 
F&AFFc                 <   |                      |d          }|                      |d          }d}|r|                    ||           nTt          | j        dz   |                     |          z             }|                    ||           ||j        v r|j        |= |S )Nr   successrj   r  )r|   r   r   r   r  r  r}   )re   r{   r   r   r  r   r  s          rg   r  zpoloniex.handle_authenticate  s     w////$	22% 	6NN7K0000'#		'8J8J(JKKEMM%---f222(5rh   c                 
    ddiS )Nru   r]    r  s     rg   r]   zpoloniex.ping  s    V
 	
rh   r  )<__name__
__module____qualname__r   rd   r   r   boolr   rl   r   r   r   floatr
   r   r   r   r   r   r   r   r   r   r	   listr   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r  r  r?  rC  ra  r   rr  r  r  r  rO  r  r  r   r  r  r]   __classcell__)rf   s   @rg   r   r      sD       ?# ? ? ? ? ? ?B )+ 5 5 5 5n `dln H HC Hc Hd HU\ H H H H< 57 J J J J J J" pt|~ = =C =y =	 =[` =il =  DI = = = =~ <@   S    ( DHPR V V$s) VS V V V V& 8<B C C C4PU; C C C C,6 , , , ,* ?CQUdhqs H H H H3 H^a Hx|  ~B  yC H H H H. 68 0 0 0F 0 0 0 0 6:" E E7 E E E E E$ <@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H @DB ! !S ! !S\ ! ! ! !$ 04$UYbd S S SC Ss Simnsit S S S S. 37TX\eg S SC Ss SRU Slpqvlw S S S S0 *, D D D D D D
 
D 
 
 
 
2(6 ( ( ( (T"6 " " " "HA A A AF
: 
: 
:1 1 1 1fj6 j j j jXC C C CJ+F + + + +ZU7 U7 U7 U7 U7n2V 2 2 2 22 )  )  )D2v 2 2 2 2.& . . . .*(V *( *( *( *(X46 4t 4 4 4 4l&    *
6 
 
 
 
 
 
 
 
rh   r   )#ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   ccxt.base.typesr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   ccxt.base.preciser   async_supportr   r  rh   rg   <module>r     s       f f f f f f f f f f  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H  H 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 ' ' ' ' ' ' ) ) ) ) ) ) % % % % % %N
 N
 N
 N
 N
t!* N
 N
 N
 N
 N
rh   