
    ei                         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 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IntOrder	OrderBookStrStringsTickerTickersTrade)Client)List)AuthenticationError)Precisec                       e Zd Zdef fdZd Zd(dZd(dZd(dZd Z	d(d	Z
d(d
ZdefdZi fdefdZd ZdefdZdefdZi fdedefdZdi fdedefdZd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dee         f
dZ d(dZ!d(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di fde$dededee'         fd!Z(defd"Z)d(d#Z*defd$Z+defd%Z,i fd&Z-i fd'Z. xZ/S ))phemexreturnc                     |                      t          t          |                                           ddddddddd dddddddiddidddd	d
did          S )NTF)wswatchTickerwatchTickerswatchTradeswatchMyTradeswatchOrderswatchOrderBook
watchOHLCVwatchPositionswatchOrderBookForSymbolswatchTradesForSymbolswatchOHLCVForSymbolswatchBalancer   zwss://testnet-api.phemex.com/wszwss://ws.phemex.com)testapi  )tradesLimit
OHLCVLimit	keepAlivei(#  )hasurlsoptions	streaming)deep_extendsuperr   describe)self	__class__s    M/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/phemex.pyr1   zphemex.describe   s    fd 3 3 < < > ># $#!%#"&""&,1).(- $ $ ; /	   $" 
 T; A
  A
      	    c                     |d S t          |          }|                     |j        |          |_        |                                 t	          |          S N)r   sumdecimalsreducestr)r2   enscaleprecises       r4   from_enzphemex.from_en5   sL    :4"++88G$4e<<7||r5   Nc                 b    |||S |                      ||                     |d                    S )N
priceScaler?   safe_integer)r2   epmarkets      r4   from_epzphemex.from_ep=   4    JFNI||B 1 1&, G GHHHr5   c                 b    |||S |                      ||                     |d                    S )N
valueScalerB   )r2   evrE   s      r4   from_evzphemex.from_evB   rG   r5   c                 b    |||S |                      ||                     |d                    S )N
ratioScalerB   )r2   errE   s      r4   from_erzphemex.from_erG   rG   r5   c                     |                                   |                     |                     | j        dd          d          }|| j        d<   |                                  |S )N	requestIdr      )lock_idr8   rC   r-   	unlock_id)r2   rQ   s     r4   
request_idzphemex.request_idL   sX    HHT..t|[!LLaPP	$-[!r5   c                    |                      |d          }|                     ||          }|d         }|                     |dd          }|                     |                      |d          |          }|                     |          }|                     |                     |                      |d          |                    }|                     |                     |                      |d          |                    }	d }
d }d }|                     |                     |                      |d          |                    }|                     |          }|||                     t          j        ||                    }
|                     t          j	        t          j
        ||          d                    }|                     t          j        t          j        t          j	        ||          d	          d
                    }|                     i d|d|d|                     |          d|                     |                     |                      |d          |                    d|                     |                     |                      |d          |                    dd dd dd dd dd d|d|d|dd d|
d|d||	||                     |                     |                      |d          |                    |                     |                     |                      |d          |                    |d          S )Nsymbol	timestampư>closeturnovervolumeopen21100datetimehighlowbid	bidVolumeask	askVolumevwaplastpreviousClosechange
percentageaverage	markPrice
indexPrice)
baseVolumequoteVolumern   ro   info)safe_stringsafe_marketsafe_integer_productrF   parse_numberrK   	omit_zeror   
string_sub
string_div
string_add
string_mulsafe_tickeriso8601)r2   tickerrE   marketIdrW   rX   
lastStringri   rq   rp   rk   rl   rm   
openStringr]   s                  r4   parse_swap_tickerzphemex.parse_swap_tickerS   s   " ##FH55!!(F33!--fk8LL	\\$"2"267"C"CVLL
  ,,''T5E5Efj5Y5Y[a(b(bcc&&t||D4D4DVX4V4VX^'_'_``

^^DLL1A1A&&1Q1QSY$Z$Z[[
  ,,")?&&w'9*j'Q'QRRF''(:7;MjZd;e;egj(k(kllG**7+=g>PQXQcdnpzQ{Q{  ~A  ?B  ?B  DI  ,J  ,J  K  KJ !
f!
!
 Y//!
 D%%dll43C3CFF3S3SU[&\&\]]	!

 4$$T\\$2B2B652Q2QSY%Z%Z[[!
 4!
 !
 4!
 !
 D!
 D!
 T!
 D!
 T!
 f!
  *!!
" w#!
$ %&**4<<8H8HQ\8]8]_e+f+fgg++DLL9I9I&R^9_9_ag,h,hii-!
 !
 !
   	r5   c                 ~   |                      |d          }|                     ||          }|d         }|                     |                      |d          |          }|                     |          }|                     |                     |                      |d          |                    }|                     |                     |                      |d          |                    }d }	d }
d }|                     |                     |                      |d          |                    }|                     |          }|||                     t          j        ||                    }	|                     t          j        t          j	        ||          d                    }|                     t          j
        t          j        t          j        ||          d          d	                    }
|                     i d|d
d dd d|                     |                     |                      |d          |                    d|                     |                     |                      |d          |                    dd dd dd dd dd d|d|d|dd d|	d|
d||||d          S )Nr   rW            rR   r^   r_   r`   rX   ra   rb      rc      rd   re   rf   rg   rh   r]   rZ   ri   rj   rk   rl   rm   )rp   rq   rr   )rs   rt   rF   rv   rK   rw   r   rx   ry   rz   r{   r|   )r2   r~   rE   r   rW   r   ri   rq   rp   rk   rl   rm   r   r]   s                 r4   parse_perpetual_tickerzphemex.parse_perpetual_ticker   s.   " ##FA..!!(F33!\\$"2"261"="=vFF
  ,,''T5E5Efa5P5PRX(Y(YZZ&&t||D4D4DVQ4O4OQW'X'XYY

^^DLL1A1A&!1L1Lf$U$UVV
  ,,")?&&w'9*j'Q'QRRF''(:7;MjZd;e;egj(k(kllG**7+=g>PQXQcdnpzQ{Q{  ~A  ?B  ?B  DI  ,J  ,J  K  KJ !
f!
!
 !
 D%%dll43C3CFA3N3NPV&W&WXX	!

 4$$T\\$2B2B612M2Mv%V%VWW!
 4!
 !
 4!
 !
 D!
 D!
 T!
 D!
 T!
 f!
  *!!
" w#!
$ %&)!
 !
 !
   	r5   clientc                    g }d|v r?|                      |d          }|                    |                     |                     nd|v r?|                      |d          }|                    |                     |                     nid|v re|                      |dg           }t	          dt          |                    D ]0}|                    |                     ||                              1t	          dt          |                    D ]k}||         }|d         }d|z   }|                     |dd          }	|	|d<   |                     |	          |d	<   || j	        |<   |
                    ||           ld S )
N	market24hspot_market24hdatar   rW   ticker:rX   rY   ra   )
safe_valueappendr   parse_tickerrangelenr   ru   r}   tickersresolve)
r2   r   messager   r~   r   irW   messageHashrX   s
             r4   handle_tickerzphemex.handle_ticker   s   X '!!__Wk::FNN411&99::::((__W.>??FNN4,,V445555w??7FB77D1c$ii(( E Et::47CCDDDDq#g,,'' 	0 	0AQZFH%F#f,K11';QQI"+F;!%i!8!8F:#)DL NN6;////	0 	0r5   c                    K   |                                   d{V  d}|                     dd|          \  }}|                     |d          dk    }d}|rd|z   n||z   }|                     |||           d{V S )a  

        https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-account-order-position-aop
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-account-order-position-aop
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Spot-API-en.md#subscribe-wallet-order-messages

        watch balance and get the amount of funds available for trading or funds locked in orders
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.settle]: set to USDT to use hedged perpetual api
        :returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
        Nr$   settleUSDT:balance	perpetual)load_marketshandle_market_type_and_paramsrs   subscribe_private)r2   paramstypeusePerpetualApir   s        r4   watch_balancezphemex.watch_balance'  s       !!!!!!!!!99.$PVWWf**68<<F 3BZkK//{HZ++D+vFFFFFFFFFr5   c                    || j         d<   t          dt          |                    D ]}}||         }|                     |d          }|                     |          }|                     | j        |i           }|                     |dd          }	|                                 }
|                     |d          }|p|                     |d          }|B|                     |d          }| 	                    |d	d
          }t          j        ||          }|                     ||	          }|                     |d          }|-| 	                    |dd          }|                     ||	          }||
d<   ||
d<   |
| j         |<   |                     | j                   | _         |dz   }|                    | j         |           d S )Nrr   r   currencyrI      totalUsedBalanceRvtotalUsedBalanceEvlockedTradingBalanceEvlockedWithdrawEvlockedWithdrawRvaccountBalanceRvaccountBalanceEv	balanceEvusedtotalr   )balancer   r   rs   safe_currency_coder   
currenciesrC   accountsafe_string_2r   rz   r?   safe_balancer   )r2   r   r   r   r   r   
currencyIdcoder   r=   r   r   usedEvr   r   r   totalEvr   s                     r4   handle_balancezphemex.handle_balance;  s   V  'Vq#g,,'' 	; 	;AajG))':>>J**:66DtbAAH%%ha@@EllnnG##G-ABBD|))'3GHH>-1-=-=gG_-`-`*'+'9'9'CUWi'j'j$$/0FHXYYF||FE22$$W.@AAE},,W6H+VVWe44"GFO$GG!(DL,,T\::DLLZ't|[11111r5   c                 "   d}|                      |d          }|                     |          }|d         }|dz   |z   }|                     | j        |          }|5|                     | j        dd          }	t          |	          }|| j        |<   |                     |ddg           }
|                     |
|          }t          dt          |                    D ]}|                    ||                    |                    ||           d S )	NtraderW   :r(   r'   tradestrades_pr   )rs   rt   r   r   rC   r-   r   safe_value_2parse_tradesr   r   r   r   )r2   r   r   namer   rE   rW   r   storedlimitr   parsedr   s                r4   handle_tradeszphemex.handle_trades  s   6 ##GX66!!(++!Sj6)f55>%%dlM4HHE&&F"(DK""7Hj"EE""6622q#f++&& 	% 	%AMM&)$$$$v{+++++r5   c                    |                      |d          }|                     |          }|d         }|                     |ddg           }|                     |dg           }|                      |d          }|                     |          }	|	d|	z   dz   |z   }
|                     ||          }|                     | j        |i           | j        |<   |                     | j        |         |	          }|;|                     | j        dd	          }t          |          }|| j        |         |	<   t          dt          |                    D ]}||         }|                    |            |                    ||
           d S d S )
NrW   klinekline_pr   rR   kline:r   r)   r'   )rs   rt   r   r   find_timeframeparse_ohlcvsohlcvsrC   r-   r   r   r   r   r   )r2   r   r   r   rE   rW   candlesfirstinterval	timeframer   r   r   r   r   candles                   r4   handle_ohlcvzphemex.handle_ohlcv  s   @ ##GX66!!(++!##GWiDDB//##E1--''11	 "Y.4v=K&&w77F"&//$+vr"J"JDK__T[%8)DDF~))$,dKK.u5517F#I.1c&kk** & &f%%%%NN6;///// ! r5   rW   c                   K   |                                   d{V  |                     |          }|d         }|d         }|d         dk    }d}|r|rdnd}| j        d	         d
         }|                                 }|dz   }	d|z   }
|	|g d}|                     ||          }|                     ||
||	           d{V S )a  

        https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-24-hours-ticker
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-24-hours-ticker
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Spot-API-en.md#subscribe-24-hours-ticker

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :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   swapr   r   r   perp_market24h_pack_pr   r&   r   
.subscriber   methodidr   )r   rE   r,   rU   r/   watch)r2   rW   r   rE   isSwapsettleIsUSDTr   urlrQ   subscriptionHashr   	subscriberequests                r4   watch_tickerzphemex.watch_ticker  s	      !!!!!!!!!V$$!h'61 	L.:K**Dit$OO%%	,.&(&
 
	
 ""9f55ZZ[';KLLLLLLLLLr5   symbolsc                   K   |                                   d{V  |                     |dd          }|d         }|                     |          }|d         }|d         dk    }d}|r|rdnd	}| j        d
         d         }|                                 }	|dz   }
g }t          dt          |                    D ] }|                    d||         z              !|
|	g d}|                     ||          }| 	                    ||||           d{V }| j
        ri }|||d         <   |S |                     | j        d|          S )a  

        https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-24-hours-ticker
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-24-hours-ticker
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Spot-API-en.md#subscribe-24-hours-ticker

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list
        :param str[] [symbols]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.channel]: the channel to subscribe to, tickers by default. Can be tickers, sprd-tickers, index-tickers, block-tickers
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        NFr   r   r   r   r   r   r   r&   r   r   r   r   rW   )r   market_symbolsrE   r,   rU   r   r   r   r/   watch_multiple
newUpdatesfilter_by_arrayr   )r2   r   r   r   rE   r   r   r   r   rQ   r   messageHashesr   r   r   r~   results                    r4   watch_tickerszphemex.watch_tickers  s      !!!!!!!!!%%gtU;;
U##h'61 	L.:K**Dit$OO%%	,.q#g,,'' 	9 	9A  WQZ!78888&
 
	
 ""9f55**3wVVVVVVVV? 	F'-F6(#$M##DL(GDDDr5   sincer   c                   K   |                                   d{V  |                     |          }|d         }| j        d         d         }|                                 }|d         }|d         dk    }	|r|	rdnd	}
d
|z   }|
dz   }|||d         gd}|                     ||          }|                     ||||           d{V }| j        r|                    ||          }|                     |||dd          S )a  

        https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-trade
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-trade
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Spot-API-en.md#subscribe-trade

        get the list of most recent trades for a particular symbol
        :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>`
        NrW   r&   r   r   r   r   trade_pr   ztrade:r   r   r   rX   T)	r   rE   r,   rU   r/   r   r   getLimitfilter_by_since_limit)r2   rW   r   r   r   rE   r   rQ   r   r   r   r   r   r   r   r   s                   r4   watch_tradeszphemex.watch_trades*  s=      !!!!!!!!!V$$!it$OO%%	h'61#BByy7'$t
 
	 ""9f55zz#{G[IIIIIIII? 	3OOFE22E))&%TRRRr5   c                   K   |                                   d{V  |                     |          }|d         }| j        d         d         }|                                 }|d         }|d         dk    }|r|rdnd	}	d
|z   }
|	dz   }|||d         gd}|                     ||          }|                     ||
||
           d{V }|                                S )a  

        https://github.com/phemex/phemex-api-docs/blob/master/Public-Spot-API-en.md#subscribe-orderbook
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-orderbook-for-new-model
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-30-levels-orderbook
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-full-orderbook

        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :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
        NrW   r&   r   r   r   r   orderbook_p	orderbookz
orderbook:r   r   r   )r   rE   r,   rU   r/   r   r   )r2   rW   r   r   rE   r   rQ   r   r   r   r   r   r   r   r   s                  r4   watch_order_bookzphemex.watch_order_bookO  s      !!!!!!!!!V$$!it$OO%%	h'61!'JLJ}}{"V+$t
 
	 ""9f55**S+wLLLLLLLL	   r5   1mr   c                 <  K   |                                   d{V  |                     |          }|d         }| j        d         d         }|                                 }|d         }	|d         dk    }
|	r|
rdnd	}d
|z   dz   |z   }|dz   }|||d         |                     | j        |          gd}|                     ||          }|                     ||||           d{V }| j        r|	                    ||          }| 
                    |||dd          S )a  

        https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-kline
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-kline
        https://github.com/phemex/phemex-api-docs/blob/master/Public-Spot-API-en.md#subscribe-kline

        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
        :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   r&   r   r   r   r   r   r   r   r   r   r   r   r   T)r   rE   r,   rU   rC   
timeframesr/   r   r   r   r   )r2   rW   r   r   r   r   rE   r   rQ   r   r   r   r   r   r   r   ohlcvs                    r4   watch_ohlcvzphemex.watch_ohlcvr  s\      !!!!!!!!!V$$!it$OO%%	h'61#BByy7*S069$t!!$/9==
 
	 ""9f55jjk7KHHHHHHHH? 	2NN6511E))%q$GGGr5   c                 `    |                      |dd|          }|                    |           d S )Nr   rR   )custom_parse_bid_ask
storeArray)r2   booksidedeltarE   bidAsks        r4   custom_handle_deltazphemex.custom_handle_delta  s5    **5!Q??F#####r5   c                     t          dt          |                    D ]}|                     |||         |            d S )Nr   )r   r   r  )r2   r  deltasrE   r   s        r4   custom_handle_deltaszphemex.custom_handle_deltas  sN    q#f++&& 	B 	BA$$Xvay&AAAA	B 	Br5   c           
         |                      |d          }|                     |          }|d         }|                      |d          }|                     |d          }d}|dz   |z   }	|                     |d          }
|                     |dd          }|d	k    rq|                     |d
di           }|                     |||dddd|          }|
|d<   |                     ||          }|| j        |<   |                    ||	           d S || j        v r| j        |         }| 	                    |d
di           }| 
                    |dg           }| 
                    |dg           }|                     |d         ||           |                     |d         ||           |
|d<   ||d<   |                     |          |d<   || j        |<   |                    ||	           d S d S )NrW   r   depthr   r   sequencerX   rY   snapshotbookr   bidsasksr   rR   noncera   )rs   rt   rC   ru   r   custom_parse_order_book
order_book
orderbooksr   safe_dict_2	safe_listr  r}   )r2   r   r   r   rE   rW   r   r
  r   r   r  rX   r  r  r   changesr  r  s                     r4   handle_order_bookzphemex.handle_order_book  s   X ##GX66!!(++!00!!'733Sj6)!!':66--g{HMM	:$$WfmRHHD33D&)VU[]^`acijjH %HW%88I&/DOF#NN9k22222(( OF3	**7FM2NN~~gvr::~~gvr::)))F*;T6JJJ)))F*;T6JJJ%*	'")2	+&(,Y(?(?	*%*3'y+66666 )(r5   c                   K   |                                   d{V  d}d}d}|N|                     |          }|d         }||d         z   }|d         dk    r|                     |          }d|d<   |                     d||          \  }}|&|                     |d          }|dk    r|dz   n||z   }|                     |||           d{V }	| j        r|	                    ||          }|                     |	|||d          S )	a  
        watches information on multiple trades made by the user
        :param str symbol: unified market symbol of the market trades were made in
        :param int [since]: the earliest time in ms to fetch trades for
        :param int [limit]: the maximum number of trade structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=trade-structure>`
        Nztrades:rW   r   r   r   r   T	r   rE   extendr   rs   r   r   r   filter_by_symbol_since_limit)
r2   rW   r   r   r   rE   r   r   r   r   s
             r4   watch_my_tradeszphemex.watch_my_trades  sN      !!!!!!!!![[((FH%F%x(88Kh6))V,,#)x 99/6SYZZf>%%fh77F:@F:J:J;44R]`dRdK--dKHHHHHHHH? 	3OOFE22E00tTTTr5   c                    d}t          |          }|dk    rd S | j        }|+|                     | j        dd          }t	          |          }i }d }t          dt          |                    D ]}	||	         }
|                     |
d          }|                     |          }|                     |
          }|	                    |           |d         }||d         dk    rdn|d	         }d
||<   t          |                                          }t          dt          |                    D ](}	||	         }|dz   |z   }|                    ||           )|dz   |z   }|                    ||           d S )Nr   r   r(   r'   rW   r   r   r   r   Tr   )r   myTradesrC   r-   r   r   rs   rt   parse_trader   listkeysr   )r2   r   r   channeltradesLengthcachedTradesr   	marketIdsr   r   rawTrader   rE   r   rW   r!  hashr   s                     r4   handle_my_tradeszphemex.handle_my_trades  s   | 7||1F}%%dlM4HHE1%88L	q#g,,'' 		% 		%AqzH''(;;H%%h//F%%h//F'''H%F|'-h'76'A'A{{v $IfINN$$%%q#d))$$ 	/ 	/A!WFS=6)DNN<....md*|[11111r5   c                   K   |                                   d{V  d}d}d}|N|                     |          }|d         }||d         z   }|d         dk    r|                     |          }d|d<   |                     d||          \  }}|                     |d          dk    }||r|dz   n||z   }|                     |||           d{V }	| j        r|	                    ||          }|                     |	|||d          S )	a  
        watches information on multiple orders made by the user
        :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
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        Norders:rW   r   r   r   r   Tr  )
r2   rW   r   r   r   r   rE   r   isUSDTSettledorderss
             r4   watch_orderszphemex.watch_orders  sM      !!!!!!!!![[((FH%F%x(88Kh6))V,,#)x 99-QWXXf((::fD>:Gb;44{]aOaK--dKHHHHHHHH? 	3OOFE22E00tTTTr5   c                    g }g }d|v sd|v sd|v r|                      |dg           }|                      |dg           }|                     ||          }t          |          }|dk    rd S |                      |dg           }t          dt          |                    D ]4}	||	         }
|                     |
          }|                    |           5nt          |          }|dk    rd S t          dt          |                    D ]g}	||	         }|                     |d          }||dk    r|                    |           |                     |          }|                    |           h|                     ||           | 	                    | j
        dd          }i }| j        t          |          | _        d }| j        }t          dt          |                    D ][}	||	         }|                    |           |d	         }|                     |          }||d
         dk    }|rdn|d         }d||<   \t          |                                          }t          dt          |                    D ](}	d||	         z   }|                    | j        |           )d|z   }|                    | j        |           d S )Nclosedfillsr]   r   actionCancelordersLimitr'   rW   r   r   r   r   Tr*  )r   array_concatr   r   parse_orderr   rs   parse_ws_swap_orderr(  rC   r-   r,  r   rE   r   r!  r   )r2   r   r   r   parsedOrdersr/  r]   r,  ordersLengthr   rawOrderparsedOrdermessageLengthupdater1  r   r%  r   r   r   rW   rE   isUsdtr!  currentMessageHashr   s                             r4   handle_orderszphemex.handle_orders  s    ~ W%7%7Vw=N=N__Wh;;F??7FB77D&&tV44Fv;;Lq  __Wgr::F1c&kk** 1 1!!9"..x88##K00001
  LLM!!1c'll++ 1 1 ))&(;;&Vx-?-?MM&)))"66v>>##K0000ff---!!$,tDD	;077DKq#l++,, 	% 	%A!!_FMM&!!!H%F[[((F|)V3&,@{{&. $IfINN$$%%q#d))$$ 	< 	<A!/$q'!9NN4;(:;;;;$&t{K00000r5   c           
         |                      |d          }|                      |d          }|t          |          dk     rd }|                      |d          }|                     ||          }|d         }|                     |                      |d                    }|                     |d          }|                     |                      |d                    }	|                      |d|                     |                      |d	          |                    }
|                      |d
          }|                      |d          }|                      |d          }|                     |dd          }|                      |d|                     |                      |d          |                    }|                     |dd          }|dk    rd }| 	                    |                      |d                    }|                      |d          }|dk    }| 
                    i d|d|d|d|                     |          d|d|d|d|	d|d|d|d|
d|d |d!|d"|d#||d |d d d$|          S )%NorderIDclOrdIDrR   rW   	ordStatussideordTypepriceRppriceEporderQtycumQty	leavesQtyactionTimeNsrY   
cumValueRv
cumValueEvtransactTimeNsr   timeInForcestopPxPOrr   r   clientOrderIdra   rX   lastTradeTimestampr   postOnlyprice	stopPricetriggerPriceamountfilled	remaining)costrm   statusfeer   )rs   r   rt   parse_order_statussafe_string_lowerparseOrderTyperF   ru   rK   parse_time_in_force
safe_orderr}   )r2   orderrE   r   rR  r   rW   r\  rD  r   rU  rX  rY  rZ  rX   r[  rS  rO  rV  rT  s                       r4   r6  zphemex.parse_ws_swap_orderq  s#   v eY//((	::%C,>,>,B,B M##E844!!(F33!(()9)9%)M)MNN%%eV44""4#3#3E9#E#EFF  	4<<@P@PQVXa@b@bdj3k3kll!!%44!!%22$$UK88	--e^XNN	|T\\$BRBRSXZfBgBgio5p5pqq!66u>NPXYY""!%..t/?/?}/U/UVV$$UH55	4'  
E 
" 
 ] 
 Y//	 

  
 !"4 
 f 
 D 
 ; 
  
 D 
 U 
  
 I 
 f 
  f! 
" # 
$ - 
  
  
. /  	r5   c                    |                      |d          }||j        v r'|j        |         }|j        |= |dur |||           d S |                      |dd          }d|v sd|v s|                    d          dk    r|                     ||           d S d	|v sd
|v r|                     ||           d S d|v sd|v r|                     ||           d S d|v sd|v r|                     ||           d S d|v sd|v r.|                     |ddi           }|                     ||           d|v sd|v sd|v r@d|v rdnd}d|v rd}| 	                    |g dg           }| 
                    |||           d S d S )Nr   Tr    r   r   r   r   r   r   r   r   r  r   r,  orders_paccounts
accounts_pwalletsr   spotr   )rg  rh  ri  )rs   subscriptionsfindr   r   r   r  r   r?  safe_value_nr   )	r2   r   r   r   r   
methodNamer,  r   rg  s	            r4   handle_messagezphemex.handle_message  s    @ gt,,%%%)"-F$R(T!!vw'''%%gx<<
7""(8G(C(CYpIqIquvIvIvvw///F'!!zW'<'<vw///F  i7&:&:fg...F]g%=%=""67333FZ7%:%:&&w*bIIFvv...'!!|w'>'>IQXDXDX(G3366&Dw&&"((2W2W2WY[\\Hfh77777 EYDXr5   c                 J   |                      |d          }|                     |d          }d}|dk    r|                    ||           d S t          | j        dz   |                     |          z             }|                    ||           ||j        v r
|j        |= d S d S )Nr   r\  authenticatedsuccess )r   rs   r   r   r   jsonrejectrk  )r2   r   r   r   r\  r   errors          r4   handle_authenticatezphemex.handle_authenticate  s     (33!!&(33%YNN7K00000'#		'8J8J(JKKEMM%---f222(555 32r5   c                   K   |                                   d {V  |                                  d {V  | j        d         d         }|                                 }|                     |dd          dk    }|                     |d          }d}|dk    rd}|rd	}||g d
}|                     ||          }|                     ||||           d {V S )Nr&   r   r   re  r   zaop.subscriberj  zwo.subscribezaop_p.subscribe)r   r   r   )r   authenticater,   secondsr   omitr  r   )	r2   r   r   r   r   rQ   r   r"  r   s	            r4   r   zphemex.subscribe_private  s     !!!!!!!!!!!!!!!!!!it$LLNN	"==G68,,!6>>$G 	('G
 

 ++gv..ZZ['7CCCCCCCCCr5   c                   K   |                                   | j        d         d         }|                     |          }|                                 }d}|                     |j        |          }||                     | j        dd          }|                                 |z   }| j	        t          |          z   }	|                     |                     |	          |                     | j                  t          j                  }
d}|d| j	        |
|g|d}t          |          }|                     ||          }||j        vr| j        |j        |<   |                     ||||           d {V }||j        |<   |S )	Nr&   r   rq  expiresx   z	user.authAPI)r   r   r   )check_required_credentialsr,   r   rU   r   rk  rC   r-   rz  apiKeyr;   hmacencodesecrethashlibsha256r  rw  r   )r2   r   r   r   rQ   r   futureexpiryDelta
expirationpayload	signaturer   r   r   r   s                  r4   ry  zphemex.authenticate  sy     '')))it$S!!OO%%	%!5{CC>++DL)SIIK+5JkC
OO3G		$++g"6"6DK8P8PRYR`aaI F  $+y*E G
  #9~~kk'622G6#7779=9Q$%56::c;MMMMMMMMF06F -r5   r7   )0__name__
__module____qualname__r   r1   r?   rF   rK   rO   rU   r   r   r   r   r   r   r   r   r   r;   r   r   r   r   r   r   r   r   r   r
   r   r   r   r  r  r  r   r  r(  r	   r-  r?  r6  ro  rw  r   ry  __classcell__)r3   s   @r4   r   r      s       !# ! ! ! ! ! !F  I I I I
I I I I
I I I I
  9 9 9 9v6 6 6 6p_0F _0 _0 _0 _0B *, G G G G G G(D2 D2 D2L),F ), ), ), ),V306 30 30 30 30j 68 M M MF M M M M@ 6:" 'E 'E7 'E 'E 'E 'E 'ER <@d[] #S #S #SS #S #Sbfglbm #S #S #S #SJ @DB !! !!S !! !!S\ !! !! !! !!F ?CQUdhqs %H %H %H %H3 %H^a %Hx|  ~B  yC %H %H %H %HN$ $ $ $B B B BH7 H7 H7 H7 H7T 37TX\eg U UC Us URU Ulpqvlw U U U U:y2v y2 y2 y2 y2v 04$UYbd U U UC Us Uimnsit U U U U:O1F O1 O1 O1 O1bh h h hT|8V |8 |8 |8 |8|6& 6 6 6 6* AC D D D D( )+        r5   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   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   ccxt.base.preciser   async_supportr    r5   r4   <module>r     s       f f f f f f f f f f  f f f f f f f f f f f f f f f f f f f f f f f f 4 4 4 4 4 4       0 0 0 0 0 0 % % % % % %J J J J JT& J J J J Jr5   