
    ":h}                         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 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j2                  j4                        Zy)    N)
ArrayCacheArrayCacheByTimestamp)
AnyIntMarket	OrderBookStringsTickerTickersFundingRateFundingRatesTrade)Client)List)ExchangeError)AuthenticationError)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
i fde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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fd%Z,d& Z- xZ.S )(coinbaseinternationalreturnc                    | j                  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    X/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/coinbaseinternational.pyrK   zcoinbaseinternational.describe   sw   &;T K Mdt ( !$	
 +D t    d ' u ( !%     u!"  #$ #(%* %!&"'#(/6 B 9	 x   $#!%.03,-+" ,.Ae7P
 7 7	    Nnamesymbolsc                 "  K   | j                          d{    | j                          d}|}d}|| j                         }t        |      }g }|dkD  rY| j	                  |      }	| j                  |	      }
|
}t        dt        |	            D ]  }|j                  |dz   |	|   z           n*|dk(  r%| j                  |d         }|dz   |d   z   }|d   g}| j                  d   d   }|t        | j                  d	z         t        | j                               }|| j                  z   d
z   | j                  z   }| j!                  | j#                  |      | j%                  | j&                        t(        j*                  d      }d|g|| j                  | j                  |d}|||d<   |dkD  r,| j-                  ||| j/                  ||      |       d{   S | j1                  ||| j/                  ||      |       d{   S 7 7 37 	w)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idr/   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marketrF   r   rV   strnonceapiKeypasswordhmacencodebase64_to_binarysecrethashlibsha256watch_multipleextendwatch)rL   rP   rQ   paramsrj   messageHash
productIdssymbolsLengthmessageHashesparsedSymbols	marketIdsiurl	timestampauthr`   	subscribes                    rN   r   zcoinbaseinternational.subscribeM   s     !!!'')
?--/GG1 //8M6I"J1c-01 E$$TD[=3C%CDE a[[,F+x(88K ,Jiit$;tww)SSTT

%	4;;&3dmmCIIdkk$/1F1Ft{{1SU\UcUcemn	;;--"
	 !'1Im$1,,S-YX^A_anoooZZ[$++i2PR]^^^O 	"L p^s4   HHG HH+HHHHHc                   K   | j                          d{    | j                          | j                  |      r| j                  }n| j	                  |      }g }g }t        dt        |            D ]O  }| j                  ||         }| j                  |      }|j                  |       |j                  |dz   |z          Q | j                  d   d   }	|	t        | j                  dz         | j                  | j                               }
|
| j                  z   dz   | j                   z   }| j#                  | j%                  |      | j'                  | j(                        t*        j,                  d      }d	|
||g| j                  | j                   |d
}| j/                  |	|| j1                  ||      |       d{   S 7 7 w)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   rT   r/   r   rW   rX   rY   rZ   )r[   r]   ra   r\   r^   r_   r`   )rb   rc   is_emptyrQ   rf   rh   re   	market_idrU   ri   rF   r   rV   number_to_stringsecondsrm   rn   ro   rp   rq   rr   rs   rt   ru   rv   )rL   rP   rQ   rx   r|   rz   r   marketIdrU   r   r   r   r`   r   s                 rN   subscribe_multiplez(coinbaseinternational.subscribe_multiple   s     !!!'')==!llG))'2G
q#g,' 	7A~~gaj1H[[*Fh'  v!56		7
 iit$;tww)SSTT))$,,.9	4;;&3dmmCIIdkk$/1F1Ft{{1SU\UcUcemn	%;;--"
	 ((mT[[TZ=[]jkkk9 	"8 ls"   G	GF(G	?G G	G	rU   c                 z   K   | j                          d{    | j                  d|g|       d{   S 7  7 w)at  
        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)rb   r   )rL   rU   rx   s      rN   watch_funding_ratez(coinbaseinternational.watch_funding_rate   s;      !!!^^FVHf=== 	"=s   ;7;9;;c                    K   | j                          d{    | j                  d||       d{   }| j                  |d      }| j                  r	i }|||<   |S | j	                  | j
                  d|      S 7 c7 Jw)a  
        watch the funding rate for multiple markets

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

        :param str[]|None symbols: 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
        Nr   rU   )rb   r   safe_string
newUpdatesfilter_by_arrayfundingRates)rL   rQ   rx   fundingRaterU   results         rN   watch_funding_ratesz)coinbaseinternational.watch_funding_rates   s      !!! 33FGVLL!!+x8??F(F6NM##D$5$5xII 	"Ls    A>A:A>A<A	A><A>c                    K   | j                          d{    d}| j                  |ddd      \  }}| j                  ||g|       d{   S 7 97 w)aW  
        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   r1   r2   )rb   handle_option_and_paramsr   )rL   rU   rx   r1   s       rN   watch_tickerz"coinbaseinternational.watch_ticker   s]      !!!77yZbc^^GfXv>>> 	" ?s!   AA4AAAAc                     | j                   }g }t        dt        |            D ]-  }||   }| j                  |   }|d   s|j	                  |       / |S )Nr   active)rQ   rh   re   marketsri   )rL   rQ   outputr   rU   rj   s         rN   rd   z(coinbaseinternational.get_active_symbols   s\    ,,q#g,' 	&AQZF\\&)Fhf%		&
 rO   c                   K   | j                          d{    d}| j                  |ddd      \  }}| j                  |||       d{   }| j                  ri }|||d   <   |S | j	                  | j
                  d|      S 7 m7 ;w)ad  
        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%   r1   r2   rU   )rb   r   r   r   r   tickers)rL   rQ   rx   r1   tickerr   s         rN   watch_tickersz#coinbaseinternational.watch_tickers   s      !!!77PY[cd~~gw????F'-F6(#$M##DLL(GDD 	" @s!   BB3B
B:BBclientc                     | j                  |      }| j                  |d      }|j                  ||       |j                  ||dz   |d   z          y Nr1   rT   rU   )parse_ws_instrumentr   resolverL   r   messager   r1   s        rN   handle_instrumentz'coinbaseinternational.handle_instrument   sP    4 ))'2""7I6vw'vw~x0@@ArO   r   c           
      b   | j                  |d      }| j                  |d      }| j                  i d|d| j                  ||d      d| j                  |      d|dd d	d d
d dd dd dd dd dd dd dd dd dd dd d | j	                  |dd      | j	                  |dd      d      S )N
product_idr]   inforU   -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)rL   r   rj   r   r   s        rN   r   z)coinbaseinternational.parse_ws_instrument  sS   l ##FL9##FF3 !
F!
d&&x=!
 1!
 	!

 D!
 4!
 4!
 !
 4!
 !
 D!
 D!
 T!
 D!
 T!
  d!!
" $#!
$ ,,V5MOfg--f6LNcd)!
  	rO   c                     | j                  |      }| j                  |d      }|j                  ||       |j                  ||dz   |d   z          y r   )parse_ws_tickerr   r   r   s        rN   handle_tickerz#coinbaseinternational.handle_tickeri  sP    2 %%g.""7I6vw'vw~x0@@ArO   rj   c                    | j                  |d      }| j                  |d      }| j                  i d|d| j                  ||      d| j                  |      d|d| j	                  |d      d	| j	                  |d
      d| j	                  |d      d| j	                  |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   rU   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)rL   r   rj   r   r   s        rN   r   z%coinbaseinternational.parse_ws_ticker  sb    ##FF3##FL9 !
F!
d&&x8!
 1!
 	!

 4##FK8!
 ))&)<!
 4##FK8!
 ))&)<!
 D!
 4!
 D!
 T!
 D!
 d!
 $!
  t!!
" D#!
$ !)!
  	rO   r:   sincelimitc                 l  K   | j                          d{    | j                  |      }|d   }| j                  | j                  di       }| j	                  |||      }| j                  ||g|       d{   }	| j                  r|	j                  ||      }| j                  |	||dd      S 7 7 9w)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
        NrU   rC   r   T)	rb   rj   	safe_dictrG   r   r   r   getLimitfilter_by_since_limit)
rL   rU   	timeframer   r   rx   rj   rG   intervalohlcvs
             rN   watch_ohlcvz!coinbaseinternational.watch_ohlcv  s      !!!V$!..|R@##GY	BnnXx@@??NN651E))%q$GG 	"
 As"   B4B0A!B48B298B42B4c                    | j                  |d      }| j                  |d      }| j                  |      }|d   }| j                  |      }| j                  | j                  |i       | j                  |<   | j                  | j                  |   |      8| j                  | j                  dd      }t        |      | j                  |   |<   | j                  |   |   }	| j                  |dg       }
t        dt        |
            D ]*  }|
|   }| j                  ||      }|	j                  |       , |j                  |	|dz   |z          y )	Nr1   r   rU   
OHLCVLimitr3   candlesr   rT   )r   safe_marketfind_timeframe
safe_valueohlcvssafe_integerrG   r   	safe_listrh   re   parse_ohlcvri   r   )rL   r   r   ry   r   rj   rU   r   r   storeddatar   tickparseds                 rN   handle_ohlcvz"coinbaseinternational.handle_ohlcv  s;   & &&w	:##G\:!!(+!''4	"oodkk62FF??4;;v.	:B%%dllL$GE-B5-IDKK	*V$Y/~~gy"5q#d)$ 	"A7D%%dF3FMM&!	" 	v{T1F:;rO   c                 H   K   | j                  |g|||       d{   S 7 w)a1  
        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)rL   rU   r   r   rx   s        rN   watch_tradesz"coinbaseinternational.watch_trades  s'      22F8UE6RRRRs   " "c                 P  K   | j                          d{    | j                  |dddd      }| j                  d||       d{   }| j                  r6| j	                  |d      }| j                  |d      }|j                  ||      }| j                  |||dd      S 7 7 ]w)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   rU   r   )rb   rf   r   r   r   r   r   r   )rL   rQ   r   r   rx   tradesfirsttradeSymbols           rN   r   z.coinbaseinternational.watch_trades_for_symbols  s      !!!%%gtUD$G..wHH??NN61-E**5(;KOOK7E))&%TRR 	"Hs"   B&B"/B&B$AB&$B&c                    | j                  |      }|d   }| j                  |d      }|| j                  vr7| j                  | j                  dd      }t        |      }|| j                  |<   | j                  |   }|j                  |       || j                  |<   |j                  ||       |j                  ||dz   |d   z          |S )NrU   r1   r@   r3   rT   )parse_ws_trader   r   r   rG   r   ri   r   )	rL   r   r   traderU   r1   r   tradesArrayCachetradesArrays	            rN   handle_tradez"coinbaseinternational.handle_trade	  s     ##G,x""7I6$++%%%dllM4HE)%0"2DKKkk&)5!)F{G,{GdNU8_$DErO   c                 H   | j                  |dd      }| j                  |d      }| j                  || j                  |d      d | j                  |      || j	                  ||      d | j                  |d      d | j                  |d      | j                  |d      d d d      S )	NrU   r   r]   match_idagressor_sidetrade_price	trade_qty)r   rV   orderr   r   rU   r[   sidetakerOrMakerpriceamountcostfee)r   r   
safe_trader   r   safe_string_lower)rL   r   rj   r   r   s        rN   r   z$coinbaseinternational.parse_ws_trade%  s     %%eX|D##E62""5*51 &&x8**5/B %%e];&&uk: 
  	rO   c                 F   K   | j                  |g||       d{   S 7 w)aI  
        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)rL   rU   r   rx   s       rN   watch_order_bookz&coinbaseinternational.watch_order_bookD  s%      66xOOOOs   !!c                 x   K   | j                          d{    | j                  d||       d{   S 7 7 w)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)rb   r   )rL   rQ   r   rx   s       rN   r  z2coinbaseinternational.watch_order_book_for_symbolsQ  s;      !!!,,XwGGG 	"Gs   :6:8::c                    | j                  |d      }| j                  |d      }| j                  |      }| j                  |d      }| j                  |d      }|| j                  vr<| j                  | j                  dd      }| j                  i |      | j                  |<   | j                  |   }	|dk(  r,| j                  ||d dd	      }
|	j                  |
       ||	d
<   n%| j                  |dg       }| j                  |	|       | j                  |d      |	d<   ||	d<   | j                  |      |	d<   |	| j                  |<   |j                  |	|dz   |z          y )Nr[   r   r]   r1   watchOrderBookLimitr3   SNAPSHOTbidsasksrU   changessequencerl   r   r   rT   )r   r   
orderbooksr   rG   
order_bookparse_order_bookresetr   handle_deltasr   r   )rL   r   r   r[   r   rU   r   r1   r   	orderbookparsedSnapshotr  s               rN   handle_order_bookz'coinbaseinternational.handle_order_book_  s[   H 0##G\:!!(+##GV4""7I6$//)%%dll4I4PE&*oob%&@DOOF#OOF+	:!227FD&RXYNOON+"(IhnnWi<Gy'2!..w
C	' (	*!%!9	+"+y'D.6"9:rO   c                     | j                  |d      }|dk(  rdnd}| j                  |d      }| j                  |d      }||   }|j                  ||       y )Nr   buyr  r  rS      )r  
safe_floatstore)rL   r  deltarawSider   r   r   booksides           rN   handle_deltaz"coinbaseinternational.handle_delta  sY    ((2!U*vq)*T?uf%rO   c                 b    t        dt        |            D ]  }| j                  |||           y )Nr   )rh   re   r   )rL   r  deltasr   s       rN   r  z#coinbaseinternational.handle_deltas  s0    q#f+& 	4Ai3	4rO   c                     |S N )rL   r   r   s      rN   handle_subscription_statusz0coinbaseinternational.handle_subscription_status  s	    2 rO   c                     | j                  |d      }| j                  |      }|| j                  |d   <   |j                  ||dz   |d   z          y )Nr1   rU   rT   )r   parse_funding_rater   r   )rL   r   r   r1   r   s        rN   handle_funding_ratez)coinbaseinternational.handle_funding_rate  sW    . ""7I6--g63>+h/0{GdN[5J$JKrO   c                    | j                  |d      }|dk7  ry| j                  |d      }| j                  |d      }	 | j                  dz   |z   |z   }| j                  | j                  d   ||       | j	                  | j                  d   ||       t        |      # t        $ r}|j                  |       Y d }~y	d }~ww xY w)
Nr[   REJECTFreasonr    rD   broadT)r   rV   throw_exactly_matched_exceptionrH   throw_broadly_matched_exceptionr   	Exceptionreject)rL   r   r   r[   r,  errMsgfeedbackes           rN   handle_error_messagez*coinbaseinternational.handle_error_message  s     08!!'84!!'95	ww}v-6H001I6S[\001I6S[\)) 	MM!	s   A B 	C'B==Cc                    | j                  ||      ry | j                  |dd      }| j                  | j                  | j                  | j
                  | j                  | j                  | j                  d}| j                  |d      }|dk(  r| j                  |d      }t        |      |j                  d      dkD  r| j                  ||       | j                  ||      }|
 |||       y y )	Nr1    )SUBSCRIPTIONSINSTRUMENTSr2   r   r  FUNDINGr   r[   errorr   CANDLES)r6  r   r&  r   r   r   r  r)  r   findr   r   )rL   r   r   r1   methodsr[   errorMessagemethods           rN   handle_messagez$coinbaseinternational.handle_message  s    $$VW5""7Ir:!<<11((&&,,//&&
 07?++GY?L--<<	"R'fg.'267# rO   r$  )/__name__
__module____qualname__r   rK   rk   r	   r   r   r   r   r   r   r   r
   r   rd   r   r   r   r   dictr   r   objectr   r   r   listr   r   r   r   r   r   r   r   r  r  r  r   r  r&  r)  r6  rC  __classcell__)rM   s   @rN   r   r      s_   8# 8t =A 3_C 3_' 3_j FJRT (lS (l7 (lT <> >s >+ > DF Jc J, J& 68 ? ?F ?  6:" E7 E E*B B>M$ M^BF B<%f %f % %N 8<$]ajl H HC HWZ Hquvzq{ H."<6 "<H <@d[] S SS S Sbfglbm S OSaenp Sd3i S S[^ Suyz  vA S$8> @DB PS P PS\ P SW_a H$s) HC Hfo H8;t&46L& L86 .$rO   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   rs   ccxt.base.typesr   r   r   r   r	   r
   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   async_supportr   r%  rO   rN   <module>rS     sB     N  s s s 4  * 0 )w$D..DD w$rO   