
    ":hQ                         d dl Zd dlmZm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 d dlmZ  G d d	ej&                  j(                        Zy)
    N)
ArrayCacheArrayCacheBySymbolById)AnyIntOrder	OrderBookStrTrade)Client)List)AuthenticationError)ArgumentsRequiredc            
       "    e Zd Zdef 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 )bitstampreturnc                     | j                  t        t        |          d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watchTradesForSymbols
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    K/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/pro/bitstamp.pyr/   zbitstamp.describe   s    h > @"&##).#$ %	 1  "$%&*+#  $"
 /7 C
    	    Nsymbollimitc                 6  K   | j                          d{    | j                  |      }|d   }d|z   }d|d   z   }| j                  d   d   }dd	|id
}| j                  ||      }	| j	                  |||	|       d{   }
|
j                         S 7 ~7 w)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
        Nr4   
orderbook:diff_order_book_idr   r   bts:subscribechanneleventdata)load_marketsmarketr*   extendwatchr5   )r0   r4   r5   paramsr@   messageHashr;   urlrequestmessage	orderbooks              r2   watch_order_bookzbitstamp.watch_order_book4   s      !!!V$!"V+$vd|3iit$$7
 ++gv.**S+wLL	   	" Ms"   BBA)B BBBclientc                 h   | j                  |d      }|j                  d      }| j                  |d      }| j                  |      }| j                  | j                  |      }| j                  |d      }| j                  |d      }	| j                  |	d      }
d|z   }|it        |j                        }| j                  dd	d
      }||k(  r | j                  | j                  |||d i        |j                  j                  |	       y ||
k\  ry | j                  ||	       |j                  ||       y )Nr;   _r   noncer>   microtimestampr7   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)r0   rJ   rG   r;   partsmarketIdr4   storedOrderBookrM   delta
deltaNoncerD   cacheLengthr   s                 r2   handle_order_bookzbitstamp.handle_order_bookL   s)   0 ""7I6c"##E1-!!(+//$//6B90&&u.>?
"V+=o334K !../?RSTMm+

4//fdTVW!!((/j /514r3   c                 ,   | j                  |d      }||d<   | j                  |      |d<   | j                  |d      |d<   | j                  |dg       }| j                  |dg       }|d   }|d   }| j	                  ||       | j	                  ||       y )N	timestampdatetimerN   rM   bidsasks)safe_timestampiso8601rT   rR   handle_bid_asks)r0   rH   r`   re   rg   rh   
storedBids
storedAskss           r2   r[   zbitstamp.handle_delta{   s    ''{;	!*	+ $Y 7	*!..u6FG	'ufb1ufb1v&
v&
Z.Z.r3   c                     t        dt        |            D ]'  }| j                  ||         }|j                  |       ) y )Nr   )rangerU   parse_bid_ask
storeArray)r0   bookSidebidAsksibidAsks        r2   rk   zbitstamp.handle_bid_asks   s>    q#g,' 	(A''
3F'	(r3   c                     |d   }| j                  |d      }| j                  |d      }||k  ryt        dt        |            D ]%  }||   }| j                  |d      }||k(  s |dz   c S  t        |      S )Nr   rN   rM      )rT   ro   rU   )	r0   rH   deltasfirstElementfirstElementNoncerM   rt   r`   ra   s	            r2   get_cache_indexzbitstamp.get_cache_index   s    ay --l<LM!!)W5$$q#f+& 	A1IE**52BCJU"1u		
 6{r3   sincec                 |  K   | j                          d{    | j                  |      }|d   }d|z   }| j                  d   d   }d|d   z   }dd	|id
}	| j                  |	|      }
| j	                  |||
|       d{   }| j
                  r|j                  ||      }| j                  |||dd      S 7 7 9w)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>`
        Nr4   trades:r   r   live_trades_r9   r:   r;   r<   re   T)r?   r@   r*   rA   rB   
newUpdatesgetLimitfilter_by_since_limit)r0   r4   r}   r5   rC   r@   rD   rE   r;   rF   rG   tradess               r2   watch_tradeszbitstamp.watch_trades   s      !!!V$!&(iit$ 6$</$7
 ++gv.zz#{G[II??OOFE2E))&%TRR! 	" Js"   B<B8A)B< B:8B<:B<c                 X   | j                  |d      }| j                  |d      }| j                  |dz        }| j                  |d      }| j                  |d      }|d   }| j                  |d      }	|	dk(  rd	nd
}
| j                  ||| j	                  |      ||d d d |
||d d d|      S )NrN   r9   r!   priceamountr4   typer   buysell)infore   rf   r4   r9   orderr   takerOrMakersider   r   costfee)rT   rO   parse_to_int
safe_traderj   )r0   trader@   rN   r9   re   r   r   r4   sideRawr   s              r2   parse_ws_tradezbitstamp.parse_ws_trade   s     **52BCeT*%%nt&;<	  0!!%2!##E62 AuF"Y/  
  	r3   c                    | j                  |d      }|j                  d      }| j                  |d      }| j                  |      }|d   }d|z   }| j                  |d      }	| j	                  |	|      }
| j                  | j
                  |      }|7| j                  | j                  dd      }t        |      }|| j
                  |<   |j                  |
       |j                  ||       y )	Nr;   rL      r4   r   r>   r%   r!   )rO   rP   safe_marketrR   r   r   rT   r+   r   rZ   r\   )r0   rJ   rG   r;   r]   r^   r@   r4   rD   r>   r   tradesArrayr5   s                r2   handle_tradezbitstamp.handle_trade   s    * ""7I6c"##E1-!!(+!&(w/##D&1oodkk6:%%dllM4HE$U+K"-DKK5!{K0r3   c                 f  K   |t        | j                  dz         | j                          d{    | j                  |      }|d   }d}|dz   |d   z   }||||d}| j	                  |||       d{   }	| j
                  r|	j                  ||      }| j                  |	||dd	      S 7 |7 9w)
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 argumentr4   private-my_ordersrL   r9   )r4   r5   r   rC   re   T)r   r9   r?   r@   subscribe_privater   r   r   )
r0   r4   r}   r5   rC   r@   r;   rD   subscriptionorderss
             r2   watch_orderszbitstamp.watch_orders  s      >#DGG.Y$YZZ!!!V$!%mfTl2	
 --lKPP??OOFE2E))&%TRR 	" Qs"   .B1B-AB15B/68B1/B1c                    | j                  |d      }| j                  |di       }| j                  | j                  dd      }| j                  t        |      | _        | j                  }| j                  |j                  |      }| j                  |d      }| j                  |      }	| j                  ||	      }
|j                  |
       |j                  | j                  |       y )Nr;   r>   ordersLimitr!   r4   )rO   rR   rT   r+   r   r   subscriptionsr@   parse_ws_orderrZ   r\   )r0   rJ   rG   r;   r   r5   storedr   r4   r@   parseds              r2   handle_orderszbitstamp.handle_orders  s    " ""7I64!!$,,tD;;07DKv';';WE!!,9V$$$UF3ft{{G,r3   c           	         | j                  |d      }| j                  |d      }| j                  |d      }| j                  |d      }|dk(  rdnd}| j                  |d      }| j                  d |      }|d	   }	| j	                  i d
|d	|	d|dd 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|      S )Nid_str
order_type	price_str
amount_str1r   r   rf   r4   r   r9   clientOrderIdre   lastTradeTimestampr   timeInForcepostOnlyr   r   	stopPricetriggerPricer   r   average)filled	remainingstatusr   r   )rO   safe_string_lowerri   r   
safe_orderrj   )
r0   r   r@   r9   	orderTyper   r   r   re   r4   s
             r2   r   zbitstamp.parse_ws_order:  s    eX.**5,?	  4!!%6#s*v''z:	!!$/!  
E 
f 
 " 
 T	 

  
 Y/ 
 !$ 
 D 
 4 
  
 D 
 U 
  
 D 
 f 
  D! 
" t# 
$ - 
. / 	r3   c                     | j                  |d      }|j                  d      }| j                  |d      }| j                  |      }| j                         | j                  |<   y )Nr;   rL   r   )rO   rP   rQ   
order_bookrS   )r0   rJ   rG   r;   r]   r^   r4   s          r2   handle_order_book_subscriptionz'bitstamp.handle_order_book_subscriptioni  sY    ""7I6c"##E1-!!(+"&//"3r3   c                 v    | j                  |d      }|j                  d      dkD  r| j                  ||       y y )Nr;   r   rw   )rO   findr   )r0   rJ   rG   r;   s       r2   handle_subscription_statusz#bitstamp.handle_subscription_statusp  s<     ""7I6<<%*//@ +r3   c                 &   | j                  |d      }| j                  | j                  | j                  d}t	        |j                               }t        dt        |            D ]*  }||   }|j                  |      dkD  s||   } |||       , y )Nr;   )live_tradesdiff_order_bookr   r   rw   )	rO   r   rc   r   listkeysro   rU   r   )	r0   rJ   rG   r;   methodsr   rt   keymethods	            r2   handle_subjectzbitstamp.handle_subject  s    J ""7I6,,#55!%!3!3

 GLLN#q#d)$ 	(Aq'C||C 2% vw'		(r3   c                     | j                  |d      }|dk(  rf| j                  dz   | j                  |      z   }| j                  |di       }| j	                  |d      }| j                  | j                  d   ||       |S )Nr=   z	bts:error r>   coder'   )rO   r9   jsonrR   safe_numberthrow_exactly_matched_exceptionr,   )r0   rJ   rG   r=   feedbackr>   r   s          r2   handle_error_messagezbitstamp.handle_error_message  s~       '2Kww}tyy'99H??7FB7D##D&1D001I4QYZr3   c                     | j                  ||      sy | j                  |d      }|dk(  r| j                  ||       y | j                  ||       y )Nr=   zbts:subscription_succeeded)r   rO   r   r   )r0   rJ   rG   r=   s       r2   handle_messagezbitstamp.handle_message  sS    ((9B   '200++FG<0r3   c                   K   | j                          | j                         }| j                  | j                  d      }|||kD  r| j	                  |       d {   }| j                  |d      }|c| j                  |d      }| j                  |dd      }| j                  ||      | j                  d<   || j                  d<   || j                  d<   y y y 7 }w)Nr"   tokenuser_id	valid_secr!   r#   r$   )	check_required_credentialsmillisecondsrT   r+   privatePostWebsocketsTokenrO   r   safe_integer_productsum)r0   rC   timer"   responsesessionTokenr#   validitys           r2   authenticatezbitstamp.authenticate  s     '')  "%%dllK@	4)#3!<<VDDH  ++Hg>L'))(I>44X{DQ,0HHT8,D[))/X&1=-. ( $4Ds   ACCA>Cc                   K   | j                   d   d   }| j                          d {    |d| j                  d   z   z  }d|| j                  d   dd}||d	<   | j                  ||| j	                  ||      ||       d {   S 7 `7 w)
Nr   r   -r#   r:   r$   )r;   authr<   rD   )r*   r   r+   rB   rA   )r0   r   rD   rC   rE   rF   s         r2   r   zbitstamp.subscribe_private  s     iit$!!!sT\\(333$&%56
 '2]#ZZ[$++gv2NP[]ijjj 	" ks"   &BB	ABBBB)N) __name__
__module____qualname__r   r/   strr   r   rI   r   rc   r[   rk   r|   r   r
   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r1   s   @r2   r   r      s>   !# !F @DB !S ! !S\ !0-5 -5^
/(
 <@d[] S SS S Sbfglbm S6%N#16 #1J 04$UYbd S SC Ss Simnsit S6-F -<-^4V 4A A"0(V 0(d6 '1V '1R )+ >* IK kr3   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   ccxt.base.typesr   r   r   r   r	   r
   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   async_supportr    r3   r2   <module>r      s;     O B B 4  0 .|kt!!** |kr3   