
    iSW                         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 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ArrayCacheByTimestamp)BalancesInt	OrderBookTickerTrade)Client)List)Precisec            
       @    e Zd Z fdZd ZdefdZdefdZdefdZddZ	defd	Z
dd
ZdefdZd Zi fdZi fdZi fdefdZi fdedefdZddi fdedededee         fdZdi fde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 ZdefdZdefdZ xZ S )currencycomc                     |                      t          t          |                                           dddddddddddiidddd	d
ddddddd	d| j        ddd          S )NTF)wswatchBalancewatchTickerwatchTickerswatchTradeswatchOrderBook
watchOHLCVapir   z.wss://api-adapter.backend.currency.com/connect  M1M3M5M15M30H1H4D1W1)	1m3m5m15m30m1h4h1d1w)tradesLimit
OHLCVLimit
timeframesi N  )ping	keepAlive)hasurlsoptions	streaming)deep_extendsuperr   describer.   )self	__class__s    Z/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/currencycom.pyr6   zcurrencycom.describe   s    k4 8 8 A A C C $# %#"& #
 
 J  $"   
 
	 & 	"	 C'F
 'F
 ' ' '	    c                 P    t          |                                           }d|i dS )Nr.   destinationcorrelationIdpayload)str
request_id)r7   client	requestIds      r9   r.   zcurrencycom.ping;   s1    ))**	!&
 
 	
r:   rB   c                 8    |                                  |_        |S N)millisecondslastPong)r7   rB   messages      r9   handle_pongzcurrencycom.handle_pongD   s    ++--r:   c                 $   |                      |d          }|                     |          }|                     | j        |          | _        |                     |d          }|                    | j        |           ||j        v r
|j        |= d S d S )Nr?   messageHash)
safe_valueparse_balanceextendbalancesafe_stringresolvesubscriptions)r7   rB   rH   subscriptionr?   rO   rK   s          r9   handle_balancezcurrencycom.handle_balanceH   s    F //'955$$W--{{4<99&&|]CCt|[111&...$[111 /.r:   c                 \   d}|                      |d          }|                      |dg           }t          dt          |                    D ]^}|                     ||                   }|d         }	|| j        |	<   |dz   |	z   }
|                    ||
           |
|j        v r|j        |
= _d S )N/api/v1/ticker/24hrr?   tickersr   symbol:)rL   rangelenparse_tickerrW   rQ   rR   )r7   rB   rH   rS   r=   r?   rW   itickerrX   rK   s              r9   handle_tickerzcurrencycom.handle_tickers   s    : ,//'955//'9b99q#g,,'' 	6 	6A&&wqz22FH%F#)DL %+f4KNN6;///f222(5	6 	6r:   Nc                 Z   |                      |d          }|                     |d d          }|                     |d          }|                      |d          }|                      |d          }|                     t	          j        ||                    }|                     |          }	|                     |          }
|                      |d          }|                      |d          }|                     |d          }|rd	nd
}|||                     |          |||d d ||	|
|d dS )NrX   /tspricesizeidorderIdbuyerbuysell)info	timestampdatetimerX   re   ordertypetakerOrMakersiderc   amountcostfee)rP   safe_symbolsafe_integerparse_numberr   
string_mulrL   iso8601)r7   trademarketmarketIdrX   rk   priceStringamountStringrr   rc   rq   re   rf   rg   rp   s                  r9   handle_tradezcurrencycom.handle_trade   sB    ##E844!!(D#66%%eT22	&&ug66''v66  !3K!N!NOO!!+..""<00eT**""5)44w//)uu6"Y// 
 
 	
r:   c                 z   |                      |d          }|                     |          }|d         }d}|dz   |z   }|                      | j        |          }|5|                     | j        dd          }	t          |	          }|| j        |<   |                    |           |                    ||           d S )Nr?   rX   trades.subscriberY   r+   r   )rL   r~   tradesru   r2   r   appendrQ   )
r7   rB   rH   r?   parsedrX   r=   rK   storedlimits
             r9   handle_tradeszcurrencycom.handle_trades   s    " //'955""7++!(!C'&0f55>%%dlM4HHE&&F"(DKfv{+++++r:   c                     |                      | j        d|          }t          |                                          }t	          dt          |                    D ]}||         }||         |k    r|c S d S )Nr-   r   )rL   r2   listkeysrZ   r[   )r7   	timeframedefaultTimeframesr-   r   r]   keys          r9   find_timeframezcurrencycom.find_timeframe   s{    __T\<ARSS
JOO%%&&q#d))$$ 	 	Aq'C#)++


 ,tr:   c                 B   d}|                      |di           }|                     |d          }|                     |          }|                     |d          }|                     |          }|d         }	|dz   |z   dz   |	z   }
|                     |d          |                     |d          |                     |d          |                     |d	          |                     |d
          d g}|                      | j        |	i           | j        |	<   |                      | j        |	         |          }|;|                     | j        dd          }t          |          }|| j        |	         |<   |	                    |           |
                    ||
           d S )NOHLCMarketData.subscriber?   intervalrX   rY   tohlcr,   r   )rL   rP   r   safe_marketru   safe_numberohlcvsr2   r   r   rQ   )r7   rB   rH   r=   r?   r   r   r{   rz   rX   rK   resultr   r   s                 r9   handle_ohlcvzcurrencycom.handle_ohlcv   s   " 1//'9b99##GZ88''11	##GX66!!(++!!C')3c9FBgs++Wc**Wc**Wc**Wc**
 #oodk62FFFV!4i@@>%%dlL$GGE*511F-3DK	*fv{+++++r:   c                 z    |                      |                     | j        dd          d          }|| j        d<   |S )Nr>   r      )sumru   r2   )r7   reqids     r9   rA   zcurrencycom.request_id  s:    **4<!LLaPP(-_%r:   c                   K   |                                   d {V  |                     |          }|d         }|dz   |z   }| j        d         d         }t          |                                           }|                     ||d|d         gid|          }|                     |||d          }	|                     |||||	           d {V S )	NrX   rY   r   r   symbolsre   r<   rK   rX   load_marketsrz   r1   r@   rA   r4   rN   watch)
r7   r=   rX   paramsrz   rK   urlrC   requestrS   s
             r9   watch_publiczcurrencycom.watch_public  s     !!!!!!!!!V$$!!C'&0it$))**	""&&F4L>$
 $
   {{7&-
 -
   ZZ[';UUUUUUUUUr:   c                 z  K   |                                   d {V  d}| j        d         d         }t          |                                           }|                                 | j        d}|                     |                     |                    }|                     |||d|          }| 	                    | 
                    |          | 
                    | j                  t          j                  |d         d<   |                     |d|i          }	|                     |||||	           d {V S )	N/api/v1/accountr   r   )rk   apiKeyr<   r?   	signaturerK   )r   r1   r@   rA   rF   r   	urlencodekeysortr4   hmacencodesecrethashlibsha256rN   r   )
r7   r=   r   rK   r   rC   r?   authr   rS   s
             r9   watch_privatezcurrencycom.watch_private0  sQ     !!!!!!!!!'it$))**	**,,k
 
 ~~dll73344""&&$
 $
 	 
 +/))DKK4E4Et{{SWS^G_G_ahao*p*p	;'{{7;-
   ZZ[';UUUUUUUUUr:   returnc                 r   K   |                                   d{V  |                     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   )r   r   )r7   r   s     r9   watch_balancezcurrencycom.watch_balanceE  sU       !!!!!!!!!''(96BBBBBBBBBr:   rX   c                   K   |                                   d{V  |                     |          }|d         }d}|dz   |z   }| j        d         d         }t          |                                           }|                     ||d|d         id|          }|                     |||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>`
        NrX   rV   rY   r   r   re   r<   r   r   )
r7   rX   r   rz   r=   rK   r   rC   r   rS   s
             r9   watch_tickerzcurrencycom.watch_tickerN  s      !!!!!!!!!V$$!+!C'&0it$))**	""&&&,$
 $
   {{7&-
 -
   ZZ[';UUUUUUUUUr:   sincer   c                 
  K   |                                   d{V  |                     |          }|                     d||           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>`
        Nr   rk   T)r   rX   r   
newUpdatesgetLimitfilter_by_since_limit)r7   rX   r   r   r   r   s         r9   watch_tradeszcurrencycom.watch_tradesi  s       !!!!!!!!!V$$(();VVLLLLLLLL? 	3OOFE22E))&%TRRRr:   c                    K   |                                   d{V  |                     |          }|                     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
        NdepthMarketData.subscribe)r   rX   r   r   )r7   rX   r   r   	orderbooks        r9   watch_order_bookzcurrencycom.watch_order_booky  sx       !!!!!!!!!V$$++,GQWXXXXXXXX	   r:   r"   c                   K   |                                   d{V  |                     |          }d}|dz   |z   }|                     | j        d          }|d||         gid}	|                     |||                     |	|                     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
        Nr   rY   r-   	intervals)r=   r?   r   T)	r   rX   rL   r2   r   rN   r   r   r   )r7   rX   r   r   r   r   r=   rK   r-   r   ohlcvs              r9   watch_ohlcvzcurrencycom.watch_ohlcv  s       !!!!!!!!!V$$0!C')3__T\<@@
&y)
 
 ''VT[[RX=Y=YZZZZZZZZ? 	2NN6511E))%q$GGGr:   c                    t          |                                          }t          dt          |                    D ]B}||         }||         }|                    t          |          t          |                     Cd S )Nr   )r   r   rZ   r[   storefloat)r7   booksidedeltaspricesr]   rc   rq   s          r9   handle_deltaszcurrencycom.handle_deltas  su    fkkmm$$q#f++&& 	8 	8A1IEE]FNN5<<v7777	8 	8r:   c                    |                      |di           }|                      |di           }|                     |d          }|                     |d d          }d}|dz   |z   }|                     |d          }	|                      | j        |          }
|
|                                 }
|
                    ||	|                     |	          d           |                      |d	i           }|                      |d
i           }|                     |
d         |           |                     |
d         |           |
| j        |<   |	                    |
|           d S )Nr?   datarX   ra   r   rY   rb   )rX   rk   rl   bidofrbidsasks)
rL   rP   rt   ru   
orderbooks
order_bookresetrx   r   rQ   )r7   rB   rH   r?   r   r{   rX   r=   rK   rk   r   r   r   s                r9   handle_order_bookzcurrencycom.handle_order_book  sj    //'9b99w33##GX66!!(D#661!C'&0%%dD11	OODOV<<	))I"Y//
 
 	 	 	
 tUB//tUB//9V,d3339V,d333"+y+.....r:   c                 *   |                      |d          }||                     |j        d          }|                      |d          }|                     ||          }|V|dk    rP|                      |d          }|8| j        | j        d}|                     ||          }	|	d S  |	|||           d S |                      |d          }
|
A| j        | j        | j        | j	        d}|                     ||
          }	|	 |	||           d S d S d S )Nr>   statusOKr=   )rV   r   )zmarketdepth.eventzinternal.tradez
ohlc.eventr.   )
rP   index_byrR   rL   r_   rT   r   r   r   rI   )r7   rB   rH   rC   subscriptionsByIdr   rS   subscriptionDestinationmethodsmethodr=   s              r9   handle_messagezcurrencycom.handle_message  s^   ^ $$Wo>>	  $f.BO T T%%gx88F??+<iHHL'T>>.2.>.>|].[.[+.:373E/3/B# # "&:Q!R!R!>"F"F67LAAA"F&&w>>"%)%;"&"4"/(	 G __Wk::F!vw''''' #" "!r:   rE   )!__name__
__module____qualname__r6   r.   r
   rI   rT   r_   r~   r   r   r   rA   r   r   r   r   r@   r   r   r   r   r	   r   r   r   r   r   r   r   r   __classcell__)r8   s   @r9   r   r      s       ( ( ( ( (T
 
 
&    )2V )2 )2 )2 )2V'6F '6 '6 '6 '6R'
 '
 '
 '
R,F , , , ,>   (,6 (, (, (, (,T  
 >@ V V V V( 79 V V V V* *, C C C C C C 68 V V VF V V V V6 <@d[] S S SS S Sbfglbm S S S S  @DB ! !S ! !S\ ! ! ! ! 8<$]ajl H H HC HWZ Hquvzq{ H H H H88 8 8 /  /  /  /  /DL(V L( L( L( L( L( L( L( L(r:   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r   r	   !ccxt.async_support.base.ws.clientr
   typingr   ccxt.base.preciser   async_supportr    r:   r9   <module>r      s        N N N N N N N N  C C C C C C C C C C C C C C 4 4 4 4 4 4       % % % % % %H( H( H( H( H($$0 H( H( H( H( H(r:   