
    ":hQ                         d dl Zd dlmZmZ d dlmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ  G d d	ej(                  j*                        Zy)
    N)
ArrayCacheArrayCacheByTimestamp)AnyInt	OrderBookStringsTickerTickersTrade)Client)List)ExchangeError)
BadRequestc            
       v    e Zd Zdef fdZi fdedefdZdddi fded	ed
edee	   fdZ
i fdedefdZdi fdedefdZddi fded	ed
edee   fdZddi fdee   d	ed
edee   fdZdi fded
edefdZdefdZdefdZdefdZdefdZdefdZdefdZdefdZdefdZdef fdZdef fdZ xZ S )p2breturnc                    | j                  t        t        |          i ddddddddddddd	dd
dddddddddddddddddddddddddiidddddddddiddi| j	                         dd | j
                  id!      S )"NwsTcancelAllOrdersWsFcancelOrdersWscancelOrderWscreateOrderWseditOrderWsfetchBalanceWsfetchOpenOrdersWsfetchOrderWsfetchTradesWswatchBalancewatchMyTrades
watchOHLCVwatchOrderBookwatchOrderswatchTickerwatchTickers)watchTradeswatchTradesForSymbolsapizwss://apiws.p2pb2b.com/  i  i  i  iQ )15m30m1h1dnamestate)
OHLCVLimittradesLimit
timeframesr#   r$   
tickerSubsping)hasurlsoptions	streaming)deep_extendsuperr   describecreate_safe_dictionaryr3   )self	__class__s    F/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/p2b.pyr:   zp2b.describe   sk   c4 9 ;d#U !%  	
   u !% $U       d !$ u" t#$ %&  $)-). 3 ##	 G  G! #99;$ 		[0>
 0 0	    r-   messageHashc                    K   | j                   d   d   }||| j                         d}| j                  ||      }| j                  ||||       d{   S 7 w)a[  
 @ignore
        Connects to a websocket channel
        :param str name: name of the channel
        :param str messageHash: string to look up in handler
        :param str[]|float[] request: endpoint parameters
        :param dict [params]: extra parameters specific to the p2b api
        :returns dict: data from the websocket stream
        r'   r   methodparamsidN)r5   millisecondsextendwatch)r<   r-   r@   requestrD   url	subscribequerys           r>   rK   zp2b.subscribeD   sa      iit$##%
	
 Iv.ZZ[%EEEEs   AAAAr)   Nsymbolsincelimitc                   K   | j                          d{    | j                  | j                  di       }| j                  ||      }|t	        | j
                  dz   |z         | j                  |      }|d   |g}	d|d   z   }
| j                  d|
|	|       d{   }| j                  r|j                  ||      }| j                  |||dd	      S 7 7 9w)
a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market. Can only subscribe to one timeframe at a time for each symbol

        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#kline-candlestick

        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: 15m, 30m, 1h or 1d
        :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
        Nr1   z' watchOHLCV cannot take a timeframe of rE   zkline::rM   zkline.subscriber   T)load_markets
safe_valuer6   safe_integerr   rE   marketrK   
newUpdatesgetLimitfilter_by_since_limit)r<   rM   	timeframerN   rO   rD   r1   channelrT   rI   r@   ohlcvs               r>   watch_ohlcvzp2b.watch_ohlcvW   s      !!!__T\\<D
##J	:?TWW'PPS\\]]V$4L
  &"22nn%6WfUU??NN651E))%q$GG 	" Vs"   CCBCC8CCc                   K   | j                          d{    | j                  | j                  d      }| j                  |dd      }| j	                  |dd|      \  }}| j                  |      }|d   }d| j                  d   |d	   <   | j                  d   }t        |j                               }|d
z   |d   z   }| j                  |dz   |||       d{   S 7 7 w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#last-price
        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#market-status

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param dict [params.method]: 'state'(default) or 'price'
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        Nr#   r-   r.   rC   rM   Tr2   rE   ::
.subscribe)	rQ   	safe_dictr6   safe_stringhandle_option_and_paramsrT   listkeysrK   )	r<   rM   rD   watchTickerOptionsr-   rT   r2   rI   r@   s	            r>   watch_tickerzp2b.watch_tickert   s      !!!!^^DLL-H 2FGD44VXvtTfV$!37\"6$<0\\,/
z()TkF8$44^^D<$7gvVVV 	" Ws"   C"CCC"C C" C"symbolsc                   K   | j                          d{    | j                  |dd      }| j                  | j                  d      }| j	                  |dd      }| j                  |dd|      \  }}g }g }t        dt        |            D ]D  }| j                  ||         }|j                  |dz   |d	   z          |j                  |d
          F | j                  d   d   }	|dz   || j                         d}
| j                  |	|| j                  |
|      |       d{    | j                  | j                  d	|      S 7 07 $w)a  

        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#last-price
        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#market-status

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list
        :param str[] [symbols]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param dict [params.method]: 'state'(default) or 'price'
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        NFr#   r-   r.   rC   r   r]   rM   rE   r'   r   r^   rB   )rQ   market_symbolsr_   r6   r`   ra   rangelenrT   appendr5   rF   watch_multiplerG   filter_by_arraytickers)r<   rf   rD   rd   r-   messageHashesargsirT   rJ   rI   s              r>   watch_tickerszp2b.watch_tickers   sT     !!!%%gtU;!^^DLL-H 2FGD44VXvtTfq#g,' 	&A[[,F  vh/?!?@KKt%	& iit$\)##%

 !!#}dkk'66RTabbb##DLL(GDD% 	"" 	cs"   EEDE$E	%"E	Ec                 H   K   | j                  |g|||       d{   S 7 w)a6  
        get the list of most recent trades for a particular symbol

        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#deals

        :param str symbol: unified symbol of the market to fetch trades for
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        N)watch_trades_for_symbols)r<   rM   rN   rO   rD   s        r>   watch_tradeszp2b.watch_trades   s'      22F8UE6RRRRs   " "c                 P  K   | j                          d{    | j                  |dddd      }g }|1t        dt        |            D ]  }|j	                  d||   z           | j                  |      }| j                  d   d   }d|| j                         d	}	| j                  |	|      }
| j                  |||
|       d{   }| j                  r6| j                  |d      }| j                  |d
      }|j                  ||      }| j                  |||dd      S 7 7 ^w)a7  
        get the list of most recent trades for a list of symbols

        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#deals

        :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>`
        NFTr   deals::r'   r   zdeals.subscriberB   rM   	timestamp)rQ   rh   ri   rj   rk   
market_idsr5   rF   rG   rl   rU   rR   r`   rV   rW   )r<   rf   rN   rO   rD   ro   rq   	marketIdsrJ   rK   rL   tradesfirsttradeSymbols                 r>   rt   zp2b.watch_trades_for_symbols   s1     !!!%%gtUD$G1c'l+ =$$Y%;<=OOG,	iit$'##%
	
 Iv.**3umTT??OOFA.E**5(;KOOK7E))&%TRR' 	" Us#   D&D!B.D&D$AD&$D&c                   K   | j                          d{    | j                  |      }d}d|d   z   }| j                  |dd      }|d}|d   ||g}| j                  ||||       d{   }	|	j	                         S 7 j7 w)	a  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#depth-of-market

        :param str symbol: unified symbol of the market to fetch the order book for
        :param int [limit]: 1-100, default=100
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param float [params.interval]: 0, 0.00000001, 0.0000001, 0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, interval of precision for order, default=0.001
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        Nzdepth.subscribeorderbook::rM   intervalz0.001d   rE   )rQ   rT   r`   rK   rO   )
r<   rM   rO   rD   rT   r-   r@   r   rI   	orderbooks
             r>   watch_order_bookzp2b.watch_order_book   s      !!!V$ #fX&66##FJ@=E4L

 ..{GVLL	   	" Ms"   BBAB,B-BBclientc                    | j                  |d      }| j                  |d      }| j                  |d      }|j                  d      }| j                  |d      }| j                  |d      }| j                  |      }| j	                  | j
                  di       }	| j                  ||	      }
| j                  |d      }|dz   |z   }| j                  ||      }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |
      }|_|:| j                  | j
                  d	d
      }t        |      }|| j                  |   |
<   |j                  |       |j                  ||       |S )NrD   r   rC   .   r1   rM   r]   r/   r(   )	safe_listr`   splitsafe_marketr_   r6   find_timeframeparse_ohlcvrR   ohlcvsrS   r   rk   resolve)r<   r   messagedatarC   splitMethodrY   marketIdrT   r1   rX   rM   r@   parsedstoredrO   s                   r>   handle_ohlcvzp2b.handle_ohlcv   si   & ~~gx0~~dA&!!'84ll3'"";2##D!,!!(+^^DLL,C
''<	!!&(3nv-!!$/"oodkk62FFV!4i@~))$,,dK.u517F#I.MM&!NN6;/r?   c                    | j                  |dg       }| j                  |d      }| j                  |d      }| j                  |      }| j                  |d      }| j                  | j                  |      }|7| j                  | j                  dd      }	t        |	      }|| j                  |<   t        dt        |            D ]*  }
||
   }| j                  ||      }|j                  |       , d|z   }|j                  ||       |S )NrD      r   rM   r0   r(   rw   )r   r`   r   rR   r{   rS   r6   r   ri   rj   parse_traderk   r   )r<   r   r   r   r{   r   rT   rM   tradesArrayr0   rq   itemtrader@   s                 r>   handle_tradezp2b.handle_trade   s   ( ~~gx4a(##D!,!!(+!!&(3oodkk6:++DLL-NK$[1K"-DKKq#f+& 	&A!9D$$T62Eu%	&  &({K0r?   c                    | j                  |dg       }| j                  |d      }| j                  |      }| j                  |d      }|j                  d      }| j                  |d      }| j	                  |d      }	d }
|dk(  r+| j                  |d      }| j                  |||d   d      }
n| j                  |	|      }
|
d   }|
| j                  |<   |d	z   |z   }|j                  |
|       |S )
NrD   r   rC   r   r   price.updaterM   )lastcloserM   r]   )	r   r`   r   r   r_   safe_tickerparse_tickerrn   r   )r<   r   r   r   r   rT   rC   r   messageHashStart
tickerDataticker	lastPricerM   r@   s                 r>   handle_tickerzp2b.handle_tickerF  s   B ~~gx4##D!,!!(+!!'84ll3'++K;^^D!,
^#((q1I%%!" *' F &&z6:F!%V&-6v{+r?   c                    | j                  |dg       }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |      }|d   }	d|d   z   }
| j	                  |j
                  |
i       }| j                  |d      }| j	                  | j                  |	      }|.| j                  i |      | j                  |	<   | j                  |	   }|gt        d	t        |            D ]O  }| j	                  ||      }| j                  |d	      }| j                  |d      }|d   }|j                  ||       Q |gt        d	t        |            D ]O  }| j	                  ||      }| j                  |d	      }| j                  |d      }|d   }|j                  ||       Q |	|d<   |j                  ||
       y )
NrD   r   asksbids   rM   r   rO   r   )r   r_   r`   r   rR   subscriptionsrS   
orderbooks
order_bookri   rj   safe_numberstorer   )r<   r   r   rD   r   r   r   r   rT   rM   r@   subscriptionrO   r   rq   bidpriceamountbookSideaskbooksides                        r>   handle_order_bookzp2b.handle_order_book~  s   & 26~~fa(~~dF+~~dF+##FA.!!(+!#fX&66v';';["M!!,8OODOOV<	&*oob%&@DOOF#/I1c$i( .oodA.((a0))#q1$V,uf-. 1c$i( .oodA.((a0))#q1$V,uf-. %	(y+.r?   c                 R   | j                  ||      ry | j                  |d      }|dk(  r| j                  ||       y | j                  |d      }| j                  | j                  | j
                  | j                  | j                  d}| j                  ||      }|
 |||       y y )NresultpongrC   )zdepth.updater   zkline.updatezstate.updatezdeals.update)handle_error_messager`   handle_pongr   r   r   r   rR   )r<   r   r   r   rC   methodsendpoints          r>   handle_messagezp2b.handle_message  s    $$VW5!!'84VVW-!!'84 22 .. -- .. --
 ??7F3VW%  r?   c                     | j                  |d      }|*t        | j                  dz   | j                  |      z         y)Nerrorz error: F)r`   r   rE   json)r<   r   r   r   s       r>   r   zp2b.handle_error_message  s?      '2* 4tyy7G GHHr?   c                 *    dg | j                         dS )zo
        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#ping
 @param client
        zserver.pingrB   )rF   )r<   r   s     r>   r3   zp2b.ping  s      $##%
 	
r?   c                 4    | j                  |d      |_        |S )NrE   )rS   lastPong)r<   r   r   s      r>   r   zp2b.handle_pong  s     ++GT:r?   c                 h    | j                         | j                  d<   t        t        |   ||       y Nr2   )r;   r6   r9   r   on_errorr<   r   r   r=   s      r>   r   zp2b.on_error  +    %)%@%@%B\"c4!&%0r?   c                 h    | j                         | j                  d<   t        t        |   ||       y r   )r;   r6   r9   r   on_closer   s      r>   r   zp2b.on_close  r   r?   )!__name__
__module____qualname__r   r:   strrK   r   r   rb   r[   r	   re   r   r
   rr   r   ru   rt   r   r   r   r   r   r   r   r   r   r3   r   r   r   __classcell__)r=   s   @r>   r   r      s   1# 1f LN FC Fc F& 8=4^bkm H HS HX[ Hrvw{r| H: 68 W WF W0 6:" E7 E E@ <@d[] S SS S Sbfglbm S OSaenp Sd3i S S[^ Suyz  vA SB @DB !S ! !S\ !6(6 (T$6 $L6F 6p0/ 0/d&V &&6 	
6 	
	& 	1v 11v 1 1r?   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   r	   r
   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   async_supportr    r?   r>   <module>r      s<     N P P P 4  * 'V1$


 
  V1r?   