
    i$I                         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 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)BalancesIntMarket	OrderBookStrTickerTrade)Client)List)ExchangeErrorc            
           e Zd Z 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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 )bitoproc                     |                      t          t          |                                           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watchTradesr   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    V/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/bitopro.pyr*   zbitopro.describe   s    gt 4 4 = = ? ? $!%#"&$# %#
 
 FL  $ $  $#!2 	 	/!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        r-   watch_publiczbitopro.watch_public5   sT      ioh'#-4s:XEZZ[$DDDDDDDDDr.   Nsymbollimitreturnc                   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 )a_  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :see: 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 1000r7   zORDER_BOOK:id:zorder-books)r   r@   load_marketsmarketr6   r8   )r+   r7   r8   paramsrC   r3   endPart	orderbooks           r-   watch_order_bookzbitopro.watch_order_book9   s#      

"5B;;Ub[[W\`cWcWcjosvjvjv  ~C  GK  ~K  ~K#DG.|$|}}}!!!!!!!!!V$$!(61=TlGGTlS(50G++M;PPPPPPPP	   r.   clientc           	         |                      |d          }|                     |d d          }|d         }|                      |d          }|dz   |z   }|                     | j        |          }||                     i           }|                     |d          }	|                     |||	ddd	d
          }
|                    |
           |                    ||           d S )Npair_r7   eventrA   	timestampbidsaskspriceamount)	safe_stringsafe_market
safe_value
orderbooks
order_booksafe_integerparse_order_bookresetresolve)r+   rH   messager4   rC   r7   rL   r3   rF   rM   snapshots              r-   handle_order_bookzbitopro.handle_order_bookQ   s    , ##GV44!!(D#66!  '22ckF*OODOV<<	++I%%g{;;	((&)VVU\^fgg!!!y+.....r.   sincec                 0  K   |                                   d{V  |                     |          }|d         }d|z   }|                     d||d                    d{V }| j        r|                    ||          }|                     |||dd          S )aC  
        get the list of most recent trades for a particular symbol
        :see: 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>`
        Nr7   zTRADE:tradesr@   rM   T)rB   rC   r6   
newUpdatesgetLimitfilter_by_since_limit)r+   r7   r^   r8   rD   rC   r3   r`   s           r-   watch_tradeszbitopro.watch_tradest   s       !!!!!!!!!V$$!#f,((;tMMMMMMMM? 	3OOFE22E))&%TRRRr.   c                 L   |                      |d          }|                     |d d          }|d         }|                      |d          }|dz   |z   }|                     |dg           }|                     ||          }	|                     | j        |          }
|
+|                     | j        dd          }t          |          }
t          d	t          |	                    D ]}|

                    |	|                    |
| j        |<   |                    |
|           d S )
NrJ   rK   r7   rL   rA   datar#   r    r   )rR   rS   rT   parse_tradesr`   rW   r!   r   rangelenappendrZ   )r+   rH   r[   r4   rC   r7   rL   r3   rawDatar`   tradesCacher8   is                r-   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0r.   c                   K   |                                   |                                  d{V  d}|#|                     |          }|dz   |d         z   }| j        d         d         dz   dz   }|                     |           |                     ||d|           d{V }| j        r|                    ||          }|                     |||d	d
          S )aZ  
        watches information on multiple trades made by the user
        :see: 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]{@link https://docs.ccxt.com/#/?id=trade-structure
        N
USER_TRADErA   r7   r   r   r0   zuser-tradesrM   T)	check_required_credentialsrB   rC   r&   authenticater1   ra   rb   rc   )	r+   r7   r^   r8   rD   r3   rC   r5   r`   s	            r-   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Rr.   c                 v   |                      |di           }|                     |d          }|                     |d          }|                     |          }|                     |          }|                     |dz   |z             }|                     |d          }	| j        0|                     | j        dd          }
t          |
          | _        | j        }|                     |          }|	                    |           |
                    ||	           |
                    ||	dz   |z              d S )	Nrf   basequoter0   rL   r#   r    rA   )rT   rR   safe_currency_coder7   myTradesrW   r!   r   parse_ws_traderj   rZ   )r+   rH   r[   rf   baseIdquoteIdru   rv   r7   r3   r8   r`   parseds                r-   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:::::r.   rC   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transactionTimestampru   rv   r0   rP   	orderTypesideasksellbidbuyvolumefeefeeCurrency)costcurrencyrateisMakermakertaker)r@   infoorderrM   datetimer7   takerOrMakertyper   rP   rQ   r   r   )	rR   safe_timestamprw   r7   rS   safe_string_lowerrT   
safe_tradeiso8601)r+   traderC   r@   orderIdrM   rz   r{   ru   rv   r7   rP   r   r   rQ   r   	feeAmount	feeSymbolr   r   s                       r-   ry   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//( 
  
   	r.   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
        :see: 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>`
        Nr7   zTICKER:tickersr@   )rB   rC   r6   )r+   r7   rD   rC   r3   s        r-   watch_tickerzbitopro.watch_ticker/  s       !!!!!!!!!V$$!$v-&&y+vd|LLLLLLLLLr.   c                    |                      |d          }|                     |d d          }|d         }|                      |d          }|dz   |z   }|                     |          }|                     |d          }	|                      |d          }
|	|d<   |
|d<   || j        |<   |                    ||           d S )NrJ   rK   r7   rL   rA   rM   r   )rR   rS   parse_tickerrW   r   rZ   )r+   rH   r[   r4   rC   r7   rL   r3   resultrM   r   s              r-   handle_tickerzbitopro.handle_ticker=  s    & ##GV44!!(D#66!  '22ckF*""7++%%g{;;	##GZ88'{%z%Vv{+++++r.   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)clientsrq   millisecondsjsonr   string_to_base64hmacencoder   hashlibsha384extend_exchange_optionsr!   r   rH   )	r+   r5   r   rk   payload	signaturedefaultOptionsoriginalHeadersr"   s	            r-   rr   zbitopro.authenticate]  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0r.   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
        :see: 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   r0   zaccount-balance)rq   rB   r&   rr   r1   )r+   rD   r3   r5   s       r-   watch_balancezbitopro.watch_balance}  s       	'')))!!!!!!!!!'ioi(3.1BB#ZZ[$DDDDDDDDDr.   c                    |                      |d          }|                     |d          }|                     |d          }|                      |d          }t          |                                          }|||d}t          dt          |                    D ]}	|                      ||	          }
|                     ||
          }|                      |d          }|                     |          }|                                 }|                      |d          |d	<   |                      |d
          |d<   |||<   | 	                    |          | _
        |                    | j
        |           d S )NrL   rf   rM   r   )r   rM   r   r   r   	availablefreerQ   total)rR   rT   rW   listkeysrh   ri   rw   accountsafe_balancebalancerZ   )r+   rH   r[   rL   rf   rM   r   
currenciesr   rm   r   r   
currencyIdcoder   s                  r-   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+++++r.   c                     | j         | j        | j        | j        | j        d}|                     |d          }|                     ||          }| |||           d S d S )N)TRADETICKER
ORDER_BOOKr   rp   rL   )rn   r   r]   r   r}   rR   rT   )r+   rH   r[   methodsrL   methods         r-   handle_messagezbitopro.handle_message  s|    &(0#2.
 
   '22%00F67##### r.   )N)__name__
__module____qualname__r*   r6   strr   r   rG   r   r]   r   r   rd   rn   r	   rs   r}   r   ry   r
   r   r   rr   r   r   r   r   __classcell__)r,   s   @r-   r   r      sm       " " " " "HE E E @DB ! !S ! !S\ ! ! ! !0!/ !/ !/ !/ !/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.&;f &; &; &; &;PC CF Ce C C C CJ 68 M M MF M M M M,F , , , ,@C C C@ *, E E E E E E%,V %, %, %, %,N$V $ $ $ $ $ $ $ $r.   r   )ccxt.async_supportr    ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r   r	   r
   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   async_supportr    r.   r-   <module>r      s        O O O O O O O O  P P P P P P P P P P P P P P P P P P 4 4 4 4 4 4       * * * * * *n$ n$ n$ n$ n$d ( n$ n$ n$ n$ n$r.   