@drift-labs/common 1.0.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/lib/Config.d.ts +26 -0
- package/lib/Config.js +29 -0
- package/lib/Config.js.map +1 -0
- package/lib/EnvironmentConstants.d.ts +40 -0
- package/lib/EnvironmentConstants.js +64 -0
- package/lib/EnvironmentConstants.js.map +1 -0
- package/lib/actions/actionHelpers/accountDeletionHelpers.d.ts +16 -0
- package/lib/actions/actionHelpers/accountDeletionHelpers.js +151 -0
- package/lib/actions/actionHelpers/accountDeletionHelpers.js.map +1 -0
- package/lib/actions/actionHelpers/actionHelpers.d.ts +13 -0
- package/lib/actions/actionHelpers/actionHelpers.js +8 -0
- package/lib/actions/actionHelpers/actionHelpers.js.map +1 -0
- package/lib/chartConstants.d.ts +2 -0
- package/lib/chartConstants.js +14 -0
- package/lib/chartConstants.js.map +1 -0
- package/lib/clients/candleClient.d.ts +74 -0
- package/lib/clients/candleClient.js +364 -0
- package/lib/clients/candleClient.js.map +1 -0
- package/lib/clients/dataApiWsClient.d.ts +33 -0
- package/lib/clients/dataApiWsClient.js +118 -0
- package/lib/clients/dataApiWsClient.js.map +1 -0
- package/lib/clients/index.d.ts +1 -0
- package/lib/clients/index.js +19 -0
- package/lib/clients/index.js.map +1 -0
- package/lib/clients/marketDataFeed.d.ts +42 -0
- package/lib/clients/marketDataFeed.js +422 -0
- package/lib/clients/marketDataFeed.js.map +1 -0
- package/lib/clients/redisClient.d.ts +89 -0
- package/lib/clients/redisClient.js +647 -0
- package/lib/clients/redisClient.js.map +1 -0
- package/lib/clients/swiftClient.d.ts +57 -0
- package/lib/clients/swiftClient.js +289 -0
- package/lib/clients/swiftClient.js.map +1 -0
- package/lib/clients/tvFeed.d.ts +63 -0
- package/lib/clients/tvFeed.js +351 -0
- package/lib/clients/tvFeed.js.map +1 -0
- package/lib/common-ui-utils/commonUiUtils.d.ts +211 -0
- package/lib/common-ui-utils/commonUiUtils.js +624 -0
- package/lib/common-ui-utils/commonUiUtils.js.map +1 -0
- package/lib/common-ui-utils/index.d.ts +6 -0
- package/lib/common-ui-utils/index.js +23 -0
- package/lib/common-ui-utils/index.js.map +1 -0
- package/lib/common-ui-utils/market.d.ts +27 -0
- package/lib/common-ui-utils/market.js +80 -0
- package/lib/common-ui-utils/market.js.map +1 -0
- package/lib/common-ui-utils/order.d.ts +11 -0
- package/lib/common-ui-utils/order.js +161 -0
- package/lib/common-ui-utils/order.js.map +1 -0
- package/lib/common-ui-utils/settings/settings.d.ts +51 -0
- package/lib/common-ui-utils/settings/settings.js +84 -0
- package/lib/common-ui-utils/settings/settings.js.map +1 -0
- package/lib/common-ui-utils/trading.d.ts +56 -0
- package/lib/common-ui-utils/trading.js +200 -0
- package/lib/common-ui-utils/trading.js.map +1 -0
- package/lib/common-ui-utils/user.d.ts +13 -0
- package/lib/common-ui-utils/user.js +124 -0
- package/lib/common-ui-utils/user.js.map +1 -0
- package/lib/constants/autogenerated/driftErrors.json +1787 -0
- package/lib/constants/autogenerated/jup-v4-error-codes.json +67 -0
- package/lib/constants/autogenerated/jup-v6-error-codes.json +115 -0
- package/lib/constants/dev.d.ts +15 -0
- package/lib/constants/dev.js +19 -0
- package/lib/constants/dev.js.map +1 -0
- package/lib/constants/geoblockList.d.ts +4 -0
- package/lib/constants/geoblockList.js +32 -0
- package/lib/constants/geoblockList.js.map +1 -0
- package/lib/constants/index.d.ts +9 -0
- package/lib/constants/index.js +26 -0
- package/lib/constants/index.js.map +1 -0
- package/lib/constants/markets.d.ts +1 -0
- package/lib/constants/markets.js +5 -0
- package/lib/constants/markets.js.map +1 -0
- package/lib/constants/misc.d.ts +23 -0
- package/lib/constants/misc.js +27 -0
- package/lib/constants/misc.js.map +1 -0
- package/lib/constants/orders.d.ts +12 -0
- package/lib/constants/orders.js +79 -0
- package/lib/constants/orders.js.map +1 -0
- package/lib/constants/pools.d.ts +5 -0
- package/lib/constants/pools.js +9 -0
- package/lib/constants/pools.js.map +1 -0
- package/lib/constants/predictionMarket.d.ts +3 -0
- package/lib/constants/predictionMarket.js +7 -0
- package/lib/constants/predictionMarket.js.map +1 -0
- package/lib/constants/superstake.d.ts +22 -0
- package/lib/constants/superstake.js +45 -0
- package/lib/constants/superstake.js.map +1 -0
- package/lib/constants/trade.d.ts +2 -0
- package/lib/constants/trade.js +9 -0
- package/lib/constants/trade.js.map +1 -0
- package/lib/drift/Drift/clients/AuthorityDrift/DriftL2OrderbookManager.d.ts +68 -0
- package/lib/drift/Drift/clients/AuthorityDrift/DriftL2OrderbookManager.js +146 -0
- package/lib/drift/Drift/clients/AuthorityDrift/DriftL2OrderbookManager.js.map +1 -0
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/index.d.ts +204 -0
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/index.js +530 -0
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/index.js.map +1 -0
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/types.d.ts +90 -0
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/types.js +3 -0
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/types.js.map +1 -0
- package/lib/drift/Drift/clients/AuthorityDrift/SubscriptionManager.d.ts +139 -0
- package/lib/drift/Drift/clients/AuthorityDrift/SubscriptionManager.js +287 -0
- package/lib/drift/Drift/clients/AuthorityDrift/SubscriptionManager.js.map +1 -0
- package/lib/drift/Drift/clients/AuthorityDrift/index.d.ts +242 -0
- package/lib/drift/Drift/clients/AuthorityDrift/index.js +503 -0
- package/lib/drift/Drift/clients/AuthorityDrift/index.js.map +1 -0
- package/lib/drift/Drift/clients/CentralServerDrift.d.ts +91 -0
- package/lib/drift/Drift/clients/CentralServerDrift.js +326 -0
- package/lib/drift/Drift/clients/CentralServerDrift.js.map +1 -0
- package/lib/drift/Drift/clients/index.d.ts +3 -0
- package/lib/drift/Drift/clients/index.js +20 -0
- package/lib/drift/Drift/clients/index.js.map +1 -0
- package/lib/drift/Drift/constants/blockchain.d.ts +24 -0
- package/lib/drift/Drift/constants/blockchain.js +32 -0
- package/lib/drift/Drift/constants/blockchain.js.map +1 -0
- package/lib/drift/Drift/constants/errors.d.ts +6 -0
- package/lib/drift/Drift/constants/errors.js +14 -0
- package/lib/drift/Drift/constants/errors.js.map +1 -0
- package/lib/drift/Drift/constants/index.d.ts +3 -0
- package/lib/drift/Drift/constants/index.js +20 -0
- package/lib/drift/Drift/constants/index.js.map +1 -0
- package/lib/drift/Drift/constants/orderbook.d.ts +7 -0
- package/lib/drift/Drift/constants/orderbook.js +10 -0
- package/lib/drift/Drift/constants/orderbook.js.map +1 -0
- package/lib/drift/Drift/data/PollingDlob.d.ts +154 -0
- package/lib/drift/Drift/data/PollingDlob.js +387 -0
- package/lib/drift/Drift/data/PollingDlob.js.map +1 -0
- package/lib/drift/Drift/data/index.d.ts +1 -0
- package/lib/drift/Drift/data/index.js +18 -0
- package/lib/drift/Drift/data/index.js.map +1 -0
- package/lib/drift/Drift/index.d.ts +4 -0
- package/lib/drift/Drift/index.js +21 -0
- package/lib/drift/Drift/index.js.map +1 -0
- package/lib/drift/Drift/stores/MarkPriceCache.d.ts +27 -0
- package/lib/drift/Drift/stores/MarkPriceCache.js +59 -0
- package/lib/drift/Drift/stores/MarkPriceCache.js.map +1 -0
- package/lib/drift/Drift/stores/OraclePriceCache.d.ts +21 -0
- package/lib/drift/Drift/stores/OraclePriceCache.js +57 -0
- package/lib/drift/Drift/stores/OraclePriceCache.js.map +1 -0
- package/lib/drift/Drift/stores/UserAccountCache.d.ts +49 -0
- package/lib/drift/Drift/stores/UserAccountCache.js +107 -0
- package/lib/drift/Drift/stores/UserAccountCache.js.map +1 -0
- package/lib/drift/Drift/stores/index.d.ts +3 -0
- package/lib/drift/Drift/stores/index.js +20 -0
- package/lib/drift/Drift/stores/index.js.map +1 -0
- package/lib/drift/base/actions/index.d.ts +4 -0
- package/lib/drift/base/actions/index.js +21 -0
- package/lib/drift/base/actions/index.js.map +1 -0
- package/lib/drift/base/actions/perp/index.d.ts +2 -0
- package/lib/drift/base/actions/perp/index.js +19 -0
- package/lib/drift/base/actions/perp/index.js.map +1 -0
- package/lib/drift/base/actions/perp/settleFunding.d.ts +25 -0
- package/lib/drift/base/actions/perp/settleFunding.js +41 -0
- package/lib/drift/base/actions/perp/settleFunding.js.map +1 -0
- package/lib/drift/base/actions/perp/settlePnl.d.ts +35 -0
- package/lib/drift/base/actions/perp/settlePnl.js +44 -0
- package/lib/drift/base/actions/perp/settlePnl.js.map +1 -0
- package/lib/drift/base/actions/spot/borrow.d.ts +1 -0
- package/lib/drift/base/actions/spot/borrow.js +8 -0
- package/lib/drift/base/actions/spot/borrow.js.map +1 -0
- package/lib/drift/base/actions/spot/deposit.d.ts +40 -0
- package/lib/drift/base/actions/spot/deposit.js +82 -0
- package/lib/drift/base/actions/spot/deposit.js.map +1 -0
- package/lib/drift/base/actions/spot/index.d.ts +3 -0
- package/lib/drift/base/actions/spot/index.js +20 -0
- package/lib/drift/base/actions/spot/index.js.map +1 -0
- package/lib/drift/base/actions/spot/withdraw.d.ts +16 -0
- package/lib/drift/base/actions/spot/withdraw.js +39 -0
- package/lib/drift/base/actions/spot/withdraw.js.map +1 -0
- package/lib/drift/base/actions/trade/cancelOrder.d.ts +47 -0
- package/lib/drift/base/actions/trade/cancelOrder.js +55 -0
- package/lib/drift/base/actions/trade/cancelOrder.js.map +1 -0
- package/lib/drift/base/actions/trade/editOrder.d.ts +55 -0
- package/lib/drift/base/actions/trade/editOrder.js +35 -0
- package/lib/drift/base/actions/trade/editOrder.js.map +1 -0
- package/lib/drift/base/actions/trade/index.d.ts +4 -0
- package/lib/drift/base/actions/trade/index.js +21 -0
- package/lib/drift/base/actions/trade/index.js.map +1 -0
- package/lib/drift/base/actions/trade/openPerpOrder/index.d.ts +3 -0
- package/lib/drift/base/actions/trade/openPerpOrder/index.js +20 -0
- package/lib/drift/base/actions/trade/openPerpOrder/index.js.map +1 -0
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.d.ts +84 -0
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.js +293 -0
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.js.map +1 -0
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.d.ts +19 -0
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.js +161 -0
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.js.map +1 -0
- package/lib/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.d.ts +166 -0
- package/lib/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.js +156 -0
- package/lib/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.js.map +1 -0
- package/lib/drift/base/actions/trade/swap.d.ts +62 -0
- package/lib/drift/base/actions/trade/swap.js +60 -0
- package/lib/drift/base/actions/trade/swap.js.map +1 -0
- package/lib/drift/base/actions/user/create.d.ts +75 -0
- package/lib/drift/base/actions/user/create.js +104 -0
- package/lib/drift/base/actions/user/create.js.map +1 -0
- package/lib/drift/base/actions/user/delete.d.ts +47 -0
- package/lib/drift/base/actions/user/delete.js +39 -0
- package/lib/drift/base/actions/user/delete.js.map +1 -0
- package/lib/drift/base/actions/user/index.d.ts +1 -0
- package/lib/drift/base/actions/user/index.js +18 -0
- package/lib/drift/base/actions/user/index.js.map +1 -0
- package/lib/drift/base/constants/accountNames.d.ts +1 -0
- package/lib/drift/base/constants/accountNames.js +13 -0
- package/lib/drift/base/constants/accountNames.js.map +1 -0
- package/lib/drift/base/details/index.d.ts +2 -0
- package/lib/drift/base/details/index.js +19 -0
- package/lib/drift/base/details/index.js.map +1 -0
- package/lib/drift/base/details/market/funding.d.ts +9 -0
- package/lib/drift/base/details/market/funding.js +50 -0
- package/lib/drift/base/details/market/funding.js.map +1 -0
- package/lib/drift/base/details/market/index.d.ts +2 -0
- package/lib/drift/base/details/market/index.js +19 -0
- package/lib/drift/base/details/market/index.js.map +1 -0
- package/lib/drift/base/details/market/openInterest.d.ts +5 -0
- package/lib/drift/base/details/market/openInterest.js +12 -0
- package/lib/drift/base/details/market/openInterest.js.map +1 -0
- package/lib/drift/base/details/user/balances.d.ts +40 -0
- package/lib/drift/base/details/user/balances.js +39 -0
- package/lib/drift/base/details/user/balances.js.map +1 -0
- package/lib/drift/base/details/user/index.d.ts +4 -0
- package/lib/drift/base/details/user/index.js +21 -0
- package/lib/drift/base/details/user/index.js.map +1 -0
- package/lib/drift/base/details/user/marginInfo.d.ts +29 -0
- package/lib/drift/base/details/user/marginInfo.js +49 -0
- package/lib/drift/base/details/user/marginInfo.js.map +1 -0
- package/lib/drift/base/details/user/orders.d.ts +3 -0
- package/lib/drift/base/details/user/orders.js +11 -0
- package/lib/drift/base/details/user/orders.js.map +1 -0
- package/lib/drift/base/details/user/positions.d.ts +70 -0
- package/lib/drift/base/details/user/positions.js +146 -0
- package/lib/drift/base/details/user/positions.js.map +1 -0
- package/lib/drift/cli.d.ts +25 -0
- package/lib/drift/cli.js +900 -0
- package/lib/drift/cli.js.map +1 -0
- package/lib/drift/constants/apiUrls.d.ts +27 -0
- package/lib/drift/constants/apiUrls.js +31 -0
- package/lib/drift/constants/apiUrls.js.map +1 -0
- package/lib/drift/example.d.ts +19 -0
- package/lib/drift/example.js +249 -0
- package/lib/drift/example.js.map +1 -0
- package/lib/drift/index.d.ts +3 -0
- package/lib/drift/index.js +20 -0
- package/lib/drift/index.js.map +1 -0
- package/lib/drift/utils/auctionParamsResponseMapper.d.ts +45 -0
- package/lib/drift/utils/auctionParamsResponseMapper.js +148 -0
- package/lib/drift/utils/auctionParamsResponseMapper.js.map +1 -0
- package/lib/drift/utils/funding.d.ts +2 -0
- package/lib/drift/utils/funding.js +9 -0
- package/lib/drift/utils/funding.js.map +1 -0
- package/lib/drift/utils/index.d.ts +3 -0
- package/lib/drift/utils/index.js +23 -0
- package/lib/drift/utils/index.js.map +1 -0
- package/lib/drift/utils/orderParams.d.ts +48 -0
- package/lib/drift/utils/orderParams.js +140 -0
- package/lib/drift/utils/orderParams.js.map +1 -0
- package/lib/index.d.ts +45 -0
- package/lib/index.js +68 -0
- package/lib/index.js.map +1 -0
- package/lib/serializableTypes.d.ts +961 -0
- package/lib/serializableTypes.js +3887 -0
- package/lib/serializableTypes.js.map +1 -0
- package/lib/types/MarketId.d.ts +26 -0
- package/lib/types/MarketId.js +64 -0
- package/lib/types/MarketId.js.map +1 -0
- package/lib/types/Superstake.d.ts +7 -0
- package/lib/types/Superstake.js +3 -0
- package/lib/types/Superstake.js.map +1 -0
- package/lib/types/UIEnv.d.ts +26 -0
- package/lib/types/UIEnv.js +49 -0
- package/lib/types/UIEnv.js.map +1 -0
- package/lib/types/UIMarket.d.ts +94 -0
- package/lib/types/UIMarket.js +224 -0
- package/lib/types/UIMarket.js.map +1 -0
- package/lib/types/candles.d.ts +4 -0
- package/lib/types/candles.js +9 -0
- package/lib/types/candles.js.map +1 -0
- package/lib/types/dataServer.d.ts +53 -0
- package/lib/types/dataServer.js +3 -0
- package/lib/types/dataServer.js.map +1 -0
- package/lib/types/historyServer.d.ts +38 -0
- package/lib/types/historyServer.js +11 -0
- package/lib/types/historyServer.js.map +1 -0
- package/lib/types/index.d.ts +12 -0
- package/lib/types/index.js +29 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/leaderboard.d.ts +80 -0
- package/lib/types/leaderboard.js +11 -0
- package/lib/types/leaderboard.js.map +1 -0
- package/lib/types/remote-configs.d.ts +61 -0
- package/lib/types/remote-configs.js +3 -0
- package/lib/types/remote-configs.js.map +1 -0
- package/lib/types/trade.d.ts +18 -0
- package/lib/types/trade.js +3 -0
- package/lib/types/trade.js.map +1 -0
- package/lib/types/user.d.ts +40 -0
- package/lib/types/user.js +3 -0
- package/lib/types/user.js.map +1 -0
- package/lib/types/utility.d.ts +15 -0
- package/lib/types/utility.js +3 -0
- package/lib/types/utility.js.map +1 -0
- package/lib/utils/CircularBuffers/CircularBuffer.d.ts +22 -0
- package/lib/utils/CircularBuffers/CircularBuffer.js +73 -0
- package/lib/utils/CircularBuffers/CircularBuffer.js.map +1 -0
- package/lib/utils/CircularBuffers/UniqueCircularBuffer.d.ts +19 -0
- package/lib/utils/CircularBuffers/UniqueCircularBuffer.js +78 -0
- package/lib/utils/CircularBuffers/UniqueCircularBuffer.js.map +1 -0
- package/lib/utils/CircularBuffers/index.d.ts +2 -0
- package/lib/utils/CircularBuffers/index.js +19 -0
- package/lib/utils/CircularBuffers/index.js.map +1 -0
- package/lib/utils/MultiplexWebSocket.d.ts +95 -0
- package/lib/utils/MultiplexWebSocket.js +416 -0
- package/lib/utils/MultiplexWebSocket.js.map +1 -0
- package/lib/utils/NumLib.d.ts +106 -0
- package/lib/utils/NumLib.js +300 -0
- package/lib/utils/NumLib.js.map +1 -0
- package/lib/utils/SharedInterval.d.ts +21 -0
- package/lib/utils/SharedInterval.js +47 -0
- package/lib/utils/SharedInterval.js.map +1 -0
- package/lib/utils/SlotBasedResultValidator.d.ts +9 -0
- package/lib/utils/SlotBasedResultValidator.js +27 -0
- package/lib/utils/SlotBasedResultValidator.js.map +1 -0
- package/lib/utils/Stopwatch.d.ts +15 -0
- package/lib/utils/Stopwatch.js +31 -0
- package/lib/utils/Stopwatch.js.map +1 -0
- package/lib/utils/StrictEventEmitter.d.ts +15 -0
- package/lib/utils/StrictEventEmitter.js +55 -0
- package/lib/utils/StrictEventEmitter.js.map +1 -0
- package/lib/utils/WalletConnectionState.d.ts +31 -0
- package/lib/utils/WalletConnectionState.js +83 -0
- package/lib/utils/WalletConnectionState.js.map +1 -0
- package/lib/utils/assert.d.ts +1 -0
- package/lib/utils/assert.js +10 -0
- package/lib/utils/assert.js.map +1 -0
- package/lib/utils/candles/Candle.d.ts +94 -0
- package/lib/utils/candles/Candle.js +580 -0
- package/lib/utils/candles/Candle.js.map +1 -0
- package/lib/utils/candles/types.d.ts +8 -0
- package/lib/utils/candles/types.js +3 -0
- package/lib/utils/candles/types.js.map +1 -0
- package/lib/utils/dlob-server/DlobServerWebsocketUtils.d.ts +57 -0
- package/lib/utils/dlob-server/DlobServerWebsocketUtils.js +137 -0
- package/lib/utils/dlob-server/DlobServerWebsocketUtils.js.map +1 -0
- package/lib/utils/driftEvents.d.ts +10 -0
- package/lib/utils/driftEvents.js +123 -0
- package/lib/utils/driftEvents.js.map +1 -0
- package/lib/utils/equalityChecks.d.ts +12 -0
- package/lib/utils/equalityChecks.js +71 -0
- package/lib/utils/equalityChecks.js.map +1 -0
- package/lib/utils/featureFlags.d.ts +3 -0
- package/lib/utils/featureFlags.js +7 -0
- package/lib/utils/featureFlags.js.map +1 -0
- package/lib/utils/geoblock/index.d.ts +4 -0
- package/lib/utils/geoblock/index.js +20 -0
- package/lib/utils/geoblock/index.js.map +1 -0
- package/lib/utils/index.d.ts +181 -0
- package/lib/utils/index.js +608 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/insuranceFund.d.ts +15 -0
- package/lib/utils/insuranceFund.js +84 -0
- package/lib/utils/insuranceFund.js.map +1 -0
- package/lib/utils/logger.d.ts +5 -0
- package/lib/utils/logger.js +53 -0
- package/lib/utils/logger.js.map +1 -0
- package/lib/utils/math.d.ts +10 -0
- package/lib/utils/math.js +89 -0
- package/lib/utils/math.js.map +1 -0
- package/lib/utils/orderbook/index.d.ts +65 -0
- package/lib/utils/orderbook/index.js +80 -0
- package/lib/utils/orderbook/index.js.map +1 -0
- package/lib/utils/pollingSequenceGuard.d.ts +9 -0
- package/lib/utils/pollingSequenceGuard.js +24 -0
- package/lib/utils/pollingSequenceGuard.js.map +1 -0
- package/lib/utils/priority-fees/PriorityFeeCalculator.d.ts +24 -0
- package/lib/utils/priority-fees/PriorityFeeCalculator.js +53 -0
- package/lib/utils/priority-fees/PriorityFeeCalculator.js.map +1 -0
- package/lib/utils/priority-fees/PriorityFeeStrategies.d.ts +5 -0
- package/lib/utils/priority-fees/PriorityFeeStrategies.js +43 -0
- package/lib/utils/priority-fees/PriorityFeeStrategies.js.map +1 -0
- package/lib/utils/priority-fees/index.d.ts +2 -0
- package/lib/utils/priority-fees/index.js +19 -0
- package/lib/utils/priority-fees/index.js.map +1 -0
- package/lib/utils/priorityFees.d.ts +14 -0
- package/lib/utils/priorityFees.js +68 -0
- package/lib/utils/priorityFees.js.map +1 -0
- package/lib/utils/rpcLatency.d.ts +7 -0
- package/lib/utils/rpcLatency.js +49 -0
- package/lib/utils/rpcLatency.js.map +1 -0
- package/lib/utils/rxjs.d.ts +11 -0
- package/lib/utils/rxjs.js +24 -0
- package/lib/utils/rxjs.js.map +1 -0
- package/lib/utils/s3Buckets.d.ts +43 -0
- package/lib/utils/s3Buckets.js +108 -0
- package/lib/utils/s3Buckets.js.map +1 -0
- package/lib/utils/superstake.d.ts +86 -0
- package/lib/utils/superstake.js +224 -0
- package/lib/utils/superstake.js.map +1 -0
- package/lib/utils/token.d.ts +16 -0
- package/lib/utils/token.js +44 -0
- package/lib/utils/token.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"marginInfo.js","sourceRoot":"","sources":["../../../../../src/drift/base/details/user/marginInfo.ts"],"names":[],"mappings":";;;AAAA,yCAUyB;AACzB,qDAA+D;AAC/D,6CAAwD;AA6BjD,MAAM,oBAAoB,GAAG,CACnC,WAAwB,EACxB,IAAU,EACV,cAA4C,EACxB,EAAE;IACtB,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;IAC1C,MAAM,iBAAiB,GAAG,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,2FAA2F;IAClJ,MAAM,wBAAwB,GAAG,IAAI,CAAC,uBAAuB,EAAE,CAAC;IAChE,MAAM,kBAAkB,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;IACrD,MAAM,sBAAsB,GAAG,IAAI,CAAC,kBAAkB,CAAC,aAAa,CAAC,CAAC;IACtE,MAAM,iBAAiB,GAAG,IAAI,C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,IAAI,CACpC,wBAAwB,EACxB,yBAAmB,CACnB;QACD,iBAAiB,EAAE,YAAM,CAAC,IAAI,CAAC,iBAAiB,EAAE,yBAAmB,CAAC;QACtE,kBAAkB,EAAE,YAAM,CAAC,IAAI,CAAC,kBAAkB,EAAE,yBAAmB,CAAC;QACxE,sBAAsB,EAAE,YAAM,CAAC,IAAI,CAClC,sBAAsB,EACtB,yBAAmB,CACnB;QACD,iBAAiB,EAAE,YAAM,CAAC,IAAI,CAAC,iBAAiB,EAAE,yBAAmB,CAAC;QACtE,qBAAqB,EAAE,YAAM,CAAC,IAAI,CACjC,qBAAqB,EACrB,yBAAmB,CACnB;QACD,QAAQ,EAAE,YAAM,CAAC,IAAI,CAAC,YAAY,EAAE,UAAI,CAAC,CAAC,KAAK,EAAE;QACjD,cAAc,EAAE,YAAM,CAAC,IAAI,CAAC,cAAc,EAAE,yBAAmB,CAAC;QAChE,UAAU,EAAE,YAAM,CAAC,IAAI,CAAC,UAAU,EAAE,yBAAmB,CAAC;QACxD,cAAc,EAAE,YAAM,CAAC,IAAI,CAAC,eAAe,EAAE,SAAG,CAAC,CAAC,KAAK,EAAE;KACzD,CAAC;AACH,CAAC,CAAC;AAjEW,QAAA,oBAAoB,wBAiE/B","sourcesContent":["import {\n\tQUOTE_PRECISION_EXP,\n\tBN,\n\tBigNum,\n\tUser,\n\tZERO,\n\tFOUR,\n\tTWO,\n\tDriftClient,\n\tcalculateClaimablePnl,\n} from '@drift-labs/sdk';\nimport { USDC_SPOT_MARKET_INDEX } from '../../../../constants';\nimport { MarketId, MarketKey } from '../../../../types';\n\nexport type AccountMarginInfo = {\n\t/** Net USD value of the account, including spot balances and unsettled P&L from perp positions. */\n\tnetUsdValue: BigNum;\n\t/** Total unsettled P&L from perp positions. Includes position P&L and funding P&L. */\n\ttotalUnsettledPnl: BigNum;\n\t/** Total unsettled funding P&L from perp positions. */\n\ttotalUnsettledFundingPnL: BigNum;\n\t/** Total claimable P&L from the P&L pool for perp positions. */\n\ttotalClaimablePnl: BigNum;\n\t/** Total initial margin in the account. Includes weighted values of spot balances and unsettled P&L from perp positions. */\n\ttotalInitialMargin: BigNum;\n\t/** Total maintenance margin in the account. Includes weighted values of spot balances and unsettled P&L from perp positions. */\n\ttotalMaintenanceMargin: BigNum;\n\t/** Free initial margin in the account, used to open new positions. */\n\tfreeInitialMargin: BigNum;\n\t/** Free maintenance margin in the account, used to avoid liquidations. */\n\tfreeMaintenanceMargin: BigNum;\n\t/** Current leverage multiplier of the account. */\n\tleverage: number;\n\t/** Initial margin required to open a new position. */\n\tinitialReq: BigNum;\n\t/** Maintenance margin required to avoid liquidations. */\n\tmaintenanceReq: BigNum;\n\t/** Margin ratio of the account. */\n\tmarginRatioPct: number;\n};\n\nexport const getAccountMarginInfo = (\n\tdriftClient: DriftClient,\n\tuser: User,\n\tgetOraclePrice: (marketKey: MarketKey) => BN\n): AccountMarginInfo => {\n\tconst netUsdValue = user.getNetUsdValue();\n\tconst totalUnsettledPnl = user.getUnrealizedPNL(true); // method from SDK is inappropriately named, totalUnsettledPnl is the right definition here\n\tconst totalUnsettledFundingPnL = user.getUnrealizedFundingPNL();\n\tconst totalInitialMargin = user.getTotalCollateral();\n\tconst totalMaintenanceMargin = user.getTotalCollateral('Maintenance');\n\tconst freeInitialMargin = user.getFreeCollateral();\n\tconst freeMaintenanceMargin = user.getFreeCollateral('Maintenance');\n\tconst initialReq = user.getInitialMarginRequirement();\n\tconst maintenanceReq = user.getMaintenanceMarginRequirement();\n\tconst userLeverage = user.getLeverage();\n\n\tlet userMarginRatio = user.getMarginRatio();\n\t// weird to display massive fraction when really it just means that their position is zero\n\tif (userMarginRatio.eq(new BN(Number.MAX_SAFE_INTEGER)))\n\t\tuserMarginRatio = ZERO;\n\n\tconst usdcSpotMarketAccount = driftClient.getSpotMarketAccount(\n\t\tUSDC_SPOT_MARKET_INDEX\n\t);\n\tconst totalClaimablePnl = user\n\t\t.getActivePerpPositions()\n\t\t.reduce((acc, position) => {\n\t\t\tconst perpMarket = driftClient.getPerpMarketAccount(position.marketIndex);\n\t\t\tconst oraclePrice = getOraclePrice(\n\t\t\t\tMarketId.createPerpMarket(position.marketIndex).key\n\t\t\t);\n\t\t\tconst positionClaimablePnl = calculateClaimablePnl(\n\t\t\t\tperpMarket,\n\t\t\t\tusdcSpotMarketAccount,\n\t\t\t\tposition,\n\t\t\t\t{\n\t\t\t\t\tprice: oraclePrice,\n\t\t\t\t}\n\t\t\t);\n\t\t\treturn acc.add(positionClaimablePnl);\n\t\t}, ZERO);\n\n\treturn {\n\t\tnetUsdValue: BigNum.from(netUsdValue, QUOTE_PRECISION_EXP),\n\t\ttotalUnsettledPnl: BigNum.from(totalUnsettledPnl, QUOTE_PRECISION_EXP),\n\t\ttotalUnsettledFundingPnL: BigNum.from(\n\t\t\ttotalUnsettledFundingPnL,\n\t\t\tQUOTE_PRECISION_EXP\n\t\t),\n\t\ttotalClaimablePnl: BigNum.from(totalClaimablePnl, QUOTE_PRECISION_EXP),\n\t\ttotalInitialMargin: BigNum.from(totalInitialMargin, QUOTE_PRECISION_EXP),\n\t\ttotalMaintenanceMargin: BigNum.from(\n\t\t\ttotalMaintenanceMargin,\n\t\t\tQUOTE_PRECISION_EXP\n\t\t),\n\t\tfreeInitialMargin: BigNum.from(freeInitialMargin, QUOTE_PRECISION_EXP),\n\t\tfreeMaintenanceMargin: BigNum.from(\n\t\t\tfreeMaintenanceMargin,\n\t\t\tQUOTE_PRECISION_EXP\n\t\t),\n\t\tleverage: BigNum.from(userLeverage, FOUR).toNum(),\n\t\tmaintenanceReq: BigNum.from(maintenanceReq, QUOTE_PRECISION_EXP),\n\t\tinitialReq: BigNum.from(initialReq, QUOTE_PRECISION_EXP),\n\t\tmarginRatioPct: BigNum.from(userMarginRatio, TWO).toNum(),\n\t};\n};\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOrderDetails = void 0;
|
|
4
|
+
const serializableTypes_1 = require("../../../../serializableTypes");
|
|
5
|
+
const getOrderDetails = (order) => {
|
|
6
|
+
const serializedOrder = serializableTypes_1.Serializer.Serialize.Order(order);
|
|
7
|
+
const deserializedOrder = serializableTypes_1.Serializer.Deserialize.UIOrder(serializedOrder);
|
|
8
|
+
return deserializedOrder;
|
|
9
|
+
};
|
|
10
|
+
exports.getOrderDetails = getOrderDetails;
|
|
11
|
+
//# sourceMappingURL=orders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orders.js","sourceRoot":"","sources":["../../../../../src/drift/base/details/user/orders.ts"],"names":[],"mappings":";;;AACA,qEAAgF;AAEzE,MAAM,eAAe,GAAG,CAAC,KAAY,EAAuB,EAAE;IACpE,MAAM,eAAe,GAAG,8BAAU,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC1D,MAAM,iBAAiB,GAAG,8BAAU,CAAC,WAAW,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC;IAE1E,OAAO,iBAAiB,CAAC;AAC1B,CAAC,CAAC;AALW,QAAA,eAAe,mBAK1B","sourcesContent":["import { Order } from '@drift-labs/sdk';\nimport { Serializer, UISerializableOrder } from '../../../../serializableTypes';\n\nexport const getOrderDetails = (order: Order): UISerializableOrder => {\n\tconst serializedOrder = Serializer.Serialize.Order(order);\n\tconst deserializedOrder = Serializer.Deserialize.UIOrder(serializedOrder);\n\n\treturn deserializedOrder;\n};\n"]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { BN, BigNum, DriftClient, PerpPosition, PositionDirection, User } from '@drift-labs/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Comprehensive position information derived from a PerpPosition at a specific reference price.
|
|
4
|
+
* This interface contains all the key metrics needed for position display and analysis.
|
|
5
|
+
*/
|
|
6
|
+
export interface PriceBasedPositionInfo {
|
|
7
|
+
/**
|
|
8
|
+
* Position PnL in notional terms based on the reference price.
|
|
9
|
+
* This does NOT include funding PnL - only the price-based profit/loss.
|
|
10
|
+
*/
|
|
11
|
+
positionNotionalPnl: BigNum;
|
|
12
|
+
/**
|
|
13
|
+
* Position PnL as a percentage.
|
|
14
|
+
* For prediction markets: calculated based on price movement relative to entry.
|
|
15
|
+
* For regular markets: calculated as (PnL / quote entry amount) * account leverage.
|
|
16
|
+
*/
|
|
17
|
+
positionPnlPercentage: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Derives the user-understandable position display information from a User's PerpPosition and the reference price.
|
|
21
|
+
* The reference price can be the mark price or the oracle price.
|
|
22
|
+
*
|
|
23
|
+
* @param driftClient - The DriftClient instance.
|
|
24
|
+
* @param user - The User instance.
|
|
25
|
+
* @param perpPosition - The PerpPosition instance.
|
|
26
|
+
* @param referencePrice - The reference price. This can be the mark price or the oracle price.
|
|
27
|
+
* @param accountLeverage - The account leverage.
|
|
28
|
+
*
|
|
29
|
+
* @returns The PositionDisplayInfo object. Note that position pnl does not include funding pnl.
|
|
30
|
+
*/
|
|
31
|
+
export declare const getPriceBasedPositionInfo: (driftClient: DriftClient, perpPosition: PerpPosition, referencePrice: BN, accountLeverage?: number) => PriceBasedPositionInfo;
|
|
32
|
+
export type PerpPositionInfo = {
|
|
33
|
+
marketIndex: number;
|
|
34
|
+
/** Absolute base size of the position. */
|
|
35
|
+
baseSize: BigNum;
|
|
36
|
+
/** Notional value of the position based on oracle price */
|
|
37
|
+
notionalSize: BigNum;
|
|
38
|
+
/** Direction of the position */
|
|
39
|
+
direction: PositionDirection;
|
|
40
|
+
/** Position entry price.*/
|
|
41
|
+
entryPrice: BigNum;
|
|
42
|
+
/**
|
|
43
|
+
* Price at which the position would be liquidated.
|
|
44
|
+
* Returns zero if liquidation price cannot be calculated.
|
|
45
|
+
*/
|
|
46
|
+
liquidationPrice: BigNum;
|
|
47
|
+
/**
|
|
48
|
+
* Cumulative funding and fees PnL for this position.
|
|
49
|
+
* This is separate from positionNotionalPnl to allow granular PnL breakdown.
|
|
50
|
+
*/
|
|
51
|
+
feesAndFundingPnl: BigNum;
|
|
52
|
+
/** Position P&L information based on the oracle price and the mark price as reference prices. This does not include fees and funding PnL. */
|
|
53
|
+
positionPnl: {
|
|
54
|
+
oracleBased: PriceBasedPositionInfo;
|
|
55
|
+
markBased: PriceBasedPositionInfo;
|
|
56
|
+
};
|
|
57
|
+
/** The quote amount that was used to enter the position. */
|
|
58
|
+
costBasis: BigNum;
|
|
59
|
+
/** The quote amount that the position needs to be at, to breakeven. This is net of fees and funding, hence why it is different from quoteEntryAmount. */
|
|
60
|
+
quoteBreakEvenAmount: BigNum;
|
|
61
|
+
/** This is the total of unsettled pnl and unsettled funding. */
|
|
62
|
+
totalUnsettledPnl: BigNum;
|
|
63
|
+
/** This is the total of unsettled pnl and unsettled funding that is claimable from the P&L pool. */
|
|
64
|
+
totalClaimablePnl: BigNum;
|
|
65
|
+
/** This is the unsettled funding pnl. */
|
|
66
|
+
unsettledFundingPnl: BigNum;
|
|
67
|
+
/** This is the total of settled pnl and settled funding. */
|
|
68
|
+
totalSettledPnl: BigNum;
|
|
69
|
+
};
|
|
70
|
+
export declare const getPositionInfo: (driftClient: DriftClient, user: User, perpPosition: PerpPosition, oraclePrice: BN, markPrice: BN) => PerpPositionInfo;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPositionInfo = exports.getPriceBasedPositionInfo = void 0;
|
|
4
|
+
const sdk_1 = require("@drift-labs/sdk");
|
|
5
|
+
const trading_1 = require("../../../../common-ui-utils/trading");
|
|
6
|
+
const utils_1 = require("../../../../utils");
|
|
7
|
+
const constants_1 = require("../../../../constants");
|
|
8
|
+
/**
|
|
9
|
+
* Derives the user-understandable position display information from a User's PerpPosition and the reference price.
|
|
10
|
+
* The reference price can be the mark price or the oracle price.
|
|
11
|
+
*
|
|
12
|
+
* @param driftClient - The DriftClient instance.
|
|
13
|
+
* @param user - The User instance.
|
|
14
|
+
* @param perpPosition - The PerpPosition instance.
|
|
15
|
+
* @param referencePrice - The reference price. This can be the mark price or the oracle price.
|
|
16
|
+
* @param accountLeverage - The account leverage.
|
|
17
|
+
*
|
|
18
|
+
* @returns The PositionDisplayInfo object. Note that position pnl does not include funding pnl.
|
|
19
|
+
*/
|
|
20
|
+
const getPriceBasedPositionInfo = (driftClient, perpPosition, referencePrice, accountLeverage) => {
|
|
21
|
+
const { marketIndex } = perpPosition;
|
|
22
|
+
const perpMarket = driftClient.getPerpMarketAccount(marketIndex);
|
|
23
|
+
const referencePriceBigNum = sdk_1.BigNum.from(referencePrice, sdk_1.PRICE_PRECISION_EXP);
|
|
24
|
+
// Base Size
|
|
25
|
+
const baseSize = perpPosition.baseAssetAmount;
|
|
26
|
+
const baseSizeBigNum = sdk_1.BigNum.from(baseSize.abs(), sdk_1.BASE_PRECISION_EXP);
|
|
27
|
+
// Entry Price
|
|
28
|
+
const entryPriceBN = (0, sdk_1.calculateEntryPrice)(perpPosition);
|
|
29
|
+
const entryPriceBigNum = sdk_1.BigNum.from(entryPriceBN, sdk_1.PRICE_PRECISION_EXP);
|
|
30
|
+
const isShortPosition = baseSize.isNeg();
|
|
31
|
+
// Market and prediction market detection
|
|
32
|
+
const isPredictionMarket = utils_1.ENUM_UTILS.match(perpMarket.contractType, sdk_1.ContractType.PREDICTION);
|
|
33
|
+
// Price calculations
|
|
34
|
+
// Handle prediction market resolution prices
|
|
35
|
+
if (isPredictionMarket) {
|
|
36
|
+
const isResolved = utils_1.ENUM_UTILS.match(perpMarket === null || perpMarket === void 0 ? void 0 : perpMarket.status, sdk_1.MarketStatus.SETTLEMENT) ||
|
|
37
|
+
utils_1.ENUM_UTILS.match(perpMarket === null || perpMarket === void 0 ? void 0 : perpMarket.status, sdk_1.MarketStatus.DELISTED);
|
|
38
|
+
if (isResolved) {
|
|
39
|
+
const resolvedToNo = perpMarket.expiryPrice.lte(sdk_1.ZERO.add(perpMarket.amm.orderTickSize));
|
|
40
|
+
const price = resolvedToNo
|
|
41
|
+
? sdk_1.ZERO.mul(sdk_1.PRICE_PRECISION)
|
|
42
|
+
: sdk_1.ONE.mul(sdk_1.PRICE_PRECISION);
|
|
43
|
+
referencePrice = price;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// position pnl
|
|
47
|
+
const positionNotionalPnlBN = trading_1.TRADING_UTILS.calculatePotentialProfit({
|
|
48
|
+
currentPositionSize: baseSizeBigNum,
|
|
49
|
+
currentPositionDirection: isShortPosition
|
|
50
|
+
? sdk_1.PositionDirection.SHORT
|
|
51
|
+
: sdk_1.PositionDirection.LONG,
|
|
52
|
+
currentPositionEntryPrice: sdk_1.BigNum.from(entryPriceBN, sdk_1.PRICE_PRECISION_EXP),
|
|
53
|
+
tradeDirection: isShortPosition
|
|
54
|
+
? sdk_1.PositionDirection.LONG
|
|
55
|
+
: sdk_1.PositionDirection.SHORT,
|
|
56
|
+
exitBaseSize: baseSizeBigNum,
|
|
57
|
+
exitPrice: referencePriceBigNum,
|
|
58
|
+
takerFeeBps: 0,
|
|
59
|
+
}).estimatedProfit.shiftTo(sdk_1.QUOTE_PRECISION_EXP).val;
|
|
60
|
+
const positionNotionalPnlBigNum = sdk_1.BigNum.from(positionNotionalPnlBN, sdk_1.QUOTE_PRECISION_EXP);
|
|
61
|
+
// Determine if this is a sell-side prediction market for price adjustment
|
|
62
|
+
const isSellPredictionMarket = isPredictionMarket && isShortPosition;
|
|
63
|
+
// Apply prediction market price adjustment for display
|
|
64
|
+
const markPriceToUseForPredictionMarket = isSellPredictionMarket
|
|
65
|
+
? constants_1.MAX_PREDICTION_PRICE_BIG_NUM.sub(referencePriceBigNum).val
|
|
66
|
+
: referencePrice;
|
|
67
|
+
const positionPnlPercentage = isPredictionMarket
|
|
68
|
+
? sdk_1.BigNum.from(markPriceToUseForPredictionMarket, sdk_1.PRICE_PRECISION_EXP)
|
|
69
|
+
.sub(entryPriceBigNum)
|
|
70
|
+
.mul(sdk_1.PRICE_PRECISION)
|
|
71
|
+
.div(entryPriceBN)
|
|
72
|
+
.toNum() *
|
|
73
|
+
100 *
|
|
74
|
+
(isShortPosition ? -1 : 1)
|
|
75
|
+
: accountLeverage
|
|
76
|
+
? (() => {
|
|
77
|
+
const quoteAmount = perpPosition.quoteEntryAmount.abs();
|
|
78
|
+
if (quoteAmount.eq(sdk_1.ZERO))
|
|
79
|
+
return 0;
|
|
80
|
+
const pnlPct = positionNotionalPnlBigNum.val
|
|
81
|
+
.mul(new sdk_1.BN(10000))
|
|
82
|
+
.div(quoteAmount)
|
|
83
|
+
.toNumber() / 1000000;
|
|
84
|
+
return pnlPct * (accountLeverage || 1);
|
|
85
|
+
})()
|
|
86
|
+
: 0;
|
|
87
|
+
return {
|
|
88
|
+
positionNotionalPnl: positionNotionalPnlBigNum,
|
|
89
|
+
positionPnlPercentage,
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
exports.getPriceBasedPositionInfo = getPriceBasedPositionInfo;
|
|
93
|
+
const getPositionInfo = (driftClient, user, perpPosition, oraclePrice, markPrice) => {
|
|
94
|
+
const { marketIndex } = perpPosition;
|
|
95
|
+
const baseSize = perpPosition.baseAssetAmount;
|
|
96
|
+
const baseSizeBigNum = sdk_1.BigNum.from(baseSize.abs(), sdk_1.BASE_PRECISION_EXP);
|
|
97
|
+
const notionalSizeBigNum = sdk_1.BigNum.from(user
|
|
98
|
+
.getPerpPositionValue(perpPosition.marketIndex, {
|
|
99
|
+
price: oraclePrice,
|
|
100
|
+
})
|
|
101
|
+
.abs(), sdk_1.QUOTE_PRECISION_EXP);
|
|
102
|
+
const positionDirection = baseSize.isNeg()
|
|
103
|
+
? sdk_1.PositionDirection.SHORT
|
|
104
|
+
: sdk_1.PositionDirection.LONG;
|
|
105
|
+
const entryPrice = (0, sdk_1.calculateEntryPrice)(perpPosition);
|
|
106
|
+
const entryPriceBigNum = sdk_1.BigNum.from(entryPrice, sdk_1.PRICE_PRECISION_EXP);
|
|
107
|
+
const liqPrice = user.liquidationPrice(marketIndex);
|
|
108
|
+
const liqPriceBigNum = sdk_1.BigNum.from(liqPrice, sdk_1.PRICE_PRECISION_EXP);
|
|
109
|
+
const perpMarket = driftClient.getPerpMarketAccount(marketIndex);
|
|
110
|
+
const feesAndFundingPnlBigNum = sdk_1.BigNum.from((0, sdk_1.calculateFeesAndFundingPnl)(perpMarket, perpPosition), sdk_1.QUOTE_PRECISION_EXP);
|
|
111
|
+
const accountLeverage = user.getLeverage().toNumber();
|
|
112
|
+
const oracleBasedPositionInfo = (0, exports.getPriceBasedPositionInfo)(driftClient, perpPosition, oraclePrice, accountLeverage);
|
|
113
|
+
const markBasedPositionInfo = (0, exports.getPriceBasedPositionInfo)(driftClient, perpPosition, markPrice, accountLeverage);
|
|
114
|
+
const quoteEntryAmountBigNum = sdk_1.BigNum.from(perpPosition.quoteEntryAmount, sdk_1.QUOTE_PRECISION_EXP);
|
|
115
|
+
const quoteBreakEvenAmountBigNum = sdk_1.BigNum.from(perpPosition.quoteBreakEvenAmount, sdk_1.QUOTE_PRECISION_EXP);
|
|
116
|
+
const totalUnrealizedPnlBigNum = sdk_1.BigNum.from((0, sdk_1.calculatePositionPNL)(perpMarket, perpPosition, true, {
|
|
117
|
+
price: oraclePrice,
|
|
118
|
+
}), sdk_1.QUOTE_PRECISION_EXP);
|
|
119
|
+
const usdcSpotMarketAccount = driftClient.getSpotMarketAccount(constants_1.USDC_SPOT_MARKET_INDEX);
|
|
120
|
+
const totalClaimablePnlBigNum = sdk_1.BigNum.from((0, sdk_1.calculateClaimablePnl)(perpMarket, usdcSpotMarketAccount, perpPosition, {
|
|
121
|
+
price: oraclePrice,
|
|
122
|
+
}), sdk_1.QUOTE_PRECISION_EXP);
|
|
123
|
+
const unsettledFundingPnlBigNum = sdk_1.BigNum.from((0, sdk_1.calculateUnsettledFundingPnl)(perpMarket, perpPosition), sdk_1.QUOTE_PRECISION_EXP);
|
|
124
|
+
const totalSettledPnlBigNum = sdk_1.BigNum.from(perpPosition.settledPnl, sdk_1.QUOTE_PRECISION_EXP);
|
|
125
|
+
return {
|
|
126
|
+
marketIndex,
|
|
127
|
+
baseSize: baseSizeBigNum,
|
|
128
|
+
notionalSize: notionalSizeBigNum,
|
|
129
|
+
direction: positionDirection,
|
|
130
|
+
entryPrice: entryPriceBigNum,
|
|
131
|
+
liquidationPrice: liqPriceBigNum,
|
|
132
|
+
feesAndFundingPnl: feesAndFundingPnlBigNum,
|
|
133
|
+
positionPnl: {
|
|
134
|
+
oracleBased: oracleBasedPositionInfo,
|
|
135
|
+
markBased: markBasedPositionInfo,
|
|
136
|
+
},
|
|
137
|
+
costBasis: quoteEntryAmountBigNum,
|
|
138
|
+
quoteBreakEvenAmount: quoteBreakEvenAmountBigNum,
|
|
139
|
+
unsettledFundingPnl: unsettledFundingPnlBigNum,
|
|
140
|
+
totalUnsettledPnl: totalUnrealizedPnlBigNum,
|
|
141
|
+
totalClaimablePnl: totalClaimablePnlBigNum,
|
|
142
|
+
totalSettledPnl: totalSettledPnlBigNum,
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
exports.getPositionInfo = getPositionInfo;
|
|
146
|
+
//# sourceMappingURL=positions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positions.js","sourceRoot":"","sources":["../../../../../src/drift/base/details/user/positions.ts"],"names":[],"mappings":";;;AAAA,yCAoByB;AACzB,iEAAoE;AACpE,6CAA+C;AAC/C,qDAG+B;AAqB/B;;;;;;;;;;;GAWG;AACI,MAAM,yBAAyB,GAAG,CACxC,WAAwB,EACxB,YAA0B,EAC1B,cAAkB,EAClB,eAAwB,EACC,EAAE;IAC3B,MAAM,EAAE,WAAW,EAAE,GAAG,YAAY,CAAC;IACrC,MAAM,UAAU,GAAG,WAAW,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC;IACjE,MAAM,oBAAoB,GAAG,YAAM,CAAC,IAAI,CAAC,cAAc,EAAE,yBAAmB,CAAC,C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AC,IAAI,CAC5C,IAAA,kCAA4B,EAAC,UAAU,EAAE,YAAY,CAAC,EACtD,yBAAmB,CACnB,CAAC;IACF,MAAM,qBAAqB,GAAG,YAAM,CAAC,IAAI,CACxC,YAAY,CAAC,UAAU,EACvB,yBAAmB,CACnB,CAAC;IAEF,OAAO;QACN,WAAW;QACX,QAAQ,EAAE,cAAc;QACxB,YAAY,EAAE,kBAAkB;QAChC,SAAS,EAAE,iBAAiB;QAC5B,UAAU,EAAE,gBAAgB;QAC5B,gBAAgB,EAAE,cAAc;QAChC,iBAAiB,EAAE,uBAAuB;QAC1C,WAAW,EAAE;YACZ,WAAW,EAAE,uBAAuB;YACpC,SAAS,EAAE,qBAAqB;SAChC;QACD,SAAS,EAAE,sBAAsB;QACjC,oBAAoB,EAAE,0BAA0B;QAChD,mBAAmB,EAAE,yBAAyB;QAC9C,iBAAiB,EAAE,wBAAwB;QAC3C,iBAAiB,EAAE,uBAAuB;QAC1C,eAAe,EAAE,qBAAqB;KACtC,CAAC;AACH,CAAC,CAAC;AAvGW,QAAA,eAAe,mBAuG1B","sourcesContent":["import {\n\tBASE_PRECISION_EXP,\n\tBN,\n\tBigNum,\n\tContractType,\n\tDriftClient,\n\tMarketStatus,\n\tONE,\n\tPRICE_PRECISION,\n\tPRICE_PRECISION_EXP,\n\tPerpPosition,\n\tPositionDirection,\n\tQUOTE_PRECISION_EXP,\n\tUser,\n\tZERO,\n\tcalculateClaimablePnl,\n\tcalculateEntryPrice,\n\tcalculateFeesAndFundingPnl,\n\tcalculatePositionPNL,\n\tcalculateUnsettledFundingPnl,\n} from '@drift-labs/sdk';\nimport { TRADING_UTILS } from '../../../../common-ui-utils/trading';\nimport { ENUM_UTILS } from '../../../../utils';\nimport {\n\tMAX_PREDICTION_PRICE_BIG_NUM,\n\tUSDC_SPOT_MARKET_INDEX,\n} from '../../../../constants';\n\n/**\n * Comprehensive position information derived from a PerpPosition at a specific reference price.\n * This interface contains all the key metrics needed for position display and analysis.\n */\nexport interface PriceBasedPositionInfo {\n\t/**\n\t * Position PnL in notional terms based on the reference price.\n\t * This does NOT include funding PnL - only the price-based profit/loss.\n\t */\n\tpositionNotionalPnl: BigNum;\n\n\t/**\n\t * Position PnL as a percentage.\n\t * For prediction markets: calculated based on price movement relative to entry.\n\t * For regular markets: calculated as (PnL / quote entry amount) * account leverage.\n\t */\n\tpositionPnlPercentage: number;\n}\n\n/**\n * Derives the user-understandable position display information from a User's PerpPosition and the reference price.\n * The reference price can be the mark price or the oracle price.\n *\n * @param driftClient - The DriftClient instance.\n * @param user - The User instance.\n * @param perpPosition - The PerpPosition instance.\n * @param referencePrice - The reference price. This can be the mark price or the oracle price.\n * @param accountLeverage - The account leverage.\n *\n * @returns The PositionDisplayInfo object. Note that position pnl does not include funding pnl.\n */\nexport const getPriceBasedPositionInfo = (\n\tdriftClient: DriftClient,\n\tperpPosition: PerpPosition,\n\treferencePrice: BN,\n\taccountLeverage?: number\n): PriceBasedPositionInfo => {\n\tconst { marketIndex } = perpPosition;\n\tconst perpMarket = driftClient.getPerpMarketAccount(marketIndex);\n\tconst referencePriceBigNum = BigNum.from(referencePrice, PRICE_PRECISION_EXP);\n\n\t// Base Size\n\tconst baseSize = perpPosition.baseAssetAmount;\n\tconst baseSizeBigNum = BigNum.from(baseSize.abs(), BASE_PRECISION_EXP);\n\n\t// Entry Price\n\tconst entryPriceBN = calculateEntryPrice(perpPosition);\n\tconst entryPriceBigNum = BigNum.from(entryPriceBN, PRICE_PRECISION_EXP);\n\n\tconst isShortPosition = baseSize.isNeg();\n\n\t// Market and prediction market detection\n\tconst isPredictionMarket = ENUM_UTILS.match(\n\t\tperpMarket.contractType,\n\t\tContractType.PREDICTION\n\t);\n\n\t// Price calculations\n\t// Handle prediction market resolution prices\n\tif (isPredictionMarket) {\n\t\tconst isResolved =\n\t\t\tENUM_UTILS.match(perpMarket?.status, MarketStatus.SETTLEMENT) ||\n\t\t\tENUM_UTILS.match(perpMarket?.status, MarketStatus.DELISTED);\n\n\t\tif (isResolved) {\n\t\t\tconst resolvedToNo = perpMarket.expiryPrice.lte(\n\t\t\t\tZERO.add(perpMarket.amm.orderTickSize)\n\t\t\t);\n\n\t\t\tconst price = resolvedToNo\n\t\t\t\t? ZERO.mul(PRICE_PRECISION)\n\t\t\t\t: ONE.mul(PRICE_PRECISION);\n\n\t\t\treferencePrice = price;\n\t\t}\n\t}\n\n\t// position pnl\n\tconst positionNotionalPnlBN = TRADING_UTILS.calculatePotentialProfit({\n\t\tcurrentPositionSize: baseSizeBigNum,\n\t\tcurrentPositionDirection: isShortPosition\n\t\t\t? PositionDirection.SHORT\n\t\t\t: PositionDirection.LONG,\n\t\tcurrentPositionEntryPrice: BigNum.from(entryPriceBN, PRICE_PRECISION_EXP),\n\t\ttradeDirection: isShortPosition\n\t\t\t? PositionDirection.LONG\n\t\t\t: PositionDirection.SHORT,\n\t\texitBaseSize: baseSizeBigNum,\n\t\texitPrice: referencePriceBigNum,\n\t\ttakerFeeBps: 0,\n\t}).estimatedProfit.shiftTo(QUOTE_PRECISION_EXP).val;\n\tconst positionNotionalPnlBigNum = BigNum.from(\n\t\tpositionNotionalPnlBN,\n\t\tQUOTE_PRECISION_EXP\n\t);\n\n\t// Determine if this is a sell-side prediction market for price adjustment\n\tconst isSellPredictionMarket = isPredictionMarket && isShortPosition;\n\n\t// Apply prediction market price adjustment for display\n\tconst markPriceToUseForPredictionMarket = isSellPredictionMarket\n\t\t? MAX_PREDICTION_PRICE_BIG_NUM.sub(referencePriceBigNum).val\n\t\t: referencePrice;\n\n\tconst positionPnlPercentage = isPredictionMarket\n\t\t? BigNum.from(markPriceToUseForPredictionMarket, PRICE_PRECISION_EXP)\n\t\t\t\t.sub(entryPriceBigNum)\n\t\t\t\t.mul(PRICE_PRECISION)\n\t\t\t\t.div(entryPriceBN)\n\t\t\t\t.toNum() *\n\t\t 100 *\n\t\t (isShortPosition ? -1 : 1)\n\t\t: accountLeverage\n\t\t? (() => {\n\t\t\t\tconst quoteAmount = perpPosition.quoteEntryAmount.abs();\n\n\t\t\t\tif (quoteAmount.eq(ZERO)) return 0;\n\n\t\t\t\tconst pnlPct =\n\t\t\t\t\tpositionNotionalPnlBigNum.val\n\t\t\t\t\t\t.mul(new BN(10_000))\n\t\t\t\t\t\t.div(quoteAmount)\n\t\t\t\t\t\t.toNumber() / 1_000_000;\n\t\t\t\treturn pnlPct * (accountLeverage || 1);\n\t\t })()\n\t\t: 0;\n\n\treturn {\n\t\tpositionNotionalPnl: positionNotionalPnlBigNum,\n\t\tpositionPnlPercentage,\n\t};\n};\n\nexport type PerpPositionInfo = {\n\tmarketIndex: number;\n\t/** Absolute base size of the position. */\n\tbaseSize: BigNum;\n\t/** Notional value of the position based on oracle price */\n\tnotionalSize: BigNum;\n\t/** Direction of the position */\n\tdirection: PositionDirection;\n\t/** Position entry price.*/\n\tentryPrice: BigNum;\n\t/**\n\t * Price at which the position would be liquidated.\n\t * Returns zero if liquidation price cannot be calculated.\n\t */\n\tliquidationPrice: BigNum;\n\t/**\n\t * Cumulative funding and fees PnL for this position.\n\t * This is separate from positionNotionalPnl to allow granular PnL breakdown.\n\t */\n\tfeesAndFundingPnl: BigNum;\n\t/** Position P&L information based on the oracle price and the mark price as reference prices. This does not include fees and funding PnL. */\n\tpositionPnl: {\n\t\toracleBased: PriceBasedPositionInfo;\n\t\tmarkBased: PriceBasedPositionInfo;\n\t};\n\t/** The quote amount that was used to enter the position. */\n\tcostBasis: BigNum;\n\t/** The quote amount that the position needs to be at, to breakeven. This is net of fees and funding, hence why it is different from quoteEntryAmount. */\n\tquoteBreakEvenAmount: BigNum;\n\t/** This is the total of unsettled pnl and unsettled funding. */\n\ttotalUnsettledPnl: BigNum;\n\t/** This is the total of unsettled pnl and unsettled funding that is claimable from the P&L pool. */\n\ttotalClaimablePnl: BigNum;\n\t/** This is the unsettled funding pnl. */\n\tunsettledFundingPnl: BigNum;\n\t/** This is the total of settled pnl and settled funding. */\n\ttotalSettledPnl: BigNum;\n};\n\nexport const getPositionInfo = (\n\tdriftClient: DriftClient,\n\tuser: User,\n\tperpPosition: PerpPosition,\n\toraclePrice: BN,\n\tmarkPrice: BN\n): PerpPositionInfo => {\n\tconst { marketIndex } = perpPosition;\n\n\tconst baseSize = perpPosition.baseAssetAmount;\n\tconst baseSizeBigNum = BigNum.from(baseSize.abs(), BASE_PRECISION_EXP);\n\n\tconst notionalSizeBigNum = BigNum.from(\n\t\tuser\n\t\t\t.getPerpPositionValue(perpPosition.marketIndex, {\n\t\t\t\tprice: oraclePrice,\n\t\t\t})\n\t\t\t.abs(),\n\t\tQUOTE_PRECISION_EXP\n\t);\n\n\tconst positionDirection = baseSize.isNeg()\n\t\t? PositionDirection.SHORT\n\t\t: PositionDirection.LONG;\n\n\tconst entryPrice = calculateEntryPrice(perpPosition);\n\tconst entryPriceBigNum = BigNum.from(entryPrice, PRICE_PRECISION_EXP);\n\n\tconst liqPrice = user.liquidationPrice(marketIndex);\n\tconst liqPriceBigNum = BigNum.from(liqPrice, PRICE_PRECISION_EXP);\n\n\tconst perpMarket = driftClient.getPerpMarketAccount(marketIndex);\n\tconst feesAndFundingPnlBigNum = BigNum.from(\n\t\tcalculateFeesAndFundingPnl(perpMarket, perpPosition),\n\t\tQUOTE_PRECISION_EXP\n\t);\n\n\tconst accountLeverage = user.getLeverage().toNumber();\n\tconst oracleBasedPositionInfo = getPriceBasedPositionInfo(\n\t\tdriftClient,\n\t\tperpPosition,\n\t\toraclePrice,\n\t\taccountLeverage\n\t);\n\tconst markBasedPositionInfo = getPriceBasedPositionInfo(\n\t\tdriftClient,\n\t\tperpPosition,\n\t\tmarkPrice,\n\t\taccountLeverage\n\t);\n\n\tconst quoteEntryAmountBigNum = BigNum.from(\n\t\tperpPosition.quoteEntryAmount,\n\t\tQUOTE_PRECISION_EXP\n\t);\n\tconst quoteBreakEvenAmountBigNum = BigNum.from(\n\t\tperpPosition.quoteBreakEvenAmount,\n\t\tQUOTE_PRECISION_EXP\n\t);\n\n\tconst totalUnrealizedPnlBigNum = BigNum.from(\n\t\tcalculatePositionPNL(perpMarket, perpPosition, true, {\n\t\t\tprice: oraclePrice,\n\t\t}),\n\t\tQUOTE_PRECISION_EXP\n\t);\n\tconst usdcSpotMarketAccount = driftClient.getSpotMarketAccount(\n\t\tUSDC_SPOT_MARKET_INDEX\n\t);\n\tconst totalClaimablePnlBigNum = BigNum.from(\n\t\tcalculateClaimablePnl(perpMarket, usdcSpotMarketAccount, perpPosition, {\n\t\t\tprice: oraclePrice,\n\t\t}),\n\t\tQUOTE_PRECISION_EXP\n\t);\n\tconst unsettledFundingPnlBigNum = BigNum.from(\n\t\tcalculateUnsettledFundingPnl(perpMarket, perpPosition),\n\t\tQUOTE_PRECISION_EXP\n\t);\n\tconst totalSettledPnlBigNum = BigNum.from(\n\t\tperpPosition.settledPnl,\n\t\tQUOTE_PRECISION_EXP\n\t);\n\n\treturn {\n\t\tmarketIndex,\n\t\tbaseSize: baseSizeBigNum,\n\t\tnotionalSize: notionalSizeBigNum,\n\t\tdirection: positionDirection,\n\t\tentryPrice: entryPriceBigNum,\n\t\tliquidationPrice: liqPriceBigNum,\n\t\tfeesAndFundingPnl: feesAndFundingPnlBigNum,\n\t\tpositionPnl: {\n\t\t\toracleBased: oracleBasedPositionInfo,\n\t\t\tmarkBased: markBasedPositionInfo,\n\t\t},\n\t\tcostBasis: quoteEntryAmountBigNum,\n\t\tquoteBreakEvenAmount: quoteBreakEvenAmountBigNum,\n\t\tunsettledFundingPnl: unsettledFundingPnlBigNum,\n\t\ttotalUnsettledPnl: totalUnrealizedPnlBigNum,\n\t\ttotalClaimablePnl: totalClaimablePnlBigNum,\n\t\ttotalSettledPnl: totalSettledPnlBigNum,\n\t};\n};\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { BN, PositionDirection, SwapMode } from '@drift-labs/sdk';
|
|
2
|
+
interface CliArgs {
|
|
3
|
+
[key: string]: string | string[];
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Parse command line arguments into a key-value object
|
|
7
|
+
*/
|
|
8
|
+
declare function parseArgs(args: string[]): CliArgs;
|
|
9
|
+
/**
|
|
10
|
+
* Convert human-readable amount to BN with proper precision
|
|
11
|
+
*/
|
|
12
|
+
declare function parseAmount(amount: string, precision?: BN): BN;
|
|
13
|
+
/**
|
|
14
|
+
* Parse direction string to PositionDirection
|
|
15
|
+
*/
|
|
16
|
+
declare function parseDirection(direction: string): PositionDirection;
|
|
17
|
+
/**
|
|
18
|
+
* Parse swap mode string to SwapMode
|
|
19
|
+
*/
|
|
20
|
+
declare function parseSwapMode(swapMode: string): SwapMode;
|
|
21
|
+
/**
|
|
22
|
+
* Main CLI entry point
|
|
23
|
+
*/
|
|
24
|
+
declare function main(): Promise<void>;
|
|
25
|
+
export { main, parseArgs, parseAmount, parseDirection, parseSwapMode };
|