
    izn                         d dl Zd dlmZmZmZ d dl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  G d d	ej        j                  ZdS )
    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)IntOrder	OrderBookStrTickerTrade)Client)List)InvalidNonce)Precisec            
           e Zd Z fdZd!dZd Zi f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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Zd
i fdededefdZde	fdZde	fdZd Zd Zi 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
d
i fdededefdZ!de	fdZ"de	fd Z# xZ$S )#idexc                     |                      t          t          |                                           dddddddddddii dddddi d dd	id
dd	d          S )NTF)wswatchOrderBookwatchTrades
watchOHLCVwatchTickerwatchTickerswatchOrderswatchTransactionsr   z wss://websocket-matic.idex.io/v1)testapi  
maxRetries   
   '  )	tradesLimitordersLimit
OHLCVLimitwatchOrderBookLimitorderBookSubscriptionstokenr   !fetchOrderBookSnapshotMaxAttemptsfetchOrderBookSnapshotMaxDelay)hasurlsoptions)deep_extendsuperr   describe)self	__class__s    S/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/idex.pyr/   zidex.describe   s    dD 1 1 : : < <"&#"# %#%)	 	 < 	   $#"'+*, !# 6827 #?
 ?
   	    Tc                 v   K   | j         d         d         }d|gd}|                     |||||           d {V S )Nr   r   	subscribe)methodsubscriptions)r+   watch)r0   subscribeObjectmessageHashsubscriptionurlrequests         r2   r5   zidex.subscribe2   s\      i%!
 
 ZZ[';UUUUUUUUUr3   c                    K   |                                   d {V }| j        d         d         }d||gd}|                     ||||           d {V S )Nr   r   r5   )r6   r'   r7   )authenticater+   r8   )r0   r9   r:   r'   r<   r=   s         r2   subscribe_privatezidex.subscribe_private<   s      ''))))))))i%!
 
 ZZ[';GGGGGGGGGr3   symbolreturnc                    K   |                                   d{V  |                     |          }d}||d         gd}|dz   |d         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
        :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tickersidnamemarkets:)load_marketsmarketr5   extend)r0   rA   paramsrK   rG   r9   r:   s          r2   watch_tickerzidex.watch_tickerH   s       !!!!!!!!!V$$t~
 
 Sj6$</^^DKK$H$H+VVVVVVVVVr3   clientc                    |                      |d          }|                     |d          }|                      |d          }|                     |          }|dz   |z   }|                     |d          }|                      |d          }	|                      |d          }
d }|
|	t	          j        |	|
          }|                     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 S )!NtypedatamrI   tcPrA   	timestampdatetimehighhlowlbidb	bidVolumeaska	askVolumevwapopenocloselastpreviousClosechange
percentageaveragevq)
baseVolumequoteVolumeinfo)	safe_string
safe_valuesafe_symbolsafe_integerr   
string_mulsafe_tickeriso8601resolve)r0   rO   messagerQ   rR   marketIdrA   r:   rW   rf   rj   ri   tickers                r2   handle_tickerzidex.handle_tickerY   sW   " 00w//##D#..!!(++Sj8+%%dC00	  s++%%dC00
"):'z::F!! #
f#
#
 Y//#
 D$$T3//	#

 4##D#..#
 4##D#..#
 #
 4##D#..#
 #
 D#
 D$$T3//#
 U#
 E#
 T#
 f#
  *!#
" t##
$ **455++D#66)#
 #
 #
  , 	v{+++++r3   Nsincelimitc                 P  K   |                                   d{V  |                     |          }|d         }d}||d         gd}|dz   |d         z   }|                     ||           d{V }	| j        r|	                    ||          }|                     |	||dd          S )	a  
        get the list of most recent trades for a particular symbol
        :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>`
        NrA   tradesrE   rF   rI   rW   T)rJ   rK   r5   
newUpdatesgetLimitfilter_by_since_limit)
r0   rA   r}   r~   rM   rK   rG   r9   r:   r   s
             r2   watch_tradeszidex.watch_trades   s       !!!!!!!!!V$$!t~
 
 Sj6$</~~o{CCCCCCCC? 	3OOFE22E))&%TRRRr3   c                    |                      |d          }|                     |d          }|                      |d          }|dz   |z   }|                     |          }t          | j                                                  }t          |          }	|	dk    r/|                     | j        d          }
t          |
          | _        | j        }|
                    |           |                    ||           d S )NrQ   rR   rS   rI   r   r"   )rq   rr   parse_ws_tradelistr   keyslenrt   r,   r   appendrx   )r0   rO   ry   rQ   rR   rz   r:   trader   lengthr~   r   s               r2   handle_tradezidex.handle_trade   s    00w//##D#..Sj8+##D))DK$$&&''TQ;;%%dlMBBE077DKev{+++++r3   c                 R   |                      |d          }|                     |          }|                      |d          }|                      |d          }|                      |d          }|                      |d          }|                     |d          }	|                      |d          }
|                      |d          |                      |d	          d
}|                      |d          }|                     ||	|                     |	          ||d d ||
||||d          S )NrS   iprm   QrT   sra   fcurrencycostr\   )rp   rW   rX   rA   rE   orderrQ   takerOrMakersidepriceamountr   fee)rq   rs   rt   
safe_traderw   )r0   r   rK   rz   rA   rE   r   r   r   rW   r   r   takerOrMarkers                r2   r   zidex.parse_ws_trade   sF   . ##E3//!!(++eS))  ,,!!%--s++%%eS11	s++((44$$UC00
 
 ((44"Y//) 
  
   	r3   1mc                   K   |                                   d{V  |                     |          }|d         }d}|                     | j        ||          }||d         g|d}	|dz   |d         z   }
|                     |	|
           d{V }| j        r|                    ||          }|                     |||dd          S )	aQ  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
        :param str symbol: unified symbol of the market to fetch OHLCV data for
        :param str timeframe: the length of time each candle represents
        :param int [since]: timestamp in ms of the earliest candle to fetch
        :param int [limit]: the maximum amount of candles to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int[][]: A list of candles ordered, open, high, low, close, volume
        NrA   candlesrE   )rG   rH   intervalrI   r   T)rJ   rK   rq   
timeframesr5   r   r   r   )r0   rA   	timeframer}   r~   rM   rK   rG   r   r9   r:   ohlcvs               r2   watch_ohlcvzidex.watch_ohlcv   s       !!!!!!!!!V$$!##DOY	JJt~ 
 

 Sj6$</nn_kBBBBBBBB? 	2NN6511E))%q$GGGr3   c           	      t   |                      |d          }|                     |d          }|                      |d          }|dz   |z   }|                     |d          |                     |d          |                     |d          |                     |d          |                     |d	          |                     |d
          g}|                     |          }|                      |d          }	|                     |	          }
|                     | j        |i           | j        |<   |                     | j        |         |
          }|;|                     | j        dd          }t          |          }|| j        |         |
<   |	                    |           |
                    ||           d S )NrQ   rR   rS   rI   r   re   rZ   r\   rU   rl   r   r$   r   )rq   rr   rt   
safe_floatrs   find_timeframeohlcvsr,   r   r   rx   )r0   rO   ry   rQ   rR   rz   r:   parsedrA   r   r   storedr~   s                r2   handle_ohlcvzidex.handle_ohlcv  s    00w//##D#..Sj8+dC((OOD#&&OOD#&&OOD#&&OOD#&&OOD#&&
 !!(++##D#..''11	"oodk62FFFV!4i@@>%%dlL$GGE*511F-3DK	*fv{+++++r3   c                 .   |                      |d          }t          dt          |                    D ]}||         }|                     |d          }|dk    r|                      |d          }t          dt          |                    D ]}||         }	|                      | j        di           }
|	|
vrY|                     |	          }|| j        vr|                     i           }|| j        |<   |                     | j	        ||            d S d S )Nr7   r   rG   l2orderbookrH   r&   )
rr   ranger   rq   r,   rs   
orderbookscounted_order_bookspawnfetch_order_book_snapshot)r0   rO   ry   r7   r   r;   rG   rH   jrz   r&   rA   	orderbooks                r2   handle_subscribe_messagezidex.handle_subscribe_message)  s4    AAq#m,,-- 	 	A(+L##L&99D}$$//,	BBq#g,,// 	S 	SA&qzH-1__T\Kceg-h-h*$(>>>!%!1!1(!;!; &$/ 9 9(,(?(?(C(CI6?DOF3

4#A66RRR %	 	r3   c                 V  K   | j         |         }|                     |          }d|d         z   }|j        |         }|d         s|                                 |d<   d|d<   |                     | j        dd          }|                     | j        dd	          }		 |                     |d
d          }
|                     ||
           d {V }|                     |j        d          }|                     |d          }|                     |d          }t          |j                  }|                     |j        |dz
            }|                     |d          }|                     |d          }|d uo|d u}|r||d         dz   k    r||d         k    r|
                    |           t          dt          |j                            D ]^}|j        |         }|                     |d          }|                     |d          }||d         k    r|                     |||           _d|d<   |                    ||           d S |d         dz   |d<   |                                 |d         z
  }|d         |k     }||	k     }|r&|r$|                     | j        | j        ||           d S |sdt#          |          z   dz   ndt#          |	          z   dz   }t%          | j        dz   |z   |z             # t(          $ r&}d|d<   |                    ||           Y d }~d S d }~ww xY w)Nzl2orderbook:rE   fetchingOrderBookSnapshot	startTimeTr(   r    r)   r!   r~   r   rR   u   nonceFnumAttemptsz in z	 attemptsz after z millisecondszC failed to synchronize WebSocket feed with the snapshot for symbol )r   rK   r7   millisecondsrt   r,   fetch_rest_order_book_saferr   cacher   resetr   handle_order_book_messagerx   delay	rateLimitr   strr   rE   	Exceptionreject)r0   rO   rA   rM   r   rK   r:   r;   maxAttemptsmaxDelayr~   snapshotfirstBuffered	firstData
firstNoncer   lastBufferedlastData	lastNonce	bothExistr   ry   rR   r   timeElapsedmaxAttemptsValidtimeElapsedValidendpartes                                r2   r   zidex.fetch_order_book_snapshotG  s     OF+	V$$)F4L8+K878 	<(,(9(9(;(;L%4801''6Y[]^^$$T\3SUZ[[,	*%%lGQ??E!<<VUKKKKKKKKH OOIOQ??Mv>>I**9c::J))F??9?FQJGGL|V<<H))(C88I#4/Lit6KI  KjHW,=,AAA	T\]dTeHeHe)))q#io"6"677 S SA'oa0G??7F;;D))$44A9W--- 66vw	RRR<A89y+66666 /;=.IA.M]+"//11L4MM#/#>#L #.#9 # K(8 KJJt~t/MvW]^^^^^N^  Qfs;'7'77+EEenqtu}q~q~e~  BQ  fQG&tw1v'vy'  CJ  (J  K  K  K 	* 	* 	*8=L45MM![)))))))))	*s'   F.K8 
A#K8 /A	K8 8
L(L##L(c                 $  K   |                                   d{V  |                     |          }d}||d         gd}|dz   |d         z   }dddd}|d	|d
<   n||d
<   |                     |||           d{V }	|	                                S )a  
        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
        Nr   rE   rF   rI   Fr   )r   r   r   r   r~   )rJ   rK   r5   r~   )
r0   rA   r~   rM   rK   rG   r9   r:   r;   r   s
             r2   watch_order_bookzidex.watch_order_book  s       !!!!!!!!!V$$t~
 
 Sj6$</).
 

 =$(L!!$)L!..+|TTTTTTTT	   r3   c                    |                      |d          }|                     |d          }|                     |          }| j        |         }|d         |j                            |           d S |                     |||           d S )NrR   rS   r   )rr   rq   rs   r   r   r   r   )r0   rO   ry   rR   rz   rA   r   s          r2   handle_order_bookzidex.handle_order_book  s    w//##D#..!!(++OF+	W%O""7+++++**67IFFFFFr3   c                 *   |                      |d          }|                     |d          }|                      |d          }|dz   |z   }|                     |d          }|                     |d          }	|                     |d          }
|                     |d          }|                     |d	         |
           |                     |d
         |           ||d<   |	|d<   |                     |	          |d<   |                    ||           d S )NrQ   rR   rS   rI   r   rT   r^   ra   bidsasksr   rW   rX   )rq   rr   rt   handle_deltasrw   rx   )r0   rO   ry   r   rQ   rR   rz   r:   r   rW   r   r   s               r2   r   zidex.handle_order_book_message  s   " 00w//##D#..Sj8+!!$,,%%dC00	tS))tS))9V,d3339V,d333"	'!*	+ $Y 7 7	*y+.....r3   c                     |                      |d          }|                      |d          }|                     |d          }|                    |||           d S )Nr   r      )r   rt   store)r0   booksidedeltar   r   counts         r2   handle_deltazidex.handle_delta  sY    q))**!!%++ufe,,,,,r3   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )r   r   r   )r0   r   deltasr   s       r2   r   zidex.handle_deltas  sH    q#f++&& 	3 	3Ahq	2222	3 	3r3   c                 x  K   |                                  }|                     | j        dd          }||z
  dk    rs| j        |                                 d}|                     |                     ||                     d {V }|| j        d<   |                     |d          | j        d<   | j        d         S )NlastAuthenticatedTimer   i  )walletr   r'   )secondsrt   r,   walletAddressuuidv1privateGetWsTokenrL   rq   )r0   rM   timer   r=   responses         r2   r?   zidex.authenticate  s      ||~~ $ 1 1$,@WYZ [ [''#--, G "33DKK4P4PQQQQQQQQH48DL01$($4$4Xw$G$GDL!|G$$r3   c                 b  K   |                                   d{V  d}d|i}|}|8|                     |          }|                     |          }|g|d<   |dz   |z   }|                     ||           d{V }	| j        r|	                    ||          }|                     |	||dd          S )a  
        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
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        NordersrG   rH   rI   rW   T)rJ   rA   	market_idr@   r   r   r   )
r0   rA   r}   r~   rM   rG   r9   r:   rz   r   s
             r2   watch_orderszidex.watch_orders  s       !!!!!!!!!D
 [[((F~~f--H*2OI&*x/K--o{KKKKKKKK? 	3OOFE22E))&%TRRRr3   c                    |                      |d          }|                     |d          }|                      |d          }|                     |          }|                     |d          }|                     |dg           }g }	t	          dt          |                    D ]0}
|	                    |                     ||
                              1|                      |d          }|                      |d          }|                      |d	          }|                      |d
          }|                      |d          }|                      |d          }|                      |d|          }|                      |d          }|                     |          }d d d}d }t	          dt          |	                    D ]G}
|	|
         }|d         d         |d<   |d         d         }t          j
        |d         |          |d<   H|                     |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!          }| j        0|                     | j        d"d#          }t!          |          | _        | j        }|                    |           |d$z   |z   }|                    ||           |                    ||           d S )%NrQ   rR   rS   rT   Fr   r   r   re   rm   zrl   r   Xr   r   r   r   rW   rp   rE   clientOrderIdrX   lastTradeTimestamprA   r   	stopPricetriggerPricer   rk   filled	remaining)statusr   r   r#   r   rI   )rq   rr   rs   rt   r   r   r   r   parse_order_statusr   
string_add
safe_orderrw   parse_numberr   r,   r   rx   )r0   rO   ry   rQ   r   rz   rA   rW   fillsr   r   rE   r   	orderTyper   r   rk   r   	rawStatusr   r   	lastTradestringLastTradeFeer   parsedOrderr~   r   symbolSpecificMessageHashs                               r2   handle_orderzidex.handle_order  s   N 0000##E3//!!(++%%eS11	sB//q#e**%% 	9 	9AMM$--eAh778888eS))s++$$UC00	!!%--!!%--""5#..  99$$UC00	((33
 
 	q#f++&& 	N 	NAq	I'.z:C
O!*5!1&!9!,S[:LMMCKK!..y+FFoo '
G'
"'
 T'
 	'

 Y//'
 !"4'
 f'
 I'
 D'
 T&&u--'
 '
 D'
 d''//'
 D'
 t((11'
  d''//!'
" #'
$ )'
 '
 '
  , ;%%dlM4HHE077DKk"""$(3J$9!v8999vt$$$$$r3   codec                    K   |                                   d {V  d}d|i}|}||dz   |z   }|                     ||           d {V }| j        r|                    ||          }|                     |||d          S )NbalancesrG   rI   rW   )rJ   r@   r   r   r   )	r0   r  r}   r~   rM   rG   r9   r:   transactionss	            r2   watch_transactionszidex.watch_transactionsZ  s      !!!!!!!!!D
 *t+K!33O[QQQQQQQQ? 	7 ))$66E)),ukRRRr3   c                 n   |                      |d          }|                     |d          }|                      |d          }|dz   |z   }|                     |          }|                      |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 }	|| j        vr3|                     | j        dd          }
t          |
          | j        |<   | j        |         }|                    |	           |                    ||           |                    ||           d S )NrQ   rR   ra   rI   wrp   rE   r   r   address	addressToaddressFromtagtagTotagFromr   okupdatedtxidrW   rX   r   transactionsLimitr   )	rq   rr   safe_currency_coder  rt   r,   r   r   rx   )r0   rO   ry   rQ   rR   
currencyIdr:   r  r  transactionr~   r  s               r2   handle_transactionzidex.handle_transactionh  s    00w//%%dC00
Sj:-&&z22""4--
G
$
 
 d	

 w
 
 4
 4
 T
 t
 d
 D
 t
 D
 
  !
" 4#
& )))%%dl4GNNE&0&7&7Dd#(.K(((|[111|T*****r3   c                     |                      |d          }| j        | j        | j        | j        | j        | j        | j        d}||v r||         } |||           d S d S )NrQ   )rD   r   r7   r   r   r  r   )rq   r|   r   r   r   r   r!  r  )r0   rO   ry   rQ   methodsr6   s         r2   handle_messagezidex.handle_message  s    00)'!:(1/'
 
 7??T]FF67##### ?r3   )T)N)%__name__
__module____qualname__r/   r5   r@   r   r
   rN   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  r!  r$  __classcell__)r1   s   @r2   r   r      sq           BV V V V
H 
H 
H 68 W W WF W W W W"2,F 2, 2, 2, 2,h <@d[] S S SS S Sbfglbm S S S S.,6 , , , ,2 2 2 2h 8<$]ajl H H HC HWZ Hquvzq{ H H H H4&,6 &, &, &, &,Pv    < FH 6* 6* 6* 6*p @DB ! !S ! !S\ ! ! ! !:	G 	G 	G 	G 	G/ / / / /@- - -3 3 3 )+ % % % % 04$UYbd S S SC Ss Simnsit S S S S2a%6 a% a% a% a%F 48dY]fh S SS S SSV S S S S)+ )+ )+ )+ )+V$V $ $ $ $ $ $ $ $r3   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r	   r
   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   ccxt.base.preciser   async_supportr    r3   r2   <module>r3     s        f f f f f f f f f f E E E E E E E E E E E E E E E E 4 4 4 4 4 4       ) ) ) ) ) ) % % % % % %Q
$ Q
$ Q
$ Q
$ Q
$4" Q
$ Q
$ Q
$ Q
$ Q
$r3   