@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/lib/browser/user.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="bn.js" />
|
|
3
|
+
/**
|
|
4
|
+
* User — read-oriented abstraction over the on-chain `User` account.
|
|
5
|
+
*
|
|
6
|
+
* Responsibilities:
|
|
7
|
+
* - Margin and free-collateral calculations (mirrors `programs/drift/src/math/margin.rs`).
|
|
8
|
+
* - Position accessors: perp positions, spot positions, unrealized PnL, leverage.
|
|
9
|
+
* - Open order queries and filtering.
|
|
10
|
+
* - Health factor and liquidation threshold checks.
|
|
11
|
+
* - Subscribes to and caches the latest `User` account state from chain.
|
|
12
|
+
*
|
|
13
|
+
* To send instructions (deposit, place order, etc.) use {@link DriftClient}.
|
|
14
|
+
* For referral/volume stats see {@link UserStats} (userStats.ts).
|
|
15
|
+
*/
|
|
3
16
|
import { PublicKey } from '@solana/web3.js';
|
|
4
17
|
import { EventEmitter } from 'events';
|
|
5
18
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
@@ -98,17 +111,17 @@ export declare class User {
|
|
|
98
111
|
* calculates Buying Power = free collateral / initial margin ratio
|
|
99
112
|
* @returns : Precision QUOTE_PRECISION
|
|
100
113
|
*/
|
|
101
|
-
getPerpBuyingPower(marketIndex: number, collateralBuffer?: BN,
|
|
102
|
-
getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex: number, freeCollateral: BN, baseAssetAmount: BN,
|
|
114
|
+
getPerpBuyingPower(marketIndex: number, collateralBuffer?: BN, maxMarginRatio?: any, positionType?: 'isolated' | 'cross'): BN;
|
|
115
|
+
getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex: number, freeCollateral: BN, baseAssetAmount: BN, perpMarketMaxMarginRatio?: any): BN;
|
|
103
116
|
/**
|
|
104
117
|
* calculates Free Collateral = Total collateral - margin requirement
|
|
105
118
|
* @returns : Precision QUOTE_PRECISION
|
|
106
119
|
*/
|
|
107
|
-
getFreeCollateral(marginCategory?: MarginCategory,
|
|
120
|
+
getFreeCollateral(marginCategory?: MarginCategory, perpMarketIndex?: number): BN;
|
|
108
121
|
/**
|
|
109
122
|
* @deprecated Use the overload that includes { marginType, perpMarketIndex }
|
|
110
123
|
*/
|
|
111
|
-
getMarginRequirement(marginCategory: MarginCategory, liquidationBuffer?: BN, strict?: boolean, includeOpenOrders?: boolean
|
|
124
|
+
getMarginRequirement(marginCategory: MarginCategory, liquidationBuffer?: BN, strict?: boolean, includeOpenOrders?: boolean): BN;
|
|
112
125
|
/**
|
|
113
126
|
* Calculates the margin requirement based on the specified parameters.
|
|
114
127
|
*
|
|
@@ -116,16 +129,15 @@ export declare class User {
|
|
|
116
129
|
* @param liquidationBuffer - Optional buffer amount to consider during liquidation scenarios.
|
|
117
130
|
* @param strict - Optional flag to enforce strict margin calculations.
|
|
118
131
|
* @param includeOpenOrders - Optional flag to include open orders in the margin calculation.
|
|
119
|
-
* @param enteringHighLeverage - Optional flag indicating if the user is entering high leverage mode.
|
|
120
132
|
* @param perpMarketIndex - Optional index of the perpetual market. Required if marginType is 'Isolated'.
|
|
121
133
|
*
|
|
122
134
|
* @returns The calculated margin requirement as a BN (BigNumber).
|
|
123
135
|
*/
|
|
124
|
-
getMarginRequirement(marginCategory: MarginCategory, liquidationBuffer?: BN, strict?: boolean, includeOpenOrders?: boolean,
|
|
136
|
+
getMarginRequirement(marginCategory: MarginCategory, liquidationBuffer?: BN, strict?: boolean, includeOpenOrders?: boolean, perpMarketIndex?: number): BN;
|
|
125
137
|
/**
|
|
126
138
|
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
127
139
|
*/
|
|
128
|
-
getInitialMarginRequirement(
|
|
140
|
+
getInitialMarginRequirement(perpMarketIndex?: number): BN;
|
|
129
141
|
/**
|
|
130
142
|
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
131
143
|
*/
|
|
@@ -175,7 +187,7 @@ export declare class User {
|
|
|
175
187
|
* @returns : number (value from [0, 100])
|
|
176
188
|
*/
|
|
177
189
|
getHealth(perpMarketIndex?: number): number;
|
|
178
|
-
calculateWeightedPerpPositionLiability(perpPosition: PerpPosition, marginCategory?: MarginCategory, liquidationBuffer?: BN, includeOpenOrders?: boolean, strict?: boolean
|
|
190
|
+
calculateWeightedPerpPositionLiability(perpPosition: PerpPosition, marginCategory?: MarginCategory, liquidationBuffer?: BN, includeOpenOrders?: boolean, strict?: boolean): BN;
|
|
179
191
|
/**
|
|
180
192
|
* calculates position value of a single perp market in margin system
|
|
181
193
|
* @returns : Precision QUOTE_PRECISION
|
|
@@ -185,7 +197,7 @@ export declare class User {
|
|
|
185
197
|
* calculates sum of position value across all positions in margin system
|
|
186
198
|
* @returns : Precision QUOTE_PRECISION
|
|
187
199
|
*/
|
|
188
|
-
getTotalPerpPositionLiability(marginCategory?: MarginCategory, liquidationBuffer?: BN, includeOpenOrders?: boolean, strict?: boolean
|
|
200
|
+
getTotalPerpPositionLiability(marginCategory?: MarginCategory, liquidationBuffer?: BN, includeOpenOrders?: boolean, strict?: boolean): BN;
|
|
189
201
|
/**
|
|
190
202
|
* calculates position value based on oracle
|
|
191
203
|
* @returns : Precision QUOTE_PRECISION
|
|
@@ -235,10 +247,9 @@ export declare class User {
|
|
|
235
247
|
* for large sizes where imf factor activates, result is a lower bound
|
|
236
248
|
* @param marginCategory {Initial, Maintenance}
|
|
237
249
|
* @param isLp if calculating max leveraging for adding lp, need to add buffer
|
|
238
|
-
* @param enterHighLeverageMode can pass this as true to calculate max leverage if the user was to enter high leverage mode
|
|
239
250
|
* @returns : Precision TEN_THOUSAND
|
|
240
251
|
*/
|
|
241
|
-
getMaxLeverageForPerp(perpMarketIndex: number, _marginCategory?: MarginCategory, isLp?: boolean
|
|
252
|
+
getMaxLeverageForPerp(perpMarketIndex: number, _marginCategory?: MarginCategory, isLp?: boolean): BN;
|
|
242
253
|
/**
|
|
243
254
|
* calculates max allowable leverage exceeding hitting requirement category
|
|
244
255
|
* @param spotMarketIndex
|
|
@@ -272,7 +283,6 @@ export declare class User {
|
|
|
272
283
|
canIsolatedPositionMarginBeLiquidated(isolatedMarginCalculation: IsolatedMarginCalculation): boolean;
|
|
273
284
|
hasStatus(status: UserStatus): boolean;
|
|
274
285
|
isBankrupt(): boolean;
|
|
275
|
-
isHighLeverageMode(marginCategory: MarginCategory): boolean;
|
|
276
286
|
/**
|
|
277
287
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
278
288
|
* @returns
|
|
@@ -294,9 +304,9 @@ export declare class User {
|
|
|
294
304
|
* @param offsetCollateral // allows calculating the liquidation price after this offset collateral is added to the user's account (e.g. : what will the liquidation price be for this position AFTER I deposit $x worth of collateral)
|
|
295
305
|
* @returns Precision : PRICE_PRECISION
|
|
296
306
|
*/
|
|
297
|
-
liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN, marginCategory?: MarginCategory, includeOpenOrders?: boolean, offsetCollateral?: BN,
|
|
298
|
-
calculateEntriesEffectOnFreeCollateral(market: PerpMarketAccount, oraclePrice: BN, perpPosition: PerpPosition, positionBaseSizeChange: BN, estimatedEntryPrice: BN, includeOpenOrders: boolean,
|
|
299
|
-
calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN, oraclePrice: BN, marginCategory?: MarginCategory, includeOpenOrders?: boolean
|
|
307
|
+
liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN, marginCategory?: MarginCategory, includeOpenOrders?: boolean, offsetCollateral?: BN, marginType?: MarginType): BN;
|
|
308
|
+
calculateEntriesEffectOnFreeCollateral(market: PerpMarketAccount, oraclePrice: BN, perpPosition: PerpPosition, positionBaseSizeChange: BN, estimatedEntryPrice: BN, includeOpenOrders: boolean, marginCategory?: MarginCategory): BN;
|
|
309
|
+
calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN, oraclePrice: BN, marginCategory?: MarginCategory, includeOpenOrders?: boolean): BN | undefined;
|
|
300
310
|
calculateFreeCollateralDeltaForSpot(market: SpotMarketAccount, signedTokenAmount: BN, marginCategory?: MarginCategory): BN;
|
|
301
311
|
/**
|
|
302
312
|
* Calculates the estimated liquidation price for a position after closing a quote amount of the position.
|
|
@@ -316,7 +326,7 @@ export declare class User {
|
|
|
316
326
|
* @param isLp
|
|
317
327
|
* @returns { tradeSize: BN, oppositeSideTradeSize: BN} : Precision QUOTE_PRECISION
|
|
318
328
|
*/
|
|
319
|
-
getMaxTradeSizeUSDCForPerp(targetMarketIndex: number, tradeSide: PositionDirection, isLp?: boolean,
|
|
329
|
+
getMaxTradeSizeUSDCForPerp(targetMarketIndex: number, tradeSide: PositionDirection, isLp?: boolean, maxMarginRatio?: any, positionType?: 'isolated' | 'cross'): {
|
|
320
330
|
tradeSize: BN;
|
|
321
331
|
oppositeSideTradeSize: BN;
|
|
322
332
|
};
|
|
@@ -384,7 +394,7 @@ export declare class User {
|
|
|
384
394
|
* @param quoteAmount
|
|
385
395
|
* @returns feeForQuote : Precision QUOTE_PRECISION
|
|
386
396
|
*/
|
|
387
|
-
calculateFeeForQuoteAmount(quoteAmount: BN, marketIndex?: number
|
|
397
|
+
calculateFeeForQuoteAmount(quoteAmount: BN, marketIndex?: number): BN;
|
|
388
398
|
/**
|
|
389
399
|
* Calculates a user's max withdrawal amounts for a spot market. If reduceOnly is true,
|
|
390
400
|
* it will return the max withdrawal amount without opening a liability for the user
|
|
@@ -441,7 +451,6 @@ export declare class User {
|
|
|
441
451
|
getMarginCalculation(marginCategory?: MarginCategory, opts?: {
|
|
442
452
|
strict?: boolean;
|
|
443
453
|
includeOpenOrders?: boolean;
|
|
444
|
-
enteringHighLeverage?: boolean;
|
|
445
454
|
liquidationBufferMap?: Map<number | 'cross', BN>;
|
|
446
455
|
}): MarginCalculation;
|
|
447
456
|
isPerpPositionIsolated(perpPosition: PerpPosition): boolean;
|
package/lib/browser/user.js
CHANGED
|
@@ -185,7 +185,6 @@ class User {
|
|
|
185
185
|
settledPnl: numericConstants_1.ZERO,
|
|
186
186
|
lpShares: numericConstants_1.ZERO,
|
|
187
187
|
lastQuoteAssetAmountPerLp: numericConstants_1.ZERO,
|
|
188
|
-
perLpBase: 0,
|
|
189
188
|
maxMarginRatio: 0,
|
|
190
189
|
isolatedPositionScaledBalance: numericConstants_1.ZERO,
|
|
191
190
|
positionFlag: 0,
|
|
@@ -303,7 +302,7 @@ class User {
|
|
|
303
302
|
* calculates Buying Power = free collateral / initial margin ratio
|
|
304
303
|
* @returns : Precision QUOTE_PRECISION
|
|
305
304
|
*/
|
|
306
|
-
getPerpBuyingPower(marketIndex, collateralBuffer = numericConstants_1.ZERO,
|
|
305
|
+
getPerpBuyingPower(marketIndex, collateralBuffer = numericConstants_1.ZERO, maxMarginRatio = undefined, positionType = 'cross') {
|
|
307
306
|
const perpPosition = this.getPerpPositionOrEmpty(marketIndex);
|
|
308
307
|
const perpMarket = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
309
308
|
const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
|
|
@@ -315,27 +314,26 @@ class User {
|
|
|
315
314
|
if (positionType === 'isolated') {
|
|
316
315
|
const { totalAssetValue: quoteSpotMarketAssetValue, totalLiabilityValue: quoteSpotMarketLiabilityValue, } = this.getSpotMarketAssetAndLiabilityValue(perpMarket.quoteSpotMarketIndex, 'Initial', undefined, undefined, true);
|
|
317
316
|
const usdcAvailableForIsolatedMargin = quoteSpotMarketAssetValue.sub(quoteSpotMarketLiabilityValue);
|
|
318
|
-
const generalFreeCollateral = this.getFreeCollateral('Initial',
|
|
317
|
+
const generalFreeCollateral = this.getFreeCollateral('Initial', undefined);
|
|
319
318
|
freeCollateral = anchor_1.BN.min(usdcAvailableForIsolatedMargin, generalFreeCollateral).sub(collateralBuffer);
|
|
320
319
|
}
|
|
321
320
|
else {
|
|
322
321
|
// free collateral from the cross account only
|
|
323
|
-
freeCollateral = this.getFreeCollateral('Initial',
|
|
322
|
+
freeCollateral = this.getFreeCollateral('Initial', undefined).sub(collateralBuffer);
|
|
324
323
|
}
|
|
325
|
-
return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, worstCaseBaseAssetAmount,
|
|
324
|
+
return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, worstCaseBaseAssetAmount, maxMarginRatio || perpPosition.maxMarginRatio);
|
|
326
325
|
}
|
|
327
|
-
getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, baseAssetAmount,
|
|
326
|
+
getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, baseAssetAmount, perpMarketMaxMarginRatio = undefined) {
|
|
328
327
|
const maxMarginRatio = Math.max(perpMarketMaxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
329
|
-
const marginRatio = (0, market_1.calculateMarketMarginRatio)(this.driftClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial', maxMarginRatio
|
|
328
|
+
const marginRatio = (0, market_1.calculateMarketMarginRatio)(this.driftClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial', maxMarginRatio);
|
|
330
329
|
return freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new anchor_1.BN(marginRatio));
|
|
331
330
|
}
|
|
332
331
|
/**
|
|
333
332
|
* calculates Free Collateral = Total collateral - margin requirement
|
|
334
333
|
* @returns : Precision QUOTE_PRECISION
|
|
335
334
|
*/
|
|
336
|
-
getFreeCollateral(marginCategory = 'Initial',
|
|
335
|
+
getFreeCollateral(marginCategory = 'Initial', perpMarketIndex) {
|
|
337
336
|
const calc = this.getMarginCalculation(marginCategory, {
|
|
338
|
-
enteringHighLeverage: enterHighLeverageMode,
|
|
339
337
|
strict: marginCategory === 'Initial',
|
|
340
338
|
});
|
|
341
339
|
if (perpMarketIndex !== undefined) {
|
|
@@ -351,7 +349,7 @@ class User {
|
|
|
351
349
|
return calc.getCrossFreeCollateral();
|
|
352
350
|
}
|
|
353
351
|
}
|
|
354
|
-
getMarginRequirement(marginCategory, liquidationBuffer, strict, includeOpenOrders,
|
|
352
|
+
getMarginRequirement(marginCategory, liquidationBuffer, strict, includeOpenOrders, perpMarketIndex) {
|
|
355
353
|
const liquidationBufferMap = new Map();
|
|
356
354
|
if (liquidationBuffer && perpMarketIndex !== undefined) {
|
|
357
355
|
liquidationBufferMap.set(perpMarketIndex, liquidationBuffer);
|
|
@@ -362,7 +360,6 @@ class User {
|
|
|
362
360
|
const marginCalc = this.getMarginCalculation(marginCategory, {
|
|
363
361
|
strict,
|
|
364
362
|
includeOpenOrders,
|
|
365
|
-
enteringHighLeverage,
|
|
366
363
|
liquidationBufferMap,
|
|
367
364
|
});
|
|
368
365
|
// If perpMarketIndex is provided, compute only for that market index
|
|
@@ -385,8 +382,8 @@ class User {
|
|
|
385
382
|
/**
|
|
386
383
|
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
387
384
|
*/
|
|
388
|
-
getInitialMarginRequirement(
|
|
389
|
-
return this.getMarginRequirement('Initial', undefined, true, undefined,
|
|
385
|
+
getInitialMarginRequirement(perpMarketIndex) {
|
|
386
|
+
return this.getMarginRequirement('Initial', undefined, true, undefined, perpMarketIndex);
|
|
390
387
|
}
|
|
391
388
|
/**
|
|
392
389
|
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
@@ -394,7 +391,6 @@ class User {
|
|
|
394
391
|
getMaintenanceMarginRequirement(liquidationBuffer, perpMarketIndex) {
|
|
395
392
|
return this.getMarginRequirement('Maintenance', liquidationBuffer, false, // strict default
|
|
396
393
|
true, // includeOpenOrders default
|
|
397
|
-
false, // enteringHighLeverage default
|
|
398
394
|
perpMarketIndex);
|
|
399
395
|
}
|
|
400
396
|
getActivePerpPositionsForUserAccount(userAccount) {
|
|
@@ -721,7 +717,7 @@ class User {
|
|
|
721
717
|
}
|
|
722
718
|
return health;
|
|
723
719
|
}
|
|
724
|
-
calculateWeightedPerpPositionLiability(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict = false
|
|
720
|
+
calculateWeightedPerpPositionLiability(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict = false) {
|
|
725
721
|
const market = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
726
722
|
let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
|
|
727
723
|
if ((0, types_1.isVariant)(market.status, 'settlement')) {
|
|
@@ -736,14 +732,11 @@ class User {
|
|
|
736
732
|
}
|
|
737
733
|
else {
|
|
738
734
|
baseAssetAmount = perpPosition.baseAssetAmount;
|
|
739
|
-
liabilityValue = (0, margin_1.calculatePerpLiabilityValue)(baseAssetAmount, valuationPrice
|
|
735
|
+
liabilityValue = (0, margin_1.calculatePerpLiabilityValue)(baseAssetAmount, valuationPrice);
|
|
740
736
|
}
|
|
741
737
|
if (marginCategory) {
|
|
742
738
|
const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
743
|
-
let marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory,
|
|
744
|
-
? Math.max(market.marginRatioInitial, userCustomMargin)
|
|
745
|
-
: userCustomMargin, this.isHighLeverageMode(marginCategory) ||
|
|
746
|
-
enteringHighLeverage === true));
|
|
739
|
+
let marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory, userCustomMargin));
|
|
747
740
|
if (liquidationBuffer !== undefined) {
|
|
748
741
|
marginRatio = marginRatio.add(liquidationBuffer);
|
|
749
742
|
}
|
|
@@ -782,9 +775,9 @@ class User {
|
|
|
782
775
|
* calculates sum of position value across all positions in margin system
|
|
783
776
|
* @returns : Precision QUOTE_PRECISION
|
|
784
777
|
*/
|
|
785
|
-
getTotalPerpPositionLiability(marginCategory, liquidationBuffer, includeOpenOrders, strict = false
|
|
778
|
+
getTotalPerpPositionLiability(marginCategory, liquidationBuffer, includeOpenOrders, strict = false) {
|
|
786
779
|
return this.getActivePerpPositions().reduce((totalPerpValue, perpPosition) => {
|
|
787
|
-
const baseAssetValue = this.calculateWeightedPerpPositionLiability(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict
|
|
780
|
+
const baseAssetValue = this.calculateWeightedPerpPositionLiability(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict);
|
|
788
781
|
return totalPerpValue.add(baseAssetValue);
|
|
789
782
|
}, numericConstants_1.ZERO);
|
|
790
783
|
}
|
|
@@ -808,7 +801,7 @@ class User {
|
|
|
808
801
|
return (0, margin_1.calculateWorstCasePerpLiabilityValue)(userPosition, market, oraclePriceData.price).worstCaseLiabilityValue;
|
|
809
802
|
}
|
|
810
803
|
else {
|
|
811
|
-
return (0, margin_1.calculatePerpLiabilityValue)(userPosition.baseAssetAmount, oraclePriceData.price
|
|
804
|
+
return (0, margin_1.calculatePerpLiabilityValue)(userPosition.baseAssetAmount, oraclePriceData.price);
|
|
812
805
|
}
|
|
813
806
|
}
|
|
814
807
|
getPositionSide(currentPosition) {
|
|
@@ -973,10 +966,9 @@ class User {
|
|
|
973
966
|
* for large sizes where imf factor activates, result is a lower bound
|
|
974
967
|
* @param marginCategory {Initial, Maintenance}
|
|
975
968
|
* @param isLp if calculating max leveraging for adding lp, need to add buffer
|
|
976
|
-
* @param enterHighLeverageMode can pass this as true to calculate max leverage if the user was to enter high leverage mode
|
|
977
969
|
* @returns : Precision TEN_THOUSAND
|
|
978
970
|
*/
|
|
979
|
-
getMaxLeverageForPerp(perpMarketIndex, _marginCategory = 'Initial', isLp = false
|
|
971
|
+
getMaxLeverageForPerp(perpMarketIndex, _marginCategory = 'Initial', isLp = false) {
|
|
980
972
|
const market = this.driftClient.getPerpMarketAccount(perpMarketIndex);
|
|
981
973
|
const marketPrice = this.driftClient.getOracleDataForPerpMarket(perpMarketIndex).price;
|
|
982
974
|
const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } = this.getLeverageComponents();
|
|
@@ -990,7 +982,7 @@ class User {
|
|
|
990
982
|
? marketPrice.mul(market.amm.orderStepSize).div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
991
983
|
: numericConstants_1.ZERO;
|
|
992
984
|
// absolute max fesible size (upper bound)
|
|
993
|
-
const maxSizeQuote = anchor_1.BN.max(anchor_1.BN.min(this.getMaxTradeSizeUSDCForPerp(perpMarketIndex, types_2.PositionDirection.LONG, false
|
|
985
|
+
const maxSizeQuote = anchor_1.BN.max(anchor_1.BN.min(this.getMaxTradeSizeUSDCForPerp(perpMarketIndex, types_2.PositionDirection.LONG, false).tradeSize, this.getMaxTradeSizeUSDCForPerp(perpMarketIndex, types_2.PositionDirection.SHORT, false).tradeSize).sub(lpBuffer), numericConstants_1.ZERO);
|
|
994
986
|
return totalLiabilityValue
|
|
995
987
|
.add(maxSizeQuote)
|
|
996
988
|
.mul(numericConstants_1.TEN_THOUSAND)
|
|
@@ -1150,11 +1142,6 @@ class User {
|
|
|
1150
1142
|
isBankrupt() {
|
|
1151
1143
|
return (this.getUserAccount().status & types_1.UserStatus.BANKRUPT) > 0;
|
|
1152
1144
|
}
|
|
1153
|
-
isHighLeverageMode(marginCategory) {
|
|
1154
|
-
return ((0, types_1.isVariant)(this.getUserAccount().marginMode, 'highLeverage') ||
|
|
1155
|
-
(marginCategory === 'Maintenance' &&
|
|
1156
|
-
(0, types_1.isVariant)(this.getUserAccount().marginMode, 'highLeverageMaintenance')));
|
|
1157
|
-
}
|
|
1158
1145
|
/**
|
|
1159
1146
|
* Checks if any user position cumulative funding differs from respective market cumulative funding
|
|
1160
1147
|
* @returns
|
|
@@ -1233,7 +1220,7 @@ class User {
|
|
|
1233
1220
|
* @param offsetCollateral // allows calculating the liquidation price after this offset collateral is added to the user's account (e.g. : what will the liquidation price be for this position AFTER I deposit $x worth of collateral)
|
|
1234
1221
|
* @returns Precision : PRICE_PRECISION
|
|
1235
1222
|
*/
|
|
1236
|
-
liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO, marginCategory = 'Maintenance', includeOpenOrders = false, offsetCollateral = numericConstants_1.ZERO,
|
|
1223
|
+
liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO, marginCategory = 'Maintenance', includeOpenOrders = false, offsetCollateral = numericConstants_1.ZERO, marginType) {
|
|
1237
1224
|
const market = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
1238
1225
|
const oracle = this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
1239
1226
|
const oraclePrice = this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
|
|
@@ -1242,14 +1229,13 @@ class User {
|
|
|
1242
1229
|
const marginCalculation = this.getMarginCalculation(marginCategory, {
|
|
1243
1230
|
strict: false,
|
|
1244
1231
|
includeOpenOrders,
|
|
1245
|
-
enteringHighLeverage,
|
|
1246
1232
|
});
|
|
1247
1233
|
const isolatedMarginCalculation = marginCalculation.isolatedMarginCalculations.get(marketIndex);
|
|
1248
1234
|
if (!isolatedMarginCalculation)
|
|
1249
1235
|
return new anchor_1.BN(-1);
|
|
1250
1236
|
const { totalCollateral, marginRequirement } = isolatedMarginCalculation;
|
|
1251
1237
|
const freeCollateral = anchor_1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(marginRequirement)).add(offsetCollateral);
|
|
1252
|
-
const freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange, oraclePrice, marginCategory, includeOpenOrders
|
|
1238
|
+
const freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange, oraclePrice, marginCategory, includeOpenOrders);
|
|
1253
1239
|
if (!freeCollateralDelta || freeCollateralDelta.eq(numericConstants_1.ZERO)) {
|
|
1254
1240
|
return new anchor_1.BN(-1);
|
|
1255
1241
|
}
|
|
@@ -1263,12 +1249,12 @@ class User {
|
|
|
1263
1249
|
return liqPrice;
|
|
1264
1250
|
}
|
|
1265
1251
|
const totalCollateral = this.getTotalCollateral(marginCategory, false, includeOpenOrders);
|
|
1266
|
-
const marginRequirement = this.getMarginRequirement(marginCategory, undefined, false, includeOpenOrders
|
|
1252
|
+
const marginRequirement = this.getMarginRequirement(marginCategory, undefined, false, includeOpenOrders);
|
|
1267
1253
|
let freeCollateral = anchor_1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(marginRequirement)).add(offsetCollateral);
|
|
1268
1254
|
positionBaseSizeChange = (0, orders_1.standardizeBaseAssetAmount)(positionBaseSizeChange, market.amm.orderStepSize);
|
|
1269
|
-
const freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(market, oraclePrice, currentPerpPosition, positionBaseSizeChange, estimatedEntryPrice, includeOpenOrders
|
|
1255
|
+
const freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(market, oraclePrice, currentPerpPosition, positionBaseSizeChange, estimatedEntryPrice, includeOpenOrders);
|
|
1270
1256
|
freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
|
|
1271
|
-
let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange, oraclePrice, marginCategory, includeOpenOrders
|
|
1257
|
+
let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange, oraclePrice, marginCategory, includeOpenOrders);
|
|
1272
1258
|
if (!freeCollateralDelta) {
|
|
1273
1259
|
return new anchor_1.BN(-1);
|
|
1274
1260
|
}
|
|
@@ -1301,7 +1287,7 @@ class User {
|
|
|
1301
1287
|
}
|
|
1302
1288
|
return liqPrice;
|
|
1303
1289
|
}
|
|
1304
|
-
calculateEntriesEffectOnFreeCollateral(market, oraclePrice, perpPosition, positionBaseSizeChange, estimatedEntryPrice, includeOpenOrders,
|
|
1290
|
+
calculateEntriesEffectOnFreeCollateral(market, oraclePrice, perpPosition, positionBaseSizeChange, estimatedEntryPrice, includeOpenOrders, marginCategory = 'Maintenance') {
|
|
1305
1291
|
let freeCollateralChange = numericConstants_1.ZERO;
|
|
1306
1292
|
// update free collateral to account for change in pnl from new position
|
|
1307
1293
|
if (!estimatedEntryPrice.eq(numericConstants_1.ZERO) &&
|
|
@@ -1336,12 +1322,10 @@ class User {
|
|
|
1336
1322
|
}
|
|
1337
1323
|
else {
|
|
1338
1324
|
baseAssetAmount = perpPosition.baseAssetAmount;
|
|
1339
|
-
liabilityValue = (0, margin_1.calculatePerpLiabilityValue)(baseAssetAmount, oraclePrice
|
|
1325
|
+
liabilityValue = (0, margin_1.calculatePerpLiabilityValue)(baseAssetAmount, oraclePrice);
|
|
1340
1326
|
}
|
|
1341
1327
|
const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
1342
|
-
const marginRatio = (0, market_1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory,
|
|
1343
|
-
? Math.max(market.marginRatioInitial, userCustomMargin)
|
|
1344
|
-
: userCustomMargin, this.isHighLeverageMode(marginCategory) || enteringHighLeverage === true);
|
|
1328
|
+
const marginRatio = (0, market_1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory, userCustomMargin);
|
|
1345
1329
|
return liabilityValue.mul(new anchor_1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION);
|
|
1346
1330
|
};
|
|
1347
1331
|
const freeCollateralConsumptionBefore = calculateMarginRequirement(perpPosition);
|
|
@@ -1350,7 +1334,7 @@ class User {
|
|
|
1350
1334
|
const freeCollateralConsumptionAfter = calculateMarginRequirement(perpPositionAfter);
|
|
1351
1335
|
return freeCollateralChange.sub(freeCollateralConsumptionAfter.sub(freeCollateralConsumptionBefore));
|
|
1352
1336
|
}
|
|
1353
|
-
calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange, oraclePrice, marginCategory = 'Maintenance', includeOpenOrders = false
|
|
1337
|
+
calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange, oraclePrice, marginCategory = 'Maintenance', includeOpenOrders = false) {
|
|
1354
1338
|
const baseAssetAmount = includeOpenOrders
|
|
1355
1339
|
? (0, margin_2.calculateWorstCaseBaseAssetAmount)(perpPosition, market, oraclePrice)
|
|
1356
1340
|
: perpPosition.baseAssetAmount;
|
|
@@ -1358,9 +1342,7 @@ class User {
|
|
|
1358
1342
|
const orderBaseAssetAmount = baseAssetAmount.sub(perpPosition.baseAssetAmount);
|
|
1359
1343
|
const proposedBaseAssetAmount = baseAssetAmount.add(positionBaseSizeChange);
|
|
1360
1344
|
const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
1361
|
-
const marginRatio = (0, market_1.calculateMarketMarginRatio)(market, proposedBaseAssetAmount.abs(), marginCategory,
|
|
1362
|
-
? Math.max(market.marginRatioInitial, userCustomMargin)
|
|
1363
|
-
: userCustomMargin, this.isHighLeverageMode(marginCategory) || enteringHighLeverage === true);
|
|
1345
|
+
const marginRatio = (0, market_1.calculateMarketMarginRatio)(market, proposedBaseAssetAmount.abs(), marginCategory, userCustomMargin);
|
|
1364
1346
|
const marginRatioQuotePrecision = new anchor_1.BN(marginRatio)
|
|
1365
1347
|
.mul(numericConstants_1.QUOTE_PRECISION)
|
|
1366
1348
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
@@ -1368,33 +1350,21 @@ class User {
|
|
|
1368
1350
|
return undefined;
|
|
1369
1351
|
}
|
|
1370
1352
|
let freeCollateralDelta = numericConstants_1.ZERO;
|
|
1371
|
-
if (
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
freeCollateralDelta = marginRatioQuotePrecision.neg();
|
|
1376
|
-
}
|
|
1377
|
-
else if (orderBaseAssetAmount.lt(numericConstants_1.ZERO)) {
|
|
1378
|
-
freeCollateralDelta = marginRatioQuotePrecision;
|
|
1379
|
-
}
|
|
1353
|
+
if (proposedBaseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
1354
|
+
freeCollateralDelta = numericConstants_1.QUOTE_PRECISION.sub(marginRatioQuotePrecision)
|
|
1355
|
+
.mul(proposedBaseAssetAmount)
|
|
1356
|
+
.div(numericConstants_1.BASE_PRECISION);
|
|
1380
1357
|
}
|
|
1381
1358
|
else {
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
.div(numericConstants_1.BASE_PRECISION);
|
|
1392
|
-
}
|
|
1393
|
-
if (!orderBaseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
1394
|
-
freeCollateralDelta = freeCollateralDelta.sub(marginRatioQuotePrecision
|
|
1395
|
-
.mul(orderBaseAssetAmount.abs())
|
|
1396
|
-
.div(numericConstants_1.BASE_PRECISION));
|
|
1397
|
-
}
|
|
1359
|
+
freeCollateralDelta = numericConstants_1.QUOTE_PRECISION.neg()
|
|
1360
|
+
.sub(marginRatioQuotePrecision)
|
|
1361
|
+
.mul(proposedBaseAssetAmount.abs())
|
|
1362
|
+
.div(numericConstants_1.BASE_PRECISION);
|
|
1363
|
+
}
|
|
1364
|
+
if (!orderBaseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
1365
|
+
freeCollateralDelta = freeCollateralDelta.sub(marginRatioQuotePrecision
|
|
1366
|
+
.mul(orderBaseAssetAmount.abs())
|
|
1367
|
+
.div(numericConstants_1.BASE_PRECISION));
|
|
1398
1368
|
}
|
|
1399
1369
|
return freeCollateralDelta;
|
|
1400
1370
|
}
|
|
@@ -1435,12 +1405,11 @@ class User {
|
|
|
1435
1405
|
}
|
|
1436
1406
|
getMarginUSDCRequiredForTrade(targetMarketIndex, baseSize, estEntryPrice, perpMarketMaxMarginRatio) {
|
|
1437
1407
|
const maxMarginRatio = Math.max(perpMarketMaxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
1438
|
-
return (0, margin_2.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, maxMarginRatio,
|
|
1408
|
+
return (0, margin_2.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, maxMarginRatio, estEntryPrice);
|
|
1439
1409
|
}
|
|
1440
1410
|
getCollateralDepositRequiredForTrade(targetMarketIndex, baseSize, collateralIndex, perpMarketMaxMarginRatio) {
|
|
1441
1411
|
const maxMarginRatio = Math.max(perpMarketMaxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
1442
|
-
return (0, margin_2.calculateCollateralDepositRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, collateralIndex, maxMarginRatio
|
|
1443
|
-
);
|
|
1412
|
+
return (0, margin_2.calculateCollateralDepositRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, collateralIndex, maxMarginRatio);
|
|
1444
1413
|
}
|
|
1445
1414
|
/**
|
|
1446
1415
|
* Separates the max trade size into two parts:
|
|
@@ -1451,7 +1420,7 @@ class User {
|
|
|
1451
1420
|
* @param isLp
|
|
1452
1421
|
* @returns { tradeSize: BN, oppositeSideTradeSize: BN} : Precision QUOTE_PRECISION
|
|
1453
1422
|
*/
|
|
1454
|
-
getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide, isLp = false,
|
|
1423
|
+
getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide, isLp = false, maxMarginRatio = undefined, positionType = 'cross') {
|
|
1455
1424
|
let tradeSize = numericConstants_1.ZERO;
|
|
1456
1425
|
let oppositeSideTradeSize = numericConstants_1.ZERO;
|
|
1457
1426
|
const currentPosition = this.getPerpPositionOrEmpty(targetMarketIndex);
|
|
@@ -1472,8 +1441,8 @@ class User {
|
|
|
1472
1441
|
// add any position we have on the opposite side of the current trade, because we can "flip" the size of this position without taking any extra leverage.
|
|
1473
1442
|
const oppositeSizeLiabilityValue = targetingSameSide
|
|
1474
1443
|
? numericConstants_1.ZERO
|
|
1475
|
-
: (0, margin_1.calculatePerpLiabilityValue)(currentPosition.baseAssetAmount, oracleData.price
|
|
1476
|
-
const maxPositionSize = this.getPerpBuyingPower(targetMarketIndex, lpBuffer,
|
|
1444
|
+
: (0, margin_1.calculatePerpLiabilityValue)(currentPosition.baseAssetAmount, oracleData.price);
|
|
1445
|
+
const maxPositionSize = this.getPerpBuyingPower(targetMarketIndex, lpBuffer, maxMarginRatio, positionType);
|
|
1477
1446
|
if (maxPositionSize.gte(numericConstants_1.ZERO)) {
|
|
1478
1447
|
if (oppositeSizeLiabilityValue.eq(numericConstants_1.ZERO)) {
|
|
1479
1448
|
// case 1 : Regular trade where current total position less than max, and no opposite position to account for
|
|
@@ -1489,10 +1458,9 @@ class User {
|
|
|
1489
1458
|
else {
|
|
1490
1459
|
// current leverage is greater than max leverage - can only reduce position size
|
|
1491
1460
|
if (!targetingSameSide) {
|
|
1492
|
-
const
|
|
1493
|
-
const perpLiabilityValue = (0, margin_1.calculatePerpLiabilityValue)(currentPosition.baseAssetAmount, oracleData.price, (0, types_1.isVariant)(market.contractType, 'prediction'));
|
|
1461
|
+
const perpLiabilityValue = (0, margin_1.calculatePerpLiabilityValue)(currentPosition.baseAssetAmount, oracleData.price);
|
|
1494
1462
|
const totalCollateral = this.getTotalCollateral();
|
|
1495
|
-
const marginRequirement = this.getInitialMarginRequirement(
|
|
1463
|
+
const marginRequirement = this.getInitialMarginRequirement();
|
|
1496
1464
|
const marginRatio = Math.max(currentPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
1497
1465
|
const marginFreedByClosing = perpLiabilityValue
|
|
1498
1466
|
.mul(new anchor_1.BN(marginRatio))
|
|
@@ -1513,11 +1481,11 @@ class User {
|
|
|
1513
1481
|
tradeSize = maxPositionSize;
|
|
1514
1482
|
}
|
|
1515
1483
|
}
|
|
1516
|
-
const freeCollateral = this.getFreeCollateral('Initial'
|
|
1484
|
+
const freeCollateral = this.getFreeCollateral('Initial');
|
|
1517
1485
|
let baseTradeSize = targetSide === 'long'
|
|
1518
1486
|
? tradeSize.mul(numericConstants_1.BASE_PRECISION).div(oracleData.price)
|
|
1519
1487
|
: tradeSize.mul(numericConstants_1.BASE_PRECISION).div(oracleData.price).neg();
|
|
1520
|
-
let freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(marketAccount, oracleData.price, currentPosition, baseTradeSize, oracleData.price, false,
|
|
1488
|
+
let freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(marketAccount, oracleData.price, currentPosition, baseTradeSize, oracleData.price, false, 'Initial');
|
|
1521
1489
|
while (freeCollateralChangeFromNewPosition.isNeg() &&
|
|
1522
1490
|
freeCollateralChangeFromNewPosition.abs().gt(freeCollateral)) {
|
|
1523
1491
|
tradeSize = tradeSize.mul(new anchor_1.BN(99)).div(new anchor_1.BN(100));
|
|
@@ -1526,7 +1494,7 @@ class User {
|
|
|
1526
1494
|
? tradeSize.mul(numericConstants_1.BASE_PRECISION).div(oracleData.price)
|
|
1527
1495
|
: tradeSize.mul(numericConstants_1.BASE_PRECISION).div(oracleData.price).neg();
|
|
1528
1496
|
freeCollateralChangeFromNewPosition =
|
|
1529
|
-
this.calculateEntriesEffectOnFreeCollateral(marketAccount, oracleData.price, currentPosition, baseTradeSize, oracleData.price, false,
|
|
1497
|
+
this.calculateEntriesEffectOnFreeCollateral(marketAccount, oracleData.price, currentPosition, baseTradeSize, oracleData.price, false, 'Initial');
|
|
1530
1498
|
}
|
|
1531
1499
|
return { tradeSize, oppositeSideTradeSize };
|
|
1532
1500
|
}
|
|
@@ -1871,9 +1839,6 @@ class User {
|
|
|
1871
1839
|
const state = this.driftClient.getStateAccount();
|
|
1872
1840
|
const feeTierIndex = 0;
|
|
1873
1841
|
if ((0, types_1.isVariant)(marketType, 'perp')) {
|
|
1874
|
-
if (this.isHighLeverageMode('Initial')) {
|
|
1875
|
-
return state.perpFeeStructure.feeTiers[0];
|
|
1876
|
-
}
|
|
1877
1842
|
const userStatsAccount = this.driftClient
|
|
1878
1843
|
.getUserStats()
|
|
1879
1844
|
.getAccount();
|
|
@@ -1924,9 +1889,9 @@ class User {
|
|
|
1924
1889
|
* @param quoteAmount
|
|
1925
1890
|
* @returns feeForQuote : Precision QUOTE_PRECISION
|
|
1926
1891
|
*/
|
|
1927
|
-
calculateFeeForQuoteAmount(quoteAmount, marketIndex
|
|
1892
|
+
calculateFeeForQuoteAmount(quoteAmount, marketIndex) {
|
|
1928
1893
|
if (marketIndex !== undefined) {
|
|
1929
|
-
const takerFeeMultiplier = this.driftClient.getMarketFees(types_2.MarketType.PERP, marketIndex, this
|
|
1894
|
+
const takerFeeMultiplier = this.driftClient.getMarketFees(types_2.MarketType.PERP, marketIndex, this).takerFee;
|
|
1930
1895
|
const feeAmountNum = bigNum_1.BigNum.from(quoteAmount, numericConstants_1.QUOTE_PRECISION_EXP).toNum() *
|
|
1931
1896
|
takerFeeMultiplier;
|
|
1932
1897
|
return bigNum_1.BigNum.fromPrint(feeAmountNum.toString(), numericConstants_1.QUOTE_PRECISION_EXP).val;
|
|
@@ -2158,10 +2123,10 @@ class User {
|
|
|
2158
2123
|
}
|
|
2159
2124
|
else {
|
|
2160
2125
|
worstCaseBaseAmount = perpPosition.baseAssetAmount;
|
|
2161
|
-
worstCaseLiabilityValue = (0, margin_1.calculatePerpLiabilityValue)(perpPosition.baseAssetAmount, oraclePrice
|
|
2126
|
+
worstCaseLiabilityValue = (0, margin_1.calculatePerpLiabilityValue)(perpPosition.baseAssetAmount, oraclePrice);
|
|
2162
2127
|
}
|
|
2163
2128
|
const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
|
|
2164
|
-
const marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, userCustomMargin
|
|
2129
|
+
const marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, userCustomMargin));
|
|
2165
2130
|
const _quoteOraclePriceData = quoteOraclePriceData ||
|
|
2166
2131
|
this.driftClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
2167
2132
|
let marginRequirement = worstCaseLiabilityValue
|
|
@@ -2317,23 +2282,22 @@ class User {
|
|
|
2317
2282
|
* Consumers can use this to avoid duplicating work across separate calls.
|
|
2318
2283
|
*/
|
|
2319
2284
|
getMarginCalculation(marginCategory = 'Initial', opts) {
|
|
2320
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j
|
|
2285
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
2321
2286
|
const strict = (_a = opts === null || opts === void 0 ? void 0 : opts.strict) !== null && _a !== void 0 ? _a : false;
|
|
2322
|
-
const
|
|
2323
|
-
const
|
|
2324
|
-
const includeOpenOrders = (_d = opts === null || opts === void 0 ? void 0 : opts.includeOpenOrders) !== null && _d !== void 0 ? _d : true;
|
|
2287
|
+
const liquidationBufferMap = (_b = opts === null || opts === void 0 ? void 0 : opts.liquidationBufferMap) !== null && _b !== void 0 ? _b : new Map();
|
|
2288
|
+
const includeOpenOrders = (_c = opts === null || opts === void 0 ? void 0 : opts.includeOpenOrders) !== null && _c !== void 0 ? _c : true;
|
|
2325
2289
|
// Equivalent to on-chain user_custom_margin_ratio
|
|
2326
2290
|
const userCustomMarginRatio = marginCategory === 'Initial' ? this.getUserAccount().maxMarginRatio : 0;
|
|
2327
2291
|
// Initialize calc via JS mirror of Rust/on-chain MarginCalculation
|
|
2328
2292
|
const isolatedMarginBuffers = new Map();
|
|
2329
|
-
for (const [marketIndex, isolatedMarginBuffer,] of (
|
|
2293
|
+
for (const [marketIndex, isolatedMarginBuffer,] of (_d = opts === null || opts === void 0 ? void 0 : opts.liquidationBufferMap) !== null && _d !== void 0 ? _d : new Map()) {
|
|
2330
2294
|
if (marketIndex !== 'cross') {
|
|
2331
2295
|
isolatedMarginBuffers.set(marketIndex, isolatedMarginBuffer);
|
|
2332
2296
|
}
|
|
2333
2297
|
}
|
|
2334
2298
|
const ctx = marginCalculation_1.MarginContext.standard(marginCategory)
|
|
2335
2299
|
.strictMode(strict)
|
|
2336
|
-
.setCrossMarginBuffer((
|
|
2300
|
+
.setCrossMarginBuffer((_f = (_e = opts === null || opts === void 0 ? void 0 : opts.liquidationBufferMap) === null || _e === void 0 ? void 0 : _e.get('cross')) !== null && _f !== void 0 ? _f : numericConstants_1.ZERO)
|
|
2337
2301
|
.setIsolatedMarginBuffers(isolatedMarginBuffers);
|
|
2338
2302
|
const calc = new marginCalculation_1.MarginCalculation(ctx);
|
|
2339
2303
|
// SPOT POSITIONS
|
|
@@ -2356,7 +2320,7 @@ class User {
|
|
|
2356
2320
|
}
|
|
2357
2321
|
else {
|
|
2358
2322
|
// borrow on quote contributes to margin requirement
|
|
2359
|
-
const tokenValueAbs = this.getSpotLiabilityValue(tokenAmount, strictOracle, spotMarket, marginCategory, (
|
|
2323
|
+
const tokenValueAbs = this.getSpotLiabilityValue(tokenAmount, strictOracle, spotMarket, marginCategory, (_g = liquidationBufferMap.get('cross')) !== null && _g !== void 0 ? _g : new anchor_1.BN(0)).abs();
|
|
2360
2324
|
calc.addCrossMarginRequirement(tokenValueAbs, tokenValueAbs);
|
|
2361
2325
|
}
|
|
2362
2326
|
continue;
|
|
@@ -2399,7 +2363,7 @@ class User {
|
|
|
2399
2363
|
const { worstCaseBaseAssetAmount, worstCaseLiabilityValue } = (0, margin_1.calculateWorstCasePerpLiabilityValue)(marketPosition, market, nonMmmOraclePriceData.price, includeOpenOrders);
|
|
2400
2364
|
// margin ratio for this perp
|
|
2401
2365
|
const customMarginRatio = Math.max(userCustomMarginRatio, marketPosition.maxMarginRatio);
|
|
2402
|
-
let marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), marginCategory, customMarginRatio
|
|
2366
|
+
let marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), marginCategory, customMarginRatio));
|
|
2403
2367
|
if ((0, types_1.isVariant)(market.status, 'settlement')) {
|
|
2404
2368
|
marginRatio = numericConstants_1.ZERO;
|
|
2405
2369
|
}
|
|
@@ -2443,13 +2407,13 @@ class User {
|
|
|
2443
2407
|
if (isIsolated) {
|
|
2444
2408
|
// derive isolated quote deposit value, mirroring on-chain logic
|
|
2445
2409
|
let depositValue = numericConstants_1.ZERO;
|
|
2446
|
-
if ((
|
|
2410
|
+
if ((_h = marketPosition.isolatedPositionScaledBalance) === null || _h === void 0 ? void 0 : _h.gt(numericConstants_1.ZERO)) {
|
|
2447
2411
|
const quoteSpotMarket = this.driftClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
|
|
2448
2412
|
const quoteOraclePriceData = this.getOracleDataForSpotMarket(market.quoteSpotMarketIndex);
|
|
2449
2413
|
const strictQuote = new strictOraclePrice_1.StrictOraclePrice(quoteOraclePriceData.price, strict
|
|
2450
2414
|
? quoteSpotMarket.historicalOracleData.lastOraclePriceTwap5Min
|
|
2451
2415
|
: undefined);
|
|
2452
|
-
const quoteTokenAmount = (0, spotBalance_2.getTokenAmount)((
|
|
2416
|
+
const quoteTokenAmount = (0, spotBalance_2.getTokenAmount)((_j = marketPosition.isolatedPositionScaledBalance) !== null && _j !== void 0 ? _j : numericConstants_1.ZERO, quoteSpotMarket, types_2.SpotBalanceType.DEPOSIT);
|
|
2453
2417
|
depositValue = (0, spotBalance_1.getStrictTokenValue)(quoteTokenAmount, quoteSpotMarket.decimals, strictQuote);
|
|
2454
2418
|
}
|
|
2455
2419
|
calc.addIsolatedMarginCalculation(market.marketIndex, depositValue, positionUnrealizedPnl, worstCaseLiabilityValue, perpMarginRequirement);
|
|
@@ -22,7 +22,7 @@ class WebsocketSubscription {
|
|
|
22
22
|
if (this.additionalFilters) {
|
|
23
23
|
filters.push(...this.additionalFilters);
|
|
24
24
|
}
|
|
25
|
-
this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('UserMap', '
|
|
25
|
+
this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('UserMap', 'user', this.userMap.driftClient.program, this.decodeFn, {
|
|
26
26
|
filters,
|
|
27
27
|
commitment: this.commitment,
|
|
28
28
|
}, this.resubOpts);
|
|
@@ -22,7 +22,7 @@ class grpcSubscription {
|
|
|
22
22
|
if (this.additionalFilters) {
|
|
23
23
|
filters.push(...this.additionalFilters);
|
|
24
24
|
}
|
|
25
|
-
this.subscriber = await grpcProgramAccountSubscriber_1.grpcProgramAccountSubscriber.create(this.grpcConfigs, 'UserMap', '
|
|
25
|
+
this.subscriber = await grpcProgramAccountSubscriber_1.grpcProgramAccountSubscriber.create(this.grpcConfigs, 'UserMap', 'user', this.userMap.driftClient.program, this.decodeFn, {
|
|
26
26
|
filters,
|
|
27
27
|
}, this.resubOpts);
|
|
28
28
|
}
|