
    ":h                         d dl Zd dlmZmZmZ d dlZd dlmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZ d dlmZ  G d dej*                  j,                        Zy)	    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyIntOrder	OrderBookStrTickerTrade)Client)List)ExchangeErrorc            
           e Zd Zdef fdZd Zdddi fdedededee	   fd	Z
dddi fdedededee	   fd
Zd Zi fdedefdZi fdedefdZd Zd dZddi fdedededee   fdZddi fdedededee   fdZd Zd dZdddi fdedededee   fdZd Zd dZd Zdi fdededefdZdi fdededefdZd Z d Z!de"fdZ#d Z$i fdZ% xZ&S )!lbankreturnc                     | j                  t        t        |          ddddddddddddddddddiiddd	d
ddddddddd
iii i d      S )NTF)wsfetchOHLCVWsfetchOrderBookWsfetchTickerWsfetchTradesWswatchBalancewatchTickerwatchTickerswatchTradeswatchTradesForSymbolswatchMyTradeswatchOrderswatchOrderBook
watchOHLCVapir   zwss://www.lbkex.net/ws/V2/r!   
timeframes1min5min15min30min1hr4hrdayweekmonthyear)
1m5m15m30m1h4h1d1w1M1y)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describe)self	__class__s    H/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/lbank.pyr?   zlbank.describe   s    eT ; = $$(!%!% %# %#).!&#"&"" 6  $$&&###$%$# Q*@
 * *	    c                     | j                  | j                  dd      }| j                  |d      }|| j                  d<   |S )N	requestIdr      )safe_integerr:   sum)r@   previousValuenewValues      rB   
request_idzlbank.request_id>   s=    ))$,,QG88M1-$,[!rC   r.   Nsymbolsincelimitc                 4  K   | j                          d{    | j                  |      }| j                  d   d   }| j                  | j                  di       }| j                  |di       }	| j                  |	||      }
d|d   z   dz   |
z   }d	d
|
|d   d}|3| j                  t        t        j                  |dz                    |d<   |||d<   | j                  ||      }| j                         }| j                  |||||       d{   S 7 7 w)a  

        https://www.lbank.com/en-US/docs/index.html#request-amp-subscription-instruction

        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: the length of time each candle represents
        :param int [since]: timestamp in ms of the earliest candle to fetch
        :param int [limit]: the maximum amount of candles to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr"   r   r!   r#   fetchOHLCV:rL   :requestkbarid)actionrR   rS   pair  startsize)load_marketsmarketr9   
safe_valuer:   safe_stringparse_to_intintmathfloorr=   rK   watch)r@   rL   	timeframerM   rN   paramsr[   urlwatchOHLCVOptionsr#   timeframeIdmessageHashmessagerR   rE   s                  rB   fetch_ohlcv_wszlbank.fetch_ohlcv_wsD   s+     !!!V$iit$ OODLL,K__%6bI
&&z9iH#fX&66<{J4L	
 #00TZZ5M1NOGG#GFO""7F3OO%	ZZ['9gNNN' 	"& Os"   DDC8DDDDc                    K   | j                          d{    | j                  |      }| j                  | j                  di       }| j                  |di       }| j	                  |||      }	d|d   z   dz   |	z   }
| j
                  d   d   }d	d
|	|d   d}| j                  ||      }| j                  ||
||
       d{   }| j                  r|j                  ||      }| j                  |||dd      S 7 7 9w)a  

        https://www.lbank.com/en-US/docs/index.html#subscription-of-k-line-data

        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: the length of time each candle represents
        :param int [since]: timestamp in ms of the earliest candle to fetch
        :param int [limit]: the maximum amount of candles to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        Nr!   r#   ohlcv:rL   rQ   r"   r   	subscriberS   rT   )rU   rm   rS   rV   r   T)rZ   r[   r\   r:   r]   r9   r=   rb   
newUpdatesgetLimitfilter_by_since_limit)r@   rL   rc   rM   rN   rd   r[   rf   r#   rg   rh   re   rm   rR   ohlcvs                  rB   watch_ohlcvzlbank.watch_ohlcvf   s     !!!V$ OODLL,K__%6bI
&&z9iH!11C7+Eiit$!4L	
	 ""9f5jjk7KHH??NN651E))%q$GG# 	" Is"   C>C:B+C>C<8C><C>c           	      D   | j                  |d      }| j                  |d d      }| j                  | j                  di       }| j                  |di       }| j                  |d      }|S| j                  |dg       }| j	                  |d      | j                  |d      | j                  |d      | j                  |d	      | j                  |d
      | j                  |d      g}	| j                  |d      }
| j                  |
|      }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |      }|:| j	                  | j                  dd      }t        |      }|| j                  |   |<   |j                  |	       d|z   dz   |
z   }|j                  ||       y | j                  |di       }| j                  |d      }
| j                  |d      }| j                  |      | j                  |d      | j                  |d      | j                  |d      | j                  |d      | j                  |d      g}	| j                  |
|      }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |      }|:| j	                  | j                  dd      }t        |      }|| j                  |   |<   |j                  |	       d|z   dz   |
z   }|j                  ||       y )NrV   _r!   r#   recordsr   rF               rS   
OHLCVLimitrW   rP   rQ   slottohlcvrl   )r]   safe_symbolr\   r:   rG   safe_numberfind_timeframeohlcvsr   appendresolve	parse8601)r@   clientri   marketIdrL   rf   r#   ru   rawOHLCVparsedrg   rc   storedrN   rh   datetimes                   rB   handle_ohlcvzlbank.handle_ohlcv   s   h ##GV4!!(D#6 OODLL,K__%6bI
//'95w26H!!(A.  1-  1-  1-  1-  1-F **7F;K++KDI"&//$++vr"JDKK__T[[%8)DF~))$,,dK.u517F#I.MM&!'&036DKNN6;/w;H**8V<K''#6Hx(  3/  3/  3/  3/  3/F ++KDI"&//$++vr"JDKK__T[[%8)DF~))$,,dK.u517F#I.MM&!"V+c1K?KNN6;/rC   c                 (  K   | j                          d{    | j                  |      }| j                  d   d   }d|d   z   }dd|d   d	}| j                  ||      }| j	                         }| j                  |||||       d{   S 7 w7 w)
a  

        https://www.lbank.com/en-US/docs/index.html#request-amp-subscription-instruction

        fetches 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 cex api endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr"   r   fetchTicker:rL   rR   tickrT   )rU   rR   rV   rZ   r[   r9   r=   rK   rb   )	r@   rL   rd   r[   re   rh   ri   rR   rE   s	            rB   fetch_ticker_wszlbank.fetch_ticker_ws   s      !!!V$iit$$vh'774L

 ""7F3OO%	ZZ['9gNNN 	" Os"   BBA2B	B
BBc                   K   | j                          d{    | j                  |      }| j                  d   d   }d|d   z   }dd|d   d	}| j                  ||      }| j	                  |||||       d{   S 7 g7 w)
a  

        https://www.lbank.com/en-US/docs/index.html#market

        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 lbank api endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/en/latest/manual.html#ticker-structure>`
        Nr"   r   ticker:rL   rm   r   rT   rU   rm   rV   )rZ   r[   r9   r=   rb   )r@   rL   rd   r[   re   rh   ri   rR   s           rB   watch_tickerzlbank.watch_ticker  s      !!!V$iit$&"22!4L

 ""7F3ZZ[';PPP 	" Qs"   BA>A"B9B :B Bc                 
   | j                  |d      }| j                  |      }| j                  |      }| j                  ||      }|| j                  |<   d|z   }|j                  ||       d|z   }|j                  ||       y )NrV   r   r   )r]   r   safe_marketparse_ws_tickertickersr   )r@   r   ri   r   rL   r[   parsedTickerrh   s           rB   handle_tickerzlbank.handle_ticker  s    , ##GV4!!(+!!(+++GV<+V&(|[1$v-|[1rC   c                    | j                  |d      }| j                  ||      }| j                  |d      }| j                  |d      }| j                  i d|d| j	                  |      d|d| j                  |d      d| j                  |d      d	d d
d dd dd dd dd dd d| j                  |d      dd dd d| j                  |d      dd | j                  |d      | j                  |d      |d|      S )NrV   TSr   rL   	timestampr   highlowbid	bidVolumeask	askVolumevwapopencloselastlatestpreviousClosechange
percentageaveragevolturnover)
baseVolumequoteVolumeinfo)r]   r   r\   safe_tickerr   )r@   tickerr[   r   rL   r   
tickerDatas          rB   r   zlbank.parse_ws_ticker8  s   , ##FF3!!(F3##FD1__VV4
 !
f!
1!
 !
 D$$Z8	!

 4##J6!
 4!
 !
 4!
 !
 D!
 D!
 T!
 D$$Z:!
 T!
 d!
  $**:x@!!
" t#!
$ **:u=++J
C)!
* + 	rC   c                 2  K   | j                          d{    | j                  |      }| j                  d   d   }d|d   z   }|d}dd|d	   |d
}| j                  ||      }	| j	                         }
| j                  |||	|
|	       d{   S 7 |7 w)a8  
        get the list of most recent trades for a particular symbol

        https://www.lbank.com/en-US/docs/index.html#request-amp-subscription-instruction

        :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 Trade[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        Nr"   r   fetchTrades:rL   
   rR   traderT   )rU   rR   rV   rY   r   )r@   rL   rM   rN   rd   r[   re   rh   ri   rR   rE   s              rB   fetch_trades_wszlbank.fetch_trades_wsi  s      !!!V$iit$$vh'77=E4L	
 ""7F3OO%	ZZ['9gNNN 	" Os"   BBA7BBBBc                 2  K   | j                          d{    | j                  |      }| j                  d   d   }d|d   z   }dd|d   d	}| j                  ||      }	| j	                  |||	||	       d{   }
| j                  |
||d
d      S 7 |7 w)a  

        https://www.lbank.com/en-US/docs/index.html#trade-record

        get the list of most recent trades for a particular symbol
        :param str symbol: unified symbol of the market to fetch trades for
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        Nr"   r   trades:rL   rm   r   rT   r   r   T)rZ   r[   r9   r=   rb   rp   )r@   rL   rM   rN   rd   r[   re   rh   ri   rR   tradess              rB   watch_tradeszlbank.watch_trades  s      !!!V$iit$&"22! 4L

 ""7F3zz#{G['RR))&%TRR 	" Ss"   BBA"B9B:BBc                    | j                  |d      }| j                  |      }| j                  |      }| j                  | j                  |      }|7| j                  | j                  dd      }t        |      }|| j                  |<   | j                  |d      }| j                  |d|g      }	t        dt        |	            D ]-  }
| j                  |	|
   |      }||d<   |j                  |       / || j                  |<   d|z   }|j                  | j                  |   |       d	|z   }|j                  | j                  |   |       y )
NrV   tradesLimitrW   r   r   r   rL   r   r   )r]   r   r   r\   r   rG   r:   r   rangelenparse_ws_trader   r   )r@   r   ri   r   rL   r[   r   rN   rawTrade	rawTradesir   rh   s                rB   handle_tradeszlbank.handle_trades  s:   6 ##GV4!!(+!!(+f5>%%dllM4HE&F"(DKK??7G4OOGXzB	q#i.) 	!A''	!f=E$E(OMM% 	! %F&(t{{6*K8$v-t{{6*K8rC   c                    | j                  |d      }|| j                  |      n| j                  |d      }|| j                  |      }| j	                  |dd      }|j                  d      }| j                  |d      }| j                  |d      }|}	||dk(  r	|	dk(  rd	nd}	| j                  ||d d d d d |	| j	                  |d
d      | j	                  |dd      | j                  |d      d |d|      S )Nr   r   	directionrw   rt   rF   makerbuysellpricevolumerv   amount)r   r   rL   rT   ordertypetakerOrMakersider   r   costfeer   )rG   iso8601r]   r   safe_string_2split
safe_trade)
r@   r   r[   r   r   rawSideparts	firstPart
secondPartr   s
             rB   r   zlbank.parse_ws_trade  s     %%eQ/	1:1FDLL+dN^N^_dfjNkx0I$$UK;c"$$UA.	%%eQ/
!jG&;"em6%D"  ''w:((!<$$UH5 
  	rC   c                   K   | j                          d{    | j                  |       d{   }| j                  d   d   }d}d}|d}n/| j                  |      }	| j	                  |      }d|	d   z   }|	d   }d	d
||d}
| j                  |
|      }| j                  |||||       d{   }| j                  ||||d      S 7 7 7 w)a  

        https://www.lbank.com/en-US/docs/index.html#update-subscribed-orders

        get the list of trades associated with the user
        :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 lbank api endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        Nr"   r   allz
orders:allorders:rL   rT   rm   orderUpdate)rU   rm   subscribeKeyrV   T)rZ   authenticater9   r[   rL   r=   rb   filter_by_symbol_since_limit)r@   rL   rM   rN   rd   keyre   rh   rV   r[   ri   rR   orderss                rB   watch_orderszlbank.watch_orders  s      !!!%%f--iit$>&K[[(F[[(F#fX&66K$<D!&	
 ""7F3zz#{G['RR00tTT) 	"-$ Ss2   CC	CCA?C/C0CCCc                 x   | j                  |d      }| j                  |d d      }d }| j                  )| j                  | j                  dd      }t        |      }n| j                  }| j                  |      }|j                  |       || _        |j                  |d       d|z   }|j                  ||       y )NrV   rt   ordersLimitrW   r   r   )	r]   r   r   rG   r:   r   parse_ws_orderr   r   )	r@   r   ri   r   rL   myOrdersrN   r   rh   s	            rB   handle_orderszlbank.handle_orders  s    & ##GV4!!(D#6;;%%dllM4HE-e4H{{H##G,x*&(x-rC   c           	         | j                  |di       }| j                  |dd      }|j                  d      }| j                  |d      }| j                  |d      }d }|dk7  r|dk7  r	|d	k(  rd	nd
}| j                  |d      }	| j                  |	|d      }
| j	                  |d      }| j                  |d      }| j                  |d      }d }|d	k(  r|dk(  r|}| j                  i d|d| j                  |d      d| j                  |d      d|d| j                  |      dd d| j	                  |d      d|
d|d|d| j                  |dd      dd d| j                  |d      d| j                  |dd      d | j                  |d!      d"| j                  |d#      d$| j                  |      d |d d%|      S )&Nr   r    rt   r   rF   r   r   r[   rN   rV   
updateTimeorderStatusorderAmtr   rT   uuidclientOrderId
customerIDr   r   lastTradeTimestamplastUpdateTimestamprL   r   r   
orderPrice	stopPricer   avgPricer   	remaining	remainAmtfilledaccAmtstatus)r   r   r   )	r\   r]   r   r   rG   
safe_orderr   r   parse_ws_order_status)r@   r   r[   r   rawType	typePartsr   exchangeTyper   r   rL   r   r  orderAmountr   s                  rB   r   zlbank.parse_ws_order:  sf   T ooe]B?"";;MM#&		1-''	15e6 1 , 88wD##E62!!(FC8%%k<@	!!+}=&&{J?H45=D  
E 
$"";7 
 T--k<H 
 	 

 Y/ 
 !$ 
 "4#4#4[,#O 
 f 
 D 
 D 
 T''WlK 
  
 t''Z@ 
 d((h
K 
 ))+{C 
  d&&{H=! 
" d008# 
$ ) 
* + 	rC   c                 8    dddddd}| j                  |||      S )Ncanceledr   closed)z-10124)r]   )r@   r  statusess      rB   r  zlbank.parse_ws_order_status  s/    
 &&99rC   c                 0  K   | j                          d{    | j                  |      }| j                  d   d   }d|d   z   }|d}dd||d	   d
}| j                  ||      }| j	                  ||||       d{   }	|	j                         S 7 {7 w)a\  

        https://www.lbank.com/en-US/docs/index.html#request-amp-subscription-instruction

        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|None limit: the maximum amount of order book entries to return
        :param dict params: extra parameters specific to the lbank api endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/en/latest/manual.html#order-book-structure>` indexed by market symbols
        Nr"   r   fetchOrderbook:rL   d   rR   depthrT   )rU   rR   r  rV   )rZ   r[   r9   r=   rb   rN   
r@   rL   rN   rd   r[   re   rh   rm   rR   	orderbooks
             rB   fetch_order_book_wszlbank.fetch_order_book_ws  s      !!!V$iit$'&*::=E4L	
	 ""9f5**S+wLL	   	" Ms"   BBA&B=B>BBc                 T  K   | j                          d{    | j                  |      }| j                  d   d   }d|d   z   }| j                  |d      }|d}dd	||d
   d}| j	                  ||      }| j                  ||||       d{   }	|	j                         S 7 7 w)aD  

        https://www.lbank.com/en-US/docs/index.html#market-depth

        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|None limit: the maximum amount of order book entries to return
        :param dict params: extra parameters specific to the lbank api endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/en/latest/manual.html#order-book-structure>` indexed by market symbols
        Nr"   r   
orderbook:rL   aggregationr  rm   r  rT   )rU   rm   r  rV   )rZ   r[   r9   omitr=   rb   rN   r  s
             rB   watch_order_bookzlbank.watch_order_book  s      !!!V$iit$"VH%556=1=E! 4L	
	 ""9f5**S+wLL	   	" Ms"   B(B$A8B(B&B(&B(c                    | j                  |d      }| j                  |      }| j                  |d|      }| j                  |d      }| j                  |      }|| j                  vr| j                  i       | j                  |<   | j                  |   }| j                  |||dd      }	|j                  |	       d|z   }
|j                  ||
       d|z   }
|j                  ||
       y )NrV   r  r   bidsasksr  r  )	r]   r   r\   r   
orderbooks
order_bookparse_order_bookresetr   )r@   r   ri   r   rL   	orderBookr   r   r  snapshotrh   s              rB   handle_order_bookzlbank.handle_order_book  s    r ##GV4!!(+OOGWg>	##GT2NN8,	$//)&*oob&9DOOF#OOF+	((FIvvV!"V+y+.'&0y+.rC   c                     | j                  |dd      }t        | j                  dz   |z         }|j                  |       y )Nri   r    )r]   r   rT   reject)r@   r   ri   errMsgerrors        rB   handle_error_messagezlbank.handle_error_message  s;     !!'9b9dggmf45erC   r   c                    K   | j                  |d      }	 |j                  d|d       d {    y 7 # t        $ r}| j                  ||       Y d }~y d }~ww xY ww)Npingpong)rU   r.  )r]   send	Exceptionon_error)r@   r   ri   pingIdes        rB   handle_pingzlbank.handle_ping"  sb      !!'62	%++      	%MM&!$$	%s6   A5 35 A5 	AAAAAc                 t   | j                  |d      }|dk(  r| j                  ||       y | j                  |dd      }|dk(  r| j                  | j                  ||       y | j
                  | j                  | j                  | j                  | j                  d}| j                  ||      }|
 |||       y y )Nr  r*  r   rU   r-  )rS   r  r   r   r   )r]   r+  r   spawnr4  r   r%  r   r   r   r\   )r@   r   ri   r  r   handlershandlers          rB   handle_messagezlbank.handle_message/  s    !!'84W%%fg6!!'68<6>JJt''9%%++''&&--
 //(D1FG$ rC   c                   K   | j                   d   d   }| j                  |      }| j                         }d}| j                  |j                  |      }|| j                          | j                  |       d {   }| j                  |d      }|durt        | j                  dz         | j                  |d      | j                  |d      d	|j                  d<   n| j                  |d
d      }	|	|k  rzd|d   i}
| j                  | j                  |
|             d {   }| j                  |d      }|dk7  rt        | j                  dz         | j                  |d      |d   d   d
<   |j                  d   d   S 7 
7 cw)Nr"   r   authenticatedresultTz failed to get subscribe keydataiZ2 )r   expiresr>  r   r   r   truez# failed to refresh the SubscribeKeysubscriptions)r9   r   millisecondsr\   r@  check_required_credentialsspotPrivatePostSubscribeGetKeyr   rT   r]   rH   rG   "spotPrivatePostSubscribeRefreshKeyextend)r@   rd   re   r   nowrh   r;  responser<  r>  rR   s              rB   r   zlbank.authenticateC  s     iit$S!!%(<(<kJ ++-!@@HHH __Xx8FT!#DGG.L$LMM''&988C15F  1
 ''y!DG}"M%$8! "&!H!HU\^dIe!ff ))(H=V#'2W(WXXFJhhsT[F\'8C##O4U;;3 I" gs&   A8F	:F;B(F	#F$A!F	F	)N)'__name__
__module____qualname__r   r?   rK   strr   r   listrj   rr   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   r   r   r  r	   r  r  r%  r+  r   r4  r9  r   __classcell__)rA   s   @rB   r   r      s   +# +Z ;?T`dmo  O3  Os  OZ]  Otxy}t~  OD 8<$]ajl H HC HWZ Hquvzq{ H@c0J 9; OC Ov O. 68 Q QF Q,2@/b ?CQU^` OC O O3 Oeijoep O8 <@d[] S SS S Sbfglbm S2-9^'R 04$UYbd  U  UC  Us  Uimnsit  UD .DOb: CGr ! !C !V_ !6 @DB !S ! !S\ !8G/R% %%( )+ #<rC   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r`   ccxt.base.typesr   r   r   r	   r
   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   async_supportr    rC   rB   <module>rW     s?     f f  J J J 4  *W<D$$ W<rC   