
    ih                         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 d dlmZ d dlmZ d dlmZ d dlmZ  G d d	ej        j                  ZdS )
    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)BalancesIntOrder	OrderBookStrTickerTrade)Client)List)AuthenticationError)Precisec            
           e Zd Z 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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 )&phemexc                     |                      t          t          |                                           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watchOHLCVForSymbolsr   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    U/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/phemex.pyr,   zphemex.describe   s    fd 3 3 < < > ># %#!%#"&""&,1).(- " ; /	   $" 
 U9A
 A
   	    c                     |d S t          |          }|                     |j        |          |_        |                                 t	          |          S N)r   sumdecimalsreducestr)r-   enscaleprecises       r/   from_enzphemex.from_en4   sL    :4"++88G$4e<<7||r0   Nc                 b    |||S |                      ||                     |d                    S )N
priceScaler:   safe_integer)r-   epmarkets      r/   from_epzphemex.from_ep<   4    JFNI||B 1 1&, G GHHHr0   c                 b    |||S |                      ||                     |d                    S )N
valueScaler=   )r-   evr@   s      r/   from_evzphemex.from_evA   rB   r0   c                 b    |||S |                      ||                     |d                    S )N
ratioScaler=   )r-   err@   s      r/   from_erzphemex.from_erF   rB   r0   c                 z    |                      |                     | j        dd          d          }|| j        d<   |S )N	requestIdr      )r3   r>   r(   )r-   rL   s     r/   
request_idzphemex.request_idK   s;    HHT..t|[!LLaPP	$-[!r0   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}|S )Nsymbol	timestampư>closeturnovervolumeopen21100datetimehighlowbid	bidVolumeask	askVolumevwaplastpreviousClosechange
percentageaverage
baseVolumequoteVolumeinfo)safe_stringsafe_marketsafe_integer_productrA   parse_numberrF   	omit_zeror   
string_sub
string_div
string_add
string_muliso8601)r-   tickerr@   marketIdrP   rQ   
lastStringrb   ri   rh   rd   re   rf   
openStringrV   results                   r/   parse_swap_tickerzphemex.parse_swap_tickerP   sD   " ##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#
$ %&)
 
 
, r0   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   rP            rM   rW   rX   rY   rQ   rZ   r[      r\      r]   r^   r_   r`   ra   rV   rS   rb   rc   rd   re   rf   rg   )rk   rl   rA   rn   rF   ro   r   rp   rq   rr   rs   )r-   ru   r@   rv   rP   rw   rb   ri   rh   rd   re   rf   rx   rV   ry   s                  r/   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#
$ %&)
 
 
, r0   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   rP   ticker:rQ   rR   rZ   )
safe_valueappendrz   parse_tickerrangelenr   rm   rt   tickersresolve)
r-   r   messager   ru   r   irP   messageHashrQ   s
             r/   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 	0r0   returnc                    K   |                                   d{V  d}|                     dd|          \  }}|                     |d          dk    }d}|rd|z   n||z   }|                     |||           d{V S )a  
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-account-order-position-aop
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-account-order-position-aop
        :see: 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>`
        NwatchBalancesettleUSDT:balance	perpetual)load_marketshandle_market_type_and_paramsrk   subscribe_private)r-   paramstypeusePerpetualApir   s        r/   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Fr0   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 )Nrj   r   currencyrD      totalUsedBalanceRvtotalUsedBalanceEvlockedTradingBalanceEvlockedWithdrawEvlockedWithdrawRvaccountBalanceRvaccountBalanceEv	balanceEvusedtotalr   )balancer   r   rk   safe_currency_coder   
currenciesr>   accountsafe_string_2r   rr   r:   safe_balancer   )r-   r   r   r   r   r   
currencyIdcoder   r8   r   r   usedEvr   r   r   totalEvr   s                     r/   handle_balancezphemex.handle_balance6  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1r0   c                 "   d}|                      |d          }|                     |          }|d         }|dz   |z   }|                     | j        |          }|5|                     | j        dd          }	t          |	          }|| j        |<   |                     |ddg           }
|                     |
|          }t          dt          |                    D ]}|                    ||                    |                    ||           d S )	NtraderP   :r#   r"   tradestrades_pr   )rk   rl   r   r   r>   r(   r   safe_value_2parse_tradesr   r   r   r   )r-   r   r   namerv   r@   rP   r   storedlimitr   parsedr   s                r/   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{+++++r0   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 )
NrP   klinekline_pr   rM   kline:r   r$   r"   )rk   rl   r   r   find_timeframeparse_ohlcvsohlcvsr>   r(   r   r   r   r   r   )r-   r   r   rv   r@   rP   candlesfirstinterval	timeframer   r   r   r   r   candles                   r/   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;///// ! r0   rP   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  
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-24-hours-ticker
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-24-hours-ticker
        :see: 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>`
        NrP   swapr   r   r   perp_market24h_pack_pr   r!   r   
.subscriber   methodidr   )r   r@   r'   rN   r*   watch)r-   rP   r   r@   isSwapsettleIsUSDTr   urlrL   subscriptionHashr   	subscriberequests                r/   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Lr0   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'  
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-trade
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-trade
        :see: 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>`
        NrP   r!   r   r   r   r   trade_pr   ztrade:r   r   r   rQ   T)	r   r@   r'   rN   r*   r   
newUpdatesgetLimitfilter_by_since_limit)r-   rP   r   r   r   r@   r   rL   r   r   r   r   r   r   r   r   s                   r/   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Rr0   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  
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Spot-API-en.md#subscribe-orderbook
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-orderbook-for-new-model
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-30-levels-orderbook
        :see: 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
        NrP   r!   r   r   r   r   orderbook_p	orderbookz
orderbook:r   r   r   )r   r@   r'   rN   r*   r   r   )r-   rP   r   r   r@   r   rL   r   r   r   r   r   r   r   r   s                  r/   watch_order_bookzphemex.watch_order_book  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	   r0   1mc                 <  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  
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Hedged-Perpetual-API.md#subscribe-kline
        :see: https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md#subscribe-kline
        :see: 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
        NrP   r!   r   r   r   r   r   r   r   r   r   r   r   r   T)r   r@   r'   rN   r>   
timeframesr*   r   r   r   r   )r-   rP   r   r   r   r   r@   r   rL   r   r   r   r   r   r   r   ohlcvs                    r/   watch_ohlcvzphemex.watch_ohlcv>  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Gr0   c                 `    |                      |dd|          }|                    |           d S )Nr   rM   )custom_parse_bid_ask
storeArray)r-   booksidedeltar@   bidAsks        r/   custom_handle_deltazphemex.custom_handle_deltac  s5    **5!Q??F#####r0   c                     t          dt          |                    D ]}|                     |||         |            d S )Nr   )r   r   r   )r-   r   deltasr@   r   s        r/   custom_handle_deltaszphemex.custom_handle_deltasg  sN    q#f++&& 	B 	BA$$Xvay&AAAA	B 	Br0   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        |          }||                     |d
di           }| 	                    |dg           }| 	                    |dg           }| 
                    |d         ||           | 
                    |d         ||           |
|d<   ||d<   |                     |          |d<   || j        |<   |                    ||	           d S d S )NrP   r   depthr   r   sequencerQ   rR   snapshotbookr   bidsasksr   rM   noncerZ   )rk   rl   r>   rm   r   custom_parse_order_book
order_book
orderbooksr   r   r   rt   )r-   r   r   rv   r@   rP   r   r   r   r   r  rQ   r   r   r   changesr  r   s                     r/   handle_order_bookzphemex.handle_order_bookk  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@@I$++GV]BOOw;;w;;)))F*;T6JJJ)))F*;T6JJJ%*	'")2	+&(,Y(?(?	*%*3'y+66666 %$r0   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]{@link https://docs.ccxt.com/#/?id=trade-structure
        Nztrades:rP   r   r   r   r   T	r   r@   extendr   rk   r   r   r   filter_by_symbol_since_limit)
r-   rP   r   r   r   r@   r   r   r   r   s
             r/   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Tr0   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"   rP   r   r   r   r   Tr   )r   myTradesr>   r(   r   r   rk   rl   parse_trader   listkeysr   )r-   r   r   channeltradesLengthcachedTradesr   	marketIdsr   r   rawTraderv   r@   r   rP   r  hashr   s                     r/   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1r0   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:rP   r   r   r   r   Tr	  )
r-   rP   r   r   r   r   r@   r   isUSDTSettledorderss
             r/   watch_orderszphemex.watch_ordersM  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Tr0   c                 j   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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fillsrV   r   actionCancelordersLimitr"   rP   r   r   r   r   Tr  )r   array_concatr   r   parse_orderr   rk   parse_ws_swap_orderr  r>   r(   r  r   r@   r  r  r   )r-   r   r   r   parsedOrdersr  rV   r  ordersLengthr   rawOrderparsedOrderupdater!  r   r  r   r   r   rP   r@   isUsdtr  currentMessageHashr   s                            r/   handle_orderszphemex.handle_ordersj  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
 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0r0   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clOrdIDrM   rP   	ordStatussideordTypepriceRppriceEporderQtycumQty	leavesQtyactionTimeNsrR   
cumValueRv
cumValueEvtransactTimeNsr   timeInForcestopPxPOrj   r   clientOrderIdrZ   rQ   lastTradeTimestampr   postOnlyprice	stopPricetriggerPriceamountfilled	remaining)costrf   statusfeer   )rk   r   rl   parse_order_statussafe_string_lowerparseOrderTyperA   rm   rF   parse_time_in_force
safe_orderrt   )r-   orderr@   r   rA  rv   rP   rK  r3  r   rD  rG  rH  rI  rQ   rJ  rB  r>  rE  rC  s                       r/   r&  zphemex.parse_ws_swap_order8  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! 
" # 
$ - 
  
  
. /  	r0   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   )rV  rW  rX  )rk   subscriptionsfindr   r   r   r  r   r.  safe_value_nr   )	r-   r   r   r   r   
methodNamer  r   rV  s	            r/   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DXr0   c                 J   |                      |d          }|                     |d          }d}|dk    r|                    ||           d S t          | j        dz   |                     |          z             }|                    ||           ||j        v r
|j        |= d S d S )Nry   rK  authenticatedsuccess )r   rk   r   r   r   jsonrejectrZ  )r-   r   r   ry   rK  r   errors          r/   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2r0   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   rT  r   zaop.subscriberY  zwo.subscribezaop_p.subscribe)r   r   r   )r   authenticater'   secondsr   omitr
  r   )	r-   r   r   r   r   rL   r   r  r   s	            r/   r   zphemex.subscribe_privateu  s     !!!!!!!!!!!!!!!!!!it$LLNN	"==G68,,!6>>$G 	('G
 

 ++gv..ZZ['7CCCCCCCCCr0   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   r`  expiresx   z	user.authAPI)r   r   r   )check_required_credentialsr'   r   rN   r   rZ  r>   r(   ri  apiKeyr6   hmacencodesecrethashlibsha256r
  rf  r   )r-   r   r   r   rL   r   futureexpiryDelta
expirationpayload	signaturer   r   r   r   s                  r/   rh  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 -r0   r2   ),__name__
__module____qualname__r,   r:   rA   rF   rJ   rN   rz   r   r   r   r   r   r   r   r   r6   r   r   r   r   r   r   r	   r   r  r   r   r   r  r
   r  r  r   r  r.  r&  r^  rf  r   rh  __classcell__)r.   s   @r/   r   r      s2                D  I I I I
I I I I
I I I I
  
8 8 8 8t7 7 7 7r_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< <@d[] !S !S !SS !S !Sbfglbm !S !S !S !SF @DB ! !S ! !S\ ! ! ! !B 8<$]ajl #H #H #HC #HWZ #Hquvzq{ #H #H #H #HJ$ $ $ $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:L1F L1 L1 L1 L1\h h h hT|8V |8 |8 |8 |8|6& 6 6 6 6* AC D D D D( )+        r0   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   rt  ccxt.base.typesr   r   r   r	   r
   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   ccxt.base.preciser   async_supportr    r0   r/   <module>r     s        f f f f f f f f f f  O O O O O O O O O O O O O O O O O O 4 4 4 4 4 4       0 0 0 0 0 0 % % % % % %Q Q Q Q QT& Q Q Q Q Qr0   