
    ei/b                         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mZmZmZmZmZmZ d dlmZ d dlmZ  G d dej        j                  ZdS )    N)
ArrayCacheArrayCacheBySymbolById)AnyBoolIntMarketOrder	OrderBookStrStringsTickerTickersFundingRateFundingRatesTrade)Client)Listc            
       r    e Zd Zdef fdZd Zi 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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i fde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i fde	defdZdi fdedefdZdefdZ dde!defdZ"dede#fdZ$defdZ% xZ&S )paradexreturnc                     |                      t          t          |                                           dddddddddddddddiddidd	d
ddi i d          S )NTF)wswatchFundingRatewatchFundingRateswatchTickerwatchTickerswatchOrderBookwatchOrderswatchTradeswatchTradesForSymbolswatchBalance
watchOHLCVz https://x.com/tradeparadex/photor   z"wss://ws.api.prod.paradex.trade/v1z%wss://ws.api.testnet.paradex.trade/v1zhttps://www.paradex.trade/z'https://docs.api.testnet.paradex.trade/z7https://docs.paradex.trade/getting-started/trading-fees )logoapitestwwwdocfeesreferral)hasurlsoptions	streaming)deep_extendsuperr   describe)self	__class__s    N/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/paradex.pyr1   zparadex.describe   s    gt 4 4 = = ? ?$(%)# $"&##). %#  ;> A 4@Q  9B
 B
   	    c                 z    |                      |                     | j        dd          d          }|| j        d<   |S )N	requestIdr      )sumsafe_integerr-   )r2   r7   s     r4   
request_idzparadex.request_id/   s;    HHT..t|[!LLaPP	$-[!r5   c                   K   | j         d         d         }|                     |          }d}|                    d          }|                     |j        |          }|a|                                  d {V }d|                                 dd|id}|                     |||                     ||          |           | d {V S )Nr%   r   authenticated2.0authbearer)jsonrpcidmethodparams)	r,   clientreusableFuture
safe_valuesubscriptionsauthenticateRestr;   watchr/   )	r2   rD   urlrE   messageHashfuturer=   tokenrequests	            r4   authenticatezparadex.authenticate4   s      it$S!!%&&77(<kJJ //11111111E oo'' e	 G JJsK)9)9'6)J)JKXXX||||||r5   rE   c                     |                      |d          }|4|                     |j        d          }||                    d           d S d S d S )Nresultr=   T)	safe_dictrG   futuresresolve)r2   rE   messagerR   rM   s        r4   handle_authentication_messagez%paradex.handle_authentication_messageG   s`     22__V^_EEF!t$$$$$	  "!r5   Nsymbolsincelimitc                   K   |                                   d{V  d}|!|                     |          }||d         z  }n|dz  }| j        d         d         }ddd	|id
}|                     |||                     ||          |           d{V }	| j        r|	                    ||          }|                     |	||dd          S )a$  
        get the list of most recent trades for a particular symbol

        https://docs.paradex.trade/ws/web-socket-channels/trades/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>`
        Nztrades.rB   ALLr%   r   r>   	subscribechannelrA   rC   rD   	timestampT)load_marketsmarketr,   rJ   r/   
newUpdatesgetLimitfilter_by_since_limit)
r2   rX   rY   rZ   rD   rL   rb   rK   rO   tradess
             r4   watch_tradeszparadex.watch_tradesV   s      !!!!!!!!![[((F6$<'KK5 Kit$!;
 
 zz#{D4D4DWf4U4UWbcccccccc? 	3OOFE22E))&%TRRRr5   c                    |                      |di           }|                      |di           }|                     |          }|d         }|                     |d          }|                     | j        |          }|3t          |                     | j        dd                    }|| j        |<   |                    |           |	                    ||           |S )NrD   datarX   r^   tradesLimit  )
rS   parse_tradesafe_stringrG   rf   r   r:   r-   appendrU   )	r2   rE   rV   rD   ri   parsedTraderX   rL   storeds	            r4   handle_tradezparadex.handle_tradev   s    & 266~~ffb11&&t,,X&&&vy99f55> 1 1$,t T TUUF"(DKk"""v{+++r5   c                 B  K   |                                   d{V  |                     |          }d|d         z   dz   }| j        d         d         }ddd	|id
}|                     |||                     ||          |           d{V }|                                S )aC  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://docs.paradex.trade/ws/web-socket-channels/order-book/order-book

        :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
        Nzorder_book.rB   z.snapshot@15@100msr%   r   r>   r]   r^   r_   )ra   rb   r,   rJ   r/   rZ   )	r2   rX   rZ   rD   rb   rL   rK   rO   	orderbooks	            r4   watch_order_bookzparadex.watch_order_book   s       !!!!!!!!!V$$#fTl25IIit$!;
 
 **S+t7G7GQW7X7XZeffffffff	   r5   c                    |                      |di           }|                      |di           }|                     |d          }|                     |          }|                     |d          }|d         }|| j        vr|                                 | j        |<   g g d}	|                     |d          }
t          dt          |
                    D ]}|                      |
|          }|                     |d	          }|                     |d
          }|                     |d          }|dk    r|	d         	                    ||g           ~|	d         	                    ||g           | j        |         }| 
                    |	||dd          }|                     |d          |d<   |                    |           |                     |d          }|                    ||           d S )NrD   ri   rb   last_updated_atrX   )bidsasksinsertsr   sidepricesizeBUYrw   rx   seq_nononcer^   )rS   rm   safe_marketr:   
orderbooks
order_book	safe_listrangelenrn   parse_order_bookresetrU   )r2   rE   rV   rD   ri   marketIdrb   r`   rX   orderbookDatary   iinsertrz   r{   r|   rs   snapshotrL   s                      r4   handle_order_bookzparadex.handle_order_book   s   < 266~~ffb11##D(33!!(++%%d,=>>	!$/))&*oo&7&7DOF#
 
 ..y11q#g,,'' 	< 	<A^^GQ//F##FF33D$$VW55E##FF33Du}}f%,,eT];;;;f%,,eT];;;;OF+	((	6SYZZ --dH==!!!&&vy99y+.....r5   c                   K   |                                   d{V  |                     |          }d}| j        d         d         }ddd|id}|d	z   |z   }|                     |||                     ||          |           d{V S )
a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://docs.paradex.trade/ws/web-socket-channels/markets-summary/markets-summary

        :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>`
        Nmarkets_summaryr%   r   r>   r]   r^   r_   .ra   rX   r,   rJ   r/   r2   rX   rD   r^   rK   rO   rL   s          r4   watch_tickerzparadex.watch_ticker   s       !!!!!!!!!V$$#it$!7
 
 mf,ZZ[$2B2B7F2S2SU`aaaaaaaaar5   symbolsc                 J  K   |                                   d{V  |                     |          }d}| j        d         d         }ddd|id}g }|Yt          |t                    rDt          d	t          |                    D ]%}|d
z   ||         z   }|                    |           &n|                    |           |                     ||| 	                    ||          |           d{V }	| j
        ri }
|	|
|	d         <   |
S |                     | j        d|          S )a  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://docs.paradex.trade/ws/web-socket-channels/markets-summary/markets-summary

        :param str[] symbols: 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   r%   r   r>   r]   r^   r_   r   r   rX   )ra   market_symbolsr,   
isinstancelistr   r   rn   watch_multipler/   rc   filter_by_arraytickers)r2   r   rD   r^   rK   rO   messageHashesr   rL   	newTickerrR   s              r4   watch_tickerszparadex.watch_tickers  sk      !!!!!!!!!%%g..#it$!7
 
 :gt#<#<1c'll++ 2 2%mgaj8$$[11112   )))--c=$BRBRSZ\bBcBcerssssssss	? 	F*3F9X&'M##DL(GDDDr5   c                   K   |                                   d{V  |                                  d{V  d}d}|1|                     |          }|d         }||d         z  }|d|z   z  }n|dz  }| j        d         d	         }d
dd|id}	|                     |||                     |	|          |           d{V }
| j        r|
                    ||          }|                     |
|||d          S )a6  
        watches information on multiple orders made by the user

        https://docs.paradex.trade/ws/web-socket-channels/orders/orders

        :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>`
        Norderszorders.rX   rB   :r\   r%   r   r>   r]   r^   r_   T)	ra   rP   rb   r,   rJ   r/   rc   rd   filter_by_symbol_since_limit)r2   rX   rY   rZ   rD   rL   r^   rb   rK   rO   r   s              r4   watch_orderszparadex.watch_orders%  sE      !!!!!!!!!!!!!!!!!![[((FH%Fvd|#G3<'KKuGit$!7
 
 zz#{D4D4DWf4U4UW^________? 	3OOFE22E00tTTTr5   c                    |                      |di           }|                      |di           }|                     |          }|                     |d          }| j        0|                     | j        dd          }t          |          | _        | j                            |           d}|                    | j        |           |%|dz   |z   }	|                    | j        |	           d S d S )NrD   ri   rX   ordersLimitrk   r   r   )	rS   parse_orderrm   r   r:   r-   r   rn   rU   )
r2   rE   rV   rD   ri   parsedrX   rZ   rL   symbolMessageHashs
             r4   handle_orderzparadex.handle_orderI  s    8 266~~ffb11!!$''!!&(33;%%dlM4HHE077DK6"""t{K000 +c 1F :NN4;(9::::: r5   c                    |                      |di           }|                      |di           }|                     |d          }|                     |          }|d         }|                     |d          }|dz   |z   }	|                     ||          }
|
| j        |<   |                    |
|           |                    |
|	           |S NrD   ri   rX   r^   r   )rS   rm   r   parse_tickerr   rU   )r2   rE   rV   rD   ri   r   rb   rX   r^   rL   tickers              r4   handle_tickerzparadex.handle_tickers  s    2 266~~ffb11##D(33!!(++!""6955mf,""400%Vvw'''v{+++r5   c                   K   |                                   d{V  |                     |          }d}| j        d         d         }ddd|id}|d	z   |z   }|                     |||                     ||          |           d{V S )
a  
        watch the current funding rate for a symbol

        https://docs.paradex.trade/ws/web-socket-channels/funding-data-market-symbol/funding-data-market-symbol

        :param str symbol: unified market symbol
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `funding rate structure <https://docs.ccxt.com/?id=funding-rate-structure>`
        Nfunding_datar%   r   r>   r]   r^   r_   r   r   r   s          r4   watch_funding_ratezparadex.watch_funding_rate  s       !!!!!!!!!V$$ it$!7
 
 mf,ZZ[$2B2B7F2S2SU`aaaaaaaaar5   c                 v  K   |                                   d{V  |                     |          }d}| j        d         d         }ddd|id}g }|ot          |          }|d	k    rDt	          d	t          |                    D ]%}|d
z   ||         z   }	|                    |	           &n+|                    |           n|                    |           |                     |||                     ||          |           d{V }
| j        ri }|
||
d         <   |S | 	                    | j
        d|          S )a  
        watch the funding rate for multiple markets

        https://docs.paradex.trade/ws/web-socket-channels/markets-summary/markets-summary

        :param str[] [symbols]: a list of unified market symbols
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `funding rate structure <https://docs.ccxt.com/?id=funding-rate-structure>`
        Nr   r%   r   r>   r]   r^   r_   r   r   rX   )ra   r   r,   r   r   rn   r   r/   rc   r   fundingRates)r2   r   rD   r^   rK   rO   r   symbolsLengthr   rL   newFundingRatesrR   s               r4   watch_funding_rateszparadex.watch_funding_rates  s      !!!!!!!!!%%g.. it$!7
 
 LLMq  q#g,,// 6 6A")C-'!*"<K!((55556 $$W----  ))) $ 3 3CHXHXY`bhHiHikx y yyyyyyy? 	F0?F?8,-M##D$5xIIIr5   c                    |                      |di           }|                      |di           }|                     |          }|d         }|| j        |<   |                     |d          }|dz   |z   }|                    ||           d S r   )rS   parse_funding_rate_wsr   rm   rU   )	r2   rE   rV   rD   ri   fundingRaterX   r^   rL   s	            r4   handle_funding_ratezparadex.handle_funding_rate  s    & 266~~ffb110066X&$/&!""6955mf,{K00000r5   rb   c                    |                      |d          }|                     ||          }|                     |d          }|                      |d          }i d|d|dd dd d|                     d	          d
d d|d|                     |          d|                     |d          dd dd dd dd dd dd dd dd d|dz   iS )Nrb   
created_atfunding_period_hoursinforX   	markPrice
indexPriceinterestRate0estimatedSettlePricer`   datetimer   funding_ratefundingTimestampfundingDatetimenextFundingRatenextFundingTimestampnextFundingDatetimepreviousFundingRatepreviousFundingTimestamppreviousFundingDatetimeintervalh)rm   safe_symbolr:   parse_numberiso8601safe_number)r2   contractrb   r   rX   r`   fundingPeriods          r4   r   zparadex.parse_funding_rate_ws  sk    ##Hh77!!(F33%%h==	((3IJJ
H
f
 
 $	

 D--c22
 #D
 
 Y//
 4++HnEE
 
 t
 t
 #D
 "4
 "4
  '!
" &t#
$ +%
 
 	
r5   c                 b   |                      |d          }|dS |                     |d          }||| j        dz   |                     |          z   }|                     | j        d         d|           |                     |d          }|"|                     | j        d         ||           d	S )
NerrorTcode exactz-32600rV   broadF)rS   rm   rB   jsonthrow_exactly_matched_exception
exceptionsrG   throw_broadly_matched_exception)r2   rE   rV   r   	errorCodefeedbackmessageStrings          r4   handle_error_messagezparadex.handle_error_message  s     w00=4((77I$7S=499U+;+;;44T_W5MxYabbb $y A A ,889QS`bjkkk5r5   c                    |                      ||          sd S |                     |d          }||                     ||           d S |                     |d          }||                     |d          }|                    d          }|                     |d          }| j        | j        | j        | j	        | j
        d}|                     ||          }	|	 |	||           d S d S d S )NrR   rD   r^   r   r   )rf   r   r   r   r   )r   rG   rW   rS   rm   splitrq   r   r   r   r   )
r2   rE   rV   rR   ri   r^   partsnamemethodsrC   s
             r4   handle_messagezparadex.handle_message5  s   ((99 	F: (33..vw???F~~gx00&&tY77GMM#&&E##E1--D+"4#'#5+ $ 8 G __Wd33F!vw'''''  "!r5   )N)'__name__
__module____qualname__r   r1   r;   rP   r   rW   strr   r   r   rg   rq   r
   rt   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r3   s   @r4   r   r      s'       #      @  
 )+    &%F % % % % <@d[] S S SS S Sbfglbm S S S S@6    @ @DB ! !S ! !S\ ! ! ! !29/ 9/ 9/ 9/ 9/v 68 b b bF b b b b0 6:" !E !E7 !E !E !E !E !EF 04$UYbd "U "U "UC "Us "Uimnsit "U "U "U "UH(;6 (; (; (; (;T$F $ $ $ $L <> b bs b+ b b b b0 <@ %J %J %J| %J %J %J %JN1& 1 1 1 18#
 #
f #
 #
 #
 #
 #
J6 t    81(V 1( 1( 1( 1( 1( 1( 1( 1(r5   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   r	   r
   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   async_supportr    r5   r4   <module>r      sG       O O O O O O O O F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F  F 4 4 4 4 4 4      Y	( Y	( Y	( Y	( Y	(d ( Y	( Y	( Y	( Y	( Y	(r5   