
    i$                         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 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)BalancesIntNumOrder	OrderBook	OrderSide	OrderTypeStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)
BadRequest)InvalidOrder)Precisec                       e Zd Z 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fdZd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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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%d5defd#Z&defd$Z'defd%Z(d5d&Z)d' Z*d5d(Z+defd)Z,d5d*Z-defd+Z.defd,Z/defd-Z0d. Z1defd/Z2defd0Z3defd1Z4defd2Z5defd3Z6d4 Z7 xZ8S )6poloniexc                 F   |                      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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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   describerY   )self	__class__s    W/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/poloniex.pyr`   zpoloniex.describe   s   h 5 5 > > @ @dd !$ t	
  t  u t     u $U     !$!" $T#$ "'"''  , "C#E  6:##"J# %)!$,,...****,))*+  8 #	 s=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
        :see: https://docs.poloniex.com/#authenticated-channels-market-data-authentication
        :returns dict: response from exchange
        r/   r   r1   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)ra   rs   	timestampurlmessageHashrw   future
accessPathrequestStringrn   requestmessages               rc   authenticatezpoloniex.authenticateU   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 -rd   Nr3   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 boolean isPrivate: True for the authenticated url, False for the public url
        :param str[]|None symbols: CCXT market symbols
        :param dict [params]: extra parameters specific to the poloniex api
        :returns dict: data from the websocket stream
        r1   r0   r/   r   rh   )rq   rr   all::,balancesr   N)r[   is_emptyappendjoin
market_idsr   r   )ra   r3   r   r   r   rs   publicOrPrivater   rh   	marketIdsr   s              rc   rh   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Grd   c                    K   | j         d         d         d         }t          |                                           }|||d}|                     ||||           d{V S )a*  
         * @ignore
        Connects to a websocket channel
        :param str name: name of the channel
        :param str[]|None symbols: CCXT market symbols
        :param dict [params]: extra parameters specific to the poloniex api
        :returns dict: data from the websocket stream
        r/   r   r1   )idrq   rs   N)r[   strnoncer   )ra   r3   rs   r   r   rh   s         rc   trade_requestzpoloniex.trade_request   st       it$Y/$**,,''
 
	
 ZZ[)[IIIIIIIIIrd   symboltypesideamountpricereturnc                 \  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 S )a  
        :see: https://docs.poloniex.com/#authenticated-channels-trade-requests-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 fullfilled, 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createOrderrQ   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   )load_marketsr   marketupperis_post_onlyhandle_option_and_paramssafe_numberomitcost_to_precisionr   r   ru   r   
string_mulamount_to_precisionprice_to_precisionr   r   )ra   r   r   r   r   r   rs   r   uppercaseTypeuppercaseSide
isPostOnlyr   quoteAmountrQ   r   amountStringpriceStringcostRequests                     rc   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 ''t{{7F7S7STTTTTTTTTrd   r   c                    K   |                      |d          }|1|                     |dg           }|                     ||g          |d<   |                     |g||           d{V S )a  
        :see: https://docs.poloniex.com/#authenticated-channels-trade-requests-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clientOrderIds)safe_stringrx   array_concatcancel_orders_ws)ra   r   r   rs   r   r   s         rc   cancel_order_wszpoloniex.cancel_order_ws   s       ((AA$!__V_bIIN'+'8'8-'Y'YF#$**B4@@@@@@@@@rd   idsc                    K   |                                   d{V  |                                  d{V  d|i}|                     d|                     ||                     d{V S )a  
        :see: https://docs.poloniex.com/#authenticated-channels-trade-requests-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   )ra   r   r   rs   r   s        rc   r   zpoloniex.cancel_orders_ws  s       !!!!!!!!!!!!!!!!!!
 ''GV8T8TUUUUUUUUUrd   c                    K   |                                   d{V  |                                  d{V  |                     d|           d{V S )a.  
        :see: https://docs.poloniex.com/#authenticated-channels-trade-requests-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   )ra   r   rs   s      rc   cancel_all_orders_wszpoloniex.cancel_all_orders_ws  sy       !!!!!!!!!!!!!!!!!!''(96BBBBBBBBBrd   rw   c                 4   |                      |d          }|                     |dg           }g }t          dt          |                    D ]4}||         }|                     |          }|                    |           5|                    ||           d S )Nr   datar   )safe_integerrx   rangelenparse_ws_orderr   resolve)	ra   rw   r   r   r   ordersiorderparsedOrders	            rc   handle_order_requestzpoloniex.handle_order_request!  s     ''66w33q#d))$$ 	' 	'AGE--e44KMM+&&&&v{+++++rd   rC   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
        :see: https://docs.poloniex.com/#public-channels-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
        NrW   z' watchOHLCV cannot take a timeframe of Fr   T)
r   rx   r\   r   r   r   rh   
newUpdatesgetLimitfilter_by_since_limit)	ra   r   	timeframer   r   rs   rW   rr   ohlcvs	            rc   watch_ohlcvzpoloniex.watch_ohlcv6  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Grd   c                    K   |                                   d{V  |                     |          }|                     |g|           d{V }|                     ||          S )  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :see: https://docs.poloniex.com/#public-channels-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_tickersrx   )ra   r   rs   tickerss       rc   watch_tickerzpoloniex.watch_tickerK  sz       !!!!!!!!!V$$**F8V<<<<<<<<w///rd   c                    K   |                                   d{V  d}|                     |          }|                     ||d||           d{V }| j        r|S |                     | j        d|          S )r   NtickerFr   )r   market_symbolsrh   r   filter_by_arrayr   )ra   r   rs   r3   
newTickerss        rc   r   zpoloniex.watch_tickersX  s       !!!!!!!!!%%g..>>$eWfMMMMMMMM
? 	##DL(GDDDrd   c                   K   |                                   d{V  |                     |          }d}|                     ||d|g|           d{V }| j        r|                    ||          }|                     |||dd          S )a(  
        get the list of most recent trades for a particular symbol
        :see: https://docs.poloniex.com/#public-channels-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tradesFr   T)r   r   rh   r   r   r   )ra   r   r   r   rs   r3   r   s          rc   watch_tradeszpoloniex.watch_tradesh  s       !!!!!!!!!V$$~~dD%&6JJJJJJJJ? 	3OOFE22E))&%TRRRrd   c                 <  K   |                                   d{V  |                     | j        d          }|                     |dd          }|                     |dd|          \  }}|                     ||d|g|           d{V }|                                S )a6  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :see: https://docs.poloniex.com/#public-channels-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   r3   r4   methodF)r   rx   r\   r   r   rh   r   )ra   r   r   rs   watchOrderBookOptionsr3   	orderbooks          rc   watch_order_bookzpoloniex.watch_order_bookz  s       !!!!!!!!! $>N O O 5vzJJ44VXvtTTf..tUVHfMMMMMMMM	   rd   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
        :see: https://docs.poloniex.com/#authenticated-channels-market-data-orders
        :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   rh   r   r   r   )ra   r   r   r   rs   r3   r   r   s           rc   watch_orderszpoloniex.watch_orders  s       !!!!!!!!!!!!!!!!!![[((F!>$$~~dD$HHHHHHHH? 	3OOFE22E))&%TRRRrd   c                 \  K   |                                   d{V  d}d}|                                  d{V  ||                     |          }|dn|g}|                     ||d||           d{V }| j        r|                    ||          }|                     |||dd          S )a4  
        watches information on multiple trades made by the user using orders stream
        :see: https://docs.poloniex.com/#authenticated-channels-market-data-orders
        :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   )	ra   r   r   r   rs   r3   r   r   r   s	            rc   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Rrd   c                    K   |                                   d{V  d}|                                  d{V  |                     ||dd|           d{V S )ar  
        watch balance and get the amount of funds available for trading or funds locked in orders
        :see: https://docs.poloniex.com/#authenticated-channels-market-data-balances
        :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   rh   )ra   rs   r3   s      rc   watch_balancezpoloniex.watch_balance  s       !!!!!!!!!!!!!!!!!!^^D$dFCCCCCCCCCrd   c           	         |                      |d          |                     |d          |                     |d          |                     |d          |                     |d          |                     |d          gS )N	startTimeopenhighlowcloser   )r   r   )ra   r   r   s      rc   parse_ws_ohlcvzpoloniex.parse_ws_ohlcv  s    " e[11UF++UF++UE**UG,,UJ//
 	
rd   c                    |                      |d          }|                      |d          }|                     |d          }|                     |d          }|                     |          }|                     |          }|                      | j        di           }|                     ||          }	|dz   |z   }
|                     ||          }|                      | j        |i           | j        |<   |                      | j        |         |	          }|h|;|                     | j        dd          }t          |          }|| j        |         |	<   |
                    |           |                    ||
           |S )	Nr   r   rr   r   rW   r   rT   r2   )rx   r   safe_symbolsafe_marketr\   find_timeframer   ohlcvsr   r   r   r   )ra   rw   r   r   rr   marketIdr   r   rW   r   r   parsedstoredr   s                 rc   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;///rd   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   rR   r2   )rx   r   r   r   parse_ws_trader   r   r\   r   r   r   )ra   rw   r   r   r   itemr  trader   r   r   tradesArrayrR   s                rc   handle_tradezpoloniex.handle_trade  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rd   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safe_number_2)ra   r  r   r  r   
takerMakers         rc   r  zpoloniex.parse_ws_trade(  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  
  
$ %  	rd   c                 B    ddddddd}|                      |||          S )Nr   closedcanceled)NEWPARTIALLY_FILLEDFILLEDPENDING_CANCELPARTIALLY_CANCELEDCANCELED)r   )ra   statusstatusess      rc   parse_statuszpoloniex.parse_statusk  s:     &$"("
 
 &&999rd   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  r'  r(  )ra   r  r   r   r  s        rc   parse_ws_order_tradezpoloniex.parse_ws_order_tradew  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  
  
$ %  	rd   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   rS   r   r   	eventTyper  r   placer.  r   r   lastTradeTimestamp0r   r   averagefilled	remainingr#  r  r  stater5  zorders::r   )rx   r   r   r\   r   r   r   r   r  r   r   hashmapsafe_value_2r  handle_my_tradesru   r   
string_add	string_gtparse_number
string_div
string_subr7  r   r   )"ra   rw   r   r   r   r   r   r   r   r  r=  r   r  r   r  previousOrderspreviousOrderr  	totalCosttotalAmountpreviousOrderTradesjpreviousOrderTrader   r   r:  previousOrderFilledpreviousOrderAmountstringOrderCoststringTradeCostrawStaterD  r   r   s"                                     rc   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(((rd   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  rD  r@  r   r   r   r!  r?  r   timeInForcepostOnlyr   r   	stopPricetriggerPricer   r   r   rA  remaining_sizer  r  r  )rB  rC  r5  r#  r   )	r   r   	string_eqr;  r   
safe_orderr  r'  r7  )ra   r   r   r   r   r  r   r  r5  r   r  s              rc   r   zpoloniex.parse_ws_order  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 
  
  
   	rd   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   )rx   r   r   r   parse_tickerr   find_message_hashessplitr   r   r   )ra   rw   r   r   r   r   r  r  r   r   messageHashesr   partssymbolsStringr   r   s                   rc   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,,,rd   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   r4   r   r   r[  asksbidsrd  r   r!  )rx   r   r   r   r  ry   r   
order_book
orderbooksr   storer'  r   )ra   rw   r   r   r   rn  ro  r   r  r  r   r   r3   r   subscriptionr   r   rp  rq  r   rR  bidr   r   bidsSideaskasksSides                              rc   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 !	7rd   c                     |                      |dg           }d}|                     |          | _        |                    | j        |           d S )Nr   r   )rx   parse_ws_balancebalancer   )ra   rw   r   r   r   s        rc   handle_balancezpoloniex.handle_balance  sO    ( w33 ,,T22t|[11111rd   c                    |                      |di           }|                     |d          }|||                     |          d}t          dt	          |                    D ]}|                      ||          }|                     |d          }|                     |          }|                                 }	|                     |d          |	d<   |                     |d          |	d<   |	||<   |                     |          S )	Nr   r[  )r   r   r!  r  	availablefreeholdused)	rx   r   r'  r   r   r   safe_currency_codeaccountsafe_balance)
ra   responsefirstBalancer   resultr   r}  
currencyIdcode
newAccounts
             rc   r|  zpoloniex.parse_ws_balance  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  (((rd   c                 (   d}|d         }| j         0|                     | j        dd          }t          |          | _         | j         }|                    |           |                    ||           |dz   |z   }|                    ||           d S )Nr   r   rR   r2   :)r   r   r\   r   r   r   )ra   rw   parsedTrader   r   r   r   symbolMessageHashs           rc   rG  zpoloniex.handle_my_trades  s     X&= %%dlM4HHE2599DMk"""v{+++'#-6v011111rd   c                 8    |                                  |_        d S N)rv   lastPongra   rw   s     rc   handle_pongzpoloniex.handle_pong'  s    ++--rd   c           	      
   |                      ||          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 |x|                     |d          }|                     |d          }|                     |d          }	|	dk    r|                      ||           d S | 
                    ||           d S |                     |dg           }t          |          }
|
dk    r |||           d S d S )Nrr   rq   pongr5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   bookr4   r   )r   r   r   r   cancelOrderr   ri   ri   r   r   r  r@  )handle_error_messager   rv   r  r	  rz  rk  r  rY  r~  r   handle_authenticaterx   r   )ra   rw   r   r   rq   methodsr   r   r  r  
dataLengths              rc   handle_messagezpoloniex.handle_message*  s   $$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\??7F33D??4++D&&tY77G#~~))&$77777))&':::::??7FB77DTJA~~vw''''' ~rd   c                     |                      |d          }|                      |d          }|dk    s|dk    rC|                      |d          }t          | j        dz   |                     |          z             dS )Nrq   r  errorr@  r   z error: F)r   r   r   json)ra   rw   r   rq   r  r  s         rc   r  zpoloniex.handle_error_message\  s    &   '22""7I66W'S..$$Wi88E* 4tyy7G7G GHHHurd   c                 <   |                      |d          }|                      |d          }d}|r|                    ||           nTt          | j        dz   |                     |          z             }|                    ||           ||j        v r|j        |= |S )Nr   successrf    )rx   r   r   r   r  rejectry   )ra   rw   r   r   r  r   r  s          rc   r  zpoloniex.handle_authenticatev  s     w////$	22% 	6NN7K0000'#		'8J8J(JKKEMM%---f222(5rd   c                 
    ddiS )Nrq   rY    r  s     rc   rY   zpoloniex.ping  s    V
 	
rd   r  )9__name__
__module____qualname__r`   r   r   boolr   rh   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  r7  r;  rY  r   rk  rz  r~  r|  rG  r  r  r  r  rY   __classcell__)rb   s   @rc   r   r      s       > > > > >@ )+ 3 3 3 3j `dln H HC Hc Hd HU\ H H H H: 57 J J J J J J$ pt|~ 9U 9UC 9Uy 9U	 9U[` 9Uil 9U  DI 9U 9U 9U 9Uv <@ A A AS A A A A  DHPR V V$s) VS V V V V" 8<B 
C 
C 
C 
C 
C 
C,6 , , , ,* 8<$]ajl H H HC HWZ Hquvzq{ 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$ @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, *, 
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.& . . . .0(V 0( 0( 0( 0(d6    4&    *
 
 
 
 
 
 
rd   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   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   ccxt.base.preciser   async_supportr   r  rd   rc   <module>r     sP       f f f f f f f f f f  | | | | | | | | | | | | | | | | | | | | | | | | | | | | 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 ' ' ' ' ' ' ) ) ) ) ) ) % % % % % %{
 {
 {
 {
 {
t!* {
 {
 {
 {
 {
rd   