
    iE                         d dl Zd dlmZmZ d dl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)Int	OrderBookTickerTrade)Client)List)ExchangeError)
BadRequestc            
       ,    e Zd Z 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di fd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 ZdefdZdefdZdefdZ xZS )p2bc                 L   |                      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i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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_dictionaryr.   )self	__class__s    R/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/p2b.pyr5   zp2b.describe   s   c4 0 0 9 9 ; ;d#U !%  	
   u !% $U       d !$ u" t#$ %& t' , 3 ##	  G  G! #99;; $ 	Y/>
 />
 / / /	    r(   messageHashc                    K   | j         d         d         }|||                                 d}|                     ||          }|                     ||||           d{V S )ae  
         * @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)r0   millisecondsextendwatch)r7   r(   r;   requestr?   url	subscribequerys           r9   rF   zp2b.subscribeC   sy       it$##%%
 
	
 Iv..ZZ[%EEEEEEEEEr:   r$   Nsymbolsincelimitreturnc                   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
        :see: 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
        Nr,   z' watchOHLCV cannot take a timeframe of r@   zkline::rH   zkline.subscriber   T)load_markets
safe_valuer1   safe_integerr   r@   marketrF   
newUpdatesgetLimitfilter_by_since_limit)r7   rH   	timeframerI   rJ   r?   r,   channelrP   rD   r;   ohlcvs               r9   watch_ohlcvzp2b.watch_ohlcvV   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
        :see: https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#last-price
        :see: 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)   r>   rH   Tr-   r@   ::z
.subscribe)	rM   	safe_dictr1   safe_stringhandle_option_and_paramsrP   listkeysrF   )	r7   rH   r?   watchTickerOptionsr(   rP   r-   rD   r;   s	            r9   watch_tickerzp2b.watch_tickerq   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:   c                 4  K   |                                   d{V  |                     |          }|d         g}d|d         z   }|                     d|||           d{V }| j        r|                    ||          }|                     |||dd          S )a:  
        get the list of most recent trades for a particular symbol
        :see: 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>`
        Nr@   deals::rH   zdeals.subscribe	timestampT)rM   rP   rF   rQ   rR   rS   )	r7   rH   rI   rJ   r?   rP   rD   r;   tradess	            r9   watch_tradeszp2b.watch_trades   s       !!!!!!!!!V$$4L
  &"22~~&7gvVVVVVVVV? 	3OOFE22E))&%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
        :see: 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::rH   intervalz0.001d   r@   )rM   rP   r[   rF   rJ   )
r7   rH   rJ   r?   rP   r(   r;   rh   rD   	orderbooks
             r9   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 )Nr?   r   r>   .   r,   rH   rY   r*   r#   )	safe_listr[   splitsafe_marketrZ   r1   find_timeframeparse_ohlcvrN   ohlcvsrO   r   appendresolve)r7   rl   messagedatar>   splitMethodrU   marketIdrP   r,   rT   rH   r;   parsedstoredrJ   s                   r9   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 )Nr?      r   rH   r+   r#   rb   )rp   r[   rr   rN   rd   rO   r1   r   rangelenparse_traderv   rw   )r7   rl   rx   ry   rd   r{   rP   rH   tradesArrayr+   iitemtrader;   s                 r9   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         }|d	z   |z   }|                    |
|           |S )
Nr?   r   r>   rn   r   price.updaterH   )lastcloserH   rY   )rp   r[   rr   rq   rZ   safe_tickerparse_tickerrw   )r7   rl   rx   ry   r{   rP   r>   rz   messageHashStart
tickerDataticker	lastPricerH   r;   s                 r9   handle_tickerzp2b.handle_ticker  s.   B ~~gx44##D!,,!!(++!!'844ll3''++K;;^^D!,,
^##((q11I%%!" *' '  FF &&z6::F!&-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 )
Nr?   r   asksbids   rH   rg   rJ   r   )rp   rZ   r[   rr   rN   subscriptionsrO   
orderbooks
order_bookr   r   safe_numberstorerw   )r7   rl   rx   r?   ry   r   r   r{   rP   rH   r;   subscriptionrJ   rj   r   bidpriceamountbookSideaskbooksides                        r9   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pongr>   )zdepth.updater   zkline.updatezstate.updatezdeals.update)handle_error_messager[   handle_pongr   r   r~   r   rN   )r7   rl   rx   r   r>   methodsendpoints          r9   handle_messagezp2b.handle_messagen  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)r[   r   r@   json)r7   rl   rx   r   s       r9   r   zp2b.handle_error_message  sH      '22* 4tyy7G7G GHHHur:   c                 2    dg |                                  dS )z
        :see: https://github.com/P2B-team/P2B-WSS-Public/blob/main/wss_documentation.md#ping
         * @param client
        zserver.pingr=   )rA   )r7   rl   s     r9   r.   zp2b.ping  s(     $##%%
 
 	
r:   c                 <    |                      |d          |_        |S )Nr@   )rO   lastPong)r7   rl   rx   s      r9   r   zp2b.handle_pong  s      ++GT::r:   c                 j    |                                  | j        d<   |                     ||           d S Nr-   )r6   r1   on_errorr7   rl   r   s      r9   r   zp2b.on_error  4    %)%@%@%B%B\"fe$$$$$r:   c                 j    |                                  | j        d<   |                     ||           d S r   )r6   r1   on_closer   s      r9   r   zp2b.on_close  r   r:   )__name__
__module____qualname__r5   strrF   r   r
   r]   rW   r   r`   r   re   r   rk   r	   r~   r   r   r   r   r   r.   r   r   r   __classcell__)r8   s   @r9   r   r      s       0 0 0 0 0d LN F FC Fc F F F F& 8=4^bkm H H HS HX[ Hrvw{r| H H H H6 68 W W WF W W W W, <@d[] S S SS S Sbfglbm S S S S* @DB ! !S ! !S\ ! ! ! !2(6 ( ( ( (T$6 $ $ $ $L5F 5 5 5 5n0/ 0/ 0/ 0/ 0/d&V & & & &&6    	
 	
 	
	& 	 	 	 	%v % % % %%v % % % % % % % %r:   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   !ccxt.async_support.base.ws.clientr	   typingr
   ccxt.base.errorsr   r   async_supportr    r:   r9   <module>r      s        N N N N N N N N 9 9 9 9 9 9 9 9 9 9 9 9 4 4 4 4 4 4       * * * * * * ' ' ' ' ' 'T% T% T% T% T%$

  T% T% T% T% T%r:   