@drift-labs/sdk 2.162.0-beta.2 → 2.163.0-beta.10
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/.prettierignore +1 -0
- package/.yarn/install-state.gz +0 -0
- package/README.md +2 -2
- package/VERSION +1 -1
- package/bun.lock +47 -79
- package/lib/browser/accounts/fetch.d.ts +6 -6
- package/lib/browser/accounts/fetch.js +4 -4
- package/lib/browser/accounts/grpcAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/grpcDriftClientAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.d.ts +3 -3
- package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.js +2 -2
- package/lib/browser/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/grpcMultiUserAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/grpcProgramAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/grpcUserAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/grpcUserStatsAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/laserProgramAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/oneShotUserAccountSubscriber.d.ts +3 -3
- package/lib/browser/accounts/oneShotUserStatsAccountSubscriber.d.ts +3 -3
- package/lib/browser/accounts/pollingDriftClientAccountSubscriber.d.ts +3 -3
- package/lib/browser/accounts/pollingDriftClientAccountSubscriber.js +2 -2
- package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +3 -3
- package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.js +1 -1
- package/lib/browser/accounts/pollingOracleAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/pollingTokenAccountSubscriber.d.ts +2 -2
- package/lib/browser/accounts/pollingUserAccountSubscriber.js +2 -2
- package/lib/browser/accounts/pollingUserStatsAccountSubscriber.d.ts +3 -3
- package/lib/browser/accounts/pollingUserStatsAccountSubscriber.js +1 -1
- package/lib/browser/accounts/testBulkAccountLoader.js +3 -5
- package/lib/browser/accounts/types.d.ts +11 -14
- package/lib/browser/accounts/webSocketAccountSubscriber.d.ts +3 -3
- package/lib/browser/accounts/webSocketAccountSubscriber.js +1 -2
- package/lib/browser/accounts/webSocketAccountSubscriberV2.d.ts +3 -3
- package/lib/browser/accounts/webSocketDriftClientAccountSubscriber.d.ts +5 -5
- package/lib/browser/accounts/webSocketDriftClientAccountSubscriber.js +2 -2
- package/lib/browser/accounts/webSocketDriftClientAccountSubscriberV2.d.ts +3 -3
- package/lib/browser/accounts/webSocketDriftClientAccountSubscriberV2.js +2 -2
- package/lib/browser/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +3 -3
- package/lib/browser/accounts/webSocketProgramAccountSubscriber.d.ts +3 -3
- package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.d.ts +3 -3
- package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.d.ts +3 -3
- package/lib/browser/accounts/webSocketUserAccountSubscriber.d.ts +3 -3
- package/lib/browser/accounts/webSocketUserStatsAccountSubsriber.d.ts +3 -3
- package/lib/browser/accounts/websocketProgramUserAccountSubscriber.d.ts +2 -2
- package/lib/browser/addresses/pda.d.ts +6 -2
- package/lib/browser/addresses/pda.js +7 -12
- package/lib/browser/adminClient.d.ts +11 -9
- package/lib/browser/adminClient.js +30 -72
- package/lib/browser/auctionSubscriber/auctionSubscriber.js +1 -1
- package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.js +1 -1
- package/lib/browser/config.d.ts +7 -3
- package/lib/browser/config.js +9 -7
- package/lib/browser/constants/index.d.ts +5 -0
- package/lib/browser/constants/index.js +5 -0
- package/lib/browser/constants/numericConstants.d.ts +1 -1
- package/lib/browser/constants/numericConstants.js +3 -3
- package/lib/browser/constants/perpMarkets.js +3 -412
- package/lib/browser/constants/spotMarkets.js +7 -91
- package/lib/browser/constituentMap/constituentMap.js +2 -2
- package/lib/browser/constituentMap/pollingConstituentAccountSubscriber.d.ts +3 -3
- package/lib/browser/constituentMap/webSocketConstituentAccountSubscriber.d.ts +3 -3
- package/lib/browser/constituentMap/webSocketConstituentAccountSubscriber.js +1 -1
- package/lib/browser/decode/customCoder.d.ts +7 -9
- package/lib/browser/decode/customCoder.js +17 -28
- package/lib/browser/decode/user.js +7 -25
- package/lib/browser/dlob/DLOB.d.ts +12 -0
- package/lib/browser/dlob/DLOB.js +12 -0
- package/lib/browser/dlob/orderBookLevels.js +1 -1
- package/lib/browser/driftClient.d.ts +32 -59
- package/lib/browser/driftClient.js +85 -562
- package/lib/browser/driftClientConfig.d.ts +7 -0
- package/lib/browser/events/eventSubscriber.d.ts +8 -0
- package/lib/browser/events/eventSubscriber.js +5 -2
- package/lib/browser/events/parse.d.ts +1 -1
- package/lib/browser/factory/oracleClient.d.ts +2 -2
- package/lib/browser/factory/oracleClient.js +9 -21
- package/lib/browser/idl/drift.d.ts +21406 -0
- package/lib/browser/idl/drift.js +2 -0
- package/lib/browser/idl/drift.json +20850 -15287
- package/lib/browser/idl/pyth.d.ts +97 -0
- package/lib/browser/idl/pyth.js +2 -0
- package/lib/browser/idl/token_faucet.d.ts +197 -0
- package/lib/browser/idl/token_faucet.js +2 -0
- package/lib/browser/idl/token_faucet.json +148 -61
- package/lib/browser/index.d.ts +13 -6
- package/lib/browser/index.js +12 -5
- package/lib/browser/marinade/index.d.ts +1 -1
- package/lib/browser/marinade/index.js +2 -2
- package/lib/browser/math/amm.d.ts +4 -5
- package/lib/browser/math/amm.js +8 -35
- package/lib/browser/math/funding.js +6 -1
- package/lib/browser/math/margin.d.ts +3 -4
- package/lib/browser/math/margin.js +16 -47
- package/lib/browser/math/market.d.ts +2 -2
- package/lib/browser/math/market.js +10 -42
- package/lib/browser/math/oracles.js +14 -19
- package/lib/browser/math/position.js +1 -1
- package/lib/browser/math/trade.js +4 -4
- package/lib/browser/memcmp.js +8 -8
- package/lib/browser/openbook/openbookV2Subscriber.d.ts +1 -1
- package/lib/browser/openbook/openbookV2Subscriber.js +3 -3
- package/lib/browser/oracles/oracleClientCache.d.ts +2 -2
- package/lib/browser/oracles/prelaunchOracleClient.d.ts +2 -2
- package/lib/browser/oracles/prelaunchOracleClient.js +1 -1
- package/lib/browser/oracles/pythLazerClient.js +3 -5
- package/lib/browser/oracles/types.d.ts +6 -0
- package/lib/browser/orderParams.d.ts +0 -1
- package/lib/browser/orderParams.js +1 -5
- package/lib/browser/orderSubscriber/OrderSubscriber.js +3 -4
- package/lib/browser/orderSubscriber/WebsocketSubscription.js +1 -1
- package/lib/browser/orderSubscriber/grpcSubscription.js +2 -2
- package/lib/browser/orderSubscriber/index.d.ts +5 -0
- package/lib/browser/orderSubscriber/index.js +5 -0
- package/lib/browser/priorityFee/index.d.ts +5 -0
- package/lib/browser/priorityFee/index.js +5 -0
- package/lib/browser/pyth/index.d.ts +2 -2
- package/lib/browser/pyth/types.d.ts +1 -1
- package/lib/browser/swift/grpcSignedMsgUserAccountSubscriber.js +1 -1
- package/lib/browser/swift/index.d.ts +7 -0
- package/lib/browser/swift/index.js +7 -0
- package/lib/browser/swift/signedMsgUserAccountSubscriber.js +4 -3
- package/lib/browser/tokenFaucet.d.ts +2 -2
- package/lib/browser/tokenFaucet.js +4 -4
- package/lib/browser/tx/txHandler.d.ts +12 -0
- package/lib/browser/tx/txHandler.js +12 -0
- package/lib/browser/types.d.ts +19 -31
- package/lib/browser/types.js +6 -11
- package/lib/browser/user.d.ts +27 -18
- package/lib/browser/user.js +64 -100
- package/lib/browser/userMap/WebsocketSubscription.js +1 -1
- package/lib/browser/userMap/grpcSubscription.js +1 -1
- package/lib/browser/userMap/revenueShareEscrowMap.js +4 -4
- package/lib/browser/userMap/userMap.js +4 -5
- package/lib/browser/userMap/userStatsMap.js +2 -3
- package/lib/browser/userStats.d.ts +5 -0
- package/lib/node/accounts/fetch.d.ts +6 -6
- package/lib/node/accounts/fetch.d.ts.map +1 -1
- package/lib/node/accounts/fetch.js +4 -4
- package/lib/node/accounts/grpcAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcDriftClientAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/grpcDriftClientAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts +3 -3
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.js +2 -2
- package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcUserAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/grpcUserAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcUserStatsAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/grpcUserStatsAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/laserProgramAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/laserProgramAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/oneShotUserAccountSubscriber.d.ts +3 -3
- package/lib/node/accounts/oneShotUserAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/oneShotUserStatsAccountSubscriber.d.ts +3 -3
- package/lib/node/accounts/oneShotUserStatsAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/pollingDriftClientAccountSubscriber.d.ts +3 -3
- package/lib/node/accounts/pollingDriftClientAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/pollingDriftClientAccountSubscriber.js +2 -2
- package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +3 -3
- package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.js +1 -1
- package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/pollingUserAccountSubscriber.js +2 -2
- package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts +3 -3
- package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/pollingUserStatsAccountSubscriber.js +1 -1
- package/lib/node/accounts/testBulkAccountLoader.d.ts.map +1 -1
- package/lib/node/accounts/testBulkAccountLoader.js +3 -5
- package/lib/node/accounts/types.d.ts +11 -14
- package/lib/node/accounts/types.d.ts.map +1 -1
- package/lib/node/accounts/webSocketAccountSubscriber.d.ts +3 -3
- package/lib/node/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/webSocketAccountSubscriber.js +1 -2
- package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts +3 -3
- package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/webSocketDriftClientAccountSubscriber.d.ts +5 -5
- package/lib/node/accounts/webSocketDriftClientAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/webSocketDriftClientAccountSubscriber.js +2 -2
- package/lib/node/accounts/webSocketDriftClientAccountSubscriberV2.d.ts +3 -3
- package/lib/node/accounts/webSocketDriftClientAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/webSocketDriftClientAccountSubscriberV2.js +2 -2
- package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +3 -3
- package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts +3 -3
- package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +3 -3
- package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts +3 -3
- package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts +3 -3
- package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts +3 -3
- package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts.map +1 -1
- package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts +2 -2
- package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts.map +1 -1
- package/lib/node/addresses/pda.d.ts +6 -2
- package/lib/node/addresses/pda.d.ts.map +1 -1
- package/lib/node/addresses/pda.js +7 -12
- package/lib/node/adminClient.d.ts +11 -9
- package/lib/node/adminClient.d.ts.map +1 -1
- package/lib/node/adminClient.js +30 -72
- package/lib/node/auctionSubscriber/auctionSubscriber.d.ts.map +1 -1
- package/lib/node/auctionSubscriber/auctionSubscriber.js +1 -1
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts.map +1 -1
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.js +1 -1
- package/lib/node/config.d.ts +7 -3
- package/lib/node/config.d.ts.map +1 -1
- package/lib/node/config.js +9 -7
- package/lib/node/constants/index.d.ts +5 -0
- package/lib/node/constants/index.d.ts.map +1 -1
- package/lib/node/constants/index.js +5 -0
- package/lib/node/constants/numericConstants.d.ts +1 -1
- package/lib/node/constants/numericConstants.d.ts.map +1 -1
- package/lib/node/constants/numericConstants.js +3 -3
- package/lib/node/constants/perpMarkets.d.ts.map +1 -1
- package/lib/node/constants/perpMarkets.js +3 -412
- package/lib/node/constants/spotMarkets.d.ts.map +1 -1
- package/lib/node/constants/spotMarkets.js +7 -91
- package/lib/node/constituentMap/constituentMap.d.ts.map +1 -1
- package/lib/node/constituentMap/constituentMap.js +2 -2
- package/lib/node/constituentMap/pollingConstituentAccountSubscriber.d.ts +3 -3
- package/lib/node/constituentMap/pollingConstituentAccountSubscriber.d.ts.map +1 -1
- package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.d.ts +3 -3
- package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.d.ts.map +1 -1
- package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.js +1 -1
- package/lib/node/decode/customCoder.d.ts +7 -9
- package/lib/node/decode/customCoder.d.ts.map +1 -1
- package/lib/node/decode/customCoder.js +17 -28
- package/lib/node/decode/user.d.ts.map +1 -1
- package/lib/node/decode/user.js +7 -25
- package/lib/node/dlob/DLOB.d.ts +12 -0
- package/lib/node/dlob/DLOB.d.ts.map +1 -1
- package/lib/node/dlob/DLOB.js +12 -0
- package/lib/node/dlob/orderBookLevels.d.ts.map +1 -1
- package/lib/node/dlob/orderBookLevels.js +1 -1
- package/lib/node/driftClient.d.ts +32 -59
- package/lib/node/driftClient.d.ts.map +1 -1
- package/lib/node/driftClient.js +85 -562
- package/lib/node/driftClientConfig.d.ts +7 -0
- package/lib/node/driftClientConfig.d.ts.map +1 -1
- package/lib/node/events/eventSubscriber.d.ts +8 -0
- package/lib/node/events/eventSubscriber.d.ts.map +1 -1
- package/lib/node/events/eventSubscriber.js +5 -2
- package/lib/node/events/parse.d.ts +1 -1
- package/lib/node/events/parse.d.ts.map +1 -1
- package/lib/node/factory/oracleClient.d.ts +2 -2
- package/lib/node/factory/oracleClient.d.ts.map +1 -1
- package/lib/node/factory/oracleClient.js +9 -21
- package/lib/node/idl/drift.d.ts +21407 -0
- package/lib/node/idl/drift.d.ts.map +1 -0
- package/lib/node/idl/drift.js +2 -0
- package/lib/node/idl/drift.json +20850 -15287
- package/lib/node/idl/pyth.d.ts +98 -0
- package/lib/node/idl/pyth.d.ts.map +1 -0
- package/lib/node/idl/pyth.js +2 -0
- package/lib/node/idl/token_faucet.d.ts +198 -0
- package/lib/node/idl/token_faucet.d.ts.map +1 -0
- package/lib/node/idl/token_faucet.js +2 -0
- package/lib/node/idl/token_faucet.json +148 -61
- package/lib/node/index.d.ts +13 -6
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +12 -5
- package/lib/node/marinade/index.d.ts +1 -1
- package/lib/node/marinade/index.d.ts.map +1 -1
- package/lib/node/marinade/index.js +2 -2
- package/lib/node/math/amm.d.ts +4 -5
- package/lib/node/math/amm.d.ts.map +1 -1
- package/lib/node/math/amm.js +8 -35
- package/lib/node/math/funding.d.ts.map +1 -1
- package/lib/node/math/funding.js +6 -1
- package/lib/node/math/margin.d.ts +3 -4
- package/lib/node/math/margin.d.ts.map +1 -1
- package/lib/node/math/margin.js +16 -47
- package/lib/node/math/market.d.ts +2 -2
- package/lib/node/math/market.d.ts.map +1 -1
- package/lib/node/math/market.js +10 -42
- package/lib/node/math/oracles.d.ts.map +1 -1
- package/lib/node/math/oracles.js +14 -19
- package/lib/node/math/position.d.ts.map +1 -1
- package/lib/node/math/position.js +1 -1
- package/lib/node/math/trade.d.ts.map +1 -1
- package/lib/node/math/trade.js +4 -4
- package/lib/node/memcmp.js +8 -8
- package/lib/node/openbook/openbookV2Subscriber.d.ts +1 -1
- package/lib/node/openbook/openbookV2Subscriber.d.ts.map +1 -1
- package/lib/node/openbook/openbookV2Subscriber.js +3 -3
- package/lib/node/oracles/oracleClientCache.d.ts +2 -2
- package/lib/node/oracles/oracleClientCache.d.ts.map +1 -1
- package/lib/node/oracles/prelaunchOracleClient.d.ts +2 -2
- package/lib/node/oracles/prelaunchOracleClient.d.ts.map +1 -1
- package/lib/node/oracles/prelaunchOracleClient.js +1 -1
- package/lib/node/oracles/pythLazerClient.d.ts.map +1 -1
- package/lib/node/oracles/pythLazerClient.js +3 -5
- package/lib/node/oracles/types.d.ts +6 -0
- package/lib/node/oracles/types.d.ts.map +1 -1
- package/lib/node/orderParams.d.ts +0 -1
- package/lib/node/orderParams.d.ts.map +1 -1
- package/lib/node/orderParams.js +1 -5
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
- package/lib/node/orderSubscriber/OrderSubscriber.js +3 -4
- package/lib/node/orderSubscriber/WebsocketSubscription.js +1 -1
- package/lib/node/orderSubscriber/grpcSubscription.js +2 -2
- package/lib/node/orderSubscriber/index.d.ts +5 -0
- package/lib/node/orderSubscriber/index.d.ts.map +1 -1
- package/lib/node/orderSubscriber/index.js +5 -0
- package/lib/node/priorityFee/index.d.ts +5 -0
- package/lib/node/priorityFee/index.d.ts.map +1 -1
- package/lib/node/priorityFee/index.js +5 -0
- package/lib/node/pyth/index.d.ts +2 -2
- package/lib/node/pyth/index.d.ts.map +1 -1
- package/lib/node/pyth/types.d.ts +1 -1
- package/lib/node/pyth/types.d.ts.map +1 -1
- package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.js +1 -1
- package/lib/node/swift/index.d.ts +7 -0
- package/lib/node/swift/index.d.ts.map +1 -1
- package/lib/node/swift/index.js +7 -0
- package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts.map +1 -1
- package/lib/node/swift/signedMsgUserAccountSubscriber.js +4 -3
- package/lib/node/tokenFaucet.d.ts +2 -2
- package/lib/node/tokenFaucet.d.ts.map +1 -1
- package/lib/node/tokenFaucet.js +4 -4
- package/lib/node/tx/txHandler.d.ts +12 -0
- package/lib/node/tx/txHandler.d.ts.map +1 -1
- package/lib/node/tx/txHandler.js +12 -0
- package/lib/node/types.d.ts +19 -31
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/types.js +6 -11
- package/lib/node/user.d.ts +27 -18
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +64 -100
- package/lib/node/userMap/WebsocketSubscription.js +1 -1
- package/lib/node/userMap/grpcSubscription.js +1 -1
- package/lib/node/userMap/revenueShareEscrowMap.d.ts.map +1 -1
- package/lib/node/userMap/revenueShareEscrowMap.js +4 -4
- package/lib/node/userMap/userMap.d.ts.map +1 -1
- package/lib/node/userMap/userMap.js +4 -5
- package/lib/node/userMap/userStatsMap.d.ts.map +1 -1
- package/lib/node/userMap/userStatsMap.js +2 -3
- package/lib/node/userStats.d.ts +5 -0
- package/lib/node/userStats.d.ts.map +1 -1
- package/package.json +5 -7
- package/src/accounts/fetch.ts +18 -17
- package/src/accounts/grpcAccountSubscriber.ts +3 -3
- package/src/accounts/grpcDriftClientAccountSubscriber.ts +2 -3
- package/src/accounts/grpcDriftClientAccountSubscriberV2.ts +5 -6
- package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +2 -2
- package/src/accounts/grpcMultiAccountSubscriber.ts +4 -4
- package/src/accounts/grpcMultiUserAccountSubscriber.ts +4 -4
- package/src/accounts/grpcProgramAccountSubscriber.ts +3 -3
- package/src/accounts/grpcUserAccountSubscriber.ts +2 -2
- package/src/accounts/grpcUserStatsAccountSubscriber.ts +2 -2
- package/src/accounts/laserProgramAccountSubscriber.ts +3 -3
- package/src/accounts/oneShotUserAccountSubscriber.ts +6 -7
- package/src/accounts/oneShotUserStatsAccountSubscriber.ts +10 -9
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +12 -11
- package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +11 -10
- package/src/accounts/pollingOracleAccountSubscriber.ts +2 -2
- package/src/accounts/pollingTokenAccountSubscriber.ts +2 -2
- package/src/accounts/pollingUserAccountSubscriber.ts +2 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +14 -14
- package/src/accounts/testBulkAccountLoader.ts +3 -5
- package/src/accounts/types.ts +10 -25
- package/src/accounts/webSocketAccountSubscriber.ts +5 -5
- package/src/accounts/webSocketAccountSubscriberV2.ts +4 -3
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +9 -10
- package/src/accounts/webSocketDriftClientAccountSubscriberV2.ts +13 -10
- package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +3 -3
- package/src/accounts/webSocketProgramAccountSubscriber.ts +4 -3
- package/src/accounts/webSocketProgramAccountSubscriberV2.ts +4 -3
- package/src/accounts/webSocketProgramAccountsSubscriberV2.ts +4 -3
- package/src/accounts/webSocketUserAccountSubscriber.ts +3 -3
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +3 -3
- package/src/accounts/websocketProgramUserAccountSubscriber.ts +4 -4
- package/src/addresses/pda.ts +6 -22
- package/src/adminClient.ts +154 -239
- package/src/auctionSubscriber/auctionSubscriber.ts +5 -3
- package/src/auctionSubscriber/auctionSubscriberGrpc.ts +5 -3
- package/src/config.ts +24 -16
- package/src/constants/index.ts +5 -0
- package/src/constants/numericConstants.ts +2 -3
- package/src/constants/perpMarkets.ts +3 -445
- package/src/constants/spotMarkets.ts +7 -102
- package/src/constituentMap/constituentMap.ts +5 -6
- package/src/constituentMap/pollingConstituentAccountSubscriber.ts +3 -3
- package/src/constituentMap/webSocketConstituentAccountSubscriber.ts +6 -6
- package/src/decode/customCoder.ts +26 -35
- package/src/decode/user.ts +7 -25
- package/src/dlob/DLOB.ts +12 -0
- package/src/dlob/orderBookLevels.ts +0 -1
- package/src/driftClient.ts +245 -1091
- package/src/driftClientConfig.ts +7 -0
- package/src/events/eventSubscriber.ts +14 -2
- package/src/events/parse.ts +2 -2
- package/src/factory/oracleClient.ts +17 -28
- package/src/idl/drift.json +20850 -15287
- package/src/idl/drift.ts +21406 -0
- package/src/idl/pyth.ts +97 -0
- package/src/idl/token_faucet.json +148 -61
- package/src/idl/token_faucet.ts +197 -0
- package/src/index.ts +13 -12
- package/src/margin/README.md +0 -1
- package/src/marinade/index.ts +2 -2
- package/src/math/amm.ts +2 -48
- package/src/math/funding.ts +8 -3
- package/src/math/margin.ts +12 -67
- package/src/math/market.ts +15 -69
- package/src/math/oracles.ts +14 -30
- package/src/math/position.ts +0 -1
- package/src/math/trade.ts +0 -4
- package/src/memcmp.ts +1 -1
- package/src/openbook/openbookV2Subscriber.ts +1 -1
- package/src/oracles/oracleClientCache.ts +2 -2
- package/src/oracles/prelaunchOracleClient.ts +9 -8
- package/src/oracles/pythLazerClient.ts +10 -12
- package/src/oracles/types.ts +6 -0
- package/src/orderParams.ts +0 -5
- package/src/orderSubscriber/OrderSubscriber.ts +7 -6
- package/src/orderSubscriber/WebsocketSubscription.ts +1 -1
- package/src/orderSubscriber/grpcSubscription.ts +2 -2
- package/src/orderSubscriber/index.ts +5 -0
- package/src/priorityFee/index.ts +5 -0
- package/src/pyth/index.ts +4 -4
- package/src/pyth/types.ts +1 -1
- package/src/swift/grpcSignedMsgUserAccountSubscriber.ts +1 -1
- package/src/swift/index.ts +7 -0
- package/src/swift/signedMsgUserAccountSubscriber.ts +7 -4
- package/src/tokenFaucet.ts +8 -4
- package/src/tx/txHandler.ts +12 -0
- package/src/types.ts +16 -20
- package/src/user.ts +66 -144
- package/src/userMap/WebsocketSubscription.ts +1 -1
- package/src/userMap/grpcSubscription.ts +1 -1
- package/src/userMap/revenueShareEscrowMap.ts +24 -20
- package/src/userMap/userMap.ts +8 -7
- package/src/userMap/userStatsMap.ts +6 -5
- package/src/userStats.ts +5 -0
- package/tests/accounts/customizedCadenceBulkAccountLoader.test.ts +5 -4
- package/tests/amm/test.ts +45 -64
- package/tests/ci/verifyConstants.ts +23 -26
- package/tests/decode/test.ts +2 -4
- package/tests/dlob/helpers.ts +8 -15
- package/tests/user/helpers.ts +0 -2
- package/tests/user/marginCalculations.test.ts +2 -3
- package/tests/user/test.ts +18 -15
- package/lib/browser/accounts/pollingHighLeverageModeConfigAccountSubscriber.d.ts +0 -29
- package/lib/browser/accounts/pollingHighLeverageModeConfigAccountSubscriber.js +0 -111
- package/lib/browser/accounts/webSocketHighLeverageModeConfigAccountSubscriber.d.ts +0 -23
- package/lib/browser/accounts/webSocketHighLeverageModeConfigAccountSubscriber.js +0 -69
- package/lib/browser/idl/pyth_solana_receiver.json +0 -628
- package/lib/browser/idl/switchboard.json +0 -8354
- package/lib/browser/idl/switchboard_on_demand_30.json +0 -5849
- package/lib/browser/oracles/pythPullClient.d.ts +0 -19
- package/lib/browser/oracles/pythPullClient.js +0 -65
- package/lib/browser/oracles/switchboardClient.d.ts +0 -12
- package/lib/browser/oracles/switchboardClient.js +0 -40
- package/lib/browser/oracles/switchboardOnDemandClient.d.ts +0 -12
- package/lib/browser/oracles/switchboardOnDemandClient.js +0 -33
- package/lib/browser/util/pythOracleUtils.d.ts +0 -2
- package/lib/browser/util/pythOracleUtils.js +0 -15
- package/lib/node/accounts/pollingHighLeverageModeConfigAccountSubscriber.d.ts +0 -30
- package/lib/node/accounts/pollingHighLeverageModeConfigAccountSubscriber.d.ts.map +0 -1
- package/lib/node/accounts/pollingHighLeverageModeConfigAccountSubscriber.js +0 -111
- package/lib/node/accounts/webSocketHighLeverageModeConfigAccountSubscriber.d.ts +0 -24
- package/lib/node/accounts/webSocketHighLeverageModeConfigAccountSubscriber.d.ts.map +0 -1
- package/lib/node/accounts/webSocketHighLeverageModeConfigAccountSubscriber.js +0 -69
- package/lib/node/idl/pyth_solana_receiver.json +0 -628
- package/lib/node/idl/switchboard.json +0 -8354
- package/lib/node/idl/switchboard_on_demand_30.json +0 -5849
- package/lib/node/oracles/pythPullClient.d.ts +0 -20
- package/lib/node/oracles/pythPullClient.d.ts.map +0 -1
- package/lib/node/oracles/pythPullClient.js +0 -65
- package/lib/node/oracles/switchboardClient.d.ts +0 -13
- package/lib/node/oracles/switchboardClient.d.ts.map +0 -1
- package/lib/node/oracles/switchboardClient.js +0 -40
- package/lib/node/oracles/switchboardOnDemandClient.d.ts +0 -13
- package/lib/node/oracles/switchboardOnDemandClient.d.ts.map +0 -1
- package/lib/node/oracles/switchboardOnDemandClient.js +0 -33
- package/lib/node/util/pythOracleUtils.d.ts +0 -3
- package/lib/node/util/pythOracleUtils.d.ts.map +0 -1
- package/lib/node/util/pythOracleUtils.js +0 -15
- package/src/accounts/pollingHighLeverageModeConfigAccountSubscriber.ts +0 -189
- package/src/accounts/webSocketHighLeverageModeConfigAccountSubscriber.ts +0 -131
- package/src/idl/switchboard.json +0 -8354
- package/src/idl/switchboard_on_demand_30.json +0 -5849
- package/src/oracles/pythPullClient.ts +0 -112
- package/src/oracles/switchboardClient.ts +0 -77
- package/src/oracles/switchboardOnDemandClient.ts +0 -64
- package/src/util/pythOracleUtils.ts +0 -11
package/src/user.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User — read-oriented abstraction over the on-chain `User` account.
|
|
3
|
+
*
|
|
4
|
+
* Responsibilities:
|
|
5
|
+
* - Margin and free-collateral calculations (mirrors `programs/drift/src/math/margin.rs`).
|
|
6
|
+
* - Position accessors: perp positions, spot positions, unrealized PnL, leverage.
|
|
7
|
+
* - Open order queries and filtering.
|
|
8
|
+
* - Health factor and liquidation threshold checks.
|
|
9
|
+
* - Subscribes to and caches the latest `User` account state from chain.
|
|
10
|
+
*
|
|
11
|
+
* To send instructions (deposit, place order, etc.) use {@link DriftClient}.
|
|
12
|
+
* For referral/volume stats see {@link UserStats} (userStats.ts).
|
|
13
|
+
*/
|
|
1
14
|
import { PublicKey } from '@solana/web3.js';
|
|
2
15
|
import { EventEmitter } from 'events';
|
|
3
16
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
@@ -141,8 +154,10 @@ export class User {
|
|
|
141
154
|
config.driftClient.connection,
|
|
142
155
|
config.userAccountPublicKey,
|
|
143
156
|
config.accountSubscription.accountLoader,
|
|
144
|
-
|
|
145
|
-
this.driftClient.program.account
|
|
157
|
+
(
|
|
158
|
+
this.driftClient.program.account as any
|
|
159
|
+
).user.coder.accounts.decodeUnchecked.bind(
|
|
160
|
+
(this.driftClient.program.account as any).user.coder.accounts
|
|
146
161
|
)
|
|
147
162
|
);
|
|
148
163
|
} else if (config.accountSubscription?.type === 'custom') {
|
|
@@ -347,7 +362,6 @@ export class User {
|
|
|
347
362
|
settledPnl: ZERO,
|
|
348
363
|
lpShares: ZERO,
|
|
349
364
|
lastQuoteAssetAmountPerLp: ZERO,
|
|
350
|
-
perLpBase: 0,
|
|
351
365
|
maxMarginRatio: 0,
|
|
352
366
|
isolatedPositionScaledBalance: ZERO,
|
|
353
367
|
positionFlag: 0,
|
|
@@ -525,7 +539,6 @@ export class User {
|
|
|
525
539
|
public getPerpBuyingPower(
|
|
526
540
|
marketIndex: number,
|
|
527
541
|
collateralBuffer = ZERO,
|
|
528
|
-
enterHighLeverageMode = undefined,
|
|
529
542
|
maxMarginRatio = undefined,
|
|
530
543
|
positionType: 'isolated' | 'cross' = 'cross'
|
|
531
544
|
): BN {
|
|
@@ -560,7 +573,6 @@ export class User {
|
|
|
560
573
|
);
|
|
561
574
|
const generalFreeCollateral = this.getFreeCollateral(
|
|
562
575
|
'Initial',
|
|
563
|
-
enterHighLeverageMode,
|
|
564
576
|
undefined
|
|
565
577
|
);
|
|
566
578
|
freeCollateral = BN.min(
|
|
@@ -569,18 +581,15 @@ export class User {
|
|
|
569
581
|
).sub(collateralBuffer);
|
|
570
582
|
} else {
|
|
571
583
|
// free collateral from the cross account only
|
|
572
|
-
freeCollateral = this.getFreeCollateral(
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
undefined
|
|
576
|
-
).sub(collateralBuffer);
|
|
584
|
+
freeCollateral = this.getFreeCollateral('Initial', undefined).sub(
|
|
585
|
+
collateralBuffer
|
|
586
|
+
);
|
|
577
587
|
}
|
|
578
588
|
|
|
579
589
|
return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(
|
|
580
590
|
marketIndex,
|
|
581
591
|
freeCollateral,
|
|
582
592
|
worstCaseBaseAssetAmount,
|
|
583
|
-
enterHighLeverageMode,
|
|
584
593
|
maxMarginRatio || perpPosition.maxMarginRatio
|
|
585
594
|
);
|
|
586
595
|
}
|
|
@@ -589,7 +598,6 @@ export class User {
|
|
|
589
598
|
marketIndex: number,
|
|
590
599
|
freeCollateral: BN,
|
|
591
600
|
baseAssetAmount: BN,
|
|
592
|
-
enterHighLeverageMode = undefined,
|
|
593
601
|
perpMarketMaxMarginRatio = undefined
|
|
594
602
|
): BN {
|
|
595
603
|
const maxMarginRatio = Math.max(
|
|
@@ -600,8 +608,7 @@ export class User {
|
|
|
600
608
|
this.driftClient.getPerpMarketAccount(marketIndex),
|
|
601
609
|
baseAssetAmount,
|
|
602
610
|
'Initial',
|
|
603
|
-
maxMarginRatio
|
|
604
|
-
enterHighLeverageMode || this.isHighLeverageMode('Initial')
|
|
611
|
+
maxMarginRatio
|
|
605
612
|
);
|
|
606
613
|
|
|
607
614
|
return freeCollateral.mul(MARGIN_PRECISION).div(new BN(marginRatio));
|
|
@@ -613,11 +620,9 @@ export class User {
|
|
|
613
620
|
*/
|
|
614
621
|
public getFreeCollateral(
|
|
615
622
|
marginCategory: MarginCategory = 'Initial',
|
|
616
|
-
enterHighLeverageMode = false,
|
|
617
623
|
perpMarketIndex?: number
|
|
618
624
|
): BN {
|
|
619
625
|
const calc = this.getMarginCalculation(marginCategory, {
|
|
620
|
-
enteringHighLeverage: enterHighLeverageMode,
|
|
621
626
|
strict: marginCategory === 'Initial',
|
|
622
627
|
});
|
|
623
628
|
|
|
@@ -640,8 +645,7 @@ export class User {
|
|
|
640
645
|
marginCategory: MarginCategory,
|
|
641
646
|
liquidationBuffer?: BN,
|
|
642
647
|
strict?: boolean,
|
|
643
|
-
includeOpenOrders?: boolean
|
|
644
|
-
enteringHighLeverage?: boolean
|
|
648
|
+
includeOpenOrders?: boolean
|
|
645
649
|
): BN;
|
|
646
650
|
|
|
647
651
|
/**
|
|
@@ -651,7 +655,6 @@ export class User {
|
|
|
651
655
|
* @param liquidationBuffer - Optional buffer amount to consider during liquidation scenarios.
|
|
652
656
|
* @param strict - Optional flag to enforce strict margin calculations.
|
|
653
657
|
* @param includeOpenOrders - Optional flag to include open orders in the margin calculation.
|
|
654
|
-
* @param enteringHighLeverage - Optional flag indicating if the user is entering high leverage mode.
|
|
655
658
|
* @param perpMarketIndex - Optional index of the perpetual market. Required if marginType is 'Isolated'.
|
|
656
659
|
*
|
|
657
660
|
* @returns The calculated margin requirement as a BN (BigNumber).
|
|
@@ -661,7 +664,6 @@ export class User {
|
|
|
661
664
|
liquidationBuffer?: BN,
|
|
662
665
|
strict?: boolean,
|
|
663
666
|
includeOpenOrders?: boolean,
|
|
664
|
-
enteringHighLeverage?: boolean,
|
|
665
667
|
perpMarketIndex?: number
|
|
666
668
|
): BN;
|
|
667
669
|
|
|
@@ -670,7 +672,6 @@ export class User {
|
|
|
670
672
|
liquidationBuffer?: BN,
|
|
671
673
|
strict?: boolean,
|
|
672
674
|
includeOpenOrders?: boolean,
|
|
673
|
-
enteringHighLeverage?: boolean,
|
|
674
675
|
perpMarketIndex?: number
|
|
675
676
|
): BN {
|
|
676
677
|
const liquidationBufferMap = new Map();
|
|
@@ -683,7 +684,6 @@ export class User {
|
|
|
683
684
|
const marginCalc = this.getMarginCalculation(marginCategory, {
|
|
684
685
|
strict,
|
|
685
686
|
includeOpenOrders,
|
|
686
|
-
enteringHighLeverage,
|
|
687
687
|
liquidationBufferMap,
|
|
688
688
|
});
|
|
689
689
|
|
|
@@ -711,16 +711,12 @@ export class User {
|
|
|
711
711
|
/**
|
|
712
712
|
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
713
713
|
*/
|
|
714
|
-
public getInitialMarginRequirement(
|
|
715
|
-
enterHighLeverageMode = false,
|
|
716
|
-
perpMarketIndex?: number
|
|
717
|
-
): BN {
|
|
714
|
+
public getInitialMarginRequirement(perpMarketIndex?: number): BN {
|
|
718
715
|
return this.getMarginRequirement(
|
|
719
716
|
'Initial',
|
|
720
717
|
undefined,
|
|
721
718
|
true,
|
|
722
719
|
undefined,
|
|
723
|
-
enterHighLeverageMode,
|
|
724
720
|
perpMarketIndex
|
|
725
721
|
);
|
|
726
722
|
}
|
|
@@ -737,7 +733,6 @@ export class User {
|
|
|
737
733
|
liquidationBuffer,
|
|
738
734
|
false, // strict default
|
|
739
735
|
true, // includeOpenOrders default
|
|
740
|
-
false, // enteringHighLeverage default
|
|
741
736
|
perpMarketIndex
|
|
742
737
|
);
|
|
743
738
|
}
|
|
@@ -1438,8 +1433,7 @@ export class User {
|
|
|
1438
1433
|
marginCategory?: MarginCategory,
|
|
1439
1434
|
liquidationBuffer?: BN,
|
|
1440
1435
|
includeOpenOrders?: boolean,
|
|
1441
|
-
strict = false
|
|
1442
|
-
enteringHighLeverage = undefined
|
|
1436
|
+
strict = false
|
|
1443
1437
|
): BN {
|
|
1444
1438
|
const market = this.driftClient.getPerpMarketAccount(
|
|
1445
1439
|
perpPosition.marketIndex
|
|
@@ -1468,8 +1462,7 @@ export class User {
|
|
|
1468
1462
|
baseAssetAmount = perpPosition.baseAssetAmount;
|
|
1469
1463
|
liabilityValue = calculatePerpLiabilityValue(
|
|
1470
1464
|
baseAssetAmount,
|
|
1471
|
-
valuationPrice
|
|
1472
|
-
isVariant(market.contractType, 'prediction')
|
|
1465
|
+
valuationPrice
|
|
1473
1466
|
);
|
|
1474
1467
|
}
|
|
1475
1468
|
|
|
@@ -1483,11 +1476,7 @@ export class User {
|
|
|
1483
1476
|
market,
|
|
1484
1477
|
baseAssetAmount.abs(),
|
|
1485
1478
|
marginCategory,
|
|
1486
|
-
|
|
1487
|
-
? Math.max(market.marginRatioInitial, userCustomMargin)
|
|
1488
|
-
: userCustomMargin,
|
|
1489
|
-
this.isHighLeverageMode(marginCategory) ||
|
|
1490
|
-
enteringHighLeverage === true
|
|
1479
|
+
userCustomMargin
|
|
1491
1480
|
)
|
|
1492
1481
|
);
|
|
1493
1482
|
|
|
@@ -1561,8 +1550,7 @@ export class User {
|
|
|
1561
1550
|
marginCategory?: MarginCategory,
|
|
1562
1551
|
liquidationBuffer?: BN,
|
|
1563
1552
|
includeOpenOrders?: boolean,
|
|
1564
|
-
strict = false
|
|
1565
|
-
enteringHighLeverage = undefined
|
|
1553
|
+
strict = false
|
|
1566
1554
|
): BN {
|
|
1567
1555
|
return this.getActivePerpPositions().reduce(
|
|
1568
1556
|
(totalPerpValue, perpPosition) => {
|
|
@@ -1571,8 +1559,7 @@ export class User {
|
|
|
1571
1559
|
marginCategory,
|
|
1572
1560
|
liquidationBuffer,
|
|
1573
1561
|
includeOpenOrders,
|
|
1574
|
-
strict
|
|
1575
|
-
enteringHighLeverage
|
|
1562
|
+
strict
|
|
1576
1563
|
);
|
|
1577
1564
|
return totalPerpValue.add(baseAssetValue);
|
|
1578
1565
|
},
|
|
@@ -1624,8 +1611,7 @@ export class User {
|
|
|
1624
1611
|
} else {
|
|
1625
1612
|
return calculatePerpLiabilityValue(
|
|
1626
1613
|
userPosition.baseAssetAmount,
|
|
1627
|
-
oraclePriceData.price
|
|
1628
|
-
isVariant(market.contractType, 'prediction')
|
|
1614
|
+
oraclePriceData.price
|
|
1629
1615
|
);
|
|
1630
1616
|
}
|
|
1631
1617
|
}
|
|
@@ -1933,14 +1919,12 @@ export class User {
|
|
|
1933
1919
|
* for large sizes where imf factor activates, result is a lower bound
|
|
1934
1920
|
* @param marginCategory {Initial, Maintenance}
|
|
1935
1921
|
* @param isLp if calculating max leveraging for adding lp, need to add buffer
|
|
1936
|
-
* @param enterHighLeverageMode can pass this as true to calculate max leverage if the user was to enter high leverage mode
|
|
1937
1922
|
* @returns : Precision TEN_THOUSAND
|
|
1938
1923
|
*/
|
|
1939
1924
|
public getMaxLeverageForPerp(
|
|
1940
1925
|
perpMarketIndex: number,
|
|
1941
1926
|
_marginCategory: MarginCategory = 'Initial',
|
|
1942
|
-
isLp = false
|
|
1943
|
-
enterHighLeverageMode = undefined
|
|
1927
|
+
isLp = false
|
|
1944
1928
|
): BN {
|
|
1945
1929
|
const market = this.driftClient.getPerpMarketAccount(perpMarketIndex);
|
|
1946
1930
|
const marketPrice =
|
|
@@ -1969,14 +1953,12 @@ export class User {
|
|
|
1969
1953
|
this.getMaxTradeSizeUSDCForPerp(
|
|
1970
1954
|
perpMarketIndex,
|
|
1971
1955
|
PositionDirection.LONG,
|
|
1972
|
-
false
|
|
1973
|
-
enterHighLeverageMode || this.isHighLeverageMode('Initial')
|
|
1956
|
+
false
|
|
1974
1957
|
).tradeSize,
|
|
1975
1958
|
this.getMaxTradeSizeUSDCForPerp(
|
|
1976
1959
|
perpMarketIndex,
|
|
1977
1960
|
PositionDirection.SHORT,
|
|
1978
|
-
false
|
|
1979
|
-
enterHighLeverageMode || this.isHighLeverageMode('Initial')
|
|
1961
|
+
false
|
|
1980
1962
|
).tradeSize
|
|
1981
1963
|
).sub(lpBuffer),
|
|
1982
1964
|
ZERO
|
|
@@ -2241,14 +2223,6 @@ export class User {
|
|
|
2241
2223
|
return (this.getUserAccount().status & UserStatus.BANKRUPT) > 0;
|
|
2242
2224
|
}
|
|
2243
2225
|
|
|
2244
|
-
public isHighLeverageMode(marginCategory: MarginCategory): boolean {
|
|
2245
|
-
return (
|
|
2246
|
-
isVariant(this.getUserAccount().marginMode, 'highLeverage') ||
|
|
2247
|
-
(marginCategory === 'Maintenance' &&
|
|
2248
|
-
isVariant(this.getUserAccount().marginMode, 'highLeverageMaintenance'))
|
|
2249
|
-
);
|
|
2250
|
-
}
|
|
2251
|
-
|
|
2252
2226
|
/**
|
|
2253
2227
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
2254
2228
|
* @returns
|
|
@@ -2389,7 +2363,6 @@ export class User {
|
|
|
2389
2363
|
marginCategory: MarginCategory = 'Maintenance',
|
|
2390
2364
|
includeOpenOrders = false,
|
|
2391
2365
|
offsetCollateral = ZERO,
|
|
2392
|
-
enteringHighLeverage = false,
|
|
2393
2366
|
marginType?: MarginType
|
|
2394
2367
|
): BN {
|
|
2395
2368
|
const market = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
@@ -2406,7 +2379,6 @@ export class User {
|
|
|
2406
2379
|
const marginCalculation = this.getMarginCalculation(marginCategory, {
|
|
2407
2380
|
strict: false,
|
|
2408
2381
|
includeOpenOrders,
|
|
2409
|
-
enteringHighLeverage,
|
|
2410
2382
|
});
|
|
2411
2383
|
const isolatedMarginCalculation =
|
|
2412
2384
|
marginCalculation.isolatedMarginCalculations.get(marketIndex);
|
|
@@ -2424,8 +2396,7 @@ export class User {
|
|
|
2424
2396
|
positionBaseSizeChange,
|
|
2425
2397
|
oraclePrice,
|
|
2426
2398
|
marginCategory,
|
|
2427
|
-
includeOpenOrders
|
|
2428
|
-
enteringHighLeverage
|
|
2399
|
+
includeOpenOrders
|
|
2429
2400
|
);
|
|
2430
2401
|
|
|
2431
2402
|
if (!freeCollateralDelta || freeCollateralDelta.eq(ZERO)) {
|
|
@@ -2455,8 +2426,7 @@ export class User {
|
|
|
2455
2426
|
marginCategory,
|
|
2456
2427
|
undefined,
|
|
2457
2428
|
false,
|
|
2458
|
-
includeOpenOrders
|
|
2459
|
-
enteringHighLeverage
|
|
2429
|
+
includeOpenOrders
|
|
2460
2430
|
);
|
|
2461
2431
|
|
|
2462
2432
|
let freeCollateral = BN.max(
|
|
@@ -2476,8 +2446,7 @@ export class User {
|
|
|
2476
2446
|
currentPerpPosition,
|
|
2477
2447
|
positionBaseSizeChange,
|
|
2478
2448
|
estimatedEntryPrice,
|
|
2479
|
-
includeOpenOrders
|
|
2480
|
-
enteringHighLeverage
|
|
2449
|
+
includeOpenOrders
|
|
2481
2450
|
);
|
|
2482
2451
|
|
|
2483
2452
|
freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
|
|
@@ -2488,8 +2457,7 @@ export class User {
|
|
|
2488
2457
|
positionBaseSizeChange,
|
|
2489
2458
|
oraclePrice,
|
|
2490
2459
|
marginCategory,
|
|
2491
|
-
includeOpenOrders
|
|
2492
|
-
enteringHighLeverage
|
|
2460
|
+
includeOpenOrders
|
|
2493
2461
|
);
|
|
2494
2462
|
|
|
2495
2463
|
if (!freeCollateralDelta) {
|
|
@@ -2559,7 +2527,6 @@ export class User {
|
|
|
2559
2527
|
positionBaseSizeChange: BN,
|
|
2560
2528
|
estimatedEntryPrice: BN,
|
|
2561
2529
|
includeOpenOrders: boolean,
|
|
2562
|
-
enteringHighLeverage = undefined,
|
|
2563
2530
|
marginCategory: MarginCategory = 'Maintenance'
|
|
2564
2531
|
): BN {
|
|
2565
2532
|
let freeCollateralChange = ZERO;
|
|
@@ -2607,8 +2574,7 @@ export class User {
|
|
|
2607
2574
|
baseAssetAmount = perpPosition.baseAssetAmount;
|
|
2608
2575
|
liabilityValue = calculatePerpLiabilityValue(
|
|
2609
2576
|
baseAssetAmount,
|
|
2610
|
-
oraclePrice
|
|
2611
|
-
isVariant(market.contractType, 'prediction')
|
|
2577
|
+
oraclePrice
|
|
2612
2578
|
);
|
|
2613
2579
|
}
|
|
2614
2580
|
|
|
@@ -2620,10 +2586,7 @@ export class User {
|
|
|
2620
2586
|
market,
|
|
2621
2587
|
baseAssetAmount.abs(),
|
|
2622
2588
|
marginCategory,
|
|
2623
|
-
|
|
2624
|
-
? Math.max(market.marginRatioInitial, userCustomMargin)
|
|
2625
|
-
: userCustomMargin,
|
|
2626
|
-
this.isHighLeverageMode(marginCategory) || enteringHighLeverage === true
|
|
2589
|
+
userCustomMargin
|
|
2627
2590
|
);
|
|
2628
2591
|
|
|
2629
2592
|
return liabilityValue.mul(new BN(marginRatio)).div(MARGIN_PRECISION);
|
|
@@ -2651,8 +2614,7 @@ export class User {
|
|
|
2651
2614
|
positionBaseSizeChange: BN,
|
|
2652
2615
|
oraclePrice: BN,
|
|
2653
2616
|
marginCategory: MarginCategory = 'Maintenance',
|
|
2654
|
-
includeOpenOrders = false
|
|
2655
|
-
enteringHighLeverage = undefined
|
|
2617
|
+
includeOpenOrders = false
|
|
2656
2618
|
): BN | undefined {
|
|
2657
2619
|
const baseAssetAmount = includeOpenOrders
|
|
2658
2620
|
? calculateWorstCaseBaseAssetAmount(perpPosition, market, oraclePrice)
|
|
@@ -2674,10 +2636,7 @@ export class User {
|
|
|
2674
2636
|
market,
|
|
2675
2637
|
proposedBaseAssetAmount.abs(),
|
|
2676
2638
|
marginCategory,
|
|
2677
|
-
|
|
2678
|
-
? Math.max(market.marginRatioInitial, userCustomMargin)
|
|
2679
|
-
: userCustomMargin,
|
|
2680
|
-
this.isHighLeverageMode(marginCategory) || enteringHighLeverage === true
|
|
2639
|
+
userCustomMargin
|
|
2681
2640
|
);
|
|
2682
2641
|
|
|
2683
2642
|
const marginRatioQuotePrecision = new BN(marginRatio)
|
|
@@ -2689,33 +2648,24 @@ export class User {
|
|
|
2689
2648
|
}
|
|
2690
2649
|
|
|
2691
2650
|
let freeCollateralDelta = ZERO;
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
} else if (orderBaseAssetAmount.lt(ZERO)) {
|
|
2698
|
-
freeCollateralDelta = marginRatioQuotePrecision;
|
|
2699
|
-
}
|
|
2651
|
+
|
|
2652
|
+
if (proposedBaseAssetAmount.gt(ZERO)) {
|
|
2653
|
+
freeCollateralDelta = QUOTE_PRECISION.sub(marginRatioQuotePrecision)
|
|
2654
|
+
.mul(proposedBaseAssetAmount)
|
|
2655
|
+
.div(BASE_PRECISION);
|
|
2700
2656
|
} else {
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
freeCollateralDelta = QUOTE_PRECISION.neg()
|
|
2707
|
-
.sub(marginRatioQuotePrecision)
|
|
2708
|
-
.mul(proposedBaseAssetAmount.abs())
|
|
2709
|
-
.div(BASE_PRECISION);
|
|
2710
|
-
}
|
|
2657
|
+
freeCollateralDelta = QUOTE_PRECISION.neg()
|
|
2658
|
+
.sub(marginRatioQuotePrecision)
|
|
2659
|
+
.mul(proposedBaseAssetAmount.abs())
|
|
2660
|
+
.div(BASE_PRECISION);
|
|
2661
|
+
}
|
|
2711
2662
|
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
}
|
|
2663
|
+
if (!orderBaseAssetAmount.eq(ZERO)) {
|
|
2664
|
+
freeCollateralDelta = freeCollateralDelta.sub(
|
|
2665
|
+
marginRatioQuotePrecision
|
|
2666
|
+
.mul(orderBaseAssetAmount.abs())
|
|
2667
|
+
.div(BASE_PRECISION)
|
|
2668
|
+
);
|
|
2719
2669
|
}
|
|
2720
2670
|
|
|
2721
2671
|
return freeCollateralDelta;
|
|
@@ -2800,7 +2750,6 @@ export class User {
|
|
|
2800
2750
|
targetMarketIndex,
|
|
2801
2751
|
baseSize,
|
|
2802
2752
|
maxMarginRatio,
|
|
2803
|
-
undefined,
|
|
2804
2753
|
estEntryPrice
|
|
2805
2754
|
);
|
|
2806
2755
|
}
|
|
@@ -2820,8 +2769,7 @@ export class User {
|
|
|
2820
2769
|
targetMarketIndex,
|
|
2821
2770
|
baseSize,
|
|
2822
2771
|
collateralIndex,
|
|
2823
|
-
maxMarginRatio
|
|
2824
|
-
false // assume user cant be high leverage if they havent created user account ?
|
|
2772
|
+
maxMarginRatio
|
|
2825
2773
|
);
|
|
2826
2774
|
}
|
|
2827
2775
|
|
|
@@ -2838,7 +2786,6 @@ export class User {
|
|
|
2838
2786
|
targetMarketIndex: number,
|
|
2839
2787
|
tradeSide: PositionDirection,
|
|
2840
2788
|
isLp = false,
|
|
2841
|
-
enterHighLeverageMode = undefined,
|
|
2842
2789
|
maxMarginRatio = undefined,
|
|
2843
2790
|
positionType: 'isolated' | 'cross' = 'cross'
|
|
2844
2791
|
): { tradeSize: BN; oppositeSideTradeSize: BN } {
|
|
@@ -2872,14 +2819,12 @@ export class User {
|
|
|
2872
2819
|
? ZERO
|
|
2873
2820
|
: calculatePerpLiabilityValue(
|
|
2874
2821
|
currentPosition.baseAssetAmount,
|
|
2875
|
-
oracleData.price
|
|
2876
|
-
isVariant(marketAccount.contractType, 'prediction')
|
|
2822
|
+
oracleData.price
|
|
2877
2823
|
);
|
|
2878
2824
|
|
|
2879
2825
|
const maxPositionSize = this.getPerpBuyingPower(
|
|
2880
2826
|
targetMarketIndex,
|
|
2881
2827
|
lpBuffer,
|
|
2882
|
-
enterHighLeverageMode,
|
|
2883
2828
|
maxMarginRatio,
|
|
2884
2829
|
positionType
|
|
2885
2830
|
);
|
|
@@ -2898,16 +2843,12 @@ export class User {
|
|
|
2898
2843
|
// current leverage is greater than max leverage - can only reduce position size
|
|
2899
2844
|
|
|
2900
2845
|
if (!targetingSameSide) {
|
|
2901
|
-
const market = this.driftClient.getPerpMarketAccount(targetMarketIndex);
|
|
2902
2846
|
const perpLiabilityValue = calculatePerpLiabilityValue(
|
|
2903
2847
|
currentPosition.baseAssetAmount,
|
|
2904
|
-
oracleData.price
|
|
2905
|
-
isVariant(market.contractType, 'prediction')
|
|
2848
|
+
oracleData.price
|
|
2906
2849
|
);
|
|
2907
2850
|
const totalCollateral = this.getTotalCollateral();
|
|
2908
|
-
const marginRequirement = this.getInitialMarginRequirement(
|
|
2909
|
-
enterHighLeverageMode
|
|
2910
|
-
);
|
|
2851
|
+
const marginRequirement = this.getInitialMarginRequirement();
|
|
2911
2852
|
const marginRatio = Math.max(
|
|
2912
2853
|
currentPosition.maxMarginRatio,
|
|
2913
2854
|
this.getUserAccount().maxMarginRatio
|
|
@@ -2941,10 +2882,7 @@ export class User {
|
|
|
2941
2882
|
}
|
|
2942
2883
|
}
|
|
2943
2884
|
|
|
2944
|
-
const freeCollateral = this.getFreeCollateral(
|
|
2945
|
-
'Initial',
|
|
2946
|
-
enterHighLeverageMode
|
|
2947
|
-
);
|
|
2885
|
+
const freeCollateral = this.getFreeCollateral('Initial');
|
|
2948
2886
|
|
|
2949
2887
|
let baseTradeSize =
|
|
2950
2888
|
targetSide === 'long'
|
|
@@ -2959,7 +2897,6 @@ export class User {
|
|
|
2959
2897
|
baseTradeSize,
|
|
2960
2898
|
oracleData.price,
|
|
2961
2899
|
false,
|
|
2962
|
-
enterHighLeverageMode,
|
|
2963
2900
|
'Initial'
|
|
2964
2901
|
);
|
|
2965
2902
|
|
|
@@ -2980,7 +2917,6 @@ export class User {
|
|
|
2980
2917
|
baseTradeSize,
|
|
2981
2918
|
oracleData.price,
|
|
2982
2919
|
false,
|
|
2983
|
-
enterHighLeverageMode,
|
|
2984
2920
|
'Initial'
|
|
2985
2921
|
);
|
|
2986
2922
|
}
|
|
@@ -3698,10 +3634,6 @@ export class User {
|
|
|
3698
3634
|
|
|
3699
3635
|
const feeTierIndex = 0;
|
|
3700
3636
|
if (isVariant(marketType, 'perp')) {
|
|
3701
|
-
if (this.isHighLeverageMode('Initial')) {
|
|
3702
|
-
return state.perpFeeStructure.feeTiers[0];
|
|
3703
|
-
}
|
|
3704
|
-
|
|
3705
3637
|
const userStatsAccount: UserStatsAccount = this.driftClient
|
|
3706
3638
|
.getUserStats()
|
|
3707
3639
|
.getAccount();
|
|
@@ -3766,17 +3698,12 @@ export class User {
|
|
|
3766
3698
|
* @param quoteAmount
|
|
3767
3699
|
* @returns feeForQuote : Precision QUOTE_PRECISION
|
|
3768
3700
|
*/
|
|
3769
|
-
public calculateFeeForQuoteAmount(
|
|
3770
|
-
quoteAmount: BN,
|
|
3771
|
-
marketIndex?: number,
|
|
3772
|
-
enteringHighLeverageMode?: boolean
|
|
3773
|
-
): BN {
|
|
3701
|
+
public calculateFeeForQuoteAmount(quoteAmount: BN, marketIndex?: number): BN {
|
|
3774
3702
|
if (marketIndex !== undefined) {
|
|
3775
3703
|
const takerFeeMultiplier = this.driftClient.getMarketFees(
|
|
3776
3704
|
MarketType.PERP,
|
|
3777
3705
|
marketIndex,
|
|
3778
|
-
this
|
|
3779
|
-
enteringHighLeverageMode
|
|
3706
|
+
this
|
|
3780
3707
|
).takerFee;
|
|
3781
3708
|
const feeAmountNum =
|
|
3782
3709
|
BigNum.from(quoteAmount, QUOTE_PRECISION_EXP).toNum() *
|
|
@@ -4130,8 +4057,7 @@ export class User {
|
|
|
4130
4057
|
worstCaseBaseAmount = perpPosition.baseAssetAmount;
|
|
4131
4058
|
worstCaseLiabilityValue = calculatePerpLiabilityValue(
|
|
4132
4059
|
perpPosition.baseAssetAmount,
|
|
4133
|
-
oraclePrice
|
|
4134
|
-
isVariant(perpMarket.contractType, 'prediction')
|
|
4060
|
+
oraclePrice
|
|
4135
4061
|
);
|
|
4136
4062
|
}
|
|
4137
4063
|
|
|
@@ -4144,8 +4070,7 @@ export class User {
|
|
|
4144
4070
|
perpMarket,
|
|
4145
4071
|
worstCaseBaseAmount.abs(),
|
|
4146
4072
|
marginCategory,
|
|
4147
|
-
userCustomMargin
|
|
4148
|
-
this.isHighLeverageMode(marginCategory)
|
|
4073
|
+
userCustomMargin
|
|
4149
4074
|
)
|
|
4150
4075
|
);
|
|
4151
4076
|
|
|
@@ -4427,12 +4352,10 @@ export class User {
|
|
|
4427
4352
|
opts?: {
|
|
4428
4353
|
strict?: boolean; // mirror StrictOraclePrice application
|
|
4429
4354
|
includeOpenOrders?: boolean;
|
|
4430
|
-
enteringHighLeverage?: boolean;
|
|
4431
4355
|
liquidationBufferMap?: Map<number | 'cross', BN>; // margin_buffer analog for buffer mode
|
|
4432
4356
|
}
|
|
4433
4357
|
): MarginCalculation {
|
|
4434
4358
|
const strict = opts?.strict ?? false;
|
|
4435
|
-
const enteringHighLeverage = opts?.enteringHighLeverage ?? false;
|
|
4436
4359
|
const liquidationBufferMap = opts?.liquidationBufferMap ?? new Map();
|
|
4437
4360
|
const includeOpenOrders = opts?.includeOpenOrders ?? true;
|
|
4438
4361
|
|
|
@@ -4604,8 +4527,7 @@ export class User {
|
|
|
4604
4527
|
market,
|
|
4605
4528
|
worstCaseBaseAssetAmount.abs(),
|
|
4606
4529
|
marginCategory,
|
|
4607
|
-
customMarginRatio
|
|
4608
|
-
this.isHighLeverageMode(marginCategory) || enteringHighLeverage
|
|
4530
|
+
customMarginRatio
|
|
4609
4531
|
)
|
|
4610
4532
|
);
|
|
4611
4533
|
if (isVariant(market.status, 'settlement')) {
|
|
@@ -76,11 +76,12 @@ export class RevenueShareEscrowMap {
|
|
|
76
76
|
);
|
|
77
77
|
|
|
78
78
|
if (accountInfo && accountInfo.data) {
|
|
79
|
-
const escrow =
|
|
80
|
-
this.driftClient.program.account
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
79
|
+
const escrow = (
|
|
80
|
+
this.driftClient.program.account as any
|
|
81
|
+
).revenueShareEscrow.coder.accounts.decode(
|
|
82
|
+
'revenueShareEscrow',
|
|
83
|
+
accountInfo.data
|
|
84
|
+
) as RevenueShareEscrowAccount;
|
|
84
85
|
|
|
85
86
|
this.authorityEscrowMap.set(authority, escrow);
|
|
86
87
|
}
|
|
@@ -129,11 +130,12 @@ export class RevenueShareEscrowMap {
|
|
|
129
130
|
),
|
|
130
131
|
'confirmed'
|
|
131
132
|
);
|
|
132
|
-
const escrowNew =
|
|
133
|
-
this.driftClient.program.account
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
const escrowNew = (
|
|
134
|
+
this.driftClient.program.account as any
|
|
135
|
+
).revenueShareEscrow.coder.accounts.decode(
|
|
136
|
+
'revenueShareEscrow',
|
|
137
|
+
accountInfo.data
|
|
138
|
+
) as RevenueShareEscrowAccount;
|
|
137
139
|
this.authorityEscrowMap.set(authority, escrowNew);
|
|
138
140
|
}
|
|
139
141
|
}
|
|
@@ -179,11 +181,12 @@ export class RevenueShareEscrowMap {
|
|
|
179
181
|
programAccount.account.data[1]
|
|
180
182
|
);
|
|
181
183
|
|
|
182
|
-
const escrow =
|
|
183
|
-
this.driftClient.program.account
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
184
|
+
const escrow = (
|
|
185
|
+
this.driftClient.program.account as any
|
|
186
|
+
).revenueShareEscrow.coder.accounts.decode(
|
|
187
|
+
'revenueShareEscrow',
|
|
188
|
+
buffer
|
|
189
|
+
) as RevenueShareEscrowAccount;
|
|
187
190
|
|
|
188
191
|
// Extract authority from the account data
|
|
189
192
|
const authorityKey = escrow.authority.toBase58();
|
|
@@ -205,11 +208,12 @@ export class RevenueShareEscrowMap {
|
|
|
205
208
|
programAccount.account.data[1]
|
|
206
209
|
);
|
|
207
210
|
|
|
208
|
-
const escrow =
|
|
209
|
-
this.driftClient.program.account
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
211
|
+
const escrow = (
|
|
212
|
+
this.driftClient.program.account as any
|
|
213
|
+
).revenueShareEscrow.coder.accounts.decode(
|
|
214
|
+
'revenueShareEscrow',
|
|
215
|
+
buffer
|
|
216
|
+
) as RevenueShareEscrowAccount;
|
|
213
217
|
|
|
214
218
|
// Extract authority from the account data
|
|
215
219
|
const authorityKey = escrow.authority.toBase58();
|