
    E#i^                    *   S SK rS SKJrJrJrJr  S SKrS SKJ	r	J
r
JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  S SKJr  S SKJr  S SK J!r!  S SK J"r"  S SK J#r#  S S	K J$r$  S S
K J%r%  S SK J&r&  S SK'J(r(   " S S\RR                  RT                  5      r*g)    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesBoolIntLiquidationMarket
MarketTypeNumOrder	OrderBookOrderRequest	OrderSide	OrderTypePositionStrStringsTickerTickersTrade)Client)List)ExchangeError)AuthenticationError)ArgumentsRequired)
BadRequest)NotSupported)ChecksumError)Precisec                     ^  \ rS rSrS\4U 4S jjrS0 4S\S\S\S\	S	\
4
S
 jjr0 4S\\   4S jjrS0 4S\4S jjrS0 4S\S\4S jjrSS0 4S\S\S\S\S\
S	\
4S jjrS0 4S\S\4S jjrSSS0 4S\S\S\S\\   4S jjrSSS0 4S\S\S\S\\   4S jjrSSS0 4S\S\S\S\S\\   4
S jjrS0 4S\S\S\4S jjr0 4S\S\4S jjrS\4S jrS\4S jrS rS r S r!0 4S\S\"4S  jjr#S0 4S!\$S\%4S" jjr&S\4S# jr'S0 4S!\$S\%4S$ jjr(S\4S% jr)SS0 4S!\$S&\S\%4S' jjr*S(\S\4S) jr+SS0 4S\S\S\S\\,   4S* jjr-SS0 4S!\\   S\S\S\\,   4S+ jjr.0 4S!\\   S\4S, jjr/0 4S\S\4S- jjr0S\4S. jr1S/SS0 4S\S0\S\S\S\\2   4
S1 jjr3S\4S2 jr4SSS0 4S\S\S\S\\,   4S3 jjr5S\4S4 jr60 4S\74S5 jjr8S\4S6 jr9SSS0 4S!\$S\S\S\\:   4S7 jjr;SXS\S!\$4S8 jjr<S9 r=S: r>SSS0 4S\S\S\S\\   4S; jjr?S\4S< jr@SS0 4S\S\S\S\\A   4S= jjrBSS0 4S!\\   S\S\S\\A   4S> jjrCS\4S? jrDSXS@ jrES\S\F4SA jrGSXS\4SB jjrHS\4SC jrIS\4SD jrJSE\K4SF jrLS\4SG jrMSH rNSI\O4SJ jrPSYS\Q4SK jjrRSL\4SM jrSSN rT0 S4SO jrU0 4SP jrV0 4SQ jrWSR rXS\4SS jrYSXST\4SU jjrZSYSV jr[SWr\U =r]$ )Zgate   returnc                   > U R                  [        [        U ]  5       0 SS_SS_SS_SS_SS_SS_S	S_S
S_SS_SS_SS_SS_SS_SS_SS_SS_SS_0 SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_S S_S!S_S"S_S#S_S$S_ESSSS%.ES&S'S(S)S*.S+S,S*.S-S.S*.S/.S0S1S*.S0S1S*.S2S3S*.S4.S5.S6S60 0 0 S7S80S9S:S;SS<.S=S>S?.SSS@.SA.	S[        [        [
        [
        [
        SB.0 SC.0SD.5      $ )ENwsTcancelAllOrdersWscancelOrderWscreateMarketBuyOrderWithCostWscreateMarketOrderWscreateMarketOrderWithCostWsFcreateMarketSellOrderWithCostWscreateOrderWscreateOrdersWscreatePostOnlyOrderWscreateReduceOnlyOrderWscreateStopLimitOrderWscreateStopLossOrderWscreateStopMarketOrderWscreateStopOrderWscreateTakeProfitOrderWscreateTriggerOrderWseditOrderWsfetchOrderWsfetchOrdersWsfetchOpenOrdersWsfetchClosedOrdersWswatchOrderBookwatchBidsAskswatchTickerwatchTickerswatchTradeswatchTradesForSymbolswatchMyTrades
watchOHLCVwatchBalancewatchOrderswatchLiquidationswatchLiquidationsForSymbols)watchMyLiquidationswatchMyLiquidationsForSymbols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)rP   rQ   rR   )apitest  nametickers100ms
      )intervalsnapshotDelaysnapshotMaxRetrieschecksumrM   zspot.balances)settlerO   )fetchPositionsSnapshotawaitPositionsSnapshot)	tradesLimit
OHLCVLimitwatchTradesSubscriptionswatchTickerSubscriptionswatchOrderBookSubscriptionsr@   r>   rF   rL   )124611)exactbroad)hasurlsoptions
exceptions)deep_extendsuperr$   describer   r   )self	__class__s    G/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/gate.pyrt   gate.describe   s   dD : <&d&#T&  & 1$	&
 &t& .u& 25&  & !$& (& *4& )$& (& *5& $T&  *4!&" '#&$ t%&& '&(  )&* $T+&, &t-&. !$/&0  1&2 t3&4 5&6 t7&8 (9&:  ;&< d=&> ?&@ tA&B $UC&D .uE&F (,15"&K&R 08 B@
 !LI
 !EB$ !KH
 !KH
 !MJ#B  $",.,./1I  !(%'*+ $	# %+!
 /3.2#'2 ''001  	Al?
 l l	    Nsymboltypesideamountpricec                 |  #    U R                  5       I Sh  vN   U R                  U5      nUS   nU R                  U5      nUS-   n	U R                  U5      n
SUS'   U R	                  XX4XV5      nU R                  X5      I Sh  vN   U R                  XU	5      I Sh  vN nU R                  X5      nU$  N N3 N7f)a  

https://www.gate.io/docs/developers/apiv4/ws/en/#order-place
https://www.gate.io/docs/developers/futures/ws/en/#order-place

Create an order on the exchange
:param str symbol: Unified CCXT market symbol
:param str type: 'limit' or 'market' *"market" is contract only*
:param str side: 'buy' or 'sell'
:param float amount: the amount of currency to trade
:param float [price]: *ignored in "market" orders* the price at which the order is to be fulfilled at in units of the quote currency
:param dict [params]:  extra parameters specific to the exchange API endpoint
:param float [params.stopPrice]: The price at which a trigger order is triggered at
:param str [params.timeInForce]: "GTC", "IOC", or "PO"
:param float [params.stopLossPrice]: The price at which a stop loss order is triggered at
:param float [params.takeProfitPrice]: The price at which a take profit order is triggered at
:param str [params.marginMode]: 'cross' or 'isolated' - marginMode for margin trading if not provided self.options['defaultMarginMode'] is used
:param int [params.iceberg]: Amount to display for the iceberg order, Null or 0 for normal orders, Set to -1 to hide the order completely
:param str [params.text]: User defined information
:param str [params.account]: *spot and margin only* "spot", "margin" or "cross_margin"
:param bool [params.auto_borrow]: *margin only* Used in margin or cross margin trading to allow automatic loan of insufficient amount if balance is not enough
:param str [params.settle]: *contract only* Unified Currency Code for settle currency
:param bool [params.reduceOnly]: *contract only* Indicates if self order is to reduce the size of a position
:param bool [params.close]: *contract only* Set to close the position, with size set to 0
:param bool [params.auto_size]: *contract only* Set side to close dual-mode position, close_long closes the long side, while close_short the short one, size also needs to be set to 0
:param int [params.price_type]: *contract only* 0 latest deal price, 1 mark price, 2 index price
:param float [params.cost]: *spot market buy only* the quote quantity that can be used alternative for the amount
:returns dict|None: `An order structure <https://docs.ccxt.com/#/?id=order-structure>`
Nrz   z.order_placeTtextIsRequired)load_marketsmarketget_type_by_marketget_url_by_marketcreate_order_requestauthenticaterequest_privateparse_order)ru   rz   r{   r|   r}   r~   paramsr   messageTypechannelurlrequestrawOrderorders                 rw   create_order_wsgate.create_order_ws   s     < !!!V$!--f5.$$V,#' ++F$V111--cGDD  2 	" 	2Ds4   B<B6A-B<B8B<B:B<8B<:B<ordersc                   #    U R                  5       I Sh  vN   U R                  X5      nUS   nU R                  US   5      nUS   SLa  [        U R                  S-   5      eU R                  U5      nUS-   nU R                  U5      nU R                  X5      I Sh  vN   U R                  XU5      I Sh  vN n	U R                  X5      $  N N1 N7f)a  
create a list of trade orders

https://www.gate.io/docs/developers/futures/ws/en/#order-batch-place

:param Array orders: list of orders to create, each object should contain the parameters required by createOrder, namely symbol, type, side, amount, price and params
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
Nr   rz   rP   Tz1 createOrdersWs is not supported for swap marketsz.order_batch_place)
r   createOrdersRequestr   r    idr   r   r   r   parse_orders)
ru   r   r   r   
firstOrderr   r   r   r   	rawOrderss
             rw   create_orders_wsgate.create_orders_ws   s      !!!**6:AY
Z12&>%tww)\\]]--f5 44$$V,111..sWEE	  33 	" 	2Es4   CCB	C C!C:C;CCCc                 b  #    U R                  5       I Sh  vN   Uc  SOU R                  U5      nU R                  USS5      nU R                  U5      nUS-   nU R	                  USSU5      u  pbU R                  U5      nU R                  USS/5      nU R                  SX25      u  pUS:X  a  U R                  X4U	5      OU R                  X8U	5      u  pU R                  Xu5      I Sh  vN   U R                  XpR                  X5      U5      I Sh  vN nU R                  X5      $  GN NA N7f)	aZ  
cancel all open orders

https://www.gate.io/docs/developers/futures/ws/en/#cancel-all-open-orders-matched
https://www.gate.io/docs/developers/apiv4/ws/en/#order-cancel-all-with-specified-currency-pair

:param str symbol: unified market symbol, only orders in the market of self symbol are cancelled when symbol is not None
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.channel]: the channel to use, defaults to spot.order_cancel_cp or futures.order_cancel_cp
:returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Nstoptriggerz.order_cancel_cpr)   r   cancelAllOrdersrO   )r   r   safe_bool_2r   handle_option_and_paramsr   omithandle_market_type_and_paramsmultiOrderSpotPrepareRequestprepareRequestr   r   extendr   )ru   rz   r   r   r   r   r   r   r{   queryr   requestParamsr   s                rw   cancel_all_orders_wsgate.cancel_all_orders_ws   sF     !!! .t{{6/B""669=--f5 2277@SU^`gh$$V,6FI#67889JF[_cgm_m!B!B6TY!Ztx  uH  uH  IO  W\  u]111..sKK4WY`aa	  33 	" 	2as4   D/D(CD/)D+*(D/D-D/+D/-D/r   c                 b  #    U R                  5       I Sh  vN   Uc  SOU R                  U5      nU R                  U/ SQS5      nU R                  U/ SQ5      nU R	                  SXC5      u  pgUS:X  d  US:X  a  U R                  XEU5      OU R                  XFU5      u  pU R                  U5      n
U
S-   nU R                  U5      nU R                  X5      I Sh  vN   [        U5      US'   U R                  XR                  X5      U5      I Sh  vN nU R                  X5      $  GN NO N7f)	a  
Cancels an open order

https://www.gate.io/docs/developers/apiv4/ws/en/#order-cancel
https://www.gate.io/docs/developers/futures/ws/en/#order-cancel

:param str id: Order id
:param str symbol: Unified market symbol
:param dict [params]: Parameters specified by the exchange api
:param bool [params.trigger]: True if the order to be cancelled is a trigger order
:returns: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
N)is_stop_orderr   r   FcancelOrderrO   marginz.order_cancelorder_id)r   r   safe_value_nr   r   spotOrderPrepareRequestr   r   r   r   strr   r   r   )ru   r   rz   r   r   r   r{   r   r   r   r   r   r   ress                 rw   cancel_order_wsgate.cancel_order_ws   sA     !!! .t{{6/B##F,PRWX6#GH88WZ^bhZhlpt|l|!=!=fu!U  DH  DW  DW  X^  fk  Dl--f5/$$V,111!"g
((kk'.QSZ[[,, 	" 	2[s4   D/D(CD/D+6D/D-D/+D/-D/c           	      f  #    U R                  5       I Sh  vN   U R                  U5      nU R                  XX4XVU5      n	U R                  U5      n
U
S-   nU R	                  U5      nU R                  X5      I Sh  vN   U R                  XU5      I Sh  vN nU R                  X5      $  N N1 N7f)a   
edit a trade order, gate currently only supports the modification of the price or amount fields

https://www.gate.io/docs/developers/apiv4/ws/en/#order-amend
https://www.gate.io/docs/developers/futures/ws/en/#order-amend

:param str id: order id
:param str symbol: unified symbol of the market to create an order in
:param str type: 'market' or 'limit'
:param str side: 'buy' or 'sell'
:param float amount: how much of the currency you want to trade in units of the base currency
:param float [price]: the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
Nz.order_amend)r   r   edit_order_requestr   r   r   r   r   )ru   r   rz   r{   r|   r}   r~   r   r   extendedRequestr   r   r   r   s                 rw   edit_order_wsgate.edit_order_ws   s       !!!V$11"d&Y_`--f5.$$V,111--cGLL11 	" 	2Ls4   B1B+A$B1;B-<B1B/B1-B1/B1c                   #    U R                  5       I Sh  vN   Uc  SOU R                  U5      nU R                  XU5      u  pVU R                  U5      nUS-   nU R	                  U5      n	U R                  X5      I Sh  vN   U R                  XR                  XV5      U5      I Sh  vN n
U R                  X5      $  N N@ N7f)a  
Retrieves information on an order

https://www.gate.io/docs/developers/apiv4/ws/en/#order-status
https://www.gate.io/docs/developers/futures/ws/en/#order-status

:param str id: Order id
:param str symbol: Unified market symbol, *required for spot and margin*
:param dict [params]: Parameters specified by the exchange api
:param bool [params.trigger]: True if the order being fetched is a trigger order
:param str [params.marginMode]: 'cross' or 'isolated' - marginMode for margin trading if not provided self.options['defaultMarginMode'] is used
:param str [params.type]: 'spot', 'swap', or 'future', if not provided self.options['defaultMarginMode'] is used
:param str [params.settle]: 'btc' or 'usdt' - settle currency for perpetual swap and future - market settle currency is used if symbol is not None, default="usdt" for swap and "btc" for future
:returns: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
Nz.order_status)	r   r   fetchOrderRequestr   r   r   r   r   r   )ru   r   rz   r   r   r   r   r   r   r   r   s              rw   fetch_order_wsgate.fetch_order_ws  s       !!! .t{{6/B!%!7!7F!K--f5/$$V,111--c;;w3VX_``11 	" 	2`s4   CB?A)C C(C)C*CCCsincelimitc                 D   #    U R                  SXX45      I Sh  vN $  N7f)a  
fetch all unfilled currently open orders

https://www.gate.io/docs/developers/futures/ws/en/#order-list

:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch open orders for
:param int [limit]: the maximum number of  open orders structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
openNfetch_orders_by_status_wsru   rz   r   r   r   s        rw   fetch_open_orders_wsgate.fetch_open_orders_ws1  s"      33FF5YYYY     c                 D   #    U R                  SXX45      I Sh  vN $  N7f)a  
fetches information on multiple closed orders made by the user

https://www.gate.io/docs/developers/futures/ws/en/#order-list

: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 Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
finishedNr   r   s        rw   fetch_closed_orders_wsgate.fetch_closed_orders_ws?  s"      33Ju]]]]r   statusc                 $  #    U R                  5       I Sh  vN   SnUb6  U R                  U5      nUS   nUS   SLa  [        U R                  S-   5      eU R	                  XX4U5      u  pxU R                  US/5      n	U R                  U5      n
U
S-   nU R                  U5      nU R                  X5      I Sh  vN   U R                  XR                  X5      U5      I Sh  vN nU R                  X5      nU R                  XX45      $  N NR N+7f)a  

https://www.gate.io/docs/developers/futures/ws/en/#order-list

fetches information on multiple orders made by the user by status
:param str status: requested order status
:param str symbol: unified market symbol of the market orders were made in
:param int|None [since]: the earliest time in ms to fetch orders for
:param int|None [limit]: the maximum number of order structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.orderId]: order id to begin at
:param int [params.limit]: the maximum number of order structures to retrieve
:returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
Nrz   rP   TzX fetchOrdersByStatusWs is only supported by swap markets. Use rest API for other marketsr_   z.order_list)r   r   r    r   prepareOrdersByStatusRequestr   r   r   r   r   r   r   filter_by_symbol_since_limit)ru   r   rz   r   r   r   r   r   r   
newRequestr   r   r   r   r   s                  rw   r   gate.fetch_orders_by_status_wsM  s!     !!![[(FH%Ff~T)"477  .H  $H  I  I!%!B!B6SXag!hYYw
3
--f5-$$V,111..sKK
4Z\cdd	""9500NN 	" 	2ds4   DD
B"D9D:(D"D#(DDDc                   #    U R                  5       I Sh  vN   U R                  U5      nUS   nUS   nU R                  USSS5      u  pgU R                  U5      nUS-   n	SU-   n
U R	                  U5      nXV/nUc  S	nUS
   (       a  [        U5      nUR                  U5        UUS.nU R                  XXX~5      I Sh  vN nUR                  5       $  N N7f)a  
watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://www.gate.com/docs/developers/apiv4/ws/en/#order-book-channel
https://www.gate.com/docs/developers/apiv4/ws/en/#order-book-v2-api
https://www.gate.com/docs/developers/futures/ws/en/#order-book-api
https://www.gate.com/docs/developers/futures/ws/en/#order-book-v2-api
https://www.gate.com/docs/developers/delivery/ws/en/#order-book-api

: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
Nrz   r   r>   r[   rX   .order_book_update
orderbook:d   contract)rz   r   )	r   r   r   r   r   r   appendsubscribe_publicr   )ru   rz   r   r   r   marketIdr[   r   r   r   messageHashr   payloadstringLimitsubscription	orderbooks                   rw   watch_order_bookgate.watch_order_bookm  s      !!!V$!$<77@PR\^ef--f5 44'&0$$V,&=E*e*KNN;'
 //'TYhh	  ) 	"& is"   CCB*CCCCc           
        #    U R                  5       I Sh  vN   U R                  U5      nUS   nUS   nSnU R                  USSU5      u  pRU R                  U5      nUS-   nSU-   nS	U-   n	U R	                  U5      n
XE/nU R                  US
S5      nUS   (       a  [        U5      nUR                  U5        U R                  U
SU/U	/U/XU5      I Sh  vN $  N N7f)a~  
unWatches 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 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
Nrz   r   rX   r>   r[   r   r   zunsubscribe:orderbook:r   r   r   r   )	r   r   r   r   r   safe_integerr   r   un_subscribe_public_multiple)ru   rz   r   r   r   r[   r   r   subMessageHashr   r   r   r   r   s                 rw   un_watch_order_bookgate.un_watch_order_book  s     !!!V$!$<88AQS]_gh--f5 44*V33f<$$V,&!!&'37*e*KNN;'66sK&T_S`cqbrt{  GM  N  N  	N! 	"  Ns"   C!CCC!CC!C!clientc                     U R                  US5      nU R                  US5      nU R                  0 U5      U R                  U'   g )Nrz   r   )safe_stringr   
order_book
orderbooks)ru   r   messager   rz   r   s         rw   handle_order_book_subscription#gate.handle_order_book_subscription  s?    !!,9!!,8"&//"e"<ry   c                 F   U R                  US5      nUR                  S5      nU R                  US5      nUS:H  nU(       a  SOSnU R                  US5      nU R                  US5      n	U R                  US5      n
U R                  US	5      nU R	                  US S
U5      nSU-   nU R                  U R
                  XR                  0 5      5      nU R                  US5      nUc  SnUb  [        UR                  5      nU R                  SSS5      nU(       a  UOSnUU:X  a@  UR                  U   nU R                  US5      nU R                  U R                  XUU0 5        UR                  R                  U5        g X:  a  g XS-
  :  a  U R                  X5        OpUR                  U	 U R
                  U	 U R                  SSS5      nU(       a<  [        U R                   S-   U R#                  U5      -   5      nUR%                  UU5        UR'                  X5        g )Nr   .r   rO   r   resultUus_r   noncer>   r\   rY   r      r^   T )r   split
safe_valuer   safe_symbolr   r   lencachehandle_optionsubscriptionsspawnload_order_bookr   handle_deltar!   r   orderbook_checksum_messagerejectresolve)ru   r   r   r   channelPartsrawMarketTypeisSpot
marketTypedelta
deltaStartdeltaEndr   rz   r   storedOrderBookr   cacheLengthr\   
waitAmountr   r   r^   errors                          rw   handle_order_bookgate.handle_order_book  s   l ""7I6}}S)((q9&(%V:
2&&uc2
$$UC0##E3/!!(D#zB"V+//$//6??SUCVW!!/7;=K*!/"7"78 ../?RTUM*0aJj(%33K@)),@

4//feUWX!!((/1n$o5$$[1'))*:JMH%dggmd6U6UV\6]&]^e[14ry   c                 $   U R                  US5      nUS   nU R                  US5      nX5:  a  g[        S[        U5      5       H>  nX&   nU R                  US5      nU R                  US5      n	X8S-
  :  d  M5  X9:  d  M<  Us  $    [        U5      $ )Nr   r   r   r   r   )r   ranger   )
ru   	orderBookr   r   
firstDeltafirstDeltaStartir
  r  r  s
             rw   get_cache_indexgate.get_cache_index  s    !!)W51X
++J<"q#e*%AHE**5#6J((4Ha'e.> & 5zry   c                    [        S[        U5      5       Hs  nX#   n[        U[        5      (       a"  UR	                  U R                  U5      5        M>  U R                  US5      nU R                  US5      nUR                  XV5        Mu     g )Nr   pr   )r  r   
isinstancelist
storeArrayparse_bid_ask
safe_floatstore)ru   bookSidebidAsksr  bidAskr~   r}   s          rw   handle_bid_asksgate.handle_bid_asks  sp    q#g,'AZF&$''##D$6$6v$>?45u- (ry   c                 &   U R                  US5      nX1S'   U R                  U5      US'   U R                  US5      US'   U R                  US/ 5      nU R                  US/ 5      nUS   nUS	   nU R                  Xd5        U R                  Xu5        g )
Nt	timestampdatetimer   r   babidsasks)r   iso8601r   r'  )ru   r   r
  r+  r/  r0  
storedBids
storedAskss           rw   r  gate.handle_delta%  s    %%eS1	!*+ $Y 7	*!..uc:	'uc2.uc2.v&
v&
Z.Z.ry   c                    #    U R                  5       I Sh  vN   U R                  U5      nUS   nSUS'   U R                  U/U5      I Sh  vN nU R                  XA5      $  NK N7f)a  

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>`
Nrz   r@   callerMethodName)r   r   watch_tickersr   )ru   rz   r   r   r   s        rw   watch_tickergate.watch_ticker1  sl      !!!V$!%2!"))6(F;;v.. 	" <s!   A&A"5A&A$A&$A&symbolsc                 h   #    U R                  USU R                  SS0U5      5      I Sh  vN $  N7f)a  

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>`
rA   methodrW   N%subscribe_watch_tickers_and_bids_asksr   ru   r:  r   s      rw   r7  gate.watch_tickersB  sL      ??Y]YdYdfnpyez  }C  ZD  E  E  	E  E   )202c                 (    U R                  SX5        g )Ntickerhandle_ticker_and_bid_askru   r   r   s      rw   handle_tickergate.handle_tickerN  s    & 	&&xAry   c                 h   #    U R                  USU R                  SS0U5      5      I Sh  vN $  N7f)a  

https://www.gate.io/docs/developers/apiv4/ws/en/#best-bid-or-ask-price
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>`
r?   r<  book_tickerNr=  r?  s      rw   watch_bids_asksgate.watch_bids_asksc  sL      ??Z^ZeZegoq~f  BH  [I  J  J  	J  JrA  c                 (    U R                  SX5        g )NbidaskrD  rF  s      rw   handle_bid_askgate.handle_bid_askp  s    $ 	&&xAry   r6  c                   #    U R                  5       I S h  vN   U R                  USU5      u  p#U R                  US S5      nU R                  US   5      nU R	                  U5      nU R                  U5      nS nU R                  X2S5      u  psU R                  U5      nUS-   U-   n	UR                  S5      S:  n
U
(       a  SOSn/ n[        S[        U5      5       H  nX   nUR                  US	-   U-   5        M      U R                  XXiU5      I S h  vN nU R                  (       a  0 nUUUS
   '   U$ U
(       a  U R                  OU R                  nU R!                  US
US5      $  GNU NW7f)Nr6  Fr   r<  r   r@   rC  rN  :rz   T)r   handle_param_stringmarket_symbolsr   r   
market_idsr   r   findr  r   r   subscribe_public_multiple
newUpdatesrW   bidsasksfilter_by_array)ru   r:  r6  r   r   r   	marketIdschannelNamer   r   isWatchTickersprefixmessageHashesr  rz   tickerOrBidAskitemsr   s                     rw   r>  *gate.subscribe_watch_tickers_and_bids_asks  ss    !!!#'#;#;FDVXh#i %%gtU;WQZ(--f5OOG,	";;FV^_$$V,#k1)..}=B+q#g,'AZF  #!67 (  $==cR[flmm??E.<E.*+L!/T]]##FHgtDD- 	"  ns#   E0E+C?E0E.AE0.E0
objectNamec                 f   U R                  US5      nUR                  S5      nU R                  US5      nUS:X  a  SOSnU R                  US5      n/ n	[        U[        5      (       a  U R                  US/ 5      n	OU R                  US0 5      n
U
/n	US:H  n[        S[        U	5      5       H  nX   n
U R                  U
S	5      nU R                  US S
U5      nU R                  X5      nUS   nU(       a  XR                  U'   OXR                  U'   US-   U-   nUR                  UU5        M     g )Nr   r   r   futuresr   rO   r   rC  r   r   rz   rR  )r   r   r   r  r  	safe_list	safe_dictr  r   safe_marketparse_tickerrW   rY  r  )ru   rc  r   r   r   partsr  r	  r   results	rawTickerisTickerr  r   r   
parsedItemrz   r   s                     rw   rE  gate.handle_ticker_and_bid_ask  s/   ""7I6c"((2$1Y$>ZV
(3fd##nnWh;Gw"=I kG(*q#g,'A
I''	37H%%hc:FF**9=J)F'1V$(2f%$s*V3KNN:{3 (ry   c                 F   #    U R                  U/X#U5      I Sh  vN $  N7f)a  
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_symbolsr   s        rw   watch_tradesgate.watch_trades  s$      22F8U6RRRRs   !!c                 T  #    U R                  5       I Sh  vN   U R                  U5      nU R                  U5      nU R                  US   5      nU R	                  U5      nUS-   n/ n	[        S[        U5      5       H  n
X   nU	R                  SU-   5        M     U R                  U5      nU R                  XXXU5      I Sh  vN nU R                  (       a5  U R                  US5      nU R                  US5      nUR                  X5      nU R                  XUSS5      $  GN Na7f)a  
get the list of most recent trades for a particular symbol
:param str[] symbols: unified symbol of the market to fetch trades for
:param int [since]: timestamp in ms of the earliest trade to fetch
:param int [limit]: the maximum amount of trades to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
Nr   .tradestrades:rz   r+  T)r   rT  rU  r   r   r  r   r   r   rW  rX  r   r   getLimitfilter_by_since_limit)ru   r:  r   r   r   r[  r   r   r   r_  r  rz   r   tradesfirsttradeSymbols                   rw   rq  gate.watch_trades_for_symbols  s     !!!%%g.OOG,	WQZ(--f5	)q#g,'AZF  V!34 ( $$V,55c)^dee??OOFA.E**5(;KOOK7E))&TRR! 	" fs#   D(D#B-D(D&AD(&D(c           
        #    U R                  5       I Sh  vN   U R                  U5      nU R                  U5      nU R                  US   5      nU R	                  U5      nUS-   n/ n/ n[        S[        U5      5       H/  n	X   n
UR                  SU
-   5        UR                  SU
-   5        M1     U R                  U5      nU R                  USXXsXb5      I Sh  vN $  N N7f)a*  
get the list of most recent trades for a particular symbol
:param str[] symbols: unified symbol of the market to fetch trades for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
Nr   ru  rv  zunsubscribe:trades:ry  )
r   rT  rU  r   r   r  r   r   r   r   )ru   r:  r   r[  r   r   r   subMessageHashesr_  r  rz   r   s               rw   un_watch_trades_for_symbols gate.un_watch_trades_for_symbols  s      !!!%%g.OOG,	WQZ(--f5	)q#g,'AZF##I$67  !6!?@ ( $$V,66sHg^n  |C  L  L  	L 	" Ls"   C%C!CC%C#C%#C%c                 D   #    U R                  U/U5      I Sh  vN $  N7f)a'  
get the list of most recent trades for a particular symbol
:param str symbol: unified symbol of the market to fetch trades for
: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)r  )ru   rz   r   s      rw   un_watch_tradesgate.un_watch_trades  s"      55vhGGGGr   c                    U R                  US5      n[        U[        5      (       d  U/nU R                  U5      n[	        S[        U5      5       H  nXE   nUS   nU R                  U R                  U5      nUc6  U R                  U R                  SS5      n	[        U	5      nXR                  U'   UR                  U5        SU-   n
UR                  X5        M     g )Nr   r   rz   rb   rU   rv  )r   r  r  parse_tradesr  r   ry  r   rp   r   r   r  )ru   r   r   r   parsedTradesr  traderz   cachedTradesr   hashs              rw   handle_tradesgate.handle_trades  s    " (3&$''XF((0q#l+,A OE8_F??4;;?L#))$,,tL)%0&2F#&v%DNN<. -ry   1m	timeframec                   #    U R                  5       I Sh  vN   U R                  U5      nUS   nUS   nU R                  U R                  X"5      nU R	                  U5      n	U	S-   n
SU-   S-   US   -   nU R                  U5      nX/nU R                  XXU5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XUSS5      $  N N<7f)	a  
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
Nrz   r   z.candlestickscandles:rR  r   T)
r   r   r   
timeframesr   r   r   rX  rw  rx  )ru   rz   r  r   r   r   r   r   r[   r   r   r   r   r   ohlcvs                  rw   watch_ohlcvgate.watch_ohlcv  s      !!!V$!$<##DOOYJ--f5/ 8+c1F84DD$$V,&++CgPVWW??NN61E))%q$GG 	" Xs"   CCB	C C!;CCc                 r   U R                  US5      nUR                  S5      nU R                  US5      nUS:X  a  SOSnU R                  US5      n[        U[        5      (       d  U/n0 n[        S[        U5      5       GH&  n	Xy   n
U R                  U
SS5      nUR                  S	5      nU R                  US5      nU R                  U5      nUS	-   nUR                  US5      nU R                  US S	U5      nU R                  U
5      nU R                  U R                  U0 5      U R                  U'   U R                  U R                  U   U5      nUc:  U R                  U R                  S
S5      n[        U5      nUU R                  U   U'   UR                  U5        XU'   GM)     [	        UR!                  5       5      n[        S[        U5      5       HZ  n	UU	   nUU   nU R                  U5      nSU-   S-   U-   nU R                  U R                  U   U5      nUR#                  UU5        M\     g )Nr   r   r   rO   r   r   n r   rc   rU   r  rR  )r   r   r   r  r  r  r   find_timeframereplacer   parse_ohlcvohlcvsr   rp   r   r   keysr  )ru   r   r   r   r  r  r	  r   r[  r  r  r   rj  r  timeframeIdr^  r   rz   parsedstoredr   r  r[   r  s                           rw   handle_ohlcvgate.handle_ohlcv6  s   " ""7I6}}S)((q9 - 7Vj
(3&$''XF	q#f+&AIE++E3;L &&s+E((2I--i8K_F#++FB7H%%hc:FF%%e,F"&//$++vr"JDKK__T[[%8)DF~))$,,dK.u539F#K0MM&! )f# '$ INN$%q#d)$A!WF!&)I**95H"X-3f<D__T[[%8(CFNN64( %ry   c           	        #    U R                  5       I Sh  vN   SnSnSnSnUb  U R                  U5      nUS   nU R                  SX5      u  pdU R                  SX5      u  pTU R	                  USSSSSS.5      n	U	S	-   n
S
nUb  USU-   -  nUS:H  nU R                  Xl5      nU/nUS:g  nU R                  XXXO5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  UXUS5      $  N N<7f)a  
watches information on multiple trades made by the user
:param str symbol: unified market symbol of the market trades were made in
:param int [since]: the earliest time in ms to fetch trades for
:param int [limit]: the maximum number of trade structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
N!allr   rD   rO   re  rp   rO   r   rQ   rP   rR   z.usertradesmyTradesrR  inverseT)
r   r   r   handle_sub_type_and_paramsget_supported_mappingget_url_by_market_typesubscribe_privaterX  rw  r   )ru   rz   r   r   r   subTyper{   r   r   r   r   r   	isInverser   r   requiresUidry  s                    rw   watch_my_tradesgate.watch_my_tradesj  s6     !!![[(Fd|H99/6Z99/6Z008
  - 3<'K	)	))$:*v~--cRXff??OOF2E00tTT; 	"4 gs"   DC=B+DC?;D?Dc                 $   U R                  US/ 5      n[        U5      nUS:X  a  g U R                  nUc.  U R                  U R                  SS5      n[        U5      nXPl        U R                  U5      n0 n[        S[        U5      5       H!  n	Xy   n
UR                  U
5        U
S   nSX'   M#     [        UR                  5       5      n[        S[        U5      5       H  n	X   nSU-   nUR                  X^5        M     UR                  US5        g )	Nr   r   rb   rU   rz   Tz	myTrades:r  )r   r   r  r   rp   r   r  r  r   r  r  r  )ru   r   r   r   tradesLengthr  r   r  r[  r  r  rz   r  r   r  s                  rw   handle_my_tradesgate.handle_my_trades  s   * (B76{1}}%%dllM4HE1%8L(M""6*	q#f+&AIE&8_F $I	 '
 INN$%q#d)$AWF'DNN<. % 	|Z0ry   c           	      P  #    U R                  5       I Sh  vN   SnSnU R                  SSU5      u  p!U R                  SSU5      u  p1US:H  nU R                  X$5      nUS:g  nU R	                  USSSSSS.5      nUS-   nUS	-   n	U R                  XYSXU5      I Sh  vN $  N N7f)
z
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>`
NrF   r  rO   re  rp   r  z	.balances.balance)r   r   r  r  r  r  )
ru   r   r{   r  r  r   r  channelTyper   r   s
             rw   watch_balancegate.watch_balance  s      !!!99.$PVW99.$PVW	)	))$:v~008
  +Z'++CdGU`aaa# 	"" bs"   B&B"BB&B$B&$B&c                    U R                  US/ 5      nU R                  US5      nX0R                  S'   X@R                  S'   U R                  U5      U R                  S'   [	        S[        U5      5       Ht  nX5   nU R                  5       nU R                  USS5      nU R                  U5      n	U R                  US	5      US
'   U R                  USS5      US'   XpR                  U	'   Mv     U R                  US5      n
U
R                  S5      nU R                  US5      nU R                  USSSS.5      nUS-   nU R                  U R                  5      U l        UR                  U R                  U5        g )Nr   time_msinfor+  r,  r   currencyUSDT	availablefreetotalbalancer   r   rO   rP   rR   )rO   re  rp   r  )r   r   r  r1  r  r   accountr   safe_currency_codesafe_string_2r   r  safe_balancer  )ru   r   r   r   r+  r  
rawBalancer  
currencyIdcoder   rj  rawTyper  r   s                  rw   handle_balancegate.handle_balance  sf   z (B7%%gy9	%V$-[!#'<<	#:Z q#f+&AJllnG))*j&IJ**:6D"..z;GGFO#11*gyQGG!(LL ' ""7I6c"""5!,00;
 
 "J.((6t||[1ry   c                   #    U R                  5       I Sh  vN   SnU R                  U5      nS/nU R                  U5      (       d  U R                  U5      nSnSnU R	                  SXT5      u  pxUS:X  a  SnU R                  USSSS.5      n	US	-   n
U R                  U5      (       d  U
S
SR                  U5      -   -  n
U	S-   nSnU R                  SXX5      u  pUS:H  nU R                  X}5      nU R                  U5      nU R                  XU5        U R                  SSS5      nU R                  SSS5      nU R                  U R                  U5      nU(       a&  U(       a  Uc  UR                  US-   5      I Sh  vN $ U R                  XXkUS5      I Sh  vN nU R                   (       a  U$ U R#                  U R                  U   XUS5      $  GN NW N=7f)a  

https://www.gate.io/docs/developers/futures/ws/en/#positions-subscription
https://www.gate.io/docs/developers/delivery/ws/en/#positions-subscription
https://www.gate.io/docs/developers/options/ws/en/#positions-channel

watch all open positions
:param str[] [symbols]: list of unified market symbols to watch positions for
:param int [since]: the earliest time in ms to fetch positions for
:param int [limit]: the maximum number of positions to retrieve
: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  rL   rO   rP   re  rp   rQ   rP   rR   
:positions::,z
.positionsr  r`   Tra   :fetchPositionsSnapshot)r   rT  is_emptyget_market_from_symbolsr   r  joinr  r  r   set_positions_cacher   r   	positionsrQ   r  rX  filter_by_symbols_since_limit)ru   r:  r   r   r   r   r   r{   r   typeIdr   r   r  r  r   r   r`   ra   r   r  s                       rw   watch_positionsgate.watch_positions-  s     !!!%%g.-}}W%%11':F889I6Z6>D++D3
 
 \)}}W%%4#((7"333K<'889I6Y	)	))$:S!  w7!%!3!34DF^`d!e!%!3!34DF^`d!e5!&<t.G'GHHH007UZ\`aa	??11$..2FX]_cddE 	"< Ias4   GGE'G>G?GG9GGGc                 6   U R                   c  0 U l         X R                   ;   a  g U R                  SSS5      nU(       aD  US-   nXQR                  ;  a/  UR                  U5        U R	                  U R
                  XU5        g g [        5       U R                   U'   g )NrL   r`   Fr  )r  r   re  rQ   r   load_positions_snapshotr   )ru   r   r{   r:  r`   r   s         rw   r  gate.set_positions_cache_  s    >>!DN>>!!%!3!34DF^`e!f!!::K>>1k*

477dS 2 $<#=DNN4 ry   c                   #    U R                  S SU05      I S h  vN n[        5       U R                  U'   U R                  U   n[        S[	        U5      5       H3  nXF   nU R                  USS5      nUS:  d  M"  UR                  U5        M5     UR                  U   n	U	R                  U5        UR                  XSS-   5        g  N7f)Nr{   r   	contractsz	:position)	fetch_positionsr   r  r  r   safe_numberr   re  r  )
ru   r   r   r{   r  r   r  positionr  rQ   s
             rw   r  gate.load_positions_snapshotm  s     ..tfd^DD	79tt$q#i.)A |H((;BI1}X&	 * ,uu[01 Es   CCAC<ACc                 >   U R                  UR                  5      nU R                  US/ 5      nU R                  U   n/ n[	        S[        U5      5       GH  nXG   nU R                  U5      n	U R                  U	S5      n
U R                  U	S5      nUc  U R                  XZS-   5      nUb*  US   U	S'   UR                  U	5        UR                  U	5        U R                  XZS-   5      nUb*  US   U	S'   UR                  U	5        UR                  U	5        Uc.  Uc)  SU	S'   UR                  U	5        UR                  U	5        M  M  M  UR                  U	5        UR                  U	5        GM     U R                  XS-   5      n[	        S[        U5      5       Hl  nX   nUR                  S5      nUS	   nUR                  S
5      nU R                  USUS5      nU R                  U5      (       a  MZ  UR                  UU5        Mn     UR                  XcS-   5        g )Nr   r   rz   r|   longshortz:positions::r  r   r  Fr  )get_market_type_by_urlr   r   r  r  r   parse_positionr   rg  r   find_message_hashesr   rZ  r  r  )ru   r   r   r{   datar   newPositionsr  rawPositionr  rz   r|   prevLongPositionprevShortPositionr_  r   rj  symbolsStringr:  r  s                       rw   handle_positionsgate.handle_positions{  s   > **6::6w"5t$q#d)$A'K**;7H%%h9F##Hf5D|#'>>%&#I #/'7'?HV$ ''1LL*$(NN57:J$K!$0'8'@HV$ ''1LL*#+0A0I'-HV$ ''1LL* 1J+
 ##H-X&1 %2 009NOq#m,-A'*K%%d+E!!HM#))#.G,,\8WeTI==++y+6 . 	|L%89ry   c           	        #    U R                  5       I Sh  vN   SnUb  U R                  U5      nUS   nSnSnU R                  SXT5      u  pgU R                  USSSSSS.5      nUS-   n	S	n
S
/nUb  U
SUS   -   -  n
US   /nSnU R	                  SXW5      u  pUS:H  nU R                  Xm5      nUS:g  nU R                  XXX5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  UX#SS5      $  N N<7f)ao  
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 <https://docs.ccxt.com/#/?id=order-structure>`
Nrz   rG   rO   re  rp   r  z.ordersr   r  rR  r   r  r+  T)
r   r   r   r  r  r  r  rX  rw  rx  )ru   rz   r   r   r   r   r{   r   r  r   r   r   r  r  r   r  r   s                    rw   watch_ordersgate.watch_orders  sG     !!![[(FH%F88W++D3
  9$-3--Kd|nG88V	)	))$:v~--cRWee??OOF2E))&%TRR= 	"6 fs"   D
DB4D
D;D
D
c                 B   U R                  US/ 5      nU R                  U R                  SS5      nU R                  c  [	        U5      U l        U R                  n0 nU R                  U5      n[        S[        U5      5       H  nXx   n	U R                  U	S5      n
U R                  U
S5      nUS:X  d  US:X  a  S	U	S
'   O:US:X  a4  U R                  U	S
5      nUc  U R                  U
S5      nUS:X  a  SOSU	S
'   UR                  U	5        U	S   nU R                  U5      nSXoS   '   M     [        UR                  5       5      n[        S[        U5      5       H'  nSUU   -   nUR                  U R                  U5        M)     UR                  U R                  S5        g )Nr   ordersLimitrU   r   r  eventputupdater   r   finishleftclosedcanceledrz   Tr   zorders:r   )r   r   rp   r   r   r   r  r   r   r   r   r  r  r  )ru   r   r   r   r   r  r[  parsedOrdersr  r  r  r  r   r  rz   r   r  r   s                     rw   handle_ordergate.handle_order  s   H (B7!!$,,tD;;07DK	((0q#l+,A!_F??662D$$T73E~(!2#)x ("))&(;>,,T6:D48AIxJF8$MM&!H%F[[(F&*ITl# -  INN$%q#d)$A#d1g-KNN4;;4 % 	t{{H-ry   c                 2   #    U R                  U/X#U5      $ 7f)a  
watch the public liquidations of a trading pair

https://www.gate.io/docs/developers/futures/ws/en/#liquidates-api
https://www.gate.io/docs/developers/delivery/ws/en/#liquidates-api
https://www.gate.io/docs/developers/options/ws/en/#liquidates-channel

:param str symbol: unified CCXT market symbol
:param int [since]: the earliest time in ms to fetch liquidations for
:param int [limit]: the maximum number of liquidation structures to retrieve
:param dict [params]: exchange specific parameters for the bitmex api endpoint
:returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
)!watch_my_liquidations_for_symbolsr   s        rw   watch_my_liquidationsgate.watch_my_liquidations.  s      55vhfUUs   c                   #    U R                  5       I Sh  vN   U R                  USSS5      nU R                  U5      nSnSnU R                  SXT5      u  pgU R	                  USSSS.5      nSn	U R                  SXW5      u  pU	S:H  n
U R                  Xj5      n/ nSnU R                  U5      (       a9  US:w  a  U
(       d  [        U R                  S	-   5      eS
nUR                  S5        OE[        U5      nUS:w  a  [        U R                  S-   5      eSUS   -   nUR                  US   5        US-   nU R                  XXUS5      I Sh  vN nU R                  (       a  U$ U R                  U R                  XUS5      $  GNn N87f)a  
watch the private liquidations of a trading pair

https://www.gate.io/docs/developers/futures/ws/en/#liquidates-api
https://www.gate.io/docs/developers/delivery/ws/en/#liquidates-api
https://www.gate.io/docs/developers/options/ws/en/#liquidates-channel

:param str[] symbols: unified CCXT market symbols
:param int [since]: the earliest time in ms to fetch liquidations for
:param int [limit]: the maximum number of liquidation structures to retrieve
:param dict [params]: exchange specific parameters for the gate api endpoint
:returns dict: an array of `liquidation structures <https://github.com/ccxt/ccxt/wiki/Manual#liquidation-structure>`
NTrK   re  rp   r  r  r  z watchMyLiquidationsForSymbols() does not support listening to all symbols, you must call watchMyLiquidations() instead for each symbol you wish to watch.myLiquidationsznot allr   z watchMyLiquidationsForSymbols() only allows one symbol at a time. To listen to several symbols call watchMyLiquidationsForSymbols() several times.myLiquidations::r   r   z.liquidates)r   rT  r  r   r  r  r  r  r   r   r   r   r  rX  r  liquidations)ru   r:  r   r   r   r   r{   r   r  r  r  r   r   r   symbolsLengthr   newLiquidationss                    rw   r  &gate.watch_my_liquidations_for_symbols>  s     !!!%%gtT4@--g6889XZ`i++D3
 
 889XZ`h	)	))$:==!!"9   ,H  "H  I  I*KNN9%LM!   ,A  "A  B  B,wqz9KNN6$<(=( $ 6 6s[`bf gg??""11$2C2CWUZ\`aaA 	": hs"   F	FD7F	F6F	F	c                    U R                  US/ 5      n/ n[        S[        U5      5       H  nX5   nU R                  U5      nU R	                  US5      nU R                  U R                  U5      n	U	c(  U R                  U R                  SS5      n
[        U
5      n	U	R                  U5        XR                  U'   UR                  U	SU-   5        M     UR                  US5        g )Nr   r   rz   liquidationsLimitrU   r  r  )rf  r  r   parse_ws_liquidationr   r   r  r   rp   r   r   r  )ru   r   r   rawLiquidationsr	  r  rawLiquidationliquidationrz   r  r   s              rw   handle_liquidationgate.handle_liquidationn  s    X ..(B?q#o./A,/N33NCK%%k8<F??4+<+<fEL#))$,,8KTR)%0,(4f%NN<);f)DE 0 	(89ry   c                    U R                  US5      nU R                  X25      nU R                  US5      nU R                  US5      nU R                  US5      n[        R                  " [        R
                  " XV5      5      nU R                  UU R                  X25      U R                  U5      U R                  US5      U R                  US5      S S UU R                  U5      S.	5      $ )Nr   r  sizer  contractSize
fill_price)	r  rz   r  r  r~   	baseValue
quoteValuer+  r,  )r   rh  r   r"   
string_abs
string_subsafe_liquidationr   parse_numberr  r1  )ru   r  r   r   r+  originalSizer  r}   s           rw   r  gate.parse_ws_liquidation  s    : ##K<!!(3%%k9=	''V<V4##G$6$6|$JK$$&&x8**62 ,,V^D%%k<@"Y/
&
 
 
	ry   c                    U R                  US5      nU R                  US5      nU R                  USU5      nU R                  USS5      nU R                  U/ SQ5      nUb  U R                  UR                  U5      n U R                  U R                  S   S   X`R                  U5      5        U R                  U R                  S   X`R                  U5      5        U R                  US	U R                  US	5      5      n	U R                  U R                  S   S
   XR                  U5      5        [        U R                  U5      5      eg! [         a  n
UR                  X5        Ub  XR                  ;   a  UR                  U	 U R                  US5      nUb  UR                  S5      S:  a  UR                  S5      nU R                  US/ 5      n[        S[!        U5      5       HP  nUS   S:H  nU R#                  X   S SU5      nUS   S-   U-   nUc  M1  UUR                  ;   d  MC  UR                  U	 MR      S n
A
OS n
A
ff = fUb  XqR                  ;   a  UR                  U	 g)Nr  errsr  r  label)r   	requestId
request_idr(   rl   r   rm   r   r   r   r   rP   r   r   rR  TF)rg  r  safe_string_nr   r   throw_exactly_matched_exceptionrq   jsonthrow_broadly_matched_exceptionr   	Exceptionr  rV  r   rf  r  r   r   )ru   r   r   r  r   r  r  r   r   errorMessageer   parsedChannelr   r  r	  rz   messageHashSymbols                     rw   handle_error_messagegate.handle_error_message  sY   h ~~gv.~~dF+w6!!%9)JK**6+?+?DKH44T__T5J75SUY[d[del[mn44T__W5MtU^U^_cUde#//y$BRBRSWYbBcd44T__T5J75SUaclclmtcuv#DIIg$677& %  Ha-++AUAU2U,,[9**7I>'gll3.?!.C$+MM#$6M"nnWiDG"1c'l3%21%5%?
!%!1!1'*dC!T,9!,<s,BV,K)-9@QU[UiUi@i & 4 45F G 4H R+?+?%?((,s&   ?CE 
IC II-IIc                     0 U l         g N)r  )ru   r   r   r   s       rw   handle_balance_subscription gate.handle_balance_subscription-  s	    ry   c                 b   U R                  US5      nU R                  U R                  U R                  S.nU R                  US5      nX4;   aE  U R                  UR                  U5      nU R	                  UR                  U5      nXC   nU" XU5        XQR                  ;   a  UR                  U	 g g )Nr   )r  zspot.order_book_updatezfutures.order_book_updater   )r   r1  r   r   r   )	ru   r   r   r   methodsr   subscriptionHashr   r<  s	            rw   handle_subscription_statusgate.handle_subscription_status0  s    ""7I677&*&I&I)-)L)L

 gt,#//0D0DbI??6+?+?AQRL%F6L1%%%$$R( &ry   c                 *   U R                  US5      n[        UR                  R                  5       5      n[	        S[        U5      5       H  nXE   nXaR                  ;  a  M  UR                  S5      (       d  M0  UR                  U   nU R                  US5      nX8:w  a  MX  U R                  US/ 5      n	U R                  US/ 5      n
[	        S[        U	5      5       H  nX   nX   nU R                  XU5        M     U R                  U5        M     g )Nr   r   unsubscriber_  r~  )
r   r  r   r  r  r   
startswithrf  clean_unsubscriptionclean_cache)ru   r   r   r   r  r  r   r   subIdr_  r~  j	unsubHashsubHashs                 rw   handle_un_subscribegate.handle_un_subscribe@  s   & gt,F((--/0q#d)$A'K#7#77%%m44%33K@((t<; $|_b Q#'>>,@RTV#W q#m"45A - 0I.1G--fyI 6   .! %ry   r   c                    U R                  USS5      nU R                  US/ 5      n[        U5      nUS:X  ah  U R                  US/ 5      n[        S[        U5      5       H;  nXV   nU R                  US5      nU R                  US5      n	U R                  U   U		 M=     g US:  ap  [        S[        U5      5       HV  nX6   nUR                  S5      (       a  U R                  U	 M,  US	:X  a  U R                  U	 MA  US
:X  d  MI  U R                  U	 MX     g UR                  S5      (       aO  [        U R                  R                  5       5      n
[        S[        U
5      5       H  nU R                  X   	 M     g g )Ntopicr  r:  r  symbolsAndTimeframesr   r   ry  r   rC  )r   rf  r   r  r  endswithry  r   rW   r  r  )ru   r   rD  r:  r  symbolsAndTimeFramesr  symbolAndTimeFramerz   r  r  s              rw   r<  gate.clean_cacheg  s`     w;..y"=GG#'>>,@VXZ#[ 1c"678%9%<"))*<a@ ,,-?C	KK'	2	 9
 Q1c'l+ >>(++F+k)/h&V, , ~~h'' DKK,,./q#d),ADG, -	 (ry   c                 2   U R                  X5      (       a  g U R                  US5      nUS:X  a  U R                  X5        g US:X  a  U R                  X5        g U R                  USS5      nUR	                  S5      nU R                  US5      nU R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S.
nU R                  Xv5      nUb  U" X5        U R                  US	5      n	U	S
:X  a  U R!                  X5        g U	bN  U R#                  US5      n
U R                  U
S5      nU R%                  US5      nUSLa  UR'                  X5        g g g )Nr  	subscriber9  r   r  r   r   )

usertradescandlesticksr   r  rW   rJ  ry  order_book_updatebalances
liquidatesr#  authenticatedr  r   ackT)r-  r   r6  rA  r   r   r  r  r  r  rG  rO  r  r  r  r  handle_authentication_messagerg  	safe_boolr  )ru   r   r   r  r   r  r  	v4Methodsr<  r"  r  r   rR  s                rw   handle_messagegate.handle_message  s   t $$V55  '2K++F<M!$$V5""7Ir:}}S)oolA6// --''..))..((!%!7!7++11
	 86#$$Wl;	'..v? >>'62D__T84F..%0C$v1  !ry   c                 l    U R                   S   US      nUS   (       a  US   (       a  US   $ US   $ U$ )NrS   r{   r   linearrM   rN   ro   )ru   r   baseUrls      rw   r   gate.get_url_by_market  s@    ))E"6&>2*&,X&676?JGENJNry   r   c                 0    US   (       a  gUS   (       a  gg)NrO   rR   rp   re   )ru   r   s     rw   r   gate.get_type_by_market  s    &>Hry   c                 f    U R                   S   nX1   nUS:X  d  US:X  a  U(       a  US   $ US   $ U$ )NrS   rP   rQ   rN   rM   rZ  )ru   r{   r  rS   r   s        rw   r  gate.get_url_by_market_type  s?    iiiFN 0!*3u:;F;Jry   r   c                     SSSS.n[        UR                  5       5      n[        S[        U5      5       H$  nX4   nX%   nUR	                  U5      S:  d  M"  Us  $    g)NrR   rQ   rP   )zop-deliveryfxr   rO   )r  r  r  r   rV  )ru   r   findByr  r  keyvalues          rw   r  gate.get_market_type_by_url  sa     

 FKKM"q#d)$A'CKExx}!	 %
 ry   c                 |    U R                  U R                  U R                  SS5      S5      nXR                  S'   U$ )Nreqidr   r   )sumr   rp   )ru   rj  s     rw   r#  gate.request_id)  s6    **4<<!DaH %Wry   c                 .  #    U R                  5       nU R                  5       nUUUSUS.n	Ub9  U R                  U5      n
X*R                  ;  a  [	        U5      nX*R                  U'   U R                  X5      nU R                  XXU5      I S h  vN $  N7fNrK  r   timer   r  r   )r#  secondsr   r   r   r   watch)ru   r   r   r   r   r   r   r"  rp  r   r   tempSubscriptionHashr   s                rw   r   gate.subscribe_public/  s     OO%	||~ 
 #[[%F#7#77'*9~$=H$$%9:++g.ZZ'UUUUs   BBBBc                    #    U R                  5       nU R                  5       nUUUSUS.nU R                  X5      n	U R                  XX5      I S h  vN $  N7frn  )r#  rq  r   watch_multiple)
ru   r   r_  r   r   r   r"  rp  r   r   s
             rw   rW  gate.subscribe_public_multipleA  s[     OO%	||~ 
 ++g.((WTTTTs   AAAAc	                    #    U R                  5       n	U R                  5       n
U	U
USUS.n[        U	5      USUUUS.nU R                  X5      nU R	                  XXU5      I S h  vN $  N7f)Nr9  ro  T)r   rD  r9  r_  r~  r:  )r#  rq  r   r   rv  )ru   r   rD  r:  r_  r~  r   r   r   r"  rp  r   subr   s                 rw   r   !gate.un_subscribe_public_multipleN  s}     OO%	||~"
 i.* 0
 ++g.((WUXYYYYs   A"A+$A)%A+c                    #    US-   nU R                  U5      nSnUR                  U5      nU R                  UR                  U5      nUc  U R	                  U0 X55      I S h  vN $ U$  N7f)Nz.loginrQ  )r   reusableFuturer   r   r   )ru   r   r   r   r   r   rQ   rQ  s           rw   r   gate.authenticatec  sq     (S!%&&{3(<(<kJ --c2wLLL Ms   AA*!A("A*c                 b    SnU R                  UR                  U5      nUR                  S5        g )NrQ  T)r   re  r  )ru   r   r   r   rQ   s        rw   rS  "gate.handle_authentication_messagem  s'    %=try   r"  c                    #    U R                  5         SnUc  U R                  5       n[        U5      nUnU R                  5       nSR	                  XSU R                  U5      [        U5      /5      n	U R                  U R                  U	5      U R                  U R                  5      [        R                  S5      n
U[        U5      U R                  U
US.nUS:X  d  US:X  a  SS0US	'   UUUUUS
.nU R                  XXU5      I S h  vN $  N7f)NrS   
hex)req_idr+  api_key	signature	req_paramzspot.order_placezfutures.order_placezX-Gate-Channel-Idccxt
req_headerro  )check_required_credentialsr#  r   rq  r  r&  hmacencodesecrethashlibsha512apiKeyrr  )ru   r   	reqParamsr   r"  r  reqIdr   rp  signatureStringr  r   r   s                rw   r   gate.request_privater  s
    '')OO%EE
I||~))UTYYy5I3t9$UVIIdkk/:DKK<TV]VdVdfkl	T{{""
 ))w:O/O#V%GL! 
 ZZ'	RRRRs   DDDDc                    #    U R                  5         U(       ab  U R                  b  [        U R                  5      S:X  a  [        U R                  S-   5      eU R                  /nUc  UnOU R                  Xs5      nU R                  5       nSn	SU-   S-   S-   U	-   S-   S-   [        U5      -   n
U R                  U R                  U
5      U R                  U R                  5      [        R                  S5      nS	U R                  US
.nU R                  5       nUUUU	US.nUb  X>S'   U R                  U5      nX/R                   ;  a  [        U5      nX/R                   U'   U R#                  X5      nU R%                  XUX"5      I S h  vN $  N7f)Nr   z requires uid to subscriberK  zchannel=&zevent=ztime=r  r  )r<  KEYSIGN)r   rp  r   r  authr   )r  uidr   r   r   array_concatrq  r   r  r  r  r  r  r  r#  r   r   r   rr  )ru   r   r   r   r   r   r  idArrayrp  r  signaturePayloadr  r  r"  r   r   rs  r   s                     rw   r  gate.subscribe_private  s}    '')xx3txx=A#5'2N(NOOxxjG!++G=||~%/#5@5H3NQXX[^_c[ddIIdkk*:;T[[=UW^WeWeglm	;;

 OO%	
 !(IS!333#&y> 9D  !56++g.ZZ';TTTTs   FFFF)r  r  r   r  r0  )F)^__name__
__module____qualname____firstlineno__r   rt   r   r   r   floatr   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   r   r7  rG  rK  rO  r>  rE  r   rr  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-  r1  r6  rA  dictr<  rV  r   r   r   r   r  r  r#  r   rW  r   r   rS  r   r  __static_attributes____classcell__)rv   s   @rw   r$   r$      sr   m# m^ pt|~ )C )y )	 )[` )il )V IK 4T,-? 4. 8<B 4 44 <@ - -S -6 im{  IK 2c 23 2i 2y 2be 2ux 24 ;?r 2s 2C 24 8<$]ajl Z ZC ZWZ Zquv{q| Z :>D_cln ^3 ^c ^Y\ ^swx}s~ ^ JN\`os|~ Oc O3 OVY Oil O  DH  IN  DO O@ @DB #!S #! #!S\ #!J =? N N3 N2=V =
[5 [5z.
/ 68 / /F /" 6:" 
E7 
E 
EBF B* 8<B JW J7 JBV B( NRkoxz E7 Eeh E  @G E24C 4 44 <@d[] 	S 	SS 	S 	Sbfglbm 	S OSaenp Sd3i S S[^ Suyz  vA S6 LN Lc LRU L, 9; HC Hs H/F /B ?CQUdhqs H H H3 H^a Hx|  ~B  yC H22)6 2)h 37TX\eg &UC &Us &URU &Ulpqvlw &UP*1v *1X *, b b2T2V T2l 8<$]ajl 0eW 0eC 0eWZ 0equv~q 0ed>& > >2E:N 04$UYbd )S )SC )Ss )Simnsit )SV?.6 ?.B EIW[df V# Vc VQT Vkop{k| V  X\jnwy .btCy .bQT .bdg .b  C  DO  P .b`9: 9:v-^S6 St Sj& ) ) %/& %/N- -82V 2B : #  QSae V$ \^ U AC Z*F 
S S@$U $Ury   r$   )+ccxt.async_supportr   ccxt.async_support.base.ws.cacher   r   r   r   r  ccxt.base.typesr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   r    r!   ccxt.base.preciser"   async_supportr$   r^  ry   rw   <module>r     st     A  A  A  A  A  A  A  A 4  * 0 . ' ) * %aU4"" aUry   