@drift-labs/sdk-browser 2.104.0-beta.21
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/README.md +276 -0
- package/VERSION +1 -0
- package/bun.lockb +0 -0
- package/get_events.ts +47 -0
- package/lib/browser/accounts/basicUserAccountSubscriber.d.ts +27 -0
- package/lib/browser/accounts/basicUserAccountSubscriber.js +38 -0
- package/lib/browser/accounts/bulkAccountLoader.d.ts +37 -0
- package/lib/browser/accounts/bulkAccountLoader.js +222 -0
- package/lib/browser/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/browser/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/browser/accounts/bulkUserSubscription.d.ts +7 -0
- package/lib/browser/accounts/bulkUserSubscription.js +21 -0
- package/lib/browser/accounts/fetch.d.ts +6 -0
- package/lib/browser/accounts/fetch.js +30 -0
- package/lib/browser/accounts/grpcAccountSubscriber.d.ts +16 -0
- package/lib/browser/accounts/grpcAccountSubscriber.js +154 -0
- package/lib/browser/accounts/grpcDriftClientAccountSubscriber.d.ts +12 -0
- package/lib/browser/accounts/grpcDriftClientAccountSubscriber.js +98 -0
- package/lib/browser/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +10 -0
- package/lib/browser/accounts/grpcInsuranceFundStakeAccountSubscriber.js +30 -0
- package/lib/browser/accounts/grpcProgramAccountSubscriber.d.ts +18 -0
- package/lib/browser/accounts/grpcProgramAccountSubscriber.js +171 -0
- package/lib/browser/accounts/grpcUserAccountSubscriber.d.ts +10 -0
- package/lib/browser/accounts/grpcUserAccountSubscriber.js +28 -0
- package/lib/browser/accounts/grpcUserStatsAccountSubscriber.d.ts +10 -0
- package/lib/browser/accounts/grpcUserStatsAccountSubscriber.js +28 -0
- package/lib/browser/accounts/oneShotUserAccountSubscriber.d.ts +18 -0
- package/lib/browser/accounts/oneShotUserAccountSubscriber.js +48 -0
- package/lib/browser/accounts/pollingDriftClientAccountSubscriber.d.ts +69 -0
- package/lib/browser/accounts/pollingDriftClientAccountSubscriber.js +418 -0
- package/lib/browser/accounts/pollingHighLeverageModeConfigAccountSubscriber.d.ts +29 -0
- package/lib/browser/accounts/pollingHighLeverageModeConfigAccountSubscriber.js +111 -0
- package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +29 -0
- package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.js +110 -0
- package/lib/browser/accounts/pollingOracleAccountSubscriber.d.ts +27 -0
- package/lib/browser/accounts/pollingOracleAccountSubscriber.js +78 -0
- package/lib/browser/accounts/pollingTokenAccountSubscriber.d.ts +26 -0
- package/lib/browser/accounts/pollingTokenAccountSubscriber.js +78 -0
- package/lib/browser/accounts/pollingUserAccountSubscriber.d.ts +29 -0
- package/lib/browser/accounts/pollingUserAccountSubscriber.js +102 -0
- package/lib/browser/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/browser/accounts/pollingUserStatsAccountSubscriber.js +94 -0
- package/lib/browser/accounts/testBulkAccountLoader.d.ts +4 -0
- package/lib/browser/accounts/testBulkAccountLoader.js +45 -0
- package/lib/browser/accounts/types.d.ts +168 -0
- package/lib/browser/accounts/types.js +16 -0
- package/lib/browser/accounts/utils.d.ts +8 -0
- package/lib/browser/accounts/utils.js +49 -0
- package/lib/browser/accounts/webSocketAccountSubscriber.d.ts +29 -0
- package/lib/browser/accounts/webSocketAccountSubscriber.js +149 -0
- package/lib/browser/accounts/webSocketDriftClientAccountSubscriber.d.ts +66 -0
- package/lib/browser/accounts/webSocketDriftClientAccountSubscriber.js +358 -0
- package/lib/browser/accounts/webSocketHighLeverageModeConfigAccountSubscriber.d.ts +23 -0
- package/lib/browser/accounts/webSocketHighLeverageModeConfigAccountSubscriber.js +69 -0
- package/lib/browser/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +23 -0
- package/lib/browser/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +67 -0
- package/lib/browser/accounts/webSocketProgramAccountSubscriber.d.ts +32 -0
- package/lib/browser/accounts/webSocketProgramAccountSubscriber.js +120 -0
- package/lib/browser/accounts/webSocketUserAccountSubscriber.d.ts +23 -0
- package/lib/browser/accounts/webSocketUserAccountSubscriber.js +61 -0
- package/lib/browser/accounts/webSocketUserStatsAccountSubsriber.d.ts +22 -0
- package/lib/browser/accounts/webSocketUserStatsAccountSubsriber.js +52 -0
- package/lib/browser/addresses/marketAddresses.d.ts +2 -0
- package/lib/browser/addresses/marketAddresses.js +15 -0
- package/lib/browser/addresses/pda.d.ts +32 -0
- package/lib/browser/addresses/pda.js +211 -0
- package/lib/browser/adminClient.d.ts +206 -0
- package/lib/browser/adminClient.js +1858 -0
- package/lib/browser/assert/assert.d.ts +1 -0
- package/lib/browser/assert/assert.js +9 -0
- package/lib/browser/auctionSubscriber/auctionSubscriber.d.ts +14 -0
- package/lib/browser/auctionSubscriber/auctionSubscriber.js +32 -0
- package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.d.ts +15 -0
- package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.js +32 -0
- package/lib/browser/auctionSubscriber/index.d.ts +3 -0
- package/lib/browser/auctionSubscriber/index.js +19 -0
- package/lib/browser/auctionSubscriber/types.d.ts +14 -0
- package/lib/browser/auctionSubscriber/types.js +2 -0
- package/lib/browser/bankrun/bankrunConnection.d.ts +75 -0
- package/lib/browser/bankrun/bankrunConnection.js +332 -0
- package/lib/browser/blockhashSubscriber/BlockhashSubscriber.d.ts +27 -0
- package/lib/browser/blockhashSubscriber/BlockhashSubscriber.js +89 -0
- package/lib/browser/blockhashSubscriber/index.d.ts +1 -0
- package/lib/browser/blockhashSubscriber/index.js +17 -0
- package/lib/browser/blockhashSubscriber/types.d.ts +7 -0
- package/lib/browser/blockhashSubscriber/types.js +2 -0
- package/lib/browser/clock/clockSubscriber.d.ts +31 -0
- package/lib/browser/clock/clockSubscriber.js +80 -0
- package/lib/browser/config.d.ts +60 -0
- package/lib/browser/config.js +130 -0
- package/lib/browser/constants/numericConstants.d.ts +71 -0
- package/lib/browser/constants/numericConstants.js +75 -0
- package/lib/browser/constants/perpMarkets.d.ts +19 -0
- package/lib/browser/constants/perpMarkets.js +997 -0
- package/lib/browser/constants/spotMarkets.d.ts +24 -0
- package/lib/browser/constants/spotMarkets.js +470 -0
- package/lib/browser/constants/txConstants.d.ts +1 -0
- package/lib/browser/constants/txConstants.js +4 -0
- package/lib/browser/decode/phoenix.d.ts +7 -0
- package/lib/browser/decode/phoenix.js +159 -0
- package/lib/browser/decode/user.d.ts +4 -0
- package/lib/browser/decode/user.js +339 -0
- package/lib/browser/dlob/DLOB.d.ts +186 -0
- package/lib/browser/dlob/DLOB.js +1039 -0
- package/lib/browser/dlob/DLOBNode.d.ts +68 -0
- package/lib/browser/dlob/DLOBNode.js +100 -0
- package/lib/browser/dlob/DLOBSubscriber.d.ts +54 -0
- package/lib/browser/dlob/DLOBSubscriber.js +139 -0
- package/lib/browser/dlob/NodeList.d.ts +25 -0
- package/lib/browser/dlob/NodeList.js +126 -0
- package/lib/browser/dlob/orderBookLevels.d.ts +72 -0
- package/lib/browser/dlob/orderBookLevels.js +438 -0
- package/lib/browser/dlob/types.d.ts +18 -0
- package/lib/browser/dlob/types.js +2 -0
- package/lib/browser/driftClient.d.ts +861 -0
- package/lib/browser/driftClient.js +4768 -0
- package/lib/browser/driftClientConfig.d.ts +49 -0
- package/lib/browser/driftClientConfig.js +2 -0
- package/lib/browser/events/eventList.d.ts +22 -0
- package/lib/browser/events/eventList.js +80 -0
- package/lib/browser/events/eventSubscriber.d.ts +46 -0
- package/lib/browser/events/eventSubscriber.js +223 -0
- package/lib/browser/events/eventsServerLogProvider.d.ts +21 -0
- package/lib/browser/events/eventsServerLogProvider.js +121 -0
- package/lib/browser/events/fetchLogs.d.ts +25 -0
- package/lib/browser/events/fetchLogs.js +99 -0
- package/lib/browser/events/parse.d.ts +6 -0
- package/lib/browser/events/parse.js +106 -0
- package/lib/browser/events/pollingLogProvider.d.ts +17 -0
- package/lib/browser/events/pollingLogProvider.js +58 -0
- package/lib/browser/events/sort.d.ts +2 -0
- package/lib/browser/events/sort.js +24 -0
- package/lib/browser/events/txEventCache.d.ts +24 -0
- package/lib/browser/events/txEventCache.js +71 -0
- package/lib/browser/events/types.d.ts +79 -0
- package/lib/browser/events/types.js +32 -0
- package/lib/browser/events/webSocketLogProvider.d.ts +24 -0
- package/lib/browser/events/webSocketLogProvider.js +96 -0
- package/lib/browser/factory/bigNum.d.ts +122 -0
- package/lib/browser/factory/bigNum.js +457 -0
- package/lib/browser/factory/oracleClient.d.ts +5 -0
- package/lib/browser/factory/oracleClient.js +56 -0
- package/lib/browser/idl/drift.json +14440 -0
- package/lib/browser/idl/openbook.json +3854 -0
- package/lib/browser/idl/pyth_solana_receiver.json +628 -0
- package/lib/browser/idl/switchboard.json +8354 -0
- package/lib/browser/idl/switchboard_on_demand_30.json +4546 -0
- package/lib/browser/idl/token_faucet.json +142 -0
- package/lib/browser/index.d.ts +125 -0
- package/lib/browser/index.js +147 -0
- package/lib/browser/isomorphic/grpc.browser.d.ts +1 -0
- package/lib/browser/isomorphic/grpc.browser.js +8 -0
- package/lib/browser/isomorphic/grpc.d.ts +1 -0
- package/lib/browser/isomorphic/grpc.js +8 -0
- package/lib/browser/jupiter/jupiterClient.d.ts +302 -0
- package/lib/browser/jupiter/jupiterClient.js +178 -0
- package/lib/browser/keypair.d.ts +2 -0
- package/lib/browser/keypair.js +28 -0
- package/lib/browser/marinade/index.d.ts +12 -0
- package/lib/browser/marinade/index.js +36 -0
- package/lib/browser/marinade/types.d.ts +1963 -0
- package/lib/browser/marinade/types.js +1965 -0
- package/lib/browser/math/amm.d.ts +98 -0
- package/lib/browser/math/amm.js +626 -0
- package/lib/browser/math/auction.d.ts +23 -0
- package/lib/browser/math/auction.js +130 -0
- package/lib/browser/math/bankruptcy.d.ts +2 -0
- package/lib/browser/math/bankruptcy.js +31 -0
- package/lib/browser/math/conversion.d.ts +2 -0
- package/lib/browser/math/conversion.js +11 -0
- package/lib/browser/math/exchangeStatus.d.ts +6 -0
- package/lib/browser/math/exchangeStatus.js +77 -0
- package/lib/browser/math/fuel.d.ts +6 -0
- package/lib/browser/math/fuel.js +55 -0
- package/lib/browser/math/funding.d.ts +34 -0
- package/lib/browser/math/funding.js +209 -0
- package/lib/browser/math/insurance.d.ts +7 -0
- package/lib/browser/math/insurance.js +73 -0
- package/lib/browser/math/margin.d.ts +39 -0
- package/lib/browser/math/margin.js +184 -0
- package/lib/browser/math/market.d.ts +39 -0
- package/lib/browser/math/market.js +163 -0
- package/lib/browser/math/oracles.d.ts +14 -0
- package/lib/browser/math/oracles.js +134 -0
- package/lib/browser/math/orders.d.ts +23 -0
- package/lib/browser/math/orders.js +216 -0
- package/lib/browser/math/position.d.ts +70 -0
- package/lib/browser/math/position.js +225 -0
- package/lib/browser/math/repeg.d.ts +22 -0
- package/lib/browser/math/repeg.js +164 -0
- package/lib/browser/math/spotBalance.d.ts +83 -0
- package/lib/browser/math/spotBalance.js +373 -0
- package/lib/browser/math/spotMarket.d.ts +11 -0
- package/lib/browser/math/spotMarket.js +49 -0
- package/lib/browser/math/spotPosition.d.ts +19 -0
- package/lib/browser/math/spotPosition.js +78 -0
- package/lib/browser/math/state.d.ts +5 -0
- package/lib/browser/math/state.js +30 -0
- package/lib/browser/math/superStake.d.ts +167 -0
- package/lib/browser/math/superStake.js +306 -0
- package/lib/browser/math/tiers.d.ts +4 -0
- package/lib/browser/math/tiers.js +52 -0
- package/lib/browser/math/trade.d.ts +117 -0
- package/lib/browser/math/trade.js +637 -0
- package/lib/browser/math/userStatus.d.ts +2 -0
- package/lib/browser/math/userStatus.js +8 -0
- package/lib/browser/math/utils.d.ts +23 -0
- package/lib/browser/math/utils.js +112 -0
- package/lib/browser/memcmp.d.ts +11 -0
- package/lib/browser/memcmp.js +99 -0
- package/lib/browser/openbook/openbookV2FulfillmentConfigMap.d.ts +10 -0
- package/lib/browser/openbook/openbookV2FulfillmentConfigMap.js +17 -0
- package/lib/browser/openbook/openbookV2Subscriber.d.ts +36 -0
- package/lib/browser/openbook/openbookV2Subscriber.js +104 -0
- package/lib/browser/oracles/oracleClientCache.d.ts +9 -0
- package/lib/browser/oracles/oracleClientCache.js +19 -0
- package/lib/browser/oracles/oracleId.d.ts +4 -0
- package/lib/browser/oracles/oracleId.js +38 -0
- package/lib/browser/oracles/prelaunchOracleClient.d.ts +12 -0
- package/lib/browser/oracles/prelaunchOracleClient.js +24 -0
- package/lib/browser/oracles/pythClient.d.ts +14 -0
- package/lib/browser/oracles/pythClient.js +51 -0
- package/lib/browser/oracles/pythLazerClient.d.ts +16 -0
- package/lib/browser/oracles/pythLazerClient.js +61 -0
- package/lib/browser/oracles/pythPullClient.d.ts +19 -0
- package/lib/browser/oracles/pythPullClient.js +60 -0
- package/lib/browser/oracles/quoteAssetOracleClient.d.ts +10 -0
- package/lib/browser/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/browser/oracles/strictOraclePrice.d.ts +9 -0
- package/lib/browser/oracles/strictOraclePrice.js +17 -0
- package/lib/browser/oracles/switchboardClient.d.ts +12 -0
- package/lib/browser/oracles/switchboardClient.js +40 -0
- package/lib/browser/oracles/switchboardOnDemandClient.d.ts +12 -0
- package/lib/browser/oracles/switchboardOnDemandClient.js +32 -0
- package/lib/browser/oracles/types.d.ts +23 -0
- package/lib/browser/oracles/types.js +2 -0
- package/lib/browser/orderParams.d.ts +29 -0
- package/lib/browser/orderParams.js +44 -0
- package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +42 -0
- package/lib/browser/orderSubscriber/OrderSubscriber.js +172 -0
- package/lib/browser/orderSubscriber/PollingSubscription.d.ts +12 -0
- package/lib/browser/orderSubscriber/PollingSubscription.js +23 -0
- package/lib/browser/orderSubscriber/WebsocketSubscription.d.ts +23 -0
- package/lib/browser/orderSubscriber/WebsocketSubscription.js +67 -0
- package/lib/browser/orderSubscriber/grpcSubscription.d.ts +22 -0
- package/lib/browser/orderSubscriber/grpcSubscription.js +66 -0
- package/lib/browser/orderSubscriber/index.d.ts +2 -0
- package/lib/browser/orderSubscriber/index.js +18 -0
- package/lib/browser/orderSubscriber/types.d.ts +34 -0
- package/lib/browser/orderSubscriber/types.js +2 -0
- package/lib/browser/phoenix/phoenixFulfillmentConfigMap.d.ts +10 -0
- package/lib/browser/phoenix/phoenixFulfillmentConfigMap.js +17 -0
- package/lib/browser/phoenix/phoenixSubscriber.d.ts +41 -0
- package/lib/browser/phoenix/phoenixSubscriber.js +152 -0
- package/lib/browser/priorityFee/averageOverSlotsStrategy.d.ts +5 -0
- package/lib/browser/priorityFee/averageOverSlotsStrategy.js +16 -0
- package/lib/browser/priorityFee/averageStrategy.d.ts +5 -0
- package/lib/browser/priorityFee/averageStrategy.js +11 -0
- package/lib/browser/priorityFee/driftPriorityFeeMethod.d.ts +13 -0
- package/lib/browser/priorityFee/driftPriorityFeeMethod.js +26 -0
- package/lib/browser/priorityFee/ewmaStrategy.d.ts +11 -0
- package/lib/browser/priorityFee/ewmaStrategy.js +33 -0
- package/lib/browser/priorityFee/heliusPriorityFeeMethod.d.ts +20 -0
- package/lib/browser/priorityFee/heliusPriorityFeeMethod.js +46 -0
- package/lib/browser/priorityFee/index.d.ts +11 -0
- package/lib/browser/priorityFee/index.js +27 -0
- package/lib/browser/priorityFee/maxOverSlotsStrategy.d.ts +5 -0
- package/lib/browser/priorityFee/maxOverSlotsStrategy.js +17 -0
- package/lib/browser/priorityFee/maxStrategy.d.ts +7 -0
- package/lib/browser/priorityFee/maxStrategy.js +9 -0
- package/lib/browser/priorityFee/priorityFeeSubscriber.d.ts +46 -0
- package/lib/browser/priorityFee/priorityFeeSubscriber.js +188 -0
- package/lib/browser/priorityFee/priorityFeeSubscriberMap.d.ts +48 -0
- package/lib/browser/priorityFee/priorityFeeSubscriberMap.js +88 -0
- package/lib/browser/priorityFee/solanaPriorityFeeMethod.d.ts +6 -0
- package/lib/browser/priorityFee/solanaPriorityFeeMethod.js +21 -0
- package/lib/browser/priorityFee/types.d.ts +31 -0
- package/lib/browser/priorityFee/types.js +10 -0
- package/lib/browser/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/browser/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/browser/serum/serumSubscriber.d.ts +32 -0
- package/lib/browser/serum/serumSubscriber.js +107 -0
- package/lib/browser/serum/types.d.ts +13 -0
- package/lib/browser/serum/types.js +2 -0
- package/lib/browser/slot/SlotSubscriber.d.ts +27 -0
- package/lib/browser/slot/SlotSubscriber.js +71 -0
- package/lib/browser/slot/SlothashSubscriber.d.ts +26 -0
- package/lib/browser/slot/SlothashSubscriber.js +85 -0
- package/lib/browser/testClient.d.ts +8 -0
- package/lib/browser/testClient.js +23 -0
- package/lib/browser/token/index.d.ts +5 -0
- package/lib/browser/token/index.js +15 -0
- package/lib/browser/tokenFaucet.d.ts +41 -0
- package/lib/browser/tokenFaucet.js +188 -0
- package/lib/browser/tx/baseTxSender.d.ts +59 -0
- package/lib/browser/tx/baseTxSender.js +294 -0
- package/lib/browser/tx/blockhashFetcher/baseBlockhashFetcher.d.ts +8 -0
- package/lib/browser/tx/blockhashFetcher/baseBlockhashFetcher.js +13 -0
- package/lib/browser/tx/blockhashFetcher/cachedBlockhashFetcher.d.ts +28 -0
- package/lib/browser/tx/blockhashFetcher/cachedBlockhashFetcher.js +73 -0
- package/lib/browser/tx/blockhashFetcher/types.d.ts +4 -0
- package/lib/browser/tx/blockhashFetcher/types.js +2 -0
- package/lib/browser/tx/fastSingleTxSender.d.ts +41 -0
- package/lib/browser/tx/fastSingleTxSender.js +86 -0
- package/lib/browser/tx/forwardOnlyTxSender.d.ts +37 -0
- package/lib/browser/tx/forwardOnlyTxSender.js +92 -0
- package/lib/browser/tx/priorityFeeCalculator.d.ts +44 -0
- package/lib/browser/tx/priorityFeeCalculator.js +85 -0
- package/lib/browser/tx/reportTransactionError.d.ts +20 -0
- package/lib/browser/tx/reportTransactionError.js +103 -0
- package/lib/browser/tx/retryTxSender.d.ts +37 -0
- package/lib/browser/tx/retryTxSender.js +86 -0
- package/lib/browser/tx/txHandler.d.ts +154 -0
- package/lib/browser/tx/txHandler.js +453 -0
- package/lib/browser/tx/txParamProcessor.d.ts +25 -0
- package/lib/browser/tx/txParamProcessor.js +88 -0
- package/lib/browser/tx/types.d.ts +29 -0
- package/lib/browser/tx/types.js +20 -0
- package/lib/browser/tx/utils.d.ts +2 -0
- package/lib/browser/tx/utils.js +10 -0
- package/lib/browser/tx/whileValidTxSender.d.ts +45 -0
- package/lib/browser/tx/whileValidTxSender.js +167 -0
- package/lib/browser/types.d.ts +1385 -0
- package/lib/browser/types.js +366 -0
- package/lib/browser/user.d.ts +411 -0
- package/lib/browser/user.js +2151 -0
- package/lib/browser/userConfig.d.ts +26 -0
- package/lib/browser/userConfig.js +2 -0
- package/lib/browser/userMap/PollingSubscription.d.ts +16 -0
- package/lib/browser/userMap/PollingSubscription.js +30 -0
- package/lib/browser/userMap/WebsocketSubscription.d.ts +27 -0
- package/lib/browser/userMap/WebsocketSubscription.js +45 -0
- package/lib/browser/userMap/grpcSubscription.d.ts +27 -0
- package/lib/browser/userMap/grpcSubscription.js +44 -0
- package/lib/browser/userMap/referrerMap.d.ts +45 -0
- package/lib/browser/userMap/referrerMap.js +181 -0
- package/lib/browser/userMap/userMap.d.ts +90 -0
- package/lib/browser/userMap/userMap.js +467 -0
- package/lib/browser/userMap/userMapConfig.d.ts +39 -0
- package/lib/browser/userMap/userMapConfig.js +2 -0
- package/lib/browser/userMap/userStatsMap.d.ts +46 -0
- package/lib/browser/userMap/userStatsMap.js +165 -0
- package/lib/browser/userName.d.ts +5 -0
- package/lib/browser/userName.js +21 -0
- package/lib/browser/userStats.d.ts +22 -0
- package/lib/browser/userStats.js +91 -0
- package/lib/browser/userStatsConfig.d.ts +25 -0
- package/lib/browser/userStatsConfig.js +2 -0
- package/lib/browser/util/TransactionConfirmationManager.d.ts +16 -0
- package/lib/browser/util/TransactionConfirmationManager.js +174 -0
- package/lib/browser/util/chainClock.d.ts +17 -0
- package/lib/browser/util/chainClock.js +29 -0
- package/lib/browser/util/computeUnits.d.ts +8 -0
- package/lib/browser/util/computeUnits.js +48 -0
- package/lib/browser/util/digest.d.ts +4 -0
- package/lib/browser/util/digest.js +14 -0
- package/lib/browser/util/promiseTimeout.d.ts +1 -0
- package/lib/browser/util/promiseTimeout.js +14 -0
- package/lib/browser/util/pythOracleUtils.d.ts +17 -0
- package/lib/browser/util/pythOracleUtils.js +107 -0
- package/lib/browser/util/tps.d.ts +2 -0
- package/lib/browser/util/tps.js +16 -0
- package/lib/browser/wallet.d.ts +11 -0
- package/lib/browser/wallet.js +32 -0
- package/lib/node/accounts/basicUserAccountSubscriber.d.ts +27 -0
- package/lib/node/accounts/basicUserAccountSubscriber.js +38 -0
- package/lib/node/accounts/bulkAccountLoader.d.ts +37 -0
- package/lib/node/accounts/bulkAccountLoader.js +222 -0
- package/lib/node/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/node/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/node/accounts/bulkUserSubscription.d.ts +7 -0
- package/lib/node/accounts/bulkUserSubscription.js +21 -0
- package/lib/node/accounts/fetch.d.ts +6 -0
- package/lib/node/accounts/fetch.js +30 -0
- package/lib/node/accounts/grpcAccountSubscriber.d.ts +16 -0
- package/lib/node/accounts/grpcAccountSubscriber.js +154 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriber.d.ts +12 -0
- package/lib/node/accounts/grpcDriftClientAccountSubscriber.js +98 -0
- package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +10 -0
- package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.js +30 -0
- package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts +18 -0
- package/lib/node/accounts/grpcProgramAccountSubscriber.js +171 -0
- package/lib/node/accounts/grpcUserAccountSubscriber.d.ts +10 -0
- package/lib/node/accounts/grpcUserAccountSubscriber.js +28 -0
- package/lib/node/accounts/grpcUserStatsAccountSubscriber.d.ts +10 -0
- package/lib/node/accounts/grpcUserStatsAccountSubscriber.js +28 -0
- package/lib/node/accounts/oneShotUserAccountSubscriber.d.ts +18 -0
- package/lib/node/accounts/oneShotUserAccountSubscriber.js +48 -0
- package/lib/node/accounts/pollingDriftClientAccountSubscriber.d.ts +69 -0
- package/lib/node/accounts/pollingDriftClientAccountSubscriber.js +418 -0
- package/lib/node/accounts/pollingHighLeverageModeConfigAccountSubscriber.d.ts +29 -0
- package/lib/node/accounts/pollingHighLeverageModeConfigAccountSubscriber.js +111 -0
- package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +29 -0
- package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.js +110 -0
- package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts +27 -0
- package/lib/node/accounts/pollingOracleAccountSubscriber.js +78 -0
- package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts +26 -0
- package/lib/node/accounts/pollingTokenAccountSubscriber.js +78 -0
- package/lib/node/accounts/pollingUserAccountSubscriber.d.ts +29 -0
- package/lib/node/accounts/pollingUserAccountSubscriber.js +102 -0
- package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/node/accounts/pollingUserStatsAccountSubscriber.js +94 -0
- package/lib/node/accounts/testBulkAccountLoader.d.ts +4 -0
- package/lib/node/accounts/testBulkAccountLoader.js +45 -0
- package/lib/node/accounts/types.d.ts +168 -0
- package/lib/node/accounts/types.js +16 -0
- package/lib/node/accounts/utils.d.ts +8 -0
- package/lib/node/accounts/utils.js +49 -0
- package/lib/node/accounts/webSocketAccountSubscriber.d.ts +29 -0
- package/lib/node/accounts/webSocketAccountSubscriber.js +149 -0
- package/lib/node/accounts/webSocketDriftClientAccountSubscriber.d.ts +66 -0
- package/lib/node/accounts/webSocketDriftClientAccountSubscriber.js +358 -0
- package/lib/node/accounts/webSocketHighLeverageModeConfigAccountSubscriber.d.ts +23 -0
- package/lib/node/accounts/webSocketHighLeverageModeConfigAccountSubscriber.js +69 -0
- package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +23 -0
- package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +67 -0
- package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts +32 -0
- package/lib/node/accounts/webSocketProgramAccountSubscriber.js +120 -0
- package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts +23 -0
- package/lib/node/accounts/webSocketUserAccountSubscriber.js +61 -0
- package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts +22 -0
- package/lib/node/accounts/webSocketUserStatsAccountSubsriber.js +52 -0
- package/lib/node/addresses/marketAddresses.d.ts +2 -0
- package/lib/node/addresses/marketAddresses.js +15 -0
- package/lib/node/addresses/pda.d.ts +32 -0
- package/lib/node/addresses/pda.js +211 -0
- package/lib/node/adminClient.d.ts +206 -0
- package/lib/node/adminClient.js +1858 -0
- package/lib/node/assert/assert.d.ts +1 -0
- package/lib/node/assert/assert.js +9 -0
- package/lib/node/auctionSubscriber/auctionSubscriber.d.ts +14 -0
- package/lib/node/auctionSubscriber/auctionSubscriber.js +32 -0
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts +15 -0
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.js +32 -0
- package/lib/node/auctionSubscriber/index.d.ts +3 -0
- package/lib/node/auctionSubscriber/index.js +19 -0
- package/lib/node/auctionSubscriber/types.d.ts +14 -0
- package/lib/node/auctionSubscriber/types.js +2 -0
- package/lib/node/bankrun/bankrunConnection.d.ts +75 -0
- package/lib/node/bankrun/bankrunConnection.js +332 -0
- package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts +27 -0
- package/lib/node/blockhashSubscriber/BlockhashSubscriber.js +89 -0
- package/lib/node/blockhashSubscriber/index.d.ts +1 -0
- package/lib/node/blockhashSubscriber/index.js +17 -0
- package/lib/node/blockhashSubscriber/types.d.ts +7 -0
- package/lib/node/blockhashSubscriber/types.js +2 -0
- package/lib/node/clock/clockSubscriber.d.ts +31 -0
- package/lib/node/clock/clockSubscriber.js +80 -0
- package/lib/node/config.d.ts +60 -0
- package/lib/node/config.js +130 -0
- package/lib/node/constants/numericConstants.d.ts +71 -0
- package/lib/node/constants/numericConstants.js +75 -0
- package/lib/node/constants/perpMarkets.d.ts +19 -0
- package/lib/node/constants/perpMarkets.js +997 -0
- package/lib/node/constants/spotMarkets.d.ts +24 -0
- package/lib/node/constants/spotMarkets.js +470 -0
- package/lib/node/constants/txConstants.d.ts +1 -0
- package/lib/node/constants/txConstants.js +4 -0
- package/lib/node/decode/phoenix.d.ts +7 -0
- package/lib/node/decode/phoenix.js +159 -0
- package/lib/node/decode/user.d.ts +4 -0
- package/lib/node/decode/user.js +339 -0
- package/lib/node/dlob/DLOB.d.ts +186 -0
- package/lib/node/dlob/DLOB.js +1039 -0
- package/lib/node/dlob/DLOBNode.d.ts +68 -0
- package/lib/node/dlob/DLOBNode.js +100 -0
- package/lib/node/dlob/DLOBSubscriber.d.ts +54 -0
- package/lib/node/dlob/DLOBSubscriber.js +139 -0
- package/lib/node/dlob/NodeList.d.ts +25 -0
- package/lib/node/dlob/NodeList.js +126 -0
- package/lib/node/dlob/orderBookLevels.d.ts +72 -0
- package/lib/node/dlob/orderBookLevels.js +438 -0
- package/lib/node/dlob/types.d.ts +18 -0
- package/lib/node/dlob/types.js +2 -0
- package/lib/node/driftClient.d.ts +861 -0
- package/lib/node/driftClient.js +4768 -0
- package/lib/node/driftClientConfig.d.ts +49 -0
- package/lib/node/driftClientConfig.js +2 -0
- package/lib/node/events/eventList.d.ts +22 -0
- package/lib/node/events/eventList.js +80 -0
- package/lib/node/events/eventSubscriber.d.ts +46 -0
- package/lib/node/events/eventSubscriber.js +223 -0
- package/lib/node/events/eventsServerLogProvider.d.ts +21 -0
- package/lib/node/events/eventsServerLogProvider.js +121 -0
- package/lib/node/events/fetchLogs.d.ts +25 -0
- package/lib/node/events/fetchLogs.js +99 -0
- package/lib/node/events/parse.d.ts +6 -0
- package/lib/node/events/parse.js +106 -0
- package/lib/node/events/pollingLogProvider.d.ts +17 -0
- package/lib/node/events/pollingLogProvider.js +58 -0
- package/lib/node/events/sort.d.ts +2 -0
- package/lib/node/events/sort.js +24 -0
- package/lib/node/events/txEventCache.d.ts +24 -0
- package/lib/node/events/txEventCache.js +71 -0
- package/lib/node/events/types.d.ts +79 -0
- package/lib/node/events/types.js +32 -0
- package/lib/node/events/webSocketLogProvider.d.ts +24 -0
- package/lib/node/events/webSocketLogProvider.js +96 -0
- package/lib/node/factory/bigNum.d.ts +122 -0
- package/lib/node/factory/bigNum.js +457 -0
- package/lib/node/factory/oracleClient.d.ts +5 -0
- package/lib/node/factory/oracleClient.js +56 -0
- package/lib/node/idl/drift.json +14440 -0
- package/lib/node/idl/openbook.json +3854 -0
- package/lib/node/idl/pyth_solana_receiver.json +628 -0
- package/lib/node/idl/switchboard.json +8354 -0
- package/lib/node/idl/switchboard_on_demand_30.json +4546 -0
- package/lib/node/idl/token_faucet.json +142 -0
- package/lib/node/index.d.ts +125 -0
- package/lib/node/index.js +147 -0
- package/lib/node/isomorphic/grpc.browser.d.ts +1 -0
- package/lib/node/isomorphic/grpc.browser.js +8 -0
- package/lib/node/isomorphic/grpc.d.ts +1 -0
- package/lib/node/isomorphic/grpc.js +8 -0
- package/lib/node/jupiter/jupiterClient.d.ts +302 -0
- package/lib/node/jupiter/jupiterClient.js +178 -0
- package/lib/node/keypair.d.ts +2 -0
- package/lib/node/keypair.js +28 -0
- package/lib/node/marinade/index.d.ts +12 -0
- package/lib/node/marinade/index.js +36 -0
- package/lib/node/marinade/types.d.ts +1963 -0
- package/lib/node/marinade/types.js +1965 -0
- package/lib/node/math/amm.d.ts +98 -0
- package/lib/node/math/amm.js +626 -0
- package/lib/node/math/auction.d.ts +23 -0
- package/lib/node/math/auction.js +130 -0
- package/lib/node/math/bankruptcy.d.ts +2 -0
- package/lib/node/math/bankruptcy.js +31 -0
- package/lib/node/math/conversion.d.ts +2 -0
- package/lib/node/math/conversion.js +11 -0
- package/lib/node/math/exchangeStatus.d.ts +6 -0
- package/lib/node/math/exchangeStatus.js +77 -0
- package/lib/node/math/fuel.d.ts +6 -0
- package/lib/node/math/fuel.js +55 -0
- package/lib/node/math/funding.d.ts +34 -0
- package/lib/node/math/funding.js +209 -0
- package/lib/node/math/insurance.d.ts +7 -0
- package/lib/node/math/insurance.js +73 -0
- package/lib/node/math/margin.d.ts +39 -0
- package/lib/node/math/margin.js +184 -0
- package/lib/node/math/market.d.ts +39 -0
- package/lib/node/math/market.js +163 -0
- package/lib/node/math/oracles.d.ts +14 -0
- package/lib/node/math/oracles.js +134 -0
- package/lib/node/math/orders.d.ts +23 -0
- package/lib/node/math/orders.js +216 -0
- package/lib/node/math/position.d.ts +70 -0
- package/lib/node/math/position.js +225 -0
- package/lib/node/math/repeg.d.ts +22 -0
- package/lib/node/math/repeg.js +164 -0
- package/lib/node/math/spotBalance.d.ts +83 -0
- package/lib/node/math/spotBalance.js +373 -0
- package/lib/node/math/spotMarket.d.ts +11 -0
- package/lib/node/math/spotMarket.js +49 -0
- package/lib/node/math/spotPosition.d.ts +19 -0
- package/lib/node/math/spotPosition.js +78 -0
- package/lib/node/math/state.d.ts +5 -0
- package/lib/node/math/state.js +30 -0
- package/lib/node/math/superStake.d.ts +167 -0
- package/lib/node/math/superStake.js +306 -0
- package/lib/node/math/tiers.d.ts +4 -0
- package/lib/node/math/tiers.js +52 -0
- package/lib/node/math/trade.d.ts +117 -0
- package/lib/node/math/trade.js +637 -0
- package/lib/node/math/userStatus.d.ts +2 -0
- package/lib/node/math/userStatus.js +8 -0
- package/lib/node/math/utils.d.ts +23 -0
- package/lib/node/math/utils.js +112 -0
- package/lib/node/memcmp.d.ts +11 -0
- package/lib/node/memcmp.js +99 -0
- package/lib/node/openbook/openbookV2FulfillmentConfigMap.d.ts +10 -0
- package/lib/node/openbook/openbookV2FulfillmentConfigMap.js +17 -0
- package/lib/node/openbook/openbookV2Subscriber.d.ts +36 -0
- package/lib/node/openbook/openbookV2Subscriber.js +104 -0
- package/lib/node/oracles/oracleClientCache.d.ts +9 -0
- package/lib/node/oracles/oracleClientCache.js +19 -0
- package/lib/node/oracles/oracleId.d.ts +4 -0
- package/lib/node/oracles/oracleId.js +38 -0
- package/lib/node/oracles/prelaunchOracleClient.d.ts +12 -0
- package/lib/node/oracles/prelaunchOracleClient.js +24 -0
- package/lib/node/oracles/pythClient.d.ts +14 -0
- package/lib/node/oracles/pythClient.js +51 -0
- package/lib/node/oracles/pythLazerClient.d.ts +16 -0
- package/lib/node/oracles/pythLazerClient.js +61 -0
- package/lib/node/oracles/pythPullClient.d.ts +19 -0
- package/lib/node/oracles/pythPullClient.js +60 -0
- package/lib/node/oracles/quoteAssetOracleClient.d.ts +10 -0
- package/lib/node/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/node/oracles/strictOraclePrice.d.ts +9 -0
- package/lib/node/oracles/strictOraclePrice.js +17 -0
- package/lib/node/oracles/switchboardClient.d.ts +12 -0
- package/lib/node/oracles/switchboardClient.js +40 -0
- package/lib/node/oracles/switchboardOnDemandClient.d.ts +12 -0
- package/lib/node/oracles/switchboardOnDemandClient.js +32 -0
- package/lib/node/oracles/types.d.ts +23 -0
- package/lib/node/oracles/types.js +2 -0
- package/lib/node/orderParams.d.ts +29 -0
- package/lib/node/orderParams.js +44 -0
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts +42 -0
- package/lib/node/orderSubscriber/OrderSubscriber.js +172 -0
- package/lib/node/orderSubscriber/PollingSubscription.d.ts +12 -0
- package/lib/node/orderSubscriber/PollingSubscription.js +23 -0
- package/lib/node/orderSubscriber/WebsocketSubscription.d.ts +23 -0
- package/lib/node/orderSubscriber/WebsocketSubscription.js +67 -0
- package/lib/node/orderSubscriber/grpcSubscription.d.ts +22 -0
- package/lib/node/orderSubscriber/grpcSubscription.js +66 -0
- package/lib/node/orderSubscriber/index.d.ts +2 -0
- package/lib/node/orderSubscriber/index.js +18 -0
- package/lib/node/orderSubscriber/types.d.ts +34 -0
- package/lib/node/orderSubscriber/types.js +2 -0
- package/lib/node/phoenix/phoenixFulfillmentConfigMap.d.ts +10 -0
- package/lib/node/phoenix/phoenixFulfillmentConfigMap.js +17 -0
- package/lib/node/phoenix/phoenixSubscriber.d.ts +41 -0
- package/lib/node/phoenix/phoenixSubscriber.js +152 -0
- package/lib/node/priorityFee/averageOverSlotsStrategy.d.ts +5 -0
- package/lib/node/priorityFee/averageOverSlotsStrategy.js +16 -0
- package/lib/node/priorityFee/averageStrategy.d.ts +5 -0
- package/lib/node/priorityFee/averageStrategy.js +11 -0
- package/lib/node/priorityFee/driftPriorityFeeMethod.d.ts +13 -0
- package/lib/node/priorityFee/driftPriorityFeeMethod.js +26 -0
- package/lib/node/priorityFee/ewmaStrategy.d.ts +11 -0
- package/lib/node/priorityFee/ewmaStrategy.js +33 -0
- package/lib/node/priorityFee/heliusPriorityFeeMethod.d.ts +20 -0
- package/lib/node/priorityFee/heliusPriorityFeeMethod.js +46 -0
- package/lib/node/priorityFee/index.d.ts +11 -0
- package/lib/node/priorityFee/index.js +27 -0
- package/lib/node/priorityFee/maxOverSlotsStrategy.d.ts +5 -0
- package/lib/node/priorityFee/maxOverSlotsStrategy.js +17 -0
- package/lib/node/priorityFee/maxStrategy.d.ts +7 -0
- package/lib/node/priorityFee/maxStrategy.js +9 -0
- package/lib/node/priorityFee/priorityFeeSubscriber.d.ts +46 -0
- package/lib/node/priorityFee/priorityFeeSubscriber.js +188 -0
- package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts +48 -0
- package/lib/node/priorityFee/priorityFeeSubscriberMap.js +88 -0
- package/lib/node/priorityFee/solanaPriorityFeeMethod.d.ts +6 -0
- package/lib/node/priorityFee/solanaPriorityFeeMethod.js +21 -0
- package/lib/node/priorityFee/types.d.ts +31 -0
- package/lib/node/priorityFee/types.js +10 -0
- package/lib/node/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/node/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/node/serum/serumSubscriber.d.ts +32 -0
- package/lib/node/serum/serumSubscriber.js +107 -0
- package/lib/node/serum/types.d.ts +13 -0
- package/lib/node/serum/types.js +2 -0
- package/lib/node/slot/SlotSubscriber.d.ts +27 -0
- package/lib/node/slot/SlotSubscriber.js +71 -0
- package/lib/node/slot/SlothashSubscriber.d.ts +26 -0
- package/lib/node/slot/SlothashSubscriber.js +85 -0
- package/lib/node/testClient.d.ts +8 -0
- package/lib/node/testClient.js +23 -0
- package/lib/node/token/index.d.ts +5 -0
- package/lib/node/token/index.js +15 -0
- package/lib/node/tokenFaucet.d.ts +41 -0
- package/lib/node/tokenFaucet.js +188 -0
- package/lib/node/tx/baseTxSender.d.ts +59 -0
- package/lib/node/tx/baseTxSender.js +294 -0
- package/lib/node/tx/blockhashFetcher/baseBlockhashFetcher.d.ts +8 -0
- package/lib/node/tx/blockhashFetcher/baseBlockhashFetcher.js +13 -0
- package/lib/node/tx/blockhashFetcher/cachedBlockhashFetcher.d.ts +28 -0
- package/lib/node/tx/blockhashFetcher/cachedBlockhashFetcher.js +73 -0
- package/lib/node/tx/blockhashFetcher/types.d.ts +4 -0
- package/lib/node/tx/blockhashFetcher/types.js +2 -0
- package/lib/node/tx/fastSingleTxSender.d.ts +41 -0
- package/lib/node/tx/fastSingleTxSender.js +86 -0
- package/lib/node/tx/forwardOnlyTxSender.d.ts +37 -0
- package/lib/node/tx/forwardOnlyTxSender.js +92 -0
- package/lib/node/tx/priorityFeeCalculator.d.ts +44 -0
- package/lib/node/tx/priorityFeeCalculator.js +85 -0
- package/lib/node/tx/reportTransactionError.d.ts +20 -0
- package/lib/node/tx/reportTransactionError.js +103 -0
- package/lib/node/tx/retryTxSender.d.ts +37 -0
- package/lib/node/tx/retryTxSender.js +86 -0
- package/lib/node/tx/txHandler.d.ts +154 -0
- package/lib/node/tx/txHandler.js +453 -0
- package/lib/node/tx/txParamProcessor.d.ts +25 -0
- package/lib/node/tx/txParamProcessor.js +88 -0
- package/lib/node/tx/types.d.ts +29 -0
- package/lib/node/tx/types.js +20 -0
- package/lib/node/tx/utils.d.ts +2 -0
- package/lib/node/tx/utils.js +10 -0
- package/lib/node/tx/whileValidTxSender.d.ts +45 -0
- package/lib/node/tx/whileValidTxSender.js +167 -0
- package/lib/node/types.d.ts +1385 -0
- package/lib/node/types.js +366 -0
- package/lib/node/user.d.ts +411 -0
- package/lib/node/user.js +2151 -0
- package/lib/node/userConfig.d.ts +26 -0
- package/lib/node/userConfig.js +2 -0
- package/lib/node/userMap/PollingSubscription.d.ts +16 -0
- package/lib/node/userMap/PollingSubscription.js +30 -0
- package/lib/node/userMap/WebsocketSubscription.d.ts +27 -0
- package/lib/node/userMap/WebsocketSubscription.js +45 -0
- package/lib/node/userMap/grpcSubscription.d.ts +27 -0
- package/lib/node/userMap/grpcSubscription.js +44 -0
- package/lib/node/userMap/referrerMap.d.ts +45 -0
- package/lib/node/userMap/referrerMap.js +181 -0
- package/lib/node/userMap/userMap.d.ts +90 -0
- package/lib/node/userMap/userMap.js +467 -0
- package/lib/node/userMap/userMapConfig.d.ts +39 -0
- package/lib/node/userMap/userMapConfig.js +2 -0
- package/lib/node/userMap/userStatsMap.d.ts +46 -0
- package/lib/node/userMap/userStatsMap.js +165 -0
- package/lib/node/userName.d.ts +5 -0
- package/lib/node/userName.js +21 -0
- package/lib/node/userStats.d.ts +22 -0
- package/lib/node/userStats.js +91 -0
- package/lib/node/userStatsConfig.d.ts +25 -0
- package/lib/node/userStatsConfig.js +2 -0
- package/lib/node/util/TransactionConfirmationManager.d.ts +16 -0
- package/lib/node/util/TransactionConfirmationManager.js +174 -0
- package/lib/node/util/chainClock.d.ts +17 -0
- package/lib/node/util/chainClock.js +29 -0
- package/lib/node/util/computeUnits.d.ts +8 -0
- package/lib/node/util/computeUnits.js +48 -0
- package/lib/node/util/digest.d.ts +4 -0
- package/lib/node/util/digest.js +14 -0
- package/lib/node/util/promiseTimeout.d.ts +1 -0
- package/lib/node/util/promiseTimeout.js +14 -0
- package/lib/node/util/pythOracleUtils.d.ts +17 -0
- package/lib/node/util/pythOracleUtils.js +107 -0
- package/lib/node/util/tps.d.ts +2 -0
- package/lib/node/util/tps.js +16 -0
- package/lib/node/wallet.d.ts +11 -0
- package/lib/node/wallet.js +32 -0
- package/package.json +92 -0
- package/scripts/postbuild.js +95 -0
- package/scripts/updateVersion.js +28 -0
- package/src/accounts/basicUserAccountSubscriber.ts +59 -0
- package/src/accounts/bulkAccountLoader.ts +294 -0
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +33 -0
- package/src/accounts/fetch.ts +66 -0
- package/src/accounts/grpcAccountSubscriber.ts +160 -0
- package/src/accounts/grpcDriftClientAccountSubscriber.ts +203 -0
- package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +56 -0
- package/src/accounts/grpcProgramAccountSubscriber.ts +190 -0
- package/src/accounts/grpcUserAccountSubscriber.ts +48 -0
- package/src/accounts/grpcUserStatsAccountSubscriber.ts +50 -0
- package/src/accounts/oneShotUserAccountSubscriber.ts +68 -0
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +644 -0
- package/src/accounts/pollingHighLeverageModeConfigAccountSubscriber.ts +189 -0
- package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +185 -0
- package/src/accounts/pollingOracleAccountSubscriber.ts +125 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +118 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +160 -0
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +156 -0
- package/src/accounts/testBulkAccountLoader.ts +53 -0
- package/src/accounts/types.ts +245 -0
- package/src/accounts/utils.ts +62 -0
- package/src/accounts/webSocketAccountSubscriber.ts +205 -0
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +621 -0
- package/src/accounts/webSocketHighLeverageModeConfigAccountSubscriber.ts +131 -0
- package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +129 -0
- package/src/accounts/webSocketProgramAccountSubscriber.ts +182 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +104 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +98 -0
- package/src/addresses/marketAddresses.ts +17 -0
- package/src/addresses/pda.ts +355 -0
- package/src/adminClient.ts +4030 -0
- package/src/assert/assert.ts +5 -0
- package/src/auctionSubscriber/auctionSubscriber.ts +66 -0
- package/src/auctionSubscriber/auctionSubscriberGrpc.ts +70 -0
- package/src/auctionSubscriber/index.ts +3 -0
- package/src/auctionSubscriber/types.ts +20 -0
- package/src/bankrun/bankrunConnection.ts +534 -0
- package/src/blockhashSubscriber/BlockhashSubscriber.ts +126 -0
- package/src/blockhashSubscriber/index.ts +1 -0
- package/src/blockhashSubscriber/types.ts +12 -0
- package/src/clock/clockSubscriber.ts +121 -0
- package/src/config.ts +212 -0
- package/src/constants/numericConstants.ts +113 -0
- package/src/constants/perpMarkets.ts +1084 -0
- package/src/constants/spotMarkets.ts +565 -0
- package/src/constants/txConstants.ts +1 -0
- package/src/decode/phoenix.ts +207 -0
- package/src/decode/user.ts +368 -0
- package/src/dlob/DLOB.ts +1897 -0
- package/src/dlob/DLOBNode.ts +197 -0
- package/src/dlob/DLOBSubscriber.ts +201 -0
- package/src/dlob/NodeList.ts +173 -0
- package/src/dlob/orderBookLevels.ts +643 -0
- package/src/dlob/types.ts +22 -0
- package/src/driftClient.ts +9032 -0
- package/src/driftClientConfig.ts +60 -0
- package/src/events/eventList.ts +97 -0
- package/src/events/eventSubscriber.ts +364 -0
- package/src/events/eventsServerLogProvider.ts +152 -0
- package/src/events/fetchLogs.ts +169 -0
- package/src/events/parse.ts +133 -0
- package/src/events/pollingLogProvider.ts +89 -0
- package/src/events/sort.ts +39 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.ts +185 -0
- package/src/events/webSocketLogProvider.ts +121 -0
- package/src/factory/bigNum.ts +660 -0
- package/src/factory/oracleClient.ts +72 -0
- package/src/idl/drift.json +14440 -0
- package/src/idl/openbook.json +3854 -0
- package/src/idl/pyth.json +142 -0
- package/src/idl/pyth_solana_receiver.json +628 -0
- package/src/idl/switchboard.json +8354 -0
- package/src/idl/switchboard_on_demand_30.json +4546 -0
- package/src/idl/token_faucet.json +142 -0
- package/src/index.ts +127 -0
- package/src/isomorphic/README.md +19 -0
- package/src/isomorphic/grpc.browser.ts +4 -0
- package/src/isomorphic/grpc.node.ts +23 -0
- package/src/isomorphic/grpc.ts +1 -0
- package/src/jupiter/jupiterClient.ts +510 -0
- package/src/keypair.ts +24 -0
- package/src/marinade/idl/idl.json +1962 -0
- package/src/marinade/index.ts +64 -0
- package/src/marinade/types.ts +3925 -0
- package/src/math/amm.ts +1162 -0
- package/src/math/auction.ts +173 -0
- package/src/math/bankruptcy.ts +34 -0
- package/src/math/conversion.ts +13 -0
- package/src/math/exchangeStatus.ts +121 -0
- package/src/math/fuel.ts +70 -0
- package/src/math/funding.ts +342 -0
- package/src/math/insurance.ts +110 -0
- package/src/math/margin.ts +340 -0
- package/src/math/market.ts +336 -0
- package/src/math/oracles.ts +228 -0
- package/src/math/orders.ts +343 -0
- package/src/math/position.ts +324 -0
- package/src/math/repeg.ts +214 -0
- package/src/math/spotBalance.ts +630 -0
- package/src/math/spotMarket.ts +82 -0
- package/src/math/spotPosition.ts +184 -0
- package/src/math/state.ts +29 -0
- package/src/math/superStake.ts +525 -0
- package/src/math/tiers.ts +44 -0
- package/src/math/trade.ts +993 -0
- package/src/math/userStatus.ts +5 -0
- package/src/math/utils.ts +120 -0
- package/src/memcmp.ts +94 -0
- package/src/openbook/openbookV2FulfillmentConfigMap.ts +29 -0
- package/src/openbook/openbookV2Subscriber.ts +165 -0
- package/src/oracles/oracleClientCache.ts +25 -0
- package/src/oracles/oracleId.ts +28 -0
- package/src/oracles/prelaunchOracleClient.ts +37 -0
- package/src/oracles/pythClient.ts +85 -0
- package/src/oracles/pythLazerClient.ts +102 -0
- package/src/oracles/pythPullClient.ts +111 -0
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/strictOraclePrice.ts +19 -0
- package/src/oracles/switchboardClient.ts +77 -0
- package/src/oracles/switchboardOnDemandClient.ts +56 -0
- package/src/oracles/types.ts +23 -0
- package/src/orderParams.ts +79 -0
- package/src/orderSubscriber/OrderSubscriber.ts +249 -0
- package/src/orderSubscriber/PollingSubscription.ts +39 -0
- package/src/orderSubscriber/WebsocketSubscription.ts +119 -0
- package/src/orderSubscriber/grpcSubscription.ts +121 -0
- package/src/orderSubscriber/index.ts +2 -0
- package/src/orderSubscriber/types.ts +54 -0
- package/src/phoenix/phoenixFulfillmentConfigMap.ts +26 -0
- package/src/phoenix/phoenixSubscriber.ts +235 -0
- package/src/priorityFee/averageOverSlotsStrategy.ts +16 -0
- package/src/priorityFee/averageStrategy.ts +12 -0
- package/src/priorityFee/driftPriorityFeeMethod.ts +42 -0
- package/src/priorityFee/ewmaStrategy.ts +41 -0
- package/src/priorityFee/heliusPriorityFeeMethod.ts +57 -0
- package/src/priorityFee/index.ts +11 -0
- package/src/priorityFee/maxOverSlotsStrategy.ts +17 -0
- package/src/priorityFee/maxStrategy.ts +7 -0
- package/src/priorityFee/priorityFeeSubscriber.ts +251 -0
- package/src/priorityFee/priorityFeeSubscriberMap.ts +112 -0
- package/src/priorityFee/solanaPriorityFeeMethod.ts +34 -0
- package/src/priorityFee/types.ts +60 -0
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +169 -0
- package/src/serum/types.ts +17 -0
- package/src/slot/SlotSubscriber.ts +101 -0
- package/src/slot/SlothashSubscriber.ts +126 -0
- package/src/testClient.ts +41 -0
- package/src/token/index.ts +13 -0
- package/src/tokenFaucet.ts +269 -0
- package/src/tx/baseTxSender.ts +477 -0
- package/src/tx/blockhashFetcher/baseBlockhashFetcher.ts +19 -0
- package/src/tx/blockhashFetcher/cachedBlockhashFetcher.ts +90 -0
- package/src/tx/blockhashFetcher/types.ts +5 -0
- package/src/tx/fastSingleTxSender.ts +142 -0
- package/src/tx/forwardOnlyTxSender.ts +145 -0
- package/src/tx/priorityFeeCalculator.ts +117 -0
- package/src/tx/reportTransactionError.ts +159 -0
- package/src/tx/retryTxSender.ts +135 -0
- package/src/tx/txHandler.ts +737 -0
- package/src/tx/txParamProcessor.ts +155 -0
- package/src/tx/types.ts +71 -0
- package/src/tx/utils.ts +11 -0
- package/src/tx/whileValidTxSender.ts +265 -0
- package/src/types.ts +1386 -0
- package/src/user.ts +4054 -0
- package/src/userConfig.ts +32 -0
- package/src/userMap/PollingSubscription.ts +47 -0
- package/src/userMap/WebsocketSubscription.ts +84 -0
- package/src/userMap/grpcSubscription.ts +85 -0
- package/src/userMap/referrerMap.ts +267 -0
- package/src/userMap/userMap.ts +654 -0
- package/src/userMap/userMapConfig.ts +63 -0
- package/src/userMap/userStatsMap.ts +218 -0
- package/src/userName.ts +21 -0
- package/src/userStats.ts +174 -0
- package/src/userStatsConfig.ts +31 -0
- package/src/util/TransactionConfirmationManager.ts +292 -0
- package/src/util/chainClock.ts +41 -0
- package/src/util/computeUnits.ts +65 -0
- package/src/util/digest.ts +11 -0
- package/src/util/promiseTimeout.ts +14 -0
- package/src/util/pythOracleUtils.ts +136 -0
- package/src/util/tps.ts +27 -0
- package/src/wallet.ts +43 -0
- package/tests/amm/test.ts +2092 -0
- package/tests/auctions/test.ts +81 -0
- package/tests/bn/test.ts +341 -0
- package/tests/ci/idl.ts +101 -0
- package/tests/ci/verifyConstants.ts +278 -0
- package/tests/decode/phoenix.ts +71 -0
- package/tests/decode/test.ts +266 -0
- package/tests/decode/userAccountBufferStrings.ts +102 -0
- package/tests/dlob/helpers.ts +749 -0
- package/tests/dlob/test.ts +6623 -0
- package/tests/insurance/test.ts +40 -0
- package/tests/spot/test.ts +226 -0
- package/tests/subscriber/openbook.ts +62 -0
- package/tests/tx/TransactionConfirmationManager.test.ts +305 -0
- package/tests/tx/cachedBlockhashFetcher.test.ts +96 -0
- package/tests/tx/priorityFeeCalculator.ts +77 -0
- package/tests/tx/priorityFeeStrategy.ts +95 -0
- package/tests/user/helpers.ts +92 -0
- package/tests/user/test.ts +517 -0
- package/tsconfig.browser.json +13 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,630 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SpotMarketAccount,
|
|
3
|
+
SpotBalanceType,
|
|
4
|
+
isVariant,
|
|
5
|
+
MarginCategory,
|
|
6
|
+
} from '../types';
|
|
7
|
+
import { BN } from '@coral-xyz/anchor';
|
|
8
|
+
import {
|
|
9
|
+
SPOT_MARKET_UTILIZATION_PRECISION,
|
|
10
|
+
ONE,
|
|
11
|
+
TEN,
|
|
12
|
+
ZERO,
|
|
13
|
+
SPOT_MARKET_RATE_PRECISION,
|
|
14
|
+
SPOT_MARKET_WEIGHT_PRECISION,
|
|
15
|
+
ONE_YEAR,
|
|
16
|
+
AMM_RESERVE_PRECISION,
|
|
17
|
+
} from '../constants/numericConstants';
|
|
18
|
+
import {
|
|
19
|
+
calculateSizeDiscountAssetWeight,
|
|
20
|
+
calculateSizePremiumLiabilityWeight,
|
|
21
|
+
} from './margin';
|
|
22
|
+
import { OraclePriceData } from '../oracles/types';
|
|
23
|
+
import { PERCENTAGE_PRECISION } from '../constants/numericConstants';
|
|
24
|
+
import { divCeil } from './utils';
|
|
25
|
+
import { StrictOraclePrice } from '../oracles/strictOraclePrice';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Calculates the balance of a given token amount including any accumulated interest. This
|
|
29
|
+
* is the same as `SpotPosition.scaledBalance`.
|
|
30
|
+
*
|
|
31
|
+
* @param {BN} tokenAmount - the amount of tokens
|
|
32
|
+
* @param {SpotMarketAccount} spotMarket - the spot market account
|
|
33
|
+
* @param {SpotBalanceType} balanceType - the balance type ('deposit' or 'borrow')
|
|
34
|
+
* @return {BN} the calculated balance, scaled by `SPOT_MARKET_BALANCE_PRECISION`
|
|
35
|
+
*/
|
|
36
|
+
export function getBalance(
|
|
37
|
+
tokenAmount: BN,
|
|
38
|
+
spotMarket: SpotMarketAccount,
|
|
39
|
+
balanceType: SpotBalanceType
|
|
40
|
+
): BN {
|
|
41
|
+
const precisionIncrease = TEN.pow(new BN(19 - spotMarket.decimals));
|
|
42
|
+
|
|
43
|
+
const cumulativeInterest = isVariant(balanceType, 'deposit')
|
|
44
|
+
? spotMarket.cumulativeDepositInterest
|
|
45
|
+
: spotMarket.cumulativeBorrowInterest;
|
|
46
|
+
|
|
47
|
+
let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
|
|
48
|
+
|
|
49
|
+
if (!balance.eq(ZERO) && isVariant(balanceType, 'borrow')) {
|
|
50
|
+
balance = balance.add(ONE);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return balance;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Calculates the spot token amount including any accumulated interest.
|
|
58
|
+
*
|
|
59
|
+
* @param {BN} balanceAmount - The balance amount, typically from `SpotPosition.scaledBalance`
|
|
60
|
+
* @param {SpotMarketAccount} spotMarket - The spot market account details
|
|
61
|
+
* @param {SpotBalanceType} balanceType - The balance type to be used for calculation
|
|
62
|
+
* @returns {BN} The calculated token amount, scaled by `SpotMarketConfig.precision`
|
|
63
|
+
*/
|
|
64
|
+
export function getTokenAmount(
|
|
65
|
+
balanceAmount: BN,
|
|
66
|
+
spotMarket: SpotMarketAccount,
|
|
67
|
+
balanceType: SpotBalanceType
|
|
68
|
+
): BN {
|
|
69
|
+
const precisionDecrease = TEN.pow(new BN(19 - spotMarket.decimals));
|
|
70
|
+
|
|
71
|
+
if (isVariant(balanceType, 'deposit')) {
|
|
72
|
+
return balanceAmount
|
|
73
|
+
.mul(spotMarket.cumulativeDepositInterest)
|
|
74
|
+
.div(precisionDecrease);
|
|
75
|
+
} else {
|
|
76
|
+
return divCeil(
|
|
77
|
+
balanceAmount.mul(spotMarket.cumulativeBorrowInterest),
|
|
78
|
+
precisionDecrease
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Returns the signed (positive for deposit,negative for borrow) token amount based on the balance type.
|
|
85
|
+
*
|
|
86
|
+
* @param {BN} tokenAmount - The token amount to convert (from `getTokenAmount`)
|
|
87
|
+
* @param {SpotBalanceType} balanceType - The balance type to determine the sign of the token amount.
|
|
88
|
+
* @returns {BN} - The signed token amount, scaled by `SpotMarketConfig.precision`
|
|
89
|
+
*/
|
|
90
|
+
export function getSignedTokenAmount(
|
|
91
|
+
tokenAmount: BN,
|
|
92
|
+
balanceType: SpotBalanceType
|
|
93
|
+
): BN {
|
|
94
|
+
if (isVariant(balanceType, 'deposit')) {
|
|
95
|
+
return tokenAmount;
|
|
96
|
+
} else {
|
|
97
|
+
return tokenAmount.abs().neg();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Calculates the value of a given token amount using the worst of the provided oracle price and its TWAP.
|
|
103
|
+
*
|
|
104
|
+
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
|
|
105
|
+
* @param {number} spotDecimals - The number of decimals in the token.
|
|
106
|
+
* @param {StrictOraclePrice} strictOraclePrice - Contains oracle price and 5min twap.
|
|
107
|
+
* @return {BN} The calculated value of the given token amount, scaled by `PRICE_PRECISION`
|
|
108
|
+
*/
|
|
109
|
+
export function getStrictTokenValue(
|
|
110
|
+
tokenAmount: BN,
|
|
111
|
+
spotDecimals: number,
|
|
112
|
+
strictOraclePrice: StrictOraclePrice
|
|
113
|
+
): BN {
|
|
114
|
+
if (tokenAmount.eq(ZERO)) {
|
|
115
|
+
return ZERO;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let price;
|
|
119
|
+
if (tokenAmount.gte(ZERO)) {
|
|
120
|
+
price = strictOraclePrice.min();
|
|
121
|
+
} else {
|
|
122
|
+
price = strictOraclePrice.max();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const precisionDecrease = TEN.pow(new BN(spotDecimals));
|
|
126
|
+
|
|
127
|
+
return tokenAmount.mul(price).div(precisionDecrease);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Calculates the value of a given token amount in relation to an oracle price data
|
|
132
|
+
*
|
|
133
|
+
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
|
|
134
|
+
* @param {number} spotDecimals - The number of decimal places of the token.
|
|
135
|
+
* @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
|
|
136
|
+
* @return {BN} The value of the token based on the oracle, scaled by `PRICE_PRECISION`
|
|
137
|
+
*/
|
|
138
|
+
export function getTokenValue(
|
|
139
|
+
tokenAmount: BN,
|
|
140
|
+
spotDecimals: number,
|
|
141
|
+
oraclePriceData: Pick<OraclePriceData, 'price'>
|
|
142
|
+
): BN {
|
|
143
|
+
if (tokenAmount.eq(ZERO)) {
|
|
144
|
+
return ZERO;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const precisionDecrease = TEN.pow(new BN(spotDecimals));
|
|
148
|
+
|
|
149
|
+
return tokenAmount.mul(oraclePriceData.price).div(precisionDecrease);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function calculateAssetWeight(
|
|
153
|
+
balanceAmount: BN,
|
|
154
|
+
oraclePrice: BN,
|
|
155
|
+
spotMarket: SpotMarketAccount,
|
|
156
|
+
marginCategory: MarginCategory
|
|
157
|
+
): BN {
|
|
158
|
+
const sizePrecision = TEN.pow(new BN(spotMarket.decimals));
|
|
159
|
+
let sizeInAmmReservePrecision;
|
|
160
|
+
if (sizePrecision.gt(AMM_RESERVE_PRECISION)) {
|
|
161
|
+
sizeInAmmReservePrecision = balanceAmount.div(
|
|
162
|
+
sizePrecision.div(AMM_RESERVE_PRECISION)
|
|
163
|
+
);
|
|
164
|
+
} else {
|
|
165
|
+
sizeInAmmReservePrecision = balanceAmount
|
|
166
|
+
.mul(AMM_RESERVE_PRECISION)
|
|
167
|
+
.div(sizePrecision);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
let assetWeight;
|
|
171
|
+
|
|
172
|
+
switch (marginCategory) {
|
|
173
|
+
case 'Initial':
|
|
174
|
+
assetWeight = calculateSizeDiscountAssetWeight(
|
|
175
|
+
sizeInAmmReservePrecision,
|
|
176
|
+
new BN(spotMarket.imfFactor),
|
|
177
|
+
calculateScaledInitialAssetWeight(spotMarket, oraclePrice)
|
|
178
|
+
);
|
|
179
|
+
break;
|
|
180
|
+
case 'Maintenance':
|
|
181
|
+
assetWeight = calculateSizeDiscountAssetWeight(
|
|
182
|
+
sizeInAmmReservePrecision,
|
|
183
|
+
new BN(spotMarket.imfFactor),
|
|
184
|
+
new BN(spotMarket.maintenanceAssetWeight)
|
|
185
|
+
);
|
|
186
|
+
break;
|
|
187
|
+
default:
|
|
188
|
+
assetWeight = calculateScaledInitialAssetWeight(spotMarket, oraclePrice);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return assetWeight;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function calculateScaledInitialAssetWeight(
|
|
196
|
+
spotMarket: SpotMarketAccount,
|
|
197
|
+
oraclePrice: BN
|
|
198
|
+
): BN {
|
|
199
|
+
if (spotMarket.scaleInitialAssetWeightStart.eq(ZERO)) {
|
|
200
|
+
return new BN(spotMarket.initialAssetWeight);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const deposits = getTokenAmount(
|
|
204
|
+
spotMarket.depositBalance,
|
|
205
|
+
spotMarket,
|
|
206
|
+
SpotBalanceType.DEPOSIT
|
|
207
|
+
);
|
|
208
|
+
const depositsValue = getTokenValue(deposits, spotMarket.decimals, {
|
|
209
|
+
price: oraclePrice,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
if (depositsValue.lt(spotMarket.scaleInitialAssetWeightStart)) {
|
|
213
|
+
return new BN(spotMarket.initialAssetWeight);
|
|
214
|
+
} else {
|
|
215
|
+
return new BN(spotMarket.initialAssetWeight)
|
|
216
|
+
.mul(spotMarket.scaleInitialAssetWeightStart)
|
|
217
|
+
.div(depositsValue);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function calculateLiabilityWeight(
|
|
222
|
+
size: BN,
|
|
223
|
+
spotMarket: SpotMarketAccount,
|
|
224
|
+
marginCategory: MarginCategory
|
|
225
|
+
): BN {
|
|
226
|
+
const sizePrecision = TEN.pow(new BN(spotMarket.decimals));
|
|
227
|
+
let sizeInAmmReservePrecision;
|
|
228
|
+
if (sizePrecision.gt(AMM_RESERVE_PRECISION)) {
|
|
229
|
+
sizeInAmmReservePrecision = size.div(
|
|
230
|
+
sizePrecision.div(AMM_RESERVE_PRECISION)
|
|
231
|
+
);
|
|
232
|
+
} else {
|
|
233
|
+
sizeInAmmReservePrecision = size
|
|
234
|
+
.mul(AMM_RESERVE_PRECISION)
|
|
235
|
+
.div(sizePrecision);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
let liabilityWeight;
|
|
239
|
+
|
|
240
|
+
switch (marginCategory) {
|
|
241
|
+
case 'Initial':
|
|
242
|
+
liabilityWeight = calculateSizePremiumLiabilityWeight(
|
|
243
|
+
sizeInAmmReservePrecision,
|
|
244
|
+
new BN(spotMarket.imfFactor),
|
|
245
|
+
new BN(spotMarket.initialLiabilityWeight),
|
|
246
|
+
SPOT_MARKET_WEIGHT_PRECISION
|
|
247
|
+
);
|
|
248
|
+
break;
|
|
249
|
+
case 'Maintenance':
|
|
250
|
+
liabilityWeight = calculateSizePremiumLiabilityWeight(
|
|
251
|
+
sizeInAmmReservePrecision,
|
|
252
|
+
new BN(spotMarket.imfFactor),
|
|
253
|
+
new BN(spotMarket.maintenanceLiabilityWeight),
|
|
254
|
+
SPOT_MARKET_WEIGHT_PRECISION
|
|
255
|
+
);
|
|
256
|
+
break;
|
|
257
|
+
default:
|
|
258
|
+
liabilityWeight = new BN(spotMarket.initialLiabilityWeight);
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return liabilityWeight;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function calculateUtilization(
|
|
266
|
+
bank: SpotMarketAccount,
|
|
267
|
+
delta = ZERO
|
|
268
|
+
): BN {
|
|
269
|
+
let tokenDepositAmount = getTokenAmount(
|
|
270
|
+
bank.depositBalance,
|
|
271
|
+
bank,
|
|
272
|
+
SpotBalanceType.DEPOSIT
|
|
273
|
+
);
|
|
274
|
+
let tokenBorrowAmount = getTokenAmount(
|
|
275
|
+
bank.borrowBalance,
|
|
276
|
+
bank,
|
|
277
|
+
SpotBalanceType.BORROW
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
if (delta.gt(ZERO)) {
|
|
281
|
+
tokenDepositAmount = tokenDepositAmount.add(delta);
|
|
282
|
+
} else if (delta.lt(ZERO)) {
|
|
283
|
+
tokenBorrowAmount = tokenBorrowAmount.add(delta.abs());
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
let utilization: BN;
|
|
287
|
+
if (tokenBorrowAmount.eq(ZERO) && tokenDepositAmount.eq(ZERO)) {
|
|
288
|
+
utilization = ZERO;
|
|
289
|
+
} else if (tokenDepositAmount.eq(ZERO)) {
|
|
290
|
+
utilization = SPOT_MARKET_UTILIZATION_PRECISION;
|
|
291
|
+
} else {
|
|
292
|
+
utilization = tokenBorrowAmount
|
|
293
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
294
|
+
.div(tokenDepositAmount);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return utilization;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* calculates max borrow amount where rate would stay below targetBorrowRate
|
|
302
|
+
* @param spotMarketAccount
|
|
303
|
+
* @param targetBorrowRate
|
|
304
|
+
* @returns : Precision: TOKEN DECIMALS
|
|
305
|
+
*/
|
|
306
|
+
export function calculateSpotMarketBorrowCapacity(
|
|
307
|
+
spotMarketAccount: SpotMarketAccount,
|
|
308
|
+
targetBorrowRate: BN
|
|
309
|
+
): { totalCapacity: BN; remainingCapacity: BN } {
|
|
310
|
+
const currentBorrowRate = calculateBorrowRate(spotMarketAccount);
|
|
311
|
+
|
|
312
|
+
const tokenDepositAmount = getTokenAmount(
|
|
313
|
+
spotMarketAccount.depositBalance,
|
|
314
|
+
spotMarketAccount,
|
|
315
|
+
SpotBalanceType.DEPOSIT
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
const tokenBorrowAmount = getTokenAmount(
|
|
319
|
+
spotMarketAccount.borrowBalance,
|
|
320
|
+
spotMarketAccount,
|
|
321
|
+
SpotBalanceType.BORROW
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
let targetUtilization;
|
|
325
|
+
// target utilization past mid point
|
|
326
|
+
if (targetBorrowRate.gte(new BN(spotMarketAccount.optimalBorrowRate))) {
|
|
327
|
+
const borrowRateSlope = new BN(
|
|
328
|
+
spotMarketAccount.maxBorrowRate - spotMarketAccount.optimalBorrowRate
|
|
329
|
+
)
|
|
330
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
331
|
+
.div(
|
|
332
|
+
SPOT_MARKET_UTILIZATION_PRECISION.sub(
|
|
333
|
+
new BN(spotMarketAccount.optimalUtilization)
|
|
334
|
+
)
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
const surplusTargetUtilization = targetBorrowRate
|
|
338
|
+
.sub(new BN(spotMarketAccount.optimalBorrowRate))
|
|
339
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
340
|
+
.div(borrowRateSlope);
|
|
341
|
+
|
|
342
|
+
targetUtilization = surplusTargetUtilization.add(
|
|
343
|
+
new BN(spotMarketAccount.optimalUtilization)
|
|
344
|
+
);
|
|
345
|
+
} else {
|
|
346
|
+
const borrowRateSlope = new BN(spotMarketAccount.optimalBorrowRate)
|
|
347
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
348
|
+
.div(new BN(spotMarketAccount.optimalUtilization));
|
|
349
|
+
|
|
350
|
+
targetUtilization = targetBorrowRate
|
|
351
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
352
|
+
.div(borrowRateSlope);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const totalCapacity = tokenDepositAmount
|
|
356
|
+
.mul(targetUtilization)
|
|
357
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION);
|
|
358
|
+
|
|
359
|
+
let remainingCapacity;
|
|
360
|
+
if (currentBorrowRate.gte(targetBorrowRate)) {
|
|
361
|
+
remainingCapacity = ZERO;
|
|
362
|
+
} else {
|
|
363
|
+
remainingCapacity = BN.max(ZERO, totalCapacity.sub(tokenBorrowAmount));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (spotMarketAccount.maxTokenBorrowsFraction > 0) {
|
|
367
|
+
const maxTokenBorrows = spotMarketAccount.maxTokenDeposits
|
|
368
|
+
.mul(new BN(spotMarketAccount.maxTokenBorrowsFraction))
|
|
369
|
+
.divn(10000);
|
|
370
|
+
|
|
371
|
+
remainingCapacity = BN.min(
|
|
372
|
+
remainingCapacity,
|
|
373
|
+
BN.max(ZERO, maxTokenBorrows.sub(tokenBorrowAmount))
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return { totalCapacity, remainingCapacity };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export function calculateInterestRate(
|
|
381
|
+
bank: SpotMarketAccount,
|
|
382
|
+
delta = ZERO,
|
|
383
|
+
currentUtilization: BN = null
|
|
384
|
+
): BN {
|
|
385
|
+
const utilization = currentUtilization || calculateUtilization(bank, delta);
|
|
386
|
+
let interestRate: BN;
|
|
387
|
+
if (utilization.gt(new BN(bank.optimalUtilization))) {
|
|
388
|
+
const surplusUtilization = utilization.sub(new BN(bank.optimalUtilization));
|
|
389
|
+
const borrowRateSlope = new BN(bank.maxBorrowRate - bank.optimalBorrowRate)
|
|
390
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
391
|
+
.div(
|
|
392
|
+
SPOT_MARKET_UTILIZATION_PRECISION.sub(new BN(bank.optimalUtilization))
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
interestRate = new BN(bank.optimalBorrowRate).add(
|
|
396
|
+
surplusUtilization
|
|
397
|
+
.mul(borrowRateSlope)
|
|
398
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
399
|
+
);
|
|
400
|
+
} else {
|
|
401
|
+
const borrowRateSlope = new BN(bank.optimalBorrowRate)
|
|
402
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
403
|
+
.div(new BN(bank.optimalUtilization));
|
|
404
|
+
|
|
405
|
+
interestRate = utilization
|
|
406
|
+
.mul(borrowRateSlope)
|
|
407
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return BN.max(
|
|
411
|
+
interestRate,
|
|
412
|
+
new BN(bank.minBorrowRate).mul(PERCENTAGE_PRECISION.divn(200))
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export function calculateDepositRate(
|
|
417
|
+
bank: SpotMarketAccount,
|
|
418
|
+
delta = ZERO,
|
|
419
|
+
currentUtilization: BN = null
|
|
420
|
+
): BN {
|
|
421
|
+
// positive delta => adding to deposit
|
|
422
|
+
// negative delta => adding to borrow
|
|
423
|
+
|
|
424
|
+
const utilization = currentUtilization || calculateUtilization(bank, delta);
|
|
425
|
+
const borrowRate = calculateBorrowRate(bank, delta, utilization);
|
|
426
|
+
const depositRate = borrowRate
|
|
427
|
+
.mul(PERCENTAGE_PRECISION.sub(new BN(bank.insuranceFund.totalFactor)))
|
|
428
|
+
.mul(utilization)
|
|
429
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
430
|
+
.div(PERCENTAGE_PRECISION);
|
|
431
|
+
return depositRate;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export function calculateBorrowRate(
|
|
435
|
+
bank: SpotMarketAccount,
|
|
436
|
+
delta = ZERO,
|
|
437
|
+
currentUtilization: BN = null
|
|
438
|
+
): BN {
|
|
439
|
+
return calculateInterestRate(bank, delta, currentUtilization);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export function calculateInterestAccumulated(
|
|
443
|
+
bank: SpotMarketAccount,
|
|
444
|
+
now: BN
|
|
445
|
+
): { borrowInterest: BN; depositInterest: BN } {
|
|
446
|
+
const interestRate = calculateInterestRate(bank);
|
|
447
|
+
|
|
448
|
+
const timeSinceLastUpdate = now.sub(bank.lastInterestTs);
|
|
449
|
+
|
|
450
|
+
const modifiedBorrowRate = interestRate.mul(timeSinceLastUpdate);
|
|
451
|
+
|
|
452
|
+
const utilization = calculateUtilization(bank);
|
|
453
|
+
|
|
454
|
+
const modifiedDepositRate = modifiedBorrowRate
|
|
455
|
+
.mul(utilization)
|
|
456
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION);
|
|
457
|
+
|
|
458
|
+
const borrowInterest = bank.cumulativeBorrowInterest
|
|
459
|
+
.mul(modifiedBorrowRate)
|
|
460
|
+
.div(ONE_YEAR)
|
|
461
|
+
.div(SPOT_MARKET_RATE_PRECISION)
|
|
462
|
+
.add(ONE);
|
|
463
|
+
const depositInterest = bank.cumulativeDepositInterest
|
|
464
|
+
.mul(modifiedDepositRate)
|
|
465
|
+
.div(ONE_YEAR)
|
|
466
|
+
.div(SPOT_MARKET_RATE_PRECISION);
|
|
467
|
+
|
|
468
|
+
return { borrowInterest, depositInterest };
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export function calculateTokenUtilizationLimits(
|
|
472
|
+
depositTokenAmount: BN,
|
|
473
|
+
borrowTokenAmount: BN,
|
|
474
|
+
spotMarket: SpotMarketAccount
|
|
475
|
+
): {
|
|
476
|
+
minDepositTokensForUtilization: BN;
|
|
477
|
+
maxBorrowTokensForUtilization: BN;
|
|
478
|
+
} {
|
|
479
|
+
// Calculates the allowable minimum deposit and maximum borrow amounts for immediate withdrawal based on market utilization.
|
|
480
|
+
// First, it determines a maximum withdrawal utilization from the market's target and historic utilization.
|
|
481
|
+
// Then, it deduces corresponding deposit/borrow amounts.
|
|
482
|
+
// Note: For deposit sizes below the guard threshold, withdrawals aren't blocked.
|
|
483
|
+
|
|
484
|
+
const maxWithdrawUtilization = BN.max(
|
|
485
|
+
new BN(spotMarket.optimalUtilization),
|
|
486
|
+
spotMarket.utilizationTwap.add(
|
|
487
|
+
SPOT_MARKET_UTILIZATION_PRECISION.sub(spotMarket.utilizationTwap).div(
|
|
488
|
+
new BN(2)
|
|
489
|
+
)
|
|
490
|
+
)
|
|
491
|
+
);
|
|
492
|
+
|
|
493
|
+
let minDepositTokensForUtilization = borrowTokenAmount
|
|
494
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
495
|
+
.div(maxWithdrawUtilization);
|
|
496
|
+
|
|
497
|
+
// don't block withdraws for deposit sizes below guard threshold
|
|
498
|
+
minDepositTokensForUtilization = BN.min(
|
|
499
|
+
minDepositTokensForUtilization,
|
|
500
|
+
depositTokenAmount.sub(spotMarket.withdrawGuardThreshold)
|
|
501
|
+
);
|
|
502
|
+
|
|
503
|
+
let maxBorrowTokensForUtilization = maxWithdrawUtilization
|
|
504
|
+
.mul(depositTokenAmount)
|
|
505
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION);
|
|
506
|
+
|
|
507
|
+
maxBorrowTokensForUtilization = BN.max(
|
|
508
|
+
spotMarket.withdrawGuardThreshold,
|
|
509
|
+
maxBorrowTokensForUtilization
|
|
510
|
+
);
|
|
511
|
+
|
|
512
|
+
return {
|
|
513
|
+
minDepositTokensForUtilization,
|
|
514
|
+
maxBorrowTokensForUtilization,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export function calculateWithdrawLimit(
|
|
519
|
+
spotMarket: SpotMarketAccount,
|
|
520
|
+
now: BN
|
|
521
|
+
): {
|
|
522
|
+
borrowLimit: BN;
|
|
523
|
+
withdrawLimit: BN;
|
|
524
|
+
minDepositAmount: BN;
|
|
525
|
+
maxBorrowAmount: BN;
|
|
526
|
+
currentDepositAmount;
|
|
527
|
+
currentBorrowAmount;
|
|
528
|
+
} {
|
|
529
|
+
const marketDepositTokenAmount = getTokenAmount(
|
|
530
|
+
spotMarket.depositBalance,
|
|
531
|
+
spotMarket,
|
|
532
|
+
SpotBalanceType.DEPOSIT
|
|
533
|
+
);
|
|
534
|
+
const marketBorrowTokenAmount = getTokenAmount(
|
|
535
|
+
spotMarket.borrowBalance,
|
|
536
|
+
spotMarket,
|
|
537
|
+
SpotBalanceType.BORROW
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
const twentyFourHours = new BN(60 * 60 * 24);
|
|
541
|
+
const sinceLast = now.sub(spotMarket.lastTwapTs);
|
|
542
|
+
const sinceStart = BN.max(ZERO, twentyFourHours.sub(sinceLast));
|
|
543
|
+
const borrowTokenTwapLive = spotMarket.borrowTokenTwap
|
|
544
|
+
.mul(sinceStart)
|
|
545
|
+
.add(marketBorrowTokenAmount.mul(sinceLast))
|
|
546
|
+
.div(sinceLast.add(sinceStart));
|
|
547
|
+
|
|
548
|
+
const depositTokenTwapLive = spotMarket.depositTokenTwap
|
|
549
|
+
.mul(sinceStart)
|
|
550
|
+
.add(marketDepositTokenAmount.mul(sinceLast))
|
|
551
|
+
.div(sinceLast.add(sinceStart));
|
|
552
|
+
|
|
553
|
+
const lesserDepositAmount = BN.min(
|
|
554
|
+
marketDepositTokenAmount,
|
|
555
|
+
depositTokenTwapLive
|
|
556
|
+
);
|
|
557
|
+
const maxBorrowTokensTwap = BN.max(
|
|
558
|
+
spotMarket.withdrawGuardThreshold,
|
|
559
|
+
BN.min(
|
|
560
|
+
BN.max(
|
|
561
|
+
marketDepositTokenAmount.div(new BN(6)),
|
|
562
|
+
borrowTokenTwapLive.add(lesserDepositAmount.div(new BN(10)))
|
|
563
|
+
),
|
|
564
|
+
lesserDepositAmount.sub(lesserDepositAmount.div(new BN(5)))
|
|
565
|
+
)
|
|
566
|
+
); // between ~15-80% utilization with friction on twap
|
|
567
|
+
|
|
568
|
+
const minDepositTokensTwap = depositTokenTwapLive.sub(
|
|
569
|
+
BN.max(
|
|
570
|
+
depositTokenTwapLive.div(new BN(4)),
|
|
571
|
+
BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)
|
|
572
|
+
)
|
|
573
|
+
);
|
|
574
|
+
|
|
575
|
+
const { minDepositTokensForUtilization, maxBorrowTokensForUtilization } =
|
|
576
|
+
calculateTokenUtilizationLimits(
|
|
577
|
+
marketDepositTokenAmount,
|
|
578
|
+
marketBorrowTokenAmount,
|
|
579
|
+
spotMarket
|
|
580
|
+
);
|
|
581
|
+
|
|
582
|
+
const minDepositTokens = BN.max(
|
|
583
|
+
minDepositTokensForUtilization,
|
|
584
|
+
minDepositTokensTwap
|
|
585
|
+
);
|
|
586
|
+
|
|
587
|
+
let maxBorrowTokens = BN.min(
|
|
588
|
+
maxBorrowTokensForUtilization,
|
|
589
|
+
maxBorrowTokensTwap
|
|
590
|
+
);
|
|
591
|
+
|
|
592
|
+
const withdrawLimit = BN.max(
|
|
593
|
+
marketDepositTokenAmount.sub(minDepositTokens),
|
|
594
|
+
ZERO
|
|
595
|
+
);
|
|
596
|
+
|
|
597
|
+
let borrowLimit = maxBorrowTokens.sub(marketBorrowTokenAmount);
|
|
598
|
+
|
|
599
|
+
borrowLimit = BN.min(
|
|
600
|
+
borrowLimit,
|
|
601
|
+
marketDepositTokenAmount.sub(marketBorrowTokenAmount)
|
|
602
|
+
);
|
|
603
|
+
|
|
604
|
+
if (spotMarket.maxTokenBorrowsFraction > 0) {
|
|
605
|
+
const maxTokenBorrowsByFraction = spotMarket.maxTokenDeposits
|
|
606
|
+
.mul(new BN(spotMarket.maxTokenBorrowsFraction))
|
|
607
|
+
.divn(10000);
|
|
608
|
+
|
|
609
|
+
const trueMaxBorrowTokensAvailable = maxTokenBorrowsByFraction.sub(
|
|
610
|
+
marketBorrowTokenAmount
|
|
611
|
+
);
|
|
612
|
+
|
|
613
|
+
maxBorrowTokens = BN.min(maxBorrowTokens, trueMaxBorrowTokensAvailable);
|
|
614
|
+
|
|
615
|
+
borrowLimit = BN.min(borrowLimit, maxBorrowTokens);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
if (withdrawLimit.eq(ZERO)) {
|
|
619
|
+
borrowLimit = ZERO;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
return {
|
|
623
|
+
borrowLimit,
|
|
624
|
+
withdrawLimit,
|
|
625
|
+
maxBorrowAmount: maxBorrowTokens,
|
|
626
|
+
minDepositAmount: minDepositTokens,
|
|
627
|
+
currentDepositAmount: marketDepositTokenAmount,
|
|
628
|
+
currentBorrowAmount: marketBorrowTokenAmount,
|
|
629
|
+
};
|
|
630
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { BN } from '@coral-xyz/anchor';
|
|
2
|
+
import {
|
|
3
|
+
isVariant,
|
|
4
|
+
MarginCategory,
|
|
5
|
+
SpotBalanceType,
|
|
6
|
+
SpotMarketAccount,
|
|
7
|
+
} from '../types';
|
|
8
|
+
import {
|
|
9
|
+
calculateAssetWeight,
|
|
10
|
+
calculateLiabilityWeight,
|
|
11
|
+
getTokenAmount,
|
|
12
|
+
} from './spotBalance';
|
|
13
|
+
import { MARGIN_PRECISION, ZERO } from '../constants/numericConstants';
|
|
14
|
+
import { numberToSafeBN } from './utils';
|
|
15
|
+
|
|
16
|
+
export function castNumberToSpotPrecision(
|
|
17
|
+
value: number | BN,
|
|
18
|
+
spotMarket: SpotMarketAccount
|
|
19
|
+
): BN {
|
|
20
|
+
if (typeof value === 'number') {
|
|
21
|
+
return numberToSafeBN(value, new BN(Math.pow(10, spotMarket.decimals)));
|
|
22
|
+
} else {
|
|
23
|
+
return value.mul(new BN(Math.pow(10, spotMarket.decimals)));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function calculateSpotMarketMarginRatio(
|
|
28
|
+
market: SpotMarketAccount,
|
|
29
|
+
oraclePrice: BN,
|
|
30
|
+
marginCategory: MarginCategory,
|
|
31
|
+
size: BN,
|
|
32
|
+
balanceType: SpotBalanceType,
|
|
33
|
+
customMarginRatio = 0
|
|
34
|
+
): number {
|
|
35
|
+
let marginRatio;
|
|
36
|
+
|
|
37
|
+
if (isVariant(balanceType, 'deposit')) {
|
|
38
|
+
const assetWeight = calculateAssetWeight(
|
|
39
|
+
size,
|
|
40
|
+
oraclePrice,
|
|
41
|
+
market,
|
|
42
|
+
marginCategory
|
|
43
|
+
);
|
|
44
|
+
marginRatio = MARGIN_PRECISION.sub(assetWeight).toNumber();
|
|
45
|
+
} else {
|
|
46
|
+
const liabilityWeight = calculateLiabilityWeight(
|
|
47
|
+
size,
|
|
48
|
+
market,
|
|
49
|
+
marginCategory
|
|
50
|
+
);
|
|
51
|
+
marginRatio = liabilityWeight.sub(MARGIN_PRECISION).toNumber();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (marginCategory === 'Initial') {
|
|
55
|
+
// use lowest leverage between max allowed and optional user custom max
|
|
56
|
+
return Math.max(marginRatio, customMarginRatio);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return marginRatio;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Returns the maximum remaining deposit that can be made to the spot market. If the maxTokenDeposits on the market is zero then there is no limit and this function will also return zero. (so that needs to be checked)
|
|
64
|
+
* @param market
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
export function calculateMaxRemainingDeposit(market: SpotMarketAccount) {
|
|
68
|
+
const marketMaxTokenDeposits = market.maxTokenDeposits;
|
|
69
|
+
|
|
70
|
+
if (marketMaxTokenDeposits.eq(ZERO)) {
|
|
71
|
+
// If the maxTokenDeposits is set to zero then that means there is no limit. Return the largest number we can to represent infinite available deposit.
|
|
72
|
+
return ZERO;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const totalDepositsTokenAmount = getTokenAmount(
|
|
76
|
+
market.depositBalance,
|
|
77
|
+
market,
|
|
78
|
+
SpotBalanceType.DEPOSIT
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
return marketMaxTokenDeposits.sub(totalDepositsTokenAmount);
|
|
82
|
+
}
|