
    ei                         d dl Zd dlmZmZmZm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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ArrayCacheBySymbolBySideArrayCacheByTimestamp)AnyBalancesBoolIntOrder	OrderBookPositionStrStringsTickerTickersTrade)Client)List)AuthenticationError)NotSupported)Precisec                       e Zd Zdef fdZd Zd Zdi fdedede	fdZ
d	efd
Zi fdedefdZd3dZd	efdZdi fdedefdZd	efdZdi fdedefdZd	efdZd3dZdddi fdedededede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d3dZd	efdZ i fdZ!i fdZ"i fdZ#dddi fde$dededee%         fd Z&dddi fde$dededee         fd!Z'd3d"Z(d	efd#Z)d	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-d3d	edefd'Z.d( Z/d) Z0d3d*Z1i fde2fd+Z3d, Z4d	ede5fd-Z6d	efd.Z7d	efd/Z8d	efd0Z9d	efd1Z:d	efd2Z; xZ<S )4woofiproreturnc                     |                      t          t          |                                           dddddddddddddddddiddd	did
ddddddi dddd| j        dddddt
          iiid          S )NTF)wswatchBalancewatchMyTrades
watchOHLCVwatchOrderBookwatchOrderswatchTickerwatchTickerswatchBidsAskswatchTradeswatchTradesForSymbolswatchPositionsr   z"wss://ws-evm.orderly.org/ws/streamz5wss://ws-private-evm.orderly.org/v2/ws/private/stream)publicprivatez*wss://testnet-ws-evm.orderly.org/ws/streamz=wss://testnet-ws-private-evm.orderly.org/v2/ws/private/stream)apitest)apiKeysecret	accountId  )fetchPositionsSnapshotawaitPositionsSnapshot)tradesLimitordersLimit	requestIdr'   i'  )ping	keepAliveexactzAuth is needed.)hasurlsrequiredCredentialsoptions	streaming
exceptions)deep_extendsuperr   describer5   r   )self	__class__s    O/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/pro/woofipro.pyr@   zwoofipro.describe   s   h 5 5 > > @ @ $!%""&## $!%#)."&   "F#Z  "N#b   !$ $  $#.2.2# #	  	" 
 )+>_6C
 6C
 6 6 6	    c                     |                      | j        di           }|                     ||d          }|                     |d          }|| j        d         |<   |S )Nr4   r      )	safe_dictr;   safe_integersum)rA   urlr;   previousValuenewValues        rC   
request_idzwoofipro.request_idK   sY    ..{B??))'3::88M1--)1[!#&rD   c                   K   d}| j         | j         dk    r| j         }| j        d         d         d         dz   |z   }|                     |          }d|i}|                     ||          }|                     |||||           d {V S )N(OqdphuyCtYWxwzhxyLLjOWNdFP7sQt8RPWzmb5xY r*   r   r(   /id)r.   r9   rM   extendwatch)rA   messageHashmessagerR   rJ   r4   	subscriberequests           rC   watch_publiczwoofipro.watch_publicR   s      7>%$.B*>*>Bit$X.4r9OOC((	)
	 ++i11ZZ[';	RRRRRRRRRrD   Nsymbollimitc                   K   |                                   d{V  d}|                     |          }|d         dz   |z   }d|d}|                     ||          }|                     ||           d{V }	|	                                S )aS  

        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/public/orderbook

        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	orderbookrR   @rW   eventtopic)load_marketsmarketrS   rY   r[   )
rA   rZ   r[   paramsnamerc   ra   rX   rV   r]   s
             rC   watch_order_bookzwoofipro.watch_order_book_   s       !!!!!!!!!V$$ts"T) 
 
 ++gv..++E7;;;;;;;;	   rD   clientc                    |                      |di           }|                     |d          }|                     |          }|d         }|                     |d          }|| j        vr|                                 | j        |<   | j        |         }|                     |d          }	|                     |||	dd          }
|                    |
           |                    ||           d S )NdatarZ   ra   tsbidsasks)	rG   safe_stringsafe_market
orderbooks
order_bookrH   parse_order_bookresetresolve)rA   rg   rV   ri   marketIdrc   rZ   ra   r]   	timestampsnapshots              rC   handle_order_bookzwoofipro.handle_order_bookv   s    , ~~gvr22##D(33!!(++!  '22$/))&*oo&7&7DOF#OF+	%%gt44	((vy&&QQ!!!y%(((((rD   c                   K   |                                   d{V  d}|                     |          }|d         }|d         dz   |z   }d|d}|                     ||          }|                     ||           d{V S )a  

        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/public/24-hour-ticker

        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>`
        NtickerrZ   rR   r^   rW   r_   )rb   rc   rS   rY   )rA   rZ   rd   re   rc   ra   rX   rV   s           rC   watch_tickerzwoofipro.watch_ticker   s       !!!!!!!!!V$$!ts"T) 
 
 ++gv..&&ug666666666rD   c                    |                      i d|                     d |          dd dd d|                     |d          d|                     |d          dd dd dd d	d d
d d|                     |d          d|                     |d          dd dd dd dd dd |                     |d          |                     |d          |d|          S )NrZ   ru   datetimehighlowbid	bidVolumeask	askVolumevwapopencloselastpreviousClosechange
percentageaveragevolumeamount)
baseVolumequoteVolumeinfo)safe_tickersafe_symbolrm   )rA   ry   rc   s      rC   parse_ws_tickerzwoofipro.parse_ws_ticker   sx     !
d&&tV44!
!
 !
 D$$VV44	!

 4##FE22!
 4!
 !
 4!
 !
 D!
 D$$VV44!
 T%%fg66!
 D!
 T!
 d!
  $!!
" t#!
$ **68<<++FH==)!
 !
 !
* +  	rD   c                 z   |                      |di           }|                     |d          }|                     |d          }|                     |          }|                     |d          }||d<   |                     ||          }|d         |d<   || j        |d         <   |                    ||           |S )Nri   ra   rZ   rj   date)rG   rm   rn   rH   r   tickersrs   )	rA   rg   rV   ri   ra   rt   rc   ru   ry   s	            rC   handle_tickerzwoofipro.handle_ticker   s    " ~~gvr22  '22##D(33!!(++%%gt44	 V%%dF33!(+x)/VH%&vu%%%rD   symbolsc                   K   |                                   d{V  |                     |          }d}|}d|d}|                     ||          }|                     ||           d{V }|                     |d|          S )a  

        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/public/24-hour-tickers

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list
        :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>`
        Nr   rW   r_   rZ   rb   market_symbolsrS   rY   filter_by_arrayrA   r   rd   re   ra   rX   rV   r   s           rC   watch_tickerszwoofipro.watch_tickers   s       !!!!!!!!!%%g.. 
 
 ++gv..))%99999999##GXw???rD   c                    |                      |d          }|                     |dg           }|                     |d          }g }t          dt	          |                    D ]}|                      ||         d          }|                     |          }	|                     |                     ||         d|i          |	          }
|
| j        |	d         <   |	                    |
           |
                    ||           d S )Nra   ri   rj   r   rZ   r   )rm   	safe_listrH   rangelenrn   r   rS   r   appendrs   )rA   rg   rV   ra   ri   ru   resultirt   rc   ry   s              rC   handle_tickerszwoofipro.handle_tickers  s    (   '22~~gvr22%%gt44	q#d))$$ 	" 	"A''Q::H%%h//F))$++d1g	?R*S*SU[\\F-3DL)*MM&!!!!vu%%%%%rD   c                   K   |                                   d{V  |                     |          }d}|}d|d}|                     ||          }|                     ||           d{V }|                     |d|          S )a  

        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/public/bbos

        watches 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>`
        NbbosrW   r_   rZ   r   r   s           rC   watch_bids_askszwoofipro.watch_bids_asks&  s       !!!!!!!!!%%g.. 
 
 ++gv..))%99999999##GXw???rD   c                    |                      |d          }|                     |dg           }|                     |d          }g }t          dt	          |                    D ]X}|                     |                     ||         d|i                    }|| j        |d         <   |                    |           Y|	                    ||           d S )Nra   ri   rj   r   rZ   )
rm   r   rH   r   r   parse_ws_bid_askrS   r   r   rs   )	rA   rg   rV   ra   ri   ru   r   r   ry   s	            rC   handle_bid_askzwoofipro.handle_bid_ask<  s        '22~~gvr22%%gt44	q#d))$$ 	" 	"A**4;;tAwy@Q+R+RSSF-3DL)*MM&!!!!vu%%%%%rD   c                    |                      |d          }|                     ||          }|                      |d          }|                     |d          }|                     |||                     |          |                      |d          |                      |d          |                      |d          |                      |d          |d|          S )NrZ   rj   r   askSizer   bidSize)rZ   ru   r|   r   r   r   r   r   )rm   rn   rH   r   iso8601)rA   ry   rc   rt   rZ   ru   s         rC   r   zwoofipro.parse_ws_bid_askV  s    ##FH55!!(F33!!&(33%%fd33	"Y//##FE22))&)<<##FE22))&)<<	!
 	!
 	 	 		rD   1m	timeframesincec                 <  K   |                                   d{V  |dk    rA|dk    r;|dk    r5|dk    r/|dk    r)|dk    r#|dk    r|d	k    rt          | j        d
z             |                     |          }|                     | j        ||          }d}|d         dz   |z   dz   |z   }	d|	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://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/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
        Nr   5m15m30m1h1d1w1MzG watchOHLCV timeframe argument must be 1m, 5m, 15m, 30m, 1h, 1d, 1w, 1MklinerR   r^   _rW   r_   rZ   r   T)rb   r   rR   rc   rm   
timeframesrS   rY   
newUpdatesgetLimitfilter_by_since_limit)rA   rZ   r   r   r[   rd   rc   intervalre   ra   rX   rV   ohlcvs                rC   watch_ohlcvzwoofipro.watch_ohlcvf  s      !!!!!!!!!I$5$5I<N<NU^bgUgUgnw{nn  GP  TX  GX  GX  _h  lp  _p  _p  w@  DH  wH  wHtw)rrsssV$$##DOY	JJts"T)C/(: 
 
 ++gv..''w77777777? 	<NN6(#3U;;E))%q$GGGrD   c           	         |                      |di           }|                     |d          }|                     |d          }|                     |          }|d         }|                     |d          }|                     |          }	|                     |d          |                     |d          |                     |d          |                     |d          |                     |d	          |                     |d
          g}
|                     | j        |i           | j        |<   |                     | j        |         |	          }|;|                     | j        dd          }t          |          }|| j        |         |	<   | j        |         |	         }|
                    |
           |                    ||           d S )Nri   ra   rZ   type	startTimer   r}   r~   r   r   
OHLCVLimitr/   )rG   rm   rn   find_timeframerH   safe_number
safe_valueohlcvsr;   r   r   rs   )rA   rg   rV   ri   ra   rt   rc   rZ   r   r   parsedstoredr[   
ohlcvCaches                 rC   handle_ohlcvzwoofipro.handle_ohlcv  s   & ~~gvr22  '22##D(33!!(++!##D&11''11	dK00T6**T6**T5))T7++T8,,
 #oodk62FFFV!4i@@>%%dlL$GGE*511F-3DK	*[(3
&!!!z5)))))rD   c                 p  K   |                                   d{V  |                     |          }|d         }|d         dz   }d|d}|                     ||          }|                     ||           d{V }	| j        r|	                    |d         |          }|                     |	|||d          S )aG  
        watches information on multiple trades made in a market

        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/public/trade

        :param str symbol: unified market symbol of the market trades were made in
        :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 list of `trade structures <https://docs.ccxt.com/?id=trade-structure>`
        NrZ   rR   z@traderW   r_   T)rb   rc   rS   rY   r   r   filter_by_symbol_since_limit)
rA   rZ   r   r[   rd   rc   ra   rX   rV   tradess
             rC   watch_tradeszwoofipro.watch_trades  s       !!!!!!!!!V$$!tx' 
 
 ++gv..((88888888? 	=OOF8$4e<<E00tTTTrD   c                 J   |                      |d          }|                     |d          }|                     |di           }|                      |d          }|                     |          }|d         }|                     |                     |d|i          |          }	|| j        vr5|                     | j        dd          }
t          |
          }|| j        |<   | j        |         }|	                    |	           || j        |<   |
                    ||           d S )Nra   rj   ri   rZ   ru   r2   r/   )rm   rH   rG   rn   parse_ws_traderS   r   r;   r   r   rs   )rA   rg   rV   ra   ru   ri   rt   rc   rZ   trader[   r   r   s                rC   handle_tradezwoofipro.handle_trade  s!      '22%%gt44	~~gvr22##D(33!!(++!##DKK{I6N$O$OQWXX$+%%%%dlM4HHE&&F"(DKV$e$Fvu%%%%%rD   c                    |                      |d          }|                     ||          }|d         }|                     |dd          }|                     |dd          }t          j        ||          }|                     |d          }|                     |d          }	d }
|                     |d          }||rdnd	}
d }|                      |d
          }|,||                     |                      |d                    d}| 	                    |                      |d          |	| 
                    |	          ||||||                      |d          |
|                     |d          ||d|          S )NrZ   executedPricepriceexecutedQuantitysizesideru   makertakerfeefeeAssetcostcurrencytradeIdorderIdr   )rR   ru   r|   rZ   r   r   r   r   ordertakerOrMakerr   r   r   )rm   rn   safe_string_2r   
string_mulsafe_string_lowerrH   	safe_boolsafe_currency_code
safe_trader   )rA   r   rc   rt   rZ   r   r   r   r   ru   r   r   r   feeValues                 rC   r   zwoofipro.parse_ws_trade  s   J ##E844!!(F33!""5/7CC##E+=vFF!%00%%eV44%%e[99	ug..&+877L##E511  33D4D4DUJ4W4WXX C ""5)44"Y//%%eY77(**5&99 
  
   	rD   c                 4   d}|                      |d          }|r2|                      |j        d          }|                    d           d S t          |                     |                    }|                    ||           ||j        v r
|j        d= d S d S )NauthenticatedsuccessT)r   futuresrs   r   jsonrejectsubscriptions)rA   rg   rV   rU   r   futureerrors          rC   handle_authzwoofipro.handle_auth/  s     &//'955 		:__V^_EEFNN4     '		'(:(:;;EMM%---f222(999 32rD   c                   K   |                                   | j        d         d         d         dz   | j        z   }|                     |          }d}d}|                    |          }|                     |j        |          }|t          |                                           }|}	| j	        }
|

                    d          dk    r|
                    d          }|d	         }
|                     |                     |	          |                     |
          d
          }|| j        ||dd}|                     ||          }|                     ||||           | d {V S )Nr*   r   r)   rQ   r   authzed25519:r   rF   ed25519)orderly_keysignru   )r`   rd   )check_required_credentialsr9   r.   rg   reusableFuturer   r   strnoncer-   findspliteddsaencodebase58_to_binaryr,   rS   rT   )rA   rd   rJ   rg   rU   r`   r   r   rj   r   r-   parts	signaturerX   rV   s                  rC   authenticatezwoofipro.authenticateD  si     '')))it$Y/#5FS!!%&&{33(<kJJ TZZ\\""BD[F{{:&&!++Z00q

4;;t#4#4d6K6KF6S6SU^__I#';%!#  G kk'622GJJsK+>>>||||||rD   c                    K   |                      |           d {V  | j        d         d         d         dz   | j        z   }|                     |          }d|i}|                     ||          }|                     |||||           d {V S Nr*   r   r)   rQ   rR   )r
  r9   r.   rM   rS   rT   )rA   rU   rV   rd   rJ   r4   rW   rX   s           rC   watch_privatezwoofipro.watch_private`  s      '''''''''it$Y/#5FOOC((	)
	 ++i11ZZ[';	RRRRRRRRRrD   c                    K   |                      |           d {V  | j        d         d         d         dz   | j        z   }|                     |          }d|i}|                     ||          }|                     |||||           d {V S r  )r
  r9   r.   rM   rS   watch_multiple)rA   messageHashesrV   rd   rJ   r4   rW   rX   s           rC   watch_private_multiplezwoofipro.watch_private_multiplej  s      '''''''''it$Y/#5FOOC((	)
	 ++i11((mWmU^_________rD   c                   K   |                                   d{V  |                     |ddd          }|rdnd}|                     |ddg          }|}|%|                     |          }|d         }|d|z   z  }d	|d
}	|                     |	|          }
|                     ||
           d{V }| j        r|                    ||          }|                     ||||d          S )a  
        watches information on multiple orders made by the user

        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/private/execution-report
        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/private/algo-execution-report

        :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
        :param bool [params.trigger]: True if trigger order
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        NstoptriggerFalgoexecutionreportexecutionreportrZ   :rW   r_   T	rb   safe_bool_2omitrc   rS   r  r   r   r   rA   rZ   r   r[   rd   r  ra   rU   rc   rX   rV   orderss               rC   watch_orderszwoofipro.watch_orderst  s)      !!!!!!!!!""669eDD*1I%%8I6FI#677[[((FH%F3<'K 
 
 ++gv..))+w????????? 	3OOFE22E00tTTTrD   c                   K   |                                   d{V  |                     |ddd          }|rdnd}|                     |d          }d}|%|                     |          }|d         }|d	|z   z  }d
|d}	|                     |	|          }
|                     ||
           d{V }| j        r|                    ||          }|                     ||||d          S )a  
        watches information on multiple trades made by the user

        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/private/execution-report
        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/private/algo-execution-report

        :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
        :param bool [params.trigger]: True if trigger order
        :returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
        Nr  r  Fr  r  myTradesrZ   r  rW   r_   Tr  r  s               rC   watch_my_tradeszwoofipro.watch_my_trades  s$      !!!!!!!!!""669eDD*1I%%8I66** [[((FH%F3<'K 
 
 ++gv..))+w????????? 	3OOFE22E00tTTTrD   c           	      |   |                      |d          }|                      |d          }|                     |          }|d         }|                     |d          }|                      |d          |                      |d          d}|                      |d          }|                     |d          }	|                     |d          }
t	          j        |d	          r|
|
}	|                      |d
          }|                     |d          }|                     |d          }|                     |d          }|                      |d          }|}t	          j        ||          rt	          j        ||          }|                      |d          }| 	                    |          }d }|                      |d          }|                     |d          }| 
                    i d|d|d|d|d|d|                     |          d|d|dd dd d|d|	d|d|d|dd dd |||||d          S )Nr   rZ   ru   totalFeer   r   r   avgPrice0quantityr   r   totalExecutedQuantitystatusclientOrderIdtriggerPricer   rR   r|   lastTradeTimestamptimeInForcepostOnly	stopPricer   r   r   )filled	remainingr'  r   r   )rm   rc   rH   r   r   	string_eqr   	string_ge
string_subparse_order_status
safe_orderr   )rA   r   rc   r   rt   rZ   ru   r   priceStringr   r#  r   r   r   r.  totalExecQuantityr/  	rawStatusr'  r   r(  r)  s                         rC   parse_ws_orderzwoofipro.parse_ws_order  s   D ""5)44##E844X&&!%%e[99	$$UJ77((
;;
 
 &&ug66  00##E:66[#.. 	H4HE!!%44%%eV44%%eV44!!%)@AA ,,U4KLL	V%677 	I*96GHHI$$UH55	((33((@@''~>>  
E 
f 
 ' 
 ]	 

  
 Y// 
 !) 
 D 
 4 
  
 D 
 U 
  
 L 
 f 
  D! 
" t# 
$ "- 
  
  
   	rD   c                 <   |                      |d          }|                     |d          }t          |t                    rt	          dt          |                    D ]b}||         }|                     |                      |d                    }||                     ||           |                     |||           cd S |                     |                      |d                    }||                     ||           |                     |||           d S )Nra   ri   r   r   )	rm   r   
isinstancelistr   r   	omit_zerohandle_my_tradehandle_order)rA   rg   rV   ra   ri   r   r   r   s           rC   handle_order_updatezwoofipro.handle_order_update,  s%   :   '22w//dD!! 	31c$ii(( 8 8Q..)9)9$	)J)JKK&((777!!&%77778 8 nnT%5%5dI%F%FGGG"$$VT222fdE22222rD   c                 &   |                      |          }|                     |d          }|                     |d          }|L| j        0|                     | j        dd          }t          |          | _        | j        }|                     |j        |i           }	|                     |	|          }
|
|                     |
d          }|||d<   | 	                    |
d          }|||d<   | 	                    |
d          |d<   |                     |
d          |d<   |                     |
d	          |d	<   |
                    |           |                    | j        |           |d
z   |z   }|                    | j        |           d S d S )NrZ   rR   r3   r/   r   feesr   ru   r|   r  )r8  rm   r  rH   r;   r   rG   hashmapr   r   r   rs   )rA   rg   rV   ra   r   rZ   r   r[   cachedOrdersr  r   r   rA  messageHashSymbols                 rC   r>  zwoofipro.handle_orderZ  s   $$W--!!&(33""6400{"))$,tLL4U;;;L^^L$8&"EEFNN6733E ooeU33?$'F5M~~eV44#%)F6N#'>>%#B#Bx &*&7&7{&K&K{#%)%5%5eZ%H%Hz"'''NN4;... %f 4NN4;(9:::::) rD   c                    d}|                      |d          }|                     |          }|d         }|                     ||          }| j        }|2|                     | j        dd          }	t          |	          }|| _        |                    |           |                    ||           |dz   |z   }
|                    ||
           d S )Nr  rZ   r2   r/   r  )	rm   rn   r   r  rH   r;   r   r   rs   )rA   rg   rV   rU   rt   rc   rZ   r   r   r[   symbolSpecificMessageHashs              rC   r=  zwoofipro.handle_my_tradet  s    : !##GX66!!(++!##GV44>%%dlM4HHE+E22F"DMev{+++$/#$5$>!v899999rD   c                 H  K   |                                   d{V  g }|                     |          }|                     |          sAt          dt	          |                    D ]"}||         }|                    d|z              #n|                    d           | j        d         d         d         dz   | j        z   }|                     |          }	| 	                    |	|           | 
                    d	d
d          }
| 
                    d	dd          }|
r=|r;| j        4|	                    d
           d{V }|                     ||||d          S ddd}|                     |||           d{V }| j        r|S |                     | j        |||d          S )a  

        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/private/position-push

        watch all open positions
        :param str[] [symbols]: list of unified market symbols
 @param since timestamp in ms of the earliest position to fetch
 @param limit the maximum number of positions to fetch
        :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   positions::	positionsr*   r   r)   rQ   r'   r0   Tr1   rW   positionr_   )rb   r   is_emptyr   r   r   r9   r.   rg   set_positions_cachehandle_optionrI  r   filter_by_symbols_since_limitr  r   )rA   r   r   r[   rd   r  r   rZ   rJ   rg   r0   r1   rv   rX   newPositionss                  rC   watch_positionszwoofipro.watch_positions  s      !!!!!!!!!%%g..}}W%% 	.1c'll++ = = $$]V%;<<<<=   ---it$Y/#5FS!!  111!%!3!34DF^`d!e!e!%!3!34DF^`d!e!e! 	]&< 	]AW#]]+CDDDDDDDDH55hPUW[\\\ 
 
 "88QWXXXXXXXX? 	 11$.'5RWY]^^^rD   c                     |                      ddd          }|r@d}||j        vr3|                    |           |                     | j        ||           d S d S t                      | _        d S )Nr'   r0   F)rM  r   r   spawnload_positions_snapshotr   rI  )rA   rg   r   r   r0   rU   s         rC   rL  zwoofipro.set_positions_cache  s    !%!3!34DF^`e!f!f! 	82K6>11k***

47MMMMM 21 677DNNNrD   c                   K   |                                   d {V }t                      | _        | j        }t          dt	          |                    D ]K}||         }|                     |dd          }t          j        |d          r|                    |           L||j	        v r:|j	        |         }|
                    |           |
                    |d           d S d S )Nr   	contractsr$  rI  )fetch_positionsr   rI  r   r   rm   r   	string_gtr   r   rs   )	rA   rg   rU   rI  cacher   rJ  rU  r   s	            rC   rS  z woofipro.load_positions_snapshot  s      ..00000000	133q#i..)) 	' 	'A |H((;DDI C00 'X&&&&.((^K0FNN5!!!NN5+..... )(rD   c                 <   |                      |di           }|                     |dg           }| j        t                      | _        | j        }g }t	          dt          |                    D ]}||         }|                     |d          }	|                     |	          }
|                     ||
          }|	                    |           |	                    |           d|
d         z   }|
                    ||           |
                    |d           d S )Nri   rI  r   rZ   rH  )rG   r   rI  r   r   r   rm   rn   parse_ws_positionr   rs   )rA   rg   rV   ri   rawPositionsrX  rO  r   rawPositionrt   rc   rJ  rU   s                rC   handle_positionszwoofipro.handle_positions  s   B ~~gvr22~~dK<<>!577DNq#l++,, 	2 	2A&q/K''X>>H%%h//F--k6BBH)))LL"""'&*::KNN8[1111|[11111rD   c                    |                      |d          }|                     ||          }|                      |d          }d }t          j        |d          rd}nd}|                      |d          }|                      |d          }|                     |d          }|                      |d	          }	|                      |d
          }
t          j        |          }t          j        ||          }|                     i d|dd d|                      |d          d|d|                     |          dd dd dd dd dd d| 	                    |	          d| 	                    |          dd d| 	                    |
          d| 	                    |          d| 	                    |          dd | 
                    |d          | 	                    |          d d dd |d d d d d          S )NrZ   positionQtyr$  longshortcontractSize	markPriceru   averageOpenPriceunsettledPnlr   rR   r|   lastUpdateTimestampinitialMargininitialMarginPercentagemaintenanceMarginmaintenanceMarginPercentage
entryPricenotionalleverageunrealizedPnlrU  marginRatioestLiqPricecross)liquidationPricerc  	lastPrice
collateral
marginMode
marginTyper   r   hedgedstopLossPricetakeProfitPrice)rm   rn   r   rW  rH   
string_absr   safe_positionr   parse_numberr   )rA   rJ  rc   contractr   r   rb  rc  ru   rk  unrealisedPnlrl  s               rC   rZ  zwoofipro.parse_ws_position  s   2 ##Hh77!!(F33-88T3'' 	DDD''??$$X{;;	%%h<<	%%h0BCC
((>BB!$''%dI66!! #
H#
$#
 d&&vx88#
 	#

 Y//#
 "4#
 T#
 &t#
  #
 *4#
 $++J77#
 ))(33#
 #
 T..}==#
 **400#
  D--l;;!#
" 4##
$ !% 0 0= I I**955!!#9#
 #
 #
   	rD   c                    K   |                                   d{V  d}|}d|d}|                     ||          }|                     ||           d{V S )a{  
        watch balance and get the amount of funds available for trading or funds locked in orders

        https://orderly.network/docs/build-on-omnichain/evm-api/websocket-api/private/balance

        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
        NbalancerW   r_   )rb   rS   r  )rA   rd   ra   rU   rX   rV   s         rC   watch_balancezwoofipro.watch_balanceX  s       !!!!!!!!! 
 
 ++gv..''W=========rD   c                 4   |                      |di           }|                      |di           }t          |                                          }|                     |d          }|| j        d<   || j        d<   |                     |          | j        d<   t          dt          |                    D ]}||         }||         }	|                     |          }
|
| j        v r| j        |
         n| 	                                }| 
                    |	d          }| 
                    |	d	          }||d
<   ||d<   t          j        ||          |d<   || j        |
<   |                     | j                  | _        |                    | j        d           d S )Nri   balancesrj   r   ru   r|   r   holdingfrozentotalusedfreer  )rG   r;  keysrH   r  r   r   r   r   accountrm   r   r2  safe_balancers   )rA   rg   rV   ri   r  r  rj   r   keyvaluecoder  r  r  s                 rC   handle_balancezwoofipro.handle_balancek  s   8 ~~gvr22>>$
B77HMMOO$$w--#V$&[!#'<<#3#3Z q#d))$$ 
	) 
	)Aq'CSME**3//D-1T\-A-Adl4((G$$UI66E##E844D$GG"GFO%0==GFO!(DL((66t|Y/////rD   c                    d|vrdS |                      |d          }|rdS |                     |d          }	 |B| j        dz   |                     |          z   }|                     | j        d         ||           dS # t          $ r_}t          |t                    r*d}|	                    ||           ||j
        v r|j
        |= n|	                    |           Y d }~dS d }~ww xY w)Nr   FerrorMsg r7   r   T)r   rm   rR   r   throw_exactly_matched_exceptionr=   	Exceptionr:  r   r   r   )rA   rg   rV   r   errorMessagefeedbackr   rU   s           rC   handle_error_messagezwoofipro.handle_error_message  s    W$$5..)44 	5''<<	'7S=499W+=+==44T_W5M|]efff5 	 	 	%!455 %-e[111&"666,[9e$$$44444	s   AA> >
C'AC""C'c                 x   |                      ||          rd S | j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
        | j
        | j        | j        | j        d}|                     |d          }|                     ||          }| |||           d S |                     |d          }||                     ||          }| |||           d S |                    d          }t#          |          }|dk    r|                     |d          }	|                     ||	          }| |||           d S |	                    d          }
t#          |          }|dk    r>|                     ||                     |
d                    }| |||           d S d S d S d S d S )	N)r5   pongrW   r]   ry   r   r   r   r   r  r  rJ  r  r   r`   ra   r^      rF   r   r   )r  handle_pinghandle_ponghandle_subscriberw   r   r   r   r   r   r?  r]  r  r   rm   r   r  r   )rA   rg   rV   methodsr`   methodra   
splitTopicsplitLengthre   	splitNamesplitNameLengths               rC   handle_messagezwoofipro.handle_message  s   $$VW55 	F$$./(*&&$#7#'#;-*'
 
    '22%00F67###F  '22__We44F!vw'''S))Jj//Ka''
A66$77%F67+++F JJsOO	"%j//"a''!__Wd6F6FyRS6T6TUUF)vw/////%    ('))rD   c                 
    ddiS )Nr`   r5    )rA   rg   s     rC   r5   zwoofipro.ping        rD   c                 
    ddiS )Nr`   r  r  rA   rg   rV   s      rC   r  zwoofipro.handle_ping  r  rD   c                 8    |                                  |_        |S N)millisecondslastPongr  s      rC   r  zwoofipro.handle_pong  s     ++--rD   c                     |S r  r  r  s      rC   r  zwoofipro.handle_subscribe  s	     rD   r  )=__name__
__module____qualname__r   r@   rM   rY   r  r
   r   rf   r   rw   r   rz   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   r8  r?  r>  r=  r   rP  rL  rS  r]  rZ  r   r  r  r	   r  r  r5   r  r  r  __classcell__)rB   s   @rC   r   r      s       7# 7 7 7 7 7 7r  S S S @DB ! !S ! !S\ ! ! ! !.!) !) !) !) !)F 68 7 7 7F 7 7 7 7," " " "HF    : 6:" @ @7 @ @ @ @ @,&V & & & &@ 8<B @ @W @7 @ @ @ @,&V & & & &4     ?CQUdhqs H H H H3 H^a Hx|  ~B  yC H H H H<**6 ** ** ** **X <@d[] U U US U Ubfglbm U U U U4&6 & & & &:F F F FP:& : : : :* )+    8 @B S S S S KM ` ` ` ` 04$UYbd U U UC Us Uimnsit U U U UB 37TX\eg U UC Us URU Ulpqvlw U U U UBt t t tl,3& ,3 ,3 ,3 ,3\;6 ; ; ; ;4*:f *: *: *: *:X 8<$]ajl $_ $_W $_C $_WZ $_quv~q $_ $_ $_ $_L8 8& 8 8 8 8 8/ / /02 02 02dE E E EN *, > > > > > >&/0 /0 /0b6 t    2+0V +0 +0 +0 +0Z!6 ! ! ! !!& ! ! ! !&    	v 	 	 	 	 	 	 	 	rD   r   )ccxt.async_supportccxt ccxt.async_support.base.ws.cacher   r   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   ccxt.base.preciser   async_supportr   r  rD   rC   <module>r     sM       A  A  A  A  A  A  A  A  A  A  A  A v v v v v v v v v v v v v v v v v v v v v v v v v v v v 4 4 4 4 4 4       0 0 0 0 0 0 ) ) ) ) ) ) % % % % % %h h h h ht!* h h h h hrD   