@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,993 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MarketType,
|
|
3
|
+
PerpMarketAccount,
|
|
4
|
+
PositionDirection,
|
|
5
|
+
SpotMarketAccount,
|
|
6
|
+
UserStatsAccount,
|
|
7
|
+
} from '../types';
|
|
8
|
+
import { BN } from '@coral-xyz/anchor';
|
|
9
|
+
import { assert } from '../assert/assert';
|
|
10
|
+
import {
|
|
11
|
+
PRICE_PRECISION,
|
|
12
|
+
PEG_PRECISION,
|
|
13
|
+
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
14
|
+
ZERO,
|
|
15
|
+
BASE_PRECISION,
|
|
16
|
+
BN_MAX,
|
|
17
|
+
} from '../constants/numericConstants';
|
|
18
|
+
import {
|
|
19
|
+
calculateBidPrice,
|
|
20
|
+
calculateAskPrice,
|
|
21
|
+
calculateReservePrice,
|
|
22
|
+
} from './market';
|
|
23
|
+
import {
|
|
24
|
+
calculateAmmReservesAfterSwap,
|
|
25
|
+
calculatePrice,
|
|
26
|
+
getSwapDirection,
|
|
27
|
+
AssetType,
|
|
28
|
+
calculateUpdatedAMMSpreadReserves,
|
|
29
|
+
calculateQuoteAssetAmountSwapped,
|
|
30
|
+
calculateMarketOpenBidAsk,
|
|
31
|
+
} from './amm';
|
|
32
|
+
import { squareRootBN } from './utils';
|
|
33
|
+
import { isVariant } from '../types';
|
|
34
|
+
import { OraclePriceData } from '../oracles/types';
|
|
35
|
+
import { DLOB } from '../dlob/DLOB';
|
|
36
|
+
import { PublicKey } from '@solana/web3.js';
|
|
37
|
+
import { Orderbook } from '@project-serum/serum';
|
|
38
|
+
import { L2OrderBook } from '../dlob/orderBookLevels';
|
|
39
|
+
|
|
40
|
+
const MAXPCT = new BN(1000); //percentage units are [0,1000] => [0,1]
|
|
41
|
+
|
|
42
|
+
export type PriceImpactUnit =
|
|
43
|
+
| 'entryPrice'
|
|
44
|
+
| 'maxPrice'
|
|
45
|
+
| 'priceDelta'
|
|
46
|
+
| 'priceDeltaAsNumber'
|
|
47
|
+
| 'pctAvg'
|
|
48
|
+
| 'pctMax'
|
|
49
|
+
| 'quoteAssetAmount'
|
|
50
|
+
| 'quoteAssetAmountPeg'
|
|
51
|
+
| 'acquiredBaseAssetAmount'
|
|
52
|
+
| 'acquiredQuoteAssetAmount'
|
|
53
|
+
| 'all';
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Calculates avg/max slippage (price impact) for candidate trade
|
|
57
|
+
*
|
|
58
|
+
* @deprecated use calculateEstimatedPerpEntryPrice instead
|
|
59
|
+
*
|
|
60
|
+
* @param direction
|
|
61
|
+
* @param amount
|
|
62
|
+
* @param market
|
|
63
|
+
* @param inputAssetType which asset is being traded
|
|
64
|
+
* @param useSpread whether to consider spread with calculating slippage
|
|
65
|
+
* @return [pctAvgSlippage, pctMaxSlippage, entryPrice, newPrice]
|
|
66
|
+
*
|
|
67
|
+
* 'pctAvgSlippage' => the percentage change to entryPrice (average est slippage in execution) : Precision PRICE_PRECISION
|
|
68
|
+
*
|
|
69
|
+
* 'pctMaxSlippage' => the percentage change to maxPrice (highest est slippage in execution) : Precision PRICE_PRECISION
|
|
70
|
+
*
|
|
71
|
+
* 'entryPrice' => the average price of the trade : Precision PRICE_PRECISION
|
|
72
|
+
*
|
|
73
|
+
* 'newPrice' => the price of the asset after the trade : Precision PRICE_PRECISION
|
|
74
|
+
*/
|
|
75
|
+
export function calculateTradeSlippage(
|
|
76
|
+
direction: PositionDirection,
|
|
77
|
+
amount: BN,
|
|
78
|
+
market: PerpMarketAccount,
|
|
79
|
+
inputAssetType: AssetType = 'quote',
|
|
80
|
+
oraclePriceData: OraclePriceData,
|
|
81
|
+
useSpread = true
|
|
82
|
+
): [BN, BN, BN, BN] {
|
|
83
|
+
let oldPrice: BN;
|
|
84
|
+
|
|
85
|
+
if (useSpread && market.amm.baseSpread > 0) {
|
|
86
|
+
if (isVariant(direction, 'long')) {
|
|
87
|
+
oldPrice = calculateAskPrice(market, oraclePriceData);
|
|
88
|
+
} else {
|
|
89
|
+
oldPrice = calculateBidPrice(market, oraclePriceData);
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
oldPrice = calculateReservePrice(market, oraclePriceData);
|
|
93
|
+
}
|
|
94
|
+
if (amount.eq(ZERO)) {
|
|
95
|
+
return [ZERO, ZERO, oldPrice, oldPrice];
|
|
96
|
+
}
|
|
97
|
+
const [acquiredBaseReserve, acquiredQuoteReserve, acquiredQuoteAssetAmount] =
|
|
98
|
+
calculateTradeAcquiredAmounts(
|
|
99
|
+
direction,
|
|
100
|
+
amount,
|
|
101
|
+
market,
|
|
102
|
+
inputAssetType,
|
|
103
|
+
oraclePriceData,
|
|
104
|
+
useSpread
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const entryPrice = acquiredQuoteAssetAmount
|
|
108
|
+
.mul(AMM_TO_QUOTE_PRECISION_RATIO)
|
|
109
|
+
.mul(PRICE_PRECISION)
|
|
110
|
+
.div(acquiredBaseReserve.abs());
|
|
111
|
+
|
|
112
|
+
let amm: Parameters<typeof calculateAmmReservesAfterSwap>[0];
|
|
113
|
+
if (useSpread && market.amm.baseSpread > 0) {
|
|
114
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } =
|
|
115
|
+
calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
116
|
+
amm = {
|
|
117
|
+
baseAssetReserve,
|
|
118
|
+
quoteAssetReserve,
|
|
119
|
+
sqrtK: sqrtK,
|
|
120
|
+
pegMultiplier: newPeg,
|
|
121
|
+
};
|
|
122
|
+
} else {
|
|
123
|
+
amm = market.amm;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const newPrice = calculatePrice(
|
|
127
|
+
amm.baseAssetReserve.sub(acquiredBaseReserve),
|
|
128
|
+
amm.quoteAssetReserve.sub(acquiredQuoteReserve),
|
|
129
|
+
amm.pegMultiplier
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
if (direction == PositionDirection.SHORT) {
|
|
133
|
+
assert(newPrice.lte(oldPrice));
|
|
134
|
+
} else {
|
|
135
|
+
assert(oldPrice.lte(newPrice));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const pctMaxSlippage = newPrice
|
|
139
|
+
.sub(oldPrice)
|
|
140
|
+
.mul(PRICE_PRECISION)
|
|
141
|
+
.div(oldPrice)
|
|
142
|
+
.abs();
|
|
143
|
+
const pctAvgSlippage = entryPrice
|
|
144
|
+
.sub(oldPrice)
|
|
145
|
+
.mul(PRICE_PRECISION)
|
|
146
|
+
.div(oldPrice)
|
|
147
|
+
.abs();
|
|
148
|
+
|
|
149
|
+
return [pctAvgSlippage, pctMaxSlippage, entryPrice, newPrice];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Calculates acquired amounts for trade executed
|
|
154
|
+
* @param direction
|
|
155
|
+
* @param amount
|
|
156
|
+
* @param market
|
|
157
|
+
* @param inputAssetType
|
|
158
|
+
* @param useSpread
|
|
159
|
+
* @return
|
|
160
|
+
* | 'acquiredBase' => positive/negative change in user's base : BN AMM_RESERVE_PRECISION
|
|
161
|
+
* | 'acquiredQuote' => positive/negative change in user's quote : BN TODO-PRECISION
|
|
162
|
+
*/
|
|
163
|
+
export function calculateTradeAcquiredAmounts(
|
|
164
|
+
direction: PositionDirection,
|
|
165
|
+
amount: BN,
|
|
166
|
+
market: PerpMarketAccount,
|
|
167
|
+
inputAssetType: AssetType = 'quote',
|
|
168
|
+
oraclePriceData: OraclePriceData,
|
|
169
|
+
useSpread = true
|
|
170
|
+
): [BN, BN, BN] {
|
|
171
|
+
if (amount.eq(ZERO)) {
|
|
172
|
+
return [ZERO, ZERO, ZERO];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const swapDirection = getSwapDirection(inputAssetType, direction);
|
|
176
|
+
|
|
177
|
+
let amm: Parameters<typeof calculateAmmReservesAfterSwap>[0];
|
|
178
|
+
if (useSpread && market.amm.baseSpread > 0) {
|
|
179
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } =
|
|
180
|
+
calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
181
|
+
amm = {
|
|
182
|
+
baseAssetReserve,
|
|
183
|
+
quoteAssetReserve,
|
|
184
|
+
sqrtK: sqrtK,
|
|
185
|
+
pegMultiplier: newPeg,
|
|
186
|
+
};
|
|
187
|
+
} else {
|
|
188
|
+
amm = market.amm;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const [newQuoteAssetReserve, newBaseAssetReserve] =
|
|
192
|
+
calculateAmmReservesAfterSwap(amm, inputAssetType, amount, swapDirection);
|
|
193
|
+
|
|
194
|
+
const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
195
|
+
const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
196
|
+
const acquiredQuoteAssetAmount = calculateQuoteAssetAmountSwapped(
|
|
197
|
+
acquiredQuote.abs(),
|
|
198
|
+
amm.pegMultiplier,
|
|
199
|
+
swapDirection
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
return [acquiredBase, acquiredQuote, acquiredQuoteAssetAmount];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* calculateTargetPriceTrade
|
|
207
|
+
* simple function for finding arbitraging trades
|
|
208
|
+
*
|
|
209
|
+
* @deprecated
|
|
210
|
+
*
|
|
211
|
+
* @param market
|
|
212
|
+
* @param targetPrice
|
|
213
|
+
* @param pct optional default is 100% gap filling, can set smaller.
|
|
214
|
+
* @param outputAssetType which asset to trade.
|
|
215
|
+
* @param useSpread whether or not to consider the spread when calculating the trade size
|
|
216
|
+
* @returns trade direction/size in order to push price to a targetPrice,
|
|
217
|
+
*
|
|
218
|
+
* [
|
|
219
|
+
* direction => direction of trade required, PositionDirection
|
|
220
|
+
* tradeSize => size of trade required, TODO-PRECISION
|
|
221
|
+
* entryPrice => the entry price for the trade, PRICE_PRECISION
|
|
222
|
+
* targetPrice => the target price PRICE_PRECISION
|
|
223
|
+
* ]
|
|
224
|
+
*/
|
|
225
|
+
export function calculateTargetPriceTrade(
|
|
226
|
+
market: PerpMarketAccount,
|
|
227
|
+
targetPrice: BN,
|
|
228
|
+
pct: BN = MAXPCT,
|
|
229
|
+
outputAssetType: AssetType = 'quote',
|
|
230
|
+
oraclePriceData?: OraclePriceData,
|
|
231
|
+
useSpread = true
|
|
232
|
+
): [PositionDirection, BN, BN, BN] {
|
|
233
|
+
assert(market.amm.baseAssetReserve.gt(ZERO));
|
|
234
|
+
assert(targetPrice.gt(ZERO));
|
|
235
|
+
assert(pct.lte(MAXPCT) && pct.gt(ZERO));
|
|
236
|
+
|
|
237
|
+
const reservePriceBefore = calculateReservePrice(market, oraclePriceData);
|
|
238
|
+
const bidPriceBefore = calculateBidPrice(market, oraclePriceData);
|
|
239
|
+
const askPriceBefore = calculateAskPrice(market, oraclePriceData);
|
|
240
|
+
|
|
241
|
+
let direction;
|
|
242
|
+
if (targetPrice.gt(reservePriceBefore)) {
|
|
243
|
+
const priceGap = targetPrice.sub(reservePriceBefore);
|
|
244
|
+
const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
245
|
+
targetPrice = reservePriceBefore.add(priceGapScaled);
|
|
246
|
+
direction = PositionDirection.LONG;
|
|
247
|
+
} else {
|
|
248
|
+
const priceGap = reservePriceBefore.sub(targetPrice);
|
|
249
|
+
const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
250
|
+
targetPrice = reservePriceBefore.sub(priceGapScaled);
|
|
251
|
+
direction = PositionDirection.SHORT;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
let tradeSize;
|
|
255
|
+
let baseSize;
|
|
256
|
+
|
|
257
|
+
let baseAssetReserveBefore: BN;
|
|
258
|
+
let quoteAssetReserveBefore: BN;
|
|
259
|
+
|
|
260
|
+
let peg = market.amm.pegMultiplier;
|
|
261
|
+
|
|
262
|
+
if (useSpread && market.amm.baseSpread > 0) {
|
|
263
|
+
const { baseAssetReserve, quoteAssetReserve, newPeg } =
|
|
264
|
+
calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
265
|
+
baseAssetReserveBefore = baseAssetReserve;
|
|
266
|
+
quoteAssetReserveBefore = quoteAssetReserve;
|
|
267
|
+
peg = newPeg;
|
|
268
|
+
} else {
|
|
269
|
+
baseAssetReserveBefore = market.amm.baseAssetReserve;
|
|
270
|
+
quoteAssetReserveBefore = market.amm.quoteAssetReserve;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const invariant = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
274
|
+
const k = invariant.mul(PRICE_PRECISION);
|
|
275
|
+
|
|
276
|
+
let baseAssetReserveAfter;
|
|
277
|
+
let quoteAssetReserveAfter;
|
|
278
|
+
const biasModifier = new BN(1);
|
|
279
|
+
let markPriceAfter;
|
|
280
|
+
|
|
281
|
+
if (
|
|
282
|
+
useSpread &&
|
|
283
|
+
targetPrice.lt(askPriceBefore) &&
|
|
284
|
+
targetPrice.gt(bidPriceBefore)
|
|
285
|
+
) {
|
|
286
|
+
// no trade, market is at target
|
|
287
|
+
if (reservePriceBefore.gt(targetPrice)) {
|
|
288
|
+
direction = PositionDirection.SHORT;
|
|
289
|
+
} else {
|
|
290
|
+
direction = PositionDirection.LONG;
|
|
291
|
+
}
|
|
292
|
+
tradeSize = ZERO;
|
|
293
|
+
return [direction, tradeSize, targetPrice, targetPrice];
|
|
294
|
+
} else if (reservePriceBefore.gt(targetPrice)) {
|
|
295
|
+
// overestimate y2
|
|
296
|
+
baseAssetReserveAfter = squareRootBN(
|
|
297
|
+
k.div(targetPrice).mul(peg).div(PEG_PRECISION).sub(biasModifier)
|
|
298
|
+
).sub(new BN(1));
|
|
299
|
+
quoteAssetReserveAfter = k.div(PRICE_PRECISION).div(baseAssetReserveAfter);
|
|
300
|
+
|
|
301
|
+
markPriceAfter = calculatePrice(
|
|
302
|
+
baseAssetReserveAfter,
|
|
303
|
+
quoteAssetReserveAfter,
|
|
304
|
+
peg
|
|
305
|
+
);
|
|
306
|
+
direction = PositionDirection.SHORT;
|
|
307
|
+
tradeSize = quoteAssetReserveBefore
|
|
308
|
+
.sub(quoteAssetReserveAfter)
|
|
309
|
+
.mul(peg)
|
|
310
|
+
.div(PEG_PRECISION)
|
|
311
|
+
.div(AMM_TO_QUOTE_PRECISION_RATIO);
|
|
312
|
+
baseSize = baseAssetReserveAfter.sub(baseAssetReserveBefore);
|
|
313
|
+
} else if (reservePriceBefore.lt(targetPrice)) {
|
|
314
|
+
// underestimate y2
|
|
315
|
+
baseAssetReserveAfter = squareRootBN(
|
|
316
|
+
k.div(targetPrice).mul(peg).div(PEG_PRECISION).add(biasModifier)
|
|
317
|
+
).add(new BN(1));
|
|
318
|
+
quoteAssetReserveAfter = k.div(PRICE_PRECISION).div(baseAssetReserveAfter);
|
|
319
|
+
|
|
320
|
+
markPriceAfter = calculatePrice(
|
|
321
|
+
baseAssetReserveAfter,
|
|
322
|
+
quoteAssetReserveAfter,
|
|
323
|
+
peg
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
direction = PositionDirection.LONG;
|
|
327
|
+
tradeSize = quoteAssetReserveAfter
|
|
328
|
+
.sub(quoteAssetReserveBefore)
|
|
329
|
+
.mul(peg)
|
|
330
|
+
.div(PEG_PRECISION)
|
|
331
|
+
.div(AMM_TO_QUOTE_PRECISION_RATIO);
|
|
332
|
+
baseSize = baseAssetReserveBefore.sub(baseAssetReserveAfter);
|
|
333
|
+
} else {
|
|
334
|
+
// no trade, market is at target
|
|
335
|
+
direction = PositionDirection.LONG;
|
|
336
|
+
tradeSize = ZERO;
|
|
337
|
+
return [direction, tradeSize, targetPrice, targetPrice];
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
let tp1 = targetPrice;
|
|
341
|
+
let tp2 = markPriceAfter;
|
|
342
|
+
let originalDiff = targetPrice.sub(reservePriceBefore);
|
|
343
|
+
|
|
344
|
+
if (direction == PositionDirection.SHORT) {
|
|
345
|
+
tp1 = markPriceAfter;
|
|
346
|
+
tp2 = targetPrice;
|
|
347
|
+
originalDiff = reservePriceBefore.sub(targetPrice);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const entryPrice = tradeSize
|
|
351
|
+
.mul(AMM_TO_QUOTE_PRECISION_RATIO)
|
|
352
|
+
.mul(PRICE_PRECISION)
|
|
353
|
+
.div(baseSize.abs());
|
|
354
|
+
|
|
355
|
+
assert(tp1.sub(tp2).lte(originalDiff), 'Target Price Calculation incorrect');
|
|
356
|
+
assert(
|
|
357
|
+
tp2.lte(tp1) || tp2.sub(tp1).abs() < 100000,
|
|
358
|
+
'Target Price Calculation incorrect' +
|
|
359
|
+
tp2.toString() +
|
|
360
|
+
'>=' +
|
|
361
|
+
tp1.toString() +
|
|
362
|
+
'err: ' +
|
|
363
|
+
tp2.sub(tp1).abs().toString()
|
|
364
|
+
);
|
|
365
|
+
if (outputAssetType == 'quote') {
|
|
366
|
+
return [direction, tradeSize, entryPrice, targetPrice];
|
|
367
|
+
} else {
|
|
368
|
+
return [direction, baseSize, entryPrice, targetPrice];
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Calculates the estimated entry price and price impact of order, in base or quote
|
|
374
|
+
* Price impact is based on the difference between the entry price and the best bid/ask price (whether it's dlob or vamm)
|
|
375
|
+
*
|
|
376
|
+
* @param assetType
|
|
377
|
+
* @param amount
|
|
378
|
+
* @param direction
|
|
379
|
+
* @param market
|
|
380
|
+
* @param oraclePriceData
|
|
381
|
+
* @param dlob
|
|
382
|
+
* @param slot
|
|
383
|
+
* @param usersToSkip
|
|
384
|
+
*/
|
|
385
|
+
export function calculateEstimatedPerpEntryPrice(
|
|
386
|
+
assetType: AssetType,
|
|
387
|
+
amount: BN,
|
|
388
|
+
direction: PositionDirection,
|
|
389
|
+
market: PerpMarketAccount,
|
|
390
|
+
oraclePriceData: OraclePriceData,
|
|
391
|
+
dlob: DLOB,
|
|
392
|
+
slot: number,
|
|
393
|
+
usersToSkip = new Map<PublicKey, boolean>()
|
|
394
|
+
): {
|
|
395
|
+
entryPrice: BN;
|
|
396
|
+
priceImpact: BN;
|
|
397
|
+
bestPrice: BN;
|
|
398
|
+
worstPrice: BN;
|
|
399
|
+
baseFilled: BN;
|
|
400
|
+
quoteFilled: BN;
|
|
401
|
+
} {
|
|
402
|
+
if (amount.eq(ZERO)) {
|
|
403
|
+
return {
|
|
404
|
+
entryPrice: ZERO,
|
|
405
|
+
priceImpact: ZERO,
|
|
406
|
+
bestPrice: ZERO,
|
|
407
|
+
worstPrice: ZERO,
|
|
408
|
+
baseFilled: ZERO,
|
|
409
|
+
quoteFilled: ZERO,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const takerIsLong = isVariant(direction, 'long');
|
|
414
|
+
const limitOrders = dlob[
|
|
415
|
+
takerIsLong ? 'getRestingLimitAsks' : 'getRestingLimitBids'
|
|
416
|
+
](market.marketIndex, slot, MarketType.PERP, oraclePriceData);
|
|
417
|
+
|
|
418
|
+
const swapDirection = getSwapDirection(assetType, direction);
|
|
419
|
+
|
|
420
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } =
|
|
421
|
+
calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
422
|
+
const amm = {
|
|
423
|
+
baseAssetReserve,
|
|
424
|
+
quoteAssetReserve,
|
|
425
|
+
sqrtK: sqrtK,
|
|
426
|
+
pegMultiplier: newPeg,
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
const [ammBids, ammAsks] = calculateMarketOpenBidAsk(
|
|
430
|
+
market.amm.baseAssetReserve,
|
|
431
|
+
market.amm.minBaseAssetReserve,
|
|
432
|
+
market.amm.maxBaseAssetReserve,
|
|
433
|
+
market.amm.orderStepSize
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
let ammLiquidity: BN;
|
|
437
|
+
if (assetType === 'base') {
|
|
438
|
+
ammLiquidity = takerIsLong ? ammAsks.abs() : ammBids;
|
|
439
|
+
} else {
|
|
440
|
+
const [afterSwapQuoteReserves, _] = calculateAmmReservesAfterSwap(
|
|
441
|
+
amm,
|
|
442
|
+
'base',
|
|
443
|
+
takerIsLong ? ammAsks.abs() : ammBids,
|
|
444
|
+
getSwapDirection('base', direction)
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
ammLiquidity = calculateQuoteAssetAmountSwapped(
|
|
448
|
+
amm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(),
|
|
449
|
+
amm.pegMultiplier,
|
|
450
|
+
swapDirection
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const invariant = amm.sqrtK.mul(amm.sqrtK);
|
|
455
|
+
|
|
456
|
+
let bestPrice = calculatePrice(
|
|
457
|
+
amm.baseAssetReserve,
|
|
458
|
+
amm.quoteAssetReserve,
|
|
459
|
+
amm.pegMultiplier
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
let cumulativeBaseFilled = ZERO;
|
|
463
|
+
let cumulativeQuoteFilled = ZERO;
|
|
464
|
+
|
|
465
|
+
let limitOrder = limitOrders.next().value;
|
|
466
|
+
if (limitOrder) {
|
|
467
|
+
const limitOrderPrice = limitOrder.getPrice(oraclePriceData, slot);
|
|
468
|
+
bestPrice = takerIsLong
|
|
469
|
+
? BN.min(limitOrderPrice, bestPrice)
|
|
470
|
+
: BN.max(limitOrderPrice, bestPrice);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
let worstPrice = bestPrice;
|
|
474
|
+
|
|
475
|
+
if (assetType === 'base') {
|
|
476
|
+
while (
|
|
477
|
+
!cumulativeBaseFilled.eq(amount) &&
|
|
478
|
+
(ammLiquidity.gt(ZERO) || limitOrder)
|
|
479
|
+
) {
|
|
480
|
+
const limitOrderPrice = limitOrder?.getPrice(oraclePriceData, slot);
|
|
481
|
+
|
|
482
|
+
let maxAmmFill: BN;
|
|
483
|
+
if (limitOrderPrice) {
|
|
484
|
+
const newBaseReserves = squareRootBN(
|
|
485
|
+
invariant
|
|
486
|
+
.mul(PRICE_PRECISION)
|
|
487
|
+
.mul(amm.pegMultiplier)
|
|
488
|
+
.div(limitOrderPrice)
|
|
489
|
+
.div(PEG_PRECISION)
|
|
490
|
+
);
|
|
491
|
+
|
|
492
|
+
// will be zero if the limit order price is better than the amm price
|
|
493
|
+
maxAmmFill = takerIsLong
|
|
494
|
+
? amm.baseAssetReserve.sub(newBaseReserves)
|
|
495
|
+
: newBaseReserves.sub(amm.baseAssetReserve);
|
|
496
|
+
} else {
|
|
497
|
+
maxAmmFill = amount.sub(cumulativeBaseFilled);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
maxAmmFill = BN.min(maxAmmFill, ammLiquidity);
|
|
501
|
+
|
|
502
|
+
if (maxAmmFill.gt(ZERO)) {
|
|
503
|
+
const baseFilled = BN.min(amount.sub(cumulativeBaseFilled), maxAmmFill);
|
|
504
|
+
const [afterSwapQuoteReserves, afterSwapBaseReserves] =
|
|
505
|
+
calculateAmmReservesAfterSwap(amm, 'base', baseFilled, swapDirection);
|
|
506
|
+
|
|
507
|
+
ammLiquidity = ammLiquidity.sub(baseFilled);
|
|
508
|
+
|
|
509
|
+
const quoteFilled = calculateQuoteAssetAmountSwapped(
|
|
510
|
+
amm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(),
|
|
511
|
+
amm.pegMultiplier,
|
|
512
|
+
swapDirection
|
|
513
|
+
);
|
|
514
|
+
|
|
515
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
516
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
517
|
+
|
|
518
|
+
amm.baseAssetReserve = afterSwapBaseReserves;
|
|
519
|
+
amm.quoteAssetReserve = afterSwapQuoteReserves;
|
|
520
|
+
|
|
521
|
+
worstPrice = calculatePrice(
|
|
522
|
+
amm.baseAssetReserve,
|
|
523
|
+
amm.quoteAssetReserve,
|
|
524
|
+
amm.pegMultiplier
|
|
525
|
+
);
|
|
526
|
+
|
|
527
|
+
if (cumulativeBaseFilled.eq(amount)) {
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (!limitOrder) {
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (usersToSkip.has(limitOrder.userAccount)) {
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const baseFilled = BN.min(
|
|
541
|
+
limitOrder.order.baseAssetAmount.sub(
|
|
542
|
+
limitOrder.order.baseAssetAmountFilled
|
|
543
|
+
),
|
|
544
|
+
amount.sub(cumulativeBaseFilled)
|
|
545
|
+
);
|
|
546
|
+
const quoteFilled = baseFilled.mul(limitOrderPrice).div(BASE_PRECISION);
|
|
547
|
+
|
|
548
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
549
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
550
|
+
|
|
551
|
+
worstPrice = limitOrderPrice;
|
|
552
|
+
|
|
553
|
+
if (cumulativeBaseFilled.eq(amount)) {
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
limitOrder = limitOrders.next().value;
|
|
558
|
+
}
|
|
559
|
+
} else {
|
|
560
|
+
while (
|
|
561
|
+
!cumulativeQuoteFilled.eq(amount) &&
|
|
562
|
+
(ammLiquidity.gt(ZERO) || limitOrder)
|
|
563
|
+
) {
|
|
564
|
+
const limitOrderPrice = limitOrder?.getPrice(oraclePriceData, slot);
|
|
565
|
+
|
|
566
|
+
let maxAmmFill: BN;
|
|
567
|
+
if (limitOrderPrice) {
|
|
568
|
+
const newQuoteReserves = squareRootBN(
|
|
569
|
+
invariant
|
|
570
|
+
.mul(PEG_PRECISION)
|
|
571
|
+
.mul(limitOrderPrice)
|
|
572
|
+
.div(amm.pegMultiplier)
|
|
573
|
+
.div(PRICE_PRECISION)
|
|
574
|
+
);
|
|
575
|
+
|
|
576
|
+
// will be zero if the limit order price is better than the amm price
|
|
577
|
+
maxAmmFill = takerIsLong
|
|
578
|
+
? newQuoteReserves.sub(amm.quoteAssetReserve)
|
|
579
|
+
: amm.quoteAssetReserve.sub(newQuoteReserves);
|
|
580
|
+
} else {
|
|
581
|
+
maxAmmFill = amount.sub(cumulativeQuoteFilled);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
maxAmmFill = BN.min(maxAmmFill, ammLiquidity);
|
|
585
|
+
|
|
586
|
+
if (maxAmmFill.gt(ZERO)) {
|
|
587
|
+
const quoteFilled = BN.min(
|
|
588
|
+
amount.sub(cumulativeQuoteFilled),
|
|
589
|
+
maxAmmFill
|
|
590
|
+
);
|
|
591
|
+
const [afterSwapQuoteReserves, afterSwapBaseReserves] =
|
|
592
|
+
calculateAmmReservesAfterSwap(
|
|
593
|
+
amm,
|
|
594
|
+
'quote',
|
|
595
|
+
quoteFilled,
|
|
596
|
+
swapDirection
|
|
597
|
+
);
|
|
598
|
+
|
|
599
|
+
ammLiquidity = ammLiquidity.sub(quoteFilled);
|
|
600
|
+
|
|
601
|
+
const baseFilled = afterSwapBaseReserves
|
|
602
|
+
.sub(amm.baseAssetReserve)
|
|
603
|
+
.abs();
|
|
604
|
+
|
|
605
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
606
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
607
|
+
|
|
608
|
+
amm.baseAssetReserve = afterSwapBaseReserves;
|
|
609
|
+
amm.quoteAssetReserve = afterSwapQuoteReserves;
|
|
610
|
+
|
|
611
|
+
worstPrice = calculatePrice(
|
|
612
|
+
amm.baseAssetReserve,
|
|
613
|
+
amm.quoteAssetReserve,
|
|
614
|
+
amm.pegMultiplier
|
|
615
|
+
);
|
|
616
|
+
|
|
617
|
+
if (cumulativeQuoteFilled.eq(amount)) {
|
|
618
|
+
break;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
if (!limitOrder) {
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
if (usersToSkip.has(limitOrder.userAccount)) {
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
const quoteFilled = BN.min(
|
|
631
|
+
limitOrder.order.baseAssetAmount
|
|
632
|
+
.sub(limitOrder.order.baseAssetAmountFilled)
|
|
633
|
+
.mul(limitOrderPrice)
|
|
634
|
+
.div(BASE_PRECISION),
|
|
635
|
+
amount.sub(cumulativeQuoteFilled)
|
|
636
|
+
);
|
|
637
|
+
|
|
638
|
+
const baseFilled = quoteFilled.mul(BASE_PRECISION).div(limitOrderPrice);
|
|
639
|
+
|
|
640
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
641
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
642
|
+
|
|
643
|
+
worstPrice = limitOrderPrice;
|
|
644
|
+
|
|
645
|
+
if (cumulativeQuoteFilled.eq(amount)) {
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
limitOrder = limitOrders.next().value;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
const entryPrice =
|
|
654
|
+
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
|
|
655
|
+
? cumulativeQuoteFilled.mul(BASE_PRECISION).div(cumulativeBaseFilled)
|
|
656
|
+
: ZERO;
|
|
657
|
+
|
|
658
|
+
const priceImpact =
|
|
659
|
+
bestPrice && bestPrice.gt(ZERO)
|
|
660
|
+
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
|
|
661
|
+
: ZERO;
|
|
662
|
+
|
|
663
|
+
return {
|
|
664
|
+
entryPrice,
|
|
665
|
+
priceImpact,
|
|
666
|
+
bestPrice,
|
|
667
|
+
worstPrice,
|
|
668
|
+
baseFilled: cumulativeBaseFilled,
|
|
669
|
+
quoteFilled: cumulativeQuoteFilled,
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Calculates the estimated entry price and price impact of order, in base or quote
|
|
675
|
+
* Price impact is based on the difference between the entry price and the best bid/ask price (whether it's dlob or serum)
|
|
676
|
+
*
|
|
677
|
+
* @param assetType
|
|
678
|
+
* @param amount
|
|
679
|
+
* @param direction
|
|
680
|
+
* @param market
|
|
681
|
+
* @param oraclePriceData
|
|
682
|
+
* @param dlob
|
|
683
|
+
* @param serumBids
|
|
684
|
+
* @param serumAsks
|
|
685
|
+
* @param slot
|
|
686
|
+
* @param usersToSkip
|
|
687
|
+
*/
|
|
688
|
+
export function calculateEstimatedSpotEntryPrice(
|
|
689
|
+
assetType: AssetType,
|
|
690
|
+
amount: BN,
|
|
691
|
+
direction: PositionDirection,
|
|
692
|
+
market: SpotMarketAccount,
|
|
693
|
+
oraclePriceData: OraclePriceData,
|
|
694
|
+
dlob: DLOB,
|
|
695
|
+
serumBids: Orderbook,
|
|
696
|
+
serumAsks: Orderbook,
|
|
697
|
+
slot: number,
|
|
698
|
+
usersToSkip = new Map<PublicKey, boolean>()
|
|
699
|
+
): {
|
|
700
|
+
entryPrice: BN;
|
|
701
|
+
priceImpact: BN;
|
|
702
|
+
bestPrice: BN;
|
|
703
|
+
worstPrice: BN;
|
|
704
|
+
baseFilled: BN;
|
|
705
|
+
quoteFilled: BN;
|
|
706
|
+
} {
|
|
707
|
+
if (amount.eq(ZERO)) {
|
|
708
|
+
return {
|
|
709
|
+
entryPrice: ZERO,
|
|
710
|
+
priceImpact: ZERO,
|
|
711
|
+
bestPrice: ZERO,
|
|
712
|
+
worstPrice: ZERO,
|
|
713
|
+
baseFilled: ZERO,
|
|
714
|
+
quoteFilled: ZERO,
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
const basePrecision = new BN(Math.pow(10, market.decimals));
|
|
719
|
+
|
|
720
|
+
const takerIsLong = isVariant(direction, 'long');
|
|
721
|
+
const dlobLimitOrders = dlob[
|
|
722
|
+
takerIsLong ? 'getRestingLimitAsks' : 'getRestingLimitBids'
|
|
723
|
+
](market.marketIndex, slot, MarketType.SPOT, oraclePriceData);
|
|
724
|
+
const serumLimitOrders = takerIsLong
|
|
725
|
+
? serumAsks.getL2(100)
|
|
726
|
+
: serumBids.getL2(100);
|
|
727
|
+
|
|
728
|
+
let cumulativeBaseFilled = ZERO;
|
|
729
|
+
let cumulativeQuoteFilled = ZERO;
|
|
730
|
+
|
|
731
|
+
let dlobLimitOrder = dlobLimitOrders.next().value;
|
|
732
|
+
let serumLimitOrder = serumLimitOrders.shift();
|
|
733
|
+
|
|
734
|
+
const dlobLimitOrderPrice = dlobLimitOrder?.getPrice(oraclePriceData, slot);
|
|
735
|
+
const serumLimitOrderPrice = serumLimitOrder
|
|
736
|
+
? new BN(serumLimitOrder[0] * PRICE_PRECISION.toNumber())
|
|
737
|
+
: undefined;
|
|
738
|
+
|
|
739
|
+
const bestPrice = takerIsLong
|
|
740
|
+
? BN.min(serumLimitOrderPrice || BN_MAX, dlobLimitOrderPrice || BN_MAX)
|
|
741
|
+
: BN.max(serumLimitOrderPrice || ZERO, dlobLimitOrderPrice || ZERO);
|
|
742
|
+
let worstPrice = bestPrice;
|
|
743
|
+
|
|
744
|
+
if (assetType === 'base') {
|
|
745
|
+
while (
|
|
746
|
+
!cumulativeBaseFilled.eq(amount) &&
|
|
747
|
+
(dlobLimitOrder || serumLimitOrder)
|
|
748
|
+
) {
|
|
749
|
+
const dlobLimitOrderPrice = dlobLimitOrder?.getPrice(
|
|
750
|
+
oraclePriceData,
|
|
751
|
+
slot
|
|
752
|
+
);
|
|
753
|
+
const serumLimitOrderPrice = serumLimitOrder
|
|
754
|
+
? new BN(serumLimitOrder[0] * PRICE_PRECISION.toNumber())
|
|
755
|
+
: undefined;
|
|
756
|
+
|
|
757
|
+
const useSerum = takerIsLong
|
|
758
|
+
? (serumLimitOrderPrice || BN_MAX).lt(dlobLimitOrderPrice || BN_MAX)
|
|
759
|
+
: (serumLimitOrderPrice || ZERO).gt(dlobLimitOrderPrice || ZERO);
|
|
760
|
+
|
|
761
|
+
if (!useSerum) {
|
|
762
|
+
if (dlobLimitOrder && usersToSkip.has(dlobLimitOrder.userAccount)) {
|
|
763
|
+
continue;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
const baseFilled = BN.min(
|
|
767
|
+
dlobLimitOrder.order.baseAssetAmount.sub(
|
|
768
|
+
dlobLimitOrder.order.baseAssetAmountFilled
|
|
769
|
+
),
|
|
770
|
+
amount.sub(cumulativeBaseFilled)
|
|
771
|
+
);
|
|
772
|
+
const quoteFilled = baseFilled
|
|
773
|
+
.mul(dlobLimitOrderPrice)
|
|
774
|
+
.div(basePrecision);
|
|
775
|
+
|
|
776
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
777
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
778
|
+
|
|
779
|
+
worstPrice = dlobLimitOrderPrice;
|
|
780
|
+
|
|
781
|
+
dlobLimitOrder = dlobLimitOrders.next().value;
|
|
782
|
+
} else {
|
|
783
|
+
const baseFilled = BN.min(
|
|
784
|
+
new BN(serumLimitOrder[1] * basePrecision.toNumber()),
|
|
785
|
+
amount.sub(cumulativeBaseFilled)
|
|
786
|
+
);
|
|
787
|
+
const quoteFilled = baseFilled
|
|
788
|
+
.mul(serumLimitOrderPrice)
|
|
789
|
+
.div(basePrecision);
|
|
790
|
+
|
|
791
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
792
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
793
|
+
|
|
794
|
+
worstPrice = serumLimitOrderPrice;
|
|
795
|
+
|
|
796
|
+
serumLimitOrder = serumLimitOrders.shift();
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
} else {
|
|
800
|
+
while (
|
|
801
|
+
!cumulativeQuoteFilled.eq(amount) &&
|
|
802
|
+
(dlobLimitOrder || serumLimitOrder)
|
|
803
|
+
) {
|
|
804
|
+
const dlobLimitOrderPrice = dlobLimitOrder?.getPrice(
|
|
805
|
+
oraclePriceData,
|
|
806
|
+
slot
|
|
807
|
+
);
|
|
808
|
+
const serumLimitOrderPrice = serumLimitOrder
|
|
809
|
+
? new BN(serumLimitOrder[0] * PRICE_PRECISION.toNumber())
|
|
810
|
+
: undefined;
|
|
811
|
+
|
|
812
|
+
const useSerum = takerIsLong
|
|
813
|
+
? (serumLimitOrderPrice || BN_MAX).lt(dlobLimitOrderPrice || BN_MAX)
|
|
814
|
+
: (serumLimitOrderPrice || ZERO).gt(dlobLimitOrderPrice || ZERO);
|
|
815
|
+
|
|
816
|
+
if (!useSerum) {
|
|
817
|
+
if (dlobLimitOrder && usersToSkip.has(dlobLimitOrder.userAccount)) {
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
const quoteFilled = BN.min(
|
|
822
|
+
dlobLimitOrder.order.baseAssetAmount
|
|
823
|
+
.sub(dlobLimitOrder.order.baseAssetAmountFilled)
|
|
824
|
+
.mul(dlobLimitOrderPrice)
|
|
825
|
+
.div(basePrecision),
|
|
826
|
+
amount.sub(cumulativeQuoteFilled)
|
|
827
|
+
);
|
|
828
|
+
|
|
829
|
+
const baseFilled = quoteFilled
|
|
830
|
+
.mul(basePrecision)
|
|
831
|
+
.div(dlobLimitOrderPrice);
|
|
832
|
+
|
|
833
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
834
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
835
|
+
|
|
836
|
+
worstPrice = dlobLimitOrderPrice;
|
|
837
|
+
|
|
838
|
+
dlobLimitOrder = dlobLimitOrders.next().value;
|
|
839
|
+
} else {
|
|
840
|
+
const serumOrderBaseAmount = new BN(
|
|
841
|
+
serumLimitOrder[1] * basePrecision.toNumber()
|
|
842
|
+
);
|
|
843
|
+
const quoteFilled = BN.min(
|
|
844
|
+
serumOrderBaseAmount.mul(serumLimitOrderPrice).div(basePrecision),
|
|
845
|
+
amount.sub(cumulativeQuoteFilled)
|
|
846
|
+
);
|
|
847
|
+
|
|
848
|
+
const baseFilled = quoteFilled
|
|
849
|
+
.mul(basePrecision)
|
|
850
|
+
.div(serumLimitOrderPrice);
|
|
851
|
+
|
|
852
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
853
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
854
|
+
|
|
855
|
+
worstPrice = serumLimitOrderPrice;
|
|
856
|
+
|
|
857
|
+
serumLimitOrder = serumLimitOrders.shift();
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
const entryPrice =
|
|
863
|
+
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
|
|
864
|
+
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
|
|
865
|
+
: ZERO;
|
|
866
|
+
|
|
867
|
+
const priceImpact =
|
|
868
|
+
bestPrice && bestPrice.gt(ZERO)
|
|
869
|
+
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
|
|
870
|
+
: ZERO;
|
|
871
|
+
|
|
872
|
+
return {
|
|
873
|
+
entryPrice,
|
|
874
|
+
priceImpact,
|
|
875
|
+
bestPrice,
|
|
876
|
+
worstPrice,
|
|
877
|
+
baseFilled: cumulativeBaseFilled,
|
|
878
|
+
quoteFilled: cumulativeQuoteFilled,
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
export function calculateEstimatedEntryPriceWithL2(
|
|
883
|
+
assetType: AssetType,
|
|
884
|
+
amount: BN,
|
|
885
|
+
direction: PositionDirection,
|
|
886
|
+
basePrecision: BN,
|
|
887
|
+
l2: L2OrderBook
|
|
888
|
+
): {
|
|
889
|
+
entryPrice: BN;
|
|
890
|
+
priceImpact: BN;
|
|
891
|
+
bestPrice: BN;
|
|
892
|
+
worstPrice: BN;
|
|
893
|
+
baseFilled: BN;
|
|
894
|
+
quoteFilled: BN;
|
|
895
|
+
} {
|
|
896
|
+
const takerIsLong = isVariant(direction, 'long');
|
|
897
|
+
|
|
898
|
+
let cumulativeBaseFilled = ZERO;
|
|
899
|
+
let cumulativeQuoteFilled = ZERO;
|
|
900
|
+
|
|
901
|
+
const levels = [...(takerIsLong ? l2.asks : l2.bids)];
|
|
902
|
+
let nextLevel = levels.shift();
|
|
903
|
+
|
|
904
|
+
let bestPrice: BN;
|
|
905
|
+
let worstPrice: BN;
|
|
906
|
+
if (nextLevel) {
|
|
907
|
+
bestPrice = nextLevel.price;
|
|
908
|
+
worstPrice = nextLevel.price;
|
|
909
|
+
} else {
|
|
910
|
+
bestPrice = takerIsLong ? BN_MAX : ZERO;
|
|
911
|
+
worstPrice = bestPrice;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
if (assetType === 'base') {
|
|
915
|
+
while (!cumulativeBaseFilled.eq(amount) && nextLevel) {
|
|
916
|
+
const price = nextLevel.price;
|
|
917
|
+
const size = nextLevel.size;
|
|
918
|
+
|
|
919
|
+
worstPrice = price;
|
|
920
|
+
|
|
921
|
+
const baseFilled = BN.min(size, amount.sub(cumulativeBaseFilled));
|
|
922
|
+
const quoteFilled = baseFilled.mul(price).div(basePrecision);
|
|
923
|
+
|
|
924
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
925
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
926
|
+
|
|
927
|
+
nextLevel = levels.shift();
|
|
928
|
+
}
|
|
929
|
+
} else {
|
|
930
|
+
while (!cumulativeQuoteFilled.eq(amount) && nextLevel) {
|
|
931
|
+
const price = nextLevel.price;
|
|
932
|
+
const size = nextLevel.size;
|
|
933
|
+
|
|
934
|
+
worstPrice = price;
|
|
935
|
+
|
|
936
|
+
const quoteFilled = BN.min(
|
|
937
|
+
size.mul(price).div(basePrecision),
|
|
938
|
+
amount.sub(cumulativeQuoteFilled)
|
|
939
|
+
);
|
|
940
|
+
const baseFilled = quoteFilled.mul(basePrecision).div(price);
|
|
941
|
+
|
|
942
|
+
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
943
|
+
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
944
|
+
|
|
945
|
+
nextLevel = levels.shift();
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
const entryPrice =
|
|
950
|
+
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
|
|
951
|
+
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
|
|
952
|
+
: ZERO;
|
|
953
|
+
|
|
954
|
+
const priceImpact =
|
|
955
|
+
bestPrice && bestPrice.gt(ZERO)
|
|
956
|
+
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
|
|
957
|
+
: ZERO;
|
|
958
|
+
|
|
959
|
+
return {
|
|
960
|
+
entryPrice,
|
|
961
|
+
priceImpact,
|
|
962
|
+
bestPrice,
|
|
963
|
+
worstPrice,
|
|
964
|
+
baseFilled: cumulativeBaseFilled,
|
|
965
|
+
quoteFilled: cumulativeQuoteFilled,
|
|
966
|
+
};
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
export function getUser30dRollingVolumeEstimate(
|
|
970
|
+
userStatsAccount: UserStatsAccount,
|
|
971
|
+
now?: BN
|
|
972
|
+
) {
|
|
973
|
+
now = now || new BN(new Date().getTime() / 1000);
|
|
974
|
+
const sinceLastTaker = BN.max(
|
|
975
|
+
now.sub(userStatsAccount.lastTakerVolume30DTs),
|
|
976
|
+
ZERO
|
|
977
|
+
);
|
|
978
|
+
const sinceLastMaker = BN.max(
|
|
979
|
+
now.sub(userStatsAccount.lastMakerVolume30DTs),
|
|
980
|
+
ZERO
|
|
981
|
+
);
|
|
982
|
+
const thirtyDaysInSeconds = new BN(60 * 60 * 24 * 30);
|
|
983
|
+
const last30dVolume = userStatsAccount.takerVolume30D
|
|
984
|
+
.mul(BN.max(thirtyDaysInSeconds.sub(sinceLastTaker), ZERO))
|
|
985
|
+
.div(thirtyDaysInSeconds)
|
|
986
|
+
.add(
|
|
987
|
+
userStatsAccount.makerVolume30D
|
|
988
|
+
.mul(BN.max(thirtyDaysInSeconds.sub(sinceLastMaker), ZERO))
|
|
989
|
+
.div(thirtyDaysInSeconds)
|
|
990
|
+
);
|
|
991
|
+
|
|
992
|
+
return last30dVolume;
|
|
993
|
+
}
|