
    E#io                         S SK rS SKJrJr  S SKJrJrJrJ	r	J
r
JrJrJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr   " S	 S
\R,                  R.                  5      rg)    N)
ArrayCacheArrayCacheBySymbolById)AnyBoolIntOrder	OrderBookStrTickerTrade)Client)List)ExchangeError)AuthenticationError)UnsubscribeErrorc            
         ^  \ rS rSrS\4U 4S jjrS rS rS0 4S\S\	S\
4S	 jjrS
\4S jr0 4S\S\4S jjrS
\4S jr0 4S\S\4S jjr0 4S\S\4S jjrS rS
\4S jrS
\4S jrS
\4S jrSS0 4S\S\	S\	S\\   4S jjrS
\4S jr0 4S jrS rSSS0 4S\S\	S\	S\\   4S jjrS
\4S jr SSS0 4S\S\	S\	S\\   4S jjr!S
\4S jr"S
\S\#4S jr$S
\4S jr%S
\4S jr&S r'U =r($ )!derive   returnc                    > U R                  [        [        U ]  5       SSSSSSSSSSSSS.SS0SS0S.SS0 S	.S
S0SS0 00S.5      $ )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/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/derive.pyr2   derive.describe   s    fd < > %!%#"&## %!&#)."'  5 :	  $# T R?$A
 $ $	    c                     U R                  U R                  S0 5      nU R                  X!S5      nU R                  US5      nX@R                  S   U'   U$ )Nr(   r      )
safe_valuer-   safe_integersum)r3   urlr-   previousValuenewValues        r5   
request_idderive.request_id9   sP    //$,,R@))':88M1-)1[!#&r7   c                    #    U R                   S   S   nU R                  U5      nU R                  USU05      nU R                  UUSS.5      nU R                  XAXaU5      I S h  vN $  N7fNr#   r   id	subscriberD   methodr,   r@   extendwatchr3   messageHashmessagesubscriptionr=   r(   requests          r5   watch_publicderive.watch_public@   sx     iit$OOC(	++g)(
  {{<!2
  ZZ'UUUU   A#A,%A*&A,Nsymbollimitc                   #    U R                  5       I Sh  vN   Uc  SnU R                  U5      nSUS   -   S-   U R                  U5      -   nSSU/0S.nUUUUS	.nU R                  XVU5      I Sh  vN nUR	                  5       $  Np N7f)
a  

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.rD   .10.rE   channelsrG   params)namerS   rT   r[   )load_marketsmarketnumber_to_stringrP   rT   )	r3   rS   rT   r[   r^   topicrO   rN   	orderbooks	            r5   watch_order_bookderive.watch_order_bookL   s      !!!=EV$vd|+f4t7L7LU7SS!
 	
 ++ELII	  ) 	"& Js"   BBAB2B	3B	Bclientc                 H   U R                  US5      nU R                  US5      nU R                  US5      nU R                  U5      nUS   nU R                  US5      nXpR                  ;  a^  U R	                  U R
                  SS5      n	UR                  U   n
U R	                  U
SU	5      nU R                  0 U5      U R                  U'   U R                  U   nU R	                  US	5      nU R                  XGUS
S5      nUR                  U5        UR                  X5        g )Nr[   datainstrument_namerS   channelwatchOrderBookLimitr%   rT   	timestampbidsasks)	safe_dictsafe_stringsafe_market
orderbooksr;   r-   subscriptions
order_bookparse_order_bookresetresolve)r3   rd   rM   r[   rf   marketIdr^   rS   r`   defaultLimitrN   rT   ra   rj   snapshots                  r5   handle_order_bookderive.handle_order_bookm   s     2~~ff-##D*;<!!(+!  3//),,T\\;PRVWL!//6L%%lG\JE&*oob%&@DOOF#OOF+	%%dK8	((y&&Q!y(r7   c                    #    U R                  5       I Sh  vN   U R                  U5      nSUS   -   S-   nSSU/0S.nUUUS.nU R                  XEU5      I Sh  vN $  NH N7f)	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.rD   z.100rE   rY   rZ   r\   rS   r[   )r]   r^   rP   )r3   rS   r[   r^   r`   rO   rN   s          r5   watch_tickerderive.watch_ticker   s      !!!V$F4L(61!
 

 &&u|DDD! 	"  E"   A#AAA#A!A#!A#c                     U R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  U5      nXpR                  US   '   UR	                  Xv5        U$ )Nr[   rf   instrument_tickerrh   rS   )rm   r:   parse_tickertickersru   )r3   rd   rM   r[   rawDatarf   r`   tickers           r5   handle_tickerderive.handle_ticker   sx    B 2..0~~g':;	2""4()/VH%&v%r7   c                 "  #    U R                  5       I Sh  vN   U R                  US5      nUc  SnU R                  U5      nSUS   -   S-   U R                  U5      -   nSU-   nSS	U/0S
.nSU0nU R	                  XgU5      I Sh  vN $  Nt N7f)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
NrT   rV   rW   rD   rX   unwatchunsubscriberY   rZ   r\   )r]   r;   r^   r_   un_watch_public)	r3   rS   r[   rT   r^   r`   rL   rO   rN   s	            r5   un_watch_order_bookderive.un_watch_order_book   s      !!!!!&'2=EV$vd|+f4t7L7LU7SS%'#
 E
 ))+MMM% 	"$ Ns"   BBA/BBBBc                    #    U R                  5       I Sh  vN   U R                  U5      nSUS   -   nSU-   nSSU/0S.nSU0nU R                  XVU5      I Sh  vN $  NH N7f)	z
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.rD   r   r   rY   rZ   r\   )r]   r^   r   )r3   rS   r[   r^   r`   
messageHahrO   rN   s           r5   un_watch_tradesderive.un_watch_trades  s      !!!V$F4L(&
#
 E
 ))*|LLL 	" Mr   c                    #    U R                   S   S   nU R                  U5      nU R                  USU05      nU R                  UUSS.5      nU R                  XAXaU5      I S h  vN $  N7f)Nr#   r   rD   r   rF   rH   rK   s          r5   r   derive.un_watch_public(  sx     iit$OOC(	++g)(
  {{<#2
  ZZ'UUUUrR   c                 h   UR                  S5      nU R                  US5      nU R                  U5      nUS   nX`R                  ;   a  U R                  U	 X!R                  ;   a  UR                  U	 [        U R                  S-   U-   5      nUR                  Xr5        UR                  USU-   5        g )N.r9   rS   z orderbook r   )	splitrn   ro   rp   rq   r   rD   rejectru   r3   rd   r`   parsedTopicrv   r^   rS   errors           r5   !handle_order_book_un_subscription(derive.handle_order_book_un_subscription4  s    kk#&##K3!!(+!__$'((($$U+ =!86!ABe#ui%/0r7   c                 h   UR                  S5      nU R                  US5      nU R                  U5      nUS   nX`R                  ;   a  U R                  U	 X!R
                  ;   a  UR
                  U	 [        U R                  S-   U-   5      nUR                  Xr5        UR                  USU-   5        g )Nr   r9   rS   z trades r   )
r   rn   ro   rp   tradesrq   r   rD   r   ru   r   s           r5   handle_trades_un_subscription$derive.handle_trades_un_subscriptionA  s    kk#&##K3!!(+!__$F#((($$U+ :!5!>?e#ui%/0r7   c                 f   U R                  US5      nU R                  US5      nUb  [        UR                  5       5      n[        S[	        U5      5       HW  nXV   nUR                  S5      S:  a  U R                  X5        M/  UR                  S5      S:  d  MF  U R                  X5        MY     U$ )Nresultstatusr   ra   r   )rm   listkeysrangelenfindr   r   )r3   rd   rM   r   r   topicsir`   s           r5   handle_un_subscribederive.handle_un_subscribeN  s     21&++-(F1c&k*	::k*a/::6IZZ)Q.66vE + r7   sincec                 6  #    U R                  5       I Sh  vN   U R                  U5      nSUS   -   nSSU/0S.nUUUS.nU R                  XgU5      I Sh  vN n	U R                  (       a  U	R	                  US   U5      nU R                  XX#S	5      $  N~ N?7f)
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   rD   rE   rY   rZ   r|   rS   T)r]   r^   rP   
newUpdatesgetLimitfilter_by_symbol_since_limit)
r3   rS   r   rT   r[   r^   r`   rO   rN   r   s
             r5   watch_trades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                 .   U R                  US5      nU R                  US5      nU R                  US5      nUR                  S5      nU R                  US5      nU R	                  U5      nUS   n	U R                  U R
                  U	5      n
U
c(  U R                  U R                  SS5      n[        U5      n
[        S	[        U5      5       H'  nU R                  XL   5      nU
R                  U5        M)     XR
                  U	'   UR                  X5        g )
Nr[   rf   rh   r   r9   rS   r&   r%   r   )rm   r:   r   rn   ro   r   r;   r-   r   r   r   parse_tradeappendru   )r3   rd   rM   r[   rf   r`   r   rv   r^   rS   tradesArrayrT   r   trades                 r5   handle_tradederive.handle_trade  s     2~~ff-	2kk#&##K3!!(+!oodkk6:%%dllM4HE$U+Kq#d)$A$$TW-Eu% % *F{*r7   c                 
  #    U R                  5         U R                  S   S   nU R                  U5      nSnUR                  U5      nU R	                  UR
                  U5      nUc  U R                  U5      n[        U R                  5       5      nU R                  XR                  5      n	U R                  U R                  S5      n
USU
UU	S.S.nU R                  X5      nU R                  X$XU5        UI S h  vN $  N7f)Nr#   r   authenticatedderiveWalletAddresspublic/login)walletrj   	signature)rD   rG   r[   )check_required_credentialsr,   rd   reusableFuturer:   rq   r@   strmillisecondssignMessage
privateKeyrn   r-   rI   rJ   )r3   r[   r=   rd   rL   futurer   r(   nowr   r   rO   rM   s                r5   authenticatederive.authenticate  s     '')iit$S!%&&{3(<(<kJ ,Id'')*C((oo>I"&"2"24<<AV"W(1!$!*G kk'2GJJswG||s   C:D<D=Dc                   #    U R                  5       I S h  vN   U R                  S   S   nU R                  U5      nU R                  USU05      nU R                  UUSS.5      nU R	                  XAXaU5      I S h  vN $  Nk N7frC   )r   r,   r@   rI   rJ   rK   s          r5   watch_privatederive.watch_private  s     !!!iit$OOC(	++g)(
  {{<!2
  ZZ'UUU 	" Vs"   BBA&B=B>BBc                   #    U R                  5       I Sh  vN   SnU R                  SU5      u  pTU R                  U5      S-   nUnUb  U R                  U5      nUS   nUSU-   -  nSSU/0S.n	UUS	.n
U R	                  X5      nU R                  X{U
5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XX#S
5      $  N N;7f)a-  

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.ordersrS   :rE   rY   rZ   r\   r[   T	r]   handleDeriveSubaccountIdr_   r^   rI   r   r   r   r   )r3   rS   r   rT   r[   subaccountIdr`   rL   r^   rO   rN   rM   orderss                r5   watch_ordersderive.watch_orders  s      !!!#<<]FS%%l3i?[[(FH%F3<'K!
 
 ++g.))+MM??OOF2E00tTT3 	", N"   CCBCC:CCc                    U R                  US5      nU R                  US5      nU R                  US5      n[        S[	        U5      5       GH`  nXV   nU R                  U5      nU R                  US5      n	U R                  US5      n
U	c  MB  U R                  c-  U R                  U R                  SS5      n[        U5      U l        U R                  nU R                  UR                  U	0 5      nU R                  X5      nUbr  U R                  US	5      nUb  XS	'   U R                  US
5      nUb  UUS
'   U R                  US5      US'   U R                  US5      US'   U R                  US5      US'   UR                  U5        US-   U	-   nUR                  U R                  U5        GMc     UR                  U R                  U5        g )Nr[   rh   rf   r   rS   rD   r'   r%   feefeesr   rj   datetimer   )rm   rn   	safe_listr   r   parse_orderr   r;   r-   r   r:   hashmapr   ru   )r3   rd   rM   r[   r`   	rawOrdersr   rf   parsedrS   orderIdrT   cachedOrdersr   orderr   r   messageHashSymbols                     r5   handle_orderderive.handle_order  s   T 2  3NN662	q#i.)A<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*r7   c                   #    U R                  5       I Sh  vN   SnU R                  SU5      u  pTU R                  U5      S-   nUnUb  U R                  U5      nUS   nUSU-   -  nSSU/0S.n	UUS	.n
U R	                  X5      nU R                  X{U
5      I Sh  vN nU R                  (       a  UR                  X5      nU R                  XX#S
5      $  N N;7f)a-  

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.tradesrS   r   rE   rY   rZ   r   Tr   )r3   rS   r   rT   r[   r   r`   rL   r^   rO   rN   rM   r   s                r5   watch_my_tradesderive.watch_my_trades4  s      !!!#<<_fU%%l3i?[[(FH%F3<'K!
 
 ++g.))+MM??OOF2E00tTT3 	", Nr   c                    U R                   nUc(  U R                  U R                  SS5      n[        U5      nU R	                  US5      nU R                  US5      nU R                  US5      n[        S[        U5      5       HN  nU R                  U5      n	UR                  U	5        UR                  X65        XiS   -   n
UR                  X:5        MP     g )Nr&   r%   r[   rh   rf   r   rS   )myTradesr;   r-   r   rm   rn   r   r   r   r   r   ru   )r3   rd   rM   r   rT   r[   r`   	rawTradesr   r   rL   s              r5   handle_my_tradederive.handle_my_trade\  s     ==%%dllM4HE-e4H2  3NN662	q#i.)A$$W-EOOE"NN8+/1KNN81 *r7   c                    SU;  a  gU R                  US5      nU R                  US5      n UbK  U R                  S-   U R                  U5      -   nU R	                  U R
                  S   XE5        [        U5      eg! [         a`  n[        U[        5      (       a0  SnUR                  Xg5        XqR                  ;   a  UR                  U	 OUR                  U5         S nAgS nAff = f)Nr   Fcode r*   r   T)rm   rn   rD   jsonthrow_exactly_matched_exceptionr/   r   	Exception
isinstancer   r   rq   )r3   rd   rM   errorMessage	errorCodefeedbackr   rL   s           r5   handle_error_messagederive.handle_error_messagem  s     7"~~gw7$$\6:		$77S=499W+==44T__W5Myc#H-- 	%!455-e1"6"66,,[9e$	s   AA< <
C&AC!!C&c                    U R                  X5      (       a  g U R                  U R                  U R                  U R                  U R
                  S.nS nU R                  US5      nUb}  U R                  US5      nUbh  UR                  S5      nUR                  S5      S:  d  UR                  S5      S:  a  U R                  US5      nUS:X  a  S	nOU R                  US5      nU R                  X45      nUb	  U" X5        g S
U;   a~  U R                  US
5      n	U R                  UR                  S
5      n
U R                  X0 5      nSU;   a7  US   S:X  a  U R                  X5        g US   S:X  a  U R                  X5        g g g g )N)ra   r   r   r   mytradesr[   rh   r   r   r   r   r9   r   rD   rG   r   r   )r   ry   r   r   r   r   rm   rn   r   r   r:   index_byrq   handle_authr   )r3   rd   rM   methodseventr[   rh   parsedChannelrG   rD   subscriptionsByIdrN   s               r5   handle_messagederive.handle_message  s   $$V55//(('''',,
 2&&vy9G" 'c 2LL*a/GLL4JQ4N ,,]A>E( * ,,]A>E06#7?!!'40B $f.B.BD I??+<"EL<')^;$$V5!(+}<,,V= = (	 r7   c                 6   SnU R                  US5      n[        U5      S:  a.  U R                  UR                  S5      nUR	                  S5        g [        U R                  U5      5      nUR                  Xc5        X1R                  ;   a  UR                  S	 g g )Nr   r   r   T)	r   r   r:   futuresru   r   r   r   rq   )r3   rd   rM   rL   idsr   r   s          r5   r   derive.handle_auth  s     &nnWh/s8a<__V^^_EFNN4 '		'(:;EMM%-222((9 3r7   )r   ))__name__
__module____qualname____firstlineno__r   r2   r@   rP   r   r   r	   rb   r   ry   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r  r   __static_attributes____classcell__)r4   s   @r5   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 t 6#>V #>L:& : :r7   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   r	   r
   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   async_supportr    r7   r5   <module>r     s?     O P P P 4  * 0 -p
:T&& p
:r7   