@drift-labs/sdk 2.31.1-beta.5 → 2.31.1-beta.6

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.31.1-beta.5
1
+ 2.31.1-beta.6
@@ -230,6 +230,7 @@ export declare class DriftClient {
230
230
  getRemovePerpLpSharesIx(marketIndex: number, sharesToBurn?: BN): Promise<TransactionInstruction>;
231
231
  addPerpLpShares(amount: BN, marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
232
232
  getAddPerpLpSharesIx(amount: BN, marketIndex: number): Promise<TransactionInstruction>;
233
+ getQuoteValuePerLpShare(marketIndex: number): BN;
233
234
  /**
234
235
  * @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
235
236
  */
@@ -1358,6 +1358,19 @@ class DriftClient {
1358
1358
  remainingAccounts: remainingAccounts,
1359
1359
  });
1360
1360
  }
1361
+ getQuoteValuePerLpShare(marketIndex) {
1362
+ const perpMarketAccount = this.getPerpMarketAccount(marketIndex);
1363
+ const openBids = anchor_1.BN.max(perpMarketAccount.amm.baseAssetReserve.sub(perpMarketAccount.amm.minBaseAssetReserve), numericConstants_1.ZERO);
1364
+ const openAsks = anchor_1.BN.max(perpMarketAccount.amm.maxBaseAssetReserve.sub(perpMarketAccount.amm.baseAssetReserve), numericConstants_1.ZERO);
1365
+ const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
1366
+ const maxOpenBidsAsks = anchor_1.BN.max(openBids, openAsks);
1367
+ const quoteValuePerLpShare = maxOpenBidsAsks
1368
+ .mul(oraclePriceData.price)
1369
+ .mul(numericConstants_1.QUOTE_PRECISION)
1370
+ .div(numericConstants_1.PRICE_PRECISION)
1371
+ .div(perpMarketAccount.amm.sqrtK);
1372
+ return quoteValuePerLpShare;
1373
+ }
1361
1374
  /**
1362
1375
  * @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
1363
1376
  */
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.1-beta.4",
2
+ "version": "2.31.1-beta.5",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/lib/user.js CHANGED
@@ -180,7 +180,9 @@ class User {
180
180
  * @returns : pnl from settle
181
181
  */
182
182
  getPerpPositionWithLPSettle(marketIndex, originalPosition) {
183
- originalPosition = originalPosition !== null && originalPosition !== void 0 ? originalPosition : this.getPerpPosition(marketIndex);
183
+ var _a;
184
+ originalPosition =
185
+ (_a = originalPosition !== null && originalPosition !== void 0 ? originalPosition : this.getPerpPosition(marketIndex)) !== null && _a !== void 0 ? _a : this.getEmptyPosition(marketIndex);
184
186
  if (originalPosition.lpShares.eq(numericConstants_1.ZERO)) {
185
187
  return [originalPosition, numericConstants_1.ZERO, numericConstants_1.ZERO];
186
188
  }
@@ -232,7 +234,7 @@ class User {
232
234
  let pnl;
233
235
  if (updateType == 'open' || updateType == 'increase') {
234
236
  newQuoteEntry = position.quoteEntryAmount.add(deltaQaa);
235
- pnl = 0;
237
+ pnl = numericConstants_1.ZERO;
236
238
  }
237
239
  else if (updateType == 'reduce' || updateType == 'close') {
238
240
  newQuoteEntry = position.quoteEntryAmount.sub(position.quoteEntryAmount
@@ -608,7 +610,8 @@ class User {
608
610
  * @returns : Precision QUOTE_PRECISION
609
611
  */
610
612
  getPerpPositionValue(marketIndex, oraclePriceData, includeOpenOrders = false) {
611
- const userPosition = this.getPerpPosition(marketIndex) || this.getEmptyPosition(marketIndex);
613
+ const userPosition = this.getPerpPositionWithLPSettle(marketIndex)[0] ||
614
+ this.getEmptyPosition(marketIndex);
612
615
  const market = this.driftClient.getPerpMarketAccount(userPosition.marketIndex);
613
616
  return (0, margin_1.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData, includeOpenOrders);
614
617
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.31.1-beta.5",
3
+ "version": "2.31.1-beta.6",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
package/src/config.ts CHANGED
@@ -131,7 +131,7 @@ export async function findAllMarketAndOracles(program: Program): Promise<{
131
131
  (await program.account.spotMarket.all()) as ProgramAccount<SpotMarketAccount>[];
132
132
 
133
133
  for (const perpMarketProgramAccount of perpMarketProgramAccounts) {
134
- const perpMarket = perpMarketProgramAccount.account;
134
+ const perpMarket = perpMarketProgramAccount.account as PerpMarketAccount;
135
135
  perpMarketIndexes.push(perpMarket.marketIndex);
136
136
  oracleInfos.set(perpMarket.amm.oracle.toString(), {
137
137
  publicKey: perpMarket.amm.oracle,
@@ -140,7 +140,7 @@ export async function findAllMarketAndOracles(program: Program): Promise<{
140
140
  }
141
141
 
142
142
  for (const spotMarketProgramAccount of spotMarketProgramAccounts) {
143
- const spotMarket = spotMarketProgramAccount.account;
143
+ const spotMarket = spotMarketProgramAccount.account as SpotMarketAccount;
144
144
  spotMarketIndexes.push(spotMarket.marketIndex);
145
145
  oracleInfos.set(spotMarket.oracle.toString(), {
146
146
  publicKey: spotMarket.oracle,
@@ -95,6 +95,7 @@ import {
95
95
  PRICE_PRECISION,
96
96
  QUOTE_SPOT_MARKET_INDEX,
97
97
  ZERO,
98
+ QUOTE_PRECISION,
98
99
  } from './constants/numericConstants';
99
100
  import { findDirectionToClose, positionIsAvailable } from './math/position';
100
101
  import { getSignedTokenAmount, getTokenAmount } from './math/spotBalance';
@@ -2301,6 +2302,35 @@ export class DriftClient {
2301
2302
  });
2302
2303
  }
2303
2304
 
2305
+ public getQuoteValuePerLpShare(marketIndex: number): BN {
2306
+ const perpMarketAccount = this.getPerpMarketAccount(marketIndex);
2307
+
2308
+ const openBids = BN.max(
2309
+ perpMarketAccount.amm.baseAssetReserve.sub(
2310
+ perpMarketAccount.amm.minBaseAssetReserve
2311
+ ),
2312
+ ZERO
2313
+ );
2314
+
2315
+ const openAsks = BN.max(
2316
+ perpMarketAccount.amm.maxBaseAssetReserve.sub(
2317
+ perpMarketAccount.amm.baseAssetReserve
2318
+ ),
2319
+ ZERO
2320
+ );
2321
+
2322
+ const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
2323
+
2324
+ const maxOpenBidsAsks = BN.max(openBids, openAsks);
2325
+ const quoteValuePerLpShare = maxOpenBidsAsks
2326
+ .mul(oraclePriceData.price)
2327
+ .mul(QUOTE_PRECISION)
2328
+ .div(PRICE_PRECISION)
2329
+ .div(perpMarketAccount.amm.sqrtK);
2330
+
2331
+ return quoteValuePerLpShare;
2332
+ }
2333
+
2304
2334
  /**
2305
2335
  * @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
2306
2336
  */
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.1-beta.5",
2
+ "version": "2.31.1-beta.6",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/src/user.ts CHANGED
@@ -304,7 +304,10 @@ export class User {
304
304
  marketIndex: number,
305
305
  originalPosition?: PerpPosition
306
306
  ): [PerpPosition, BN, BN] {
307
- originalPosition = originalPosition ?? this.getPerpPosition(marketIndex);
307
+ originalPosition =
308
+ originalPosition ??
309
+ this.getPerpPosition(marketIndex) ??
310
+ this.getEmptyPosition(marketIndex);
308
311
 
309
312
  if (originalPosition.lpShares.eq(ZERO)) {
310
313
  return [originalPosition, ZERO, ZERO];
@@ -371,7 +374,7 @@ export class User {
371
374
  let pnl;
372
375
  if (updateType == 'open' || updateType == 'increase') {
373
376
  newQuoteEntry = position.quoteEntryAmount.add(deltaQaa);
374
- pnl = 0;
377
+ pnl = ZERO;
375
378
  } else if (updateType == 'reduce' || updateType == 'close') {
376
379
  newQuoteEntry = position.quoteEntryAmount.sub(
377
380
  position.quoteEntryAmount
@@ -1136,7 +1139,8 @@ export class User {
1136
1139
  includeOpenOrders = false
1137
1140
  ): BN {
1138
1141
  const userPosition =
1139
- this.getPerpPosition(marketIndex) || this.getEmptyPosition(marketIndex);
1142
+ this.getPerpPositionWithLPSettle(marketIndex)[0] ||
1143
+ this.getEmptyPosition(marketIndex);
1140
1144
  const market = this.driftClient.getPerpMarketAccount(
1141
1145
  userPosition.marketIndex
1142
1146
  );