
    ii                    P   d dl mZ d dlmZ d dl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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 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" 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( d dl)m*Z*  G d dee          Z+dS )    )Exchange)ImplicitAPIN)Balances
CurrenciesCurrencyIntMarketNumOrder	OrderBook	OrderSide	OrderTypeStrStringsTickerTickersTradeTradingFeesTransaction)List)ExchangeError)AuthenticationError)
BadRequest)InsufficientFunds)InvalidAddress)InvalidOrder)NotSupported)DDoSProtection)ExchangeNotAvailable)ROUND)TRUNCATE)DECIMAL_PLACES)	TICK_SIZE)PAD_WITH_ZERO)Precisec                       e Zd Z fdZd Zi fdee         fdZi fdede	fdZ
di fdedefd	ZdAd
ede	fdZdddi fdedededee         fdZdAd
edefdZddi fdedededee         fdZdAd
edefdZi fdefdZdi fdededefdZd Zi fdefdZdefdZi fdefdZdddi fde 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ddi fde dededee#         fdZ%dddi fde dedefdZ&d Z'dAd
ede#fd Z(i fd!Z)di fded"e*d#e+d$e,d%e-f
d&Z.di fd'ed$e,fd(Z/di fde fd)Z0di fdede fd*Z1d+ Z2di fded'e fd,Z3dddi fd'e dededee4         fd-Z5i fd.Z6i fd/Z7di fded'e fd0Z8dddi fd'e dededee4         fd1Z9dddi fd'e dedefd2Z:d3 Z;dAd4e<de4fd5Z=i fd6Z>di fd'e fd7Z?dAd4e<fd8Z@d9d:i ddfd;ZAd< ZBd= ZCd> ZDd? ZEd@ ZF xZGS )Bidexc                    |                      t          t          |                                           i ddddddgddd	d
dddd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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d0dd1dd2di d3dd4dd5dd6dd7dd8dd9dd:dd;dd<dd=dd>dd?dd@ddAddBddCdi dDddEddFddGddHddIddJddKddLddMddNddOddPddQddRddSddTddddUdVdWdXdYdZd[d\d]d^d_d`daidbd`dcidddegdfdgdhdidididididididididj	ididididkdkdidididldidkdkdidmdndkidodpdqdrdsd`dtdudvt          t
          t          t          t          t          t          dwidxdddddydzt          d{t          d|i i          S )}Nidr'   nameIDEX	countriesUS	rateLimit  versionv3proT	certifiedFrequiresWeb3hasCORSspotmarginswapfutureoption	addMargincancelAllOrderscancelOrdercancelOrderscloseAllPositionsclosePositioncreateDepositAddresscreateOrdercreateReduceOnlyOrdercreateStopLimitOrdercreateStopMarketOrdercreateStopOrderfetchBalancefetchBorrowRateHistoriesfetchBorrowRateHistoryfetchClosedOrdersfetchCrossBorrowRatefetchCrossBorrowRatesfetchCurrenciesfetchDepositfetchDepositAddressfetchDepositAddressesfetchDepositAddressesByNetworkfetchDepositsfetchFundingHistoryfetchFundingRatefetchFundingRateHistoryfetchFundingRatesfetchIndexOHLCVfetchIsolatedBorrowRatefetchIsolatedBorrowRatesfetchLeveragefetchLeverageTiersfetchMarginModefetchMarketsfetchMarkOHLCVfetchMyTrades
fetchOHLCVfetchOpenInterestHistoryfetchOpenOrders
fetchOrderfetchOrderBookfetchOrdersfetchPositionfetchPositionModefetchPositionsfetchPositionsRiskfetchPremiumIndexOHLCVfetchStatusfetchTickerfetchTickers	fetchTimefetchTradesfetchTradingFeefetchTradingFeesfetchTransactionsfetchWithdrawalfetchWithdrawalsreduceMarginsetLeveragesetMarginModesetPositionMode)transferwithdraw
timeframes1m5m15m30m1h6h1d)r}   r~   r   r   r   r   r   urlsMATICz!https://api-sandbox-matic.idex.iozdhttps://user-images.githubusercontent.com/51840849/94481303-2f222100-01e0-11eb-97dd-bc14c5943a86.jpgzhttps://api-matic.idex.iozhttps://idex.iozhttps://api-docs-v3.idex.io/)testlogoapiwwwdocr   get   )	pingtimeexchangeassetsmarketstickerscandlestrades	orderbookg?)userwalletsbalancesordersfillsdepositswithdrawalswsToken)r   r   zorders/testr   r   )r   postdelete)publicprivateoptionsgtccn)defaultTimeInForcedefaultSelfTradePreventionnetwork
exceptionsexact)INVALID_ORDER_QUANTITYINSUFFICIENT_FUNDSSERVICE_UNAVAILABLEEXCEEDED_RATE_LIMITINVALID_PARAMETERWALLET_NOT_ASSOCIATEDINVALID_WALLET_SIGNATURErequiredCredentials)walletAddress
privateKeyapiKeysecretprecisionModepaddingModecommonCurrencies)deep_extendsuperr'   describer   r   r   r   r   r   r   r#   r$   )self	__class__s    O/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/idex.pyr   zidex.describe   sY   dD 1 1 : : < < d?
&d?
Fd?
 $d?
 	d?

 td?
 4d?
 d?
 Dd?
  GGG %G 	G
 %G %G UG "4G tG G $UG  G 'G tG (G  '!G" (#G G$ "4%G& 'G( +E)G* )%+G, $T-G. '/G0 (1G2 "43G4 5G6 &t7G8 (9G: 1%;G<  =G> &u?G@ #EAGB *5CGD $UEG G GF "5GGH *5IGJ +EKGL  MGN %eOGP "5QGR SGT !%UGV  WGX dYGZ +E[G\ "4]G^ d_G` !$aGb ucGd  eGf $UgG G Gh !%iGj %ekGl )%mGn toGp tqGr sGt TuGv twGx "5yGz #D{G| $U}G~ "4G@ #DAGB CGD uEGF  GGH "5IG GJ " MG G Gd?
b  cd?
t @ 8 )2 ud?
N  ! !$%"##$#$#$"#%&
 
 !"#$$%"%!$$%'(#$	 	 $%"%'*'(	  !## # #Od?
V &+.2" Wd?
` .:*;+?+9)3-;0C 
ad?
v "!%"	$ $wd?
B YCd?
D =Ed?
F Gd?
 d?
 d d d	    c                 2   |                      |          }|                     |di           }|                     |d          }|                     |t          |d         d         | j                  }|                     |t          |t          t                    S )NinfoquoteAssetPrecision	precisionprice)	market
safe_valuesafe_integerdecimal_to_precisionr    r   r!   r"   r$   )r   symbolr   r   r   r   s         r   price_to_precisionzidex.price_to_precision   s     V$$vvr22"//6KLL))%{8KG8TVZVhii((:M~_lmmmr   returnc                    |                      |          }|                                 }|                     |d          }|                     |d          }|                     |d          }|                     |d          }|                     t          j        ||                    }g }	t          dt          |                    D ]}
||
         }|                     |d          }|                     |d          }|                     |d          }| 	                    |          }| 	                    |          }|                     | 
                    |                     |d	                              }|                     | 
                    |                     |d
                              }|                     |d          }d}|dk    r|}|	                    i d|d|dz   |z   d|d|ddd|d|ddddddddddddddd|d k    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 )(a
  
        retrieves data on all markets for idex
        :see: https://api-docs-v3.idex.io/#get-markets
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: an array of objects representing market data
        makerFeeRatetakerFeeRatemakerTradeMinimumtakerTradeMinimumr   r   	baseAsset
quoteAssetbaseAssetPrecisionr   statusNETHr)   r   /basequotesettlebaseIdquoteIdsettleIdtyper7   Tr8   Fr9   r:   r;   activeinactivecontractlineartickSize)amountr   minmax)leverager   r   cost)inversetakermakercontractSizeexpiryexpiryDatetimestrike
optionTyper   limitscreatedr   )publicGetMarketspublicGetExchangesafe_numbersafe_stringparse_numberr%   
string_minrangelensafe_currency_codeparse_precisionappend)r   paramsresponse	response2r   r   makerMintakerMin
minCostETHresultientrymarketIdr   r   r   r   basePrecisionquotePrecisionr   minCosts                        r   fetch_marketszidex.fetch_markets   sd    ((00& **,,	:   N;;  N;;##I/BCC##I/BCC&&w'9(H'M'MNN
q#h--(( ?	 ?	AQKE''x88H%%e[99F&&ul;;G**622D++G44E --d.B.B4CSCSTY[oCpCp.q.qrrM!..t/C/CDDTDTUZ\qDrDr/s/sttN%%eX66FG~~$MM 2h2$*u,2 2 	2
 $2 &2 72 D2 2 2 %2 2 %2 %2 6Z/2  E!2" $#2$   $"&"+!--eZ@@   $#! !
  -# 
  .# 
  '#  $  c2 2 2 2 2 2 2f r   r   c                    |                                   |                     |          }d|d         i}|                     |                     ||                    }|                     |d          }|                     ||          S )a  
        fetches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
        :see: https://api-docs-v3.idex.io/#get-tickers
        :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>`
        r   r)   r   )load_marketsr   publicGetTickersextend	safe_dictparse_ticker)r   r   r   r   requestr   tickers          r   fetch_tickerzidex.fetch_tickerP  s     	V$$fTl
* ((Wf)E)EFF!,,  000r   Nsymbolsc                     |                                   |                     |          }|                     ||          S )a  
        fetches price tickers for multiple markets, statistical information calculated over the past 24 hours for each market
        :see: https://api-docs-v3.idex.io/#get-tickers
        :param str[]|None symbols: unified symbols of the markets to fetch the ticker for, all market tickers are returned if not assigned
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a dictionary of `ticker structures <https://docs.ccxt.com/#/?id=ticker-structure>`
        )r  r  parse_tickers)r   r  r   r   s       r   fetch_tickerszidex.fetch_tickerss  sA     	& ((00!!(G444r   r   c                    |                      |d          }|                     ||d          }|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|          S )Nr   -r   r   close	timestampdatetimehighlowbid	bidVolumeask	askVolumevwapopenlastpreviousClosechange
percentagepercentChangeaverage
baseVolumequoteVolume)r,  r-  r   )r   safe_marketr   safe_tickeriso8601)r   r  r   r  r   r  r  s          r   r  zidex.parse_ticker  s   " ##FH55!!(FC88!%%ff55	  11 !
f!
!
 Y//!
 D$$VV44	!

 4##FE22!
 4##FE22!
 !
 4##FE22!
 !
 D!
 D$$VV44!
 U!
 E!
 T!
 d!
  $**6?CC!!
" t#!
$ **6<@@++FMBB)!
 !
 !
* +  	r   r}   sincelimitc                 T   |                                   |                     |          }|d         |d}|||d<   |t          |d          |d<   |                     |                     ||                    }t          |t                    r|                     |||||          S g S )a  
        fetches historical candlestick data containing the open, high, low, and close price, and the volume of a market
        :see: https://api-docs-v3.idex.io/#get-candles
        :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
        r)   )r   intervalNstartr/   r2  )r  r   r   publicGetCandlesr  
isinstancelistparse_ohlcvs)	r   r   	timeframer1  r2  r   r   r  r   s	            r   fetch_ohlcvzidex.fetch_ohlcv  s     	V$$Tl!
 
 $GG"5$//GG((Wf)E)EFFh%% 	 $$Xvy%OOO Ir   c                    |                      |d          }|                     |d          }|                     |d          }|                     |d          }|                     |d          }|                     |d          }||||||gS )Nr5  r%  r  r  r  volume)r   r   )	r   ohlcvr   r  r%  r  r  r  r=  s	            r   parse_ohlcvzidex.parse_ohlcv  s     %%eW55	v..v..ue,,  00!!%224sE6::r   c                 "   |                                   |                     |          }d|d         i}|||d<   |t          |d          |d<   |                     |                     ||                    }|                     ||||          S )a  
        get the list of most recent trades for a particular symbol
        :see: https://api-docs-v3.idex.io/#get-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 Trade[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        r   r)   Nr5  r/   r2  )r  r   r   publicGetTradesr  parse_trades)r   r   r1  r2  r   r   r  r   s           r   fetch_tradeszidex.fetch_trades  s     	V$$fTl
 $GG"5$//GG ''GV(D(DEE  65%@@@r   c                    |                      |d          }|                      |d          }|                      |d          }|                      |d          }|                     |d          }|                      |d          }|                     ||d          }	|                      |d          }
|
d	k    rd
nd	}|                      |d|          }|                      |dd          }|                      |d          }d }|.|                      |d          }||                     |          d}|                      |d          }|                     |||                     |          |	||d||||||d|          S )NfillIdr   quantityquoteQuantityr   r   r  	makerSidebuysellside	liquidityr   feefeeAssetr   currencyorderIdr2  )r   r  r  r   r)   orderr   rK  takerOrMakerr   r   r   rM  )r   r   safe_symbolr   
safe_trader0  )r   trader   r)   priceStringamountString
costStringr  r  r   rH  oppositeSiderK  rS  feeCostStringrM  feeCurrencyIdrQ  s                     r   parse_tradezidex.parse_trade  s   N eX..&&ug66''z::%%e_==
%%eV44	##E844!!(FC88$$UK88	"+u"4"4vv5v|<<''{GDD((66$ ,,UJ??M% 33MBB C ""5)44"Y//( " 
  
   	r   c                    |                                   |                                  |                                 }d|i}d}|                     |                     ||                    }|                     |d          }|                     |d          }i }t          dt          | j                            D ]}| j        |         }	||	||ddd||	<   |S )	aK  
        fetch the trading fees for multiple markets
        :see: https://api-docs-v3.idex.io/#get-api-account
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a dictionary of `fee structures <https://docs.ccxt.com/#/?id=fee-structure>` indexed by market symbols
        nonceNr   r   r   TF)r   r   r   r   r)  	tierBased)	check_required_credentialsr  uuidv1privateGetUserr  r   r   r   r  )
r   r   r_  r  r   r   r   r  r  r   s
             r   fetch_trading_feeszidex.fetch_trading_feesd  s     	'')))U
 &&t{{7F'C'CDD   >::  >::q#dl++,, 		 		A\!_F  "" F6NN r   c           	      X   |                                   |                     |          }|d         dd}|||d<   |                     |                     ||                    }|                     |d          }|dd||                     |d          |                     |d          d	S )
a.  
        fetches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
        :see: https://api-docs-v3.idex.io/#get-order-books
        :param str symbol: unified symbol of the market to fetch the order book for
        :param int [limit]: the maximum amount of order book entries to return
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        r)      )r   levelNr2  sequencebidsasks)r   r  r  r_  ri  rj  )r  r   publicGetOrderbookr  r   
parse_side)r   r   r2  r   r   r  r   r_  s           r   fetch_order_bookzidex.fetch_order_book  s     	V$$Tl
 
 $GG* **4;;w+G+GHH!!(J77OOHf55OOHf55
 
 	
r   c                 r   |                      ||g           }g }t          dt          |                    D ]d}||         }|                     |d          }|                     |d          }|                     |d          }	|                    |||	g           e|dk    }
|                     |d|
          S )Nr   r   rf  ri  )r   r   r   r   r   r   sort_by)r   bookrK  bookSider  r  rR  r   r   
orderCount
descendings              r   rl  zidex.parse_side  s    ??4r22q#h--(( 	7 	7AQKE$$UA..E%%eQ//F**5!44JMM5&*56666V^
||FAz222r   c                    |                      |          }i }t          dt          |                    D ]}||         }|                     |d          }|                     |d          }|                     |          }|                     |                     |                     |d                              }	|||d|dddd|	|	dd|	dddd||<   |S )	a  
        fetches all available currencies on an exchange
        :see: https://api-docs-v3.idex.io/#get-assets
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: an associative dictionary of currencies
        r   r*   r   exchangeDecimalsNr   )r   r{   )r)   coder   r   r*   r   depositr{   rM  r   r   )publicGetAssetsr   r   r   r   r   r   )
r   r   r   r  r  r  r*   
currencyIdrv  r   s
             r   fetch_currencieszidex.fetch_currencies  s    ''// q#h--(( 	 	AQKE##E622D))%::J**:66D))$*>*>t?O?OPUWi?j?j*k*kllI  &&/==(1$ ? ?  F4LL  r   c                    |d d d}t          dt          |                    D ]}||         }|                     |d          }|                     |          }|                                 }|                     |d          |d<   |                     |d          |d<   |                     |d          |d	<   |||<   |                     |          S )
N)r   r  r  r   assetrF  totalavailableForTradefreelockedused)r   r   r   r   accountsafe_balance)r   r   r  r  r  ry  rv  r  s           r   parse_balancezidex.parse_balance  s    
 

 q#h--(( 	# 	#AQKE))%99J**:66DllnnG#//zBBGG"..u6IJJGFO"..uh??GFO"F4LL  (((r   c                    |                                   |                                  |                                 }|| j        d}|                     ||          }|d         t          | j        dz             d}	 |                     |          }na# t          $ rT}t          |t                    r3|d         }|                     |           |                     |          }n|Y d}~nd}~ww xY w|                     |          S )aY  
        query for balance and get the amount of funds available for trading or funds locked in orders
        :see: https://api-docs-v3.idex.io/#get-balances
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        r_  walletr  NzM fetchBalance() wallet is None, set self.walletAddress or "address" in params)ra  r  rb  r   r  r   r)   privateGetBalances	Exceptionr7  r   associate_walletr  )r   r   nonce1r  extendedRequestr   er   s           r   fetch_balancezidex.fetch_balance	  s+    	'')))(
 
 ++gv668$,TW'vvwww	..??HH 	 	 	!^,,  / 9%%m44422?CC 		 !!(+++s   ?B 
C3A
C..C3c                 |   |                                   |                                  d}|                                 | j        d}| |                     |          }|d         |d<   |||d<   |||d<   |                     ||          }|d         t          | j        dz             d}	 |                     |          }na# t          $ rT}	t          |	t                    r3|d         }
|                     |
           |                     |          }n|	Y d}	~	nd}	~	ww xY w|                     ||||          S )	a  
        fetch all trades made by the user
        :see: https://api-docs-v3.idex.io/#get-fills
        :param str symbol: unified market symbol
        :param int [since]: the earliest time in ms to fetch trades for
        :param int [limit]: the maximum number of trades structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns Trade[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
        Nr  r)   r   r5  r2  r  zU fetchMyTrades() walletAddress is None, set self.walletAddress or "address" in params)ra  r  rb  r   r   r  r   r)   privateGetFillsr  r7  r   r  rB  )r   r   r1  r2  r   r   r  r  r   r  r   s              r   fetch_my_tradeszidex.fetch_my_trades/  sx    	'')))[[]](
 
 [[((F &tGH$GG$GG* ++gv668$,TW'~~	++O<<HH 	 	 	!^,,  / 9%%m444//@@ 		   65%@@@s   /C 
D#A
DD#r)   c           	      b    d|i}|                      |dd|                     ||                    S )au  
        fetches information on an order made by the user
        :see: https://api-docs-v3.idex.io/#get-orders
        :param str symbol: unified symbol of the market the order was made in
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        rQ  Nfetch_orders_helperr  )r   r)   r   r   r  s        r   fetch_orderzidex.fetch_orderj  s;     r
 ''dDKKQW<X<XYYYr   c           	      b    ddi}|                      ||||                     ||                    S )a  
        fetch all unfilled currently open orders
        :see: https://api-docs-v3.idex.io/#get-orders
        :param str symbol: unified market symbol
        :param int [since]: the earliest time in ms to fetch open orders for
        :param int [limit]: the maximum number of  open orders structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        closedFr  r   r   r1  r2  r   r  s         r   fetch_open_orderszidex.fetch_open_ordersw  s;     e
 ''udkk'SY>Z>Z[[[r   c           	      b    ddi}|                      ||||                     ||                    S )a*  
        fetches information on multiple closed orders made by the user
        :see: https://api-docs-v3.idex.io/#get-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 Order[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        r  Tr  r  s         r   fetch_closed_orderszidex.fetch_closed_orders  s;     d
 ''udkk'SY>Z>Z[[[r   c                    |                                   |                                 | j        d}d }| |                     |          }|d         |d<   |||d<   |||d<   |                     |                     ||                    }t          |t                    r|                     ||||          S | 	                    ||          S )Nr  r)   r   r5  r2  )
r  rb  r   r   privateGetOrdersr  r7  r8  parse_ordersparse_order)r   r   r1  r2  r   r  r   r   s           r   r  zidex.fetch_orders_helper  s    [[]](
 
 [[((F &tGH$GG$GG((Wf)E)EFF| h%% 	6$$XvueDDD##Hf555r   c                 >    ddddd}|                      |||          S )Nr%  canceledr  )r   partiallyFilledrejectedfilledr   r   r   statusess      r   parse_order_statuszidex.parse_order_status  s6     %"	
 
 &&999r   c           	      `   |                      |d          }|                     |dg           }|                     |d          }|                     |d          }|                     |d          }|                     |d          }|                     ||d          }	|                     |d          }
|                     |d	          }|                     |d
          }|                     |d          }|                     |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|          S )Nr   r   rQ  clientOrderIdr   rK  r  r   originalQuantityexecutedQuantityavgExecutionPricer   r   timeInForcer   r)   r  r  lastTradeTimestampr   postOnly	stopPricetriggerPricer   r   r+  )r  	remainingr   rM  r   )r   r   r   rT  safe_string_upperr  
safe_orderr0  )r   rR  r   r  r   r)   r  r  rK  r   r   r   r  r+  r   	rawStatusr  r   s                     r   r  zidex.parse_order  sW   D %%eV44	w33eY//((@@##E844v..!!(FC88v..!!%);<<!!%);<<""5*=>>  00$$UH55	,,UMBB((33  
E 
" 
 ] 
 	 

 Y// 
 !$ 
 f 
 D 
 ; 
  
 D 
 U 
  
 D 
 f 
  D! 
" w# 
$ - 
  
  
. /  	r   c                 t   |                                  }|                     |          }|                     |          |                     |          g}|                     |          }|                     |dd          }|                     || j                  }||d|d}	|                     |	          }
|
S )Nkeccakhexr  
parameters	signature)rb  remove0x_prefixbase16_to_binarybinary_concat_arrayhashsign_message_stringr   privatePostWallets)r   r   r   r_  noPrefix	byteArraybinaryr  r  r  r  s              r   r  zidex.associate_wallet:  s    ''66!!%((!!(++
	 )))44yy511,,T4?CC	 '  #
 
 ((11r   r   rK  r   r   c                     |                                   |                                  |                     |dd          }|                     |d          }|                     |          }|                                 }	d}
ddddd}d}|d	k    s
|d
k    sd|v r=d|vrt          | j        dz   |z   dz             |                     ||d                   }ddd}d}|	                                }|
                    d          dk    }||v r||         }
|                     ||          }nI||v r||         }
|                     ||          }n&|dk    rd}
nt          | j        dz   |z   dz             d}d|v r;|dk    rt          | j        dz   |z   dz             d}|                     |d          }|dk    rdnd}|                     | j                  }|                     | j        dd          }|                     |dddd          }|                     ||          }dddd}|                     | j        dd          }|                     |d |          }d}||v r	||         }nVt'          |                                          }d!                    |          }t          | j        dz   |z   d"z   |z             ddddd#}|                     | j        d$d%          }|                     |d&|          }d} ||v r	||         } nVt'          |                                          }d!                    |          }t          | j        dz   |z   d'z   |z             |                     |d          |                     |	          |                     |          |                     |d(                   |                     |
d          |                     |d          |                     |          |                     |d          g}!|r*|                     |          }"|!                    |"           ||v r,|                     |p|          }"|!                    |"           |                     |d)          }#|#(|!                    |                     |#                     |                     |d          |                     | d          |                     dd*          g}$|                     |!|$          }%|                     |%          }&|                     |&d+d,          }'|                     |'| j                  }(|	|d(         ||| j        |d-|(d.})|dk    r||)d/         d <   |r||)d/         d0<   ||v r|p||)d/         d<   |dk    r||)d/         d1<   n||)d/         d<   |#|#|)d/         d)<   d}*|r|                     |)          }*n|                      |)          }*| !                    |*|          S )2a8  
        create a trade order, https://docs.idex.io/#create-order
        :see: https://api-docs-v3.idex.io/#create-order
        :param str symbol: unified symbol of the market to create an order in
        :param str type: 'market' or 'limit'
        :param str side: 'buy' or 'sell'
        :param float amount: how much of currency you want to trade in units of base currency
        :param float [price]: the price at which the order is to be fullfilled, in units of the quote currency, ignored in market orders
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param bool [params.test]: set to True to test an order, no order will be created but the request will be validated
        :returns dict: an `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        r   FN            )stopLossstopLossLimit
takeProfittakeProfitLimitr  r  r  z5 createOrder() stopPrice is a required parameter for r   r   rf  )r2  
limitMakerr2  r   r    z is not a valid order typequoteOrderQuantityz7 createOrder() quoteOrderQuantity is not supported for z) orders, only supported for market ordersrI  r   r   )r   BSCr   )r   iocfokr   r   r  z, z2 is not a valid timeInForce, please choose one of )dccor   cbr   r   selfTradePreventionz: is not a valid selfTradePrevention, please choose one of r)   r     r  r  )r_  r   rK  r   r  r  r  r  r   rF  )"ra  r  	safe_boolomitr   rb  r   r)   r   lowerfindr   r   r  r   r   r   get_supported_mappingamount_to_precisionr8  keysjoinnumber_to_ber  encoder   array_concatr  r  r  r   privatePostOrdersTestprivatePostOrdersr  )+r   r   r   rK  r   r   r   	testOrderr   r_  typeEnumstopLossTypeEnumsstopPriceStringlimitTypeEnumsrW  	typeLower
limitOrder
amountEnumsideEnumwalletBytesr   orderVersionrX  timeInForceEnumsr   r  timeInForceEnum
allOptionsasStringselfTradePreventionEnumsr   r  selfTradePreventionEnumr  encodedPricer  afterallBytesr  r  r  r  r   s+                                              r   create_orderzidex.create_orderS  s    	'')))NN66599	66**V$$ 	
 
 O##1B)B)BW]H]H]6)) +b!bei!ilt!tuuu"55ff[>QRRO
 
 JJLL	^^G,,1
>!!%d+H11&%@@KK&&&(.H11&%@@KKXHHTWs]T14PPQQQ
6))x"47-f#fim#m  q\  $\  ]  ]  ]J%%f.BCCF11Q**4+=>>""4<EBB11'<
 <
  
 //?? 
 

 "--dl<PRWXX&&v}>PQQ***.{;OO.335566Jyy,,HTWs][8;oorzz{{{ 	$
 $
  &*%5%5dlD`bf%g%g""..v7LNhii"&":::&>?R&S##6;;==>>Jyy,,HTWs]-@@C  CK  K  L  L  LlA..!!%((!!+..KKt%%h**h**KK%%j!,,	
	  	+;;{33L\***$$$;;'E+FFL\***((AA$T[[77888oq115q99a##

 $$Y66))(33yy511,,T4?CC	  ,,':  #

 

 83>GL!-0 	9-8GL!'*$$$1@1OKGL!+.??0<GL!*--:FGL!"67$5BGL!/2@  	711'::HH--g66H&111r   rv  c                    |                      ||          \  }}|                                  |                                  |                                 }|                     ||          }|                     |          }|                     | j                  }	|                     |          |                     |	          | 	                    |d                   | 	                    |          | 
                    dd          g}
|                     |
          }|                     |dd          }|                     || j                  }|||d         |d|d}|                     |          }|                     ||          S )a  
        make a withdrawal
        :see: https://api-docs-v3.idex.io/#withdraw-funds
        :param str code: unified currency code
        :param float amount: the amount to withdraw
        :param str address: the address to withdraw to
        :param str tag:
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `transaction structure <https://docs.ccxt.com/#/?id=transaction-structure>`
        r)   r   r  r  )r_  r  r|  rF  r  )handle_withdraw_tag_and_paramsra  r  rb  currency_to_precisionrP  r  r   r  r  r  r  r  r  r   privatePostWithdrawalsparse_transaction)r   rv  r   addresstagr   r_  rX  rP  r  r  r  r  r  r  r   s                   r   r{   zidex.withdraw
  s    99#vFFV'')))11$??==&&**4+=>>!!%((!!+..KK''KK%%a##
	 )))44yy511,,T4?CC	 !!$(	  #
 
 ..w77 %%h999r   c                    |                                   |                                  d}||                     |          }|                                 }d|| j        di}|                     | j                  }|                     |          |                     |          g}|?|                    |                     |d                              |d         |d         d<   | 	                    |          }| 
                    |dd          }	|                     |	| j                  }
|
|d<   |                     |                     ||                    }|                     ||          S )	a  
        cancel all open orders
        :see: https://api-docs-v3.idex.io/#cancel-order
        :param str symbol: unified market symbol, only orders in the market of self symbol are cancelled when symbol is not None
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nr  r  r)   r   r  r  r  )ra  r  r   rb  r   r  r  r   r  r  r  r  r   privateDeleteOrdersr  r  )r   r   r   r   r_  r  r  r  r  r  r  r   s               r   cancel_all_orderszidex.cancel_all_orders>  sn    	'')))[[((F, 
 **4+=>>!!%((!!+..
	 T[[66777.4TlGL!(+)))44yy511,,T4?CC	(++DKK,H,HII  6222r   c                    |                                   |                                  d}||                     |          }|                                 }|                     | j                  }|                     |          |                     |          |                     |          g}|                     |          }| 	                    |dd          }	| 
                    |	| j                  }
|| j        |d|
d}|                     |                     ||                    }|                     |d          }|                     ||          S )a|  
        cancels an open order
        :see: https://api-docs-v3.idex.io/#cancel-order
        :param str id: order id
        :param str symbol: unified symbol of the market the order was made in
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: An `order structure <https://docs.ccxt.com/#/?id=order-structure>`
        Nr  r  )r_  r  rQ  r  r   )ra  r  r   rb  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   canceledOrders                 r   cancel_orderzidex.cancel_orderb  sS    	'')))[[((F**4+=>>!!%((!!+..KKOO
	
 )))44yy511,,T4?CC	 , 
 #
 
 ++DKK,H,HIIx33v666r   c
                     |                      |d          }
|                      |d          }|
<|                     | j        d         |
|           t          | j        dz   |z             d S )Nrv  messager   r  )r   throw_exactly_matched_exceptionr   r   r)   )r   rv  reasonurlmethodheadersbodyr   requestHeadersrequestBody	errorCoder
  s               r   handle_errorszidex.handle_errors  sp    $$Xv66	""8Y77 001I9V]^^^# 7888tr   c                     |                                   |                                 }|| j        |d}|                     |                     ||                    }|                     |          S )a}  
        fetch information on a deposit
        :see: https://api-docs-v3.idex.io/#get-deposits
        :param str id: deposit id
        :param str code: not used by idex fetchDeposit()
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `transaction structure <https://docs.ccxt.com/#/?id=transaction-structure>`
        )r_  r  	depositId)r  rb  r   privateGetDepositsr  r   r   r)   rv  r   r_  r  r   s          r   fetch_depositzidex.fetch_deposit  sr     	(
 

 **4;;w+G+GHH%%h///r   c                 b    |                      ddi|          }|                     ||||          S )a  
        fetch all deposits made to an account
        :see: https://api-docs-v3.idex.io/#get-deposits
        :param str code: unified currency code
        :param int [since]: the earliest time in ms to fetch deposits for
        :param int [limit]: the maximum number of deposits structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `transaction structures <https://docs.ccxt.com/#/?id=transaction-structure>`
        r  r  r  fetch_transactions_helperr   rv  r1  r2  r   s        r   fetch_depositszidex.fetch_deposits  sA     *
  --dE5&IIIr   c                 <    |                      |          }dddd|dS )aC  
        the latest known information on the availability of the exchange API
        :see: https://api-docs-v3.idex.io/#get-ping
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `status structure <https://docs.ccxt.com/#/?id=exchange-status-structure>`
        okN)r   updatedetar  r   )publicGetPingr   r   r   s      r   fetch_statuszidex.fetch_status  s5     %%f--
 
 	
r   c                 X    |                      |          }|                     |d          S )aF  
        fetches the current integer timestamp in milliseconds from the exchange server
        :see: https://api-docs-v3.idex.io/#get-time
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns int: the current integer timestamp in milliseconds from the exchange server
        
serverTime)publicGetTimer   r$  s      r   
fetch_timezidex.fetch_time  s/     %%f--   <888r   c                     |                                   |                                 }|| j        |d}|                     |                     ||                    }|                     |          S )a  
        fetch data on a currency withdrawal via the withdrawal id
        :see: https://api-docs-v3.idex.io/#get-withdrawals
        :param str id: withdrawal id
        :param str code: not used by idex.fetchWithdrawal
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `transaction structure <https://docs.ccxt.com/#/?id=transaction-structure>`
        )r_  r  withdrawalId)r  rb  r   privateGetWithdrawalsr  r   r  s          r   fetch_withdrawalzidex.fetch_withdrawal  sr     	(
 

 --dkk'6.J.JKK%%h///r   c                 b    |                      ddi|          }|                     ||||          S )a  
        fetch all withdrawals made from an account
        :see: https://api-docs-v3.idex.io/#get-withdrawals
        :param str code: unified currency code
        :param int [since]: the earliest time in ms to fetch withdrawals for
        :param int [limit]: the maximum number of withdrawals structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `transaction structures <https://docs.ccxt.com/#/?id=transaction-structure>`
        r  r,  r  r  s        r   fetch_withdrawalszidex.fetch_withdrawals  sA     -
  --dE5&IIIr   c                 (   |                                   |                                 }|| j        d}d }| |                     |          }|d         |d<   |||d<   |||d<   |d         }|                     |d          }d }	|dk    r*|                     |                     ||                    }	nG|dk    r*|                     |                     ||                    }	nt          | j	        d	z             | 
                    |	|||          S )
Nr  r)   r|  r5  r2  r  r  r,  z2 fetchTransactionsHelper() not support self method)r  rb  r   rP  r  r  r  r,  r   r)   parse_transactions)
r   rv  r1  r2  r   r_  r  rP  r  r   s
             r   r  zidex.fetch_transactions_helper  s4   (
 
 }}T**H'~GG$GG$GG !68,,)))..t{{7F/K/KLLHH...11$++gv2N2NOOHHtw)]]^^^&&x5%HHHr   c                 8    ddi}|                      |||          S )Nminedr   r  r  s      r   parse_transaction_statuszidex.parse_transaction_status  s(    T
 &&999r   rP  c                    d }d|v rd}n
d|v sd|v rd}|                      |dd          }|                     |d|          }|                     |                     |d          |          }|                     |d          }|                     |d          }|                     |d	d
          }d }	d|v r|                     |d          d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!S )"Nr  rw  
withdrawIdr+  
withdrawalr|  rF  txIdtxTimer   rM  r   rO  txStatusconfirmationTimer   r)   txidr  r  r   r  	addressToaddressFromr  tagTotagFromr   r   rP  r   r!  )commentinternalrM  )safe_string_2r   r   r   safe_integer_2r4  r   r0  )r   transactionrP  r   r)   rv  r   r<  r  rM  r  r   r!  s                r   r   zidex.parse_transaction  s4   N +%%DDk))~/L/LD[,GGk>2>>&&t'7'7W'M'MxXX!!+z::V44''XvFF	K((e<<! C $$[*==	..y99##K1CDD
K
"
 D
 	

 Y//
 t
 t
 
 4
 4
 T
 t
 D
 f
 
  f!
" w#
$ )
 
 
 	
r   c                     | j         d u}| j        d u}| j        d u}| j        d u}	|                     |dd          }
|o|o|o|	}|r|
dz  n|
S )Nr   r   rf  )r   r   r   r   r   )r   r   r  pathr   config	hasApiKey	hasSecrethasWalletAddresshasPrivateKeydefaultCostauthenticateds               r   calculate_rate_limiter_costz idex.calculate_rate_limiter_costl  sq    [,	[,	 .d:4ooffa88!ViV4DV#0A{QkAr   c                     i }|                                  |d<   |                     |                     ||                    }|                     |          S )aM  
        fetch the Polygon address of the wallet
        :see: https://api-docs-v3.idex.io/#get-wallets
        :param str code: not used by idex
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: an `address structure <https://docs.ccxt.com/#/?id=address-structure>`
        r_  )rb  privateGetWalletsr  parse_deposit_address)r   rv  r   r  r   s        r   fetch_deposit_addresszidex.fetch_deposit_addressu  sR     ;;==))$++gv*F*FGG ))(333r   c                     t          |          }|                     ||dz
            }|                     |d          }|                     |           |d |d ddS )Nr   r  r   )r   rP  r  r  r   )r   r  r   check_address)r   depositAddressrP  lengthr  r  s         r   rR  zidex.parse_deposit_address  so     ^$$~vz::""5)447###"
 
 	
r   r   GETc                    |                      | j        dd          }|                      | j        dd          }| j        d         |         dz   |z   dz   |z   }	t          |                                          }
t          |
          }d }|dk    r9|dk    r|                     |          }|	d	z   |z   }	n|                     |          }d
di}| j        
| j        |d<   |dk    r]d }|dk    r|}n|}| 	                    | 
                    |          | 
                    | j                  t          j        d          |d<   |	|||dS )Nr   r   r0   v1r   r   r   rX  ?zContent-Typezapplication/jsonzIDEX-API-Keyr   r  zIDEX-HMAC-Signature)r  r  r  r  )r   r   r   r8  r  r   	urlencodejsonr   hmacr  r   hashlibsha256)r   rG  r   r  r   r  r  r   r0   r  r  rW  querypayloads                 r   signz	idex.sign  sZ   ""4<EBB""4<DAAiw'#-7#=DFKKMM""TA::v..Ci%'yy((.
 ;"&*kGN#)G-1YYt{{77K7KT[[Y]YdMeMegnguw|-}-}G)*fdwOOOr   c                 6    |dd         dk    r
|dd          S |S )Nr   rf  0x )r   hexDatas     r   r  zidex.remove0x_prefix  s(    1Q3<4122;Nr   c                     |                      |                     |                    }|                     d|j        z             }d|                     |                     ||          dd          z   S )NzEthereum Signed Message:
re  r  r  )r  r  r  
byteLengthr  binary_concat)r   r
  binaryMessageprefixs       r   hash_messagezidex.hash_message  si    --d.B.B7.K.KLL=@XXYYdii 2 26= I I8UZ[[[[r   c                     |                      |dd          |dd          dd           }d|d         z   d|d         z   d|d         z   dS )	N	secp256k1re  rs   v)rq  rr  rt  )ecdsa)r   r  r   r  s       r   	sign_hashzidex.sign_hash  s]    JJtCDDz:cdd+;[$OO		#&	#&in$
 
 	
r   c                 d    |                      |                     |          |dd                    S )Nro  )rv  rm  )r   r
  r   s      r   sign_messagezidex.sign_message  s-    ~~d//88*STT:JKKKr   c                     |                      ||          }|d         |                     |d                   z   |                     |                     |d         d                    z   S )Nrq  rr  rt  r   )rx  r  binary_to_base16r  )r   r
  r   r  s       r   r  zidex.sign_message_string  s     %%gz::	~ 4 4Ys^ D DDtG\G\]a]n]noxy|o}  @A  ^B  ^B  HC  HC  C  	Cr   )N)H__name__
__module____qualname__r   r   r   r	   r  strr   r  r   r   r  r  r   r8  r;  r?  r   rC  r]  r   rd  r   rm  rl  r   rz  r   r  r  r   r  r  r   r  r  r  r  r  r  r   r   floatr
   r  r{   r  r  r  r  r   r  r%  r)  r-  r/  r  r4  r   r   rO  rS  rR  rc  r  rm  rv  rx  r  __classcell__)r   s   @r   r'   r'      s       e e e e eN
n 
n 
n $& } }$v, } } } }~ 02 !1 !13 !1f !1 !1 !1 !1F 04B 5 5W 57 5 5 5 5<+ +6 +V + + + +Z 26DW[df % %# %c %QT %koptku % % % %N; ; ;4 ; ; ; ;$ 6:UW A A3 As A# A\`af\g A A A ABJ J J5 J J J JX )+ ) ){ ) ) ) )V :>b .
 .
s .
3 .
Y .
 .
 .
 .
`
3 
3 
3 ') + +Z + + + +Z) ) ) ) )" $& $, $,( $, $, $, $,L -1tRV_a 9A 9Ac 9A 9AC 9A 9A 9A 9Av 26b Z Zc Z3 Z Z Z Z /3TXac \ \ \3 \c \hlmrhs \ \ \ \ 154VZce \ \# \S \PS \jnotju \ \ \ \ 154VZce O6 O6# O6S O6PS O6 O6 O6 O6b: : :H H H5 H H H HT 68    2 gksu u2 u23 u2i u2y u2RW u2`c u2 u2 u2 u2n ?C2 2: 2:S 2:% 2: 2: 2: 2:h /32 "3 "3 "3 "3 "3 "3H 37r #7 #7s #7C #7 #7 #7 #7J   26b 0 0 03 0 0 0 0& *.Dt\^ J J3 Jc J Jcghsct J J J J #% 
 
 
 
  !# 9 9 9 9 59 0 03 0c 0 0 0 0& -1tRV_a J Jc J JC Jfjkvfw J J J J 59tZ^gi "I "Ic "I "ITW "I "I "I "IH: : :P
 P
x P
; P
 P
 P
 P
d MO B B B B 15R 4 4# 4 4 4 46
 
h 
 
 
 
6 &eBSW P P P P6  \ \ \
 
 
L L LC C C C C C Cr   r'   ),ccxt.base.exchanger   ccxt.abstract.idexr   r_  ccxt.base.typesr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   typingr   ccxt.base.errorsr   r   r   r   r   r   r   r   r   ccxt.base.decimal_to_precisionr    r!   r"   r#   r$   ccxt.base.preciser%   r'   rf  r   r   <module>r     s   ( ' ' ' ' ' * * * * * *  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u  u       * * * * * * 0 0 0 0 0 0 ' ' ' ' ' ' . . . . . . + + + + + + ) ) ) ) ) ) ) ) ) ) ) ) + + + + + + 1 1 1 1 1 1 0 0 0 0 0 0 3 3 3 3 3 3 9 9 9 9 9 9 4 4 4 4 4 4 8 8 8 8 8 8 % % % % % %EC EC EC EC EC8[ EC EC EC EC ECr   