
    eiJ                         d dl Zd dl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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ArrayCacheBySymbolById)AnyBalancesIntMarket	OrderBookStrTickerTrade)Client)List)ExchangeErrorc            
       &    e Zd Zdef fdZd Zdi fdededefdZ	de
fd	Zddi fded
ededee         fdZde
fdZdddi fded
ededee         fdZde
fdZddededefdZi fdedefdZde
fdZd Zi fdefdZde
fdZde
fdZ xZS )bitoproreturnc                     |                      t          t          |                                           ddddddddddd
ddddiddddd	d	d
di iidd          S )NTF)
wswatchBalancewatchMyTrades
watchOHLCVwatchOrderBookwatchOrderswatchTickerwatchTickerswatchTradeswatchTradesForSymbolsr   z&wss://stream.bitopro.com:443/ws/v1/pubz+wss://stream.bitopro.com:443/ws/v1/pub/auth)publicprivate)apiKeysecretlogin  optionsheaders)tradesLimitordersLimitr   )hasurlsrequiredCredentialsr$   )deep_extendsuperr   describe)self	__class__s    N/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/bitopro.pyr-   zbitopro.describe   s    gt 4 4 = = ? ? $!%#"&$# %#).  FL  $ $  $#!2 	 	1"B
 "B
 " " "	    c                    K   | j         d         d         dz   |z   dz   |z   }|                     ||d |           d {V S )Nr   r   /)r)   watch)r.   pathmessageHashmarketIdurls        r0   watch_publiczbitopro.watch_public6   sT      ioh'#-4s:XEZZ[$DDDDDDDDDr1   Nsymbollimitc                   K   |A|dk    r;|dk    r5|dk    r/|dk    r)|dk    r#|dk    r|dk    rt          | j        d	z             |                                  d{V  |                     |          }|d
         }d|z   }d}|	|d         }n!|d         dz   |                     |          z   }|                     d||           d{V }|                                S )aY  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://github.com/bitoex/bitopro-offical-api-docs/blob/master/ws/public/order_book_stream.md

        :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
        N   
      2   d   i  r#   zL watchOrderBook limit argument must be None, 5, 10, 20, 50, 100, 500 or 1000r:   zORDER_BOOK:id:zorder-books)r   rB   load_marketsmarketnumber_to_stringr9   r;   )r.   r:   r;   paramsrE   r6   endPart	orderbooks           r0   watch_order_bookzbitopro.watch_order_book:   s1      

"5B;;Ub[[W\`cWcWcjosvjvjv  ~C  GK  ~K  ~K#DG.|$|}}}!!!!!!!!!V$$!(61=TlGGTlS(4+@+@+G+GGG++M;PPPPPPPP	   r1   clientc           	         |                      |d          }|                     |d d          }|d         }|                      |d          }|dz   |z   }|                     | j        |          }||                     i           }|                     |d          }	|                     |||	ddd	d
          }
|                    |
           |                    ||           d S )Npair_r:   eventrC   	timestampbidsaskspriceamount)	safe_stringsafe_market
safe_value
orderbooks
order_booksafe_integerparse_order_bookresetresolve)r.   rK   messager7   rE   r:   rO   r6   rI   rP   snapshots              r0   handle_order_bookzbitopro.handle_order_bookT   s    , ##GV44!!(D#66!  '22ckF*OODOV<<	++I%%g{;;	((&)VVU\^fgg!!!y+.....r1   sincec                 0  K   |                                   d{V  |                     |          }|d         }d|z   }|                     d||d                    d{V }| j        r|                    ||          }|                     |||dd          S )a=  
        get the list of most recent trades for a particular symbol

        https://github.com/bitoex/bitopro-offical-api-docs/blob/master/ws/public/trade_stream.md

        :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:   zTRADE:tradesrB   rP   T)rD   rE   r9   
newUpdatesgetLimitfilter_by_since_limit)r.   r:   ra   r;   rG   rE   r6   rc   s           r0   watch_tradeszbitopro.watch_tradesw   s       !!!!!!!!!V$$!#f,((;tMMMMMMMM? 	3OOFE22E))&%TRRRr1   c                 L   |                      |d          }|                     |d d          }|d         }|                      |d          }|dz   |z   }|                     |dg           }|                     ||          }	|                     | j        |          }
|
+|                     | j        dd          }t          |          }
t          d	t          |	                    D ]}|

                    |	|                    |
| j        |<   |                    |
|           d S )
NrM   rN   r:   rO   rC   datar&   r#   r   )rU   rV   rW   parse_tradesrc   rZ   r$   r   rangelenappendr]   )r.   rK   r^   r7   rE   r:   rO   r6   rawDatarc   tradesCacher;   is                r0   handle_tradezbitopro.handle_trade   s(   ( ##GV44!!(D#66!  '22ckF*//'6266""7F33oodk6::%%dlM4HHE$U++Kq#f++&& 	* 	*Avay)))))F{K00000r1   c                   K   |                                   |                                  d{V  d}|#|                     |          }|dz   |d         z   }| j        d         d         dz   dz   }|                     |           |                     ||d|           d{V }| j        r|                    ||          }|                     |||d	d
          S )aP  
        watches information on multiple trades made by the user

        https://github.com/bitoex/bitopro-offical-api-docs/blob/master/ws/private/matches_stream.md

        :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
USER_TRADErC   r:   r   r   r3   zuser-tradesrP   T)	check_required_credentialsrD   rE   r)   authenticater4   rd   re   rf   )	r.   r:   ra   r;   rG   r6   rE   r8   rc   s	            r0   watch_my_tradeszbitopro.watch_my_trades   s       	'')))!!!!!!!!!"[[((F%+fX.>>Kioi(3.>#zz#{D+FFFFFFFF? 	3OOFE22E))&%TRRRr1   c                 v   |                      |di           }|                     |d          }|                     |d          }|                     |          }|                     |          }|                     |dz   |z             }|                     |d          }	| j        0|                     | j        dd          }
t          |
          | _        | j        }|                     |          }|	                    |           |
                    ||	           |
                    ||	dz   |z              d S )	Nri   basequoter3   rO   r&   r#   rC   )rW   rU   safe_currency_coder:   myTradesrZ   r$   r   parse_ws_traderm   r]   )r.   rK   r^   ri   baseIdquoteIdrx   ry   r:   r6   r;   rc   parseds                r0   handle_my_tradezbitopro.handle_my_trade   s,   0 w33!!$//""411&&v..''00TCZ%/00&&w88= %%dlM4HHE2599DM$$T**fv{+++v{S069:::::r1   traderE   c                    |                      |d          }|                      |d          }|                     |d          }|                      |d          }|                      |d          }|                     |          }|                     |          }	|                     |dz   |	z             }
|                     |
|          }|                      |d          }|                     |d          }|                      |d	          }||d
k    rd}n|dk    rd}|                      |d          }d }|                      |d          }|                     |                      |d                    }|||d d}|                     |d          }d }||rd}nd}|                     |||||                     |          |
|||||d |d|          S )NmatchIDorderIDtransactionTimestamprx   ry   r3   rS   	orderTypesideasksellbidbuyvolumefeefeeCurrency)costcurrencyrateisMakermakertaker)rB   infoorderrP   datetimer:   takerOrMakertyper   rS   rT   r   r   )	rU   safe_timestamprz   r:   rV   safe_string_lowerrW   
safe_tradeiso8601)r.   r   rE   rB   orderIdrP   r}   r~   rx   ry   r:   rS   r   r   rT   r   	feeAmount	feeSymbolr   r   s                       r0   r|   zbitopro.parse_ws_trade   s   & eY//""5)44''/EFF	!!%00""5'22&&v..''00TCZ%/00!!&&11  00%%e[99v..u}}!!%22$$UE22	++D,<,<UM,R,RSS	 !% C
 //%33 '&&"Y//( 
  
   	r1   c                    K   |                                   d{V  |                     |          }|d         }d|z   }|                     d||d                    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/bitoex/bitopro-offical-api-docs/blob/master/ws/public/ticker_stream.md

        :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>`
        Nr:   zTICKER:tickersrB   )rD   rE   r9   )r.   r:   rG   rE   r6   s        r0   watch_tickerzbitopro.watch_ticker6  s       !!!!!!!!!V$$!$v-&&y+vd|LLLLLLLLLr1   c                    |                      |d          }|                     |                                d d          }|d         }|                      |d          }|dz   |z   }|                     ||          }|                      |d          |d<   |                     |d          }	|	|d<   |                     |	          |d<   || j        |<   |                    ||           d S )NrM   rN   r:   rO   rC   rP   r   )rU   rV   lowerparse_tickerrZ   r   r   r]   )
r.   rK   r^   r7   rE   r:   rO   r6   resultrP   s
             r0   handle_tickerzbitopro.handle_tickerF  s    & ##GV44!!(.."2"2D#>>!  '22ckF*""7F33++FH==x%%g{;;	'{!\\)44z%Vv{+++++r1   c                 |   | j         || j         v rd S |                                  |                                 }|                     || j        d          }|                     |          }|                     |                     |          |                     | j                  t          j
                  }dddi iii}|                     |           | j        d         d         d         }d| j        ||d}|| j        d         d         d<   |                     |           || j        d         d         d<   d S )N)nonceidentityr   r$   r%   ccxt)zX-BITOPRO-APIzX-BITOPRO-APIKEYzX-BITOPRO-PAYLOADzX-BITOPRO-SIGNATURE)clientsrt   millisecondsjsonr"   string_to_base64hmacencoder!   hashlibsha384extend_exchange_optionsr$   r    rK   )	r.   r8   r   rn   payload	signaturedefaultOptionsoriginalHeadersr%   s	            r0   ru   zbitopro.authenticateg  sQ   L$3$,+>+>F'')))!!##))

 
   ''00IIdkk'22DKK4L4Lgn]]	r 
 	$$^444,t,Y7	B# $!(#,	
 
 4;T9%i0C3BT9%i000r1   c                    K   |                                   |                                  d{V  d}| j        d         d         dz   dz   }|                     |           |                     ||d|           d{V S )a  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://github.com/bitoex/bitopro-offical-api-docs/blob/master/ws/private/user_balance_stream.md

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
        NACCOUNT_BALANCEr   r   r3   zaccount-balance)rt   rD   r)   ru   r4   )r.   rG   r6   r8   s       r0   watch_balancezbitopro.watch_balance  s       	'')))!!!!!!!!!'ioi(3.1BB#ZZ[$DDDDDDDDDr1   c                    |                      |d          }|                     |d          }|                     |d          }|                      |d          }t          |                                          }|||d}t          dt          |                    D ]}	|                      ||	          }
|                     ||
          }|                      |d          }|                     |          }|                                 }|                      |d          |d	<   |                      |d
          |d<   |||<   | 	                    |          | _
        |                    | j
        |           d S )NrO   ri   rP   r   )r   rP   r   r   r   	availablefreerT   total)rU   rW   rZ   listkeysrk   rl   rz   accountsafe_balancebalancer]   )r.   rK   r^   rO   ri   rP   r   
currenciesr   rp   r   r   
currencyIdcoder   s                  r0   handle_balancezbitopro.handle_balance  sm   "   '22w//%%g{;;	##GZ88$))++&&
" 
 

 q#j//** 	# 	#A''
A66HoodH55G))':>>J**:66DllnnG"..wDDGFO#//BBGG"F4LL((00t|U+++++r1   c                     | j         | j        | j        | j        | j        d}|                     |d          }|                     ||          }| |||           d S d S )N)TRADETICKER
ORDER_BOOKr   rs   rO   )rq   r   r`   r   r   rU   rW   )r.   rK   r^   methodsrO   methods         r0   handle_messagezbitopro.handle_message  s|    &(0#2.
 
   '22%00F67##### r1   )N)__name__
__module____qualname__r   r-   r9   strr   r	   rJ   r   r`   r   r   rg   rq   r
   rv   r   dictr   r|   r   r   r   ru   r   r   r   r   __classcell__)r/   s   @r0   r   r      s       ## # # # # # #JE E E @DB ! !S ! !S\ ! ! ! !4!/ !/ !/ !/ !/F <@d[] S S SS S Sbfglbm S S S S*"16 "1 "1 "1 "1H 37TX\eg S SC Ss SRU Slpqvlw S S S S2&;f &; &; &; &;PC CD C& CE C C C CJ 68 M M MF M M M M ,F , , , ,BC C C@ *, E E E E E E %,V %, %, %, %,N$V $ $ $ $ $ $ $ $r1   r   )ccxt.async_supportr    ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r   r	   r
   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   async_supportr    r1   r0   <module>r      s        O O O O O O O O  U U U U U U U U U U U U U U U U U U U U 4 4 4 4 4 4       * * * * * *z$ z$ z$ z$ z$d ( z$ z$ z$ z$ z$r1   