@velocity-exchange/sdk 0.0.5 → 0.1.0
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 -1
- package/CHANGELOG.md +20 -0
- package/README.md +30 -30
- package/bun.lock +1 -1
- package/lib/browser/accounts/grpcProgramAccountSubscriber.js +1 -1
- package/lib/browser/accounts/grpcVelocityClientAccountSubscriber.d.ts +0 -4
- package/lib/browser/accounts/grpcVelocityClientAccountSubscriber.js +1 -3
- package/lib/browser/accounts/grpcVelocityClientAccountSubscriberV2.d.ts +0 -4
- package/lib/browser/accounts/grpcVelocityClientAccountSubscriberV2.js +5 -7
- package/lib/browser/accounts/laserProgramAccountSubscriber.js +1 -1
- package/lib/browser/accounts/pollingVelocityClientAccountSubscriber.d.ts +0 -4
- package/lib/browser/accounts/pollingVelocityClientAccountSubscriber.js +5 -7
- package/lib/browser/accounts/types.d.ts +2 -8
- package/lib/browser/accounts/utils.js +2 -2
- package/lib/browser/accounts/webSocketVelocityClientAccountSubscriber.d.ts +0 -4
- package/lib/browser/accounts/webSocketVelocityClientAccountSubscriber.js +5 -7
- package/lib/browser/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts +0 -4
- package/lib/browser/accounts/webSocketVelocityClientAccountSubscriberV2.js +5 -7
- package/lib/browser/addresses/pda.d.ts +1 -7
- package/lib/browser/addresses/pda.js +4 -10
- package/lib/browser/adminClient.d.ts +1 -1
- package/lib/browser/adminClient.js +21 -21
- package/lib/browser/auctionSubscriber/auctionSubscriber.d.ts +1 -3
- package/lib/browser/auctionSubscriber/auctionSubscriber.js +2 -6
- package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.d.ts +1 -3
- package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.js +2 -6
- package/lib/browser/auctionSubscriber/types.d.ts +3 -2
- package/lib/browser/config.d.ts +10 -19
- package/lib/browser/config.js +6 -33
- package/lib/browser/constants/insuranceFund.d.ts +1 -1
- package/lib/browser/constants/insuranceFund.js +1 -1
- package/lib/browser/constituentMap/constituentMap.d.ts +0 -2
- package/lib/browser/constituentMap/constituentMap.js +5 -5
- package/lib/browser/core/VelocityCore.d.ts +9 -15
- package/lib/browser/core/VelocityCore.js +5 -7
- package/lib/browser/core/instructions/withdraw.d.ts +1 -1
- package/lib/browser/core/instructions/withdraw.js +1 -1
- package/lib/browser/core/remainingAccounts.js +3 -3
- package/lib/browser/dlob/DLOB.d.ts +1 -1
- package/lib/browser/dlob/DLOB.js +6 -6
- package/lib/browser/dlob/DLOBSubscriber.d.ts +0 -2
- package/lib/browser/dlob/DLOBSubscriber.js +2 -7
- package/lib/browser/dlob/orderBookLevels.js +7 -7
- package/lib/browser/dlob/types.d.ts +0 -2
- package/lib/browser/events/eventSubscriber.d.ts +1 -1
- package/lib/browser/events/eventsServerLogProvider.js +2 -2
- package/lib/browser/events/parse.js +14 -14
- package/lib/browser/events/types.d.ts +3 -5
- package/lib/browser/events/types.js +1 -1
- package/lib/browser/idl/{drift.d.ts → velocity.d.ts} +1042 -767
- package/lib/browser/idl/{drift.json → velocity.json} +1033 -758
- package/lib/browser/index.d.ts +1 -1
- package/lib/browser/index.js +1 -2
- package/lib/browser/math/amm.d.ts +10 -10
- package/lib/browser/math/amm.js +29 -28
- package/lib/browser/math/auction.js +16 -12
- package/lib/browser/math/builder.d.ts +18 -1
- package/lib/browser/math/builder.js +32 -1
- package/lib/browser/math/funding.js +26 -26
- package/lib/browser/math/margin.d.ts +4 -4
- package/lib/browser/math/margin.js +15 -15
- package/lib/browser/math/market.js +13 -13
- package/lib/browser/math/oracles.d.ts +4 -4
- package/lib/browser/math/oracles.js +23 -23
- package/lib/browser/math/orders.d.ts +2 -2
- package/lib/browser/math/orders.js +9 -9
- package/lib/browser/math/position.js +4 -4
- package/lib/browser/math/superStake.d.ts +8 -10
- package/lib/browser/math/superStake.js +18 -20
- package/lib/browser/math/trade.js +5 -5
- package/lib/browser/math/utils.d.ts +1 -1
- package/lib/browser/math/utils.js +1 -1
- package/lib/browser/oracles/pythLazerClient.js +2 -2
- package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +0 -2
- package/lib/browser/orderSubscriber/OrderSubscriber.js +8 -12
- package/lib/browser/orderSubscriber/WebsocketSubscription.js +1 -1
- package/lib/browser/orderSubscriber/grpcSubscription.js +2 -2
- package/lib/browser/orderSubscriber/types.d.ts +0 -2
- package/lib/browser/priorityFee/priorityFeeSubscriber.d.ts +1 -5
- package/lib/browser/priorityFee/priorityFeeSubscriber.js +7 -15
- package/lib/browser/priorityFee/priorityFeeSubscriberMap.d.ts +0 -4
- package/lib/browser/priorityFee/priorityFeeSubscriberMap.js +3 -11
- package/lib/browser/priorityFee/types.d.ts +3 -8
- package/lib/browser/priorityFee/velocityPriorityFeeMethod.d.ts +0 -8
- package/lib/browser/priorityFee/velocityPriorityFeeMethod.js +1 -3
- package/lib/browser/pyth/pythLazerSubscriber.d.ts +1 -1
- package/lib/browser/pyth/pythLazerSubscriber.js +1 -1
- package/lib/browser/swap/UnifiedSwapClient.js +1 -1
- package/lib/browser/swift/signedMsgUserAccountSubscriber.d.ts +4 -5
- package/lib/browser/swift/signedMsgUserAccountSubscriber.js +2 -6
- package/lib/browser/swift/swiftOrderSubscriber.d.ts +6 -5
- package/lib/browser/swift/swiftOrderSubscriber.js +5 -11
- package/lib/browser/types.d.ts +79 -91
- package/lib/browser/types.js +4 -0
- package/lib/browser/user.d.ts +1 -3
- package/lib/browser/user.js +19 -23
- package/lib/browser/userConfig.d.ts +3 -2
- package/lib/browser/userMap/WebsocketSubscription.js +1 -1
- package/lib/browser/userMap/grpcSubscription.js +1 -1
- package/lib/browser/userMap/referrerMap.d.ts +0 -2
- package/lib/browser/userMap/referrerMap.js +0 -4
- package/lib/browser/userMap/revenueShareEscrowMap.d.ts +0 -2
- package/lib/browser/userMap/revenueShareEscrowMap.js +0 -4
- package/lib/browser/userMap/userMap.d.ts +2 -4
- package/lib/browser/userMap/userMap.js +10 -14
- package/lib/browser/userMap/userMapConfig.d.ts +3 -2
- package/lib/browser/userMap/userStatsMap.d.ts +0 -2
- package/lib/browser/userMap/userStatsMap.js +0 -4
- package/lib/browser/userStats.d.ts +0 -2
- package/lib/browser/userStats.js +10 -14
- package/lib/browser/userStatsConfig.d.ts +3 -2
- package/lib/browser/util/ed25519Utils.d.ts +1 -1
- package/lib/browser/util/ed25519Utils.js +1 -1
- package/lib/browser/velocityClient.d.ts +53 -38
- package/lib/browser/velocityClient.js +258 -196
- package/lib/browser/velocityClientConfig.d.ts +0 -8
- package/lib/node/accounts/grpcProgramAccountSubscriber.js +1 -1
- package/lib/node/accounts/grpcVelocityClientAccountSubscriber.d.ts +0 -4
- package/lib/node/accounts/grpcVelocityClientAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/grpcVelocityClientAccountSubscriber.js +1 -3
- package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts +0 -4
- package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.js +5 -7
- package/lib/node/accounts/laserProgramAccountSubscriber.js +1 -1
- package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts +0 -4
- package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/pollingVelocityClientAccountSubscriber.js +5 -7
- package/lib/node/accounts/types.d.ts +2 -8
- package/lib/node/accounts/types.d.ts.map +1 -1
- package/lib/node/accounts/utils.js +2 -2
- package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts +0 -4
- package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts.map +1 -1
- package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.js +5 -7
- package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts +0 -4
- package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.js +5 -7
- package/lib/node/addresses/pda.d.ts +1 -7
- package/lib/node/addresses/pda.d.ts.map +1 -1
- package/lib/node/addresses/pda.js +4 -10
- package/lib/node/adminClient.d.ts +1 -1
- package/lib/node/adminClient.js +21 -21
- package/lib/node/auctionSubscriber/auctionSubscriber.d.ts +1 -3
- package/lib/node/auctionSubscriber/auctionSubscriber.d.ts.map +1 -1
- package/lib/node/auctionSubscriber/auctionSubscriber.js +2 -6
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts +1 -3
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts.map +1 -1
- package/lib/node/auctionSubscriber/auctionSubscriberGrpc.js +2 -6
- package/lib/node/auctionSubscriber/types.d.ts +3 -2
- package/lib/node/auctionSubscriber/types.d.ts.map +1 -1
- package/lib/node/config.d.ts +10 -19
- package/lib/node/config.d.ts.map +1 -1
- package/lib/node/config.js +6 -33
- package/lib/node/constants/insuranceFund.d.ts +1 -1
- package/lib/node/constants/insuranceFund.js +1 -1
- package/lib/node/constituentMap/constituentMap.d.ts +0 -2
- package/lib/node/constituentMap/constituentMap.d.ts.map +1 -1
- package/lib/node/constituentMap/constituentMap.js +5 -5
- package/lib/node/core/VelocityCore.d.ts +9 -15
- package/lib/node/core/VelocityCore.d.ts.map +1 -1
- package/lib/node/core/VelocityCore.js +5 -7
- package/lib/node/core/instructions/withdraw.d.ts +1 -1
- package/lib/node/core/instructions/withdraw.d.ts.map +1 -1
- package/lib/node/core/instructions/withdraw.js +1 -1
- package/lib/node/core/remainingAccounts.js +3 -3
- package/lib/node/dlob/DLOB.d.ts +1 -1
- package/lib/node/dlob/DLOB.d.ts.map +1 -1
- package/lib/node/dlob/DLOB.js +6 -6
- package/lib/node/dlob/DLOBSubscriber.d.ts +0 -2
- package/lib/node/dlob/DLOBSubscriber.d.ts.map +1 -1
- package/lib/node/dlob/DLOBSubscriber.js +2 -7
- package/lib/node/dlob/orderBookLevels.d.ts.map +1 -1
- package/lib/node/dlob/orderBookLevels.js +7 -7
- package/lib/node/dlob/types.d.ts +0 -2
- package/lib/node/dlob/types.d.ts.map +1 -1
- package/lib/node/events/eventSubscriber.d.ts +1 -1
- package/lib/node/events/eventsServerLogProvider.js +2 -2
- package/lib/node/events/parse.d.ts.map +1 -1
- package/lib/node/events/parse.js +14 -14
- package/lib/node/events/types.d.ts +3 -5
- package/lib/node/events/types.d.ts.map +1 -1
- package/lib/node/events/types.js +1 -1
- package/lib/node/idl/{drift.d.ts → velocity.d.ts} +1043 -768
- package/lib/node/idl/velocity.d.ts.map +1 -0
- package/lib/node/idl/{drift.json → velocity.json} +1033 -758
- package/lib/node/index.d.ts +1 -1
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +1 -2
- package/lib/node/math/amm.d.ts +10 -10
- package/lib/node/math/amm.d.ts.map +1 -1
- package/lib/node/math/amm.js +29 -28
- package/lib/node/math/auction.d.ts.map +1 -1
- package/lib/node/math/auction.js +16 -12
- package/lib/node/math/builder.d.ts +18 -1
- package/lib/node/math/builder.d.ts.map +1 -1
- package/lib/node/math/builder.js +32 -1
- package/lib/node/math/funding.d.ts.map +1 -1
- package/lib/node/math/funding.js +26 -26
- package/lib/node/math/margin.d.ts +4 -4
- package/lib/node/math/margin.d.ts.map +1 -1
- package/lib/node/math/margin.js +15 -15
- package/lib/node/math/market.d.ts.map +1 -1
- package/lib/node/math/market.js +13 -13
- package/lib/node/math/oracles.d.ts +4 -4
- package/lib/node/math/oracles.d.ts.map +1 -1
- package/lib/node/math/oracles.js +23 -23
- package/lib/node/math/orders.d.ts +2 -2
- package/lib/node/math/orders.d.ts.map +1 -1
- package/lib/node/math/orders.js +9 -9
- package/lib/node/math/position.d.ts.map +1 -1
- package/lib/node/math/position.js +4 -4
- package/lib/node/math/superStake.d.ts +8 -10
- package/lib/node/math/superStake.d.ts.map +1 -1
- package/lib/node/math/superStake.js +18 -20
- package/lib/node/math/trade.d.ts.map +1 -1
- package/lib/node/math/trade.js +5 -5
- package/lib/node/math/utils.d.ts +1 -1
- package/lib/node/math/utils.js +1 -1
- package/lib/node/oracles/pythLazerClient.js +2 -2
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts +0 -2
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
- package/lib/node/orderSubscriber/OrderSubscriber.js +8 -12
- package/lib/node/orderSubscriber/WebsocketSubscription.js +1 -1
- package/lib/node/orderSubscriber/grpcSubscription.js +2 -2
- package/lib/node/orderSubscriber/types.d.ts +0 -2
- package/lib/node/orderSubscriber/types.d.ts.map +1 -1
- package/lib/node/priorityFee/priorityFeeSubscriber.d.ts +1 -5
- package/lib/node/priorityFee/priorityFeeSubscriber.d.ts.map +1 -1
- package/lib/node/priorityFee/priorityFeeSubscriber.js +7 -15
- package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts +0 -4
- package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts.map +1 -1
- package/lib/node/priorityFee/priorityFeeSubscriberMap.js +3 -11
- package/lib/node/priorityFee/types.d.ts +3 -8
- package/lib/node/priorityFee/types.d.ts.map +1 -1
- package/lib/node/priorityFee/velocityPriorityFeeMethod.d.ts +0 -8
- package/lib/node/priorityFee/velocityPriorityFeeMethod.d.ts.map +1 -1
- package/lib/node/priorityFee/velocityPriorityFeeMethod.js +1 -3
- package/lib/node/pyth/pythLazerSubscriber.d.ts +1 -1
- package/lib/node/pyth/pythLazerSubscriber.js +1 -1
- package/lib/node/swap/UnifiedSwapClient.js +1 -1
- package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts +4 -5
- package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts.map +1 -1
- package/lib/node/swift/signedMsgUserAccountSubscriber.js +2 -6
- package/lib/node/swift/swiftOrderSubscriber.d.ts +6 -5
- package/lib/node/swift/swiftOrderSubscriber.d.ts.map +1 -1
- package/lib/node/swift/swiftOrderSubscriber.js +5 -11
- package/lib/node/types.d.ts +79 -91
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/types.js +4 -0
- package/lib/node/user.d.ts +1 -3
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +19 -23
- package/lib/node/userConfig.d.ts +3 -2
- package/lib/node/userConfig.d.ts.map +1 -1
- package/lib/node/userMap/WebsocketSubscription.js +1 -1
- package/lib/node/userMap/grpcSubscription.js +1 -1
- package/lib/node/userMap/referrerMap.d.ts +0 -2
- package/lib/node/userMap/referrerMap.d.ts.map +1 -1
- package/lib/node/userMap/referrerMap.js +0 -4
- package/lib/node/userMap/revenueShareEscrowMap.d.ts +0 -2
- package/lib/node/userMap/revenueShareEscrowMap.d.ts.map +1 -1
- package/lib/node/userMap/revenueShareEscrowMap.js +0 -4
- package/lib/node/userMap/userMap.d.ts +2 -4
- package/lib/node/userMap/userMap.d.ts.map +1 -1
- package/lib/node/userMap/userMap.js +10 -14
- package/lib/node/userMap/userMapConfig.d.ts +3 -2
- package/lib/node/userMap/userMapConfig.d.ts.map +1 -1
- package/lib/node/userMap/userStatsMap.d.ts +0 -2
- package/lib/node/userMap/userStatsMap.d.ts.map +1 -1
- package/lib/node/userMap/userStatsMap.js +0 -4
- package/lib/node/userStats.d.ts +0 -2
- package/lib/node/userStats.d.ts.map +1 -1
- package/lib/node/userStats.js +10 -14
- package/lib/node/userStatsConfig.d.ts +3 -2
- package/lib/node/userStatsConfig.d.ts.map +1 -1
- package/lib/node/util/ed25519Utils.d.ts +1 -1
- package/lib/node/util/ed25519Utils.js +1 -1
- package/lib/node/velocityClient.d.ts +53 -38
- package/lib/node/velocityClient.d.ts.map +1 -1
- package/lib/node/velocityClient.js +258 -196
- package/lib/node/velocityClientConfig.d.ts +0 -8
- package/lib/node/velocityClientConfig.d.ts.map +1 -1
- package/package.json +6 -7
- package/scripts/deposit-isolated-positions.ts +2 -2
- package/scripts/find-flagged-users.ts +1 -1
- package/scripts/grpc-client-test-comparison.ts +4 -4
- package/scripts/grpc-multiuser-client-test-comparison.ts +2 -2
- package/scripts/single-grpc-client-test.ts +3 -3
- package/scripts/updateVersion.js +4 -4
- package/scripts/withdraw-isolated-positions.ts +2 -2
- package/src/accounts/grpcProgramAccountSubscriber.ts +1 -1
- package/src/accounts/grpcVelocityClientAccountSubscriber.ts +0 -7
- package/src/accounts/grpcVelocityClientAccountSubscriberV2.ts +4 -11
- package/src/accounts/laserProgramAccountSubscriber.ts +1 -1
- package/src/accounts/pollingVelocityClientAccountSubscriber.ts +4 -11
- package/src/accounts/types.ts +2 -9
- package/src/accounts/utils.ts +2 -2
- package/src/accounts/webSocketVelocityClientAccountSubscriber.ts +4 -11
- package/src/accounts/webSocketVelocityClientAccountSubscriberV2.ts +4 -11
- package/src/addresses/pda.ts +3 -13
- package/src/adminClient.ts +21 -21
- package/src/auctionSubscriber/auctionSubscriber.ts +1 -6
- package/src/auctionSubscriber/auctionSubscriberGrpc.ts +1 -6
- package/src/auctionSubscriber/types.ts +3 -3
- package/src/config.ts +8 -67
- package/src/constants/insuranceFund.ts +1 -1
- package/src/constituentMap/constituentMap.ts +2 -6
- package/src/core/VelocityCore.ts +11 -20
- package/src/core/instructions/withdraw.ts +2 -2
- package/src/core/remainingAccounts.ts +3 -3
- package/src/dlob/DLOB.ts +6 -6
- package/src/dlob/DLOBSubscriber.ts +2 -8
- package/src/dlob/orderBookLevels.ts +12 -6
- package/src/dlob/types.ts +0 -2
- package/src/events/eventSubscriber.ts +1 -1
- package/src/events/eventsServerLogProvider.ts +2 -2
- package/src/events/parse.ts +14 -14
- package/src/events/types.ts +4 -7
- package/src/idl/{drift.json → velocity.json} +1033 -758
- package/src/idl/{drift.ts → velocity.ts} +1035 -760
- package/src/index.ts +1 -4
- package/src/margin/README.md +2 -2
- package/src/math/amm.ts +43 -21
- package/src/math/auction.ts +16 -12
- package/src/math/builder.ts +45 -1
- package/src/math/funding.ts +46 -43
- package/src/math/margin.ts +15 -15
- package/src/math/market.ts +21 -13
- package/src/math/oracles.ts +29 -26
- package/src/math/orders.ts +18 -5
- package/src/math/position.ts +9 -3
- package/src/math/superStake.ts +21 -24
- package/src/math/trade.ts +9 -1
- package/src/math/utils.ts +1 -1
- package/src/oracles/pythLazerClient.ts +3 -3
- package/src/orderSubscriber/OrderSubscriber.ts +2 -8
- package/src/orderSubscriber/WebsocketSubscription.ts +1 -1
- package/src/orderSubscriber/grpcSubscription.ts +2 -2
- package/src/orderSubscriber/types.ts +0 -2
- package/src/priorityFee/priorityFeeSubscriber.ts +5 -12
- package/src/priorityFee/priorityFeeSubscriberMap.ts +2 -10
- package/src/priorityFee/types.ts +3 -13
- package/src/priorityFee/velocityPriorityFeeMethod.ts +0 -12
- package/src/pyth/pythLazerSubscriber.ts +1 -1
- package/src/swap/UnifiedSwapClient.ts +1 -1
- package/src/swift/signedMsgUserAccountSubscriber.ts +2 -8
- package/src/swift/swiftOrderSubscriber.ts +7 -12
- package/src/types.ts +84 -102
- package/src/user.ts +9 -14
- package/src/userConfig.ts +1 -3
- package/src/userMap/WebsocketSubscription.ts +1 -1
- package/src/userMap/grpcSubscription.ts +1 -1
- package/src/userMap/referrerMap.ts +0 -4
- package/src/userMap/revenueShareEscrowMap.ts +0 -4
- package/src/userMap/userMap.ts +3 -7
- package/src/userMap/userMapConfig.ts +3 -3
- package/src/userMap/userStatsMap.ts +0 -4
- package/src/userStats.ts +1 -5
- package/src/userStatsConfig.ts +3 -3
- package/src/util/ed25519Utils.ts +1 -1
- package/src/velocityClient.ts +339 -225
- package/src/velocityClientConfig.ts +0 -10
- package/tests/{DriftCore → VelocityCore}/decode.test.ts +1 -1
- package/tests/{DriftCore → VelocityCore}/fill_trigger.test.ts +1 -1
- package/tests/{DriftCore → VelocityCore}/instructions.test.ts +1 -1
- package/tests/{DriftCore → VelocityCore}/pdas.test.ts +4 -2
- package/tests/ci/verifyConstants.ts +24 -27
- package/tests/decode/test.ts +2 -2
- package/tests/dlob/helpers.ts +5 -9
- package/tests/events/parseLogsForCuUsage.ts +15 -15
- package/tests/user/helpers.ts +9 -9
- package/tests/user/test.ts +13 -10
- package/lib/browser/util/deprecatedAlias.d.ts +0 -26
- package/lib/browser/util/deprecatedAlias.js +0 -10
- package/lib/node/idl/drift.d.ts.map +0 -1
- package/lib/node/util/deprecatedAlias.d.ts +0 -27
- package/lib/node/util/deprecatedAlias.d.ts.map +0 -1
- package/lib/node/util/deprecatedAlias.js +0 -10
- package/src/util/deprecatedAlias.ts +0 -21
- /package/lib/browser/idl/{drift.js → velocity.js} +0 -0
- /package/lib/node/idl/{drift.js → velocity.js} +0 -0
- /package/tests/{DriftCore → VelocityCore}/perp_orders.test.ts +0 -0
- /package/tests/{DriftCore → VelocityCore}/remainingAccounts.test.ts +0 -0
- /package/tests/{DriftCore → VelocityCore}/settlement_liquidation.test.ts +0 -0
package/src/index.ts
CHANGED
|
@@ -26,10 +26,7 @@ export { WebSocketAccountSubscriberV2 } from './accounts/webSocketAccountSubscri
|
|
|
26
26
|
export { WebSocketProgramAccountSubscriber } from './accounts/webSocketProgramAccountSubscriber';
|
|
27
27
|
export { WebSocketProgramUserAccountSubscriber } from './accounts/websocketProgramUserAccountSubscriber';
|
|
28
28
|
export { WebSocketProgramAccountsSubscriberV2 } from './accounts/webSocketProgramAccountsSubscriberV2';
|
|
29
|
-
export {
|
|
30
|
-
WebSocketVelocityClientAccountSubscriberV2,
|
|
31
|
-
WebSocketDriftClientAccountSubscriberV2,
|
|
32
|
-
} from './accounts/webSocketVelocityClientAccountSubscriberV2';
|
|
29
|
+
export { WebSocketVelocityClientAccountSubscriberV2 } from './accounts/webSocketVelocityClientAccountSubscriberV2';
|
|
33
30
|
export * from './accounts/bulkAccountLoader';
|
|
34
31
|
export * from './accounts/bulkUserSubscription';
|
|
35
32
|
export * from './accounts/bulkUserStatsSubscription';
|
package/src/margin/README.md
CHANGED
|
@@ -4,8 +4,8 @@ This document describes the single-source-of-truth margin engine in the SDK that
|
|
|
4
4
|
|
|
5
5
|
### Alignment with on-chain
|
|
6
6
|
|
|
7
|
-
- The SDK snapshot shape mirrors `programs/
|
|
8
|
-
- The inputs and ordering mirror `calculate_margin_requirement_and_total_collateral_and_liability_info` in `programs/
|
|
7
|
+
- The SDK snapshot shape mirrors `programs/velocity/src/state/margin_calculation.rs` field-for-field.
|
|
8
|
+
- The inputs and ordering mirror `calculate_margin_requirement_and_total_collateral_and_liability_info` in `programs/velocity/src/math/margin.rs`.
|
|
9
9
|
- Isolated positions are represented as `isolatedMarginCalculations` keyed by perp `marketIndex`, matching program logic.
|
|
10
10
|
|
|
11
11
|
### Core SDK types (shape parity)
|
package/src/math/amm.ts
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
} from '../constants/numericConstants';
|
|
18
18
|
import {
|
|
19
19
|
AMM,
|
|
20
|
+
MarketStats,
|
|
20
21
|
PositionDirection,
|
|
21
22
|
SwapDirection,
|
|
22
23
|
PerpMarketAccount,
|
|
@@ -52,6 +53,7 @@ export function calculatePegFromTargetPrice(
|
|
|
52
53
|
|
|
53
54
|
export function calculateOptimalPegAndBudget(
|
|
54
55
|
amm: AMM,
|
|
56
|
+
totalExchangeFee: BN,
|
|
55
57
|
mmOraclePriceData: MMOraclePriceData
|
|
56
58
|
): [BN, BN, BN, boolean] {
|
|
57
59
|
const reservePriceBefore = calculatePrice(
|
|
@@ -67,7 +69,7 @@ export function calculateOptimalPegAndBudget(
|
|
|
67
69
|
);
|
|
68
70
|
const prePegCost = calculateRepegCost(amm, newPeg);
|
|
69
71
|
|
|
70
|
-
const totalFeeLB =
|
|
72
|
+
const totalFeeLB = totalExchangeFee.div(new BN(2));
|
|
71
73
|
const budget = BN.max(ZERO, amm.totalFeeMinusDistributions.sub(totalFeeLB));
|
|
72
74
|
|
|
73
75
|
let checkLowerBound = true;
|
|
@@ -102,7 +104,7 @@ export function calculateOptimalPegAndBudget(
|
|
|
102
104
|
|
|
103
105
|
return [newTargetPrice, newOptimalPeg, newBudget, false];
|
|
104
106
|
} else if (
|
|
105
|
-
amm.totalFeeMinusDistributions.lt(
|
|
107
|
+
amm.totalFeeMinusDistributions.lt(totalExchangeFee.div(new BN(2)))
|
|
106
108
|
) {
|
|
107
109
|
checkLowerBound = false;
|
|
108
110
|
}
|
|
@@ -113,13 +115,14 @@ export function calculateOptimalPegAndBudget(
|
|
|
113
115
|
|
|
114
116
|
export function calculateNewAmm(
|
|
115
117
|
amm: AMM,
|
|
118
|
+
totalExchangeFee: BN,
|
|
116
119
|
mmOraclePriceData: MMOraclePriceData
|
|
117
120
|
): [BN, BN, BN, BN] {
|
|
118
121
|
let pKNumer = new BN(1);
|
|
119
122
|
let pKDenom = new BN(1);
|
|
120
123
|
|
|
121
124
|
const [targetPrice, _newPeg, budget, _checkLowerBound] =
|
|
122
|
-
calculateOptimalPegAndBudget(amm, mmOraclePriceData);
|
|
125
|
+
calculateOptimalPegAndBudget(amm, totalExchangeFee, mmOraclePriceData);
|
|
123
126
|
let prePegCost = calculateRepegCost(amm, _newPeg);
|
|
124
127
|
let newPeg = _newPeg;
|
|
125
128
|
|
|
@@ -155,6 +158,7 @@ export function calculateNewAmm(
|
|
|
155
158
|
|
|
156
159
|
export function calculateUpdatedAMM(
|
|
157
160
|
amm: AMM,
|
|
161
|
+
totalExchangeFee: BN,
|
|
158
162
|
mmOraclePriceData: MMOraclePriceData
|
|
159
163
|
): AMM {
|
|
160
164
|
if (amm.curveUpdateIntensity == 0 || mmOraclePriceData === undefined) {
|
|
@@ -163,6 +167,7 @@ export function calculateUpdatedAMM(
|
|
|
163
167
|
const newAmm = Object.assign({}, amm);
|
|
164
168
|
const [prepegCost, pKNumer, pKDenom, newPeg] = calculateNewAmm(
|
|
165
169
|
amm,
|
|
170
|
+
totalExchangeFee,
|
|
166
171
|
mmOraclePriceData
|
|
167
172
|
);
|
|
168
173
|
|
|
@@ -195,13 +200,16 @@ export function calculateUpdatedAMM(
|
|
|
195
200
|
|
|
196
201
|
export function calculateUpdatedAMMSpreadReserves(
|
|
197
202
|
amm: AMM,
|
|
203
|
+
marketStats: MarketStats,
|
|
204
|
+
totalExchangeFee: BN,
|
|
198
205
|
direction: PositionDirection,
|
|
199
206
|
mmOraclePriceData: MMOraclePriceData,
|
|
200
207
|
latestSlot?: BN
|
|
201
208
|
): { baseAssetReserve: BN; quoteAssetReserve: BN; sqrtK: BN; newPeg: BN } {
|
|
202
|
-
const newAmm = calculateUpdatedAMM(amm, mmOraclePriceData);
|
|
209
|
+
const newAmm = calculateUpdatedAMM(amm, totalExchangeFee, mmOraclePriceData);
|
|
203
210
|
const [shortReserves, longReserves] = calculateSpreadReserves(
|
|
204
211
|
newAmm,
|
|
212
|
+
marketStats,
|
|
205
213
|
mmOraclePriceData,
|
|
206
214
|
undefined,
|
|
207
215
|
latestSlot
|
|
@@ -223,19 +231,22 @@ export function calculateUpdatedAMMSpreadReserves(
|
|
|
223
231
|
|
|
224
232
|
export function calculateBidAskPrice(
|
|
225
233
|
amm: AMM,
|
|
234
|
+
marketStats: MarketStats,
|
|
235
|
+
totalExchangeFee: BN,
|
|
226
236
|
mmOraclePriceData: MMOraclePriceData,
|
|
227
237
|
withUpdate = true,
|
|
228
238
|
latestSlot?: BN
|
|
229
239
|
): [BN, BN] {
|
|
230
240
|
let newAmm: AMM;
|
|
231
241
|
if (withUpdate) {
|
|
232
|
-
newAmm = calculateUpdatedAMM(amm, mmOraclePriceData);
|
|
242
|
+
newAmm = calculateUpdatedAMM(amm, totalExchangeFee, mmOraclePriceData);
|
|
233
243
|
} else {
|
|
234
244
|
newAmm = amm;
|
|
235
245
|
}
|
|
236
246
|
|
|
237
247
|
const [bidReserves, askReserves] = calculateSpreadReserves(
|
|
238
248
|
newAmm,
|
|
249
|
+
marketStats,
|
|
239
250
|
mmOraclePriceData,
|
|
240
251
|
undefined,
|
|
241
252
|
latestSlot
|
|
@@ -844,6 +855,7 @@ export function calculateSpreadBN(
|
|
|
844
855
|
|
|
845
856
|
export function calculateSpread(
|
|
846
857
|
amm: AMM,
|
|
858
|
+
marketStats: MarketStats,
|
|
847
859
|
oraclePriceData: OraclePriceData,
|
|
848
860
|
now?: BN,
|
|
849
861
|
reservePrice?: BN
|
|
@@ -867,9 +879,13 @@ export function calculateSpread(
|
|
|
867
879
|
.div(reservePrice);
|
|
868
880
|
|
|
869
881
|
now = now || new BN(new Date().getTime() / 1000); //todo
|
|
870
|
-
const liveOracleStd = calculateLiveOracleStd(
|
|
882
|
+
const liveOracleStd = calculateLiveOracleStd(
|
|
883
|
+
marketStats,
|
|
884
|
+
oraclePriceData,
|
|
885
|
+
now
|
|
886
|
+
);
|
|
871
887
|
const confIntervalPct = getNewOracleConfPct(
|
|
872
|
-
|
|
888
|
+
marketStats,
|
|
873
889
|
oraclePriceData,
|
|
874
890
|
reservePrice,
|
|
875
891
|
now
|
|
@@ -890,11 +906,11 @@ export function calculateSpread(
|
|
|
890
906
|
amm.baseAssetReserve,
|
|
891
907
|
amm.minBaseAssetReserve,
|
|
892
908
|
amm.maxBaseAssetReserve,
|
|
893
|
-
|
|
909
|
+
marketStats.markStd,
|
|
894
910
|
liveOracleStd,
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
911
|
+
marketStats.longIntensityVolume,
|
|
912
|
+
marketStats.shortIntensityVolume,
|
|
913
|
+
marketStats.volume24H,
|
|
898
914
|
amm.ammInventorySpreadAdjustment
|
|
899
915
|
);
|
|
900
916
|
let longSpread = spreads[0];
|
|
@@ -925,6 +941,7 @@ export function calculateSpread(
|
|
|
925
941
|
|
|
926
942
|
export function calculateSpreadReserves(
|
|
927
943
|
amm: AMM,
|
|
944
|
+
marketStats: MarketStats,
|
|
928
945
|
mmOraclePriceData: MMOraclePriceData,
|
|
929
946
|
now?: BN,
|
|
930
947
|
latestSlot?: BN
|
|
@@ -1022,25 +1039,27 @@ export function calculateSpreadReserves(
|
|
|
1022
1039
|
|
|
1023
1040
|
referencePriceOffset = calculateReferencePriceOffset(
|
|
1024
1041
|
reservePrice,
|
|
1025
|
-
|
|
1042
|
+
marketStats.last24HAvgFundingRate,
|
|
1026
1043
|
liquidityFractionAfterDeadband,
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1044
|
+
marketStats.historicalOracleData.lastOraclePriceTwap5Min,
|
|
1045
|
+
marketStats.lastMarkPriceTwap5Min,
|
|
1046
|
+
marketStats.historicalOracleData.lastOraclePriceTwap,
|
|
1047
|
+
marketStats.lastMarkPriceTwap,
|
|
1031
1048
|
maxOffset
|
|
1032
1049
|
).toNumber();
|
|
1033
1050
|
}
|
|
1034
1051
|
|
|
1035
1052
|
let [longSpread, shortSpread] = calculateSpread(
|
|
1036
1053
|
amm,
|
|
1054
|
+
marketStats,
|
|
1037
1055
|
mmOraclePriceData,
|
|
1038
1056
|
now,
|
|
1039
1057
|
reservePrice
|
|
1040
1058
|
);
|
|
1041
1059
|
|
|
1060
|
+
const lastReferencePriceOffset = marketStats.lastReferencePriceOffset;
|
|
1042
1061
|
const doReferencePricOffsetSmooth =
|
|
1043
|
-
Math.sign(referencePriceOffset) !== Math.sign(
|
|
1062
|
+
Math.sign(referencePriceOffset) !== Math.sign(lastReferencePriceOffset) &&
|
|
1044
1063
|
amm.curveUpdateIntensity > 100;
|
|
1045
1064
|
|
|
1046
1065
|
if (doReferencePricOffsetSmooth) {
|
|
@@ -1048,17 +1067,17 @@ export function calculateSpreadReserves(
|
|
|
1048
1067
|
latestSlot != null
|
|
1049
1068
|
? BN.max(latestSlot.sub(amm.lastUpdateSlot), ZERO).toNumber()
|
|
1050
1069
|
: 0;
|
|
1051
|
-
const fullOffsetDelta = referencePriceOffset -
|
|
1070
|
+
const fullOffsetDelta = referencePriceOffset - lastReferencePriceOffset;
|
|
1052
1071
|
const raw = Math.trunc(
|
|
1053
1072
|
Math.min(Math.abs(fullOffsetDelta), slotsPassed * 1000) / 10
|
|
1054
1073
|
);
|
|
1055
1074
|
const maxAllowed =
|
|
1056
|
-
Math.abs(
|
|
1075
|
+
Math.abs(lastReferencePriceOffset) || Math.abs(referencePriceOffset);
|
|
1057
1076
|
|
|
1058
1077
|
const magnitude = Math.min(Math.max(raw, 10), maxAllowed);
|
|
1059
1078
|
const referencePriceDelta = Math.sign(fullOffsetDelta) * magnitude;
|
|
1060
1079
|
|
|
1061
|
-
referencePriceOffset =
|
|
1080
|
+
referencePriceOffset = lastReferencePriceOffset + referencePriceDelta;
|
|
1062
1081
|
|
|
1063
1082
|
if (referencePriceDelta < 0) {
|
|
1064
1083
|
longSpread += Math.abs(referencePriceDelta);
|
|
@@ -1159,6 +1178,7 @@ export function calculateTerminalPrice(market: PerpMarketAccount) {
|
|
|
1159
1178
|
|
|
1160
1179
|
export function calculateMaxBaseAssetAmountToTrade(
|
|
1161
1180
|
amm: AMM,
|
|
1181
|
+
marketStats: MarketStats,
|
|
1162
1182
|
limit_price: BN,
|
|
1163
1183
|
direction: PositionDirection,
|
|
1164
1184
|
mmOraclePriceData?: MMOraclePriceData,
|
|
@@ -1175,6 +1195,7 @@ export function calculateMaxBaseAssetAmountToTrade(
|
|
|
1175
1195
|
const newBaseAssetReserve = squareRootBN(newBaseAssetReserveSquared);
|
|
1176
1196
|
const [shortSpreadReserves, longSpreadReserves] = calculateSpreadReserves(
|
|
1177
1197
|
amm,
|
|
1198
|
+
marketStats,
|
|
1178
1199
|
mmOraclePriceData,
|
|
1179
1200
|
now
|
|
1180
1201
|
);
|
|
@@ -1221,6 +1242,7 @@ export function calculateQuoteAssetAmountSwapped(
|
|
|
1221
1242
|
|
|
1222
1243
|
export function calculateMaxBaseAssetAmountFillable(
|
|
1223
1244
|
amm: AMM,
|
|
1245
|
+
orderStepSize: BN,
|
|
1224
1246
|
orderDirection: PositionDirection
|
|
1225
1247
|
): BN {
|
|
1226
1248
|
const maxFillSize = amm.baseAssetReserve.div(
|
|
@@ -1241,6 +1263,6 @@ export function calculateMaxBaseAssetAmountFillable(
|
|
|
1241
1263
|
|
|
1242
1264
|
return standardizeBaseAssetAmount(
|
|
1243
1265
|
BN.min(maxFillSize, maxBaseAssetAmountOnSide),
|
|
1244
|
-
|
|
1266
|
+
orderStepSize
|
|
1245
1267
|
);
|
|
1246
1268
|
}
|
package/src/math/auction.ts
CHANGED
|
@@ -254,11 +254,11 @@ export function getTriggerAuctionStartPrice(params: {
|
|
|
254
254
|
const { perpMarket, direction, oraclePrice, limitPrice } = params;
|
|
255
255
|
|
|
256
256
|
const twapMismatch =
|
|
257
|
-
perpMarket.
|
|
258
|
-
.sub(perpMarket.
|
|
257
|
+
perpMarket.marketStats.historicalOracleData.lastOraclePriceTwapTs
|
|
258
|
+
.sub(perpMarket.marketStats.lastMarkPriceTwapTs)
|
|
259
259
|
.abs()
|
|
260
260
|
.gte(new BN(60)) ||
|
|
261
|
-
perpMarket.
|
|
261
|
+
perpMarket.marketStats.volume24H.lte(new BN(100_000).mul(QUOTE_PRECISION));
|
|
262
262
|
|
|
263
263
|
let baselineStartOffset: BN;
|
|
264
264
|
|
|
@@ -266,27 +266,31 @@ export function getTriggerAuctionStartPrice(params: {
|
|
|
266
266
|
const contractTierNumber = getPerpMarketTierNumber(perpMarket);
|
|
267
267
|
const priceDivisor = contractTierNumber <= 1 ? 500 : 100;
|
|
268
268
|
baselineStartOffset = isVariant(direction, 'long')
|
|
269
|
-
? perpMarket.
|
|
270
|
-
: perpMarket.
|
|
269
|
+
? perpMarket.marketStats.lastBidPriceTwap.divn(priceDivisor)
|
|
270
|
+
: perpMarket.marketStats.lastAskPriceTwap.divn(priceDivisor).neg();
|
|
271
271
|
} else {
|
|
272
272
|
const markTwapSlow = isVariant(direction, 'long')
|
|
273
|
-
? perpMarket.
|
|
274
|
-
: perpMarket.
|
|
273
|
+
? perpMarket.marketStats.lastBidPriceTwap
|
|
274
|
+
: perpMarket.marketStats.lastAskPriceTwap;
|
|
275
275
|
|
|
276
|
-
const markTwapFast = perpMarket.
|
|
276
|
+
const markTwapFast = perpMarket.marketStats.lastMarkPriceTwap5Min;
|
|
277
277
|
const oracleTwapSlow =
|
|
278
|
-
perpMarket.
|
|
278
|
+
perpMarket.marketStats.historicalOracleData.lastOraclePriceTwap;
|
|
279
279
|
const oracleTwapFast =
|
|
280
|
-
perpMarket.
|
|
280
|
+
perpMarket.marketStats.historicalOracleData.lastOraclePriceTwap5Min;
|
|
281
281
|
|
|
282
282
|
const offsetSlow = markTwapSlow.sub(oracleTwapSlow);
|
|
283
283
|
const offsetFast = markTwapFast.sub(oracleTwapFast);
|
|
284
284
|
|
|
285
|
-
|
|
285
|
+
// long_spread/short_spread were removed from AMM in the decoupling refactor.
|
|
286
|
+
// Fall back to half base_spread as the per-side spread approximation; the
|
|
287
|
+
// AMM no longer caches an exact per-side spread without oracle context.
|
|
288
|
+
const halfBaseSpread = new BN(Math.floor(perpMarket.amm.baseSpread / 2));
|
|
289
|
+
const fracOfLongSpreadInPrice = halfBaseSpread
|
|
286
290
|
.mul(markTwapSlow)
|
|
287
291
|
.div(PRICE_PRECISION.muln(10)); // divide by 10x for safety
|
|
288
292
|
|
|
289
|
-
const fracOfShortSpreadInPrice =
|
|
293
|
+
const fracOfShortSpreadInPrice = halfBaseSpread
|
|
290
294
|
.mul(markTwapSlow)
|
|
291
295
|
.div(PRICE_PRECISION.muln(10)); // divide by 10x for safety
|
|
292
296
|
|
package/src/math/builder.ts
CHANGED
|
@@ -1,4 +1,48 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
|
+
import {
|
|
3
|
+
OrderParams,
|
|
4
|
+
ReferrerStatus,
|
|
5
|
+
RevenueShareEscrowAccount,
|
|
6
|
+
RevenueShareOrder,
|
|
7
|
+
UserStatsAccount,
|
|
8
|
+
} from '../types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* True when the user's RevenueShareEscrow was initialized with a referrer.
|
|
12
|
+
* Fills for such users must include the escrow account or the program rejects
|
|
13
|
+
* them with UnableToLoadRevenueShareAccount.
|
|
14
|
+
*/
|
|
15
|
+
export function isBuilderReferral(
|
|
16
|
+
userStats: Pick<UserStatsAccount, 'referrerStatus'>
|
|
17
|
+
): boolean {
|
|
18
|
+
return (userStats.referrerStatus & ReferrerStatus.BuilderReferral) !== 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* True when the escrow was initialized with a referrer. Referral rewards
|
|
23
|
+
* accrue into such escrows on fills, so fills of the escrow owner's orders
|
|
24
|
+
* must include the escrow account (see `isBuilderReferral`).
|
|
25
|
+
*/
|
|
26
|
+
export function escrowHasReferrer(
|
|
27
|
+
escrow: Pick<RevenueShareEscrowAccount, 'referrer'>
|
|
28
|
+
): boolean {
|
|
29
|
+
return !escrow.referrer.equals(PublicKey.default);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* True when the order params carry a builder code (`builderIdx` +
|
|
34
|
+
* `builderFeeTenthBps`).
|
|
35
|
+
*/
|
|
36
|
+
export function hasBuilderParams(
|
|
37
|
+
orderParams: Pick<OrderParams, 'builderIdx' | 'builderFeeTenthBps'>
|
|
38
|
+
): boolean {
|
|
39
|
+
return (
|
|
40
|
+
orderParams.builderIdx !== null &&
|
|
41
|
+
orderParams.builderIdx !== undefined &&
|
|
42
|
+
orderParams.builderFeeTenthBps !== null &&
|
|
43
|
+
orderParams.builderFeeTenthBps !== undefined
|
|
44
|
+
);
|
|
45
|
+
}
|
|
2
46
|
|
|
3
47
|
const FLAG_IS_OPEN = 0x01;
|
|
4
48
|
export function isBuilderOrderOpen(order: RevenueShareOrder): boolean {
|
package/src/math/funding.ts
CHANGED
|
@@ -28,8 +28,8 @@ function calculateLiveMarkTwap(
|
|
|
28
28
|
): BN {
|
|
29
29
|
now = now || new BN((Date.now() / 1000).toFixed(0));
|
|
30
30
|
|
|
31
|
-
const lastMarkTwapWithMantissa = market.
|
|
32
|
-
const lastMarkPriceTwapTs = market.
|
|
31
|
+
const lastMarkTwapWithMantissa = market.marketStats.lastMarkPriceTwap;
|
|
32
|
+
const lastMarkPriceTwapTs = market.marketStats.lastMarkPriceTwapTs;
|
|
33
33
|
|
|
34
34
|
const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
35
35
|
const markTwapTimeSinceLastUpdate = BN.max(
|
|
@@ -38,7 +38,12 @@ function calculateLiveMarkTwap(
|
|
|
38
38
|
);
|
|
39
39
|
|
|
40
40
|
if (!markPrice) {
|
|
41
|
-
const [bid, ask] = calculateBidAskPrice(
|
|
41
|
+
const [bid, ask] = calculateBidAskPrice(
|
|
42
|
+
market.amm,
|
|
43
|
+
market.marketStats,
|
|
44
|
+
market.totalExchangeFee,
|
|
45
|
+
mmOraclePriceData
|
|
46
|
+
);
|
|
42
47
|
markPrice = bid.add(ask).div(new BN(2));
|
|
43
48
|
}
|
|
44
49
|
|
|
@@ -60,25 +65,28 @@ function shrinkStaleTwaps(
|
|
|
60
65
|
let newMarkTwap = markTwapWithMantissa;
|
|
61
66
|
let newOracleTwap = oracleTwapWithMantissa;
|
|
62
67
|
if (
|
|
63
|
-
market.
|
|
64
|
-
market.
|
|
68
|
+
market.marketStats.lastMarkPriceTwapTs.gt(
|
|
69
|
+
market.marketStats.historicalOracleData.lastOraclePriceTwapTs
|
|
65
70
|
)
|
|
66
71
|
) {
|
|
67
72
|
// shrink oracle based on invalid intervals
|
|
68
73
|
const oracleInvalidDuration = BN.max(
|
|
69
74
|
ZERO,
|
|
70
|
-
market.
|
|
71
|
-
market.
|
|
75
|
+
market.marketStats.lastMarkPriceTwapTs.sub(
|
|
76
|
+
market.marketStats.historicalOracleData.lastOraclePriceTwapTs
|
|
72
77
|
)
|
|
73
78
|
);
|
|
74
79
|
const timeSinceLastOracleTwapUpdate = now.sub(
|
|
75
|
-
market.
|
|
80
|
+
market.marketStats.historicalOracleData.lastOraclePriceTwapTs
|
|
76
81
|
);
|
|
77
82
|
const oracleTwapTimeSinceLastUpdate = BN.max(
|
|
78
83
|
ONE,
|
|
79
84
|
BN.min(
|
|
80
|
-
market.
|
|
81
|
-
BN.max(
|
|
85
|
+
market.marketStats.fundingPeriod,
|
|
86
|
+
BN.max(
|
|
87
|
+
ONE,
|
|
88
|
+
market.marketStats.fundingPeriod.sub(timeSinceLastOracleTwapUpdate)
|
|
89
|
+
)
|
|
82
90
|
)
|
|
83
91
|
);
|
|
84
92
|
newOracleTwap = oracleTwapTimeSinceLastUpdate
|
|
@@ -86,23 +94,28 @@ function shrinkStaleTwaps(
|
|
|
86
94
|
.add(oracleInvalidDuration.mul(markTwapWithMantissa))
|
|
87
95
|
.div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
|
|
88
96
|
} else if (
|
|
89
|
-
market.
|
|
90
|
-
market.
|
|
97
|
+
market.marketStats.lastMarkPriceTwapTs.lt(
|
|
98
|
+
market.marketStats.historicalOracleData.lastOraclePriceTwapTs
|
|
91
99
|
)
|
|
92
100
|
) {
|
|
93
101
|
// shrink mark to oracle twap over tradless intervals
|
|
94
102
|
const tradelessDuration = BN.max(
|
|
95
103
|
ZERO,
|
|
96
|
-
market.
|
|
97
|
-
market.
|
|
104
|
+
market.marketStats.historicalOracleData.lastOraclePriceTwapTs.sub(
|
|
105
|
+
market.marketStats.lastMarkPriceTwapTs
|
|
98
106
|
)
|
|
99
107
|
);
|
|
100
|
-
const timeSinceLastMarkTwapUpdate = now.sub(
|
|
108
|
+
const timeSinceLastMarkTwapUpdate = now.sub(
|
|
109
|
+
market.marketStats.lastMarkPriceTwapTs
|
|
110
|
+
);
|
|
101
111
|
const markTwapTimeSinceLastUpdate = BN.max(
|
|
102
112
|
ONE,
|
|
103
113
|
BN.min(
|
|
104
|
-
market.
|
|
105
|
-
BN.max(
|
|
114
|
+
market.marketStats.fundingPeriod,
|
|
115
|
+
BN.max(
|
|
116
|
+
ONE,
|
|
117
|
+
market.marketStats.fundingPeriod.sub(timeSinceLastMarkTwapUpdate)
|
|
118
|
+
)
|
|
106
119
|
)
|
|
107
120
|
);
|
|
108
121
|
newMarkTwap = markTwapTimeSinceLastUpdate
|
|
@@ -141,13 +154,13 @@ export function calculateAllEstimatedFundingRate(
|
|
|
141
154
|
mmOraclePriceData,
|
|
142
155
|
markPrice,
|
|
143
156
|
now,
|
|
144
|
-
market.
|
|
157
|
+
market.marketStats.fundingPeriod
|
|
145
158
|
);
|
|
146
159
|
const liveOracleTwap = calculateLiveOracleTwap(
|
|
147
|
-
market.
|
|
160
|
+
market.marketStats.historicalOracleData,
|
|
148
161
|
oraclePriceData,
|
|
149
162
|
now,
|
|
150
|
-
market.
|
|
163
|
+
market.marketStats.fundingPeriod
|
|
151
164
|
);
|
|
152
165
|
const [markTwap, oracleTwap] = shrinkStaleTwaps(
|
|
153
166
|
market,
|
|
@@ -187,10 +200,10 @@ export function calculateAllEstimatedFundingRate(
|
|
|
187
200
|
|
|
188
201
|
const secondsInHour = new BN(3600);
|
|
189
202
|
const hoursInDay = new BN(24);
|
|
190
|
-
const timeSinceLastUpdate = now.sub(market.
|
|
203
|
+
const timeSinceLastUpdate = now.sub(market.lastFundingRateTs);
|
|
191
204
|
|
|
192
205
|
const lowerboundEst = twapSpreadPct
|
|
193
|
-
.mul(market.
|
|
206
|
+
.mul(market.marketStats.fundingPeriod)
|
|
194
207
|
.mul(BN.min(secondsInHour, timeSinceLastUpdate))
|
|
195
208
|
.div(secondsInHour)
|
|
196
209
|
.div(secondsInHour)
|
|
@@ -210,19 +223,15 @@ export function calculateAllEstimatedFundingRate(
|
|
|
210
223
|
let cappedAltEst: BN;
|
|
211
224
|
let largerSide: BN;
|
|
212
225
|
let smallerSide: BN;
|
|
213
|
-
if (
|
|
214
|
-
market.
|
|
215
|
-
|
|
216
|
-
largerSide = market.amm.baseAssetAmountLong.abs();
|
|
217
|
-
smallerSide = market.amm.baseAssetAmountShort.abs();
|
|
226
|
+
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
227
|
+
largerSide = market.baseAssetAmountLong.abs();
|
|
228
|
+
smallerSide = market.baseAssetAmountShort.abs();
|
|
218
229
|
if (twapSpread.gt(new BN(0))) {
|
|
219
230
|
return [markTwap, oracleTwap, lowerboundEst, interpEst, interpEst];
|
|
220
231
|
}
|
|
221
|
-
} else if (
|
|
222
|
-
market.
|
|
223
|
-
|
|
224
|
-
largerSide = market.amm.baseAssetAmountShort.abs();
|
|
225
|
-
smallerSide = market.amm.baseAssetAmountLong.abs();
|
|
232
|
+
} else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
233
|
+
largerSide = market.baseAssetAmountShort.abs();
|
|
234
|
+
smallerSide = market.baseAssetAmountLong.abs();
|
|
226
235
|
if (twapSpread.lt(new BN(0))) {
|
|
227
236
|
return [markTwap, oracleTwap, lowerboundEst, interpEst, interpEst];
|
|
228
237
|
}
|
|
@@ -370,11 +379,9 @@ export function calculateLongShortFundingRate(
|
|
|
370
379
|
now
|
|
371
380
|
);
|
|
372
381
|
|
|
373
|
-
if (market.
|
|
382
|
+
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort)) {
|
|
374
383
|
return [cappedAltEst, interpEst];
|
|
375
|
-
} else if (
|
|
376
|
-
market.amm.baseAssetAmountLong.lt(market.amm.baseAssetAmountShort)
|
|
377
|
-
) {
|
|
384
|
+
} else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort)) {
|
|
378
385
|
return [interpEst, cappedAltEst];
|
|
379
386
|
} else {
|
|
380
387
|
return [interpEst, interpEst];
|
|
@@ -404,13 +411,9 @@ export function calculateLongShortFundingRateAndLiveTwaps(
|
|
|
404
411
|
now
|
|
405
412
|
);
|
|
406
413
|
|
|
407
|
-
if (
|
|
408
|
-
market.amm.baseAssetAmountLong.gt(market.amm.baseAssetAmountShort.abs())
|
|
409
|
-
) {
|
|
414
|
+
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
410
415
|
return [markTwapLive, oracleTwapLive, cappedAltEst, interpEst];
|
|
411
|
-
} else if (
|
|
412
|
-
market.amm.baseAssetAmountLong.lt(market.amm.baseAssetAmountShort.abs())
|
|
413
|
-
) {
|
|
416
|
+
} else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
414
417
|
return [markTwapLive, oracleTwapLive, interpEst, cappedAltEst];
|
|
415
418
|
} else {
|
|
416
419
|
return [markTwapLive, oracleTwapLive, interpEst, interpEst];
|
|
@@ -424,7 +427,7 @@ export function calculateLongShortFundingRateAndLiveTwaps(
|
|
|
424
427
|
*/
|
|
425
428
|
export function calculateFundingPool(market: PerpMarketAccount): BN {
|
|
426
429
|
// todo
|
|
427
|
-
const totalFeeLB = market.
|
|
430
|
+
const totalFeeLB = market.totalExchangeFee.div(new BN(2));
|
|
428
431
|
const feePool = BN.max(
|
|
429
432
|
ZERO,
|
|
430
433
|
market.amm.totalFeeMinusDistributions
|
package/src/math/margin.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Margin calculation helpers — TypeScript mirror of `programs/
|
|
2
|
+
* Margin calculation helpers — TypeScript mirror of `programs/velocity/src/math/margin.rs`.
|
|
3
3
|
* Computes initial/maintenance margin requirements, free collateral, and account health.
|
|
4
4
|
* Used by {@link User} for leverage queries and by keeper bots for liquidation eligibility checks.
|
|
5
5
|
*/
|
|
@@ -220,17 +220,17 @@ export function calculatePerpLiabilityValue(
|
|
|
220
220
|
* @returns
|
|
221
221
|
*/
|
|
222
222
|
export function calculateMarginUSDCRequiredForTrade(
|
|
223
|
-
|
|
223
|
+
velocityClient: VelocityClient,
|
|
224
224
|
targetMarketIndex: number,
|
|
225
225
|
baseSize: BN,
|
|
226
226
|
userMaxMarginRatio?: number,
|
|
227
227
|
entryPrice?: BN
|
|
228
228
|
): BN {
|
|
229
|
-
const targetMarket =
|
|
229
|
+
const targetMarket = velocityClient.getPerpMarketAccount(targetMarketIndex);
|
|
230
230
|
|
|
231
231
|
const price =
|
|
232
232
|
entryPrice ??
|
|
233
|
-
|
|
233
|
+
velocityClient.getOracleDataForPerpMarket(targetMarket.marketIndex).price;
|
|
234
234
|
|
|
235
235
|
const perpLiabilityValue = calculatePerpLiabilityValue(baseSize, price);
|
|
236
236
|
|
|
@@ -254,7 +254,7 @@ export function calculateMarginUSDCRequiredForTrade(
|
|
|
254
254
|
* Returns collateral required in the precision of the target collateral market.
|
|
255
255
|
*/
|
|
256
256
|
export function calculateCollateralDepositRequiredForTrade(
|
|
257
|
-
|
|
257
|
+
velocityClient: VelocityClient,
|
|
258
258
|
targetMarketIndex: number,
|
|
259
259
|
baseSize: BN,
|
|
260
260
|
collateralIndex: number,
|
|
@@ -262,17 +262,17 @@ export function calculateCollateralDepositRequiredForTrade(
|
|
|
262
262
|
estEntryPrice?: BN
|
|
263
263
|
): BN {
|
|
264
264
|
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(
|
|
265
|
-
|
|
265
|
+
velocityClient,
|
|
266
266
|
targetMarketIndex,
|
|
267
267
|
baseSize,
|
|
268
268
|
userMaxMarginRatio,
|
|
269
269
|
estEntryPrice
|
|
270
270
|
);
|
|
271
271
|
|
|
272
|
-
const collateralMarket =
|
|
272
|
+
const collateralMarket = velocityClient.getSpotMarketAccount(collateralIndex);
|
|
273
273
|
|
|
274
274
|
const collateralOracleData =
|
|
275
|
-
|
|
275
|
+
velocityClient.getOracleDataForSpotMarket(collateralIndex);
|
|
276
276
|
|
|
277
277
|
const scaledAssetWeight = calculateScaledInitialAssetWeight(
|
|
278
278
|
collateralMarket,
|
|
@@ -280,7 +280,7 @@ export function calculateCollateralDepositRequiredForTrade(
|
|
|
280
280
|
);
|
|
281
281
|
|
|
282
282
|
// Base amount required to deposit = (marginRequiredUsdc / priceOfAsset) / assetWeight .. (E.g. $100 required / $10000 price / 0.5 weight)
|
|
283
|
-
const baseAmountRequired =
|
|
283
|
+
const baseAmountRequired = velocityClient
|
|
284
284
|
.convertToSpotPrecision(collateralIndex, marginRequiredUsdc)
|
|
285
285
|
.mul(PRICE_PRECISION) // adjust for division by oracle price
|
|
286
286
|
.mul(SPOT_MARKET_WEIGHT_PRECISION) // adjust for division by scaled asset weight
|
|
@@ -294,14 +294,14 @@ export function calculateCollateralDepositRequiredForTrade(
|
|
|
294
294
|
}
|
|
295
295
|
|
|
296
296
|
export function calculateCollateralValueOfDeposit(
|
|
297
|
-
|
|
297
|
+
velocityClient: VelocityClient,
|
|
298
298
|
collateralIndex: number,
|
|
299
299
|
baseSize: BN
|
|
300
300
|
): BN {
|
|
301
|
-
const collateralMarket =
|
|
301
|
+
const collateralMarket = velocityClient.getSpotMarketAccount(collateralIndex);
|
|
302
302
|
|
|
303
303
|
const collateralOracleData =
|
|
304
|
-
|
|
304
|
+
velocityClient.getOracleDataForSpotMarket(collateralIndex);
|
|
305
305
|
|
|
306
306
|
const scaledAssetWeight = calculateScaledInitialAssetWeight(
|
|
307
307
|
collateralMarket,
|
|
@@ -341,20 +341,20 @@ export function calculateLiquidationPrice(
|
|
|
341
341
|
}
|
|
342
342
|
|
|
343
343
|
export function calculateUserMaxPerpOrderSize(
|
|
344
|
-
|
|
344
|
+
velocityClient: VelocityClient,
|
|
345
345
|
userAccountKey: PublicKey,
|
|
346
346
|
userAccount: UserAccount,
|
|
347
347
|
targetMarketIndex: number,
|
|
348
348
|
tradeSide: PositionDirection
|
|
349
349
|
): { tradeSize: BN; oppositeSideTradeSize: BN } {
|
|
350
350
|
const userAccountSubscriber = new OneShotUserAccountSubscriber(
|
|
351
|
-
|
|
351
|
+
velocityClient.program,
|
|
352
352
|
userAccountKey,
|
|
353
353
|
userAccount
|
|
354
354
|
);
|
|
355
355
|
|
|
356
356
|
const user = new User({
|
|
357
|
-
|
|
357
|
+
velocityClient,
|
|
358
358
|
userAccountPublicKey: userAccountKey,
|
|
359
359
|
accountSubscription: {
|
|
360
360
|
type: 'custom',
|