
    i͈                         d dl Zd dlmZmZmZ d dl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ArrayCacheBySymbolByIdArrayCacheByTimestamp)BalancesIntOrder	OrderBookStrTickerTrade)Client)List)AuthenticationError)ArgumentsRequired)
BadRequest)Precisec            
           e Zd Z 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i fdededefdZd	e	fdZd Zd Zi fded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	e	fdZdddi fdedededee         fdZd'd	e	fdZd'dZdddi fdedededee         fdZd'd	e	fdZd'dZd Zi fdefdZ d	e	fdZ!g i fdZ"di fdZ#g i fdZ$i f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*d& Z+ xZ,S )(whitebitc                    |                      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didd| j        iddt
          t
          t
          t          diid          S )NT)wswatchBalancewatchMyTrades
watchOHLCVwatchOrderBookwatchOrderswatchTickerwatchTradesapir   zwss://api.whitebit.com/ws6030090018003600144002880086400604800)	1m5m15m30m1h4h8h1d1wpriceIntervalr   )
timeframesr   pingexact)1246)hasurlsoptions	streaming
exceptions)deep_extendsuperr   describer3   r   r   )self	__class__s    W/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/ccxt/pro/whitebit.pyr@   zwhitebit.describe   s    h 5 5 > > @ @ $!%""&###	 	 5  ! !!!"
 
 $Q# " 	 '''0	 	G-C
 -C
 - - -	    r(   Nsymbolsincelimitreturnc                   K   |                                   d{V  |                     |          }|d         }|                     | j        di           }|                     ||          }|d         }	d|z   }
|	|g}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
        NrE   r2   idcandles:candles_subscriber   T)	load_marketsmarket
safe_valuer;   safe_integerwatch_public
newUpdatesgetLimitfilter_by_since_limit)rA   rE   	timeframerF   rG   paramsrN   r2   intervalmarketIdmessageHash	reqParamsmethodohlcvs                 rC   watch_ohlcvzwhitebit.watch_ohlcvC   s       !!!!!!!!!V$$!__T\<DD
$$Z;;$<
 !6)x(	$''VYOOOOOOOO? 	2NN6511E))%q$GGGrD   clientc                 V   |                      |dg           }t          dt          |                    D ]}||         }|                     |d          }|                     |          }|d         }d|z   }	|                     ||          }
|| j        vr
i | j        |<   d| j        |         vr;|                     | j        dd          }t          |          }|| j        |         d<   | j        |         d         }|
                    |
           |                    ||	           |S )	NrV   r      rE   rK   unknown
OHLCVLimit  )rO   rangelensafe_stringsafe_marketparse_ohlcvohlcvsrP   r;   r   appendresolve)rA   r^   messagerV   idatarX   rN   rE   rY   parsedrG   storedr\   s                 rC   handle_ohlcvzwhitebit.handle_ohlcv_   s3   & (B77q#f++&& 	/ 	/A!9D''a00H%%h//FH%F)F2K%%dF33Fdk))&(F#V!444))$,dKK.u5517F#I.K'	2ELL   NN5+....rD   c                   K   |                                   d{V  |                     |          }|d}d|d         z   }d}|                     | j        di           }|                     |dd          }|                     |d|          }	|                     |d          }|d	         ||	d
g}
|                     |||
|           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
   
orderbook:rE   depth_subscriber   r1   0rJ   T)rM   rN   rO   r;   rf   omitrQ   rG   )rA   rE   rG   rV   rN   rY   r[   r;   defaultPriceIntervalr1   rZ   	orderbooks               rC   watch_order_bookzwhitebit.watch_order_book   s
      !!!!!!!!!V$$=E'&*::"//$,0@"EE#//#NN((BVWW6?334L	
	 ++KFSSSSSSSS	   rD   c                    |                      |dg           }|                      |d          }|                     |d          }|                     |          }|d         }|                      |d          }|                     |d          }	|| j        vr|                                 }
|
| j        |<   | j        |         }|	|d<   |                     |	          |d<   |r,|                     ||          }|                    |           nf|                      |dg           }|                      |d	g           }| 	                    |d         |           | 	                    |d	         |           d
|z   }|
                    ||           d S )NrV   r      rE      	timestampdatetimeasksbidsrt   )rO   rf   rg   safe_timestamp
orderbooks
order_bookiso8601parse_order_bookresethandle_deltasrk   )rA   r^   rl   rV   
isSnapshotrX   rN   rE   rn   r~   obry   snapshotr   r   rY   s                   rC   handle_order_bookzwhitebit.handle_order_book   s   L (B77__VQ//
##FA..!!(++!vq))''k::	$/))""B&(DOF#OF+	!*	+ $Y 7 7	* 	8,,T6::HOOH%%%%??444D??444Dy0$777y0$777'&0y+.....rD   c                     |                      |d          }|                      |d          }|                    ||           d S )Nr   r}   )
safe_floatstore)rA   booksidedeltapriceamounts        rC   handle_deltazwhitebit.handle_delta   sB    q))**uf%%%%%rD   c                 ~    t          dt          |                    D ]}|                     |||                    d S )Nr   )rd   re   r   )rA   r   deltasrm   s       rC   r   zwhitebit.handle_deltas   sH    q#f++&& 	3 	3Ahq	2222	3 	3rD   c                    K   |                                   d{V  |                     |          }|d         }d}d|z   }|                     |||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>`
        NrE   market_subscribeticker:F)rM   rN   watch_multiple_subscription)rA   rE   rV   rN   r[   rY   s         rC   watch_tickerzwhitebit.watch_ticker   s       !!!!!!!!!V$$!#&(55k66SXZ`aaaaaaaaarD   c                 l   |                      |dg           }|                     |d          }|                     |d           }|d         }|                      |di           }d|z   }|                     ||          }	|	| j        |<   |                    |	|           t          |j                                                  }
t          dt          |
                    D ]R}|
|         }|                    d          dk    r/|                    |          dk    r|                    |	|           S|S )NrV   r   rE   r}   r   tickers)rO   rf   rg   parse_tickerr   rk   listfutureskeysrd   re   find)rA   r^   rl   r   rX   rN   rE   	rawTickerrY   tickermessageHashesrm   currentMessageHashs                rC   handle_tickerzwhitebit.handle_ticker   s9   ( //'8R88##GQ//!!(D11!OOGQ33	$v-""9f55%Vv{+++V^002233q#m,,-- 	; 	;A!.q!1!&&y11Q66;M;R;RSY;Z;Z^_;_;_ v'9:::rD   c                 ,  K   |                                   d{V  |                     |          }|d         }d|z   }d}|                     |||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>`
        NrE   trades:trades_subscribeFr~   T)rM   rN   r   rR   rS   rT   	rA   rE   rF   rG   rV   rN   rY   r[   tradess	            rC   watch_tradeszwhitebit.watch_trades'  s       !!!!!!!!!V$$!$v-#77VVUZ\bcccccccc? 	3OOFE22E))&%TRRRrD   c                 P   |                      |dg           }|                     |d          }|                     |          }|d         }|                      | j        |          }|5|                     | j        dd          }t          |          }|| j        |<   |                      |dg           }	|                     |	|          }
t          dt          |
                    D ]}|
                    |
|                    d|d         z   }|                    ||           d S )NrV   r   rE   tradesLimitrc   r}   r   )rO   rf   rg   r   rP   r;   r   parse_tradesrd   re   rj   rk   )rA   r^   rl   rV   rX   rN   rE   rp   rG   rn   parsedTradesjrY   s                rC   handle_tradeszwhitebit.handle_trades;  s$   2 (B77##FA..!!(++!f55>%%dlM4HHE&&F"(DKvq"--((v66q#l++,, 	+ 	+AMM,q/****&"22v{+++++rD   c                   K   |t          | j        dz             |                                  d{V  |                                  d{V  |                     |          }|d         }d|z   }d}|                     |||d|           d{V }| j        r|                    ||          }|                     ||||d          S )a  
        watches trades made by the user
        :param str symbol: unified market symbol
        :param int [since]: the earliest time in ms to fetch trades for
        :param int [limit]: the maximum number of trades structures to retrieve
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=trade-structure>`
        Nz+ watchMyTrades() requires a symbol argumentrE   	myTrades:deals_subscribeT	r   rJ   rM   authenticaterN   r   rR   rS   filter_by_symbol_since_limitr   s	            rC   watch_my_tradeszwhitebit.watch_my_tradesd  s       >#DG.[$[\\\!!!!!!!!!!!!!!!!!!V$$!!F*"77VVUY[abbbbbbbb? 	3OOFE22E00tTTTrD   c                 H   |                      |d          }| j        0|                     | j        dd          }t	          |          | _        | j        }|                     |          }|                    |           |d         }d|z   }	|                    ||	           d S )NrV   r   rc   rE   r   )rO   myTradesrP   r;   r   parse_ws_traderj   rk   )
rA   r^   rl   subscriptiontraderG   rp   ro   rE   rY   s
             rC   handle_my_tradeszwhitebit.handle_my_tradesz  s    " 22= %%dlM4HHE&u--DM$$U++f!!F*v{+++++rD   c                    |                      |d          }|                     |d          }|                      |d          }|                      |d          }|                      |d          }|                      |d          }|                     ||          }d }	|                      |d          }
|
|
|d         d	}	|                     ||||                     |          |d
         |d d d ||d |	d|          S )N   r}   r         r|      quotecostcurrencyrE   )rJ   infor~   r   rE   ordertypesidetakerOrMakerr   r   r   fee)rf   r   rg   
safe_trader   )rA   r   rN   orderIdr~   rJ   r   r   rX   r   feeCosts              rC   r   zwhitebit.parse_ws_trade  s,    ""5!,,''q11	eQ''  **!!%++##E1--!!(F33""5!,,"7O C "Y//X&  
  
   	rD   c                   K   |t          | j        dz             |                                  d{V  |                                  d{V  |                     |          }|d         }d|z   }d}|                     |||d|           d{V }| j        r|                    ||          }|                     ||||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]{@link https://docs.ccxt.com/#/?id=order-structure
        Nz) watchOrders() requires a symbol argumentrE   orders:ordersPending_subscribeFTr   r   s	            rC   watch_orderszwhitebit.watch_orders  s       >#DG.Y$YZZZ!!!!!!!!!!!!!!!!!!V$$!&(*77VVUZ\bcccccccc? 	3OOFE22E00tTTTrD   c                    |                      |dg           }|                      |d          }| j        0|                     | j        dd          }t	          |          | _        | j        }|                     |d          }|                     |                     |d|i                    }	|                    |	           |	d         }
d|
z   }|                    | j        |           d S )	NrV   r}   ordersLimitrc   r   statusrE   r   )	rO   ordersrP   r;   r   parse_ws_orderextendrj   rk   )rA   r^   rl   r   rV   rn   rG   rp   r   ro   rE   rY   s               rC   handle_orderzwhitebit.handle_order  s    6 (B77vq));%%dlM4HHE077DK""61--$$T[[&7I%J%JKKf!&(t{K00000rD   c           	         |                      |d          }|                     |d          }|                     ||          }|                     |d          }|                     |                     |d                    }|                     |d          }|                     |d          }|                     |d          }	|                     |d          }
|                     |d	          }|                     |          }d }d }|dk    r|                     |d          }d
}n,|                     |d          }|                     |d          }|                     |d          }|                     |d          }|d         }|                      |d          }|dk    rdnd}|                     |d          }d }||                     |          |d         d}d }|dk    s|dk    rd}nt          j        |d
          rd}nd}| 	                    i d|d|d|d|d|d| 
                    |          d|d	|d d d!d d|d|d"|
d#|
d|d$|	d%d ||||d d&|          S )'Nr   rN   rJ   client_order_idr   
deal_stock
deal_moneyactivation_pricer   rv   leftr   ctimemtimerE   r   r}   sellbuydeal_feer   r   r|   openclosedcanceledr   clientOrderIdr~   r   lastTradeTimestamptimeInForcepostOnly	stopPricetriggerPricer   average)filled	remainingr   r   r   )rP   rf   rg   	omit_zeroparse_ws_order_typer   parse_numberr   string_equals
safe_orderr   )rA   r   rN   r   rX   rJ   r   r   r   r   r   rawTyper   r   r   r~   r   rE   rawSider   dealFeer   unifiedStatuss                          rC   r   zwhitebit.parse_ws_order  sF   . ""5(33##E844!!(F33eT**t'7'7?P'Q'QRR  00!!%66|44$$U,>??	""5&11''00	8%%e\::FII((77I%%eX66F''w77	!00@@!##E622!Q,,vvU""5*55))'22"7O C aKKVq[["MM$Y44 + ( *  
E 
f 
 " 
 ]	 

  
 Y// 
 !"4 
 D 
 4 
  
 D 
 U 
  
 I 
 f 
  D! 
" t# 
$ "#- 
  
  
. /  	rD   c           
      H    dddddddddd	}|                      |||          S )NrG   rN   )	r5   r6   2023r7   5r8   810)rf   )rA   r   statusess      rC   r   zwhitebit.parse_ws_order_typeV  sC    

 

 &&999rD   c                 ,  K   |                                   d{V  d}|                     dd|          \  }}d}d}|dk    rd}|dz  }nd}|dz  }t          | j                                                  }|                     ||||           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
        :param str [params.type]: spot or contract if not provided self.options['defaultType'] is used
        :returns dict: a `balance structure <https://docs.ccxt.com/#/?id=balance-structure>`
        Nr   wallet:spotbalanceSpot_subscribebalanceMargin_subscribemargin)rM   handle_market_type_and_paramsr   
currenciesr   watch_private)rA   rV   r   rY   r[   r  s         rC   watch_balancezwhitebit.watch_balanced  s       !!!!!!!!!99.$PVWWf6>>,F6!KK.F8#K$/..0011
''VZPPPPPPPPPrD   c                    |                      |d          }|                     |d          }|                     |d          }|| j        d<   t          |                                          }|                     |d          }|                     ||          }|                     |          }	|                                 }
|                      |d          |
d<   |                      |d          |
d<   |
| j        |	<   |                     | j                  | _        d	}|                    d
          dk    r|dz  }n|dz  }|	                    | j        |           d S )Nr[   rV   r   r   	availablefreefreezeusedr  Spotr  r
  )
rf   rO   balancer   r   safe_currency_codeaccountsafe_balancer   rk   )rA   r^   rl   r[   rn   balanceDictr   
currencyId
rawBalancecoder  rY   s               rC   handle_balancezwhitebit.handle_balancey  sM    !!'844w11oodA..*VK$$&&''__T1--
__[*==
&&z22,,..**:{CC**:x@@$T((66;;v!##6!KK8#Kt|[11111rD   c                    K   | j         d         d         }|                                 }|||d}|                     ||          }|                     ||||           d {V S Nr   r   rJ   r[   rV   )r:   noncer   watch	rA   rY   r[   rZ   rV   urlrJ   requestrl   s	            rC   rQ   zwhitebit.watch_public  sx      it$ZZ\\
 

 ++gv..ZZ[';GGGGGGGGGrD   Fc                 L  K   |                                   d {V  | j        d         d         }|                                 }|                     | j        |          }d }	g }
|gi }|                     |          }|d         }d||<   |g}
|r|
g}
|||
d}	|                     |	|          }|                     |||||           d {V S |                     |j        |i           }d}|                     |          }|d         }| 	                    ||d          }|sd||<   d}|r|                     |||	||           d {V S g }t          |                                          }|r|g}|||d}||j        v r|j        |= |                     |||||           d {V S )Nr   r   rJ   Tr   F)rM   r:   r!  rO   clientsrN   r   r"  subscriptions	safe_boolr   r   )rA   rY   r[   rE   isNestedrV   r$  rJ   r^   r%  	marketIdsr   rN   rX   rl   hasSymbolSubscriptionisSubscribedmarketIdsNewresubRequests                      rC   r   z$whitebit.watch_multiple_subscription  s'     !!!!!!!!!it$ZZ\\s33	>L[[((Fd|H%)L"!
I (&K	 # G
 kk'622GCgv|TTTTTTTTT??6+?LLL$(![[((Fd|H>>,%HHL .)-X&(-%$ ^!ZZ['6<XXXXXXXXX  "#L$5$5$7$788 2$0>L$*   
 V111,V4!ZZ[,P\]]]]]]]]]rD   c                 $  K   |                                   |                                  d {V  | j        d         d         }|                                 }|||d}|                     ||          }|                     ||||           d {V S r  )check_required_credentialsr   r:   r!  r   r"  r#  s	            rC   r  zwhitebit.watch_private  s      '')))!!!!!!!!!it$ZZ\\
 

 ++gv..ZZ[';GGGGGGGGGrD   c                 L  K   |                                   | j        d         d         }d}|                     |          }|                    d          }|                     |j        |          }||                                  d {V }|                     |d          }|                                 }	|	d|dgd}
|	| j	        d}	 | 
                    |||
||           d {V  n4# t          $ r'}|j        |= |                    |           Y d }~nd }~ww xY w| d {V S )	Nr   r   authenticatedwebsocket_token	authorizepublicr   )rJ   r[   )r1  r:   r^   futurerO   r(  "v4PrivatePostProfileWebsocketTokenrf   r!  handle_authenticater"  	Exceptionreject)rA   rV   r$  rY   r^   r7  r3  	authTokentokenrJ   r%  r   es                rC   r   zwhitebit.authenticate  s{     '')))it$%S!!//(<kJJ "EEGGGGGGGGI $$Y0ABBEB% G 2 L!jjk7KVVVVVVVVVV ! ! !(5a        ! ||||||s   
C* *
D4DDc                 J    |j         d         }|                    d           |S )Nr3  r}   )r   rk   )rA   r^   rl   r7  s       rC   r9  zwhitebit.handle_authenticate  s'     0qrD   c                    |                      |d          }	 |^|                     |d          }| j        dz   |                     |          z   }|                     | j        d         d         ||           nY# t          $ rL}t          |t                    r-|	                    |d           d|j
        v r|j
        d= Y d }~dS Y d }~nd }~ww xY w|S )Nerrorr   r   r4   r3  F)rO   rf   rJ   jsonthrow_exactly_matched_exceptionr=   r:  
isinstancer   r;  r(  )rA   r^   rl   rA  r  feedbackr>  s          rC   handle_error_messagezwhitebit.handle_error_message  s    11
	 ''887S=499W+=+==44T_T5J75SUY[cddd 	 	 	!011 a111"f&:::,_=uuuuu	    	 s   A A9 9
C<C

Cc           
         |                      ||          sd S |                     |d          }|dk    r|                     ||           d S |                     |d          }||                     |||           d S | j        | j        | j        | j        | j	        | j	        | j
        | j
        | j        d	}|                     |d          }|                     ||          }| |||           d S d S )NresultpongrJ   )	market_updatetrades_updatedepth_updatecandles_updateordersPending_updateordersExecuted_updatebalanceSpot_updatebalanceMargin_updatedeals_updater[   )rG  rf   handle_pongrP   handle_subscription_statusr   r   r   rq   r   r  r   rO   )rA   r^   rl   rI  rJ   methodstopicr[   s           rC   handle_messagezwhitebit.handle_message&  s#    ((99 	F!!'844VVW---Fw-->++FGR@@@F!/!/ 2"/$($5%)%6"&"5$($7 1

 

 22%00F67##### rD   c                 8   |j         }t          |                                          }t          dt	          |                    D ]S}||         }|durE|                     |d          }|-||k    r'|                     |d          }	|	 |	||            d S Td S )Nr   TrJ   r[   )r(  r   valuesrd   re   rP   rO   )
rA   r^   rl   rJ   subsrZ  rm   r   subIdr[   s
             rC   rU  z#whitebit.handle_subscription_statusH  s     #dkkmm$$q#f++&& 	 	A!!9L4'')),==%ERKK!__\8DDF)vw///	 	rD   c                 8    |                                  |_        |S N)millisecondslastPong)rA   r^   rl   s      rC   rT  zwhitebit.handle_pongW  s    ++--rD   c                     ddg dS )Nr   r3   r    )rA   r^   s     rC   r3   zwhitebit.ping[  s    
 
 	
rD   r^  )-__name__
__module____qualname__r@   strr   r   r   r]   r   rq   r	   rz   r   r   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r   r   r  r  rQ   r   r  r   r9  rG  rX  rU  rT  r3   __classcell__)rB   s   @rC   r   r      sU       . . . . .` 8<$]ajl H H HC HWZ Hquvzq{ H H H H8&6 & & & &P @DB ! !S ! !S\ ! ! ! !6</ </ </ </ </|& & &
3 3 3 68 b b bF b b b b-F - - - -^ <@d[] S S SS S Sbfglbm S S S S(',F ', ', ', ',R 37TX\eg U UC Us URU Ulpqvlw U U U U,, ,v , , , ,8) ) ) )V 04$UYbd U U UC Us Uimnsit U U U U,&1 &16 &1 &1 &1 &1PU U U Un: : : *, Q Q Q Q Q Q*!2V !2 !2 !2 !2F AC2 	H 	H 	H 	H W\df /^ /^ /^ /^b BDB H H H H )+ ! ! ! !F&    6    , $V  $  $  $  $D    &    
 
 
 
 
 
 
rD   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r	   r
   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   r   ccxt.base.preciser   async_supportr   rb  rD   rC   <module>rq     s       f f f f f f f f f f O O O O O O O O O O O O O O O O O O 4 4 4 4 4 4       0 0 0 0 0 0 . . . . . . ' ' ' ' ' ' % % % % % %O
 O
 O
 O
 O
t!* O
 O
 O
 O
 O
rD   