
    eiQ                         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mZ d dlmZ d dlmZ d dlmZ d dlmZ  G d d	ej        j                  ZdS )
    N)
ArrayCacheArrayCacheByTimestamp)AnyBoolInt	OrderBookStringsTickerTickersTrade)Client)List)ExchangeError)
BadRequestc                       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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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                 N   |                      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|                                 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_dictionaryr4   )self	__class__s    J/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/p2b.pyr;   zp2b.describe   s   c4 0 0 9 9 ; ;d#U !%  	
   u !% $U       d !$ u" t#$ %&  $)-)  . 3 ##	  G  G! #99;; $ 	[0>
 0>
 0 0 0	    r.   messageHashc                    K   | j         d         d         }|||                                 d}|                     ||          }|                     ||||           d{V S )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)r6   millisecondsextendwatch)r=   r.   rA   requestrE   url	subscribequerys           r?   rL   zp2b.subscribeD   sy       it$##%%
 
	
 Iv..ZZ[%EEEEEEEEEr@   r*   Nsymbol	timeframesincelimitc                   K   |                                   d{V  |                     | j        di           }|                     ||          }|t	          | j        dz   |z             |                     |          }|d         |g}	d|d         z   }
|                     d|
|	|           d{V }| j        r|	                    ||          }| 
                    |||dd	          S )
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
        Nr2   z' watchOHLCV cannot take a timeframe of rF   zkline::rN   zkline.subscriber   T)load_markets
safe_valuer7   safe_integerr   rF   marketrL   
newUpdatesgetLimitfilter_by_since_limit)r=   rN   rO   rP   rQ   rE   r2   channelrV   rJ   rA   ohlcvs               r?   watch_ohlcvzp2b.watch_ohlcvW   s      !!!!!!!!!__T\<DD
##J	::?TW'PPS\\]]]V$$4L
  &"22nn%6WfUUUUUUUU? 	2NN6511E))%q$GGGr@   c                   K   |                                   d{V  |                     | j        d          }|                     |dd          }|                     |dd|          \  }}|                     |          }|d         }d| j        d         |d	         <   | j        d         }t          |                                          }|d
z   |d         z   }|                     |dz   |||           d{V S )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/   rD   rN   Tr3   rF   ::
.subscribe)	rS   	safe_dictr7   safe_stringhandle_option_and_paramsrV   listkeysrL   )	r=   rN   rE   watchTickerOptionsr.   rV   r3   rJ   rA   s	            r?   watch_tickerzp2b.watch_tickert   s      !!!!!!!!!!^^DL-HH 2FGDD44VXvtTTfV$$!37\"6$<0\,/
z(())TkF8$44^^D<$7gvVVVVVVVVVr@   symbolsc                   K   |                                   d{V  |                     |dd          }|                     | j        d          }|                     |dd          }|                     |dd|          \  }}g }g }t          dt          |                    D ]Y}|                     ||                   }|	                    |dz   |d	         z              |	                    |d
                    Z| j
        d         d         }	|dz   ||                                 d}
|                     |	||                     |
|          |           d{V  |                     | j        d	|          S )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/   rD   r   r^   rN   rF   r(   r   r_   rC   )rS   market_symbolsr`   r7   ra   rb   rangelenrV   appendr6   rG   watch_multiplerH   filter_by_arraytickers)r=   rg   rE   re   r.   messageHashesargsirV   rK   rJ   s              r?   watch_tickerszp2b.watch_tickers   s      !!!!!!!!!%%gtU;;!^^DL-HH 2FGDD44VXvtTTfq#g,,'' 	& 	&A[[,,F  vh/?!?@@@KKt%%%%it$\)##%%
 

 !!#}dkk'66R6RTabbbbbbbbb##DL(GDDDr@   c                 D   K   |                      |g|||           d{V S )a4  
        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=   rN   rP   rQ   rE   s        r?   watch_tradeszp2b.watch_trades   s6       22F8UE6RRRRRRRRRr@   c                   K   |                                   d{V  |                     |dddd          }g }|>t          dt          |                    D ] }|                    d||         z              !|                     |          }| j        d         d         }d||                                 d	}	|                     |	|          }
| 	                    |||
|           d{V }| j
        rB|                     |d          }|                     |d
          }|                    ||          }|                     |||dd          S )a5  
        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.subscriberC   rN   	timestamp)rS   ri   rj   rk   rl   
market_idsr6   rG   rH   rm   rW   rT   ra   rX   rY   )r=   rg   rP   rQ   rE   rp   rr   	marketIdsrK   rL   rM   tradesfirsttradeSymbols                 r?   ru   zp2b.watch_trades_for_symbols   su      !!!!!!!!!%%gtUD$GG1c'll++ = =$$Y%;<<<<OOG,,	it$'##%%
 
	
 Iv..**3umTTTTTTTT? 	8OOFA..E**5(;;KOOK77E))&%TRRRr@   c                 .  K   |                                   d{V  |                     |          }d}d|d         z   }|                     |dd          }|d}|d         ||g}|                     ||||           d{V }	|	                                S )	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::rN   intervalz0.001d   rF   )rS   rV   ra   rL   rQ   )
r=   rN   rQ   rE   rV   r.   rA   r   rJ   	orderbooks
             r?   watch_order_bookzp2b.watch_order_book   s       !!!!!!!!!V$$ #fX&66##FJ@@=E4L

 ..{GVLLLLLLLL	   r@   clientc                 `   |                      |d          }|                      |d          }|                     |d          }|                    d          }|                     |d          }|                     |d          }|                     |          }|                     | j        di           }	|                     ||	          }
|                     |d          }|dz   |z   }|                     ||          }|                     | j	        |i           | j	        |<   |                     | j	        |         |
          }|h|;| 
                    | j        d	d
          }t          |          }|| j	        |         |
<   |                    |           |                    ||           |S )NrE   r   rD   .   r2   rN   r^   r0   r)   )	safe_listra   splitsafe_marketr`   r7   find_timeframeparse_ohlcvrT   ohlcvsrU   r   rl   resolve)r=   r   messagedatarD   splitMethodrZ   marketIdrV   r2   rO   rN   rA   parsedstoredrQ   s                   r?   handle_ohlcvzp2b.handle_ohlcv   s   & ~~gx00~~dA&&!!'844ll3''"";22##D!,,!!(++^^DL,CC
''<<	!!&(33nv-!!$//"oodk62FFFV!4i@@~))$,dKK.u5517F#I.MM&!!!NN6;///r@   c                 b   |                      |dg           }|                      |d          }|                     |d          }|                     |          }|                     |d          }|                     | j        |          }|5|                     | j        dd          }	t          |	          }|| j        |<   t          dt          |                    D ]5}
||
         }| 
                    ||          }|                    |           6d|z   }|                    ||           |S )NrE      r   rN   r1   r)   rx   )r   ra   r   rT   r|   rU   r7   r   rj   rk   parse_traderl   r   )r=   r   r   r   r|   r   rV   rN   tradesArrayr1   rr   itemtraderA   s                 r?   handle_tradezp2b.handle_trade   s-   ( ~~gx44a((##D!,,!!(++!!&(33oodk6::++DL-NNK$[11K"-DKq#f++&& 	& 	&A!9D$$T622Eu%%%%&({K000r@   c                 @   |                      |dg           }|                     |d          }|                     |          }|                     |d          }|                    d          }|                     |d          }|                     |d          }	d }
|dk    r6|                     |d          }|                     |||d         d          }
n|                     |	|          }
|
d         }|
| j        |<   |d	z   |z   }|                    |
|           |S )
NrE   r   rD   r   r   price.updaterN   )lastcloserN   r^   )	r   ra   r   r   r`   safe_tickerparse_tickerro   r   )r=   r   r   r   r   rV   rD   r   messageHashStart
tickerDataticker	lastPricerN   rA   s                 r?   handle_tickerzp2b.handle_tickerF  s:   B ~~gx44##D!,,!!(++!!'844ll3''++K;;^^D!,,
^##((q11I%%!" *' '  FF &&z6::F!%V&-6v{+++r@   c                 f   |                      |dg           }|                     |d          }|                      |d          }|                      |d          }|                     |d          }|                     |          }|d         }	d|d         z   }
|                     |j        |
i           }|                     |d          }|                     | j        |	          }|+|                     i |          | j        |	<   | j        |	         }|t          d	t          |                    D ]b}|                     ||          }|                     |d	          }|                     |d          }|d         }|                    ||           c|t          d	t          |                    D ]b}|                     ||          }|                     |d	          }|                     |d          }|d         }|                    ||           c|	|d<   |                    ||
           d S )
NrE   r   asksbids   rN   r   rQ   r   )r   r`   ra   r   rT   subscriptionsrU   
orderbooks
order_bookrj   rk   safe_numberstorer   )r=   r   r   rE   r   r   r   r   rV   rN   rA   subscriptionrQ   r   rr   bidpriceamountbookSideaskbooksides                        r?   handle_order_bookzp2b.handle_order_book~  s+   & 266~~fa((~~dF++~~dF++##FA..!!(++!#fX&66v';["MM!!,88OODOV<<	&*oob%&@&@DOF#/I1c$ii(( . .oodA..((a00))#q11$V,uf----1c$ii(( . .oodA..((a00))#q11$V,uf----$	(y+.....r@   c                 X   |                      ||          rd S |                     |d          }|dk    r|                     ||           d S |                     |d          }| j        | j        | j        | j        | j        d}|                     ||          }| |||           d S d S )NresultpongrD   )zdepth.updater   zkline.updatezstate.updatezdeals.update)handle_error_messagera   handle_pongr   r   r   r   rT   )r=   r   r   r   rD   methodsendpoints          r?   handle_messagezp2b.handle_message  s    $$VW55 	F!!'844VVW---F!!'844 2 . - . -
 
 ??7F33HVW%%%%%  r@   c                     |                      |d          }|-t          | j        dz   |                     |          z             dS )Nerrorz error: F)ra   r   rF   json)r=   r   r   r   s       r?   r   zp2b.handle_error_message  sH      '22* 4tyy7G7G GHHHur@   c                 2    dg |                                  dS )zo
        https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#ping
 @param client
        zserver.pingrC   )rG   )r=   r   s     r?   r4   zp2b.ping  s(     $##%%
 
 	
r@   c                 b    |                      |d|                                           |_        |S )NrF   )rU   rG   lastPong)r=   r   r   s      r?   r   zp2b.handle_pong  s.     ++GT4;L;L;N;NOOr@   c                     |                                  | j        d<   t          t          |                               ||           d S Nr3   )r<   r7   r:   r   on_errorr=   r   r   r>   s      r?   r   zp2b.on_error  A    %)%@%@%B%B\"c4!!&%00000r@   c                     |                                  | j        d<   t          t          |                               ||           d S r   )r<   r7   r:   r   on_closer   s      r?   r   zp2b.on_close  r   r@   )"__name__
__module____qualname__r   r;   strrL   r   r   rc   r\   r
   rf   r	   r   rs   r   rv   ru   r   r   r   r   r   r   r   r   r   r   r4   r   r   r   __classcell__)r>   s   @r?   r   r      sY       1# 1 1 1 1 1 1f LN F FC Fc F F F F& ?DRVeirt H H H HC H_b Hy}  C  zD H H H H: 68 W W WF W W W W0 6:" E E7 E E E E E@ <@d[] S S SS S Sbfglbm S S S S OSaenp S Sd3i S S[^ Suyz  vA S S S SB @DB ! !S ! !S\ ! ! ! !6(6 ( ( ( (T$6 $ $ $ $L6F 6 6 6 6p0/ 0/ 0/ 0/ 0/d&V & & & &&6 t    	
6 	
 	
 	
 	
	& 	 	 	 	1v 1 1 1 1 1 11v 1 1 1 1 1 1 1 1 1 1r@   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   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 N N N N N N N V V V V V V V V V V V V V V V V V V V V 4 4 4 4 4 4       * * * * * * ' ' ' ' ' 'V1 V1 V1 V1 V1$

  V1 V1 V1 V1 V1r@   