
    ":ho                         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j4                  j6                        Zy)
    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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                     | j                  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    I/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/phemex.pyr1   zphemex.describe   s    fd < ># $#!%#"&""&,1).(- $$ ; /	  $"
 T; A
    	    c                     |y t        |      }| j                  |j                  |      |_        |j                          t	        |      S N)r   sumdecimalsreducestr)r2   enscaleprecises       r4   from_enzphemex.from_en5   s@    :"+88G$4$4e<7|r5   Nc                 R    |||S | j                  || j                  |d            S )N
priceScaler?   safe_integer)r2   epmarkets      r4   from_epzphemex.from_ep=   .    JFNI||B 1 1&, GHHr5   c                 R    |||S | j                  || j                  |d            S )N
valueScalerB   )r2   evrE   s      r4   from_evzphemex.from_evB   rG   r5   c                 R    |||S | j                  || j                  |d            S )N
ratioScalerB   )r2   errE   s      r4   from_erzphemex.from_erG   rG   r5   c                 ~    | j                  | j                  | j                  dd      d      }|| j                  d<   |S )N	requestIdr      )r8   rC   r-   )r2   rQ   s     r4   
request_idzphemex.request_idL   s9    HHT..t||[!LaP	$-[!r5   c                    | j                  |d      }| j                  ||      }|d   }| j                  |dd      }| j                  | j                  |d      |      }| j	                  |      }| j	                  | j                  | j                  |d      |            }| j	                  | j                  | j                  |d      |            }	d }
d }d }| j                  | j                  | j                  |d      |            }| j	                  |      }||| j	                  t        j                  ||            }
| j	                  t        j                  t        j                  ||      d            }| j	                  t        j                  t        j                  t        j                  ||      d	      d
            }i d|d|d| j                  |      d| j	                  | j                  | j                  |d      |            d| j	                  | j                  | j                  |d      |            dd dd dd dd dd d|d|d|dd d|
d|d||	|| j	                  | j                  | j                  |d      |            | j	                  | j                  | j                  |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quoteVolumerl   rm   info)safe_stringsafe_marketsafe_integer_productrF   parse_numberrK   	omit_zeror   
string_sub
string_div
string_add
string_muliso8601)r2   tickerrE   marketIdrU   rV   
lastStringrg   ro   rn   ri   rj   rk   
openStringr[   results                   r4   parse_swap_tickerzphemex.parse_swap_tickerQ   s3   " ##FH5!!(F3!--fk8L	\\$"2"267"CVL
  ,''T5E5Efj5Y[a(bc&&t||D4D4DVX4VX^'_`

^^DLL1A1A&&1QSY$Z[
  ,")?&&w'9'9*j'QRF''(:(:7;M;MjZd;egj(klG**7+=+=g>P>PQXQcQcdnpzQ{  ~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-
0 r5   c           
         | j                  |d      }| j                  ||      }|d   }| j                  | j                  |d      |      }| j                  |      }| j                  | j	                  | j                  |d      |            }| j                  | j	                  | j                  |d      |            }d }	d }
d }| j                  | j                  | j                  |d      |            }| j                  |      }||| j                  t        j                  ||            }	| j                  t        j                  t        j                  ||      d            }| j                  t        j                  t        j                  t        j                  ||      d      d	            }
i d|d
d dd d| j                  | j                  | j                  |d      |            d| j                  | j                  | j                  |d      |            dd dd dd dd dd d|d|d|dd d|	d|
d||||d}|S )Nr   rU            rR   r\   r]   r^   rV   r_   r`      ra      rb   rc   rd   re   rf   r[   rX   rg   rh   ri   rj   rk   )rn   ro   rp   )rq   rr   rF   rt   rK   ru   r   rv   rw   rx   ry   )r2   r{   rE   r|   rU   r}   rg   ro   rn   ri   rj   rk   r~   r[   r   s                  r4   parse_perpetual_tickerzphemex.parse_perpetual_ticker   s   " ##FA.!!(F3!\\$"2"261"=vF
  ,''T5E5Efa5PRX(YZ&&t||D4D4DVQ4OQW'XY

^^DLL1A1A&!1Lf$UV
  ,")?&&w'9'9*j'QRF''(:(:7;M;MjZd;egj(klG**7+=+=g>P>PQXQcQcdnpzQ{  ~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#
$ %&)
, r5   clientc                 x   g }d|v r3| j                  |d      }|j                  | j                  |             nd|v r3| j                  |d      }|j                  | j                  |             nTd|v rP| j                  |dg       }t	        dt        |            D ]%  }|j                  | j                  ||                ' t	        dt        |            D ]^  }||   }|d   }d|z   }| j                  |dd      }	|	|d<   | j                  |	      |d	<   || j                  |<   |j                  ||       ` y )
N	market24hspot_market24hdatar   rU   ticker:rV   rW   r_   )
safe_valueappendr   parse_tickerrangelenr   rs   rz   tickersresolve)
r2   r   messager   r{   r   irU   messageHashrV   s
             r4   handle_tickerzphemex.handle_ticker   s>   X '!__Wk:FNN411&9:(__W.>?FNN4,,V45w??7FB7D1c$i( Et::47CDEq#g,' 	0AQZFH%F#f,K11';QI"+F;!%i!8F:#)DLL NN6;/	0r5   c                    K   | j                          d{    d}| j                  dd|      \  }}| j                  |d      dk(  }d}|rd|z   n||z   }| j                  |||       d{   S 7 Z7 w)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_paramsrq   subscribe_private)r2   paramstypeusePerpetualApir   s        r4   watch_balancezphemex.watch_balance'  s      !!!99.$PVWf**68<F 3BkK/{HZ++D+vFFF 	" Gs"   A5A1AA5,A3-A53A5c                 :   || j                   d<   t        dt        |            D ]R  }||   }| j                  |d      }| j	                  |      }| j                  | j                  |i       }| j                  |dd      }	| j                         }
| j                  |d      }|a| j                  |d      }|;| j                  |d      }| j                  |d	d
      }t        j                  ||      }| j                  ||	      }| j                  |d      }|%| j                  |dd      }| j                  ||	      }||
d<   ||
d<   |
| j                   |<   | j                  | j                         | _         U |dz   }|j                  | j                   |       y )Nrp   r   currencyrI      totalUsedBalanceRvtotalUsedBalanceEvlockedTradingBalanceEvlockedWithdrawEvlockedWithdrawRvaccountBalanceRvaccountBalanceEv	balanceEvusedtotalr   )balancer   r   rq   safe_currency_coder   
currenciesrC   accountsafe_string_2r   rx   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**:6DtbAH%%ha@EllnG##G-ABD|))'3GH>-1-=-=gG_-`*'+'9'9'CUWi'j$$//0FHXYF||FE2$$W.@AE},,W6H+VWe4"GFO$GG!(DLL,,T\\:DL-	;. Z't||[1r5   c                    d}| j                  |d      }| j                  |      }|d   }|dz   |z   }| j                  | j                  |      }|7| j	                  | j
                  dd      }	t        |	      }|| j                  |<   | j                  |ddg       }
| j                  |
|      }t        dt        |            D ]  }|j                  ||           |j                  ||       y )	NtraderU   :r(   r'   tradestrades_pr   )rq   rr   r   r   rC   r-   r   safe_value_2parse_tradesr   r   r   r   )r2   r   r   namer|   rE   rU   r   storedlimitr   parsedr   s                r4   handle_tradeszphemex.handle_trades  s    6 ##GX6!!(+!Sj6)f5>%%dllM4HE&F"(DKK""7Hj"E""662q#f+& 	%AMM&)$	%v{+r5   c                    | j                  |d      }| j                  |      }|d   }| j                  |ddg       }| j                  |dg       }| j                  |d      }| j	                  |      }	|	d|	z   dz   |z   }
| j                  ||      }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |	      }|:| j                  | j                  dd	      }t        |      }|| j                  |   |	<   t        dt        |            D ]  }||   }|j                  |        |j                  ||
       y y )
NrU   klinekline_pr   rR   kline:r   r)   r'   )rq   rr   r   r   find_timeframeparse_ohlcvsohlcvsrC   r-   r   r   r   r   r   )r2   r   r   r|   rE   rU   candlesfirstinterval	timeframer   r   r   r   r   candles                   r4   handle_ohlcvzphemex.handle_ohlcv  s]   @ ##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;/ !r5   rU   c                 \  K   | j                          d{    | j                  |      }|d   }|d   }|d   dk(  }d}|r|rdnd}| j                  d	   d
   }| j                         }|dz   }	d|z   }
|	|g d}| j	                  ||      }| j                  ||
||	       d{   S 7 7 w)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>`
        NrU   swapr   r   r   perp_market24h_pack_pr   r&   r   
.subscriber   methodidr   )r   rE   r,   rS   r/   watch)r2   rU   r   rE   isSwapsettleIsUSDTr   urlrQ   subscriptionHashr   	subscriberequests                r4   watch_tickerzphemex.watch_ticker  s      !!!V$!h'61.:*Diit$OO%	,.&(&
	
 ""9f5ZZ[';KLLL% 	"$ Ms"   B,B(BB,#B*$B,*B,symbolsc                 J  K   | j                          d{    | j                  |dd      }|d   }| j                  |      }|d   }|d   dk(  }d}|r|rdnd	}| j                  d
   d   }| j	                         }	|dz   }
g }t        dt        |            D ]  }|j                  d||   z           |
|	g d}| j                  ||      }| j                  ||||       d{   }| j                  ri }|||d   <   |S | j                  | j                  d|      S 7 7 <w)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   rU   )r   market_symbolsrE   r,   rS   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{   r   s                    r4   watch_tickerszphemex.watch_tickers  sS     !!!%%gtU;
U#h'61.:*Diit$OO%	,.q#g,' 	9A  WQZ!78	9 '
	
 ""9f5**3wVV??F'-F6(#$M##DLL(GDD5 	"* Ws"   D#DCD#$D!%:D#!D#sincer   c                   K   | j                          d{    | j                  |      }|d   }| j                  d   d   }| j                         }|d   }|d   dk(  }	|r|	rdnd	}
d
|z   }|
dz   }|||d   gd}| j	                  ||      }| j                  ||||       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 9w)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>`
        NrU   r&   r   r   r   r   trade_pr   ztrade:r   r   r   rV   T)	r   rE   r,   rS   r/   r   r   getLimitfilter_by_since_limit)r2   rU   r   r   r   rE   r   rQ   r   r   r   r   r   r   r   r   s                   r4   watch_tradeszphemex.watch_trades*  s     !!!V$!iit$OO%	h'61#y7'$t
	 ""9f5zz#{G[II??OOFE2E))&%TRR+ 	"$ Js"   C!CBC!%C&8C!C!c                   K   | j                          d{    | j                  |      }|d   }| j                  d   d   }| j                         }|d   }|d   dk(  }|r|rdnd	}	d
|z   }
|	dz   }|||d   gd}| j	                  ||      }| j                  ||
||
       d{   }|j                         S 7 7 w)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
        NrU   r&   r   r   r   r   orderbook_p	orderbookz
orderbook:r   r   r   )r   rE   r,   rS   r/   r   r   )r2   rU   r   r   rE   r   rQ   r   r   r   r   r   r   r   r   s                  r4   watch_order_bookzphemex.watch_order_bookO  s      !!!V$!iit$OO%	h'61!'L}{"V+$t
	 ""9f5**S+wLL	  ' 	"$ Ms"   B>B:BB>%B<&B><B>1mc                   K   | j                          d{    | j                  |      }|d   }| j                  d   d   }| j                         }|d   }	|d   dk(  }
|	r|
rdnd	}d
|z   dz   |z   }|dz   }|||d   | j	                  | j
                  |      gd}| j                  ||      }| j                  ||||       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 9w)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
        NrU   r&   r   r   r   r   r   r   r   r   r   r   r   r   T)r   rE   r,   rS   rC   
timeframesr/   r   r   r   r   )r2   rU   r   r   r   r   rE   r   rQ   r   r   r   r   r   r   r   ohlcvs                    r4   watch_ohlcvzphemex.watch_ohlcvr  s&     !!!V$!iit$OO%	h'61#y7*S069$t!!$//9=
	 ""9f5jjk7KHH??NN651E))%q$GG- 	"& Is"   DC>B/DD 8D Dc                 N    | j                  |dd|      }|j                  |       y )Nr   rR   )custom_parse_bid_ask
storeArray)r2   booksidedeltarE   bidAsks        r4   custom_handle_deltazphemex.custom_handle_delta  s&    **5!Q?F#r5   c                 d    t        dt        |            D ]  }| j                  |||   |        y )Nr   )r   r   r  )r2   r   deltasrE   r   s        r4   custom_handle_deltaszphemex.custom_handle_deltas  s4    q#f+& 	BA$$Xvay&A	Br5   c           
      T   | j                  |d      }| j                  |      }|d   }| j                  |d      }| j                  |d      }d}|dz   |z   }	| j                  |d      }
| j                  |dd      }|d	k(  re| j	                  |d
di       }| j                  |||dddd|      }|
|d<   | j                  ||      }|| j                  |<   |j                  ||	       y || j                  v r| j                  |   }| j                  |d
di       }| j                  |dg       }| j                  |dg       }| j                  |d   ||       | j                  |d   ||       |
|d<   ||d<   | j                  |      |d<   || j                  |<   |j                  ||	       y y )NrU   r   depthr   r   sequencerV   rW   snapshotbookr   bidsasksr   rR   noncer_   )rq   rr   rC   rs   r   custom_parse_order_book
order_book
orderbooksr   safe_dict_2	safe_listr  rz   )r2   r   r   r|   rE   rU   r   r  r   r   r  rV   r
  r	  r   changesr  r  s                     r4   handle_order_bookzphemex.handle_order_book  s   X ##GX6!!(+!0!!'73Sj6)!!':6--g{HM	:$$WfmRHD33D&)VU[]^`acijH %HW%8I&/DOOF#NN9k2( OOF3	**7FM2N~~gvr:~~gvr:)))F*;T6J)))F*;T6J%*	'")2	+&(,Y(?	*%*3'y+6 )r5   c                   K   | j                          d{    d}d}d}|<| j                  |      }|d   }||d   z   }|d   dk(  r| j                  |      }d|d<   | j                  d||      \  }}|!| j	                  |d      }|dk(  r|dz   n||z   }| j                  |||       d{   }	| j                  r|	j                  ||      }| j                  |	|||d      S 7 7 9w)	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:rU   r   r   r   r   T	r   rE   extendr   rq   r   r   r   filter_by_symbol_since_limit)
r2   rU   r   r   r   rE   r   r   r   r   s
             r4   watch_my_tradeszphemex.watch_my_trades  s     !!![[(FH%F%x(88Kh6)V,#)x 99/6SYZf>%%fh7F:@F:J;4R]`dRdK--dKHH??OOFE2E00tTT% 	" I"   C*C&BC*.C(/8C*(C*c                 t   d}t        |      }|dk(  ry | j                  }|(| j                  | j                  dd      }t	        |      }i }d }t        dt        |            D ]g  }	||	   }
| j                  |
d      }| j                  |      }| j                  |
      }|j                  |       |d   }||d   dk(  rdn|d	   }d
||<   i t        |j                               }t        dt        |            D ]!  }	||	   }|dz   |z   }|j                  ||       # |dz   |z   }|j                  ||       y )Nr   r   r(   r'   rU   r   r   r   r   Tr   )r   myTradesrC   r-   r   r   rq   rr   parse_trader   listkeysr   )r2   r   r   channeltradesLengthcachedTradesr   	marketIdsr   r   rawTrader|   rE   r   rU   r  hashr   s                     r4   handle_my_tradeszphemex.handle_my_trades  s\   | 7|1}}%%dllM4HE1%8L	q#g,' 		%AqzH''(;H%%h/F%%h/F'H%F|'-h'76'A{v $If		% INN$%q#d)$ 	/A!WFS=6)DNN<.	/
 md*|[1r5   c                   K   | j                          d{    d}d}d}|<| j                  |      }|d   }||d   z   }|d   dk(  r| j                  |      }d|d<   | j                  d||      \  }}| j	                  |d      dk(  }||r|dz   n||z   }| j                  |||       d{   }	| j                  r|	j                  ||      }| j                  |	|||d      S 7 7 9w)	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:rU   r   r   r   r   Tr  )
r2   rU   r   r   r   r   rE   r   isUSDTSettledorderss
             r4   watch_orderszphemex.watch_orders  s     !!![[(FH%F%x(88Kh6)V,#)x 99-QWXf((:fD>:G;4{]aOaK--dKHH??OOFE2E00tTT% 	" Ir  c                    g }g }d|v sd|v sd|v r| j                  |dg       }| j                  |dg       }| j                  ||      }t        |      }|dk(  ry | j                  |dg       }t        dt        |            D ])  }	||	   }
| j	                  |
      }|j                  |       + nkt        dt        |            D ]S  }	||	   }| j                  |d      }||dk7  r|j                  |       | j                  |      }|j                  |       U | j                  ||       | j                  | j                  dd      }i }| j                  t        |      | _        d }| j                  }t        dt        |            D ]F  }	||	   }|j                  |       |d	   }| j                  |      }||d
   dk(  }|rdn|d   }d||<   H t        |j                               }t        dt        |            D ]&  }	d||	   z   }|j!                  | j                  |       ( d|z   }|j!                  | j                  |       y )Nclosedfillsr[   r   actionCancelordersLimitr'   rU   r   r   r   r   Tr(  )r   array_concatr   r   parse_orderr   rq   parse_ws_swap_orderr&  rC   r-   r*  r   rE   r  r  r   )r2   r   r   r   parsedOrdersr-  r[   r*  ordersLengthr   rawOrderparsedOrderupdater/  r   r#  r   r   r   rU   rE   isUsdtr  currentMessageHashr   s                            r4   handle_orderszphemex.handle_orders  sc   ~ W%7Vw=N__Wh;F??7FB7D&&tV4Fv;Lq __Wgr:F1c&k* 1!!9"..x8##K01
 1c'l+ 1 ))&(;&Vx-?MM&)"66v>##K01 	ff-!!$,,tD	;;07DKq#l+, 	%A!!_FMM&!H%F[[(F|)V3&,{&. $If	% INN$%q#d)$ 	<A!/$q'!9NN4;;(:;	<  $&t{{K0r5   c           
      "   | j                  |d      }| j                  |d      }|t        |      dk  rd }| j                  |d      }| j                  ||      }|d   }| j                  | j                  |d            }| j	                  |d      }| j                  | j                  |d            }	| j                  |d| j                  | j                  |d	      |            }
| j                  |d
      }| j                  |d      }| j                  |d      }| j                  |dd      }| j                  |d| j                  | j                  |d      |            }| j                  |dd      }|dk(  rd }| j                  | j                  |d            }| j                  |d      }|dk(  }| j                  i d|d|d|d| j                  |      d|d|d|d|	d|d|d|d|
d|d |d!|d"|d#||d |d d d$|      S )%NorderIDclOrdIDrR   rU   	ordStatussideordTypepriceRppriceEporderQtycumQty	leavesQtyactionTimeNsrW   
cumValueRv
cumValueEvtransactTimeNsr   timeInForcestopPxPOrp   r   clientOrderIdr_   rV   lastTradeTimestampr   postOnlyprice	stopPricetriggerPriceamountfilled	remaining)costrk   statusfeer   )rq   r   rr   parse_order_statussafe_string_lowerparseOrderTyperF   rs   rK   parse_time_in_force
safe_orderrz   )r2   orderrE   r   rO  r|   rU   rY  rA  r   rR  rU  rV  rW  rV   rX  rP  rL  rS  rQ  s                       r4   r4  zphemex.parse_ws_swap_ordern  s   v eY/((	:%C,>,B M##E84!!(F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! 
" # 
$ - 
. / 	r5   c                    | j                  |d      }||j                  v r*|j                  |   }|j                  |= |dur
 |||       y | j                  |dd      }d|v sd|v s|j                  d      dk\  r| j                  ||       y d	|v sd
|v r| j	                  ||       y d|v sd|v r| j                  ||       y d|v sd|v r| j                  ||       y d|v sd|v r&| j                  |ddi       }| j                  ||       d|v sd|v sd|v r7d|v rdnd}d|v rd}| j                  |g dg       }| j                  |||       y y )Nr   Tr    r   r   r   r   r   r   r   r   r
  r   r*  orders_paccounts
accounts_pwalletsr   spotr   )rd  re  rf  )rq   subscriptionsfindr   r   r   r  r   r<  safe_value_nr   )	r2   r   r   r   r   
methodNamer*  r   rd  s	            r4   handle_messagezphemex.handle_message  s   @ gt,%%%))"-F$$R(T!vw'%%gx<
7"(8G(CYpIquvIvvw/'!zW'<vw/ i7&:fg.]g%=""673Z7%:&&w*bIFvv.'!|w'>IQXDX(G36&Dw&"((2WY[\Hfh7 EYr5   c                 0   | j                  |d      }| j                  |d      }d}|dk(  r|j                  ||       y t        | j                  dz   | j                  |      z         }|j                  ||       ||j                  v r|j                  |= y y )Nr   rY  authenticatedsuccess )r   rq   r   r   r   jsonrejectrh  )r2   r   r   r   rY  r   errors          r4   handle_authenticatezphemex.handle_authenticate  s     (3!!&(3%YNN7K0'#		'8J(JKEMM%-f222((5 3r5   c                   K   | j                          d {    | j                          d {    | j                  d   d   }| j                         }| j	                  |dd      dk(  }| j                  |d      }d}|dk(  rd}|rd	}||g d
}| j                  ||      }| j                  ||||       d {   S 7 7 7 w)Nr&   r   r   rb  r   zaop.subscriberg  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     !!!!!!iit$LLN	"=G68,!6>$G'G

 ++gv.ZZ['7CCC# 	"!  Ds2   CB>CC B
C9C:C CCc                   K   | j                          | j                  d   d   }| j                  |      }| j                         }d}| j	                  |j
                  |      }|| j                  | j                  dd      }| j                         |z   }| j                  t        |      z   }	| j                  | j                  |	      | j                  | j                        t        j                        }
d}|d| j                  |
|g|d}t        |      }| j!                  ||      }||j
                  vr| j"                  |j
                  |<   | j%                  ||||       d {   }||j
                  |<   |S 7 w)	Nr&   r   rn  expiresx   z	user.authAPI)r   r   r   )check_required_credentialsr,   r   rS   r   rh  rC   r-   rw  apiKeyr;   hmacencodesecrethashlibsha256r  rt  r   )r2   r   r   r   rQ   r   futureexpiryDelta
expirationpayload	signaturer   r   r   r   s                  r4   rv  zphemex.authenticate  s]    '')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'62G6#7#779=9Q9Q$$%56::c;MMF06F  - Ns   E'F)E?*Fr7   )0__name__
__module____qualname__r   r1   r?   rF   rK   rO   rS   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<  r4  rl  rt  r   rv  __classcell__)r3   s   @r4   r   r      s5   !# !FI
I
I

:x7r_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 8<$]ajl %H %HC %HWZ %Hquvzq{ %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:L1F L1\hT|8V |8|6& 6* AC 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     sB     f f  f f f 4  0 %GT&& Gr5   