@katanaperps/katana-perps-sdk 0.0.0-placeholder
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.nvmrc +1 -0
- package/LICENSE +21 -0
- package/README.md +90 -0
- package/dist/abis/ERC20.json +288 -0
- package/dist/abis/EarningsEscrow.json +468 -0
- package/dist/abis/Exchange_v1.json +3644 -0
- package/dist/abis/IOFT.json +447 -0
- package/dist/bridge/config.d.ts +262 -0
- package/dist/bridge/config.d.ts.map +1 -0
- package/dist/bridge/config.js +221 -0
- package/dist/bridge/deposit.d.ts +107 -0
- package/dist/bridge/deposit.d.ts.map +1 -0
- package/dist/bridge/deposit.js +284 -0
- package/dist/bridge/index.d.ts +5 -0
- package/dist/bridge/index.d.ts.map +1 -0
- package/dist/bridge/index.js +7 -0
- package/dist/bridge/utils.d.ts +140 -0
- package/dist/bridge/utils.d.ts.map +1 -0
- package/dist/bridge/utils.js +66 -0
- package/dist/bridge/withdraw.d.ts +42 -0
- package/dist/bridge/withdraw.d.ts.map +1 -0
- package/dist/bridge/withdraw.js +86 -0
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +6 -0
- package/dist/client/orderBook/index.d.ts +2 -0
- package/dist/client/orderBook/index.d.ts.map +1 -0
- package/dist/client/orderBook/index.js +4 -0
- package/dist/client/orderBook/realTime.d.ts +180 -0
- package/dist/client/orderBook/realTime.d.ts.map +1 -0
- package/dist/client/orderBook/realTime.js +344 -0
- package/dist/client/orderBook/utils.d.ts +15 -0
- package/dist/client/orderBook/utils.d.ts.map +1 -0
- package/dist/client/orderBook/utils.js +77 -0
- package/dist/client/rest/authenticated.d.ts +1199 -0
- package/dist/client/rest/authenticated.d.ts.map +1 -0
- package/dist/client/rest/authenticated.js +1311 -0
- package/dist/client/rest/index.d.ts +3 -0
- package/dist/client/rest/index.d.ts.map +1 -0
- package/dist/client/rest/index.js +5 -0
- package/dist/client/rest/public.d.ts +436 -0
- package/dist/client/rest/public.d.ts.map +1 -0
- package/dist/client/rest/public.js +502 -0
- package/dist/client/webSocket/guards.d.ts +15 -0
- package/dist/client/webSocket/guards.d.ts.map +1 -0
- package/dist/client/webSocket/guards.js +37 -0
- package/dist/client/webSocket/index.d.ts +632 -0
- package/dist/client/webSocket/index.d.ts.map +1 -0
- package/dist/client/webSocket/index.js +1048 -0
- package/dist/client/webSocket/transform.d.ts +3 -0
- package/dist/client/webSocket/transform.d.ts.map +1 -0
- package/dist/client/webSocket/transform.js +244 -0
- package/dist/constants.d.ts +34 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +38 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/orderbook/apiConversions.d.ts +8 -0
- package/dist/orderbook/apiConversions.d.ts.map +1 -0
- package/dist/orderbook/apiConversions.js +100 -0
- package/dist/orderbook/demo.d.ts +2 -0
- package/dist/orderbook/demo.d.ts.map +1 -0
- package/dist/orderbook/demo.js +68 -0
- package/dist/orderbook/index.d.ts +3 -0
- package/dist/orderbook/index.d.ts.map +1 -0
- package/dist/orderbook/index.js +5 -0
- package/dist/orderbook/quantities.d.ts +112 -0
- package/dist/orderbook/quantities.d.ts.map +1 -0
- package/dist/orderbook/quantities.js +257 -0
- package/dist/orderbook/utils.d.ts +8 -0
- package/dist/orderbook/utils.d.ts.map +1 -0
- package/dist/orderbook/utils.js +28 -0
- package/dist/pipmath.d.ts +21 -0
- package/dist/pipmath.d.ts.map +1 -0
- package/dist/pipmath.js +110 -0
- package/dist/signatures.d.ts +37 -0
- package/dist/signatures.d.ts.map +1 -0
- package/dist/signatures.js +450 -0
- package/dist/tests/orderbook/quantities/calculateInitialMarginFractionWithOverride.test.d.ts +2 -0
- package/dist/tests/orderbook/quantities/calculateInitialMarginFractionWithOverride.test.d.ts.map +1 -0
- package/dist/tests/orderbook/quantities/calculateInitialMarginFractionWithOverride.test.js +49 -0
- package/dist/tests/orderbook/quantities/calculateMaximumInitialMarginFractionOverride.test.d.ts +2 -0
- package/dist/tests/orderbook/quantities/calculateMaximumInitialMarginFractionOverride.test.d.ts.map +1 -0
- package/dist/tests/orderbook/quantities/calculateMaximumInitialMarginFractionOverride.test.js +122 -0
- package/dist/tests/orderbook/quantities/determineMaximumReduceOnlyQuantityAvailableAtPriceLevel.test.d.ts +2 -0
- package/dist/tests/orderbook/quantities/determineMaximumReduceOnlyQuantityAvailableAtPriceLevel.test.d.ts.map +1 -0
- package/dist/tests/orderbook/quantities/determineMaximumReduceOnlyQuantityAvailableAtPriceLevel.test.js +138 -0
- package/dist/tests/orderbook/quantities.test.d.ts +2 -0
- package/dist/tests/orderbook/quantities.test.d.ts.map +1 -0
- package/dist/tests/orderbook/quantities.test.js +310 -0
- package/dist/tests/testHelpers.d.ts +2 -0
- package/dist/tests/testHelpers.d.ts.map +1 -0
- package/dist/tests/testHelpers.js +16 -0
- package/dist/tests/utils/deriveBaseURL.test.d.ts +2 -0
- package/dist/tests/utils/deriveBaseURL.test.d.ts.map +1 -0
- package/dist/tests/utils/deriveBaseURL.test.js +46 -0
- package/dist/typechain-types/ERC20.d.ts +170 -0
- package/dist/typechain-types/ERC20.d.ts.map +1 -0
- package/dist/typechain-types/ERC20.js +2 -0
- package/dist/typechain-types/EarningsEscrow.d.ts +210 -0
- package/dist/typechain-types/EarningsEscrow.d.ts.map +1 -0
- package/dist/typechain-types/EarningsEscrow.js +2 -0
- package/dist/typechain-types/ExchangeLayerZeroAdapter.d.ts +253 -0
- package/dist/typechain-types/ExchangeLayerZeroAdapter.d.ts.map +1 -0
- package/dist/typechain-types/ExchangeLayerZeroAdapter.js +2 -0
- package/dist/typechain-types/ExchangeLayerZeroAdapter_v2.d.ts +338 -0
- package/dist/typechain-types/ExchangeLayerZeroAdapter_v2.d.ts.map +1 -0
- package/dist/typechain-types/ExchangeLayerZeroAdapter_v2.js +2 -0
- package/dist/typechain-types/ExchangeLayerZeroAdapter_v3.d.ts +413 -0
- package/dist/typechain-types/ExchangeLayerZeroAdapter_v3.d.ts.map +1 -0
- package/dist/typechain-types/ExchangeLayerZeroAdapter_v3.js +2 -0
- package/dist/typechain-types/Exchange_v1.d.ts +2172 -0
- package/dist/typechain-types/Exchange_v1.d.ts.map +1 -0
- package/dist/typechain-types/Exchange_v1.js +2 -0
- package/dist/typechain-types/Exchange_v2.d.ts +2148 -0
- package/dist/typechain-types/Exchange_v2.d.ts.map +1 -0
- package/dist/typechain-types/Exchange_v2.js +2 -0
- package/dist/typechain-types/FixedIncomeVaultProviderStateAggregator_v1.d.ts +148 -0
- package/dist/typechain-types/FixedIncomeVaultProviderStateAggregator_v1.d.ts.map +1 -0
- package/dist/typechain-types/FixedIncomeVaultProviderStateAggregator_v1.js +2 -0
- package/dist/typechain-types/FixedIncomeVaultProvider_v1.d.ts +1204 -0
- package/dist/typechain-types/FixedIncomeVaultProvider_v1.d.ts.map +1 -0
- package/dist/typechain-types/FixedIncomeVaultProvider_v1.js +2 -0
- package/dist/typechain-types/IOFT.d.ts +247 -0
- package/dist/typechain-types/IOFT.d.ts.map +1 -0
- package/dist/typechain-types/IOFT.js +2 -0
- package/dist/typechain-types/IRewardVault.d.ts +649 -0
- package/dist/typechain-types/IRewardVault.d.ts.map +1 -0
- package/dist/typechain-types/IRewardVault.js +2 -0
- package/dist/typechain-types/KumaOFTAdapterUSDC.d.ts +638 -0
- package/dist/typechain-types/KumaOFTAdapterUSDC.d.ts.map +1 -0
- package/dist/typechain-types/KumaOFTAdapterUSDC.js +2 -0
- package/dist/typechain-types/KumaOFTUSDC.d.ts +756 -0
- package/dist/typechain-types/KumaOFTUSDC.d.ts.map +1 -0
- package/dist/typechain-types/KumaOFTUSDC.js +2 -0
- package/dist/typechain-types/KumaStargateForwarder_v1.d.ts +252 -0
- package/dist/typechain-types/KumaStargateForwarder_v1.d.ts.map +1 -0
- package/dist/typechain-types/KumaStargateForwarder_v1.js +2 -0
- package/dist/typechain-types/KumaStargateForwarder_v2.d.ts +252 -0
- package/dist/typechain-types/KumaStargateForwarder_v2.d.ts.map +1 -0
- package/dist/typechain-types/KumaStargateForwarder_v2.js +2 -0
- package/dist/typechain-types/ProfitShareVaultProvider_v1.d.ts +1095 -0
- package/dist/typechain-types/ProfitShareVaultProvider_v1.d.ts.map +1 -0
- package/dist/typechain-types/ProfitShareVaultProvider_v1.js +2 -0
- package/dist/typechain-types/StargatePoolUSDC.d.ts +956 -0
- package/dist/typechain-types/StargatePoolUSDC.d.ts.map +1 -0
- package/dist/typechain-types/StargatePoolUSDC.js +2 -0
- package/dist/typechain-types/common.d.ts +51 -0
- package/dist/typechain-types/common.d.ts.map +1 -0
- package/dist/typechain-types/common.js +2 -0
- package/dist/typechain-types/factories/ERC20__factory.d.ts +226 -0
- package/dist/typechain-types/factories/ERC20__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/ERC20__factory.js +305 -0
- package/dist/typechain-types/factories/EarningsEscrow__factory.d.ts +370 -0
- package/dist/typechain-types/factories/EarningsEscrow__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/EarningsEscrow__factory.js +485 -0
- package/dist/typechain-types/factories/ExchangeLayerZeroAdapter__factory.d.ts +359 -0
- package/dist/typechain-types/factories/ExchangeLayerZeroAdapter__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/ExchangeLayerZeroAdapter__factory.js +471 -0
- package/dist/typechain-types/factories/ExchangeLayerZeroAdapter_v2__factory.d.ts +419 -0
- package/dist/typechain-types/factories/ExchangeLayerZeroAdapter_v2__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/ExchangeLayerZeroAdapter_v2__factory.js +545 -0
- package/dist/typechain-types/factories/ExchangeLayerZeroAdapter_v3__factory.d.ts +546 -0
- package/dist/typechain-types/factories/ExchangeLayerZeroAdapter_v3__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/ExchangeLayerZeroAdapter_v3__factory.js +710 -0
- package/dist/typechain-types/factories/Exchange_v1__factory.d.ts +2852 -0
- package/dist/typechain-types/factories/Exchange_v1__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/Exchange_v1__factory.js +3661 -0
- package/dist/typechain-types/factories/Exchange_v2__factory.d.ts +2832 -0
- package/dist/typechain-types/factories/Exchange_v2__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/Exchange_v2__factory.js +3636 -0
- package/dist/typechain-types/factories/FixedIncomeVaultProviderStateAggregator_v1__factory.d.ts +180 -0
- package/dist/typechain-types/factories/FixedIncomeVaultProviderStateAggregator_v1__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/FixedIncomeVaultProviderStateAggregator_v1__factory.js +246 -0
- package/dist/typechain-types/factories/FixedIncomeVaultProvider_v1__factory.d.ts +2092 -0
- package/dist/typechain-types/factories/FixedIncomeVaultProvider_v1__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/FixedIncomeVaultProvider_v1__factory.js +2702 -0
- package/dist/typechain-types/factories/IOFT__factory.d.ts +352 -0
- package/dist/typechain-types/factories/IOFT__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/IOFT__factory.js +464 -0
- package/dist/typechain-types/factories/IRewardVault__factory.d.ts +1019 -0
- package/dist/typechain-types/factories/IRewardVault__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/IRewardVault__factory.js +1311 -0
- package/dist/typechain-types/factories/KumaOFTAdapterUSDC__factory.d.ts +996 -0
- package/dist/typechain-types/factories/KumaOFTAdapterUSDC__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/KumaOFTAdapterUSDC__factory.js +1306 -0
- package/dist/typechain-types/factories/KumaOFTUSDC__factory.d.ts +1234 -0
- package/dist/typechain-types/factories/KumaOFTUSDC__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/KumaOFTUSDC__factory.js +1619 -0
- package/dist/typechain-types/factories/KumaStargateForwarder_v1__factory.d.ts +354 -0
- package/dist/typechain-types/factories/KumaStargateForwarder_v1__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/KumaStargateForwarder_v1__factory.js +464 -0
- package/dist/typechain-types/factories/KumaStargateForwarder_v2__factory.d.ts +370 -0
- package/dist/typechain-types/factories/KumaStargateForwarder_v2__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/KumaStargateForwarder_v2__factory.js +486 -0
- package/dist/typechain-types/factories/ProfitShareVaultProvider_v1__factory.d.ts +1914 -0
- package/dist/typechain-types/factories/ProfitShareVaultProvider_v1__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/ProfitShareVaultProvider_v1__factory.js +2475 -0
- package/dist/typechain-types/factories/StargatePoolUSDC__factory.d.ts +1557 -0
- package/dist/typechain-types/factories/StargatePoolUSDC__factory.d.ts.map +1 -0
- package/dist/typechain-types/factories/StargatePoolUSDC__factory.js +2019 -0
- package/dist/typechain-types/factories/index.d.ts +5 -0
- package/dist/typechain-types/factories/index.d.ts.map +1 -0
- package/dist/typechain-types/factories/index.js +13 -0
- package/dist/typechain-types/index.d.ts +10 -0
- package/dist/typechain-types/index.d.ts.map +1 -0
- package/dist/typechain-types/index.js +15 -0
- package/dist/types/delegatedKeys.d.ts +44 -0
- package/dist/types/delegatedKeys.d.ts.map +1 -0
- package/dist/types/delegatedKeys.js +2 -0
- package/dist/types/enums/index.d.ts +4 -0
- package/dist/types/enums/index.d.ts.map +1 -0
- package/dist/types/enums/index.js +6 -0
- package/dist/types/enums/request.d.ts +980 -0
- package/dist/types/enums/request.d.ts.map +1 -0
- package/dist/types/enums/request.js +906 -0
- package/dist/types/enums/response.d.ts +329 -0
- package/dist/types/enums/response.d.ts.map +1 -0
- package/dist/types/enums/response.js +296 -0
- package/dist/types/enums/signature.d.ts +118 -0
- package/dist/types/enums/signature.d.ts.map +1 -0
- package/dist/types/enums/signature.js +131 -0
- package/dist/types/errors.d.ts +5 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +2 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +10 -0
- package/dist/types/orderBook.d.ts +72 -0
- package/dist/types/orderBook.d.ts.map +1 -0
- package/dist/types/orderBook.js +2 -0
- package/dist/types/rest/common/common.d.ts +118 -0
- package/dist/types/rest/common/common.d.ts.map +1 -0
- package/dist/types/rest/common/common.js +2 -0
- package/dist/types/rest/common/guards.d.ts +8 -0
- package/dist/types/rest/common/guards.d.ts.map +1 -0
- package/dist/types/rest/common/guards.js +14 -0
- package/dist/types/rest/endpoints/AssociateWallet.d.ts +40 -0
- package/dist/types/rest/endpoints/AssociateWallet.d.ts.map +1 -0
- package/dist/types/rest/endpoints/AssociateWallet.js +2 -0
- package/dist/types/rest/endpoints/CancelOrders.d.ts +134 -0
- package/dist/types/rest/endpoints/CancelOrders.d.ts.map +1 -0
- package/dist/types/rest/endpoints/CancelOrders.js +2 -0
- package/dist/types/rest/endpoints/CreateOrder.d.ts +401 -0
- package/dist/types/rest/endpoints/CreateOrder.d.ts.map +1 -0
- package/dist/types/rest/endpoints/CreateOrder.js +2 -0
- package/dist/types/rest/endpoints/GetAuthenticationToken.d.ts +39 -0
- package/dist/types/rest/endpoints/GetAuthenticationToken.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetAuthenticationToken.js +2 -0
- package/dist/types/rest/endpoints/GetCandles.d.ts +115 -0
- package/dist/types/rest/endpoints/GetCandles.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetCandles.js +2 -0
- package/dist/types/rest/endpoints/GetDeposits.d.ts +109 -0
- package/dist/types/rest/endpoints/GetDeposits.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetDeposits.js +2 -0
- package/dist/types/rest/endpoints/GetExchange.d.ts +78 -0
- package/dist/types/rest/endpoints/GetExchange.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetExchange.js +9 -0
- package/dist/types/rest/endpoints/GetFills.d.ts +178 -0
- package/dist/types/rest/endpoints/GetFills.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetFills.js +2 -0
- package/dist/types/rest/endpoints/GetFundingPayments.d.ts +52 -0
- package/dist/types/rest/endpoints/GetFundingPayments.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetFundingPayments.js +2 -0
- package/dist/types/rest/endpoints/GetFundingRates.d.ts +34 -0
- package/dist/types/rest/endpoints/GetFundingRates.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetFundingRates.js +2 -0
- package/dist/types/rest/endpoints/GetGasFees.d.ts +23 -0
- package/dist/types/rest/endpoints/GetGasFees.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetGasFees.js +2 -0
- package/dist/types/rest/endpoints/GetHistoricalPnL.d.ts +39 -0
- package/dist/types/rest/endpoints/GetHistoricalPnL.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetHistoricalPnL.js +2 -0
- package/dist/types/rest/endpoints/GetInitialMarginFractionOverride.d.ts +31 -0
- package/dist/types/rest/endpoints/GetInitialMarginFractionOverride.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetInitialMarginFractionOverride.js +2 -0
- package/dist/types/rest/endpoints/GetLiquidations.d.ts +52 -0
- package/dist/types/rest/endpoints/GetLiquidations.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetLiquidations.js +2 -0
- package/dist/types/rest/endpoints/GetMarketMakerRewardsEpochs.d.ts +534 -0
- package/dist/types/rest/endpoints/GetMarketMakerRewardsEpochs.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetMarketMakerRewardsEpochs.js +2 -0
- package/dist/types/rest/endpoints/GetMarkets.d.ts +161 -0
- package/dist/types/rest/endpoints/GetMarkets.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetMarkets.js +2 -0
- package/dist/types/rest/endpoints/GetOrderBook.d.ts +176 -0
- package/dist/types/rest/endpoints/GetOrderBook.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetOrderBook.js +2 -0
- package/dist/types/rest/endpoints/GetOrders.d.ts +188 -0
- package/dist/types/rest/endpoints/GetOrders.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetOrders.js +2 -0
- package/dist/types/rest/endpoints/GetPing.d.ts +8 -0
- package/dist/types/rest/endpoints/GetPing.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetPing.js +2 -0
- package/dist/types/rest/endpoints/GetPositions.d.ts +104 -0
- package/dist/types/rest/endpoints/GetPositions.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetPositions.js +2 -0
- package/dist/types/rest/endpoints/GetTickers.d.ts +129 -0
- package/dist/types/rest/endpoints/GetTickers.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetTickers.js +2 -0
- package/dist/types/rest/endpoints/GetTime.d.ts +14 -0
- package/dist/types/rest/endpoints/GetTime.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetTime.js +2 -0
- package/dist/types/rest/endpoints/GetTrades.d.ts +62 -0
- package/dist/types/rest/endpoints/GetTrades.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetTrades.js +2 -0
- package/dist/types/rest/endpoints/GetWallets.d.ts +121 -0
- package/dist/types/rest/endpoints/GetWallets.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetWallets.js +2 -0
- package/dist/types/rest/endpoints/GetWithdrawals.d.ts +98 -0
- package/dist/types/rest/endpoints/GetWithdrawals.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetWithdrawals.js +2 -0
- package/dist/types/rest/endpoints/GetWithdrawalsFromManagedAccount.d.ts +98 -0
- package/dist/types/rest/endpoints/GetWithdrawalsFromManagedAccount.d.ts.map +1 -0
- package/dist/types/rest/endpoints/GetWithdrawalsFromManagedAccount.js +2 -0
- package/dist/types/rest/endpoints/Payouts.d.ts +199 -0
- package/dist/types/rest/endpoints/Payouts.d.ts.map +1 -0
- package/dist/types/rest/endpoints/Payouts.js +2 -0
- package/dist/types/rest/endpoints/SetInitialMarginFractionOverride.d.ts +83 -0
- package/dist/types/rest/endpoints/SetInitialMarginFractionOverride.d.ts.map +1 -0
- package/dist/types/rest/endpoints/SetInitialMarginFractionOverride.js +2 -0
- package/dist/types/rest/endpoints/WithdrawFunds.d.ts +144 -0
- package/dist/types/rest/endpoints/WithdrawFunds.d.ts.map +1 -0
- package/dist/types/rest/endpoints/WithdrawFunds.js +2 -0
- package/dist/types/rest/endpoints/WithdrawFundsFromManagedAccountByQuantity.d.ts +161 -0
- package/dist/types/rest/endpoints/WithdrawFundsFromManagedAccountByQuantity.d.ts.map +1 -0
- package/dist/types/rest/endpoints/WithdrawFundsFromManagedAccountByQuantity.js +2 -0
- package/dist/types/rest/endpoints/WithdrawFundsFromManagedAccountByShares.d.ts +160 -0
- package/dist/types/rest/endpoints/WithdrawFundsFromManagedAccountByShares.d.ts.map +1 -0
- package/dist/types/rest/endpoints/WithdrawFundsFromManagedAccountByShares.js +2 -0
- package/dist/types/rest/endpoints/index.d.ts +33 -0
- package/dist/types/rest/endpoints/index.d.ts.map +1 -0
- package/dist/types/rest/endpoints/index.js +35 -0
- package/dist/types/rest/endpoints/internal.d.ts +329 -0
- package/dist/types/rest/endpoints/internal.d.ts.map +1 -0
- package/dist/types/rest/endpoints/internal.js +14 -0
- package/dist/types/rest/index.d.ts +4 -0
- package/dist/types/rest/index.d.ts.map +1 -0
- package/dist/types/rest/index.js +6 -0
- package/dist/types/swaps.d.ts +11 -0
- package/dist/types/swaps.d.ts.map +1 -0
- package/dist/types/swaps.js +2 -0
- package/dist/types/utils.d.ts +67 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utils.js +3 -0
- package/dist/types/webSocket/base.d.ts +83 -0
- package/dist/types/webSocket/base.d.ts.map +1 -0
- package/dist/types/webSocket/base.js +2 -0
- package/dist/types/webSocket/constants.d.ts +41 -0
- package/dist/types/webSocket/constants.d.ts.map +1 -0
- package/dist/types/webSocket/constants.js +54 -0
- package/dist/types/webSocket/index.d.ts +6 -0
- package/dist/types/webSocket/index.d.ts.map +1 -0
- package/dist/types/webSocket/index.js +8 -0
- package/dist/types/webSocket/options.d.ts +276 -0
- package/dist/types/webSocket/options.d.ts.map +1 -0
- package/dist/types/webSocket/options.js +2 -0
- package/dist/types/webSocket/request/index.d.ts +3 -0
- package/dist/types/webSocket/request/index.d.ts.map +1 -0
- package/dist/types/webSocket/request/index.js +5 -0
- package/dist/types/webSocket/request/request.d.ts +137 -0
- package/dist/types/webSocket/request/request.d.ts.map +1 -0
- package/dist/types/webSocket/request/request.js +2 -0
- package/dist/types/webSocket/request/subscriptions.d.ts +498 -0
- package/dist/types/webSocket/request/subscriptions.d.ts.map +1 -0
- package/dist/types/webSocket/request/subscriptions.js +2 -0
- package/dist/types/webSocket/response/candles.d.ts +138 -0
- package/dist/types/webSocket/response/candles.d.ts.map +1 -0
- package/dist/types/webSocket/response/candles.js +2 -0
- package/dist/types/webSocket/response/deposits.d.ts +80 -0
- package/dist/types/webSocket/response/deposits.d.ts.map +1 -0
- package/dist/types/webSocket/response/deposits.js +2 -0
- package/dist/types/webSocket/response/fundingPayments.d.ts +78 -0
- package/dist/types/webSocket/response/fundingPayments.d.ts.map +1 -0
- package/dist/types/webSocket/response/fundingPayments.js +2 -0
- package/dist/types/webSocket/response/index.d.ts +13 -0
- package/dist/types/webSocket/response/index.d.ts.map +1 -0
- package/dist/types/webSocket/response/index.js +15 -0
- package/dist/types/webSocket/response/liquidations.d.ts +78 -0
- package/dist/types/webSocket/response/liquidations.d.ts.map +1 -0
- package/dist/types/webSocket/response/liquidations.js +2 -0
- package/dist/types/webSocket/response/orderbook.d.ts +214 -0
- package/dist/types/webSocket/response/orderbook.d.ts.map +1 -0
- package/dist/types/webSocket/response/orderbook.js +2 -0
- package/dist/types/webSocket/response/orders.d.ts +355 -0
- package/dist/types/webSocket/response/orders.d.ts.map +1 -0
- package/dist/types/webSocket/response/orders.js +2 -0
- package/dist/types/webSocket/response/ordersFill.d.ts +138 -0
- package/dist/types/webSocket/response/ordersFill.d.ts.map +1 -0
- package/dist/types/webSocket/response/ordersFill.js +2 -0
- package/dist/types/webSocket/response/positions.d.ts +120 -0
- package/dist/types/webSocket/response/positions.d.ts.map +1 -0
- package/dist/types/webSocket/response/positions.js +2 -0
- package/dist/types/webSocket/response/tickers.d.ts +131 -0
- package/dist/types/webSocket/response/tickers.d.ts.map +1 -0
- package/dist/types/webSocket/response/tickers.js +2 -0
- package/dist/types/webSocket/response/trades.d.ts +90 -0
- package/dist/types/webSocket/response/trades.d.ts.map +1 -0
- package/dist/types/webSocket/response/trades.js +2 -0
- package/dist/types/webSocket/response/webclient/base.d.ts +24 -0
- package/dist/types/webSocket/response/webclient/base.d.ts.map +1 -0
- package/dist/types/webSocket/response/webclient/base.js +2 -0
- package/dist/types/webSocket/response/webclient/events/exchangeStatus.d.ts +31 -0
- package/dist/types/webSocket/response/webclient/events/exchangeStatus.d.ts.map +1 -0
- package/dist/types/webSocket/response/webclient/events/exchangeStatus.js +2 -0
- package/dist/types/webSocket/response/webclient/events/index.d.ts +4 -0
- package/dist/types/webSocket/response/webclient/events/index.d.ts.map +1 -0
- package/dist/types/webSocket/response/webclient/events/index.js +6 -0
- package/dist/types/webSocket/response/webclient/events/reloadBanners.d.ts +17 -0
- package/dist/types/webSocket/response/webclient/events/reloadBanners.d.ts.map +1 -0
- package/dist/types/webSocket/response/webclient/events/reloadBanners.js +2 -0
- package/dist/types/webSocket/response/webclient/events/transactionSettled.d.ts +76 -0
- package/dist/types/webSocket/response/webclient/events/transactionSettled.d.ts.map +1 -0
- package/dist/types/webSocket/response/webclient/events/transactionSettled.js +2 -0
- package/dist/types/webSocket/response/webclient/index.d.ts +3 -0
- package/dist/types/webSocket/response/webclient/index.d.ts.map +1 -0
- package/dist/types/webSocket/response/webclient/index.js +5 -0
- package/dist/types/webSocket/response/webclient/webclient.d.ts +13 -0
- package/dist/types/webSocket/response/webclient/webclient.d.ts.map +1 -0
- package/dist/types/webSocket/response/webclient/webclient.js +2 -0
- package/dist/types/webSocket/response/withdrawals.d.ts +83 -0
- package/dist/types/webSocket/response/withdrawals.d.ts.map +1 -0
- package/dist/types/webSocket/response/withdrawals.js +2 -0
- package/dist/utils.d.ts +63 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +136 -0
- package/package.json +200 -0
|
@@ -0,0 +1,1048 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebSocketClient = exports.WEBSOCKET_PING_TIMEOUT_MS = exports.transformWebsocketShortResponseMessage = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const isomorphic_ws_1 = tslib_1.__importDefault(require("isomorphic-ws"));
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
const _utils_1 = require("#utils");
|
|
8
|
+
const authenticated_1 = require("#client/rest/authenticated");
|
|
9
|
+
const guards = tslib_1.__importStar(require("#client/webSocket/guards"));
|
|
10
|
+
const transform_1 = require("#client/webSocket/transform");
|
|
11
|
+
Object.defineProperty(exports, "transformWebsocketShortResponseMessage", { enumerable: true, get: function () { return transform_1.transformWebsocketShortResponseMessage; } });
|
|
12
|
+
const request_1 = require("#types/enums/request");
|
|
13
|
+
tslib_1.__exportStar(require("#client/webSocket/guards"), exports);
|
|
14
|
+
let autoId = 1;
|
|
15
|
+
/**
|
|
16
|
+
* This is the doc comment for file1.ts
|
|
17
|
+
*
|
|
18
|
+
* Specify this is a module comment and rename it to my-module:
|
|
19
|
+
* @module my-module
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
*
|
|
24
|
+
* How often to send `ping` messages to the server (in milliseconds).
|
|
25
|
+
*/
|
|
26
|
+
exports.WEBSOCKET_PING_TIMEOUT_MS = 30_000;
|
|
27
|
+
/**
|
|
28
|
+
* ### WebSocket API client
|
|
29
|
+
*
|
|
30
|
+
* You must provide constructor options that match either:
|
|
31
|
+
*
|
|
32
|
+
* - If you only need {@link KatanaPerpsSubscribeTypePublic public subscriptions}, all options are optional.
|
|
33
|
+
* - If you want to make {@link KatanaPerpsSubscribeTypeAuthenticated authenticated subscriptions}:
|
|
34
|
+
* - You must provide a valid {@link WebSocketClientOptions.auth auth}
|
|
35
|
+
* property in the {@link WebSocketClientOptions constructor options} which includes:
|
|
36
|
+
* - {@link katanaperps.WebSocketClientAuthOptions.apiKey apiKey}
|
|
37
|
+
* - {@link katanaperps.WebSocketClientAuthOptions.apiSecret apiSecret}
|
|
38
|
+
* - {@link katanaperps.WebSocketClientAuthOptions.wallet wallet}
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import {
|
|
43
|
+
* WebSocketClient,
|
|
44
|
+
* SubscriptionNameAuthenticated,
|
|
45
|
+
* SubscriptionNamePublic
|
|
46
|
+
* } from '@katanaperps/katana-perps-sdk';
|
|
47
|
+
*
|
|
48
|
+
* const webSocketClientPublicOnly = new WebSocketClient();
|
|
49
|
+
*
|
|
50
|
+
* // or ... to enable both public and authenticated subscriptions:
|
|
51
|
+
*
|
|
52
|
+
* const client = new WebSocketClient({
|
|
53
|
+
* // Edit the values before for your environment
|
|
54
|
+
* auth: {
|
|
55
|
+
* apiKey: '1f7c4f52-4af7-4e1b-aa94-94fac8d931aa',
|
|
56
|
+
* apiSecret: 'axuh3ywgg854aq7m73oy6gnnpj5ar9a67szuw5lclbz77zqu0j',
|
|
57
|
+
* wallet: '0x...',
|
|
58
|
+
* }
|
|
59
|
+
* // sandbox: true,
|
|
60
|
+
* // logger: console.log
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* client.onConnect(() => {
|
|
64
|
+
* // [see onConnect docs for full example and information]
|
|
65
|
+
* })
|
|
66
|
+
*
|
|
67
|
+
* client.onDisconnect((code, reason) => {
|
|
68
|
+
* // [see onDisconnect docs for full example and information]
|
|
69
|
+
* })
|
|
70
|
+
*
|
|
71
|
+
* client.onError(errorEvent => {
|
|
72
|
+
* // [see onError docs for full example and information]
|
|
73
|
+
* })
|
|
74
|
+
*
|
|
75
|
+
* client.onMessage(message => {
|
|
76
|
+
* console.log('Receiving Message: ', message)
|
|
77
|
+
* switch(message.type) {
|
|
78
|
+
* case MessageEventType.subscriptions:
|
|
79
|
+
* return handleSubscriptions(message.subscriptions);
|
|
80
|
+
* case MessageEventType.error:
|
|
81
|
+
* return handleAPIError(message.data);
|
|
82
|
+
* case MessageEventType.candles:
|
|
83
|
+
* return handleCandles(message.data);
|
|
84
|
+
* default:
|
|
85
|
+
* break;
|
|
86
|
+
* })
|
|
87
|
+
*
|
|
88
|
+
* await client.connect();
|
|
89
|
+
*
|
|
90
|
+
* // subscribe as needed!
|
|
91
|
+
*
|
|
92
|
+
* await client
|
|
93
|
+
* .subscribePublic([
|
|
94
|
+
* {
|
|
95
|
+
* name: SubscriptionNamePublic.tickers,
|
|
96
|
+
* markets: ['ETH-USD']
|
|
97
|
+
* },
|
|
98
|
+
* ])
|
|
99
|
+
* .subscribeAuthenticated([
|
|
100
|
+
* { name: SubscriptionNameAuthenticated.positions },
|
|
101
|
+
* { name: SubscriptionNameAuthenticated.orders },
|
|
102
|
+
* ]);
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* <br />
|
|
106
|
+
*
|
|
107
|
+
* ---
|
|
108
|
+
*
|
|
109
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html)
|
|
110
|
+
*
|
|
111
|
+
* @category API Clients
|
|
112
|
+
* @category KatanaPerps - Get Candles
|
|
113
|
+
* @category KatanaPerps - Get Deposits
|
|
114
|
+
* @category KatanaPerps - Get Funding Payments
|
|
115
|
+
* @category KatanaPerps - Get Liquidations
|
|
116
|
+
* @category KatanaPerps - Get OrderBook
|
|
117
|
+
* @category KatanaPerps - Get Orders
|
|
118
|
+
* @category KatanaPerps - Get Positions
|
|
119
|
+
* @category KatanaPerps - Get Tickers
|
|
120
|
+
* @category KatanaPerps - Get Trades
|
|
121
|
+
* @category KatanaPerps - Get Withdrawals
|
|
122
|
+
*/
|
|
123
|
+
class WebSocketClient {
|
|
124
|
+
/**
|
|
125
|
+
* @internal
|
|
126
|
+
* @private
|
|
127
|
+
*/
|
|
128
|
+
#state = {
|
|
129
|
+
/**
|
|
130
|
+
* Set to true when the reconnect logic should not be run.
|
|
131
|
+
* @private
|
|
132
|
+
*/
|
|
133
|
+
doNotReconnect: false,
|
|
134
|
+
isReconnecting: false,
|
|
135
|
+
/**
|
|
136
|
+
* Used to track the number of reconnect attempts for exponential backoff
|
|
137
|
+
* @private
|
|
138
|
+
*/
|
|
139
|
+
reconnectAttempt: 0,
|
|
140
|
+
/**
|
|
141
|
+
* When the ping timeout is scheduled, it saves its id to this property. Since
|
|
142
|
+
* the type from Node & dom are not compatible, using it may require casting
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* clearTimeout(this.#state.pingTimeoutId as number);
|
|
146
|
+
*
|
|
147
|
+
* @private
|
|
148
|
+
*/
|
|
149
|
+
pingTimeoutId: undefined,
|
|
150
|
+
reconnectTimeoutId: undefined,
|
|
151
|
+
connectHandlers: new Set(),
|
|
152
|
+
disconnectHandlers: new Set(),
|
|
153
|
+
errorHandlers: new Set(),
|
|
154
|
+
messageHandlers: new Set(),
|
|
155
|
+
};
|
|
156
|
+
#websocketCustomAuthTokenFetcher = null;
|
|
157
|
+
#websocketAutoAuthTokenFetcher = null;
|
|
158
|
+
/**
|
|
159
|
+
* If `true`, this client will throw an error if it attempts to subscribe to
|
|
160
|
+
* an {@link KatanaPerpsSubscribeTypeAuthenticated authenticated subscription}.
|
|
161
|
+
*
|
|
162
|
+
* - This is determined by whether or not the {@link katanaperps.WebSocketClientOptions.auth auth}
|
|
163
|
+
* property was provided during construction.
|
|
164
|
+
*
|
|
165
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#isPublicOnly)
|
|
166
|
+
*
|
|
167
|
+
* @category Accessors
|
|
168
|
+
*/
|
|
169
|
+
get isPublicOnly() {
|
|
170
|
+
return (!this.#websocketCustomAuthTokenFetcher &&
|
|
171
|
+
!this.#websocketAutoAuthTokenFetcher);
|
|
172
|
+
}
|
|
173
|
+
#options;
|
|
174
|
+
#terminated = false;
|
|
175
|
+
/**
|
|
176
|
+
* When a client is terminated, it will cease to function and a new WebSocketClient must be created
|
|
177
|
+
* to start a new connection.
|
|
178
|
+
*/
|
|
179
|
+
get terminated() {
|
|
180
|
+
return this.#terminated;
|
|
181
|
+
}
|
|
182
|
+
#ws = null;
|
|
183
|
+
/**
|
|
184
|
+
* You can access the current {@link WebSocket}
|
|
185
|
+
*
|
|
186
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#ws)
|
|
187
|
+
*
|
|
188
|
+
* @category Accessors
|
|
189
|
+
*/
|
|
190
|
+
get ws() {
|
|
191
|
+
return this.#ws;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* A boolean indicating whether the WebSocket client is currently connected to the Katana Perps WebSocket API.
|
|
195
|
+
*
|
|
196
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#isConnected)
|
|
197
|
+
*
|
|
198
|
+
* @category Accessors
|
|
199
|
+
*/
|
|
200
|
+
get isConnected() {
|
|
201
|
+
if (!this.ws) {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
return this.ws.readyState === this.ws.OPEN;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* ### WebSocket API client
|
|
208
|
+
*
|
|
209
|
+
* You must provide constructor options that match either:
|
|
210
|
+
*
|
|
211
|
+
* - If you only need {@link KatanaPerpsSubscribeTypePublic public subscriptions}, all options are optional.
|
|
212
|
+
* - If you want to make {@link KatanaPerpsSubscribeTypeAuthenticated authenticated subscriptions}:
|
|
213
|
+
* - You must provide a valid {@link WebSocketClientOptions.auth auth}
|
|
214
|
+
* property in the {@link WebSocketClientOptions constructor options} which includes:
|
|
215
|
+
* - {@link katanaperps.WebSocketClientAuthOptions.apiKey apiKey}
|
|
216
|
+
* - {@link katanaperps.WebSocketClientAuthOptions.apiSecret apiSecret}
|
|
217
|
+
* - {@link katanaperps.WebSocketClientAuthOptions.wallet wallet}
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* import {
|
|
222
|
+
* WebSocketClient,
|
|
223
|
+
* SubscriptionNameAuthenticated,
|
|
224
|
+
* SubscriptionNamePublic
|
|
225
|
+
* } from '@katanaperps/katana-perps-sdk';
|
|
226
|
+
*
|
|
227
|
+
* const webSocketClientPublicOnly = new WebSocketClient();
|
|
228
|
+
*
|
|
229
|
+
* // or ... to enable both public and authenticated subscriptions:
|
|
230
|
+
*
|
|
231
|
+
* const client = new WebSocketClient({
|
|
232
|
+
* // Edit the values before for your environment
|
|
233
|
+
* auth: {
|
|
234
|
+
* apiKey: '1f7c4f52-4af7-4e1b-aa94-94fac8d931aa',
|
|
235
|
+
* apiSecret: 'axuh3ywgg854aq7m73oy6gnnpj5ar9a67szuw5lclbz77zqu0j',
|
|
236
|
+
* wallet: '0x...',
|
|
237
|
+
* }
|
|
238
|
+
* // sandbox: true,
|
|
239
|
+
* // logger: console.log
|
|
240
|
+
* });
|
|
241
|
+
*
|
|
242
|
+
* client.onConnect(() => {
|
|
243
|
+
* // [see onConnect docs for full example and information]
|
|
244
|
+
* })
|
|
245
|
+
*
|
|
246
|
+
* client.onDisconnect((code, reason) => {
|
|
247
|
+
* // [see onDisconnect docs for full example and information]
|
|
248
|
+
* })
|
|
249
|
+
*
|
|
250
|
+
* client.onError(errorEvent => {
|
|
251
|
+
* // [see onError docs for full example and information]
|
|
252
|
+
* })
|
|
253
|
+
*
|
|
254
|
+
* client.onMessage(message => {
|
|
255
|
+
* console.log('Receiving Message: ', message)
|
|
256
|
+
* switch(message.type) {
|
|
257
|
+
* case MessageEventType.subscriptions:
|
|
258
|
+
* return handleSubscriptions(message.subscriptions);
|
|
259
|
+
* case MessageEventType.error:
|
|
260
|
+
* return handleAPIError(message.data);
|
|
261
|
+
* case MessageEventType.candles:
|
|
262
|
+
* return handleCandles(message.data);
|
|
263
|
+
* default:
|
|
264
|
+
* break;
|
|
265
|
+
* })
|
|
266
|
+
*
|
|
267
|
+
* await client.connect();
|
|
268
|
+
*
|
|
269
|
+
* // subscribe as needed!
|
|
270
|
+
*
|
|
271
|
+
* await client
|
|
272
|
+
* .subscribePublic([
|
|
273
|
+
* {
|
|
274
|
+
* name: SubscriptionNamePublic.tickers,
|
|
275
|
+
* markets: ['ETH-USD']
|
|
276
|
+
* },
|
|
277
|
+
* ])
|
|
278
|
+
* .subscribeAuthenticated([
|
|
279
|
+
* { name: SubscriptionNameAuthenticated.positions },
|
|
280
|
+
* { name: SubscriptionNameAuthenticated.orders },
|
|
281
|
+
* ]);
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* <br />
|
|
285
|
+
*
|
|
286
|
+
* ---
|
|
287
|
+
*
|
|
288
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html)
|
|
289
|
+
*/
|
|
290
|
+
constructor(options = {}) {
|
|
291
|
+
const sandbox = options.sandbox ?? false;
|
|
292
|
+
const baseWebSocketURL = (0, _utils_1.deriveBaseURL)({
|
|
293
|
+
sandbox,
|
|
294
|
+
api: 'websocket',
|
|
295
|
+
baseRestApiURL: options.baseRestApiURL,
|
|
296
|
+
baseWebSocketURL: options.baseWebSocketURL,
|
|
297
|
+
});
|
|
298
|
+
const baseRestApiURL = (0, _utils_1.deriveBaseURL)({
|
|
299
|
+
sandbox,
|
|
300
|
+
api: 'rest',
|
|
301
|
+
baseWebSocketURL: options.baseWebSocketURL,
|
|
302
|
+
baseRestApiURL: options.baseRestApiURL,
|
|
303
|
+
});
|
|
304
|
+
const initializedOptions = {
|
|
305
|
+
sandbox,
|
|
306
|
+
baseWebSocketURL,
|
|
307
|
+
baseRestApiURL,
|
|
308
|
+
shouldReconnectAutomatically: options.shouldReconnectAutomatically ?? true,
|
|
309
|
+
// eslint-disable-next-line no-plusplus
|
|
310
|
+
clientId: options.clientId ?? `${autoId++}`,
|
|
311
|
+
};
|
|
312
|
+
this.#options = {
|
|
313
|
+
...options,
|
|
314
|
+
...initializedOptions,
|
|
315
|
+
};
|
|
316
|
+
// Options must exactly match one of the subsets of WebSocketClientOptionsBase
|
|
317
|
+
if (this.#options.websocketAuthTokenFetch) {
|
|
318
|
+
// WebSocketClientOptionsWithFetch
|
|
319
|
+
this.#websocketCustomAuthTokenFetcher =
|
|
320
|
+
this.#options.websocketAuthTokenFetch.bind(this);
|
|
321
|
+
this.#websocketAutoAuthTokenFetcher = null;
|
|
322
|
+
}
|
|
323
|
+
else if (guards.isWebSocketOptionsAutoFetch(this.#options)) {
|
|
324
|
+
// WebSocketClientOptionsWithAPIKey
|
|
325
|
+
this.#websocketCustomAuthTokenFetcher = null;
|
|
326
|
+
this.#websocketAutoAuthTokenFetcher = createDefaultWebSocketTokenFetcher(this.#options);
|
|
327
|
+
}
|
|
328
|
+
else if (guards.isWebSocketOptionsPublicOnly(this.#options)) {
|
|
329
|
+
// WebSocketClientOptionsPublicOnly
|
|
330
|
+
this.#websocketCustomAuthTokenFetcher = null;
|
|
331
|
+
this.#websocketAutoAuthTokenFetcher = null;
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
throw new Error('Invalid configuration, must specify options for either public only, auto wsToken fetch, or custom wsToken fetch');
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
throwIfTerminated() {
|
|
338
|
+
if (this.terminated) {
|
|
339
|
+
throw new Error(`WebSocketClient ${this.#options.clientId} is terminated and cannot be used`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Connect to the [Katana Perps WebSocket API](https://api-docs-v1-perps.katana.network/#websocket-api-interaction)
|
|
344
|
+
*
|
|
345
|
+
* ---
|
|
346
|
+
* @param awaitConnected
|
|
347
|
+
* - If `true`, the promise will not resolve until the connection is established
|
|
348
|
+
* @returns
|
|
349
|
+
* - `this` to allow chaining with other methods or requests.
|
|
350
|
+
* ---
|
|
351
|
+
*
|
|
352
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#connect)
|
|
353
|
+
*
|
|
354
|
+
* @category Connection Management
|
|
355
|
+
*/
|
|
356
|
+
async connect(awaitConnected = true) {
|
|
357
|
+
this.throwIfTerminated();
|
|
358
|
+
if (this.isConnected) {
|
|
359
|
+
return this;
|
|
360
|
+
}
|
|
361
|
+
this.log('info', `Connecting to WebSocket, awaiting connection? ${awaitConnected}`);
|
|
362
|
+
this.#state.doNotReconnect = false;
|
|
363
|
+
// connect and await connection to succeed
|
|
364
|
+
const connectionProm = this.createWebSocketIfNeeded(true).then(() => {
|
|
365
|
+
this.#state.connectHandlers.forEach((listener) => listener());
|
|
366
|
+
});
|
|
367
|
+
if (awaitConnected) {
|
|
368
|
+
await connectionProm;
|
|
369
|
+
}
|
|
370
|
+
return this;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Disconnect from the WebSocket if connected.
|
|
374
|
+
*
|
|
375
|
+
* - If `terminate` is `true`, the WebSocket will be disconnected immediately and this client will
|
|
376
|
+
* cease to work or connect.
|
|
377
|
+
* - All listeners will be cleared and all methods will throw errors once terminated.
|
|
378
|
+
*
|
|
379
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#disconnect)
|
|
380
|
+
*
|
|
381
|
+
* @category Connection Management
|
|
382
|
+
*/
|
|
383
|
+
disconnect(terminate) {
|
|
384
|
+
if (!terminate) {
|
|
385
|
+
this.throwIfTerminated();
|
|
386
|
+
}
|
|
387
|
+
else if (this.terminated) {
|
|
388
|
+
return this;
|
|
389
|
+
}
|
|
390
|
+
this.#state.doNotReconnect = true;
|
|
391
|
+
this.log('info', 'Disconnecting from WebSocket');
|
|
392
|
+
this.stopPinging();
|
|
393
|
+
this.cancelReconnect();
|
|
394
|
+
if (!this.#ws) {
|
|
395
|
+
if (terminate) {
|
|
396
|
+
this.#terminated = true;
|
|
397
|
+
}
|
|
398
|
+
return this; // Already disconnected
|
|
399
|
+
}
|
|
400
|
+
this.#state.doNotReconnect = true;
|
|
401
|
+
if (terminate) {
|
|
402
|
+
this.#options.shouldReconnectAutomatically = false;
|
|
403
|
+
this.#terminated = true;
|
|
404
|
+
// handlers are not called when terminating
|
|
405
|
+
this.#state.connectHandlers.clear();
|
|
406
|
+
this.#state.disconnectHandlers.clear();
|
|
407
|
+
this.#state.errorHandlers.clear();
|
|
408
|
+
this.#state.disconnectHandlers.clear();
|
|
409
|
+
this.#state.messageHandlers.clear();
|
|
410
|
+
if (typeof this.#ws.terminate === 'function') {
|
|
411
|
+
this.#ws.terminate();
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
this.#ws.close();
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
this.#ws.close();
|
|
419
|
+
}
|
|
420
|
+
this.#ws = null;
|
|
421
|
+
return this;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Subscribe a handler to all WebSocket disconnect events.
|
|
425
|
+
*
|
|
426
|
+
* - Calling multiple times will add multiple subscribers to the given event
|
|
427
|
+
*
|
|
428
|
+
* ---
|
|
429
|
+
* @param handler
|
|
430
|
+
* - A handler function matching {@link WebSocketHandlerConnect} that will receive events.
|
|
431
|
+
* @param replaceAll
|
|
432
|
+
* - Replaces all current handlers with the provided handler.
|
|
433
|
+
* @returns
|
|
434
|
+
* - `this` to allow chaining with other methods or requests.
|
|
435
|
+
* ---
|
|
436
|
+
*
|
|
437
|
+
* @example
|
|
438
|
+
* ```typescript
|
|
439
|
+
* websocketClient.onConnect(() => {
|
|
440
|
+
* console.warn('WebSocket Connection Connects')
|
|
441
|
+
* })
|
|
442
|
+
* ```
|
|
443
|
+
*
|
|
444
|
+
* <br />
|
|
445
|
+
*
|
|
446
|
+
* ---
|
|
447
|
+
*
|
|
448
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#onConnect)
|
|
449
|
+
*
|
|
450
|
+
* @category Event Handling
|
|
451
|
+
*/
|
|
452
|
+
onConnect(handler, replaceAll) {
|
|
453
|
+
this.throwIfTerminated();
|
|
454
|
+
if (replaceAll) {
|
|
455
|
+
this.#state.connectHandlers.clear();
|
|
456
|
+
}
|
|
457
|
+
this.#state.connectHandlers.add(handler);
|
|
458
|
+
return this;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Subscribe a handler to all WebSocket disconnect events.
|
|
462
|
+
*
|
|
463
|
+
* ---
|
|
464
|
+
* @param handler
|
|
465
|
+
* - A handler function matching {@link WebSocketHandlerDisconnect} that will receive events.
|
|
466
|
+
* @param replaceAll
|
|
467
|
+
* - Replaces all current handlers with the provided handler.
|
|
468
|
+
* @returns
|
|
469
|
+
* - `this` to allow chaining with other methods or requests.
|
|
470
|
+
* ---
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```typescript
|
|
474
|
+
* websocketClient.onDisconnect((code, reason) => {
|
|
475
|
+
* console.warn('WebSocket Connection Disconnects: ', code, reason)
|
|
476
|
+
* })
|
|
477
|
+
* ```
|
|
478
|
+
*
|
|
479
|
+
* <br />
|
|
480
|
+
*
|
|
481
|
+
* ---
|
|
482
|
+
*
|
|
483
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#onDisconnect)
|
|
484
|
+
*
|
|
485
|
+
* @category Event Handling
|
|
486
|
+
*/
|
|
487
|
+
onDisconnect(handler, replaceAll) {
|
|
488
|
+
this.throwIfTerminated();
|
|
489
|
+
if (replaceAll) {
|
|
490
|
+
this.#state.disconnectHandlers.clear();
|
|
491
|
+
}
|
|
492
|
+
this.#state.disconnectHandlers.add(handler);
|
|
493
|
+
return this;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Subscribe a handler to all WebSocket connection errors that may occur during
|
|
497
|
+
* your requests.
|
|
498
|
+
*
|
|
499
|
+
* - **Note:** These errors will be coming from the `ws` library itself and provide
|
|
500
|
+
* its {@link ErrorEvent} errors.
|
|
501
|
+
* - Errors coming from the Katana Perps WebSocket client will be present as a {@link katanaperps.KatanaPerpsErrorEvent KatanaPerpsErrorEvent}
|
|
502
|
+
* message to the {@link onMessage} handler.
|
|
503
|
+
*
|
|
504
|
+
* ---
|
|
505
|
+
* @param handler
|
|
506
|
+
* - A handler function matching {@link WebSocketHandlerError} that will receive events.
|
|
507
|
+
* @param replaceAll
|
|
508
|
+
* - Replaces all current handlers with the provided handler.
|
|
509
|
+
* @returns
|
|
510
|
+
* - `this` to allow chaining with other methods or requests.
|
|
511
|
+
* ---
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```typescript
|
|
515
|
+
* websocketClient.onError(errorEvent => {
|
|
516
|
+
* console.error('Connection Error on WebSocket: ', errorEvent.error)
|
|
517
|
+
* })
|
|
518
|
+
* ```
|
|
519
|
+
*
|
|
520
|
+
* <br />
|
|
521
|
+
*
|
|
522
|
+
* ---
|
|
523
|
+
*
|
|
524
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#onError)
|
|
525
|
+
*
|
|
526
|
+
* @category Event Handling
|
|
527
|
+
*/
|
|
528
|
+
onError(handler, replaceAll) {
|
|
529
|
+
this.throwIfTerminated();
|
|
530
|
+
if (replaceAll) {
|
|
531
|
+
this.#state.errorHandlers.clear();
|
|
532
|
+
}
|
|
533
|
+
this.#state.errorHandlers.add(handler);
|
|
534
|
+
return this;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Subscribe a handler to all subscription responses.
|
|
538
|
+
*
|
|
539
|
+
* - Your handler will receive updates when available matching the {@link KatanaPerpsMessageEvent}
|
|
540
|
+
* interface.
|
|
541
|
+
* - Use the {@link katanaperps.MessageEventType MessageEventType} enum to get an enum
|
|
542
|
+
* defining all possible `type` values that can be received on the WebSocket message (see example).
|
|
543
|
+
*
|
|
544
|
+
* ---
|
|
545
|
+
* @param handler
|
|
546
|
+
* - A handler function matching {@link WebSocketHandlerMessage} that will receive events.
|
|
547
|
+
* @param replaceAll
|
|
548
|
+
* - Replaces all current handlers with the provided handler.
|
|
549
|
+
* @returns
|
|
550
|
+
* - `this` to allow chaining with other methods or requests.
|
|
551
|
+
* ---
|
|
552
|
+
*
|
|
553
|
+
* @example
|
|
554
|
+
* ```typescript
|
|
555
|
+
* import { MessageEventType } from '@katanaperps/katana-perps-sdk';
|
|
556
|
+
*
|
|
557
|
+
* // ... client setup
|
|
558
|
+
*
|
|
559
|
+
* websocketClient.onMessage(message => {
|
|
560
|
+
* console.log('Receiving Message: ', message)
|
|
561
|
+
* switch(message.type) {
|
|
562
|
+
* case MessageEventType.subscriptions:
|
|
563
|
+
* return handleSubscriptions(message.subscriptions);
|
|
564
|
+
* case MessageEventType.error:
|
|
565
|
+
* return handleAPIError(message.data);
|
|
566
|
+
* case MessageEventType.candles:
|
|
567
|
+
* return handleCandles(message.data);
|
|
568
|
+
* default:
|
|
569
|
+
* break;
|
|
570
|
+
* }
|
|
571
|
+
* })
|
|
572
|
+
* ```
|
|
573
|
+
*
|
|
574
|
+
* <br />
|
|
575
|
+
*
|
|
576
|
+
* ---
|
|
577
|
+
*
|
|
578
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#onMessage)
|
|
579
|
+
* @see enum {@link katanaperps.MessageEventType MessageEventType}
|
|
580
|
+
* @see type {@link KatanaPerpsMessageEvent}
|
|
581
|
+
*
|
|
582
|
+
* @category Event Handling
|
|
583
|
+
*/
|
|
584
|
+
onMessage(handler, replaceAll) {
|
|
585
|
+
this.throwIfTerminated();
|
|
586
|
+
if (replaceAll) {
|
|
587
|
+
this.#state.messageHandlers.clear();
|
|
588
|
+
}
|
|
589
|
+
this.#state.messageHandlers.add(handler);
|
|
590
|
+
return this;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Creates new {@link KatanaPerpsSubscribeTypeAuthenticated authenticated subscriptions} based on the provided parameters.
|
|
594
|
+
*
|
|
595
|
+
* - Use the {@link katanaperps.SubscriptionNameAuthenticated SubscriptionNameAuthenticated} enum for IDE
|
|
596
|
+
* inline documentation and auto completion (see example)
|
|
597
|
+
*
|
|
598
|
+
* ---
|
|
599
|
+
*
|
|
600
|
+
* **Subscription Update Events:**
|
|
601
|
+
*
|
|
602
|
+
* - Receives a {@link katanaperps.KatanaPerpsSubscriptionsListEvent KatanaPerpsSubscriptionsListEvent} WebSocket response via
|
|
603
|
+
* the {@link katanaperps.WebSocketClient.onMessage WebSocketClient.onMessage} handler listing all active
|
|
604
|
+
* subscriptions.
|
|
605
|
+
* - Begins receiving {@link katanaperps.KatanaPerpsSubscriptionEvent KatanaPerpsSubscriptionEvent}'s for all subscribed
|
|
606
|
+
* subscriptions via the {@link katanaperps.WebSocketClient.onMessage WebSocketClient.onMessage} handler.
|
|
607
|
+
*
|
|
608
|
+
* ---
|
|
609
|
+
* @param subscriptions
|
|
610
|
+
* - An array of {@link KatanaPerpsSubscribeTypeAuthenticated} subscription objects.
|
|
611
|
+
* @param cid
|
|
612
|
+
* - Optionally provide a `cid` property which will be returned in the response
|
|
613
|
+
* so that you can correlate the response to the request.
|
|
614
|
+
* @returns
|
|
615
|
+
* - `this` to allow chaining with other methods or requests.
|
|
616
|
+
* ---
|
|
617
|
+
*
|
|
618
|
+
* @throws
|
|
619
|
+
* > This method will **throw an error** if you did not provide the {@link katanaperps.WebSocketClientOptionsWithAPIKey.auth auth} option
|
|
620
|
+
* to the constructor in order to handle authenticated subscriptions.
|
|
621
|
+
*
|
|
622
|
+
* ---
|
|
623
|
+
*
|
|
624
|
+
* @example
|
|
625
|
+
* ```typescript
|
|
626
|
+
* import {
|
|
627
|
+
* WebSocketClient,
|
|
628
|
+
* SubscriptionNameAuthenticated,
|
|
629
|
+
* } from '@katanaperps/katana-perps-sdk';
|
|
630
|
+
*
|
|
631
|
+
* const client = new WebSocketClient({
|
|
632
|
+
* auth: {
|
|
633
|
+
* apiKey: '...',
|
|
634
|
+
* apiSecret: '...',
|
|
635
|
+
* wallet: '0x...'
|
|
636
|
+
* }
|
|
637
|
+
* })
|
|
638
|
+
*
|
|
639
|
+
* client.onMessage(message => {
|
|
640
|
+
* console.log('Received WebSocket Message: ', message)
|
|
641
|
+
* })
|
|
642
|
+
*
|
|
643
|
+
* await client.subscribeAuthenticated([
|
|
644
|
+
* { name: SubscriptionNameAuthenticated.positions },
|
|
645
|
+
* { name: SubscriptionNameAuthenticated.orders },
|
|
646
|
+
* ])
|
|
647
|
+
* ```
|
|
648
|
+
*
|
|
649
|
+
* <br />
|
|
650
|
+
*
|
|
651
|
+
* ---
|
|
652
|
+
*
|
|
653
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#subscribeAuthenticated)
|
|
654
|
+
* @see related {@link subscribePublic client.subscribePublic}
|
|
655
|
+
*
|
|
656
|
+
* @category Subscription Management
|
|
657
|
+
*/
|
|
658
|
+
subscribeAuthenticated(
|
|
659
|
+
/**
|
|
660
|
+
* An array of {@link KatanaPerpsSubscribeTypeAuthenticated} subscription
|
|
661
|
+
* objects.
|
|
662
|
+
*/
|
|
663
|
+
subscriptions,
|
|
664
|
+
/**
|
|
665
|
+
* Optionally provide a `cid` property which will be returned in the response
|
|
666
|
+
* so that you can correlate the response to the request.
|
|
667
|
+
*/
|
|
668
|
+
cid) {
|
|
669
|
+
this.throwIfTerminated();
|
|
670
|
+
if (this.isPublicOnly) {
|
|
671
|
+
throw new Error('[subscribeAuthenticated] Cannot subscribe to authenticated subscriptions ' +
|
|
672
|
+
'without providing the apiKey, apiSecret, and wallet options during client construction');
|
|
673
|
+
}
|
|
674
|
+
this.subscribe(subscriptions, undefined, cid);
|
|
675
|
+
return this;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* This method allows subscribing to Katana Perps's {@link KatanaPerpsSubscribeTypePublic public subscriptions}.
|
|
679
|
+
*
|
|
680
|
+
* - Use the {@link katanaperps.SubscriptionNamePublic SubscriptionNamePublic} enum for IDE
|
|
681
|
+
* inline documentation and auto completion (see example)
|
|
682
|
+
*
|
|
683
|
+
* ---
|
|
684
|
+
*
|
|
685
|
+
* **Subscription Update Events:**
|
|
686
|
+
*
|
|
687
|
+
* - Receives a {@link katanaperps.KatanaPerpsSubscriptionsListEvent KatanaPerpsSubscriptionsListEvent} WebSocket response via
|
|
688
|
+
* the {@link katanaperps.WebSocketClient.onMessage WebSocketClient.onMessage} handler listing all active
|
|
689
|
+
* subscriptions.
|
|
690
|
+
* - Begins receiving {@link katanaperps.KatanaPerpsSubscriptionEvent KatanaPerpsSubscriptionEvent}'s for all subscribed
|
|
691
|
+
* subscriptions via the {@link katanaperps.WebSocketClient.onMessage WebSocketClient.onMessage} handler.
|
|
692
|
+
*
|
|
693
|
+
* ---
|
|
694
|
+
* @param subscriptions
|
|
695
|
+
* - An array of {@link KatanaPerpsSubscribeTypePublic} subscription objects.
|
|
696
|
+
* @param markets
|
|
697
|
+
* - Optionally provide top-level markets.
|
|
698
|
+
* - Any {@link subscriptions} that **require but do not define** their own `markets`
|
|
699
|
+
* array will inherit this set of markets.
|
|
700
|
+
* - If a subscription in your {@link subscriptions} array defines its own
|
|
701
|
+
* {@link KatanaPerpsSubscribeType.markets markets} array, the top-level markets
|
|
702
|
+
* **will not be inherited**
|
|
703
|
+
* @param cid
|
|
704
|
+
* - Optionally provide a `cid` property which will be returned in the response
|
|
705
|
+
* so that you can correlate the response to the request.
|
|
706
|
+
* @returns
|
|
707
|
+
* - `this` to allow chaining with other methods or requests.
|
|
708
|
+
* ---
|
|
709
|
+
*
|
|
710
|
+
* @example
|
|
711
|
+
* ```typescript
|
|
712
|
+
* import {
|
|
713
|
+
* WebSocketClient,
|
|
714
|
+
* SubscriptionNamePublic,
|
|
715
|
+
* CandleInterval
|
|
716
|
+
* } from '@katanaperps/katana-perps-sdk';
|
|
717
|
+
*
|
|
718
|
+
* const client = new WebSocketClient();
|
|
719
|
+
*
|
|
720
|
+
* await client.subscribePublic([
|
|
721
|
+
* // will inherit markets from the markets array
|
|
722
|
+
* { name: SubscriptionNamePublic.tickers },
|
|
723
|
+
* // overrides the top level markets array with its own
|
|
724
|
+
* {
|
|
725
|
+
* name: SubscriptionNamePublic.candles,
|
|
726
|
+
* interval: CandleInterval.ONE_MINUTE,
|
|
727
|
+
* // replaces the top-level markets when provided
|
|
728
|
+
* markets: ['ETH-USD', 'BTC-USD'],
|
|
729
|
+
* },
|
|
730
|
+
* ], ['ETH-USD'])
|
|
731
|
+
* ```
|
|
732
|
+
*
|
|
733
|
+
* <br />
|
|
734
|
+
*
|
|
735
|
+
* ---
|
|
736
|
+
*
|
|
737
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#subscribePublic)
|
|
738
|
+
* @see related {@link subscribeAuthenticated client.subscribeAuthenticated}
|
|
739
|
+
*
|
|
740
|
+
* @category Subscription Management
|
|
741
|
+
*/
|
|
742
|
+
subscribePublic(subscriptions, markets, cid) {
|
|
743
|
+
this.throwIfTerminated();
|
|
744
|
+
this.subscribe(subscriptions, markets, cid);
|
|
745
|
+
return this;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Unsubscribes from a subscription or subscriptions
|
|
749
|
+
*
|
|
750
|
+
* ---
|
|
751
|
+
*
|
|
752
|
+
* **Subscription Update Events:**
|
|
753
|
+
*
|
|
754
|
+
* - Receives a {@link katanaperps.KatanaPerpsSubscriptionsListEvent KatanaPerpsSubscriptionsListEvent} WebSocket response via
|
|
755
|
+
* the {@link katanaperps.WebSocketClient.onMessage WebSocketClient.onMessage} handler listing all active
|
|
756
|
+
* subscriptions.
|
|
757
|
+
*
|
|
758
|
+
* ---
|
|
759
|
+
*
|
|
760
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#unsubscribe)
|
|
761
|
+
* @see related {@link WebSocketRequestUnsubscribeSubscription}
|
|
762
|
+
*
|
|
763
|
+
* @category Subscription Management
|
|
764
|
+
*/
|
|
765
|
+
unsubscribe(subscriptions, markets, cid) {
|
|
766
|
+
this.throwIfTerminated();
|
|
767
|
+
return this.sendMessage({
|
|
768
|
+
cid,
|
|
769
|
+
method: request_1.WebSocketRequestMethod.unsubscribe,
|
|
770
|
+
markets,
|
|
771
|
+
subscriptions,
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* List all active subscriptions
|
|
776
|
+
*
|
|
777
|
+
* ---
|
|
778
|
+
*
|
|
779
|
+
* **Subscription Update Events:**
|
|
780
|
+
*
|
|
781
|
+
* - Receives a {@link katanaperps.KatanaPerpsSubscriptionsListEvent KatanaPerpsSubscriptionsListEvent} WebSocket response via
|
|
782
|
+
* the {@link katanaperps.WebSocketClient.onMessage WebSocketClient.onMessage} handler listing all active
|
|
783
|
+
* subscriptions.
|
|
784
|
+
*
|
|
785
|
+
* ---
|
|
786
|
+
* @param cid
|
|
787
|
+
* - Optionally provide a `cid` property which will be returned in the response
|
|
788
|
+
* so that you can correlate the response to the request.
|
|
789
|
+
* @returns
|
|
790
|
+
* - `this` to allow chaining with other methods or requests.
|
|
791
|
+
* ---
|
|
792
|
+
*
|
|
793
|
+
* @see typedoc [Reference Documentation](https://sdk-js-docs-v1-perps.katana.network/classes/WebSocketClient.html#listSubscriptions)
|
|
794
|
+
*
|
|
795
|
+
* @category Subscription Management
|
|
796
|
+
*/
|
|
797
|
+
listSubscriptions(cid) {
|
|
798
|
+
this.throwIfTerminated();
|
|
799
|
+
return this.sendMessage({
|
|
800
|
+
method: request_1.WebSocketRequestMethod.subscriptions,
|
|
801
|
+
cid,
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
/* Private / Protected */
|
|
805
|
+
/**
|
|
806
|
+
* @internal
|
|
807
|
+
*/
|
|
808
|
+
subscribe(subscriptions, markets, cid) {
|
|
809
|
+
this.throwIfTerminated();
|
|
810
|
+
this.subscribeRequest(subscriptions, markets, cid).catch((error) => {
|
|
811
|
+
this.handleWebSocketError(error);
|
|
812
|
+
});
|
|
813
|
+
return this;
|
|
814
|
+
}
|
|
815
|
+
async subscribeRequest(subscriptions, markets, cid) {
|
|
816
|
+
const wallet = this.#options.auth?.wallet ?? this.#options.wallet;
|
|
817
|
+
// One of the two must be provided for authenticated subscriptions
|
|
818
|
+
const websocketAuthTokenFetch = this.#websocketCustomAuthTokenFetcher ??
|
|
819
|
+
this.#websocketAutoAuthTokenFetcher;
|
|
820
|
+
const walletAuthAvailable = !!websocketAuthTokenFetch && !!wallet;
|
|
821
|
+
const authSubscriptions = subscriptions.filter((subscription) => guards.isWebSocketAuthenticatedSubscription(subscription, walletAuthAvailable));
|
|
822
|
+
// Public subscriptions can be subscribed all at once
|
|
823
|
+
if (authSubscriptions.length === 0) {
|
|
824
|
+
return this.sendMessage({
|
|
825
|
+
cid,
|
|
826
|
+
method: request_1.WebSocketRequestMethod.subscribe,
|
|
827
|
+
markets,
|
|
828
|
+
subscriptions,
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
// For authenticated, we do require token manager
|
|
832
|
+
if (!websocketAuthTokenFetch) {
|
|
833
|
+
throw new Error('WebSocket: `websocketAuthTokenFetch` is required for authenticated subscriptions');
|
|
834
|
+
}
|
|
835
|
+
if (!wallet) {
|
|
836
|
+
throw new Error('WebSocket: Missing `wallet` for authenticated subscriptions');
|
|
837
|
+
}
|
|
838
|
+
// For single wallet, send all subscriptions at once (also unauthenticated)
|
|
839
|
+
return this.sendMessage({
|
|
840
|
+
cid,
|
|
841
|
+
method: request_1.WebSocketRequestMethod.subscribe,
|
|
842
|
+
markets,
|
|
843
|
+
subscriptions,
|
|
844
|
+
token: await websocketAuthTokenFetch.call(this, this.#options),
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
async createWebSocketIfNeeded(awaitConnect = false) {
|
|
848
|
+
try {
|
|
849
|
+
this.throwIfTerminated();
|
|
850
|
+
this.#state.doNotReconnect = false;
|
|
851
|
+
if (this.#ws) {
|
|
852
|
+
return this.#ws;
|
|
853
|
+
}
|
|
854
|
+
const ws = new isomorphic_ws_1.default(this.#options.baseWebSocketURL, _utils_1.isNode ?
|
|
855
|
+
{
|
|
856
|
+
headers: { 'User-Agent': 'katana-perps-sdk-js' },
|
|
857
|
+
}
|
|
858
|
+
: undefined);
|
|
859
|
+
this.#ws = ws;
|
|
860
|
+
this.#ws.addEventListener('message', this.handleWebSocketMessage.bind(this));
|
|
861
|
+
this.#ws.addEventListener('close', this.handleWebSocketClose.bind(this));
|
|
862
|
+
this.#ws.addEventListener('error', this.handleWebSocketError.bind(this));
|
|
863
|
+
this.#ws.addEventListener('open', this.handleWebSocketConnect.bind(this));
|
|
864
|
+
if (awaitConnect) {
|
|
865
|
+
await this.resolveWhenConnected();
|
|
866
|
+
}
|
|
867
|
+
return this.#ws;
|
|
868
|
+
}
|
|
869
|
+
catch (err) {
|
|
870
|
+
if (this.terminated) {
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
let handlerErr = err;
|
|
874
|
+
if (this.#options.shouldReconnectAutomatically &&
|
|
875
|
+
!this.#state.doNotReconnect) {
|
|
876
|
+
this.reconnect();
|
|
877
|
+
handlerErr = new Error(`Failed to connect: "${err.message}" - a reconnect attempt will be scheduled automatically`);
|
|
878
|
+
}
|
|
879
|
+
if (this.#state.errorHandlers.size) {
|
|
880
|
+
this.handleWebSocketError({
|
|
881
|
+
error: handlerErr,
|
|
882
|
+
type: 'connection_error',
|
|
883
|
+
message: handlerErr.message,
|
|
884
|
+
target: this.#ws,
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
else {
|
|
888
|
+
throw handlerErr;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
return this.#ws;
|
|
892
|
+
}
|
|
893
|
+
/**
|
|
894
|
+
* Waits until the WebSocket is connected before returning
|
|
895
|
+
*
|
|
896
|
+
* @internal
|
|
897
|
+
*/
|
|
898
|
+
async resolveWhenConnected(timeout = 5_000) {
|
|
899
|
+
const { ws } = this;
|
|
900
|
+
if (!ws) {
|
|
901
|
+
throw new Error('Can not wait for WebSocket to connect, no WebSocket was found');
|
|
902
|
+
}
|
|
903
|
+
if (ws.readyState === ws.OPEN) {
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
906
|
+
if (ws.readyState !== ws.CONNECTING) {
|
|
907
|
+
throw new Error('Can not wait for WebSocket to connect that is not open or connecting');
|
|
908
|
+
}
|
|
909
|
+
await new Promise((resolve, reject) => {
|
|
910
|
+
const timeoutId = setTimeout(() => {
|
|
911
|
+
if (this.terminated) {
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
if (this.#ws === ws) {
|
|
915
|
+
this.disconnect();
|
|
916
|
+
reject(new Error('timed out while waiting for WebSocket to connect'));
|
|
917
|
+
}
|
|
918
|
+
}, timeout);
|
|
919
|
+
const listener = () => {
|
|
920
|
+
clearTimeout(timeoutId);
|
|
921
|
+
ws.removeEventListener('open', listener);
|
|
922
|
+
resolve(true);
|
|
923
|
+
};
|
|
924
|
+
ws.addEventListener('open', listener);
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
handleWebSocketConnect() {
|
|
928
|
+
this.resetReconnectionState();
|
|
929
|
+
this.startPinging();
|
|
930
|
+
}
|
|
931
|
+
// we need to ping from the client side to detect client-side socket closures which would otherwise
|
|
932
|
+
// not generate any close notifications. This also aids against idle timeouts being hit.
|
|
933
|
+
// we can only send a ping from node-based environments, on browsers we need to instead use
|
|
934
|
+
// a standard message to accomplish this.
|
|
935
|
+
//
|
|
936
|
+
// the server will always only reply to custom ping messages with native pong responses so the
|
|
937
|
+
// client will not recieve any events in the browser when they occur.
|
|
938
|
+
startPinging() {
|
|
939
|
+
this.stopPinging();
|
|
940
|
+
if (!this.isConnected || this.terminated) {
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
try {
|
|
944
|
+
const { ws } = this;
|
|
945
|
+
if (!ws) {
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
if (typeof ws.ping === 'function') {
|
|
949
|
+
ws.ping(JSON.stringify({ method: request_1.WebSocketRequestMethod.ping }));
|
|
950
|
+
}
|
|
951
|
+
else {
|
|
952
|
+
ws.send(JSON.stringify({ method: request_1.WebSocketRequestMethod.ping }));
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
finally {
|
|
956
|
+
if (this.isConnected) {
|
|
957
|
+
this.#state.pingTimeoutId = setTimeout(this.startPinging.bind(this), exports.WEBSOCKET_PING_TIMEOUT_MS);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
stopPinging() {
|
|
962
|
+
clearTimeout(this.#state.pingTimeoutId);
|
|
963
|
+
this.#state.pingTimeoutId = undefined;
|
|
964
|
+
}
|
|
965
|
+
handleWebSocketClose(event) {
|
|
966
|
+
this.stopPinging();
|
|
967
|
+
this.#ws = null;
|
|
968
|
+
this.#state.disconnectHandlers.forEach((listener) => listener(event.code, event.reason));
|
|
969
|
+
if (!this.terminated &&
|
|
970
|
+
this.#options.shouldReconnectAutomatically &&
|
|
971
|
+
!this.#state.doNotReconnect) {
|
|
972
|
+
this.reconnect();
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
handleWebSocketError(event) {
|
|
976
|
+
this.#state.errorHandlers.forEach((listener) => listener(event));
|
|
977
|
+
}
|
|
978
|
+
handleWebSocketMessage(event) {
|
|
979
|
+
if (this.terminated) {
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
if (!event || !event.data) {
|
|
983
|
+
throw new Error('Malformed response data'); // Shouldn't happen
|
|
984
|
+
}
|
|
985
|
+
const message = (0, transform_1.transformWebsocketShortResponseMessage)(JSON.parse(String(event.data)));
|
|
986
|
+
this.#state.messageHandlers.forEach((listener) => listener(message));
|
|
987
|
+
}
|
|
988
|
+
cancelReconnect() {
|
|
989
|
+
clearTimeout(this.#state.reconnectTimeoutId);
|
|
990
|
+
this.#state.isReconnecting = false;
|
|
991
|
+
this.#state.reconnectTimeoutId = undefined;
|
|
992
|
+
}
|
|
993
|
+
reconnect() {
|
|
994
|
+
// Reconnect with exponential backoff
|
|
995
|
+
if (!this.#state.isReconnecting &&
|
|
996
|
+
!this.#state.doNotReconnect &&
|
|
997
|
+
!this.terminated) {
|
|
998
|
+
this.disconnect();
|
|
999
|
+
this.#state.doNotReconnect = false;
|
|
1000
|
+
this.#state.isReconnecting = true;
|
|
1001
|
+
const backoffSeconds = 2 ** this.#state.reconnectAttempt;
|
|
1002
|
+
this.#state.reconnectAttempt += 1;
|
|
1003
|
+
this.log('info', `Reconnecting after ${backoffSeconds} seconds...`);
|
|
1004
|
+
this.#state.reconnectTimeoutId = setTimeout(() => {
|
|
1005
|
+
this.#state.reconnectTimeoutId = undefined;
|
|
1006
|
+
this.connect(false);
|
|
1007
|
+
this.#state.isReconnecting = false;
|
|
1008
|
+
}, backoffSeconds * 1000);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
log(level, ...args) {
|
|
1012
|
+
if (this.#options.logger) {
|
|
1013
|
+
this.#options.logger(`[WebSocketClient] | ${level.toUpperCase()} | clientId:${this.#options.clientId} | `, ...args);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
resetReconnectionState() {
|
|
1017
|
+
this.#state.reconnectAttempt = 0;
|
|
1018
|
+
}
|
|
1019
|
+
sendMessage(payload) {
|
|
1020
|
+
const { ws } = this;
|
|
1021
|
+
this.throwIfDisconnected(ws);
|
|
1022
|
+
ws.send(JSON.stringify(payload));
|
|
1023
|
+
return this;
|
|
1024
|
+
}
|
|
1025
|
+
throwIfDisconnected(_webSocket) {
|
|
1026
|
+
if (!this.isConnected) {
|
|
1027
|
+
throw new Error('Websocket not yet connected, await connect() method first');
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
exports.WebSocketClient = WebSocketClient;
|
|
1032
|
+
/**
|
|
1033
|
+
* @internal
|
|
1034
|
+
*/
|
|
1035
|
+
function createDefaultWebSocketTokenFetcher({ auth, baseRestApiURL, sandbox, wallet, }) {
|
|
1036
|
+
const client = new authenticated_1.RestAuthenticatedClient({
|
|
1037
|
+
apiKey: auth.apiKey,
|
|
1038
|
+
apiSecret: auth.apiSecret,
|
|
1039
|
+
baseURL: baseRestApiURL,
|
|
1040
|
+
sandbox,
|
|
1041
|
+
});
|
|
1042
|
+
return async function autoWebSocketTokenFetcher() {
|
|
1043
|
+
return client.getWsToken({
|
|
1044
|
+
nonce: (0, uuid_1.v1)(),
|
|
1045
|
+
wallet: auth.wallet ?? wallet,
|
|
1046
|
+
});
|
|
1047
|
+
};
|
|
1048
|
+
}
|