
    E#i                         S SK rS SKJrJrJr  S SKrS SKJrJ	r	J
r
JrJrJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S SKJr   " S S	\R4                  R6                  5      rg)
    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)
AnyBalancesIntOrder	OrderBookStrStringsTickerTickersTrade)Client)List)AuthenticationError)Precisec                   0  ^  \ rS rSrS\4U 4S jjrS rS*S jrS*S jrS*S jr	S	 r
S*S
 jrS*S jrS\4S jr0 4S\4S jjrS rS\4S jrS\4S jr0 4S\S\4S jjrS0 4S\S\4S jjrSS0 4S\S\S\S\\   4S jjrS0 4S\S\S\4S jjrSSS0 4S\S\S\S\S\\    4
S jjr!S*S jr"S*S jr#S\4S jr$SSS0 4S\%S\S\S\\   4S  jjr&S\4S! jr'SSS0 4S\%S\S\S\\(   4S" jjr)S\4S# jr*S*S$ jr+S\4S% jr,S\4S& jr-0 4S' jr.0 4S( jr/S)r0U =r1$ )+phemex   returnc                    > U R                  [        [        U ]  5       SSSSSSSSS SSSSS.SS0SS0S.SSS	.S
S0S.5      $ )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    I/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/phemex.pyr2   phemex.describe   s    fd < ># $#!%#"&""&,1).(- $$ ; /	  $"
 T; A
    	    c                     Uc  g [        U5      nU R                  UR                  U5      Ul        UR                  5         [	        U5      $ N)r   sumdecimalsreducestr)r3   enscaleprecises       r5   from_enphemex.from_en5   s@    :"+88G$4$4e<7|r7   Nc                 T    Ub  Uc  U$ U R                  XR                  US5      5      $ )N
priceScalerA   safe_integer)r3   epmarkets      r5   from_epphemex.from_ep=   ,    JFNI||B 1 1&, GHHr7   c                 T    Ub  Uc  U$ U R                  XR                  US5      5      $ )N
valueScalerE   )r3   evrH   s      r5   from_evphemex.from_evB   rK   r7   c                 T    Ub  Uc  U$ U R                  XR                  US5      5      $ )N
ratioScalerE   )r3   errH   s      r5   from_erphemex.from_erG   rK   r7   c                 |    U R                  U R                  U R                  SS5      S5      nXR                  S'   U$ )N	requestIdr      )r:   rF   r.   )r3   rW   s     r5   
request_idphemex.request_idL   s7    HHT..t||[!LaP	$-[!r7   c                    U R                  US5      nU R                  X25      nUS   nU R                  USS5      nU R                  U R                  US5      U5      nU R	                  U5      nU R	                  U R                  U R                  US5      U5      5      nU R	                  U R                  U R                  US5      U5      5      n	S n
S nS nU R                  U R                  U R                  US5      U5      5      nU R	                  U5      nUb  Ub  U R	                  [        R                  " Xm5      5      n
U R	                  [        R                  " [        R                  " Xm5      S5      5      nU R	                  [        R                  " [        R                  " [        R                  " Xm5      S	5      S
5      5      nU R                  0 SU_SU_SU R                  U5      _SU R	                  U R                  U R                  US5      U5      5      _SU R	                  U R                  U R                  US5      U5      5      _SS _SS _SS _SS _SS _SU_SU_SU_SS _SU
_SU_SU_U	UU R	                  U R                  U R                  US5      U5      5      U R	                  U R                  U R                  US5      U5      5      US.E5      $ )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quoteVolumers   rt   info)safe_stringsafe_marketsafe_integer_productrI   parse_numberrO   	omit_zeror   
string_sub
string_div
string_add
string_mulsafe_tickeriso8601)r3   tickerrH   marketIdr\   r]   
lastStringrn   rv   ru   rp   rq   rr   
openStringrb   s                  r5   parse_swap_tickerphemex.parse_swap_tickerQ   s3   " ##FH5!!(3!--fk8L	\\$"2"267"CVL
  ,''T5E5Efj5Y[a(bc&&t||D4D4DVX4VX^'_`

^^DLL1A1A&&1QSY$Z[
  ,")?&&w'9'9*'QRF''(:(:7;M;Mj;egj(klG**7+=+=g>P>PQXQcQcdnQ{  ~A  ?B  DI  ,J  KJ !
f!
!
 Y/!
 D%%dll43C3CFF3SU[&\]	!

 4$$T\\$2B2B652QSY%Z[!
 4!
 !
 4!
 !
 D!
 D!
 T!
 D!
 T!
 f!
  *!!
" w#!
$ %&**4<<8H8HQ\8]_e+fg++DLL9I9I&R^9_ag,hi-!
  	r7   c                    U R                  US5      nU R                  X25      nUS   nU R                  U R                  US5      U5      nU R                  U5      nU R                  U R	                  U R                  US5      U5      5      nU R                  U R	                  U R                  US5      U5      5      nS n	S n
S nU R                  U R                  U R                  US5      U5      5      nU R                  U5      nUb  Ub  U R                  [        R                  " X\5      5      n	U R                  [        R                  " [        R                  " X\5      S5      5      nU R                  [        R                  " [        R                  " [        R                  " X\5      S5      S	5      5      n
U R                  0 SU_S
S _SS _SU R                  U R                  U R                  US5      U5      5      _SU R                  U R                  U R                  US5      U5      5      _SS _SS _SS _SS _SS _SU_SU_SU_SS _SU	_SU
_SU_UUUS.E5      $ )Nr   r\            rX   rc   rd   re   r]   rf   rg      rh      ri   rj   rk   rl   rm   rb   r_   rn   ro   rp   rq   rr   )ru   rv   rw   )rx   ry   rI   r{   rO   r|   r   r}   r~   r   r   r   )r3   r   rH   r   r\   r   rn   rv   ru   rp   rq   rr   r   rb   s                 r5   parse_perpetual_tickerphemex.parse_perpetual_ticker   s   " ##FA.!!(3!\\$"2"261"=vF
  ,''T5E5Efa5PRX(YZ&&t||D4D4DVQ4OQW'XY

^^DLL1A1A&!1Lf$UV
  ,")?&&w'9'9*'QRF''(:(:7;M;Mj;egj(klG**7+=+=g>P>PQXQcQcdnQ{  ~A  ?B  DI  ,J  KJ !
f!
!
 !
 D%%dll43C3CFA3NPV&WX	!

 4$$T\\$2B2B612Mv%VW!
 4!
 !
 4!
 !
 D!
 D!
 T!
 D!
 T!
 f!
  *!!
" w#!
$ %&)!
  	r7   clientc                    / nSU;   a3  U R                  US5      nUR                  U R                  U5      5        OSU;   a3  U R                  US5      nUR                  U R                  U5      5        OWSU;   aQ  U R                  US/ 5      n[	        S[        U5      5       H%  nUR                  U R                  XV   5      5        M'     [	        S[        U5      5       H[  nX6   nUS   nSU-   nU R                  USS5      n	XS'   U R                  U	5      US	'   X@R                  U'   UR                  XH5        M]     g )
N	market24hspot_market24hdatar   r\   ticker:r]   r^   rf   )
safe_valueappendr   parse_tickerrangelenr   rz   r   tickersresolve)
r3   r   messager   r   r   ir\   messageHashr]   s
             r5   handle_tickerphemex.handle_ticker   s(   X '!__Wk:FNN411&9:(__W.>?FNN4,,V45w??7FB7D1c$i(t::47CD )q#g,'AZFH%F#f,K11';QI"+;!%i!8F:#)LL NN6/ (r7   c                    #    U R                  5       I Sh  vN   SnU R                  SSU5      u  p!U R                  US5      S:H  nSnU(       a  SU-   OX$-   nU R                  X$U5      I Sh  vN $  N\ N7f)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_paramsrx   subscribe_private)r3   paramstypeusePerpetualApir   s        r5   watch_balancephemex.watch_balance%  s      !!!99.$PVW**68<F 3BkK/HZ++DvFFF 	" Gs"   A7A3AA7.A5/A75A7c                 8   X0R                   S'   [        S[        U5      5       GHQ  nX4   nU R                  US5      nU R	                  U5      nU R                  U R                  U0 5      nU R                  USS5      n	U R                  5       n
U R                  US5      nUca  U R                  US5      nUc;  U R                  US5      nU R                  US	S
5      n[        R                  " X5      nU R                  X5      nU R                  US5      nUc%  U R                  USS5      nU R                  UU	5      nXS'   XS'   XR                   U'   U R                  U R                   5      U l         GMT     US-   nUR                  U R                   U5        g )Nrw   r   currencyrM      totalUsedBalanceRvtotalUsedBalanceEvlockedTradingBalanceEvlockedWithdrawEvlockedWithdrawRvaccountBalanceRvaccountBalanceEv	balanceEvusedtotalr   )balancer   r   rx   safe_currency_coder   
currenciesrF   accountsafe_string_2r   r   rA   safe_balancer   )r3   r   r   r   r   r   
currencyIdcoder   r?   r   r   usedEvr   r   r   totalEvr   s                     r5   handle_balancephemex.handle_balance9  s   V  'Vq#g,'AjG))':>J**:6DtbAH%%ha@EllnG##G-ABD|))'3GH>-1-=-=gG_-`*'+'9'9'CUWi'j$$//0FYF||F2$$W.@AE},,W6H+VWe4"FO$G!(LL,,T\\:DL- (. Z't||[1r7   c                    SnU R                  US5      nU R                  U5      nUS   nUS-   U-   nU R                  U R                  U5      nUc6  U R	                  U R
                  SS5      n	[        U	5      nXR                  U'   U R                  USS/ 5      n
U R                  X5      n[        S[        U5      5       H  nUR                  X   5        M     UR                  X5        g )	Ntrader\   :r)   r(   tradestrades_pr   )rx   ry   r   r   rF   r.   r   safe_value_2parse_tradesr   r   r   r   )r3   r   r   namer   rH   r\   r   storedlimitr   parsedr   s                r5   handle_tradesphemex.handle_trades  s    6 ##GX6!!(+!Sj6)f5>%%dllM4HE&F"(KK""7Hj"E""62q#f+&AMM&)$ 'v+r7   c                    U R                  US5      nU R                  U5      nUS   nU R                  USS/ 5      nU R                  US/ 5      nU R                  US5      nU R	                  U5      n	U	b  SU	-   S-   U-   n
U R                  Xd5      nU R                  U R                  U0 5      U R                  U'   U R                  U R                  U   U	5      nUc9  U R                  U R                  SS	5      n[        U5      nXR                  U   U	'   [        S[        U5      5       H  nX   nUR                  U5        M     UR                  X5        g g )
Nr\   klinekline_pr   rX   kline:r   r*   r(   )rx   ry   r   r   find_timeframeparse_ohlcvsohlcvsrF   r.   r   r   r   r   r   )r3   r   r   r   rH   r\   candlesfirstinterval	timeframer   r   r   r   r   candles                   r5   handle_ohlcvphemex.handle_ohlcv  sP   @ ##GX6!!(+!##GWiDB/##E1-''1	 "Y.4v=K&&w7F"&//$++vr"JDKK__T[[%8)DF~))$,,dK.u517F#I.1c&k*f% + NN6/ !r7   r\   c                 j  #    U R                  5       I Sh  vN   U R                  U5      nUS   nUS   nUS   S:H  nSnU(       a  U(       a  SOSnU R                  S	   S
   nU R                  5       nUS-   n	SU-   n
U	U/ S.nU R	                  X5      nU R                  XzX5      I Sh  vN $  N N7f)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>`
Nr\   swapr   r   r   perp_market24h_pack_pr   r'   r   
.subscriber   methodidr   )r   rH   r-   rY   r0   watch)r3   r\   r   rH   isSwapsettleIsUSDTr   urlrW   subscriptionHashr   	subscriberequests                r5   watch_tickerphemex.watch_ticker  s      !!!V$!h'61.:*Diit$OO%	,.&(&
	
 ""95ZZ'LLL% 	"$ Ms"   B3B/BB3*B1+B31B3symbolsc                 d  #    U R                  5       I Sh  vN   U R                  USS5      nUS   nU R                  U5      nUS   nUS   S:H  nSnU(       a  U(       a  SOS	nU R                  S
   S   nU R	                  5       n	US-   n
/ n[        S[        U5      5       H  nUR                  SX   -   5        M     U
U	/ S.nU R                  X5      nU R                  XX5      I Sh  vN nU R                  (       a  0 nUUUS   '   U$ U R                  U R                  SU5      $  GN NA7f)aD  

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   r\   )r   market_symbolsrH   r-   rY   r   r   r   r0   watch_multiple
newUpdatesfilter_by_arrayr   )r3   r   r   r   rH   r   r   r   r   rW   r   messageHashesr   r   r   r   results                    r5   watch_tickersphemex.watch_tickers  sD     !!!%%gtU;
U#h'61.:*Diit$OO%	,.q#g,'A  WZ!78 ( '
	
 ""95**3wVV??F'-F6(#$M##DLL(GDD5 	"* Ws"   D0D+CD0,D.-?D0.D0sincer   c                   #    U R                  5       I Sh  vN   U R                  U5      nUS   nU R                  S   S   nU R                  5       nUS   nUS   S:H  n	U(       a	  U	(       a  SOS	n
S
U-   nU
S-   nUUUS   /S.nU R	                  X5      nU R                  XkX5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XUSS5      $  N N<7f)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>`
Nr\   r'   r   r   r   r   trade_pr   ztrade:r   r   r   r]   T)	r   rH   r-   rY   r0   r   r   getLimitfilter_by_since_limit)r3   r\   r   r   r   rH   r   rW   r   r   r   r   r   r   r   r   s                   r5   watch_tradesphemex.watch_trades(  s      !!!V$!iit$OO%	h'61#y7'$t
	 ""95zz#GII??OOF2E))&TRR+ 	"$ Js"   C+C'BC+,C)-;C+)C+c                   #    U R                  5       I Sh  vN   U R                  U5      nUS   nU R                  S   S   nU R                  5       nUS   nUS   S:H  nU(       a	  U(       a  SOS	n	S
U-   n
U	S-   nUUUS   /S.nU R	                  X5      nU R                  XZX5      I Sh  vN nUR                  5       $  N N7f)as  

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
Nr\   r'   r   r   r   r   orderbook_p	orderbookz
orderbook:r   r   r   )r   rH   r-   rY   r0   r   r   )r3   r\   r   r   rH   r   rW   r   r   r   r   r   r   r   r  s                  r5   watch_order_bookphemex.watch_order_bookM  s      !!!V$!iit$OO%	h'61!'L}{"V+$t
	 ""95**SwLL	  ' 	"$ Ms"   CCBC,C-CC1mr   c                   #    U R                  5       I Sh  vN   U R                  U5      nUS   nU R                  S   S   nU R                  5       nUS   n	US   S:H  n
U	(       a	  U
(       a  SOS	nS
U-   S-   U-   nUS-   nUUUS   U R	                  U R
                  U5      /S.nU R                  X5      nU R                  X|X5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  UX4SS5      $  N N<7f)a4  

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
Nr\   r'   r   r   r   r   r   r   r   r   r   r   r   r   T)r   rH   r-   rY   rF   
timeframesr0   r   r   r  r  )r3   r\   r   r   r   r   rH   r   rW   r   r   r   r   r   r   r   ohlcvs                    r5   watch_ohlcvphemex.watch_ohlcvp  s     !!!V$!iit$OO%	h'61#y7*S069$t!!$//9=
	 ""95jj7HH??NN61E))%q$GG- 	"& Is"   DDB6DD
;D
Dc                 N    U R                  USSU5      nUR                  U5        g )Nr   rX   )custom_parse_bid_ask
storeArray)r3   booksidedeltarH   bidAsks        r5   custom_handle_deltaphemex.custom_handle_delta  s&    **5!Q?F#r7   c                 f    [        S[        U5      5       H  nU R                  XU   U5        M     g )Nr   )r   r   r  )r3   r  deltasrH   r   s        r5   custom_handle_deltasphemex.custom_handle_deltas  s+    q#f+&A$$Xay&A 'r7   c           
      F   U R                  US5      nU R                  U5      nUS   nU R                  US5      nU R                  US5      nSnUS-   U-   n	U R                  US5      n
U R                  USS5      nUS	:X  a`  U R	                  US
S0 5      nU R                  XUSSSSU5      nXS'   U R                  X5      nXR                  U'   UR                  X5        g XPR                  ;   a  U R                  U   nU R                  US
S0 5      nU R                  US/ 5      nU R                  US/ 5      nU R                  US   UU5        U R                  US   UU5        XS'   XS'   U R                  U5      US'   XR                  U'   UR                  X5        g g )Nr\   r   depthr  r   sequencer]   r^   snapshotbookr  bidsasksr   rX   noncerf   )rx   ry   rF   rz   r   custom_parse_order_book
order_book
orderbooksr   safe_dict_2	safe_listr  r   )r3   r   r   r   rH   r\   r   r  r   r   r#  r]   r   r  r  changesr"  r!  s                     r5   handle_order_bookphemex.handle_order_book  s   X ##GX6!!(+!0!!'73Sj6)!!':6--g{HM	:$$WfmRHD33D)VU[]^`acijH %W8I&/OOF#NN92( OOF3	**7FM2N~~gvr:~~gvr:)))F*;T6J)))F*;T6J%*'")2+&(,Y(?	*%*3'y6 )r7   c                   #    U R                  5       I Sh  vN   SnSnSnUb<  U R                  U5      nUS   nXuS   -   nUS   S:X  a  U R                  U5      nSUS'   U R                  SXT5      u  pdUc!  U R	                  US5      nUS:X  a  US-   OXv-   nU R                  XgU5      I Sh  vN n	U R                  (       a  U	R                  X5      nU R                  XX#S5      $  N N;7f)	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:r\   r   r   r   r   T	r   rH   extendr   rx   r   r   r  filter_by_symbol_since_limit)
r3   r\   r   r   r   rH   r   r   r   r   s
             r5   watch_my_tradesphemex.watch_my_trades  s     !!![[(FH%F%x(88Kh6)V,#)x 99/6Z>%%fh7F:@F:J;4R]RdK--dHH??OOF2E00tTT% 	" Is"   C+C'BC+-C).:C+)C+c                 ~   Sn[        U5      nUS:X  a  g U R                  nUc(  U R                  U R                  SS5      n[	        U5      n0 nS n[        S[        U5      5       Hh  n	X)   n
U R                  U
S5      nU R                  U5      nU R                  U
5      nUR                  U5        US   nUc  US   S:X  a  SOUS	   nS
X~'   Mj     [        UR                  5       5      n[        S[        U5      5       H!  n	X   nUS-   U-   nUR                  UU5        M#     US-   U-   nUR                  UU5        g )Nr   r   r)   r(   r\   r   r   r   r   Tr   )r   myTradesrF   r.   r   r   rx   ry   parse_trader   listkeysr   )r3   r   r   channeltradesLengthcachedTradesr   	marketIdsr   r   rawTrader   rH   r   r\   r6  hashr   s                     r5   handle_my_tradesphemex.handle_my_trades  sL   | 7|1}}%%dllM4HE1%8L	q#g,'AzH''(;H%%h/F%%h/F'H%F|'-h'76'A{v $I ( INN$%q#d)$AWFS=6)DNN<. %
 md*|[1r7   c                   #    U R                  5       I Sh  vN   SnSnSnUb<  U R                  U5      nUS   nXVS   -   nUS   S:X  a  U R                  U5      nSUS'   U R                  SXd5      u  ptU R	                  US5      S:H  nUc  U(       a  US-   OXW-   nU R                  XuU5      I Sh  vN n	U R                  (       a  U	R                  X5      nU R                  XX#S5      $  N N;7f)	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:r\   r   r   r   r   Tr-  )
r3   r\   r   r   r   r   rH   r   isUSDTSettledorderss
             r5   watch_ordersphemex.watch_orders  s     !!![[(FH%F%x(88Kh6)V,#)x 99-X((:fD>:G;4{OaK--dHH??OOF2E00tTT% 	" Is"   C/C+BC/1C-2:C/-C/c                    / n/ nSU;   d  SU;   d  SU;   a  U R                  US/ 5      nU R                  US/ 5      nU R                  Xe5      n[        U5      nUS:X  a  g U R                  US/ 5      n[        S[        U5      5       H)  n	Xy   n
U R	                  U
5      nUR                  U5        M+     O[        U5      nUS:X  a  g [        S[        U5      5       HU  n	X)   nU R                  US5      nUb  US:w  a  UR                  U5        U R                  U5      nUR                  U5        MW     U R                  X5        U R                  U R                  SS5      n0 nU R                  c  [        U5      U l        S nU R                  n[        S[        U5      5       HL  n	XI   nUR                  U5        US	   nU R                  U5      nUc  US
   S:H  nU(       a  SOUS   nSUU'   MN     [        UR                  5       5      n[        S[        U5      5       H'  n	SUU	   -   nUR!                  U R                  U5        M)     SU-   nUR!                  U R                  U5        g )Nclosedfillsrb   r   actionCancelordersLimitr(   r\   r   r   r   r   Tr@  )r   array_concatr   r   parse_orderr   rx   parse_ws_swap_orderr=  rF   r.   rB  r   rH   r5  r6  r   )r3   r   r   r   parsedOrdersrF  rb   rB  ordersLengthr   rawOrderparsedOrdermessageLengthupdaterH  r   r:  r   r   r   r\   rH   isUsdtr6  currentMessageHashr   s                             r5   handle_ordersphemex.handle_orders  sY   ~ W%7Vw=N__Wh;F??7FB7D&&t4Fv;Lq __Wgr:F1c&k*!9"..x8##K0 +
  LM!1c'l+ ))&(;&Vx-?MM&)"66v>##K0 , 	f-!!$,,tD	;;07DKq#l+,A!_FMM&!H%F[[(F|)V3&,{&. $If - INN$%q#d)$A!/$q'!9NN4;;(:; %  $&t{{K0r7   c           
      &   U R                  US5      nU R                  US5      nUb  [        U5      S:  a  S nU R                  US5      nU R                  XR5      nUS   nU R                  U R                  US5      5      nU R	                  US5      nU R                  U R                  US5      5      n	U R                  USU R                  U R                  US	5      U5      5      n
U R                  US
5      nU R                  US5      nU R                  US5      nU R                  USS5      nU R                  USU R                  U R                  US5      U5      5      nU R                  USS5      nUS:X  a  S nU R                  U R                  US5      5      nU R                  US5      nUS:H  nU R                  0 SU_SU_SU_SU R                  U5      _SU_SU_SU_SU	_SU_SU_SU_SU
_SU_S U_S!U_S"U_S#U_US US S S$.EU5      $ )%NorderIDclOrdIDrX   r\   	ordStatussideordTypepriceRppriceEporderQtycumQty	leavesQtyactionTimeNsr^   
cumValueRv
cumValueEvtransactTimeNsr   timeInForcestopPxPOrw   r   clientOrderIdrf   r]   lastTradeTimestampr   postOnlyprice	stopPricetriggerPriceamountfilled	remaining)costrr   statusfeer   )rx   r   ry   parse_order_statussafe_string_lowerparseOrderTyperI   rz   rO   parse_time_in_force
safe_orderr   )r3   orderrH   r   rj  r   r\   rt  r\  r   rm  rp  rq  rr  r]   rs  rk  rg  rn  rl  s                       r5   rM  phemex.parse_ws_swap_ordero  s   v eY/((	:%C,>,B M##E84!!(3!(()9)9%)MN%%eV4""4#3#3E9#EF  	4<<@P@PQVXa@bdj3kl!!%4!!%2$$UK8	--e^XN	|T\\$BRBRSXZfBgio5pq!66u>NPXY"!%..t/?/?}/UV$$UH5	4'  
E 
" 
 ] 
 Y/	 

  
 !"4 
 f 
 D 
 ; 
  
 D 
 U 
  
 I 
 f 
  f! 
" # 
$ - 
. / 	r7   c                    U R                  US5      nX1R                  ;   a*  UR                  U   nUR                  U	 USLa	  U" X5        g U R                  USS5      nSU;   d  SU;   d  UR                  S5      S:  a  U R                  X5        g S	U;   d  S
U;   a  U R	                  X5        g SU;   d  SU;   a  U R                  X5        g SU;   d  SU;   a  U R                  X5        g SU;   d  SU;   a%  U R                  USS0 5      nU R                  X5        SU;   d  SU;   d  SU;   a:  SU;   a  SOSnSU;   a  SnU R                  U/ SQ/ 5      nU R                  XqU5        g g )Nr   Tr    r   r   r   r   r   r   r   r   r   r  rB  orders_paccounts
accounts_pwalletsr   spotr   )r  r  r  )rx   subscriptionsfindr   r   r   r*  r   rV  safe_value_nr   )	r3   r   r   r   r   
methodNamerB  r   r  s	            r5   handle_messagephemex.handle_message  s   @ gt,%%%))"-F$$R(T!v'%%gx<
7"(8G(CYpIquvIvv/'!zW'<v/ i7&:f.]g%=""63Z7%:&&w*bIFv.'!|w'>IQXDX(G36&Dw&"((2WY[\Hh7 EYr7   c                 0   U R                  US5      nU R                  US5      nSnUS:X  a  UR                  X%5        g [        U R                  S-   U R                  U5      -   5      nUR                  Xe5        XQR                  ;   a  UR                  U	 g g )Nr   rt  authenticatedsuccess )r   rx   r   r   r   jsonrejectr  )r3   r   r   r   rt  r   errors          r5   handle_authenticatephemex.handle_authenticate  s     (3!!&(3%YNN70'#		'8J(JKEMM%-222((5 3r7   c                   #    U R                  5       I S h  vN   U R                  5       I S h  vN   U R                  S   S   nU R                  5       nU R	                  USS5      S:H  nU R                  US5      nSnUS:X  a  SnU(       a  S	nUU/ S
.nU R                  X5      nU R                  XBX5      I S h  vN $  N N N7f)Nr'   r   r   r~  r   zaop.subscriber  zwo.subscribezaop_p.subscribe)r   r   r   )r   authenticater-   secondsr   omitr.  r   )	r3   r   r   r   r   rW   r   r7  r   s	            r5   r   phemex.subscribe_private  s     !!!!!!iit$LLN	"=G68,!6>$G'G

 ++g.ZZ'CCC# 	"!  Ds2   CCCCBC<C=CCCc                    #    U R                  5         U R                  S   S   nU R                  U5      nU R                  5       nSnU R	                  UR
                  U5      nUGc  U R                  U R                  SS5      nU R                  5       U-   nU R                  [        U5      -   n	U R                  U R                  U	5      U R                  U R                  5      [        R                  5      n
SnUSU R                  X/US.n[        U5      nU R!                  X5      nXSR
                  ;  a  U R"                  UR
                  U'   U R%                  X%X5      I S h  vN nXcR
                  U'   U$  N7f)	Nr'   r   r  expiresx   z	user.authAPI)r   r   r   )check_required_credentialsr-   r   rY   r   r  rF   r.   r  apiKeyr=   hmacencodesecrethashlibsha256r.  r  r   )r3   r   r   r   rW   r   futureexpiryDelta
expirationpayload	signaturer   r   r   r   s                  r5   r  phemex.authenticate  sP    '')iit$S!OO%	%!5!5{C>++DLL)SIK+5JkkC
O3G		$++g"6DKK8PRYR`R`aI F  $++yEG
  #9~kk'2G#7#779=9Q9Q$$%56::cMMF06  - Ns   E%E>'E<(E>)r   rB  r9   )2__name__
__module____qualname____firstlineno__r   r2   rA   rI   rO   rT   rY   r   r   r   r   r   r   r   r   r   r=   r   r   r   r   r   r   r   r   r  r
   r  r5  r  r  r  r*  r   r0  r=  r	   rC  rV  rM  r  r  r   r  __static_attributes____classcell__)r4   s   @r5   r   r      sL   !# !FI
I
I

9v6p_0F _0B *, G G(D2L),F ),V306 30j 68 M MF M@ 6:" 'E7 'E 'ER <@d[] #S #SS #S #Sbfglbm #SJ @DB !!S !! !!S\ !!F ?CQUdhqs %H %H %H3 %H^a %Hx|  ~B  yC %HN$BH7 H7T 37TX\eg UC Us URU Ulpqvlw U:y2v y2v 04$UYbd U UC Us Uimnsit U:O1F O1bhT|8V |8|6& 6* AC D( )+  r7   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    r7   r5   <module>r     sB     f f  f f f 4  0 %HT&& Hr7   