
    ":hb                         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 d dlmZ d dlmZ  G d	 d
ej.                  j0                        Zy)    N)
ArrayCacheArrayCacheBySymbolById)AnyIntOrder	OrderBookStrTickerTrade)Client)List)ExchangeError)AuthenticationError)Precisec            
       V    e Zd Zdef fdZi fdZ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
efdZdde	f fdZd
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i fdZd
efdZdi f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 Zd
efdZd
efdZ  xZ!S )	bitfinex1returnc                 |    | j                  t        t        |          ddddddddddddddiid	d
dddd      S )NTF)wswatchTickerwatchTickerswatchOrderBookwatchTradeswatchTradesForSymbolswatchBalance
watchOHLCVapir   zwss://api-pub.bitfinex.com/ws/1zwss://api.bitfinex.com/ws/1)publicprivateP0F0)precfreq  )r   ordersLimit)hasurlsoptions)deep_extendsuperr   describe)self	__class__s    L/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/bitfinex1.pyr+   zbitfinex1.describe   sy    i ? A# %"&#). %#	 "C#@ ! #  $'D
  	    c                   K   | j                          d {    | j                  |      }|d   }| j                  d   d   d   }|dz   |z   }d|||d}| j                  ||| j	                  ||      |       d {   S 7 j7 w)Nidr   r   r   :	subscribe)eventchannelsymbolmessageHash)load_marketsmarketr'   watchr)   )	r,   r5   r6   paramsr9   marketIdurlr7   requests	            r.   r3   zbitfinex1.subscribe0   s     !!!V$$<iit$X.mh. !&	
 ZZ[$2B2B7F2SU`aaa 	" bs"   BBA%B<B=BBNr6   sincelimitc                    K   | j                          d{    | j                  |      }| j                  d||       d{   }| j                  r|j	                  ||      }| j                  |||dd      S 7 c7 9w)a  
        get the list of most recent trades for a particular symbol

        https://docs.bitfinex.com/v1/reference/ws-public-trades

        :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trades	timestampT)r8   r6   r3   
newUpdatesgetLimitfilter_by_since_limit)r,   r6   r?   r@   r;   rB   s         r.   watch_tradeszbitfinex1.watch_trades?   sv      !!!V$~~h????OOFE2E))&%TRR 	"?s!   A>A:+A>A<8A><A>c                 D   K   | j                  d||       d{   S 7 w)a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://docs.bitfinex.com/v1/reference/ws-public-ticker

        :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>`
        tickerN)r3   )r,   r6   r;   s      r.   watch_tickerzbitfinex1.watch_tickerR   s!      ^^Hff====s     clientc                    | j                  |d      }| j                  |d      }|dz   |z   }| j                  | j                  dd      }| j	                  |      }|d   }	| j                  |d      }
| j                  | j
                  |	      }|t        |      }|| j
                  |	<   t        |
t              rA| j                  |
|      }t        dt        |            D ]  }|j                  ||           n;| j                  |d      }|d	k7  ry | j                  ||      }|j                  |       |j                  ||       y )
Nr5   pairr2   tradesLimitr$   r6      r   tu)
safe_valuesafe_stringsafe_integerr(   safe_marketrB   r   
isinstancelistparse_tradesrangelenappendparse_traderesolve)r,   rK   messagesubscriptionr5   r<   r7   rN   r9   r6   datastoredrB   isecondtrades                   r.   handle_tradeszbitfinex1.handle_trades^   s7   . //,	:##L&9mh.''mTJ!!(+!w*f5>,F"(DKKdD!&&tV4F1c&k* )fQi() %%gq1F~$$Wf5EMM% v{+r/   c                    t        |t              st        t        |   ||      S t        |      }| j                  |d      }d }|dk(  r| j                  ||dz
        }| j                  ||dz
        }| j                  ||dz
        }| j                  ||dz
        }d }	|/t        j                  |d      rdnd}	t        j                  |      }| j                  |d      }
|
j                  d	      }| j                  |d      }||j                  d
d      }| j                  ||      }d }d }| j                  ||| j                  |      |||d ||	||d d d      S )NrO   rP            0buysell-t )inforC   datetimer6   r1   ordertypetakerOrMakersidepriceamountcostfee)rU   rV   r*   r   r[   rY   rR   safe_timestampr   	string_gt
string_abssplitreplacesafe_symbol
safe_tradeiso8601)r,   rc   r9   tradeLengthr4   r1   rC   ru   rv   rt   seqpartsr<   r6   rs   orderIdr-   s                   r.   r[   zbitfinex1.parse_trade   s~   " %&D5eVDD%j  *D=!!%q9B''{Q?	  a8!!%q9#--fc:5D''/Fua(		###E1-''R0H!!(F3"Y/( 
  	r/   c                    | j                  |d      }| j                  |      }d}|dz   |z   }| j                  |d      }| j                  |d      }	d }
||	t        j                  ||	      }
| j	                  i d|dd dd d	| j                  |d
      d| j                  |d      d| j                  |d      dd d| j                  |d      dd dd d| j                  |
      d| j                  |      d| j                  |      dd d| j                  |	      d| j                  |d      dd | j                  |d      d |d      }|| j                  |<   |j                  ||       y )NrM   rI   r2         r6   rC   rp   high	   low
   bidrO   	bidVolumeaskrg   	askVolumevwapopencloselastpreviousClosechange
percentage   average   )
baseVolumequoteVolumero   )rR   r~   r   
string_subsafe_tickerparse_numbertickersr\   )r,   rK   r]   r^   r<   r6   r5   r7   r   r   r   results               r.   handle_tickerzbitfinex1.handle_ticker   s     ##L&9!!(+mh.+!!'1-6#5%%dF3D!! #
f#
#
 #
 D$$Wa0	#

 4##GR0#
 4##GQ/#
 #
 4##GQ/#
 #
 D#
 D%%d+#
 T&&t,#
 D%%d+#
 T#
 d''/#
  $**7A6!#
" t##
$ **7A6)#
 ,  &Vv{+r/   c                 ^  K   |"|dk7  r|dk7  rt        | j                  dz         | j                  | j                  di       }| j	                  |dd      }| j	                  |dd	      }|||d
}| j                  d|| j                  ||             d{   }|j                         S 7 w)a:  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://docs.bitfinex.com/v1/reference/ws-public-order-books

        :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   d   z6 watchOrderBook limit argument must be None, 25 or 100r   r"   r    r#   r!   )r"   r#   rY   book)r   r1   rQ   r(   rR   r3   r)   r@   )	r,   r6   r@   r;   r(   r"   r#   r>   	orderbooks	            r.   watch_order_bookzbitfinex1.watch_order_book   s      %3,#DGG.f$fgg//$,,0@"E66
 
 ..9I9I'SY9Z[[	   \s   BB-B+B-c                    | j                  |d      }| j                  |      }d}|dz   |z   }| j                  |dd      }|dk(  }	t        |d   t              rL| j	                  |d      }
|	r | j                  i |
      | j                  |<   n| j                  i |
      | j                  |<   | j                  |   }|	rz|d   }t        d	t        |            D ]\  }||   }| j                  |d	      }| j                  |d      }|d
   }|d	k  r| n|}|d	k  rdnd}||   }|j                  |||g       ^ n[|d   }t        d	t        |            D ]>  }||   }|d
   }|d	k  r| n|}|d	k  rdnd}||   }|j                  |d	   ||d   g       @ |j                  ||       y | j                  |   }|	r| j                  |d      }| j                  |d
      }|d   }|d	k  r| n|}|d	k  rdnd}||   }t        j                  |d      r|nd}|j                  | j                  |      | j                  |      |g       n7|d   }|d	k  r| n|}|d	k  rdnd}||   }|j                  |d   ||d
   g       |j                  ||       y )NrM   r   r2   r"   r    R0rO   rY   r   rh   asksbidsrg   ri   )rR   r~   rU   rV   rS   indexed_order_book
orderbookscounted_order_bookrX   rY   
safe_float
storeArrayr\   r   rz   r   )r,   rK   r]   r^   r<   r6   r5   r7   r"   isRawr@   r   deltasra   deltar1   ru   delta2Valuesizert   booksidedelta2countedBookSidemessage3rv   message3Values                             r.   handle_order_bookzbitfinex1.handle_order_book  s   2 ##L&9!!(+mh.fd;gaj$'%%lE:E*.*A*A"e*L' +/*A*A"e*L'/I q#f+. ;A"1IE))%3B OOE15E"'(K,7!OK<+D&1Ao6FD(H''b(9:; !q#f+. KA"1IE"1XF'-zF7D&,qj6vD&/oO#..a$a/IJK NN9k2/I%%gq1((!4"1:%-\y"*Q,vV$T?!(!2!25#!>C##T%6%6u%=t?P?PQW?XZ\$]^ '
*7!*;~-"/!"3v&"+D/**GAJgaj+IJNN9k2r/   c                 L    | j                  |d      }|j                  ||       y )Nr4   )rR   r\   )r,   rK   r]   r4   s       r.   handle_heartbeatzbitfinex1.handle_heartbeatc  s$       '2w&r/   c                     |S N )r,   rK   r]   s      r.   handle_system_statuszbitfinex1.handle_system_statusl  s	     r/   c                 H    | j                  |d      }||j                  |<   |S )NchanId)rR   subscriptions)r,   rK   r]   	channelIds       r.   handle_subscription_statusz$bitfinex1.handle_subscription_status{  s+     $$Wh7	*1Y'r/   c                   K   | j                   d   d   d   }| j                  |      }|j                  d      }d}| j                  |j                  |      }|| j                         }dt        |      z   }| j                  | j                  |      | j                  | j                        t        j                  d      }	| j                  |	|||dd	gd
}
| j                  | j                  |||
d       | d {   S 7 w)Nr   r   r   authenticatedauthAUTHhextradingwallet)apiKeyauthSig	authNonceauthPayloadr4   filterrO   )r'   rK   futurerQ   r   millisecondsstrhmacencodesecrethashlibsha384r   spawnr:   )r,   r;   r=   rK   r   methodr   noncepayload	signaturer>   s              r.   authenticatezbitfinex1.authenticate  s     iit$Y/S!/(<(<fE %%'Es5z)G		$++g"6DKK8PRYR`R`bghI++$"&
G JJtzz3;||s   C9D;D <Dc                 B   | j                  |d      }|dk(  r.| j                  |j                  d      }|j                  d       y t	        | j                  |            }|j                  |d       | j                  |d      }||j                  v r|j                  |= y y )NstatusOKr   Tr4   )rR   rQ   futuresr\   r   jsonrejectr   )r,   rK   r]   r   r   errorr   s          r.   handle_authentication_messagez'bitfinex1.handle_authentication_message  s    !!'84T>__V^^_EFNN4 '		'(:;EMM%1%%gw7F---((0 .r/   c                    K   | j                          d {    | j                  d   d   d   }| j                          d {    | j                  ||d d       d {   S 7 M7 "7 w)Nr   r   r   rO   )r8   r'   r   r:   )r,   r1   r6   r;   r=   s        r.   watch_orderzbitfinex1.watch_order  sg     !!!iit$Y/!!!ZZRq111 	"!1s3   A*A$,A*A&A*A( A*&A*(A*c                 d  K   | j                          d{    | j                          d{    || j                  |      }| j                  d   d   d   }| j	                  |ddd       d{   }| j
                  r|j                  ||      }| j                  ||||d      S 7 7 }7 ;w)az  
        watches information on multiple orders made by the user

        https://docs.bitfinex.com/v1/reference/ws-auth-order-updates
        https://docs.bitfinex.com/v1/reference/ws-auth-order-snapshots

        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr   r   r   osrO   T)r8   r   r6   r'   r:   rD   rE   filter_by_symbol_since_limit)r,   r6   r?   r@   r;   r=   orderss          r.   watch_orderszbitfinex1.watch_orders  s      !!!!!![[(Fiit$Y/zz#tT155??OOFE2E00tTT 	"! 6s2   B0B*B0B,AB02B.38B0,B0.B0c                 2   | j                  |dg       }| j                  |d      }|dk(  r2t        dt        |            D ]  }||   }| j	                  ||        n| j	                  ||       | j
                  |j                  | j
                  d       y y )Nrh   rO   r   r   )rQ   rR   rX   rY   handle_orderr   r\   )r,   rK   r]   r^   r_   messageTypera   values           r.   handle_orderszbitfinex1.handle_orders  s    \ w2.&&w2$1c$i( 1Q!!&%01 fd+;;"NN4;;- #r/   c                 2    ddd}| j                  |||      S )Nr   canceled)ACTIVECANCELED)rR   )r,   r   statusess      r.   parse_ws_order_statuszbitfinex1.parse_ws_order_status  s&    "
 &&99r/   c                    | j                  |d      }| j                  |d      }| j                  |      }| j                  |d      }| j                  |d      }d}t        j                  |d      r,t        j                  |      }t        j                  |      }d}| j                  |d      }	|	j                  d	      d
kD  rd}	n|	j                  d      d
kD  rd}	| j                  | j                  |d            }
| j                  |d      }| j                  |d      }| j                  |      }| j                  i d|d|dd d|d| j                  |      dd d|d|	d|d|dd dd dd d|d|d d d!|
d d d d"      }| j                  -| j                  | j                  d#d$      }t        |      | _
        | j                  }|j                  |       |j                  ||       |S )%Nr   rO   rh   rg   rj   ri   rk   rf   LIMITr@   MARKETr9   r   r   r   ro   r1   clientOrderIdrC   rp   lastTradeTimestampr6   rr   rt   ru   	stopPricetriggerPricer   rv   	remainingfilledr   )rx   rw   rB   r%   r$   )rR   r~   r   	string_ltr{   findr   	parse8601
safe_orderr   r   rS   r(   r   rZ   r\   )r,   rK   rq   r1   r<   r6   rv   r  rt   rr   r   ru   rawDatetimerC   parsedr@   r   s                    r.   r   zbitfinex1.handle_order  so    eQ'##E1-!!(+!!%+$$UA.	VS)''/F**95IDq)99W"DYYx 2%D++D,<,<UA,FG  *&&ua0NN;/	 "
E"
""
 T"
 	"

 Y/"
 !$"
 f"
 D"
 D"
 U"
 "
 D"
 t"
 f"
 "
  d!"
" f#"
$ )"
 , ;;%%dllM4HE07DKfvr"r/   c                 V   t        |t              r| j                  |d      }|d   dk(  ry | j                  |j                  |i       }| j                  |d      }| j                  |d      }| j
                  | j                  | j                  | j                  | j                  | j                  d}| j                  |||      }| ||||       y y | j                  |d      }	|	C| j                  | j                  | j                  d}| j                  ||	      }|
 |||       y y y )Nr   rO   hbr5   )r   rI   rB   r   onocr4   )ro   
subscribedr   )rU   rV   rR   rQ   r   r   r   rd   r   safe_value_2r   r   r   )
r,   rK   r]   r   r^   r5   namemethodsr   r4   s
             r.   handle_messagezbitfinex1.handle_messageM  s=   gt$((!4I qzT!??6+?+?BOL&&|Y?G##GQ/D..,,,,((((((G &&w>F!vw5 " $$Wg6E  55"&"A"A >>	! %8%67+ & !r/   r   )"__name__
__module____qualname__r   r+   r3   r   r   r   r   rG   r
   rJ   r   rd   r[   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r  __classcell__)r-   s   @r.   r   r      sv   # : 79 b <@d[] S SS S Sbfglbm S& 68 
> 
>F 
>,,F ,,\5 5n0,F 0,d @DB !S ! !S\ !8P3 P3d'v '6  " )+ 01F 1 37r 2C 2 04$UYbd U UC Us Uimnsit U.7.F 7.r:<6 <|.,V .,r/   r   )ccxt.async_supportccxt 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   r   ccxt.base.preciser   async_supportr   r   r/   r.   <module>r!     sB     O  J J J 4  * 0 %j	,"",, j	,r/   