
    ei~l                         d dl Zd dlmZmZ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  G d dej        j                  ZdS )	    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesBoolIntOrder	OrderBookPositionStrStringsTickerTrade)Client)List)ExchangeErrorc            
       "    e Zd Zdef fdZdefdZdedededefdZ	i fd	ede
fd
Z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efdZd(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di fd	edededee         fdZdefdZd(dZi fdZi fdefdZdefdZd Z dddi fde!dededee"         fdZ#d  Z$di fd!ee         dee         de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efd%Z*d(de(fd&Z+dede,fd'Z- xZ.S ))arkhamreturnc                     |                      t          t          |                                           ddddddddddddddddiiddd	d
iddid          S )NTF)wswatchTradeswatchTradesForSymbolswatchOrderBookwatchOrderBookForSymbols
watchOHLCVwatchOHLCVForSymbolswatchOrderswatchMyTradeswatchTickerwatchTickerswatchBalanceapir   zwss://arkm.com/wsr   d   i  )depthinterval	keepAlivei )hasurlsoptions	streaming)deep_extendsuperr   describe)self	__class__s    M/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/arkham.pyr0   zarkham.describe   s    fd 3 3 < < > >#)."&,1"(-#!&# % $  - !  ## # V5A
 A
   	    clientc           	      $   |                      ||          rd S | j        | j        | j        | j        | j        | j        | j        | j        d}|                     |d          }|dk    rd S | 	                    ||          }| |||           d S d S )N)tickercandles
l2_updatestradesbalances	positionsorder_statusestrigger_orderschannelconfirmations)
handle_error_messagehandle_tickerhandle_ohlcvhandle_order_bookhandle_tradeshandle_balancehandle_positionshandle_ordersafe_string
safe_value)r1   r5   messagemethodsr?   methods         r3   handle_messagezarkham.handle_message1   s    
 $$VW55 	F((0(+."/"/

 

 ""7I66o%%F
 '22F67##### r4   messageHash
rawChannelparamsc                    K   |}||d|                                  dd}|                     | j        d         d         |||           d {V S )N)r?   rQ   	subscribe)argsconfirmationIdrM   r%   r   )uuidwatchr+   )r1   rO   rP   rQ   subscriptionHashrequests         r3   rS   zarkham.subscribeN   ss      & &   #iikk!
 
 ZZ	% 0 6WN^_________r4   symbolc                    K   |                                   d{V  |                     |          }d|d         i}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

        https://arkm.com/docs#stream/ticker

        :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>`
        NrZ   idticker::r7   )load_marketsmarketrS   extend)r1   rZ   rQ   r_   
requestArgrO   s         r3   watch_tickerzarkham.watch_tickerZ   s       !!!!!!!!!V$$fTl

 !6(#33^^K4;;vz;Z;Z[[[[[[[[[r4   c                    |                      |di           }|                     |d          }|                     |d           }|d         }|                     ||          }|| j        |<   |                    |d|z              d S )NdatarZ   r]   )	safe_dictrI   safe_marketparse_ws_tickertickersresolve)r1   r5   rK   rd   marketIdr_   rZ   r7   s           r3   rB   zarkham.handle_tickerl   s    8 ~~gvr22##D(33!!(D11!%%dF33%VvzF233333r4   Nc                 .    |                      ||          S N)parse_ticker)r1   rK   r_   s      r3   rg   zarkham.parse_ws_ticker   s      &111r4   1msincelimitc                   K   |                                   d{V  |                     |          }|                     | j        ||          }|d         |d}d|d         z   dz   |z   }	|                     |	d|                     ||                     d{V }
|
}| j        r|                    |d         |          }|                     |||dd	          S )
a  
        watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

        https://arkm.com/docs#stream/candles

        :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
        Nr\   )rZ   durationohlcv::rZ   ::r8   r   T)	r^   r_   rI   
timeframesrS   r`   
newUpdatesgetLimitfilter_by_since_limit)r1   rZ   	timeframero   rp   rQ   r_   rawTimeframera   rO   resultohlcvs               r3   watch_ohlcvzarkham.watch_ohlcv   s       !!!!!!!!!V$$''INNTl$
 

  &"22T9LH~~k9dkk*V\>]>]^^^^^^^^? 	<NN6(#3U;;E))%q$GGGr4   c                 ~   |                      |di           }|                     |d          }|                     |d           }|d         }|                     |d          }|                     |          }d|z   dz   |z   }	|                     | j        |i           | j        |<   || j        |         vr4|                     ddd          }
t          |
          | j        |         |<   | j        |         |         }| 	                    ||          }|
                    |           |                    ||	           |S )	Nrd   rZ   rr   rs   rt   r   rp     )re   rI   rf   safe_integerfindTimeframeByDurationrJ   ohlcvshandle_optionr   parse_ws_ohlcvappendri   )r1   r5   rK   rd   rj   r_   rZ   rr   ry   rO   rp   storedparseds                r3   rC   zarkham.handle_ohlcv   s:   $ ~~gvr22##D(33!!(D11!$$T:6600::	&(4/);"oodk62FFFT[000&&|WdCCE-B5-I-IDK	*V$Y/$$T622fv{+++r4   c                 .    |                      ||          S rl   )parse_ohlcv)r1   r|   r_   s      r3   r   zarkham.parse_ws_ohlcv       v...r4   c                   K   |                                   d{V  |                     |          }|d         dd}d|d         z   }|                     |d|                     ||                     d{V }|                                S )a#  
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://arkm.com/docs#stream/l2_updates

        :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
        Nr\   T)rZ   snapshotorderBook::rZ   r9   )r^   r_   rS   r`   rp   )r1   rZ   rp   rQ   r_   ra   rO   	orderbooks           r3   watch_order_bookzarkham.watch_order_book   s       !!!!!!!!!V$$Tl
 

 $fX&66..lDKKPZ\bDcDcdddddddd	   r4   c           	      ,   |                      |d          }|                     |d          }|                     |d          }|                     |          }|d         }d|z   }|| j        vr$|                     i           }	||	d<   |	| j        |<   | j        |         }
|dk    rH|                     |dd          }|                     |||dd	d
d          }|
                    |           n||dk    rv|                     |dd          }|                     |d          }|dk    r|
d         n|
d	         }|                     ||           ||
d<   | 	                    |          |
d<   |
| j        |<   |
                    | j        |         |           d S )Nrd   typerZ   r   r   lastTimegMbP?bidsaskspricesizeupdatetimesidebuy	timestampdatetime)re   rI   rf   
orderbooks
order_booksafe_integer_productparse_order_bookresethandle_deltaiso8601ri   )r1   r5   rK   rd   r   rj   r_   rZ   rO   obr   r   parsedOrderBookr   booksides                  r3   rD   zarkham.handle_order_book   s   @ ~~gv..00##D(33!!(++!#f,$/))$$B!BxL&(DOF#OF+	:11$
EJJI"33D&)VU[]dflmmOOOO,,,,X11$FFI##D&11D-1U]]y((6ARHh---%.Ik"$(LL$;$;Ij!"+tv.<<<<<r4   c                 ^    |                      |dd          }|                    |           d S )Nr   r   )parse_bid_ask
storeArray)r1   r   deltabidAsks       r3   r   zarkham.handle_delta'  s3    ##E7F;;F#####r4   c                 h  K   |                                   d{V  |                     |          }d|d         i}d|d         z   }|                     |d|                     ||                     d{V }| j        r|                    |d         |          }|                     |||dd          S )a  
        watches information on multiple trades made in a market

        https://arkm.com/docs#stream/trades

        :param str symbol: unified market symbol of the market trades were made in
        :param int [since]: the earliest time in ms to fetch orders for
        :param int [limit]: the maximum number of trade 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>`
        NrZ   r\   trade::r:   r   T)r^   r_   rS   r`   rv   rw   rx   )	r1   rZ   ro   rp   rQ   r_   ra   rO   r:   s	            r3   watch_tradeszarkham.watch_trades+  s       !!!!!!!!!V$$fTl

  &"22~~k8T[[U[=\=\]]]]]]]]? 	=OOF8$4e<<E))&%TRRRr4   c                    |                      |d          }|                     |d          }|                     |          }|| j        vr3|                     | j        dd          }t          |          | j        |<   |                     |          }| j        |         }|                    |           |	                    |d|z              d S )Nrd   rZ   tradesLimitr   r   )
re   rI   safe_symbolr:   r   r,   r   parse_ws_trader   ri   )	r1   r5   rK   rd   rj   rZ   rp   r   r   s	            r3   rE   zarkham.handle_tradesB  s     ~~gv..##D(33!!(++$+%%%%dlM4HHE",U"3"3DK$$T**V$fvy6122222r4   c                 .    |                      ||          S rl   )parse_trade)r1   trader_   s      r3   r   zarkham.parse_ws_trade\  r   r4   c           	        K   |                                   |                                 |                     | j        dd          z   dz  }|                     | j        di           }|                     |d          }|d}d}d}| j        t          |          z   |                                z   dz   |z   |z   }| 	                    | j
                  }	|                     |                     |          |	t          j        d	          }
dd
ddd| j        t          |          |
diii}|                     |           |                     | j        d         d                    d S d S )NrequestExpirationi  r   r   tokenGET /base64r,   headerszapplication/json)zContent-TypeAcceptzArkham-Api-KeyzArkham-ExpireszArkham-Signaturer%   )check_required_credentialsmillisecondsr   r,   re   rI   apiKeystrupperbase64_to_binarysecrethmacencodehashlibsha256extend_exchange_optionsr5   r+   )r1   rQ   expires	wsOptionsauthenticatedrM   bodyStrpathpayloaddecodedSecret	signaturedefaultOptionss               r3   authenticatezarkham.authenticate`  sr     '')))$$&&):):4<I\^b)c)ccgkk..tR@@	((G<< FGDkCLL06<<>>ACG$NQXXG 11$+>>M		$++g"6"6w~W_``I!,>&8.2k.1'll09$ $ 
$N ((888KK	%(./////+ ! r4   c                    K   |                                   d{V  |                                  d{V  ddi}d}|                     |d|                     ||                     d{V }|S )a  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://arkm.com/docs#stream/balances

        :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   Tr;   )r   r^   rS   r`   )r1   rQ   ra   rO   r{   s        r3   watch_balancezarkham.watch_balance|  s       !!!!!!!!!!!!!!!!!!

 !~~k:t{{:W]?^?^________r4   c                    |                      |d          }i }|dk    r8|                     |d          }|                     |          }||d<   || _        ni|                     |d          }|g}|                     |          }|                      |d          }|                     |          }||         | j        |<   d}	|                    |                     | j                  |	           d S )Nr   r   rd   inforZ   r;   )rI   	safe_listparse_ws_balancebalancere   safe_currency_coderi   safe_balance)
r1   r5   rK   r   r   rd   balancesArray
currencyIdcoderO   s
             r3   rF   zarkham.handle_balance  s    r 00:>>'622D**400F$F6N!DLL>>'622D!FM**=99F))$99J**:66D!'DL t((66DDDDDr4   c                 ,    |                      |          S rl   )parse_balance)r1   r   s     r3   r   zarkham.parse_ws_balance  s    !!'***r4   symbolsc                   K   |                                   d{V  |                                  d{V  d}|                     |          s0|                     |          }|dd                    |          z   z  }t                      | _        ddi}|                     |d|                     ||                     d{V }| j	        r|S | 
                    | j        |||d          S )a  

        https://arkm.com/docs#stream/positions

        watch all open positions
        :param str[] [symbols]: list of unified market symbols
        :param int [since]: the earliest time in ms to fetch positions for
        :param int [limit]: the maximum number of positions to retrieve
        :param dict params: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `position structure <https://docs.ccxt.com/en/latest/manual.html#position-structure>`
        Nr<   rt   ,r   FT)r   r^   is_emptymarket_symbolsjoinr   r<   rS   r`   rv   filter_by_symbols_since_limit)r1   r   ro   rp   rQ   rO   ra   newPositionss           r3   watch_positionszarkham.watch_positions  s      !!!!!!!!!!!!!!!!!!!}}W%% 	4))'22G4#((7"3"333K133

 "^^KdkkR\^dFeFeffffffff? 	 11$.'5RWY]^^^r4   c                 "   g }| j         i | _         |                     |d          }|dk    r|                     |dg           }t          dt	          |                    D ]l}|                     ||                   }|                     |d          dk    r5|                    |           |                     |d          }|| j         |<   mn`|                     |d          }|                     |          }|                     |d          }|| j         |<   |                    |           | 	                    |d          }	t          dt	          |	                    D ]}|	|         }
|

                    d          }|d	         }|
                    d
          }|                     |d|d          }|                     |          s|                    ||
           t	          |          }|dk    r|                    |d           d S d S )Nr   r   rd   r   
entryPricerZ   zpositions::rt      r   Fr<   )r<   rI   r   rangelenparse_ws_positionr   r   re   find_message_hashessplitfilter_by_arrayr   ri   )r1   r5   rK   r   r   rd   ipositionrZ   messageHashesrO   partssymbolsStringr   r<   lengths                   r3   rG   zarkham.handle_positions  s   B >!DN00:>>'6266D1c$ii(( 6 611$q'::$$X|<<AA ''111!--hAAF-5DN6*6 >>'622D--d33H%%h99F%-DN6")))00GGq#m,,-- 	7 	7A'*K%%d++E!!HM#))#..G,,\8WeTTI==++ 7y+666\""A::NN<55555 :r4   r<   c                 T   |                      |          }|                     |          }g }t          dt          |                    D ]G}|                     |                     ||         d           |          }|                    |           H|                     |d|d          S )Nr   rZ   F)r   to_arrayr   r   r`   r   r   filter_by_array_positions)r1   r<   r   rQ   r{   r   r   s          r3   parse_ws_positionszarkham.parse_ws_positions9  s    %%g..MM),,	q#i..)) 	$ 	$A{{4#9#9)A,#M#MvVVHMM(####--fhOOOr4   c                 .    |                      ||          S rl   )parse_position)r1   r   r_   s      r3   r   zarkham.parse_ws_positionB  s    ""8V444r4   c                   K   |                                   d{V  |                                  d{V  d}||                     |          }ddi}d}|                     |ddd          \  }}|rdnd}d}	||	d	|d
         z   z  }	|	d	|z   z  }	|                     |	||                     ||                     d{V }
| j        r|
                    ||          }|                     |
|||d          S )a   
        watches information on multiple orders made by the user

        https://arkm.com/docs#stream/order_statuses

        :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>`
        Nr   Fr    triggerr>   r=   ordersrt   rZ   T)	r   r^   r_   handle_option_and_paramsrS   r`   rv   rw   filter_by_symbol_since_limit)r1   rZ   ro   rp   rQ   r_   ra   isTriggerOrderrP   rO   r  s              r3   watch_orderszarkham.watch_ordersF  sM      !!!!!!!!!!!!!!!!!![[((F

 !%!>!>v}V_af!g!g)7M%%=M
4&"222Ktj((~~k:t{{:W]?^?^________? 	3OOFE22E00tTTTr4   c                    |                      |d          }|                     |d          }| j        0|                     | j        dd          }t          |          | _        | j        }|                     |          }|                    |           |                    |d           |                    |d|d         z   dz   |z              |                    |d|z              d S )	Nr?   rd   ordersLimitr   r  zorders::rZ   rt   )	rI   re   r  r   r,   r   parse_ws_orderr   ri   )r1   r5   rK   r?   rd   rp   r  orders           r3   rH   zarkham.handle_orderf  s    J ""7I66~~gv..;%%dlM4HHE077DK##D))evx(((vzE(O;dBWLMMMvzG344444r4   c                 .    |                      ||          S rl   )parse_order)r1   r  r_   s      r3   r
  zarkham.parse_ws_order  r   r4   c                    |                      |d          }||                     |          }|                      |d          }| j        dz   |z   }|                     | j        d         ||           |                     | j        d         ||           |                     | j        d         ||           t          | j        dz   |z             dS )NrK   r\    exactbroadF)rI   jsonr\   throw_exactly_matched_exception
exceptionsthrow_broadly_matched_exceptionr   )r1   r5   responserK   body	errorCodefeedbacks          r3   rA   zarkham.handle_error_message  s     ""8Y7799X&&D((488Iw}t+H001I9V^___001I7T\]]]001I7T\]]]# 4555ur4   rl   )/__name__
__module____qualname__r   r0   r   rN   r   dictrS   r   rb   rB   rg   r
   r   listr}   rC   r   r   r   rD   r   r   r   rE   r   r   r   r   rF   r   r   r   r   rG   r   r   r   r   r  rH   r
  r	   rA   __classcell__)r2   s   @r3   r   r      sW       #      @$V $ $ $ $:
`3 
`C 
` 
`RU 
` 
` 
` 
` 68 \ \ \F \ \ \ \$"4F "4 "4 "4 "4N2 2 2 2 8<$]ajl H H HC HWZ Hquvzq{ H H H H6!6 ! ! ! !F/ /D / / / / @DB ! !S ! !S\ ! ! ! !*7= 7= 7= 7= 7=r$ $ $ <@d[] S S SS S Sbfglbm S S S S.3F 3 3 3 34/ / / / )+ 0 0 0 08 *,      &IEV IE IE IE IEV+ + + 8<$]ajl _ _W _C _WZ _quv~q _ _ _ _6>6 >6 >6@ MQY[ P PDI PS	 P`dem`n P P P P5 5 5 5 04$UYbd U U UC Us Uimnsit U U U U@/56 /5 /5 /5 /5b/ /E / / / /6         r4   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   r   r   ccxt.base.typesr   r   r	   r
   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   async_supportr    r4   r3   <module>r)     s,       A  A  A  A  A  A  A  A  A  A  A  A  m m m m m m m m m m m m m m m m m m m m m m m m m m 4 4 4 4 4 4       * * * * * *_
 _
 _
 _
 _
T& _
 _
 _
 _
 _
r4   