
    i	                         d dl Zd dlmZ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 d dlmZ d d	lmZ  G d
 dej        j                  ZdS )    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)
BalancesIntOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)AuthenticationError)ArgumentsRequired)
BadRequest)InvalidNoncec            
           e Zd Z fdZdi fdededefdZdefdZ	defd	Z
d
 Zd Zd Zi fdedefdZdi fdedefdZdefdZdi fdedefdZdefdZddi fdededefdZdedefdZddi fdedededee         fdZddi fde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di fdedededee         fdZ!defdZ"i fde#fd Z$defd!Z%dddi fdedededee&         fd"Z'd6dedefd#Z(d$ Z)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efd(Z.d6defd)Z/defd*Z0defd+Z1d, Z2d- Z3d7d/Z4d0efd1Z5d2 Z6i dfd3Z7i fd4Z8d7d5Z9 xZ:S )8gatec                 @   |                      t          t          |                                           ddddddddddddddddddd	dd
ddddddddddddddddi i i ddiddddddddddd	dd t          t
          t
          t
          d!iid"          S )#NT)wswatchOrderBookwatchTickerwatchTickerswatchTradeswatchTradesForSymbolswatchMyTrades
watchOHLCVwatchBalancewatchOrderswatchPositionszwss://ws.gate.io/v4zwss://api.gateio.ws/ws/v4/z wss://fx-ws.gateio.ws/v4/ws/usdtzwss://fx-ws.gateio.ws/v4/ws/btc)usdtbtcz)wss://fx-ws.gateio.ws/v4/ws/delivery/usdtz(wss://fx-ws.gateio.ws/v4/ws/delivery/btcz"wss://op-ws.gateio.live/v4/ws/usdtz!wss://op-ws.gateio.live/v4/ws/btc)r   spotswapfutureoptionz(wss://fx-ws-testnet.gateio.ws/v4/ws/usdtz'wss://fx-ws-testnet.gateio.ws/v4/ws/btcz*wss://op-ws-testnet.gateio.live/v4/ws/usdtz)wss://op-ws-testnet.gateio.live/v4/ws/btc)r(   r)   r*   )apitest  nametickers100ms
      )intervalsnapshotDelaysnapshotMaxRetriesr%   zspot.balances)settler'   )fetchPositionsSnapshotawaitPositionsSnapshot)	tradesLimit
OHLCVLimitwatchTradesSubscriptionswatchTickerSubscriptionswatchOrderBookSubscriptionsr   r   r"   r$   r   exact)24611)hasurlsoptions
exceptions)deep_extendsuperr   describer   r   )self	__class__s    S/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/gate.pyrI   zgate.describe   s   dD 1 1 : : < <"&# $#)-!%" $#"&  08 B@ 
 !LI 
 !EB  $ !KH 
 !KH 
 !MJ  # B  $",.,./1I  !(%'*+# # %+! !
 /3.2# #% 0 '001	 	KO?
 O?
 O O O	    Nsymbollimitreturnc                   K   |                                   d{V  |                     |          }|d         }|d         }|                     |ddd          \  }}|                     |          }|dz   }	d|z   }
|                     |          }||g}|d	}|d
         r$t          |          }|                    |           ||d}|                     ||
||	||           d{V }|                                S )a  
        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
        NrN   idr   r3   r0   z.order_book_update
orderbook:d   contract)rN   rO   )	load_marketsmarkethandle_option_and_paramsget_type_by_marketget_url_by_marketstrappendsubscribe_publicrO   )rJ   rN   rO   paramsrW   marketIdr3   querymessageTypechannelmessageHashurlpayloadstringLimitsubscription	orderbooks                   rL   watch_order_bookzgate.watch_order_bookf   s>      !!!!!!!!!V$$!$<77@PR\^eff%--f55 44'&0$$V,,X&=E* 	(e**KNN;'''
 
 //['7TY[ghhhhhhhh	   rM   clientc                     |                      |d          }|                     |d          }|                     i |          | j        |<   d S )NrN   rO   )safe_stringsafe_integer
order_book
orderbooks)rJ   rj   messagerg   rN   rO   s         rL   handle_order_book_subscriptionz#gate.handle_order_book_subscription   sL    !!,99!!,88"&//"e"<"<rM   c                 @   |                      |d          }|                    d          }|                      |d          }|dk    }|rdnd}|                     |d          }|                     |d          }	|                     |d          }
|                      |d	          }|                     |d d
|          }d|z   }|                     | j        ||                     i                     }|                     |d          }|d}|t          |j                  }| 	                    ddd          }|r|nd}||k    rB|j
        |         }|                     |d          }|                     | j        ||||i            |j                            |           d S ||
k    rd S ||	dz
  k    r|                     ||           n=t          | j        dz             }|j
        |= | j        |= |                    ||           |                    ||           d S )Nrb   .r   r'   rU   resultUus_rS   noncer   r4   r1   rO      z+ orderbook update has a nonce bigger than u)rl   split
safe_valuerm   safe_symbolro   rn   lencachehandle_optionsubscriptionsspawnload_order_bookr\   handle_deltar   rR   rejectresolve)rJ   rj   rp   rb   channelPartsrawMarketTypeisSpot
marketTypedelta
deltaStartdeltaEndr_   rN   rc   storedOrderBookry   cacheLengthr4   
waitAmountrg   rO   errors                         rL   handle_order_bookzgate.handle_order_book   sF   l ""7I66}}S))((q99&(%5VV:
22&&uc22
$$UC00##E3//!!(D#zBB"V+//$/64??SUCVCVWW!!/7;;=K*!/"788 ../?RTUUM*07aJj((%3K@)),@@

4/feUWXXX!((///FhFj1n$$ou5555 +X!XYYE$[1'MM%---44444rM   c                 f   |                      |d          }|d         }|                      |d          }||k     rdS t          dt          |                    D ]I}||         }|                      |d          }|                      |d          }	||dz
  k    r
||	k     r|c S Jt          |          S )Nry   r   ru   rv   rz   )rm   ranger~   )
rJ   	orderBookr   ry   
firstDeltafirstDeltaStartir   r   r   s
             rL   get_cache_indexzgate.get_cache_index   s    !!)W551X
++J<<?""2q#e**%% 	 	A!HE**5#66J((44Ha''eh.>.>5zzrM   c                 V   t          dt          |                    D ]}||         }t          |t                    r)|                    |                     |                     H|                     |d          }|                     |d          }|                    ||           d S )Nr   prw   )r   r~   
isinstancelist
storeArrayparse_bid_ask
safe_floatstore)rJ   bookSidebidAsksr   bidAskpriceamounts          rL   handle_bid_askszgate.handle_bid_asks   s    q#g,,'' 	. 	.AQZF&$'' .##D$6$6v$>$>????4455uf----	. 	.rM   c                 r   |                      |d          }||d<   |                     |          |d<   |                      |d          |d<   |                     |dg           }|                     |dg           }|d         }|d	         }|                     ||           |                     ||           d S )
Nt	timestampdatetimerv   ry   babidsasks)rm   iso8601r|   r   )rJ   rh   r   r   r   r   
storedBids
storedAskss           rL   r   zgate.handle_delta   s    %%eS11	!*	+ $Y 7 7	*!..uc::	'uc2..uc2..v&
v&
Z...Z.....rM   c                    K   |                                   d{V  |                     |          }|d         }d|d<   |                     |g|           d{V }|                     ||          S )a  
        :see: https://www.gate.io/docs/developers/apiv4/ws/en/#tickers-channel
        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>`
        NrN   r   callerMethodName)rV   rW   watch_tickersr|   )rJ   rN   r^   rW   rt   s        rL   watch_tickerzgate.watch_ticker  s       !!!!!!!!!V$$!%2!"))6(F;;;;;;;;vv...rM   symbolsc                 l   K   |                      |d|                     ddi|                     d{V S )a  
        :see: https://www.gate.io/docs/developers/apiv4/ws/en/#tickers-channel
        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
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        r   methodr/   N%subscribe_watch_tickers_and_bids_asksextendrJ   r   r^   s      rL   r   zgate.watch_tickers  sr       ??Y]YdYdfnpyez  }C  ZD  ZD  E  E  E  E  E  E  E  E  	ErM   c                 4    |                      d||           d S )Ntickerhandle_ticker_and_bid_askrJ   rj   rp   s      rL   handle_tickerzgate.handle_ticker!  s"    & 	&&xAAAAArM   c                 l   K   |                      |d|                     ddi|                     d{V S )a  
        :see: https://www.gate.io/docs/developers/apiv4/ws/en/#best-bid-or-ask-price
        :see: https://www.gate.io/docs/developers/apiv4/ws/en/#order-book-channel
        watches best bid & ask for symbols
        :param str[] symbols: 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>`
        watchBidsAsksr   book_tickerNr   r   s      rL   watch_bids_askszgate.watch_bids_asks6  sr       ??Z^ZeZegoq~f  BH  [I  [I  J  J  J  J  J  J  J  J  	JrM   c                 4    |                      d||           d S )Nbidaskr   r   s      rL   handle_bid_askzgate.handle_bid_askA  s"    $ 	&&xAAAAArM   r   c                   K   |                                   d {V  |                     |d|          \  }}|                     |d d          }|                     |d                   }|                     |          }|                     |          }d }|                     ||d          \  }}|                     |          }|dz   |z   }	|                    d          dk    }
|
rdnd}g }t          dt          |                    D ]%}||         }|                    |d	z   |z              &|                     ||||	|           d {V }| j        ri }|||d
         <   |S |
r| j        n| j        }|                     |d
|d          S )Nr   Fr   r   rs   r   r   r   :rN   T)rV   handle_param_stringmarket_symbolsrW   rY   
market_idsrX   rZ   findr   r~   r\   subscribe_public_multiple
newUpdatesr/   bidsasksfilter_by_array)rJ   r   r   r^   rW   ra   	marketIdschannelNamerd   rb   isWatchTickersprefixmessageHashesr   rN   tickerOrBidAskitemsrt   s                     rL   r   z*gate.subscribe_watch_tickers_and_bids_asksU  s     !!!!!!!!!#'#;#;FDVXh#i#i &%%gtU;;WQZ((--f55OOG,,	";;FDTV^__V$$V,,#k1)..}==B+9q#g,,'' 	8 	8AQZF  #!67777#==c=R[]dflmmmmmmmm? 	E.<E.*+L!/BT]##FHgtDDDrM   
objectNamec                    |                      |d          }|                    d          }|                      |d          }|dk    rdnd}|                     |d          }g }	t          |t                    r|                     |dg           }	n|                     |di           }
|
g}	|dk    }t          dt          |	                    D ]}|	|         }
|                      |
d	          }| 	                    |d d
|          }| 
                    |
|          }|d         }|r|| j        |<   n
|| j        |<   |dz   |z   }|                    ||           d S )Nrb   rs   r   futuresrU   r'   rt   r   rw   rx   rN   r   )rl   r{   r|   r   r   	safe_list	safe_dictr   r~   safe_marketparse_tickerr/   r   r   )rJ   r   rj   rp   rb   partsr   r   rt   results	rawTickerisTickerr   r_   rW   
parsedItemrN   rc   s                     rL   r   zgate.handle_ticker_and_bid_askn  s   ""7I66c""((22$1Y$>$>ZZV
(33fd## 	"nnWh;;GGw"==I kG(*q#g,,'' 	4 	4A
I''	377H%%hc:FFF**9f==J)F 3'1V$$(2f%$s*V3KNN:{3333	4 	4rM   sincec                 D   K   |                      |g|||           d{V S )  
        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>`
        N)watch_trades_for_symbols)rJ   rN   r   rO   r^   s        rL   watch_tradeszgate.watch_trades  s6       22F8UE6RRRRRRRRRrM   c                   K   |                                   d{V  |                     |          }|                     |          }|                     |d                   }|                     |          }|dz   }g }	t          dt          |                    D ]"}
||
         }|	                    d|z              #|                     |          }| 	                    ||	|||           d{V }| j
        rB|                     |d          }|                     |d          }|                    ||          }|                     |||dd          S )r   Nr   z.tradestrades:rN   r   T)rV   r   r   rW   rY   r   r~   r\   rZ   r   r   r|   rl   getLimitfilter_by_since_limit)rJ   r   r   rO   r^   r   rW   ra   rb   r   r   rN   rd   tradesfirsttradeSymbols                   rL   r   zgate.watch_trades_for_symbols  sn      !!!!!!!!!%%g..OOG,,	WQZ((--f55	)q#g,,'' 	5 	5AQZF  V!34444$$V,,55c=)U\^deeeeeeee? 	8OOFA..E**5(;;KOOK77E))&%TRRRrM   c                    |                      |d          }t          |t                    s|g}|                     |          }t	          dt          |                    D ]}||         }|d         }|                      | j        |          }|5|                     | j        dd          }	t          |	          }|| j        |<   |
                    |           d|z   }
|                    ||
           d S )Nrt   r   rN   r9   r-   r   )r|   r   r   parse_tradesr   r~   r   rm   rE   r   r\   r   )rJ   rj   rp   rt   parsedTradesr   traderN   cachedTradesrO   hashs              rL   handle_tradeszgate.handle_trades  s   " (33&$'' 	XF((00q#l++,, 
	/ 
	/A OE8_F??4;??L#))$,tLL)%00&2F#&&&v%DNN<....
	/ 
	/rM   1mc                   K   |                                   d{V  |                     |          }|d         }|d         }|                     | j        ||          }|                     |          }	|	dz   }
d|z   dz   |d         z   }|                     |          }||g}|                     ||||
|           d{V }| j        r|                    ||          }| 	                    |||dd          S )	aQ  
        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
        NrN   rR   z.candlestickscandles:r   r   T)
rV   rW   rl   
timeframesrY   rZ   r]   r   r   r   )rJ   rN   	timeframer   rO   r^   rW   r_   r3   ra   rb   rc   rd   re   ohlcvs                  rL   watch_ohlcvzgate.watch_ohlcv  s#      !!!!!!!!!V$$!$<##DOY	JJ--f55/ 8+c1F84DD$$V,,X&++CgwPVWWWWWWWW? 	2NN6511E))%q$GGGrM   c                    |                      |d          }|                    d          }|                      |d          }|dk    rdnd}|                     |d          }t          |t                    s|g}i }t          dt          |                    D ]F}	||	         }
|                      |
dd          }|                    d	          }|                      |d          }|                     |          }|d	z   }|                    |d          }| 	                    |d d	|          }| 
                    |
          }|                     | j        |i           | j        |<   |                     | j        |         |          }|;|                     | j        d
d          }t          |          }|| j        |         |<   |                    |           |||<   Ht	          |                                          }t          dt          |                    D ]i}	||	         }||         }|                     |          }d|z   dz   |z   }|                     | j        |         |          }|                    ||           jd S )Nrb   rs   r   r'   rU   rt   n rx   r:   r-   r   r   )rl   r{   r|   r   r   r   r~   find_timeframereplacer}   parse_ohlcvohlcvsrm   rE   r   r\   keysr   )rJ   rj   rp   rb   r   r   r   rt   r   r   r  rg   r   r   timeframeIdr   r_   rN   parsedstoredrO   r
  r3   r   s                           rL   handle_ohlcvzgate.handle_ohlcv  s   " ""7I66}}S))((q99 - 7 7VVj
(33&$'' 	XF	q#f++&& 	* 	*A1IE++E3;;L &&s++E((22I--i88K_F#++FB77H%%hc:FFF%%e,,F"&//$+vr"J"JDK__T[%8)DDF~))$,dKK.u5539F#K0MM&!!! )IfINN$$%%q#d))$$ 	) 	)A!WF!&)I**955H"X-3f<D__T[%8(CCFNN64((((	) 	)rM   c           	      @  K   |                                   d{V  d}d}d}d}||                     |          }|d         }|                     d||          \  }}|                     d||          \  }}|                     |dddddd          }	|	d	z   }
d
}||d|z   z  }|dk    }|                     ||          }|g}|dk    }|                     ||||
||           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
        N!allrR   r    r'   r   rE   r'   marginr)   r(   r*   z.usertradesmyTradesr   inverseT)
rV   rW   handle_market_type_and_paramshandle_sub_type_and_paramsget_supported_mappingget_url_by_market_typesubscribe_privater   r   filter_by_symbol_since_limit)rJ   rN   r   rO   r^   subTypetyper_   rW   ra   rb   rc   	isInverserd   re   requiresUidr   s                    rL   watch_my_tradeszgate.watch_my_trades  s      !!!!!!!!![[((Fd|H99/6SYZZf99/6SYZZ008
 8
   - 3<'K	)	))$	::*v~--c;RXZeffffffff? 	3OOFE22E00tTTTrM   c                    |                      |dg           }t          |          }|dk    rd S | j        }|2|                     | j        dd          }t          |          }|| _        |                     |          }i }t          dt          |                    D ],}	||	         }
|                    |
           |
d         }d||<   -t          |
                                          }t          dt          |                    D ]%}	||	         }d|z   }|                    ||           &|                    |d           d S )	Nrt   r   r9   r-   rN   Tz	myTrades:r  )r|   r~   r  rm   rE   r   r   r   r\   r   r
  r   )rJ   rj   rp   rt   tradesLengthr   rO   r  r   r   r   rN   r
  rW   r   s                  rL   handle_my_tradeszgate.handle_my_tradesD  sR   * (B776{{1F}%%dlM4HHE1%88L(DM""6**	q#f++&& 	% 	%A1IE&&&8_F $IfINN$$%%q#d))$$ 	/ 	/A!WF'DNN<....|Z00000rM   c           	      z  K   |                                   d{V  d}d}|                     dd|          \  }}|                     dd|          \  }}|dk    }|                     ||          }|dk    }|                     |dddddd          }|dz   }|d	z   }	|                     ||	d|||           d{V S )
a  
        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
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr"   r  r'   r   rE   r  z	.balances.balance)rV   r  r  r  r  r  )
rJ   r^   r  r  r  rd   r  channelTyperb   rc   s
             rL   watch_balancezgate.watch_balancep  s      !!!!!!!!!99.$PVWWf99.$PVWW	)	))$	::v~008
 8
   +Z'++CdGVU`aaaaaaaaarM   c                 @   |                      |dg           }|                     |d          }|| j        d<   || j        d<   |                     |          | j        d<   t	          dt          |                    D ]}||         }|                                 }|                     |dd          }|                     |          }	|                     |d	          |d
<   | 	                    |dd          |d<   || j        |	<   |                     |d          }
|

                    d          }|                     |d          }|                     |dddd          }|dz   }|                     | j                  | _        |                    | j        |           d S )Nrt   time_msinfor   r   r   currencyUSDT	availablefreetotalbalancerb   rs   r'   r(   r*   )r'   r   rE   r$  )r|   rm   r/  r   r   r~   accountrl   safe_currency_codesafe_string_2r{   r  safe_balancer   )rJ   rj   rp   rt   r   r   
rawBalancer0  
currencyIdcoderb   r   rawTyper%  rc   s                  rL   handle_balancezgate.handle_balance  s   z (B77%%gy99	%V$-[!#'<<	#:#:Z q#f++&& 	) 	)AJllnnG))*j&IIJ**:66D"..z;GGGFO#11*gyQQGG!(DL""7I66c""""5!,,00;
 ;
  
 "J.((66t|[11111rM   c                   K   |                                   d{V  d}|                     |          }dg}|                     |          s|                     |          }d}d}|                     d||          \  }}|dk    rd}|                     |dddd          }	|d	z   }
|                     |          s|
d
d                    |          z   z  }
|	dz   }d}|                     d||          \  }}|dk    }|                     ||          }| 	                    |          }| 
                    |||           |                     ddd          }|                     ddd          }|                     | j        |          }|r"|r ||                    |dz              d{V S |                     ||
|||d           d{V }| j        r|S |                     | j        |         |||d          S )a<  
        :see: https://www.gate.io/docs/developers/futures/ws/en/#positions-subscription
        :see: https://www.gate.io/docs/developers/delivery/ws/en/#positions-subscription
        :see: https://www.gate.io/docs/developers/options/ws/en/#positions-channel
        watch all open positions
        :param str[]|None symbols: list of unified market symbols
        :param dict params: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
        Nr  r$   r'   r(   r   rE   )r)   r(   r*   
:positions::,z
.positionsr  r7   Tr8   :fetchPositionsSnapshot)rV   r   is_emptyget_market_from_symbolsr  r  joinr  r  rj   set_positions_cacher   	safe_boolr|   	positionsr)   r  r   filter_by_symbols_since_limit)rJ   r   r   rO   r^   rW   re   r  r`   typeIdrc   rb   r  r  rd   rj   r7   r8   r   rC  s                       rL   watch_positionszgate.watch_positions  sp      !!!!!!!!!%%g..-}}W%% 	;11'::F889I6SYZZe6>>D++D3
 3
  
 \)}}W%% 	44#((7"3"333K<'889I6SXYY	)	))$	::S!!  w777!%!3!34DF^`d!e!e!%0@BZ\`!a!a55! 	I&< 	It.G'GHHHHHHHHH00k7GUZ\`aaaaaaaa	? 	11$.2FQVX]_cdddrM   c                    | j         i | _         || j         v rd S |                     ddd          }|rD|dz   }||j        vr4|                    |           |                     | j        |||           d S d S t                      | j         |<   d S )Nr$   r7   Fr=  )rC  r   r   r)   r   load_positions_snapshotr   )rJ   rj   r  r   r7   rc   s         rL   rA  zgate.set_positions_cache  s    >!DN4>!!F!%!3!34DF^`e!f!f! 	>!::K6>11k***

47dSSSSS 21 $<#=#=DN4   rM   c                 |  K   |                      d d|i           d {V }t                      | j        |<   | j        |         }t          dt	          |                    D ]}||         }|                    |            |j        |         }|                    |           |                    ||dz              d S )Nr  r   z	:position)fetch_positionsr   rC  r   r~   r\   r   r   )	rJ   rj   rc   r  rC  r   r   positionr)   s	            rL   rH  zgate.load_positions_snapshot  s      ..tfd^DDDDDDDD	799tt$q#i..)) 	# 	#A |HLL"""",uud[011111rM   c                    |                      |j                  }|                     |dg           }| j        |         }g }t	          dt          |                    D ]I}||         }|                     |          }	|                    |	           |                    |	           J|                     ||dz             }
t	          dt          |
                    D ]}|
|         }|	                    d          }|d         }|	                    d          }| 
                    |d|d          }|                     |          s|                    ||           |                    ||d	z              d S )
Nrt   r   z:positions::r;  rz   r<  rN   Fr:  )get_market_type_by_urlrd   r|   rC  r   r~   parse_positionr\   find_message_hashesr{   r   r>  r   )rJ   rj   rp   r  datar   newPositionsr   rawPositionrK  r   rc   r   symbolsStringr   rC  s                   rL   handle_positionszgate.handle_positions'  sx   > **6:66w"55t$q#d))$$ 	# 	#Aq'K**;77H)))LL""""009NOOq#m,,-- 	7 	7A'*K%%d++E!!HM#))#..G,,\8WeTTI==++ 7y+666|TL%899999rM   c           	      ^  K   |                                   d{V  d}||                     |          }|d         }d}d}|                     d||          \  }}|                     |dddddd          }|dz   }	d	}
d
g}||
d|d         z   z  }
|d         g}d}|                     d||          \  }}|dk    }|                     ||          }|dk    }|                     ||
||	||           d{V }| j        r|                    ||          }| 	                    |||d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
        :param str [params.type]: spot, margin, swap, future, or option. Required if listening to all symbols.
        :param boolean [params.isInverse]: if future, listen to inverse or linear contracts
        :returns dict[]: a list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure
        NrN   r#   r'   r   rE   r  z.ordersordersr  r   rR   r  r   T)
rV   rW   r  r  r  r  r  r   r   r   )rJ   rN   r   rO   r^   rW   r  r`   rE  rb   rc   re   r  r  rd   r  rV  s                    rL   watch_orderszgate.watch_ordersZ  s      !!!!!!!!![[((FH%F88PVWWe++D3
 3
   9$-3--Kd|nG88PUVV	)	))$	::v~--c;RWYdeeeeeeee? 	3OOFE22E))&%TRRRrM   c                    |                      |dg           }|                     | j        dd          }| j        t	          |          | _        | j        }i }|                     |          }t          dt          |                    D ]}||         }	|                      |	d          }
|                     |
d          }|dk    s|dk    rd	|	d
<   nA|dk    r;|                     |	d
          }|#| 	                    |
d          }|dk    rdnd|	d
<   |
                    |	           |	d         }|                     |          }d||d         <   t          |                                          }t          dt          |                    D ](}d||         z   }|                    | j        |           )|                    | j        d           d S )Nrt   ordersLimitr-   r   r)  eventputupdateopenstatusfinishleftclosedcanceledrN   TrR   zorders:rV  )r|   rm   rE   rV  r   parse_ordersr   r~   rl   safe_numberr\   rW   r   r
  r   )rJ   rj   rp   rV  rO   r  r   parsedOrdersr   r  r)  rZ  r^  r`  rN   rW   r
  rc   s                     rL   handle_orderzgate.handle_order  s   H (B77!!$,tDD;077DK	((00q#l++,, 	+ 	+A!!_F??6622D$$T733E~~(!2!2#)x  (""))&(;;>++D&99D48AIIxxJF8$MM&!!!H%F[[((F&*IfTl##INN$$%%q#d))$$ 	5 	5A#d1g-KNN4;4444t{H-----rM   c                    |                      |d          }|                     |d          }|                     |d          }|dS ||                     |j        |          }|{	 |                     | j        d         d         ||                     |                     n># t          $ r1}|                    ||           ||j        v r|j        |= Y d }~nd }~ww xY w|j        |= dS dS )Nr   r6  rR   Fr   r>   T)	r|   rm   rl   r   throw_exactly_matched_exceptionrF   json	Exceptionr   )rJ   rj   rp   r   r6  rR   rc   es           rL   handle_error_messagezgate.handle_error_message  s   " 11  //gt,,:5**6+?DDK&>889Nw9WY]_c_h_hip_q_qrrrr  > > >MM![111"f&:::"0=> $R(4us   ';B# #
C-'CCc                     i | _         d S N)r/  )rJ   rj   rp   rg   s       rL   handle_balance_subscriptionz gate.handle_balance_subscription  s    rM   c                 L   |                      |d          }| j        | j        | j        d}|                      |d          }||v rK|                      |j        |          }|                     |j        |          }||         } ||||           ||j        v r
|j        |= d S d S )Nrb   )r/  zspot.order_book_updatezfutures.order_book_updaterR   )rl   ro  rq   r   r|   )	rJ   rj   rp   rb   methodsrR   subscriptionHashrg   r   s	            rL   handle_subscription_statuszgate.handle_subscription_status  s    ""7I667&*&I)-)L
 

 gt,,g#//0DbII??6+?AQRRLW%FF67L111%%%$R((( &%rM   c           
         |                      ||          rd S |                     |d          }|dk    r|                     ||           d S |                     |dd          }|                    d          }|                     |d          }| j        | j        | j        | j        | j	        | j
        | j        | j        | j        d	}|                     ||          }| |||           d S d S )NrZ  	subscriberb   r  rs   rz   )	
usertradescandlesticksrV  rC  r/   r   r   order_book_updatebalances)rl  rl   rs  r{   r|   r"  r  rf  rT  r   r   r   r   r8  )	rJ   rj   rp   rZ  rb   r   r%  	v4Methodsr   s	            rL   handle_messagezgate.handle_message  s   t $$VW55 	F  '22K++FG<<<F""7Ir::}}S))oolA66/ -'.).(!%!7+

 

	 K88F67##### rM   c                 x    | j         d         |d                  }|d         r|d         r|d         n|d         S |S )Nr+   r  rU   linearr%   r&   rD   )rJ   rW   baseUrls      rL   rZ   zgate.get_url_by_marketn  sE    )E"6&>2* 	&,X&6J76??GENJNrM   c                 .    |d         rdS |d         rdS dS )Nr'   r*   rE   r    )rJ   rW   s     rL   rY   zgate.get_type_by_marketu  s*    &> 	6H 	99rM   Fc                 l    | j         d         }||         }|dk    s|dk    r|r|d         n|d         S |S )Nr+   r(   r)   r&   r%   r~  )rJ   r  r  r+   rd   s        rL   r  zgate.get_url_by_market_type}  sF    i$iFNN 0 0!*;3u::F;JrM   rd   c                     dddd}t          |                                          }t          dt          |                    D ]/}||         }||         }|                    |          dk    r|c S 0dS )Nr*   r)   r(   )zop-deliveryfxr   r'   )r   r
  r   r~   r   )rJ   rd   findByr
  r   keyvalues          rL   rM  zgate.get_market_type_by_url  s     
 

 FKKMM""q#d))$$ 	 	Aq'C3KExx}}!! "vrM   c                 z    |                      |                     | j        dd          d          }|| j        d<   |S )Nreqidr   rz   )sumrm   rE   )rJ   r  s     rL   
request_idzgate.request_id  s:    **4<!DDaHH %WrM   c                 B  K   |                                  }|                                 }|||d|d}	|7|                     |          }
||
j        vrt	          |          }||
j        |<   |                     |	|          }|                     |||||           d {V S Nru  )rR   timerb   rZ  re   )r  secondsrj   r   r[   r   watch)rJ   rd   rc   re   rb   r^   rg   	requestIdr  requestrj   tempSubscriptionHashrp   s                rL   r]   zgate.subscribe_public  s      OO%%	||~~ 
 
 #[[%%F6#777'*9~~$=H$%9:++gv..ZZ[';UUUUUUUUUrM   c                    K   |                                  }|                                 }|||d|d}|                     ||          }	|                     |||	|           d {V S r  )r  r  r   watch_multiple)
rJ   rd   r   re   rb   r^   r  r  r  rp   s
             rL   r   zgate.subscribe_public_multiple  s~      OO%%	||~~ 
 
 ++gv..((mWmTTTTTTTTTrM   c                 $  K   |                                   |rY| j        t          | j                  dk    rt          | j        dz             | j        g}||}n|                     ||          }|                                 }d}	d|z   dz   dz   |	z   dz   dz   t          |          z   }
|                     | 	                    |
          | 	                    | j
                  t          j        d          }d	| j        |d
}|                                 }|||d|d}|||d<   |                     |          }||j        vrt          |          }||j        |<   |                     ||          }|                     ||||           d {V S )Nr   z requires uid to subscriberu  zchannel=&zevent=ztime=hexapi_key)r   KEYSIGN)rR   r  rb   rZ  authre   )check_required_credentialsuidr~   r   rR   array_concatr  r[   hmacencodesecrethashlibsha512apiKeyr  rj   r   r   r  )rJ   rd   rc   re   rb   r^   r  idArrayr  rZ  signaturePayload	signaturer  r  r  rj   r  rp   s                     rL   r  zgate.subscribe_private  s     ''))) 	>x3tx==A#5#5'2N(NOOOxjG!++GW==||~~%/#5@5H3NQXX[^_c[d[ddIIdkk*:;;T[[=U=UW^Weglmm	;
 

 OO%%	 
 
 !(GIS!!v333#&y>> 9DF !56++gv..ZZ[';GGGGGGGGGrM   rn  )F);__name__
__module____qualname__rI   r[   r   r
   ri   r   rq   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&  r8  r   rF  rA  rH  rT  r	   rW  rf  rl  ro  rs  r{  rZ   rY   r  rM  r  r]   r   r  __classcell__)rK   s   @rL   r   r      sL       P P P P Pd @DB ! !S ! !S\ ! ! ! !<=V = = = =
Y5 Y5 Y5 Y5 Y5v  . . .
/ 
/ 
/ 68 / / /F / / / / 6:" E E7 E E E E EBF B B B B* 8<B 	J 	JW 	J7 	J 	J 	J 	JBV B B B B( NRkoxz E E7 Eeh E  @G E E E E24C 4 4 4 4 44 <@d[] 	S 	S 	SS 	S 	Sbfglbm 	S 	S 	S 	S OSaenp S Sd3i S S[^ Suyz  vA S S S S6/F / / / /B 8<$]ajl H H HC HWZ Hquvzq{ H H H H22)6 2) 2) 2) 2)h 37TX\eg &U &UC &Us &URU &Ulpqvlw &U &U &U &UP*1v *1 *1 *1 *1X *, b b b b b b2T2V T2 T2 T2 T2l 8<$]ajl ,e ,eW ,eC ,eWZ ,equv~q ,e ,e ,e ,e\> >& > > > > >
2 
2 
21: 1: 1:f 04$UYbd )S )S )SC )Ss )Simnsit )S )S )S )SV?.6 ?. ?. ?. ?.B!6 ! ! ! !F &    ) ) ) ) ) p$V p$ p$ p$ p$d       #       QSae V V V V$ \^ U U U U$H $H $H $H $H $H $H $HrM   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   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   r   r   r   async_supportr   r  rM   rL   <module>r     sf       A  A  A  A  A  A  A  A  A  A  A  A  k k k k k k k k k k k k k k k k k k k k k k k k 4 4 4 4 4 4       0 0 0 0 0 0 . . . . . . ' ' ' ' ' ' ) ) ) ) ) )JH JH JH JH JH4" JH JH JH JH JHrM   