
    i_                         d dl Zd dlmZmZ d dl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  G d dej        j                  ZdS )	    N)
ArrayCacheArrayCacheBySymbolById)BalancesInt	OrderBookStrTickerTrade)Client)List)NotSupportedc            
       J    e Zd Z fdZd Zi fdefdZdefdZ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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d Zd ZdefdZdefdZdefdZdefdZi fdZ  xZ!S )exmoc                     |                      t          t          |                                           dddddddddd	dddddd	iii i i d
          S )NTF)	wswatchBalancewatchTickerwatchTickerswatchTradeswatchMyTradeswatchOrderswatchOrderBook
watchOHLCVapir   z#wss://ws-api.exmo.com:443/v1/publicz$wss://ws-api.exmo.com:443/v1/privatez+wss://ws-api.exmo.com:443/v1/margin/private)publicspotmargin)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describe)self	__class__s    S/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/exmo.pyr%   zexmo.describe   s    dD 1 1 : : < < $# %#!%$"&#
 
 "G F"O 3?
 ?
   	    c                 z    |                      |                     | j        dd          d          }|| j        d<   |S )N	requestIdr      )sumsafe_integerr    )r&   r+   s     r(   
request_idzexmo.request_id/   s;    HHT..t|[!LLaPP	$-[!r)   returnc                 N  K   |                      |           d{V  |                     dd|          \  }}d|z   }| j        d         d         |         }d|dz   g|                                 d}|                     ||          }|                     |||||           d{V S )	a  
        watch balance and get the amount of funds available for trading or funds locked in orders
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr   balance:r   r   	subscribez/walletmethodtopicsid)authenticatehandle_market_type_and_paramsr   r/   r#   watch)r&   paramstypequerymessageHashurlr3   requests           r(   watch_balancezexmo.watch_balance4   s       '''''''''88vVVe 4'it$T*!i'(//##
 
	
 ""9e44ZZ[';PPPPPPPPPr)   clientc                 6   |                      |d          }|                    d          }|                      |d          }|dk    r|                     |           n|dk    r|                     |           d|z   }|                    | j        |           d S )Ntopic/r   r   r   r2   )safe_stringsplitparse_spot_balanceparse_margin_balanceresolvebalance)r&   rB   messagerD   partsr<   r>   s          r(   handle_balancezexmo.handle_balanceF   s    h   '22C  q))6>>##G,,,,X%%g... 4't|[11111r)   c                 x   |                      |d          }|                     |d          }|| j        d<   |dk    r|                     |di           }|                     |di           }t          |                                          }t          dt          |                    D ]o}||         }|                     |          }	|                                 }
|                      ||          |
d<   |                      ||          |
d	<   |
| j        |	<   pn|d
k    r{|                      |d          }|                     |          }	|                                 }
|                      |d          |
d<   |                      |d          |
d	<   |
| j        |	<   | 	                    | j                  | _        d S )Neventdatainfosnapshotbalancesreservedr   freeusedupdatecurrencyrK   )
rF   
safe_valuerK   listkeysrangelensafe_currency_codeaccountsafe_balance)r&   rL   rP   rQ   rT   rU   
currenciesi
currencyIdcoder`   s              r(   rH   zexmo.parse_spot_balance   s      '22w//#VJtZ<<HtZ<<Hhmmoo..J1c*oo.. - -']
..z::,,.."&"2"28Z"H"H"&"2"28Z"H"H%,T""- h))$
;;J**:66DllnnG"..tY??GFO"..tZ@@GFO!(DL((66r)   c                 >   |                      |d          }|| j        d<   t          |                                          }t	          dt          |                    D ]}||         }|                     |          }|                      ||          }|                                 }|                     |d          |d<   |                     |d          |d<   |                     |d          |d<   || j        |<   | 	                    | j                  | _        d S )NrQ   rR   r   rV   rW   rK   total)
rZ   rK   r[   r\   r]   r^   r_   r`   rF   ra   )	r&   rL   rQ   rb   rc   rd   re   walletr`   s	            r(   rI   zexmo.parse_margin_balance   s    w//#V$))++&&
q#j//** 		; 		;A#AJ**:66D__T:66FllnnG"..vv>>GFO"..vv>>GFO#//	BBGG!(DL,,T\::DLL		; 		;r)   symbolc                 ^  K   |                                   d{V  |                     |          }|d         }| j        d         d         d         }d|z   }dd|d	         z   g|                                 d
}|                     ||          }|                     |||||           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>`
        Nri   r   r   r   ticker:r3   zspot/ticker:r7   r4   )load_marketsmarketr   r/   r#   r:   )r&   ri   r;   rm   r?   r>   rL   r@   s           r(   watch_tickerzexmo.watch_ticker   s       !!!!!!!!!V$$!it$X.&(!- //##
 
 ""7F33ZZ[';PPPPPPPPPr)   c                    |                      |d          }|                    d          }|                      |d          }|                     |          }|                     |di           }|                     |          }|                     ||          }	d|z   }
|	| j        |<   |                    |	|
           d S )NrD   :r,   rQ   rk   )rF   rG   safe_symbolrZ   safe_marketparse_tickertickersrJ   )r&   rB   rL   rD   
topicPartsmarketIdri   tickerrm   parsedTickerr>   s              r(   handle_tickerzexmo.handle_ticker   s    (   '22[[%%
##J22!!(++&"55!!(++((88&(+V|[11111r)   Nsincelimitc                   K   |                                   d{V  |                     |          }|d         }| j        d         d         d         }d|z   }dd|d	         z   g|                                 d
}|                     ||          }	|                     |||	||	           d{V }
|                     |
||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>`
        Nri   r   r   r   trades:r3   zspot/trades:r7   r4   	timestampT)rl   rm   r   r/   r#   r:   filter_by_since_limit)r&   ri   rz   r{   r;   rm   r?   r>   rL   r@   tradess              r(   watch_tradeszexmo.watch_trades   s       !!!!!!!!!V$$!it$X.&(!- //##
 
 ""7F33zz#{G['RRRRRRRR))&%TRRRr)   c                    |                      |d          }|                    d          }|                      |d          }|                     |          }|                     |          }|                     |dg           }d|z   }	|                     | j        |          }
|
5|                     | j        dd          }t          |          }
|
| j        |<   t          dt          |                    D ]5}||         }|                     ||          }|
                    |           6|
| j        |<   |                    | j        |         |	           d S )	NrD   rp   r,   rQ   r}   tradesLimit  r   )rF   rG   rq   rr   rZ   r   r.   r    r   r]   r^   parse_tradeappendrJ   )r&   rB   rL   rD   rM   rv   ri   rm   r   r>   storedr{   rc   tradeparseds                  r(   handle_tradeszexmo.handle_trades  sO       '22C  ##E1--!!(++!!(++&"55&(f55>%%dlM4HHE&&F"(DKq#f++&& 	" 	"A1IE%%eV44FMM&!!!!$Ft{6*K88888r)   c                   K   |                                   d{V  |                     |           d{V  |                     dd|          \  }}| j        d         d         |         }d}|d|z   }n(|                     |          }	|	d         }d|	d         z   }d|dz   g|                                 d	}
|                     |
|          }|                     |||||           d{V }|                     ||||d
          S )a  
        get the list of trades associated with the user
        :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>`
        Nr   r   r   	myTrades:ri   r3   z/user_tradesr4   T)	rl   r8   r9   r   rm   r/   r#   r:   filter_by_symbol_since_limit)r&   ri   rz   r{   r;   r<   r=   r?   r>   rm   rL   r@   r   s                r(   watch_my_tradeszexmo.watch_my_trades8  sM      !!!!!!!!!'''''''''88$PVWWeit$T*>%,KK[[((FH%F%x(88K!~% //##
 
 ""7E22zz#{G['RRRRRRRR00tTTTr)   c                 v   |                      |d          }|                    d          }|                      |d          }d|z   }|                      |d          }g }d }	| j        3|                     | j        dd          }
t          |
          }	|	| _        n| j        }	|dk    r|                     |d	g           }n |d
k    r|                     |d	i           }|g}|                     |          }i }t          dt          |                    D ]*}||         }|	
                    |           d||d         <   +t          |                                          }t          dt          |                    D ]%}||         }d|z   }|                    |	|           &|                    |	|           d S )NrD   rE   r   r   rP   r   r   rS   rQ   rX   Tri   )rF   rG   myTradesr.   r    r   rZ   parse_tradesr]   r^   r   r[   r\   rJ   )r&   rB   rL   rD   rM   r<   r>   rP   	rawTradesr   r{   rawTrader   symbolsjr   
symbolKeysrc   ri   symbolSpecificMessageHashs                       r(   handle_my_tradeszexmo.handle_my_tradesW  s   t   '22C  q))!D(  '22	= %%dlM4HHE-e44H$DMM}HJ<<IIhw;;H!
I""9--q#f++&& 	, 	,A1IEOOE"""'+GE(O$$',,..))
q#j//** 	@ 	@A]F(3f(<%NN8%>????x-----r)   c                   K   |                                   d{V  |                     |          }|d         }| j        d         d         d         }d|z   }|                     |d          }d|                                 d	|d
         z   g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
        Nri   r   r   r   
orderbook:aggregationr3   zspot/order_book_updates:r7   )r5   r7   r6   )rl   rm   r   omitr/   r#   r:   r{   )
r&   ri   r{   r;   rm   r?   r>   r3   r@   	orderbooks
             r(   watch_order_bookzexmo.watch_order_book  s       !!!!!!!!!V$$!it$X."V+6=11!//##*VD\9
 
	 ""9f55**S+wLLLLLLLL	   r)   c                 V   |                      |d          }|                    d          }|                      |d          }|                     |          }|                     |di           }d|z   }|                     |d          }	|                     | j        |          }
|
|                     i           }
|
| j        |<   |                      |d          }|dk    r/|                     |||	d	d
          }|
                    |           n|                     |d
g           }|                     |d	g           }| 	                    |
d         |           | 	                    |
d         |           |	|
d<   | 
                    |	          |
d<   |                    |
|           d S )NrD   rp   r,   rQ   r   tsrP   rS   bidaskasksbidsr~   datetime)rF   rG   rq   rZ   r.   
orderbooks
order_bookparse_order_bookresethandle_deltasiso8601rJ   )r&   rB   rL   rD   rM   rv   ri   	orderBookr>   r~   r   rP   rS   r   r   s                  r(   handle_order_bookzexmo.handle_order_book  s   D   '22C  ##E1--!!(++OOGVR88	"V+%%gt44	OODOV<<	++I&/DOF#  '22J,,Y	5RWXXHOOH%%%%??9eR88D??9eR88Dy0$777y0$777%.Ik"$(LL$;$;Ij!y+.....r)   c                 ^    |                      |dd          }|                    |           d S )Nr   r,   )parse_bid_ask
storeArray)r&   booksidedeltabidAsks       r(   handle_deltazexmo.handle_delta  s3    ##E1a00F#####r)   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )r]   r^   r   )r&   r   deltasrc   s       r(   r   zexmo.handle_deltas  sH    q#f++&& 	3 	3Ahq	2222	3 	3r)   c           
      Z   |                      |d          }| j        | j        | j        d}|                     ||          }| |||           d S |dk    s|dk    r|                      |d          }||                    d          }|                      |d          }| j        | j        | j        | j        | j        | j        | j	        | j
        | j
        d	}	|                     |	|          }
|
 |
||           d S t          | j        d	z   |                     |          z             )
NrP   )	logged_inrR   
subscribedrX   rS   rD   rp   r   )	zspot/tickerzspot/walletzmargin/walletzmargin/walletszspot/tradeszmargin/tradeszspot/order_book_updateszspot/user_tradeszmargin/user_tradesz" received an unsupported message: )rF   handle_authentication_messagehandle_infohandle_subscribedrZ   rG   ry   rN   r   r   r   r   r7   json)r&   rB   rL   rP   eventseventHandlerrD   rM   channelhandlershandlers              r(   handle_messagezexmo.handle_message  s]       '22;$0
 

 vu55#L)))FX5J#6#6$$Wg66E C((**5!44#'#5#'#6%)%8&*&9#'#5%)%7/3/E )-(=*.*?  //(G<<&GFG,,,F47%IIDIIV]L^L^^___r)   c                     |S N r&   rB   rL   s      r(   r   zexmo.handle_subscribed=  s	     r)   c                     |S r   r   r   s      r(   r   zexmo.handle_infoG  s	     r)   c                 6    d}|                     ||           d S )Nauthenticated)rJ   )r&   rB   rL   r>   s       r(   r   z"exmo.handle_authentication_messageS  s#     &w,,,,,r)   c                   K   d}|                      dd |          \  }}| j        d         d         |         }|                     |          }|                     |j        |          }||                                 }|                                  |                                 }	| j        t          |          z   }
| 
                    |                     |
          |                     | j                  t          j        d          }d|	| j        ||d}|                     ||          }|                     ||||           d {V }||j        |<   |S )Nr   r8   r   r   base64login)r5   r7   api_keysignnonce)r9   r   rB   rZ   subscriptionsmillisecondscheck_required_credentialsr/   apiKeystrhmacencodesecrethashlibsha512extendr:   )r&   r;   r>   r<   r=   r?   rB   futuretimer+   signDatar   r@   rL   s                 r(   r8   zexmo.authenticate`  sM     %88vVVeit$T*S!!!5{CC>$$&&D++---))I{SYY.H99T[[22DKK4L4Lgn^fggD!; G kk'511G::c;MMMMMMMMF06F -r)   )"__name__
__module____qualname__r%   r/   r   rA   r   rN   rH   rI   r   r	   rn   ry   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   __classcell__)r'   s   @r(   r   r      s           <  
 *, Q Q Q Q Q Q$<2V <2 <2 <2 <2|$7 $7 $7L; ; ;: 68 Q Q QF Q Q Q Q,2F 2 2 2 2> <@d[] S S SS S Sbfglbm S S S S2!9F !9 !9 !9 !9F 37TX\eg U UC Us URU Ulpqvlw U U U U>W.v W. W. W. W.r @DB ! !S ! !S\ ! ! ! !28/ 8/ 8/ 8/ 8/t$ $ $3 3 30`V 0` 0` 0` 0`d    
& 
 
 
 
-F - - - - )+        r)   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   async_supportr   r   r)   r(   <module>r      s        O O O O O O O O  H H H H H H H H H H H H H H H H 4 4 4 4 4 4       ) ) ) ) ) )g	 g	 g	 g	 g	4" g	 g	 g	 g	 g	r)   