
    ":ho                         d dl Zd dlmZm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*                  j,                        Zy)    N)
ArrayCacheArrayCacheBySymbolById)AnyIntOrder	OrderBookStrTickerTrade)Client)List)ExchangeError)AuthenticationError)UnsubscribeErrorc            
           e Zd Zdef fdZd Zd Zdi fdedede	fdZ
d	efd
Zi fdedefdZd	efdZi fdedefdZi fdedefdZd Zd	efdZ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i fdZ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di fdedededee   fdZ d	efdZ!d	efdZ"d	efdZ#d	efdZ$ xZ%S )derivereturnc                     | j                  t        t        |          dddddddddddddddiddidddi d	d
diddi iid      S )NFT)wswatchBalancewatchMyTrades
watchOHLCVwatchOrderBookwatchOrderswatchTickerwatchTickerswatchBidsAskswatchTradeswatchTradesForSymbolswatchPositionsr   zwss://api.lyra.finance/wszwss://api-demo.lyra.finance/ws)apitest  )tradesLimitordersLimit	requestId	keepAlivei(#  exact)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describe)self	__class__s    I/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/derive.pyr0   zderive.describe   s    fd < > %!%#"&## %!&#)."'  5 :	  $# T R?$A
 $ $	    c                     | j                  | j                  di       }| j                  ||d      }| j                  |d      }|| j                  d   |<   |S )Nr&   r      )
safe_valuer+   safe_integersum)r1   urlr+   previousValuenewValues        r3   
request_idzderive.request_id9   sT    //$,,R@))'3:88M1-)1[!#&r4   c                    K   | j                   d   d   }| j                  |      }| j                  |d|i      }| j                  ||dd      }| j                  |||||       d {   S 7 wNr!   r   id	subscriber@   methodr*   r=   extendwatchr1   messageHashmessagesubscriptionr:   r&   requests          r3   watch_publiczderive.watch_public@   s}     iit$OOC(	++g)(
  {{<!2
  ZZ[';UUUU   A%A.'A,(A.Nsymbollimitc                   K   | j                          d{    |d}| j                  |      }d|d   z   dz   | j                  |      z   }dd|gid}||||d	}| j                  |||       d{   }|j	                         S 7 p7 w)
aF  

        https://docs.derive.xyz/reference/orderbook-instrument_name-group-depth

        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :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
   
orderbook.r@   .10.rA   channelsrC   params)namerN   rO   rV   )load_marketsmarketnumber_to_stringrL   rO   )	r1   rN   rO   rV   rY   topicrK   rJ   	orderbooks	            r3   watch_order_bookzderive.watch_order_bookL   s      !!!=EV$vd|+f4t7L7LU7SS!
 	
 ++E7LII	  ) 	"& Js"   BBAB2B	3B	Bclientc                 J   | j                  |d      }| j                  |d      }| j                  |d      }| j                  |      }|d   }| j                  |d      }|| j                  vr^| j	                  | j
                  dd      }	|j                  |   }
| j	                  |
d|	      }| j                  i |      | j                  |<   | j                  |   }| j	                  |d	      }| j                  |||d
d      }|j                  |       |j                  ||       y )NrV   datainstrument_namerN   channelwatchOrderBookLimitr#   rO   	timestampbidsasks)	safe_dictsafe_stringsafe_market
orderbooksr8   r+   subscriptions
order_bookparse_order_bookresetresolve)r1   r^   rI   rV   r`   marketIdrY   rN   r[   defaultLimitrJ   rO   r\   rd   snapshots                  r3   handle_order_bookzderive.handle_order_bookm   s     2~~ff-##D*;<!!(+!  3$//),,T\\;PRVWL!//6L%%lG\JE&*oob%&@DOOF#OOF+	%%dK8	((vy&&Q!y%(r4   c                    K   | j                          d{    | j                  |      }d|d   z   dz   }dd|gid}|||d}| j                  |||       d{   S 7 I7 w)	a  

        https://docs.derive.xyz/reference/ticker-instrument_name-interval

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :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>`
        Nzticker.r@   z.100rA   rT   rU   rW   rN   rV   )rX   rY   rL   )r1   rN   rV   rY   r[   rK   rJ   s          r3   watch_tickerzderive.watch_ticker   s      !!!V$F4L(61!
 

 &&ug|DDD! 	"  E"   A$A AA$A"A$"A$c                     | j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }| j                  |      }|| j                  |d   <   |j	                  ||       |S )NrV   r`   instrument_tickerrb   rN   )rg   r7   parse_tickertickersro   )r1   r^   rI   rV   rawDatar`   r[   tickers           r3   handle_tickerzderive.handle_ticker   s|    B 2..0~~g':;	2""4()/VH%&vu%r4   c                 "  K   | j                          d{    | j                  |d      }|d}| j                  |      }d|d   z   dz   | j                  |      z   }d|z   }dd	|gid
}d|i}| j	                  |||       d{   S 7 t7 w)a  
        unsubscribe from the orderbook channel
        :param str symbol: unified symbol of the market to fetch the order book for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param int [params.limit]: orderbook limit, default is None
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        NrO   rQ   rR   r@   rS   unwatchunsubscriberT   rU   rW   )rX   r8   rY   rZ   un_watch_public)	r1   rN   rV   rO   rY   r[   rH   rK   rJ   s	            r3   un_watch_order_bookzderive.un_watch_order_book   s      !!!!!&'2=EV$vd|+f4t7L7LU7SS%'#
 E
 ))+wMMM% 	"$ Ns"   BBA/BBBBc                    K   | j                          d{    | j                  |      }d|d   z   }d|z   }dd|gid}d|i}| j                  |||       d{   S 7 I7 w)	a  
        unsubscribe from the trades channel
        :param str symbol: unified symbol of the market to unwatch the trades for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns any: status of the unwatch request
        Ntrades.r@   r   r   rT   rU   rW   )rX   rY   r   )r1   rN   rV   rY   r[   
messageHahrK   rJ   s           r3   un_watch_tradeszderive.un_watch_trades  s      !!!V$F4L(&
#
 E
 ))*g|LLL 	" Mrw   c                    K   | j                   d   d   }| j                  |      }| j                  |d|i      }| j                  ||dd      }| j                  |||||       d {   S 7 w)Nr!   r   r@   r   rB   rD   rG   s          r3   r   zderive.un_watch_public(  s}     iit$OOC(	++g)(
  {{<#2
  ZZ[';UUUUrM   c                 f   |j                  d      }| j                  |d      }| j                  |      }|d   }|| j                  v r| j                  |= ||j                  v r|j                  |= t        | j                  dz   |z         }|j                  ||       |j                  |d|z          y )N.r6   rN   z orderbook r   )	splitrh   ri   rj   rk   r   r@   rejectro   r1   r^   r[   parsedTopicrp   rY   rN   errors           r3   !handle_order_book_un_subscriptionz(derive.handle_order_book_un_subscription4  s    kk#&##K3!!(+!T__$'F((($$U+ =!86!ABeU#ui%/0r4   c                 f   |j                  d      }| j                  |d      }| j                  |      }|d   }|| j                  v r| j                  |= ||j
                  v r|j
                  |= t        | j                  dz   |z         }|j                  ||       |j                  |d|z          y )Nr   r6   rN   z trades r   )
r   rh   ri   rj   tradesrk   r   r@   r   ro   r   s           r3   handle_trades_un_subscriptionz$derive.handle_trades_un_subscriptionA  s    kk#&##K3!!(+!T__$F#F((($$U+ :!5!>?eU#ui%/0r4   c                 ^   | j                  |d      }| j                  |d      }|t        |j                               }t        dt	        |            D ]U  }||   }|j                  d      dk\  r| j                  ||       /|j                  d      dk\  sD| j                  ||       W |S )Nresultstatusr   r\   r   )rg   listkeysrangelenfindr   r   )r1   r^   rI   r   r   topicsir[   s           r3   handle_un_subscribezderive.handle_un_subscribeN  s     21&++-(F1c&k* Fq	::k*a/::65IZZ)Q.66vuEF r4   sincec                 2  K   | j                          d{    | j                  |      }d|d   z   }dd|gid}|||d}| j                  |||       d{   }	| j                  r|	j	                  |d   |      }| j                  |	|||d	      S 7 |7 <w)
a/  
        watches information on multiple trades made in a market

        https://docs.derive.xyz/reference/trades-instrument_name

        :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>`
        Nr   r@   rA   rT   rU   ru   rN   T)rX   rY   rL   
newUpdatesgetLimitfilter_by_symbol_since_limit)
r1   rN   r   rO   rV   rY   r[   rK   rJ   r   s
             r3   watch_tradeszderive.watch_tradesd  s      !!!V$F4L(!
 

 ((FF??OOF8$4e<E00tTT' 	"  Gs"   BBABB;BBc                 .   | j                  |d      }| j                  |d      }| j                  |d      }|j                  d      }| j                  |d      }| j	                  |      }|d   }	| j                  | j
                  |	      }
|
(| j                  | j                  dd      }t        |      }
t        d	t        |            D ]'  }| j                  ||         }|
j                  |       ) |
| j
                  |	<   |j                  |
|       y )
NrV   r`   rb   r   r6   rN   r$   r#   r   )rg   r7   r   rh   ri   r   r8   r+   r   r   r   parse_tradeappendro   )r1   r^   rI   rV   r`   r[   r   rp   rY   rN   tradesArrayrO   r   trades                 r3   handle_tradezderive.handle_trade  s    2~~ff-	2kk#&##K3!!(+!oodkk6:%%dllM4HE$U+Kq#d)$ 	&A$$T!W-Eu%	& *F{E*r4   c                   K   | j                          | j                  d   d   }| j                  |      }d}|j                  |      }| j	                  |j
                  |      }|| j                  |      }t        | j                               }| j                  || j                        }	| j                  | j                  d      }
|d|
||	dd}| j                  ||      }| j                  |||||       | d {   S 7 w)Nr!   r   authenticatedderiveWalletAddresspublic/login)walletrd   	signature)r@   rC   rV   )check_required_credentialsr*   r^   futurer7   rk   r=   strmillisecondssignMessage
privateKeyrh   r+   rE   rF   )r1   rV   r:   r^   rH   r   r   r&   nowr   r   rK   rI   s                r3   authenticatezderive.authenticate  s     '')iit$S!%{+(<(<kJ ,Id'')*C((doo>I"&"2"24<<AV"W(1!$!*G kk'62GJJsK+wG||s   C=D?D Dc                   K   | j                          d {    | j                  d   d   }| j                  |      }| j                  |d|i      }| j                  ||dd      }| j	                  |||||       d {   S 7 m7 wr?   )r   r*   r=   rE   rF   rG   s          r3   watch_privatezderive.watch_private  s     !!!iit$OOC(	++g)(
  {{<!2
  ZZ[';UUU 	" Vs"   BBA(B?B BBc                   K   | j                          d{    d}| j                  d|      \  }}| j                  |      dz   }|}|| j                  |      }|d   }|d|z   z  }dd|gid}	||d	}
| j	                  |	|      }| j                  |||
       d{   }| j                  r|j                  ||      }| j                  ||||d
      S 7 7 9w)au  

        https://docs.derive.xyz/reference/subaccount_id-orders

        watches information on multiple orders made by the user
        :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
        :param str [params.subaccount_id]: *required* the subaccount id
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr   z.ordersrN   :rA   rT   rU   rW   rV   T	rX   handleDeriveSubaccountIdrZ   rY   rE   r   r   r   r   )r1   rN   r   rO   rV   subaccountIdr[   rH   rY   rK   rJ   rI   orderss                r3   watch_orderszderive.watch_orders  s
     !!!#<<]FSf%%l3i?[[(FH%F3<'K!
 
 ++gv.))+wMM??OOFE2E00tTT3 	", N"   CCBCC8CCc                    | j                  |d      }| j                  |d      }| j                  |d      }t        dt	        |            D ]\  }||   }| j                  |      }| j                  |d      }	| j                  |d      }
|	A| j                  -| j                  | j                  dd      }t        |      | _        | j                  }| j                  |j                  |	i       }| j                  ||
      }|q| j                  |d	      }|||d	<   | j                  |d
      }|||d
<   | j                  |d      |d<   | j                  |d      |d<   | j                  |d      |d<   |j                  |       |dz   |	z   }|j                  | j                  |       _ |j                  | j                  |       y )NrV   rb   r`   r   rN   r@   r%   r#   feefeesr   rd   datetimer   )rg   rh   	safe_listr   r   parse_orderr   r8   r+   r   r7   hashmapr   ro   )r1   r^   rI   rV   r[   	rawOrdersr   r`   parsedrN   orderIdrO   cachedOrdersr   orderr   r   messageHashSymbols                     r3   handle_orderzderive.handle_order  s   T 2  3NN662	q#i.) 	?AQ<D%%d+F%%fh7F&&vt4G!;;& --dllM4PE"8"?DK#{{)=)=vrJ8$//%7C(+u??5&9D')-v'+uh'GF8$*.*;*;E;*OF;')-)9)9%)LF:&##F+$)CK&$8!t{{,=>1	?2 	t{{E*r4   c                   K   | j                          d{    d}| j                  d|      \  }}| j                  |      dz   }|}|| j                  |      }|d   }|d|z   z  }dd|gid}	||d	}
| j	                  |	|      }| j                  |||
       d{   }| j                  r|j                  ||      }| j                  ||||d
      S 7 7 9w)au  

        https://docs.derive.xyz/reference/subaccount_id-trades

        watches information on multiple trades made by the user
        :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
        :param str [params.subaccount_id]: *required* the subaccount id
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
        Nr   z.tradesrN   r   rA   rT   rU   r   Tr   )r1   rN   r   rO   rV   r   r[   rH   rY   rK   rJ   rI   r   s                r3   watch_my_tradeszderive.watch_my_trades4  s
     !!!#<<_fUf%%l3i?[[(FH%F3<'K!
 
 ++gv.))+wMM??OOFE2E00tTT3 	", Nr   c                    | j                   }|(| j                  | j                  dd      }t        |      }| j	                  |d      }| j                  |d      }| j                  |d      }t        dt        |            D ]P  }| j                  |      }	|j                  |	       |j                  ||       ||	d   z   }
|j                  ||
       R y )Nr$   r#   rV   rb   r`   r   rN   )myTradesr8   r+   r   rg   rh   r   r   r   r   r   ro   )r1   r^   rI   r   rO   rV   r[   	rawTradesr   r   rH   s              r3   handle_my_tradezderive.handle_my_trade\  s     ==%%dllM4HE-e4H2  3NN662	q#i.) 	2A$$W-EOOE"NN8U+%/1KNN8[1	2r4   c                    d|vry| j                  |d      }| j                  |d      }	 |L| j                  dz   | j                  |      z   }| j	                  | j
                  d   ||       t        |      y# t        $ r[}t        |t              r0d}|j                  ||       ||j                  v r|j                  |= n|j                  |       Y d }~yd }~ww xY w)Nr   Fcode r(   r   T)rg   rh   r@   jsonthrow_exactly_matched_exceptionr-   r   	Exception
isinstancer   r   rk   )r1   r^   rI   errorMessage	errorCodefeedbackr   rH   s           r3   handle_error_messagezderive.handle_error_messagem  s     7"~~gw7$$\6:		$77S=499W+==44T__W5MyZbc#H-- 	%!45-e[1&"6"66,,[9e$	s   AA: :	CACCc                    | j                  ||      ry | j                  | j                  | j                  | j                  | j
                  d}d }| j                  |d      }|y| j                  |d      }|e|j                  d      }|j                  d      dk\  s|j                  d      dkD  r| j                  |d      }|dk(  rd	}n| j                  |d      }| j                  ||      }|
 |||       y d
|v r}| j                  |d
      }	| j                  |j                  d
      }
| j                  |
|	i       }d|v r7|d   dk(  r| j                  ||       y |d   dk(  r| j                  ||       y y y y )N)r\   r}   r   r   mytradesrV   rb   r   r   r   r   r6   r   r@   rC   r   r   )r   rs   r~   r   r   r   rg   rh   r   r   r7   index_byrk   handle_authr   )r1   r^   rI   methodseventrV   rb   parsedChannelrC   r@   subscriptionsByIdrJ   s               r3   handle_messagezderive.handle_message  s   $$VW5//(('''',,
 2&&vy9G" 'c 2LL*a/GLL4JQ4N ,,]A>E( * ,,]A>E%067#7?!!'40B $f.B.BD I??+<b"EL<')^;$$VW5!(+}<,,VW= = (	 r4   c                 4   d}| j                  |d      }t        |      dkD  r.| j                  |j                  d      }|j	                  d       y t        | j                  |            }|j                  ||       ||j                  v r|j                  d= y y )Nr   r   r   T)	r   r   r7   futuresro   r   r   r   rk   )r1   r^   rI   rH   idsr   r   s          r3   r   zderive.handle_auth  s     &nnWh/s8a<__V^^_EFNN4 '		'(:;EMM%-f222((9 3r4   )&__name__
__module____qualname__r   r0   r=   rL   r   r   r   r]   r   rs   r
   rv   r~   r   r   r   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   __classcell__)r2   s   @r3   r   r      s   %# %N
V @DB !S ! !S\ !B) )B 68 E EF E8HF HT =? N N3 N8 9; MC Ms M0
V1 11F 1& , <@d[] U US U Ubfglbm UB+6 +( )+ <V 04$UYbd &U &UC &Us &Uimnsit &UPF+6 F+P 37TX\eg &UC &Us &URU &Ulpqvlw &UP2f 2"6 6#>V #>L:& :r4   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   r	   r
   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   async_supportr    r4   r3   <module>r      s?     O J J J 4  * 0 -p
:T&& p
:r4   