@drift-labs/sdk 2.14.0 → 2.15.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.
@@ -76,6 +76,16 @@ exports.MainnetPerpMarkets = [
76
76
  launchTs: 1670347281000,
77
77
  oracleSource: __1.OracleSource.PYTH,
78
78
  },
79
+ {
80
+ fullName: 'Aptos',
81
+ category: ['L1', 'Infra'],
82
+ symbol: 'APT-PERP',
83
+ baseAssetSymbol: 'APT',
84
+ marketIndex: 3,
85
+ oracle: new web3_js_1.PublicKey('FNNvb1AFDnDVPkocEri8mWbJ1952HQZtFLuwPiUjSJQ'),
86
+ launchTs: 1675802661000,
87
+ oracleSource: __1.OracleSource.PYTH,
88
+ },
79
89
  ];
80
90
  exports.PerpMarkets = {
81
91
  devnet: exports.DevnetPerpMarkets,
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.14.0",
2
+ "version": "2.15.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -30,6 +30,7 @@ export declare function calculateOracleSpread(price: BN, oraclePriceData: Oracle
30
30
  export declare function calculateMarketMarginRatio(market: PerpMarketAccount, size: BN, marginCategory: MarginCategory): number;
31
31
  export declare function calculateUnrealizedAssetWeight(market: PerpMarketAccount, quoteSpotMarket: SpotMarketAccount, unrealizedPnl: BN, marginCategory: MarginCategory, oraclePriceData: OraclePriceData): BN;
32
32
  export declare function calculateMarketAvailablePNL(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount): BN;
33
+ export declare function calculateMarketMaxAvailableInsurance(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount): BN;
33
34
  export declare function calculateNetUserPnl(perpMarket: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
34
35
  export declare function calculateNetUserPnlImbalance(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount, oraclePriceData: OraclePriceData): BN;
35
36
  export declare function calculateAvailablePerpLiquidity(market: PerpMarketAccount, oraclePriceData: OraclePriceData, dlob: DLOB, slot: number): {
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateAvailablePerpLiquidity = exports.calculateNetUserPnlImbalance = exports.calculateNetUserPnl = exports.calculateMarketAvailablePNL = exports.calculateUnrealizedAssetWeight = exports.calculateMarketMarginRatio = exports.calculateOracleSpread = exports.calculateOracleReserveSpread = exports.calculateNewMarketAfterTrade = exports.calculateAskPrice = exports.calculateBidPrice = exports.calculateReservePrice = void 0;
3
+ exports.calculateAvailablePerpLiquidity = exports.calculateNetUserPnlImbalance = exports.calculateNetUserPnl = exports.calculateMarketMaxAvailableInsurance = exports.calculateMarketAvailablePNL = exports.calculateUnrealizedAssetWeight = exports.calculateMarketMarginRatio = exports.calculateOracleSpread = exports.calculateOracleReserveSpread = exports.calculateNewMarketAfterTrade = exports.calculateAskPrice = exports.calculateBidPrice = exports.calculateReservePrice = void 0;
4
4
  const anchor_1 = require("@project-serum/anchor");
5
5
  const types_1 = require("../types");
6
6
  const amm_1 = require("./amm");
7
7
  const margin_1 = require("./margin");
8
8
  const numericConstants_1 = require("../constants/numericConstants");
9
9
  const spotBalance_1 = require("./spotBalance");
10
+ const assert_1 = require("../assert/assert");
10
11
  /**
11
12
  * Calculates market mark price
12
13
  *
@@ -98,6 +99,14 @@ function calculateMarketAvailablePNL(perpMarket, spotMarket) {
98
99
  return spotBalance_1.getTokenAmount(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
99
100
  }
100
101
  exports.calculateMarketAvailablePNL = calculateMarketAvailablePNL;
102
+ function calculateMarketMaxAvailableInsurance(perpMarket, spotMarket) {
103
+ assert_1.assert(spotMarket.marketIndex == numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
104
+ // todo: insuranceFundAllocation technically not guaranteed to be in Insurance Fund
105
+ const insuranceFundAllocation = perpMarket.insuranceClaim.quoteMaxInsurance.sub(perpMarket.insuranceClaim.quoteSettledInsurance);
106
+ const ammFeePool = spotBalance_1.getTokenAmount(perpMarket.amm.feePool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
107
+ return insuranceFundAllocation.add(ammFeePool);
108
+ }
109
+ exports.calculateMarketMaxAvailableInsurance = calculateMarketMaxAvailableInsurance;
101
110
  function calculateNetUserPnl(perpMarket, oraclePriceData) {
102
111
  const netUserPositionValue = perpMarket.amm.baseAssetAmountWithAmm
103
112
  .mul(oraclePriceData.price)
package/lib/user.js CHANGED
@@ -777,7 +777,7 @@ class User {
777
777
  const maintenanceMarginRequirement = this.getMaintenanceMarginRequirement();
778
778
  const freeCollateral = _1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(maintenanceMarginRequirement));
779
779
  const market = this.driftClient.getPerpMarketAccount(marketIndex);
780
- const currentPerpPosition = this.getPerpPosition(marketIndex);
780
+ const currentPerpPosition = this.getPerpPosition(marketIndex) || this.getEmptyPosition(marketIndex);
781
781
  let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange);
782
782
  if (!freeCollateralDelta) {
783
783
  return new _1.BN(-1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.14.0",
3
+ "version": "2.15.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -87,6 +87,16 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
87
87
  launchTs: 1670347281000,
88
88
  oracleSource: OracleSource.PYTH,
89
89
  },
90
+ {
91
+ fullName: 'Aptos',
92
+ category: ['L1', 'Infra'],
93
+ symbol: 'APT-PERP',
94
+ baseAssetSymbol: 'APT',
95
+ marketIndex: 3,
96
+ oracle: new PublicKey('FNNvb1AFDnDVPkocEri8mWbJ1952HQZtFLuwPiUjSJQ'),
97
+ launchTs: 1675802661000,
98
+ oracleSource: OracleSource.PYTH,
99
+ },
90
100
  ];
91
101
 
92
102
  export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.14.0",
2
+ "version": "2.15.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -25,9 +25,11 @@ import {
25
25
  MARGIN_PRECISION,
26
26
  PRICE_TO_QUOTE_PRECISION,
27
27
  ZERO,
28
+ QUOTE_SPOT_MARKET_INDEX,
28
29
  } from '../constants/numericConstants';
29
30
  import { getTokenAmount } from './spotBalance';
30
31
  import { DLOB } from '../dlob/DLOB';
32
+ import { assert } from '../assert/assert';
31
33
 
32
34
  /**
33
35
  * Calculates market mark price
@@ -202,6 +204,25 @@ export function calculateMarketAvailablePNL(
202
204
  );
203
205
  }
204
206
 
207
+ export function calculateMarketMaxAvailableInsurance(
208
+ perpMarket: PerpMarketAccount,
209
+ spotMarket: SpotMarketAccount
210
+ ): BN {
211
+ assert(spotMarket.marketIndex == QUOTE_SPOT_MARKET_INDEX);
212
+
213
+ // todo: insuranceFundAllocation technically not guaranteed to be in Insurance Fund
214
+ const insuranceFundAllocation =
215
+ perpMarket.insuranceClaim.quoteMaxInsurance.sub(
216
+ perpMarket.insuranceClaim.quoteSettledInsurance
217
+ );
218
+ const ammFeePool = getTokenAmount(
219
+ perpMarket.amm.feePool.scaledBalance,
220
+ spotMarket,
221
+ SpotBalanceType.DEPOSIT
222
+ );
223
+ return insuranceFundAllocation.add(ammFeePool);
224
+ }
225
+
205
226
  export function calculateNetUserPnl(
206
227
  perpMarket: PerpMarketAccount,
207
228
  oraclePriceData: OraclePriceData
package/src/user.ts CHANGED
@@ -1377,7 +1377,8 @@ export class User {
1377
1377
  );
1378
1378
 
1379
1379
  const market = this.driftClient.getPerpMarketAccount(marketIndex);
1380
- const currentPerpPosition = this.getPerpPosition(marketIndex);
1380
+ const currentPerpPosition =
1381
+ this.getPerpPosition(marketIndex) || this.getEmptyPosition(marketIndex);
1381
1382
 
1382
1383
  let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(
1383
1384
  market,