
    ":hJ                         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y)	    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                     | j                  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    J/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/bitopro.pyr-   zbitopro.describe   s    gt = ? $!%#"&$# %#). FL $  $#!2 	1"B
 " "	    c                    K   | j                   d   d   dz   |z   dz   |z   }| j                  ||d |       d {   S 7 w)Nr   r   /)r)   watch)r.   pathmessageHashmarketIdurls        r0   watch_publiczbitopro.watch_public6   sG     iioh'#-4s:XEZZ[$DDDDs   6?=?Nsymbollimitc                   K   |;|dk7  r6|dk7  r1|dk7  r,|dk7  r'|dk7  r"|dk7  r|dk7  rt        | j                  d	z         | j                          d{    | j                  |      }|d
   }d|z   }d}||d   }n|d   dz   | j	                  |      z   }| j                  d||       d{   }|j                         S 7 n7 w)a[  
        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:   s      
"5B;Ub[W\`cWcjosvjv  ~C  GK  ~K#DGG.|$|}}!!!V$!(61=TlGTlS(4+@+@+GGG++M;PP	   	" Qs%   ACCAC-C.CCclientc           	         | j                  |d      }| j                  |d d      }|d   }| j                  |d      }|dz   |z   }| j                  | j                  |      }|| j	                  i       }| j                  |d      }	| j                  |||	ddd	d
      }
|j                  |
       |j                  ||       y )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!!(D#6!  '2ckF*OODOOV<	+I%%g{;	((&)VVU\^fg!y+.r1   sincec                   K   | j                          d{    | j                  |      }|d   }d|z   }| j                  d||d          d{   }| j                  r|j	                  ||      }| j                  |||dd      S 7 p7 9w)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??OOFE2E))&%TRR 	" Ns!   BB8BB	8B	Bc                     | j                  |d      }| j                  |d d      }|d   }| j                  |d      }|dz   |z   }| j                  |dg       }| j                  ||      }	| j                  | j                  |      }
|
(| j                  | j                  dd      }t        |      }
t        d	t        |	            D ]  }|
j                  |	|           |
| j                  |<   |j                  |
|       y )
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!!(D#6!  '2ckF*//'626""7F3oodkk6:%%dllM4HE$U+Kq#f+& 	*Avay)	*)F{K0r1   c                   K   | j                          | j                          d{    d}|| j                  |      }|dz   |d   z   }| j                  d   d   dz   dz   }| j	                  |       | j                  ||d|       d{   }| j                  r|j                  ||      }| j                  |||d	d
      S 7 7 9w)aR  
        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iioi(3.>#zz#{D+FF??OOFE2E))&%TRR 	" Gs"   $CCA$CC8CCc                 *   | j                  |di       }| j                  |d      }| j                  |d      }| j                  |      }| j                  |      }| j                  |dz   |z         }| j                  |d      }	| j                  -| j                  | j                  dd      }
t        |
      | _        | j                  }| j                  |      }|j                  |       |j                  ||	       |j                  ||	dz   |z          y )	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!!$/""41&&v.''0TCZ%/0&&w8== %%dllM4HE259DM$$T*fv{+v{S069:r1   traderE   c                    | j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |      }| j                  |      }	| j                  |dz   |	z         }
| j	                  |
|      }| j                  |d      }| j                  |d      }| j                  |d	      }||d
k(  rd}n|dk(  rd}| j                  |d      }d }| j                  |d      }| j                  | j                  |d            }|||d d}| j                  |d      }d }||rd}nd}| j                  ||||| j                  |      |
|||||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''/EF	!!%0""5'2&&v.''0TCZ%/0!!&&1  0%%e[9v.u}!!%2$$UE2	++D,<,<UM,RS	 !%C
 //%3&&"Y/( 
  	r1   c                    K   | j                          d{    | j                  |      }|d   }d|z   }| j                  d||d          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/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  sd      !!!V$!$v-&&y+vd|LLL	 	" Ms!   AA8AAAAc                    | j                  |d      }| j                  |j                         d d      }|d   }| j                  |d      }|dz   |z   }| j                  ||      }| j                  |d      |d<   | j	                  |d      }	|	|d<   | j                  |	      |d<   || j                  |<   |j                  ||       y )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!!(.."2D#>!  '2ckF*""7F3++FH=x%%g{;	'{!\\)4z%Vv{+r1   c                 ^   | j                   || j                   v ry | j                          | j                         }| j                  || j                  d      }| j                  |      }| j                  | j                  |      | j                  | j                        t        j                        }dddi iii}| j                  |       | j                  d   d   d   }d| j                  ||d}|| j                  d   d   d<   | j                  |       || j                  d   d   d<   y )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  s(   LL$3$,,+>'')!!#))


  ''0IIdkk'2DKK4Lgnn]	r 
 	$$^4,,t,Y7	B# $!(#,	
 4;T9%i0C3BT9%i0r1   c                    K   | j                          | j                          d{    d}| j                  d   d   dz   dz   }| j                  |       | j	                  ||d|       d{   S 7 K7 w)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  sz      	'')!!!'iioi(3.1BB#ZZ[$DDD	 	" Es"   $A6A2AA6-A4.A64A6c                 v   | j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }t        |j	                               }|||d}t        dt        |            D ]  }	| j                  ||	      }
| j                  ||
      }| j                  |d      }| j                  |      }| j                         }| j                  |d      |d	<   | j                  |d
      |d<   |||<    | j                  |      | _
        |j                  | j                  |       y )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  s1   "   '2w/%%g{;	##GZ8$))+&
" 

 q#j/* 	#A''
A6HoodH5G))':>J**:6DllnG"..wDGFO#//BGG"F4L	# ((0t||U+r1   c                     | j                   | j                  | j                  | j                  | j                  d}| j                  |d      }| j                  ||      }|
 |||       y y )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  so    &&((00#22..
   '2%067# 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      sN   ## #JE @DB !S ! !S\ !4!/ !/F <@d[] S SS S Sbfglbm S*"16 "1H 37TX\eg SC Ss SRU Slpqvlw S2&;f &;PCD C& CE CJ 68 M MF M ,F ,BC@ *, 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  U U U 4  *z$d  (( z$r1   