
    eiJ~                         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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  G d
 dej        j                  ZdS )    N)
ArrayCacheArrayCacheByTimestamp)AnyBoolIntMarket	OrderBookStringsTickerTickersFundingRateFundingRatesTrade)Client)List)ExchangeError)AuthenticationError)ArgumentsRequired)NotSupportedc                       e Zd Zdef fdZdi fdedefdZdi fdedefdZi fdede	fd	Z
di fdedefd
Zi fdedefdZd Zdi fdedefdZdefdZd(defdZdefdZd(dededefdZdddi fdedededede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di fdee         dededee         fdZ!d Z"d(dZ#di fdedede$fdZ%di fdee         dede$fd Z&d! Z'd" Z(d# Z)d$ Z*defd%Z+dede,fd&Z-d' Z. xZ/S ))coinbaseinternationalreturnc                 :   |                      t          t          |                                           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	d	d	d	d	d	d	dddiddidddiddddddddd d!d"d#d$t          iid%          S )&NwsTwatchTradeswatchTradesForSymbolswatchOrderBookwatchOrderBookForSymbolswatchTickerwatchBalanceFwatchMyTrades
watchOHLCVwatchOHLCVForSymbolswatchOrderswatchOrdersForSymbolswatchPositionswatchTickerscreateOrderWseditOrderWscancelOrderWs)cancelOrdersWscancelAllOrdersWsfetchOrderWsfetchOrdersWsfetchBalanceWsfetchMyTradesWsz&wss://ws-md.international.coinbase.comzwss://ws-md.n5e2.coinbase.com)apitestchannelLEVEL1  CANDLES_ONE_MINUTECANDLES_FIVE_MINUTESCANDLES_THIRTY_MINUTESCANDLES_ONE_HOURCANDLES_TWO_HOURSCANDLES_ONE_DAY)1m5m30m1h2h1d)r   tradesLimitordersLimitmyTradesLimit
timeframesexactzUnable to authenticate)hasurlsoptions
exceptions)deep_extendsuperr   describer   )self	__class__s    \/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/coinbaseinternational.pyrM   zcoinbaseinternational.describe   s   &;T B B K K M Mdt ( !$	
 +D t    d ' u ( !%     u!"  #$ #(%* %!&"'#(/  6 B 9	  x   $#!%.03,-+  " ,.Ae7P
 7P
 7 7 7	    Nnamesymbolsc                 |  K   |                                   d{V  |                                  d}|}d}||                                 }t          |          }g }|dk    rn|                     |          }	|                     |	          }
|
}t          dt          |	                    D ]#}|                    |dz   |	|         z              $n8|dk    r2|                     |d                   }|dz   |d         z   }|d         g}| j	        d         d         }|t          | j        d	z             t          |                                           }|| j        z   d
z   | j        z   }|                     |                     |          |                     | j                  t(          j        d          }d|g|| j        | j        |d}|||d<   |dk    r2|                     |||                     ||          |           d{V S |                     |||                     ||          |           d{V S )a  
 @ignore
        subscribes to a websocket channel

        https://docs.cloud.coinbase.com/intx/docs/websocket-overview#subscribe

        :param str name: the name of the channel
        :param str[] [symbols]: unified market symbol
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: subscription to a websocket channel
        N   r   ::symbolidr1   r   ( is not supported in sandbox environmentCBINTLMDbase64	SUBSCRIBE)typechannelstimekey
passphrase	signatureproduct_ids)load_marketscheck_required_credentialsget_active_symbolslenmarket_symbols
market_idsrangeappendmarketrH   r   rX   strnonceapiKeypasswordhmacencodebase64_to_binarysecrethashlibsha256watch_multipleextendwatch)rN   rR   rS   paramsrl   messageHash
productIdssymbolsLengthmessageHashesparsedSymbols	marketIdsiurl	timestampauthrb   	subscribes                    rP   r   zcoinbaseinternational.subscribeN   s|      !!!!!!!!!'')))
?--//GG1 //88M66I"J1c-0011 E E$$TD[=3C%CDDDDE a[[,,F+x(88K ,Jit$;tw)SSTTT

%%	4;&3dmCIIdkk$//1F1Ft{1S1SU\Ucemnn	;-"
 
	 !'1Im$1,,S-YX^A_A_anoooooooooZZ[$++i2P2PR]^^^^^^^^^rQ   c                   K   |                                   d{V  |                                  |                     |          r| j        }n|                     |          }g }g }t          dt          |                    D ]b}|                     ||                   }|                     |          }|	                    |           |	                    |dz   |z              c| j
        d         d         }	|	t          | j        dz             |                     |                                           }
|
| j        z   dz   | j        z   }|                     |                     |          |                     | j                  t*          j        d          }d	|
||g| j        | j        |d
}|                     |	||                     ||          |           d{V S )a  
 @ignore
        subscribes to a websocket channel using watchMultiple

        https://docs.cloud.coinbase.com/intx/docs/websocket-overview#subscribe

        :param str name: the name of the channel
        :param string|str[] [symbols]: unified market symbol
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: subscription to a websocket channel
        Nr   rV   r1   r   rY   rZ   r[   r\   )r]   r_   rc   r^   r`   ra   rb   )rd   re   is_emptyrS   rh   rj   rg   	market_idrW   rk   rH   r   rX   number_to_stringsecondsro   rp   rq   rr   rs   rt   ru   rv   rw   rx   )rN   rR   rS   rz   r~   r|   r   marketIdrW   r   r   r   rb   r   s                 rP   subscribe_multiplez(coinbaseinternational.subscribe_multiple   s      !!!!!!!!!'')))==!! 	3lGG))'22G
q#g,,'' 	7 	7A~~gaj11H[[**Fh'''  v!56666it$;tw)SSTTT))$,,..99	4;&3dmCIIdkk$//1F1Ft{1S1SU\Ucemnn	%;-"
 
	 ((mT[[TZ=[=[]jkkkkkkkkkrQ   rW   c                 v   K   |                                   d{V  |                     d|g|           d{V S )ar  
        watch the current funding rate

        https://docs.cloud.coinbase.com/intx/docs/websocket-channels#funding-channel

        :param str symbol: unified market symbol
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `funding rate structure <https://docs.ccxt.com/?id=funding-rate-structure>`
        NRISK)rd   r   )rN   rW   rz   s      rP   watch_funding_ratez(coinbaseinternational.watch_funding_rate   sV       !!!!!!!!!^^FVHf=========rQ   c                 *  K   |t          | j        dz             |                                  d{V  |                     d||           d{V }|                     |d          }| j        r	i }|||<   |S |                     | j        d|          S )a  
        watch the funding rate for multiple markets

        https://docs.cloud.coinbase.com/intx/docs/websocket-channels#funding-channel

        :param str[] symbols: a list of unified market symbols
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a dictionary of `funding rates structures <https://docs.ccxt.com/?id=funding-rates-structure>`, indexe by market symbols
        Nz1 watchFundingRates() requires an array of symbolsr   rW   )r   rX   rd   r   safe_string
newUpdatesfilter_by_arrayfundingRates)rN   rS   rz   fundingRaterW   results         rP   watch_funding_ratesz)coinbaseinternational.watch_funding_rates   s       ?#DG.a$abbb!!!!!!!!! 33FGVLLLLLLLL!!+x88? 	F(F6NM##D$5xIIIrQ   c                    K   |                                   d{V  d}|                     |ddd          \  }}|                     ||g|           d{V S )aU  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://docs.cloud.coinbase.com/intx/docs/websocket-channels#instruments-channel

        :param str [symbol]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.channel]: the channel to watch, 'LEVEL1' or 'INSTRUMENTS', default is 'LEVEL1'
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        Nr   r3   r4   )rd   handle_option_and_paramsr   )rN   rW   rz   r3   s       rP   watch_tickerz"coinbaseinternational.watch_ticker   sy       !!!!!!!!!77yZbcc^^GfXv>>>>>>>>>rQ   c                     | j         }g }t          dt          |                    D ]4}||         }| j        |         }|d         r|                    |           5|S )Nr   active)rS   rj   rg   marketsrk   )rN   rS   outputr   rW   rl   s         rP   rf   z(coinbaseinternational.get_active_symbols   sg    ,q#g,,'' 	& 	&AQZF\&)Fh &f%%%rQ   c                   K   |                                   d{V  d}|                     |ddd          \  }}|                     |||           d{V }| j        ri }|||d         <   |S |                     | j        d|          S )ab  
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://docs.cloud.coinbase.com/intx/docs/websocket-channels#instruments-channel

        :param str[] [symbols]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.channel]: the channel to watch, 'LEVEL1' or 'INSTRUMENTS', default is 'INSTLEVEL1UMENTS'
        :returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
        Nr'   r3   r4   rW   )rd   r   r   r   r   tickers)rN   rS   rz   r3   tickerr   s         rP   watch_tickersz#coinbaseinternational.watch_tickers   s       !!!!!!!!!77PY[cdd~~gw????????? 	F'-F6(#$M##DL(GDDDrQ   clientc                     |                      |          }|                     |d          }|                    ||           |                    ||dz   |d         z              d S Nr3   rV   rW   )parse_ws_instrumentr   resolverN   r   messager   r3   s        rP   handle_instrumentz'coinbaseinternational.handle_instrument   sh    4 ))'22""7I66vw'''vw~x0@@AAAAArQ   r   c           
         |                      |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          |                     |dd          d          S )N
product_idr_   inforW   -r   datetimehighlowbid	bidVolumeask	askVolumevwapopencloselastpreviousClosechange
percentagetotal_24_hour_quantitytotal24_hour_quantitytotal_24_hour_volumetotal24_hour_volume)average
baseVolumequoteVolume)r   safe_tickersafe_symbol	parse8601safe_string_2)rN   r   rl   r   r   s        rP   r   z)coinbaseinternational.parse_ws_instrument  st   l ##FL99##FF33 !
F!
d&&x==!
 11!
 	!

 D!
 4!
 4!
 !
 4!
 !
 D!
 D!
 T!
 D!
 T!
  d!!
" $#!
$ ,,V5MOfgg--f6LNcdd)!
 !
 !
   	rQ   c                     |                      |          }|                     |d          }|                    ||           |                    ||dz   |d         z              d S r   )parse_ws_tickerr   r   r   s        rP   handle_tickerz#coinbaseinternational.handle_tickerl  sh    2 %%g..""7I66vw'''vw~x0@@AAAAArQ   rl   c                    |                      |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          S )Nr_   r   r   rW   r   r   r   	bid_pricer   bid_qtyr   	ask_pricer   ask_qtyr   r   r   r   r   r   r   r   r   )r   r   r   )r   r   r   r   safe_number)rN   r   rl   r   r   s        rP   r   z%coinbaseinternational.parse_ws_ticker  s    ##FF33##FL99 !
F!
d&&x88!
 11!
 	!

 4##FK88!
 ))&)<<!
 4##FK88!
 ))&)<<!
 D!
 4!
 D!
 T!
 D!
 d!
 $!
  t!!
" D#!
$ !)!
 !
 !
   	rQ   r<   	timeframesincelimitc                   K   |                                   d{V  |                     |          }|d         }|                     | j        di           }|                     |||          }|                     ||g|           d{V }	| j        r|	                    ||          }|                     |	||dd          S )a  
        watches historical candlestick data containing the open, high, low, close price, and the volume of a market

        https://docs.cdp.coinbase.com/intx/docs/websocket-channels#candles-channel

        :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
        NrW   rE   r   T)	rd   rl   	safe_dictrI   r   r   r   getLimitfilter_by_since_limit)
rN   rW   r   r   r   rz   rl   rI   intervalohlcvs
             rP   watch_ohlcvz!coinbaseinternational.watch_ohlcv  s       !!!!!!!!!V$$!..|R@@##GY	BBnnXx@@@@@@@@? 	2NN6511E))%q$GGGrQ   c                    |                      |d          }|                      |d          }|                     |          }|d         }|                     |          }|                     | j        |i           | j        |<   |                     | j        |         |          9|                     | j        dd          }t          |          | j        |         |<   | j        |         |         }	|                     |dg           }
t          dt          |
                    D ]5}|
|         }|                     ||          }|	                    |           6|                    |	|dz   |z              d S )	Nr3   r   rW   
OHLCVLimitr5   candlesr   rV   )r   safe_marketfind_timeframe
safe_valueohlcvssafe_integerrI   r   	safe_listrj   rg   parse_ohlcvrk   r   )rN   r   r   r{   r   rl   rW   r   r   storeddatar   tickparseds                 rP   handle_ohlcvz"coinbaseinternational.handle_ohlcv  se   & &&w	::##G\::!!(++!''44	"oodk62FFF??4;v.	::B%%dlL$GGE-B5-I-IDK	*V$Y/~~gy"55q#d))$$ 	" 	"A7D%%dF33FMM&!!!!v{T1F:;;;;;rQ   c                 D   K   |                      |g|||           d{V S )a/  
        get the list of most recent trades for a particular symbol

        https://docs.cloud.coinbase.com/intx/docs/websocket-channels#match-channel

        :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>`
        N)watch_trades_for_symbols)rN   rW   r   r   rz   s        rP   watch_tradesz"coinbaseinternational.watch_trades  s6       22F8UE6RRRRRRRRRrQ   c                 j  K   |                                   d{V  |                     |dddd          }|                     d||           d{V }| j        rB|                     |d          }|                     |d          }|                    ||          }|                     |||dd          S )a  
        get the list of most recent trades for a list of symbols
        :param str[] symbols: 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>`
        NFTMATCHr   rW   r   )rd   rh   r   r   r   r   r   r   )rN   rS   r   r   rz   tradesfirsttradeSymbols           rP   r   z.coinbaseinternational.watch_trades_for_symbols  s       !!!!!!!!!%%gtUD$GG..wHHHHHHHH? 	8NN61--E**5(;;KOOK77E))&%TRRRrQ   c                    |                      |          }|d         }|                     |d          }|| j        vr5|                     | j        dd          }t          |          }|| j        |<   | j        |         }|                    |           || j        |<   |                    ||           |                    ||dz   |d         z              |S )NrW   r3   rB   r5   rV   )parse_ws_trader   r   r   rI   r   rk   r   )	rN   r   r   traderW   r3   r   tradesArrayCachetradesArrays	            rP   handle_tradez"coinbaseinternational.handle_trade  s     ##G,,x""7I66$+%%%%dlM4HHE)%00"2DKk&)5!!!)F{G,,,{GdNU8_$DEEErQ   c                    |                      |dd          }|                     |d          }|                     ||                     |d          d |                     |          ||                     ||          d |                     |d          d |                     |d          |                     |d          d d d          S )	NrW   r   r_   match_idagressor_sidetrade_price	trade_qty)r   rX   orderr   r   rW   r]   sidetakerOrMakerpriceamountcostfee)r   r   
safe_trader   r   safe_string_lower)rN   r   rl   r   r   s        rP   r   z$coinbaseinternational.parse_ws_trade(  s     %%eX|DD##E622""5*5511 &&x88**5/BB %%e];;&&uk:: 
  
   	rQ   c                 B   K   |                      |g||           d{V S )aG  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://docs.cloud.coinbase.com/intx/docs/websocket-channels#level2-channel

        :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)watch_order_book_for_symbols)rN   rW   r   rz   s       rP   watch_order_bookz&coinbaseinternational.watch_order_bookG  s4       66xOOOOOOOOOrQ   c                 t   K   |                                   d{V  |                     d||           d{V S )a  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://docs.cloud.coinbase.com/intx/docs/websocket-channels#level2-channel

        :param str[] symbols:
        :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LEVEL2)rd   r   )rN   rS   r   rz   s       rP   r  z2coinbaseinternational.watch_order_book_for_symbolsT  sV       !!!!!!!!!,,XwGGGGGGGGGrQ   c                    |                      |d          }|                      |d          }|                     |          }|                      |d          }|                      |d          }|| j        vr:|                     | j        dd          }|                     i |          | j        |<   | j        |         }	|dk    r4|                     ||d dd	          }
|	                    |
           ||	d
<   n-|                     |dg           }| 	                    |	|           |                     |d          |	d<   ||	d<   | 
                    |          |	d<   |	| j        |<   |                    |	|dz   |z              d S )Nr]   r   r_   r3   watchOrderBookLimitr5   SNAPSHOTbidsasksrW   changessequencern   r   r   rV   )r   r   
orderbooksr   rI   
order_bookparse_order_bookresetr   handle_deltasr   r   )rN   r   r   r]   r   rW   r   r3   r   	orderbookparsedSnapshotr  s               rP   handle_order_bookz'coinbaseinternational.handle_order_bookb  s   H 00##G\::!!(++##GV44""7I66$/))%%dl4I4PPE&*oob%&@&@DOF#OF+	:!227FD&RXYYNOON+++"(IhnnWi<<Gy'222!..w
CC	' (	*!%!9!9	+"+y'D.6"9:::::rQ   c                     |                      |d          }|dk    rdnd}|                     |d          }|                     |d          }||         }|                    ||           d S )Nr   buyr  r  rU      )r  
safe_floatstore)rN   r  deltarawSider   r   r  booksides           rP   handle_deltaz"coinbaseinternational.handle_delta  ss    ((22!U**vvq))**T?uf%%%%%rQ   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )rj   rg   r"  )rN   r  deltasr   s       rP   r  z#coinbaseinternational.handle_deltas  sH    q#f++&& 	4 	4Ai3333	4 	4rQ   c                     |S N )rN   r   r   s      rP   handle_subscription_statusz0coinbaseinternational.handle_subscription_status  s	    2 rQ   c                     |                      |d          }|                     |          }|| j        |d         <   |                    ||dz   |d         z              d S )Nr3   rW   rV   )r   parse_funding_rater   r   )rN   r   r   r3   r   s        rP   handle_funding_ratez)coinbaseinternational.handle_funding_rate  sf    . ""7I66--g663>+h/0{GdN[5J$JKKKKKrQ   c                    |                      |d          }|dk    rdS |                      |d          }|                      |d          }	 | j        dz   |z   |z   }|                     | j        d         ||           |                     | j        d         ||           t          |          # t          $ r}|                    |           Y d }~nd }~ww xY wd	S )
Nr]   REJECTFreasonr    rF   broadT)r   rX   throw_exactly_matched_exceptionrJ   throw_broadly_matched_exceptionr   	Exceptionreject)rN   r   r   r]   r.  errMsgfeedbackes           rP   handle_error_messagez*coinbaseinternational.handle_error_message  s     0085!!'844!!'955	w}v-6H001I6S[\\\001I6S[\\\))) 	 	 	MM!	ts   A#B/ /
C9CCc                    |                      ||          rd S |                     |dd          }| j        | j        | j        | j        | j        | j        | j        d}|                     |d          }|dk    r%|                     |d          }t          |          |	                    d          dk    r| 
                    ||           |                     ||          }| |||           d S d S )	Nr3    )SUBSCRIPTIONSINSTRUMENTSr4   r   r
  FUNDINGr   r]   errorr   CANDLES)r8  r   r(  r   r   r   r  r+  r   findr   r   )rN   r   r   r3   methodsr]   errorMessagemethods           rP   handle_messagez$coinbaseinternational.handle_message  s   $$VW55 	F""7Ir::!<1(&,/&
 
 007??++GY??L---<<	""R''fg...'22F67##### rQ   r&  )0__name__
__module____qualname__r   rM   rm   r
   r   r   r   r   r   r   r   r   rf   r   r   r   r   dictr   r   objectr   r   r   r   listr   r   r   r   r   r   r   r	   r  r  r  r"  r  r(  r+  r   r8  rE  __classcell__)rO   s   @rP   r   r      s       8# 8 8 8 8 8 8t =A 3_ 3_C 3_' 3_ 3_ 3_ 3_j FJRT (l (lS (l7 (l (l (l (lT <> > >s >+ > > > > <@ J J J| J J J J* 68 ? ? ?F ? ? ? ?    6:" E E7 E E E E E*B B B B B>M M$ M M M M^BF B B B B<% %f %f % % % % %N ?CQUdhqs H H H H3 H^a Hx|  ~B  yC H H H H."<6 "< "< "< "<H <@d[] S S SS S Sbfglbm S S S S OSaenp S Sd3i S S[^ Suyz  vA S S S S$  8   > @DB P PS P PS\ P P P P SW_a H H$s) HC Hfo H H H H8; 8; 8;t& & &4 4 4  6L& L L L L86 t    .$ $ $ $ $ $ $rQ   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ru   ccxt.base.typesr   r   r   r   r	   r
   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   r   async_supportr   r'  rQ   rP   <module>rU     s2       N N N N N N N N  y y y y y y y y y y y y y y y y y y y y y y y y y y 4 4 4 4 4 4       * * * * * * 0 0 0 0 0 0 . . . . . . ) ) ) ) ) )y$ y$ y$ y$ y$D.D y$ y$ y$ y$ y$rQ   