
    E#i                         S SK rS SKJrJrJr  S SKJrJrJ	r	J
r
JrJrJrJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S SKJr   " S S	\R4                  R6                  5      rg)
    N)
ArrayCacheArrayCacheBySymbolByIdArrayCacheByTimestamp)AnyBoolIntMarketOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)ExchangeError)ArgumentsRequiredc            
       ,  ^  \ rS rSrS\4U 4S jjr0 S4S jr0 S4S jrS\S\	\   S	\
4S
 jr0 4S\S\4S jjr0 4S\S\4S jjrS0 4S\S\4S jjrS0 4S\S\4S jjrS\4S jrSBS\
S\S\4S jjrS0 4S\S\4S jjrS0 4S\S\4S jjrS\4S jrSBS jrSSS0 4S\S\S\S\	\   4S jjrS0 4S\S\S\4S  jjrSS0 4S!\	\	\      S\S\4S" jjr0 4S!\	\	\      S\4S# jjr S\4S$ jr!SBS\4S% jjr"SS0 4S\S\S\S\	\#   4S& jjr$0 4S\S\4S' jjr%SS0 4S\	\   S\S\S\	\#   4S( jjr&0 4S\	\   S\4S) jjr'S\4S* jr(SBS+ jr)S0 4S\S\S\*4S, jjr+S0 4S\	\   S\S\*4S- jjr,0 4S\S\4S. jjr-0 4S\	\   S\4S/ jjr.S\4S0 jr/S1 r0S2 r1S3 r2SSS0 4S\3S\S\S\	\4   4S4 jjr5S0 4S\3S\4S5 jjr6S\4S6 jr7SBS7 jr8SBS8 jr9S9\34S: jr:SSS0 4S\S\S\S\	\;   4S; jjr<S0 4S\S\	\   4S< jjr=S= r>SBS> jr?S\4S? jr@S\S\A4S@ jrBSArCU =rD$ )Cbackpack   returnc                    > U R                  [        [        U ]  5       0 SS_SS_SS_SS_SS_SS_S	S_S
S_SS_SS_SS_SS_SS_SS_SS_SS_SS_SSSSSSSSS.ESSSSS.00S0 0U R                  SS.S.5      $ )NwsTwatchBalanceFwatchBidsAskswatchMyTrades
watchOHLCVwatchOHLCVForSymbolswatchOrderBookwatchOrderBookForSymbolswatchOrderswatchPositionswatchTickerwatchTickerswatchTradeswatchTradesForSymbolsunwatchBidsAsksunwatchOHLCVunwatchOHLCVForSymbols)unwatchOrderBookunwatchOrderBookForSymbolsunwatchTickerunwatchTickersunWatchTradesunWatchTradesForSymbolsunWatchOrdersunWatchPositionsapizwss://ws.backpack.exchange)publicprivate
timeframesi )ping	keepAlive)hasurlsoptions	streaming)deep_extendsuperr   describer8   )self	__class__s    K/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/pro/backpack.pyr@   backpack.describe   sR   h > @d    	
 d ' !$ +D t !$ t  t ( "4  !" )$#$ %).2!%"&!%+/!%$(38 ">#?  
 		#Q,C
 , ,	    Fc                   #    U R                  5       I S h  vN   U R                  S   S   S   nU(       a  SOSnUUS.nU R                  Xs5      nU(       a  U R                  XRU5        g U R	                  XRX5      I S h  vN $  Nn N7f)Nr4   r   r5   UNSUBSCRIBE	SUBSCRIBE)methodparams)load_marketsr;   r>   handle_unsubscriptionswatch_multiple)	rA   topicsmessageHashesrJ   unwatchurlrI   requestmessages	            rC   watch_publicbackpack.watch_public@   s     !!!iit$X."){
 ""73''GD((WTTT 	" Us"   B	BA)B	 BB	B	c                 X  #    U R                  5         U R                  S   S   S   nSn[        U R                  5       5      nU(       a  SOSnU R	                  U R
                  SSS	5      n	S
U-   S-   S-   U-   S-   U	-   n
U R                  U R                  5      nU R                  USS5      nU R                  U R                  U
5      US5      nUUU R                  XU	/S.nU R                  X5      nU(       a  U R                  XRU5        g U R                  XRX5      I S h  vN $  N7f)Nr4   r   r6   	subscriberG   rH   
recvWindowzX-Window5000zinstruction=&z
timestamp=z&window=r       ed25519)rI   rJ   	signature)check_required_credentialsr;   strnoncesafe_string_2r<   base64_to_binarysecretarray_sliceeddsaencodeapiKeyr>   rL   rM   )rA   rN   rO   rJ   rP   rQ   instructiontsrI   rX   payloadsecretBytesseedr]   rR   rS   s                   rC   watch_privatebackpack.watch_privateN   s%    '')iit$Y/!"){''lJPVW
 ;.4|CbH:UXbb++DKK8Q3JJt{{73T9E	++yjA

 ""73''GD((WTTTTs   D!D*#D($D*rQ   rO   rS   c                    U R                  U5      nU R                  XX25        [        S[        U5      5       GH  nX%   nUR	                  SS5      nU R                  XGU5        UR                  S5      S:  a2  UR	                  SS5      nXR                  ;   a  U R                  U	 Mq  Ms  UR                  S5      S:  a2  UR	                  SS5      nXR                  ;   a  U R                  U	 M  M  UR                  S5      S:  ao  UR                  S	5      n	U R                  U	S
5      nU R                  U	S5      n
XR                  ;   a(  XR                  U   ;   a  U R                  U   U
	 GM8  GM;  GM>  UR                  S5      S:  a4  UR	                  SS5      nXR                  ;   a  U R                  U	 GM  GM  UR                  S5      S:  a4  UR	                  SS5      nXR                  ;   a  U R                  U	 GM  GM  UR                  S5      S:  a  US:X  aU  U R                  n[        UR!                  5       5      n[        S[        U5      5       H  nX   nU R                  U	 M     GM@  UR	                  SS5      nXR                  ;   a  U R                  U	 GMq  GMt  UR                  S5      S:  d  GM  US:X  aU  U R"                  n[        UR!                  5       5      n[        S[        U5      5       H  nX   nU R"                  U	 M     GM  UR	                  SS5      nXR"                  ;   d  GM  U R"                  U	 GM     g )Nr   zunsubscribe: tickerunsubscribe:ticker:bidaskunsubscribe:bidask:candles:      	orderbookunsubscribe:orderbook:tradesunsubscribe:trades:ordersunsubscribe:ordersunsubscribe:orders:	positionsunsubscribe:positionsunsubscribe:positions:)clientrM   rangelenreplaceclean_unsubscriptionfindtickersbidsaskssplitsafe_stringohlcvs
orderbooksr{   r}   listkeysr   )rA   rQ   rO   rS   r   imessageHashsubMessageHashsymbolsplitHashes	timeframecacher   js                 rC   rL   backpack.handle_unsubscriptionsd   s   S!CGq#m,-A'*K(00DN%%fkJ)Q.$,,-BBG\\)V, *!!(+q0$,,-BBG]]*f- +!!),1)//4))+q9 ,,[!<	[[( KK$77 KK/	: 8 ) !!+.!3$,,-ErJ__,/ -!!(+q0$,,-BBG[[(F+ )!!(+q0"66 KKE

-D"1c$i0!% KK/ 1 )001FKF, KK/ -!!+.!3"99 NNE

-D"1c$i0!% NN62 1 )001I2NF/ NN62a .rE   r   c                    #    U R                  5       I Sh  vN   U R                  U5      nUS   nSUS   -   nSU-   nU R                  U/U/U5      I Sh  vN $  ND N7f)a  
watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

https://docs.backpack.exchange/#tag/Streams/Public/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>`
Nr   ticker.idticker:)rK   marketrT   rA   r   rJ   r   topicr   s         rC   watch_tickerbackpack.watch_ticker   sp      !!!V$!-$v-&&wvFFF 	"
 Gs!   AA?AAAAc                 D   #    U R                  U/U5      I Sh  vN $  N7f)a  
unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

https://docs.backpack.exchange/#tag/Streams/Public/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>`
N)un_watch_tickersrA   r   rJ   s      rC   un_watch_tickerbackpack.un_watch_ticker   s"      **F8V<<<<     Nsymbolsc                   #    U R                  5       I Sh  vN   U R                  USS5      n/ n/ n[        S[        U5      5       H@  nX   nU R	                  U5      nUR                  SU-   5        UR                  SU-   5        MB     U R                  XCU5      I Sh  vN   U R                  U R                  SU5      $  N N#7f)  
watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

https://docs.backpack.exchange/#tag/Streams/Public/Ticker

:param str[] symbols: 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>`
NFr   r   r   r   )	rK   market_symbolsr   r   	market_idappendrT   filter_by_arrayr   )rA   r   rJ   rO   rN   r   r   marketIds           rC   watch_tickersbackpack.watch_tickers   s      !!!%%gtU;q#g,'AZF~~f-H  V!34MM)h./	 (
 v>>>##DLL(GDD 	" 	?"   CCB	C C!"CCc                 X  #    U R                  5       I Sh  vN   U R                  USS5      n/ n/ n[        S[        U5      5       H@  nX   nU R	                  U5      nUR                  SU-   5        UR                  SU-   5        MB     U R                  X4US5      I Sh  vN $  N N7f)r   NFr   r   rr   TrK   r   r   r   r   r   rT   rA   r   rJ   rN   rO   r   r   r   s           rC   r   backpack.un_watch_tickers   s      !!!%%gtU;q#g,'AZF~~f-HMM)h./  !6!?@	 (
 &&vfdKKK 	" L"   B*B&B
B*!B("B*(B*r   c                     U R                  US0 5      nU R                  US5      nU R                  U5      nU R                  XE5      nU R	                  X55      nSU-   nXpR
                  U'   UR                  Xx5        g )Ndatasr   )	safe_dictr   safe_marketsafe_symbolparse_ws_tickerr   resolve)	rA   r   rS   rq   r   r   r   parsedTickerr   s	            rC   handle_tickerbackpack.handle_ticker   sz    $ 4##FC0!!(+!!(3++F;$v-+V|1rE   rq   r   c                 2   U R                  US5      nU R                  US-  5      nU R                  US5      nU R                  XR5      nU R	                  XR5      nU R                  US5      nU R                  US5      nU R                  0 SU_SU_SU R                  U5      _S	U R                  US
5      _SU R                  US5      _SS _SS _SS _SS _SS _SU_SU_SU_SS _SS _SS _SS _U R                  US5      U R                  US5      US.EU5      $ )NE  r   cor   	timestampdatetimehighhlowlbid	bidVolumeask	askVolumevwapopencloselastpreviousClosechange
percentageaveragevV)
baseVolumequoteVolumeinfo)safe_integerparse_to_intr   r   r   safe_tickeriso8601safe_number)	rA   rq   r   microsecondsr   r   r   r   r   s	            rC   r   backpack.parse_ws_ticker   s    ((5%%lT&9:	##FC0!!(3!!(3,, !
f!
!
 Y/!
 D$$VS1	!

 4##FC0!
 4!
 !
 4!
 !
 D!
 D!
 T!
 D!
 T!
 d!
  $!!
" t#!
$ **637++FC8)!
* + 	rE   c                   #    U R                  5       I Sh  vN   U R                  USS5      n/ n/ n[        S[        U5      5       H@  nX   nU R	                  U5      nUR                  SU-   5        UR                  SU-   5        MB     U R                  X4U5      I Sh  vN   U R                  U R                  SU5      $  N N#7f)aP  
watches best bid & ask for symbols

https://docs.backpack.exchange/#tag/Streams/Public/Book-ticker

:param str[] symbols: 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>`
NFr   bookTicker.bidask:r   )	rK   r   r   r   r   r   rT   r   r   r   s           rC   watch_bids_asksbackpack.watch_bids_asks)  s      !!!%%gtU;q#g,'AZF~~f-HMM-(23  V!34	 (
 v>>>##DMM8WEE 	" 	?r   c                 X  #    U R                  5       I Sh  vN   U R                  USS5      n/ n/ n[        S[        U5      5       H@  nX   nU R	                  U5      nUR                  SU-   5        UR                  SU-   5        MB     U R                  X4US5      I Sh  vN $  N N7f)a  
unWatches best bid & ask for symbols
:param str[] symbols: 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>`
NFr   r   rt   Tr   r   s           rC   un_watch_bids_asksbackpack.un_watch_bids_asks?  s      !!!%%gtU;q#g,'AZF~~f-HMM-(23  !6!?@	 (
 &&vfdKKK 	" Lr   c                     U R                  US0 5      nU R                  US5      nU R                  U5      nU R                  XE5      nU R	                  X55      nSU-   nXpR
                  U'   UR                  Xx5        g )Nr   r   r   )r   r   r   r   parse_ws_bid_askr   r   )	rA   r   rS   r   r   r   r   parsedBidAskr   s	            rC   handle_bid_askbackpack.handle_bid_askQ  sz      ~~gvr2##D#.!!(+!!(3,,T:$v- ,f|1rE   c                    U R                  US5      nU R                  X25      nU R                  US5      nU R                  US5      nU R                  US-  5      nU R                  US5      nU R                  US5      nU R                  US5      n	U R                  US5      n
U R	                  UUU R                  U5      UUU	U
US	.U5      $ )
Nr   r   r   r   aAbB)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )rA   rq   r   r   r   r   r   r   r   r   r   s              rC   r   backpack.parse_ws_bid_askj  s     ##FC0!!(3!!&(3((5%%lT&9:	vs+$$VS1	vs+$$VS1	"Y/""	!
 	 		rE   1msincelimitc                 V   #    U R                  X//X4U5      I Sh  vN nXa   U   $  N7f)aI  
watches historical candlestick data containing the open, high, low, close price, and the volume of a market

https://docs.backpack.exchange/#tag/Streams/Public/K-Line

: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
N)watch_ohlcv_for_symbols)rA   r   r   r   r   rJ   results          rC   watch_ohlcvbackpack.watch_ohlcv  s8      33f5H4I5Y_``~i(( as   )')r   c                 F   #    U R                  X//U5      I Sh  vN $  N7f)a  
watches historical candlestick data containing the open, high, low, and close price, and the volume of a market

https://docs.backpack.exchange/#tag/Streams/Public/K-Line

:param str symbol: unified symbol of the market to fetch OHLCV data for
:param str timeframe: the length of time each candle represents
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns int[][]: A list of candles ordered, open, high, low, close, volume
N)un_watch_ohlcv_for_symbols)rA   r   r   rJ   s       rC   un_watch_ohlcvbackpack.un_watch_ohlcv  s&      44v6I5JFSSSS   !!symbolsAndTimeframesc                   #    [        U5      nUS:X  d  [        US   [        5      (       d  [        U R                  S-   5      eU R                  5       I Sh  vN   / n/ n[        S[        U5      5       H  nX   n	U R                  U	S5      n
U R                  U
5      nU R                  U	S5      nU R                  U R                  X5      nUR                  SU-   S-   US   -   5        UR                  SUS	   -   S
-   U-   5        M     U R                  XgU5      I Sh  vN u  pnU R                  (       a  UR                  X5      nU R                  UX#SS5      nU R                  XU5      $  GN NR7f)aj  
watches historical candlestick data containing the open, high, low, close price, and the volume of a market

https://docs.backpack.exchange/#tag/Streams/Public/K-Line

:param str[][] symbolsAndTimeframes: array of arrays containing unified symbols and timeframes to fetch OHLCV data for, example [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]
: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
r   z_ watchOHLCVForSymbols() requires a an array of symbols and timeframes, like  ['ETH/USDC', '1m']N   kline..r   candles:r   rv   T)r   
isinstancer   r   r   rK   r   r   r   r7   r   rT   
newUpdatesgetLimitfilter_by_since_limitcreate_ohlcv_object)rA   r  r   r   rJ   symbolsLengthrN   rO   r   symbolAndTimeframer   r   tfintervalr   r   ru   filtereds                     rC   r    backpack.watch_ohlcv_for_symbols  s|     01AZ0DQ0G%N%N#DGG  /P  %P  Q  Q!!!q#234A!5!8''(:A>H[[*F!!"4a8B''@HMM(X-3fTlBC  fX.>!>!Dx!OP 5 ,0+<+<VTZ+[%["7??$$V3E--guQM''8DD 	" &\s&   AE5E0CE5 E3!AE53E5c                 X  #    [        U5      nUS:X  d  [        US   [        5      (       d  [        U R                  S-   5      eU R                  5       I Sh  vN   / n/ n[        S[        U5      5       H  nX   nU R                  US5      nU R                  U5      n	U R                  US5      n
U R                  U R                  X5      nUR                  SU-   S-   U	S   -   5        UR                  SU	S	   -   S
-   U-   5        M     U R                  XEUS5      I Sh  vN $  N N7f)a  
unWatches historical candlestick data containing the open, high, low, and close price, and the volume of a market

https://docs.backpack.exchange/#tag/Streams/Public/K-Line

:param str[][] symbolsAndTimeframes: array of arrays containing unified symbols and timeframes to fetch OHLCV data for, example [['BTC/USDT', '1m'], ['LTC/USDT', '5m']]
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns int[][]: A list of candles ordered, open, high, low, close, volume
r   za unWatchOHLCVForSymbols() requires a an array of symbols and timeframes, like  ['ETH/USDC', '1m']Nr	  r
  r  r   zunsubscribe:candles:r   rv   T)r   r  r   r   r   rK   r   r   r   r7   r   rT   )rA   r  rJ   r  rN   rO   r   r  r   r   r  r  s               rC   r  #backpack.un_watch_ohlcv_for_symbols  s;     01AZ0DQ0G%N%N#DGG  /R  %R  S  S!!!q#234A!5!8''(:A>H[[*F!!"4a8B''@HMM(X-3fTlBC  !7&:J!JS!PS[![\ 5 &&vfdKKK 	" Ls%   AD*D&C	D*!D("D*(D*c                 \   U R                  US0 5      nU R                  US5      nU R                  U5      nUS   nU R                  US5      nUR                  S5      nU R                  US5      n	X`R                  ;  a  0 U R                  U'   XR                  U   ;  a9  U R                  U R                  SS5      n
[        U
5      nXR                  U   U	'   U R                  U   U	   nU R                  U5      nUR                  U5        S	U-   S
-   U	-   nUR                  XiU/U5        g )Nr   r   r   streamr  r	  
OHLCVLimitr   r  rv   )r   r   r   r   r   r   r<   r   parse_ws_ohlcvr   r   )rA   r   rS   r   r   r   r   r  partsr   r   storedohlcvparsedr   s                  rC   handle_ohlcvbackpack.handle_ohlcv  s"   ( ~~gvr2##D#.X&!!!'84S!$$UA.	++%"$DKK[[00%%dllL$GE*51F-3KK	*F#I.$$T*V 6)C/);51;?rE   c           	          U R                  U R                  US5      5      U R                  US5      U R                  US5      U R                  US5      U R                  US5      U R                  US5      /$ )NTr   r   r   r   r   )	parse8601r   r   )rA   r   r   s      rC   r  backpack.parse_ws_ohlcv
  sw    $ NN4++E378UC(UC(UC(UC(UC(
 	
rE   c                 F   #    U R                  U/X#U5      I Sh  vN $  N7f)a  
watches information on multiple trades made in a market

https://docs.backpack.exchange/#tag/Streams/Public/Trade

:param str symbol: unified symbol of the market to fetch the ticker for
:param int [since]: the earliest time in ms to fetch trades 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 `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
N)watch_trades_for_symbols)rA   r   r   r   rJ   s        rC   watch_tradesbackpack.watch_trades$  s$      22F8U6RRRRr  c                 D   #    U R                  U/U5      I Sh  vN $  N7f)aI  
unWatches from the stream channel

https://docs.backpack.exchange/#tag/Streams/Public/Trade

:param str symbol: unified symbol of the market to fetch trades for
: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)un_watch_trades_for_symbolsr   s      rC   un_watch_tradesbackpack.un_watch_trades2  s"      55vhGGGGr   c                 Z  #    U R                  5       I Sh  vN   U R                  U5      n[        U5      nUS:X  a  [        U R                  S-   5      e/ n/ n[        S[        U5      5       H@  nX   n	U R                  U	5      n
UR                  SU
-   5        UR                  SU	-   5        MB     U R                  XgU5      I Sh  vN nU R                  (       a5  U R                  US5      nU R                  US5      nUR                  X5      nU R                  XUSS5      $  GN Na7f)	a  
watches information on multiple trades made in a market

https://docs.backpack.exchange/#tag/Streams/Public/Trade

:param str[] symbols: unified symbol of the market to fetch trades for
:param int [since]: the earliest time in ms to fetch trades 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 `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
Nr   z> watchTradesForSymbols() requires a non-empty array of symbolstrade.trades:r   r   T)rK   r   r   r   r   r   r   r   rT   r  
safe_valuer   r  r  )rA   r   r   r   rJ   r  rN   rO   r   r   r   r{   firsttradeSymbols                 rC   r)  !backpack.watch_trades_for_symbols>  s     !!!%%g.GA#DGG.n$nooq#g,'AZF~~f-HMM(X-.  V!34	 (
 ((GG??OOFA.E**5(;KOOK7E))&TRR# 	" Hs#   D+D&B0D+D)AD+)D+c                   #    U R                  5       I Sh  vN   U R                  U5      n[        U5      nUS:X  a  [        U R                  S-   5      e/ n/ n[        S[        U5      5       H@  nX   nU R                  U5      nUR                  SU-   5        UR                  SU-   5        MB     U R                  XEUS5      I Sh  vN $  N N7f)aL  
unWatches from the stream channel

https://docs.backpack.exchange/#tag/Streams/Public/Trade

:param str[] symbols: unified symbol of the market to fetch trades for
: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>`
Nr   z@ unWatchTradesForSymbols() requires a non-empty array of symbolsr1  r|   T)	rK   r   r   r   r   r   r   r   rT   )	rA   r   rJ   r  rN   rO   r   r   r   s	            rC   r-  $backpack.un_watch_trades_for_symbols]  s      !!!%%g.GA#DGG.p$pqqq#g,'AZF~~f-HMM(X-.  !6!?@	 (
 &&vfdKKK 	" Ls"   CCB1CC	CCc                    U R                  US0 5      nU R                  US5      nU R                  U5      nUS   nX`R                  ;  a6  U R	                  U R
                  SS5      n[        U5      nXR                  U'   U R                  U   n	U R                  X55      n
U	R                  U
5        SU-   nUR                  X5        UR                  U	S5        g )Nr   r   r   tradesLimitr   r2  r{   )
r   r   r   r{   r   r<   r   parse_ws_trader   r   )rA   r   rS   r   r   r   r   r   r  r   trader   s               rC   handle_tradesbackpack.handle_tradesu  s    $ ~~gvr2##D#.X&!++%%%dllM4HE&F"(KKF###D1U&(u*uh'rE   c                    U R                  US5      nU R                  US-  5      nU R                  US5      nU R                  US5      nU R                  Xb5      nU R	                  US5      nU(       a  SOSnU(       a  SOS	n	U R                  US
5      n
U R                  US5      nS nUS:X  a  U R                  US5      nOU R                  US5      nU R                  UUUU R                  U5      US   US UU	U
US S S S.S.U5      $ )Nr   r   tr   msellbuymakertakerpqr   r   r   currencycost)r   r   r   r   r   ordertypesidetakerOrMakerpriceamountrJ  fee)r   r   r   r   	safe_bool
safe_trader   )rA   r<  r   r   r   r   r   isMakerrM  rN  rO  rP  orderIds                rC   r;  backpack.parse_ws_trade  s-    ((4%%lT&9:	eS)##E3/!!(3.., ve")ww  ,!!%-5=&&uc2G&&uc2G"Y/X&(  
" # 	rE   c                 D   #    U R                  U/X#5      I Sh  vN $  N7f)a  
watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

https://docs.backpack.exchange/#tag/Streams/Public/Depth

: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)rA   r   r   rJ   s       rC   watch_order_bookbackpack.watch_order_book  s"      66xOOOOr   c                   #    U R                  5       I Sh  vN   U R                  USS5      nU R                  U5      n/ n/ n[        S[	        U5      5       H5  nX   nUR                  SU-   5        XG   n	SU	-   n
UR                  U
5        M7     U R                  XeU5      I Sh  vN nUR                  5       $  N N7f)an  

https://docs.backpack.exchange/#tag/Streams/Public/Depth

watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
:param str[] symbols: unified array of symbols
:param int [limit]: the maximum amount of order book entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.method]: either '/market/level2' or '/spotMarket/level2Depth5' or '/spotMarket/level2Depth50' default is '/market/level2'
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
NFr   
orderbook:depth.)rK   r   
market_idsr   r   r   rT   r   )rA   r   r   rJ   	marketIdsrO   rN   r   r   r   r   ry   s               rC   rX  %backpack.watch_order_book_for_symbols  s      !!!%%gtU;OOG,	q#g,'AZF  !67 |Hx'EMM%  ( ++F6JJ	   	" Ks"   B?B;BB?&B='B?=B?c                 D   #    U R                  U/U5      I Sh  vN $  N7f)a^  
unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
:param str symbol: unified array of symbols
: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)un_watch_order_book_for_symbolsr   s      rC   un_watch_order_bookbackpack.un_watch_order_book  s"      996(FKKKKr   c                 d  #    U R                  5       I Sh  vN   U R                  USS5      nU R                  U5      n/ n/ n[        S[	        U5      5       H5  nX   nUR                  SU-   5        X6   nSU-   n	UR                  U	5        M7     U R                  XTUS5      I Sh  vN $  N N7f)a  
unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
:param str[] symbols: unified array of symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.method]: either '/market/level2' or '/spotMarket/level2Depth5' or '/spotMarket/level2Depth50' default is '/market/level2'
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
NFr   rz   r]  T)rK   r   r^  r   r   r   rT   )
rA   r   rJ   r_  rO   rN   r   r   r   r   s
             rC   rb  (backpack.un_watch_order_book_for_symbols  s      !!!%%gtU;OOG,	q#g,'AZF  !9F!BC |Hx'EMM%  ( &&vfdKKK 	" Ls"   B0B,BB0'B.(B0.B0c                 ^   U R                  US0 5      nU R                  US5      nU R                  U5      nXPR                  ;  a  U R	                  5       U R                  U'   U R                  U   nU R                  US5      nU R                  US5      nSU-   n	Uch  [        UR                  5      n
U R                  SSS5      nX:X  a  U R                  U R                  XUS 0 5        UR                  R                  U5        g Xx:  a  g U R                  Xc5        UR                  Xi5        g )	Nr   r   r`   ur\  r!   snapshotDelay
   )r   r   r   r   
order_bookr   r   r   handle_optionspawnload_order_bookr   handle_deltar   )rA   r   rS   r   r   r   storedOrderBookr`   
deltaNoncer   cacheLengthri  s               rC   handle_order_bookbackpack.handle_order_book  s   & ~~gvr2##D#.!!(+//)&*oo&7DOOF#//&1!!/7;&&tS1
"V+=o334K !../?RTUM+

4//fdTVW!!((./04rE   c                 J   U R                  U R                  US5      S-  5      nX1S'   U R                  U5      US'   U R                  US5      US'   U R                  US/ 5      nU R                  US/ 5      nUS	   nUS
   nU R	                  Xd5        U R	                  Xu5        g )Nr%  r   r   r   rh  r`   r   r   bidsasks)r   r   r   	safe_listhandle_bid_asks)rA   ry   deltar   rv  rw  
storedBids
storedAskss           rC   ro  backpack.handle_delta6  s    %%d&7&7s&Cd&JK	!*+ $Y 7	*!..uc:	'~~eS"-~~eS"-v&
v&
Z.Z.rE   c                     [        S[        U5      5       H'  nU R                  X#   5      nUR                  U5        M)     g )Nr   )r   r   parse_bid_ask
storeArray)rA   bookSidebidAsksr   bidAsks        rC   ry  backpack.handle_bid_asksB  s7    q#g,'A''
3F' (rE   c                 D   U R                  US5      nU R                  US5      nU R                  US5      nXES-
  :  a  g[        S[        U5      5       H>  nX&   nU R                  US5      nU R                  US5      n	XHS-
  :  d  M5  XI:  d  M<  Us  $    [        U5      $ )Nr   r`   Ur	  rh  )r   r   r   r   )
rA   ry   r   
firstDeltar`   firstDeltaStartr   rz  
deltaStartdeltaEnds
             rC   get_cache_indexbackpack.get_cache_indexG  s     ^^E1-
!!)W5++J<Q&&q#e*%AHE**5#6J((4Ha'e.> & 5zrE   c                 D  #    U R                  5       I Sh  vN   SnUb  U R                  U5      nUS   nSnSnUb  SUS   -   nSU-   nU R                  U/U/U5      I Sh  vN nU R                  (       a  UR	                  X5      nU R                  XX#S5      $  N N;7f)	a  
watches information on multiple orders made by the user

https://docs.backpack.exchange/#tag/Streams/Private/Order-update

: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   account.orderUpdater}   account.orderUpdate.r   zorders:T)rK   r   rm   r  r  filter_by_symbol_since_limit)	rA   r   r   r   rJ   r   r   r   r}   s	            rC   watch_ordersbackpack.watch_ordersW  s      !!![[(FH%F%*VD\9E#f,K))5'K=&II??OOF2E00tTT 	" Js"   B BAB "B#:B B c                    #    U R                  5       I Sh  vN   SnUb  U R                  U5      nUS   nSnSnUb  SUS   -   nSU-   nU R                  U/U/US5      I Sh  vN $  NQ N7f)	at  
unWatches information on multiple orders made by the user

https://docs.backpack.exchange/#tag/Streams/Private/Order-update

:param str [symbol]: unified market symbol of the market orders were made in
: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   r  r~   r  r   r   T)rK   r   rm   r   s         rC   un_watch_ordersbackpack.un_watch_ordersr  s      !!![[(FH%F%**VD\9E/&8K''+MMM 	" Ns"   A,A(AA,#A*$A,*A,c                    SnU R                  US0 5      nU R                  US5      nU R                  U5      nUS   nU R                  XF5      nU R                  n	U	c.  U R                  U R                  SS5      n
[        U
5      n	Xl        U	R                  U5        UR                  X5        US-   U-   nUR                  X5        g )Nr}   r   r   r   ordersLimitr   rv   )
r   r   r   parse_ws_orderr}   r   r<   r   r   r   )rA   r   rS   r   r   r   r   r   r!  r}   r   symbolSpecificMessageHashs               rC   handle_orderbackpack.handle_order  s    2 ~~gvr2##D#.!!(+!$$T2>%%dllM4HE+E2F Kfv+$/#$5$>!v9rE   c                 V   U R                  US5      nU R                  US5      nU R                  US5      nU R                  US-  5      nU R                  U R                  US5      U5      nU R                  US5      nU R	                  X5      nUS   n	U R                  US5      n
U R                  US	5      nU R                  U R                  US
5      5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nS nU R                  US5      nUb  US S.nU R                  0 SU_SU_SU_SU R                  U5      _SS _SU_SU	_SU
_SU_SU_SU_SS _SU_SS _SU_S U_S!U_S US US".EU5      $ )#Nr   r   r   r   Xr   r   r   fSrF  PrG  Zr   NrH  r   clientOrderIdr   r   lastTradeTimestampstatusrL  timeInForcerM  rO  	stopPricetriggerPricer   rP  rJ  filled)	remainingrQ  r{   r   )
r   r   r   parse_ws_order_statusr   safe_string_lowerparse_ws_order_sider   
safe_orderr   )rA   rK  r   r   r  r   r   r  r   r   rL  r  rM  rO  r  rP  rJ  r  rQ  feeCurrencys                       rC   r  backpack.parse_ws_order  s`   6 eS)((4((4%%lT&9:	++D,<,<UC,H&Q##E3/!!(3!%%eS1&&uc2''(8(8(DE  ,''s3!!%-s+!!%-&&uc2"'C   
" 
] 
  
 Y/	 

 !$ 
 f 
 f 
 D 
 ; 
 D 
 U 
  
 L 
 t 
 f 
  D! 
" f# 
$ + 
, - 	rE   c                 :    SSSSSSSS.nU R                  X1U5      $ )Nr   closedcanceledrejected)NewFilled	CancelledExpiredPartiallyFilledTriggerPendingTriggerFailedr   )rA   r  r   statusess       rC   r  backpack.parse_ws_order_status  s3    #!%$'
 &99rE   rM  c                 0    SSS.nU R                  X!U5      $ )NrC  rB  )BidAskr  )rA   rM  sidess      rC   r  backpack.parse_ws_order_side  s$    
 T22rE   c                   #    U R                  5       I Sh  vN   U R                  U5      n/ n/ nUbX  [        S[        U5      5       H>  nX   nUR	                  SU-   5        UR	                  SU R                  U5      -   5        M@     O"UR	                  S5        UR	                  S5        U R                  XeU5      I Sh  vN n	U R                  (       a  U	$ U R                  U R                  XUS5      $  N N77f)a  
watch all open positions

https://docs.backpack.exchange/#tag/Streams/Private/Position-update

:param str[] [symbols]: list of unified market symbols to watch positions for
: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   z
positions:account.positionUpdate.r   account.positionUpdateT)
rK   r   r   r   r   r   rm   r  filter_by_symbols_since_limitr   )
rA   r   r   r   rJ   rO   rN   r   r   r   s
             rC   watch_positionsbackpack.watch_positions  s      !!!%%g.1c'l+ $$%6%?@7$..:PPQ ,
   -MM23,,VFKK	??11$..'RWY]^^ 	" Ls"   C<C8B+C<C:6C<:C<c                   #    U R                  5       I Sh  vN   U R                  U5      n/ n/ nUbX  [        S[        U5      5       H>  nX   nUR	                  SU-   5        UR	                  SU R                  U5      -   5        M@     O"UR	                  S5        UR	                  S5        U R                  XCUS5      I Sh  vN $  N N7f)as  
unWatches from the stream channel

https://docs.backpack.exchange/#tag/Streams/Private/Position-update

:param str[] [symbols]: list of unified market symbols to watch positions for
: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   r   r  r   r  T)rK   r   r   r   r   r   rm   )rA   r   rJ   rO   rN   r   r   s          rC   un_watch_positionsbackpack.un_watch_positions+  s      !!!%%g.1c'l+ $$%BV%KL7$..:PPQ ,
   !89MM23''vtLLL 	" Ms"   CCB,CC
C
Cc                    SnU R                  US0 5      nU R                  c  [        5       U l        U R                  nU R                  U5      nU R	                  US5      nU R                  US-  5      nXS'   U R                  U5      US'   UR                  U5        US-   US   -   n	UR                  U/U5        UR                  U/U	5        g )	Nr   r   r   r   r   r   rv   r   )	r   r   r   parse_ws_positionr   r   r   r   r   )
rA   r   rS   r   r   r   parsedPositionr   r   r  s
             rC   handle_positionsbackpack.handle_positionsC  s    0 "~~gvr2>>!35DN//5((s3%%lT&9:	&/{#%)\\)%<z"^$$/#$5x8P$P!'5')BCrE   c                 
   U R                  US5      nU R                  US5      nU R                  XB5      nUS   nU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      n	U R                  US5      n
U R                  US	5      nU R                  US
5      nU R                  US5      nSnSnUS:  a  SnUc  S nS nU R                  US5      nU R	                  US-  5      nU R                  US5      nU R                  US5      nU R                  0 SU_SU_SU_SU_SS _SU_SU_SU	_SU
_SS _SU_SS _SU_S U_S!U_S"U_S#U R                  U5      _S US S US S S$.E5      $ )%Nr   r   r   nr   r   rF  r  QMrG  Flongr   shortr   r   rA  r  r   r   notional
marginModeliquidationPrice
entryPricerealizedPnlunrealizedPnlr   	contractscontractSize	markPricerM  hedgedr   r   )maintenanceMarginmaintenanceMarginPercentage
collateralinitialMargininitialMarginPercentageleveragemarginRatio)r   r   r   r   r   safe_positionr   )rA   positionr   r   r   r   r  r  r  r  unrealisedPnlr  r  netQuantityr  rM  r   r   r  r  s                       rC   r  backpack.parse_ws_positionj  sL   * h,##Hc2!!(3!##Hc2++Hc:%%h4
&&x5((37$$Xs3	$$Xs3	&&x5?DFD((37%%lT&9:	&*&6&6x&E#"&"2"28S"A!! #
H#
"#
 f#
 	#

 $#
  0#
 *#
 ;#
 ]#
 $#
 #
 D#
 #
 D#
 f#
  !#
" Y/##
$ "&+F!'>1#
  	rE   c                 B   U R                  X5      (       d  g U R                  US5      nU R                  US5      nUS:X  a  U R                  X5        g US:X  a  U R	                  X5        g US:X  a  U R                  X5        g US:X  a  U R                  X5        g US:X  a  U R                  X5        g US:X  d*  US	:X  d$  US
:X  d  US:X  d  US:X  d  US:X  d  US:X  d  US:X  a  U R                  X5        g US:X  d  US:X  d  US:X  d  US:X  a  U R                  X5        g g )Nr   erq   
bookTickerkliner<  depthorderAcceptedorderUpdate	orderFillorderCancelledorderExpiredorderModifiedtriggerPlacedtriggerFailedpositionAdjustedpositionOpenedpositionClosedpositionUpdated)
handle_error_messager   r   r   r   r"  r=  rs  r  r  )rA   r   rS   r   events        rC   handle_messagebackpack.handle_message  sb   ((99~~gv.  s+Hv/l"0gf.gv/g""63o%-)?5KCW[`dt[tx}  BP  yP  TY  ]l  Tl  pu  yH  pH  LQ  Ud  Ldf.((E5E,ERbIbfk  pA  gA!!&2 gArE   c                     U R                  US0 5      nU R                  US5      n Ub-  U R                  US5      n[        U R                  S-   U-   5      eg! [
         a  nUR                  U5         S nAgS nAff = f)NerrorcoderS    T)r   r   r   r   r   	Exceptionreject)rA   r   rS   r  r  msgr  s          rC   r   backpack.handle_error_message  s     w4  /	&&ui8#DGGcMC$788 	MM!	s   0A 
A="A88A=)r}   r   )N)E__name__
__module____qualname____firstlineno__r   r@   rT   rm   r_   r   dictrL   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r  r   r  r"  r  r   r*  r.  r)  r-  r=  r;  r   rY  rX  rc  rb  rs  ro  ry  r  r   r
   r  r  r  r  r  r  r   r  r  r  r  r  r   r   __static_attributes____classcell__)rB   s   @rC   r   r      s   -# -^ @B5 U ACE U,33# 33d3i 33RV 33j 68 G GF G" 9; 
=C 
=s 
= 6:" E7 E E, 9=R Lg LC L*2F 26+d +F +f +Z 8<B FW F7 F, ;?r L Lc L$2V 22 D 8<$]ajl ) )C )WZ )quvzq{ )  BFb T3 T3 TUX T aesw  AC E$tCy/ EZ] Emp E@ ^` LT$s)_ Ldg L4%@6 %@N
D 
4 <@d[] S SS S Sbfglbm S 9; 
HC 
Hs 
H OSaenp Sd3i S S[^ Suyz  vA S> LN Lc LRU L0(F (B/b @DB PS P PS\ P SW_a !$s) !C !fo !4 =? L L3 L PR LT#Y LVY L*(5 (5T
/(
  04$UYbd U UC Us Uimnsit U6 37r NC Nc N,':6 ':RHT
:3 3 8<$]ajl _W _C _WZ _quv~q _: ;?r M MdSVi M0%DNEN3V 3(6 t  rE   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   r   ccxt.base.typesr   r   r   r	   r
   r   r   r   r   r   r   r   !ccxt.async_support.base.ws.clientr   typingr   ccxt.base.errorsr   r   async_supportr    rE   rC   <module>r     sB     f f t t t t 4  * .It!!** IrE   