
    iQ                         d dl Zd dlmZm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 d dlmZ  G d d	ej        j                  ZdS )
    N)
ArrayCacheArrayCacheBySymbolById)IntOrder	OrderBookStrTrade)Client)List)AuthenticationError)ArgumentsRequiredc            
       (    e Zd Z 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 Zddi fdedededee         fdZd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dZdef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i fdZ xZS )bitstampc                     |                      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t          iid          S )NTF)wswatchOrderBookwatchOrderswatchTrades
watchOHLCVwatchTickerwatchTickersapir   zwss://ws.bitstamp.net       )snapshotDelaysnapshotMaxRetries  )	expiresInuserIdwsSessionTokenr   tradesLimit
OHLCVLimitexact4009)hasurlsoptions
exceptions)deep_extendsuperr   describer   )self	__class__s    W/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/bitstamp.pyr,   zbitstamp.describe   s    h 5 5 > > @ @"&###$ %  1  "$%&*+# #  $"
 
 /5C
 C
   	    Nsymbollimitreturnc                 X  K   |                                   d{V  |                     |          }|d         }d|z   }d|d         z   }| j        d         d         }dd	|i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
        Nr1   
orderbook:diff_order_book_idr   r   bts:subscribechanneleventdata)load_marketsmarketr'   extendwatchr2   )r-   r1   r2   paramsr>   messageHashr9   urlrequestmessage	orderbooks              r/   watch_order_bookzbitstamp.watch_order_book3   s       !!!!!!!!!V$$!"V+$vd|3it$$7
 
 ++gv..**S+wLLLLLLLL	   r0   clientc                    |                      |d          }|                    d          }|                      |d          }|                     |          }|                     | j        |          }|                     |d          }|                     |d          }	|                     |	d          }
d|z   }|lt          |j                  }|                     dd	d
          }||k    r| 	                    | j
        |||d i            |j                            |	           d S ||
k    rd S |                     ||	           |                    ||           d S )Nr9   _r   noncer<   microtimestampr5   r   r   r   )safe_stringsplitsafe_symbol
safe_value
orderbookssafe_integerlencachehandle_optionspawnload_order_bookappendhandle_deltaresolve)r-   rH   rE   r9   partsmarketIdr1   storedOrderBookrK   delta
deltaNoncerB   cacheLengthr   s                 r/   handle_order_bookzbitstamp.handle_order_bookK   sd   0 ""7I66c""##E1--!!(++//$/6BB9900&&u.>??
"V+=o344K !../?RSTTMm++

4/fdTVWWW!((///Fj  F/511144444r0   c                 r   |                      |d          }||d<   |                     |          |d<   |                     |d          |d<   |                     |dg           }|                     |dg           }|d         }|d         }|                     ||           |                     ||           d S )N	timestampdatetimerL   rK   bidsasks)safe_timestampiso8601rR   rP   handle_bid_asks)r-   rF   r^   rc   re   rf   
storedBids
storedAskss           r/   rY   zbitstamp.handle_deltaz   s    ''{;;	!*	+ $Y 7 7	*!..u6FGG	'ufb11ufb11v&
v&
Z...Z.....r0   c                     t          dt          |                    D ]2}|                     ||                   }|                    |           3d S )Nr   )rangerS   parse_bid_ask
storeArray)r-   bookSidebidAsksibidAsks        r/   ri   zbitstamp.handle_bid_asks   sY    q#g,,'' 	( 	(A''
33F''''	( 	(r0   c                 .   |d         }|                      |d          }|                      |d          }||k     rdS t          dt          |                    D ]-}||         }|                      |d          }||k    r|dz   c S .t          |          S )Nr   rL   rK      )rR   rm   rS   )	r-   rF   deltasfirstElementfirstElementNoncerK   rr   r^   r_   s	            r/   get_cache_indexzbitstamp.get_cache_index   s    ay --l<LMM!!)W55$$$2q#f++&& 	 	A1IE**52BCCJU""1u #6{{r0   sincec                   K   |                                   d{V  |                     |          }|d         }d|z   }| j        d         d         }d|d         z   }dd	|i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>`
        Nr1   trades:r   r   live_trades_r7   r8   r9   r:   rc   T)r=   r>   r'   r?   r@   
newUpdatesgetLimitfilter_by_since_limit)r-   r1   r{   r2   rA   r>   rB   rC   r9   rD   rE   tradess               r/   watch_tradeszbitstamp.watch_trades   s       !!!!!!!!!V$$!&(it$ 6$</$7
 
 ++gv..zz#{G[IIIIIIII? 	3OOFE22E))&%TRRRr0   c                    |                      |d          }|                     |d          }|                     |dz            }|                     |d          }|                     |d          }|d         }|                      |d          }	|	dk    rd	nd
}
|                     |||                     |          ||d d d |
||d d d|          S )NrL   r7   r   priceamountr1   typer   buysell)inforc   rd   r1   r7   orderr   takerOrMakersider   r   costfee)rR   rM   parse_to_int
safe_traderh   )r-   trader>   rL   r7   rc   r   r   r1   sideRawr   s              r/   parse_ws_tradezbitstamp.parse_ws_trade   s     **52BCCeT**%%nt&;<<	  00!!%22!##E622 AuuF"Y//  
  
   	r0   c                    |                      |d          }|                    d          }|                      |d          }|                     |          }|d         }d|z   }|                     |d          }	|                     |	|          }
|                     | j        |          }|5|                     | j        dd          }t          |          }|| j        |<   |	                    |
           |
                    ||           d S )	Nr9   rJ      r1   r}   r<   r"   r   )rM   rN   safe_marketrP   r   r   rR   r(   r   rX   rZ   )r-   rH   rE   r9   r[   r\   r>   r1   rB   r<   r   tradesArrayr2   s                r/   handle_tradezbitstamp.handle_trade   s	   * ""7I66c""##E1--!!(++!&(w//##D&11oodk6::%%dlM4HHE$U++K"-DK5!!!{K00000r0   c                 z  K   |t          | j        dz             |                                  d{V  |                     |          }|d         }d}|dz   |d         z   }||||d}|                     |||           d{V }	| j        r|	                    ||          }|                     |	||dd	          S )
a  
        watches information on multiple orders made by the user
        :param str symbol: unified market symbol of the market orders were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of order structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/#/?id=order-structure>`
        Nz) watchOrders() requires a symbol argumentr1   private-my_ordersrJ   r7   )r1   r2   r   rA   rc   T)r   r7   r=   r>   subscribe_privater   r   r   )
r-   r1   r{   r2   rA   r>   r9   rB   subscriptionorderss
             r/   watch_orderszbitstamp.watch_orders   s       >#DG.Y$YZZZ!!!!!!!!!V$$!%mfTl2	
 
 --lKPPPPPPPP? 	3OOFE22E))&%TRRRr0   c                    |                      |d          }|                     |di           }|                     | j        dd          }| j        t          |          | _        | j        }|                     |j        |          }|                      |d          }|                     |          }	|                     ||	          }
|	                    |
           |
                    | j        |           d S )Nr9   r<   ordersLimitr   r1   )rM   rP   rR   r(   r   r   subscriptionsr>   parse_ws_orderrX   rZ   )r-   rH   rE   r9   r   r2   storedr   r1   r>   parseds              r/   handle_orderszbitstamp.handle_orders  s    " ""7I6644!!$,tDD;077DKv';WEE!!,99V$$$$UF33ft{G,,,,,r0   c           	         |                      |d          }|                     |d          }|                      |d          }|                      |d          }|dk    rdnd}|                     |d          }|                     d |          }|d	         }	|                     i d
|d	|	d|dd d|d|                     |          dd dd dd dd d|d|dd dd d|dd dd d d d d d d|          S )Nid_str
order_type	price_str
amount_str1r   r   rd   r1   r   r7   clientOrderIdrc   lastTradeTimestampr   timeInForcepostOnlyr   r   	stopPricetriggerPricer   r   average)filled	remainingstatusr   r   )rM   safe_string_lowerrg   r   
safe_orderrh   )
r-   r   r>   r7   	orderTyper   r   r   rc   r1   s
             r/   r   zbitstamp.parse_ws_order9  s    eX..**5,??	  44!!%66#s**vv''z::	!!$//!  
E 
f 
 " 
 T	 

  
 Y// 
 !$ 
 D 
 4 
  
 D 
 U 
  
 D 
 f 
  D! 
" t# 
$ - 
  
  
. /  	r0   c                     |                      |d          }|                    d          }|                      |d          }|                     |          }|                                 | j        |<   d S )Nr9   rJ   r   )rM   rN   rO   
order_bookrQ   )r-   rH   rE   r9   r[   r\   r1   s          r/   handle_order_book_subscriptionz'bitstamp.handle_order_book_subscriptionh  sl    ""7I66c""##E1--!!(++"&//"3"3r0   c                     |                      |d          }|                    d          dk    r|                     ||           d S d S )Nr9   r   ru   )rM   findr   )r-   rH   rE   r9   s       r/   handle_subscription_statusz#bitstamp.handle_subscription_statuso  sT     ""7I66<<%%**//@@@@@ +*r0   c                 H   |                      |d          }| j        | j        | j        d}t	          |                                          }t          dt          |                    D ]7}||         }|                    |          dk    r||         } |||           8d S )Nr9   )live_tradesdiff_order_bookr   r   ru   )	rM   r   ra   r   listkeysrm   rS   r   )	r-   rH   rE   r9   methodsr   rr   keymethods	            r/   handle_subjectzbitstamp.handle_subject  s    J ""7I66,#5!%!3
 

 GLLNN##q#d))$$ 	( 	(Aq'C||C  2%% vw'''		( 	(r0   c                    |                      |d          }|dk    ro| j        dz   |                     |          z   }|                     |di           }|                     |d          }|                     | j        d         ||           |S )Nr;   z	bts:error r<   coder$   )rM   r7   jsonrP   safe_numberthrow_exactly_matched_exceptionr)   )r-   rH   rE   r;   feedbackr<   r   s          r/   handle_error_messagezbitstamp.handle_error_message  s       '22Kw}tyy'9'99H??7FB77D##D&11D001I4QYZZZr0   c                     |                      ||          sd S |                     |d          }|dk    r|                     ||           d S |                     ||           d S )Nr;   zbts:subscription_succeeded)r   rM   r   r   )r-   rH   rE   r;   s       r/   handle_messagezbitstamp.handle_message  s{    ((99 	FB   '22000++FG<<<<<00000r0   c                   K   |                                   |                                 }|                     | j        d          }|||k    r|                     |           d {V }|                     |d          }|c|                     |d          }|                     |dd          }|                     ||          | j        d<   || j        d<   || j        d<   d S d S d S )Nr   tokenuser_id	valid_secr   r    r!   )	check_required_credentialsmillisecondsrR   r(   privatePostWebsocketsTokenrM   r   safe_integer_productsum)r-   rA   timer   responsesessionTokenr    validitys           r/   authenticatezbitstamp.authenticate  s     '')))  ""%%dlK@@	4)#3#3!<<VDDDDDDDDH  ++Hg>>L'))(I>>44X{DQQ,0HHT8,D,D[))/X&1=-... $4#3 ('r0   c                   K   | j         d         d         }|                                  d {V  |d| j        d         z   z  }d|| j        d         dd}||d	<   |                     |||                     ||          ||           d {V S )
Nr   r   -r    r8   r!   )r9   authr:   rB   )r'   r   r(   r@   r?   )r-   r   rB   rA   rC   rD   s         r/   r   zbitstamp.subscribe_private  s      it$!!!!!!!!!sT\(333$&%56 
 
 '2]#ZZ[$++gv2N2NP[]ijjjjjjjjjr0   )N)__name__
__module____qualname__r,   strr   r   rG   r
   ra   rY   ri   rz   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r.   s   @r/   r   r      s                D @DB ! !S ! !S\ ! ! ! !0-5 -5 -5 -5 -5^
/ 
/ 
/( ( (
   <@d[] S S SS S Sbfglbm S S S S6% % % %N#16 #1 #1 #1 #1J 04$UYbd S S SC Ss Simnsit S S S S6-F - - - -<- - - -^4V 4 4 4 4A A A A A"0(V 0( 0( 0( 0(d6    '1V '1 '1 '1 '1R )+ > > > >* IK k k k k k k k kr0   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   r	   !ccxt.async_support.base.ws.clientr
   typingr   ccxt.base.errorsr   r   async_supportr    r0   r/   <module>r      s        O O O O O O O O = = = = = = = = = = = = = = 4 4 4 4 4 4       0 0 0 0 0 0 . . . . . .{k {k {k {k {kt!* {k {k {k {k {kr0   