@drift-labs/sdk-browser 2.136.0-beta.7 → 2.137.0-beta.1

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.136.0-beta.7
1
+ 2.137.0-beta.1
@@ -2,7 +2,7 @@
2
2
  /// <reference types="node" />
3
3
  import { DataAndSlot, AccountSubscriber, ResubOpts, BufferAndSlot } from './types';
4
4
  import { Program } from '@coral-xyz/anchor';
5
- import { AccountInfoBase, AccountInfoWithBase64EncodedData, AccountInfoWithBase58EncodedData, Rpc, RpcSubscriptions, SolanaRpcSubscriptionsApi, type Commitment } from 'gill';
5
+ import { AccountInfoBase, AccountInfoWithBase64EncodedData, AccountInfoWithBase58EncodedData, Rpc, RpcSubscriptions, SolanaRpcSubscriptionsApi, Commitment } from 'gill';
6
6
  import { PublicKey } from '@solana/web3.js';
7
7
  /**
8
8
  * WebSocketAccountSubscriberV2
@@ -3,7 +3,7 @@
3
3
  import { BufferAndSlot, ProgramAccountSubscriber, ResubOpts } from './types';
4
4
  import { Program } from '@coral-xyz/anchor';
5
5
  import { Commitment, Context, MemcmpFilter, PublicKey } from '@solana/web3.js';
6
- import { AccountInfoBase, AccountInfoWithBase58EncodedData, AccountInfoWithBase64EncodedData, type Address } from 'gill';
6
+ import { AccountInfoBase, AccountInfoWithBase58EncodedData, AccountInfoWithBase64EncodedData, Address } from 'gill';
7
7
  /**
8
8
  * WebSocketProgramAccountsSubscriberV2
9
9
  *
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.135.0",
2
+ "version": "2.136.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -1389,96 +1389,6 @@
1389
1389
  }
1390
1390
  ]
1391
1391
  },
1392
- {
1393
- "name": "addPerpLpShares",
1394
- "accounts": [
1395
- {
1396
- "name": "state",
1397
- "isMut": false,
1398
- "isSigner": false
1399
- },
1400
- {
1401
- "name": "user",
1402
- "isMut": true,
1403
- "isSigner": false
1404
- },
1405
- {
1406
- "name": "authority",
1407
- "isMut": false,
1408
- "isSigner": true
1409
- }
1410
- ],
1411
- "args": [
1412
- {
1413
- "name": "nShares",
1414
- "type": "u64"
1415
- },
1416
- {
1417
- "name": "marketIndex",
1418
- "type": "u16"
1419
- }
1420
- ]
1421
- },
1422
- {
1423
- "name": "removePerpLpShares",
1424
- "accounts": [
1425
- {
1426
- "name": "state",
1427
- "isMut": false,
1428
- "isSigner": false
1429
- },
1430
- {
1431
- "name": "user",
1432
- "isMut": true,
1433
- "isSigner": false
1434
- },
1435
- {
1436
- "name": "authority",
1437
- "isMut": false,
1438
- "isSigner": true
1439
- }
1440
- ],
1441
- "args": [
1442
- {
1443
- "name": "sharesToBurn",
1444
- "type": "u64"
1445
- },
1446
- {
1447
- "name": "marketIndex",
1448
- "type": "u16"
1449
- }
1450
- ]
1451
- },
1452
- {
1453
- "name": "removePerpLpSharesInExpiringMarket",
1454
- "accounts": [
1455
- {
1456
- "name": "state",
1457
- "isMut": false,
1458
- "isSigner": false
1459
- },
1460
- {
1461
- "name": "user",
1462
- "isMut": true,
1463
- "isSigner": false
1464
- },
1465
- {
1466
- "name": "signer",
1467
- "isMut": false,
1468
- "isSigner": true
1469
- }
1470
- ],
1471
- "args": [
1472
- {
1473
- "name": "sharesToBurn",
1474
- "type": "u64"
1475
- },
1476
- {
1477
- "name": "marketIndex",
1478
- "type": "u16"
1479
- }
1480
- ]
1481
- },
1482
1392
  {
1483
1393
  "name": "updateUserName",
1484
1394
  "accounts": [
@@ -2291,27 +2201,6 @@
2291
2201
  ],
2292
2202
  "args": []
2293
2203
  },
2294
- {
2295
- "name": "settleLp",
2296
- "accounts": [
2297
- {
2298
- "name": "state",
2299
- "isMut": false,
2300
- "isSigner": false
2301
- },
2302
- {
2303
- "name": "user",
2304
- "isMut": true,
2305
- "isSigner": false
2306
- }
2307
- ],
2308
- "args": [
2309
- {
2310
- "name": "marketIndex",
2311
- "type": "u16"
2312
- }
2313
- ]
2314
- },
2315
2204
  {
2316
2205
  "name": "settleExpiredMarket",
2317
2206
  "accounts": [
@@ -6003,58 +5892,6 @@
6003
5892
  }
6004
5893
  ]
6005
5894
  },
6006
- {
6007
- "name": "updatePerpMarketTargetBaseAssetAmountPerLp",
6008
- "accounts": [
6009
- {
6010
- "name": "admin",
6011
- "isMut": false,
6012
- "isSigner": true
6013
- },
6014
- {
6015
- "name": "state",
6016
- "isMut": false,
6017
- "isSigner": false
6018
- },
6019
- {
6020
- "name": "perpMarket",
6021
- "isMut": true,
6022
- "isSigner": false
6023
- }
6024
- ],
6025
- "args": [
6026
- {
6027
- "name": "targetBaseAssetAmountPerLp",
6028
- "type": "i32"
6029
- }
6030
- ]
6031
- },
6032
- {
6033
- "name": "updatePerpMarketPerLpBase",
6034
- "accounts": [
6035
- {
6036
- "name": "admin",
6037
- "isMut": false,
6038
- "isSigner": true
6039
- },
6040
- {
6041
- "name": "state",
6042
- "isMut": false,
6043
- "isSigner": false
6044
- },
6045
- {
6046
- "name": "perpMarket",
6047
- "isMut": true,
6048
- "isSigner": false
6049
- }
6050
- ],
6051
- "args": [
6052
- {
6053
- "name": "perLpBase",
6054
- "type": "i8"
6055
- }
6056
- ]
6057
- },
6058
5895
  {
6059
5896
  "name": "updateLpCooldownTime",
6060
5897
  "accounts": [
@@ -12713,23 +12550,6 @@
12713
12550
  ]
12714
12551
  }
12715
12552
  },
12716
- {
12717
- "name": "AMMLiquiditySplit",
12718
- "type": {
12719
- "kind": "enum",
12720
- "variants": [
12721
- {
12722
- "name": "ProtocolOwned"
12723
- },
12724
- {
12725
- "name": "LPOwned"
12726
- },
12727
- {
12728
- "name": "Shared"
12729
- }
12730
- ]
12731
- }
12732
- },
12733
12553
  {
12734
12554
  "name": "AMMAvailability",
12735
12555
  "type": {
@@ -20,8 +20,7 @@ function isUserBankrupt(user) {
20
20
  for (const position of userAccount.perpPositions) {
21
21
  if (!position.baseAssetAmount.eq(numericConstants_1.ZERO) ||
22
22
  position.quoteAssetAmount.gt(numericConstants_1.ZERO) ||
23
- (0, position_1.hasOpenOrders)(position) ||
24
- position.lpShares.gt(numericConstants_1.ZERO)) {
23
+ (0, position_1.hasOpenOrders)(position)) {
25
24
  return false;
26
25
  }
27
26
  if (position.quoteAssetAmount.lt(numericConstants_1.ZERO)) {
@@ -41,6 +41,7 @@ export declare class User {
41
41
  * @returns userPerpPosition
42
42
  */
43
43
  getPerpPosition(marketIndex: number): PerpPosition | undefined;
44
+ getPerpPositionOrEmpty(marketIndex: number): PerpPosition;
44
45
  getPerpPositionAndSlot(marketIndex: number): DataAndSlot<PerpPosition | undefined>;
45
46
  getSpotPositionForUserAccount(userAccount: UserAccount, marketIndex: number): SpotPosition | undefined;
46
47
  /**
@@ -85,20 +86,6 @@ export declare class User {
85
86
  * @returns : open asks
86
87
  */
87
88
  getPerpBidAsks(marketIndex: number): [BN, BN];
88
- /**
89
- * calculates the open bids and asks for an lp
90
- * optionally pass in lpShares to see what bid/asks a user *would* take on
91
- * @returns : lp open bids
92
- * @returns : lp open asks
93
- */
94
- getLPBidAsks(marketIndex: number, lpShares?: BN): [BN, BN];
95
- /**
96
- * calculates the market position if the lp position was settled
97
- * @returns : the settled userPosition
98
- * @returns : the dust base asset amount (ie, < stepsize)
99
- * @returns : pnl from settle
100
- */
101
- getPerpPositionWithLPSettle(marketIndex: number, originalPosition?: PerpPosition, burnLpShares?: boolean, includeRemainderInBaseAmount?: boolean): [PerpPosition, BN, BN];
102
89
  /**
103
90
  * calculates Buying Power = free collateral / initial margin ratio
104
91
  * @returns : Precision QUOTE_PRECISION
@@ -17,7 +17,6 @@ const types_2 = require("./types");
17
17
  const orders_1 = require("./math/orders");
18
18
  const websocketProgramUserAccountSubscriber_1 = require("./accounts/websocketProgramUserAccountSubscriber");
19
19
  const spotBalance_2 = require("./math/spotBalance");
20
- const amm_1 = require("./math/amm");
21
20
  const margin_2 = require("./math/margin");
22
21
  const pollingUserAccountSubscriber_1 = require("./accounts/pollingUserAccountSubscriber");
23
22
  const webSocketUserAccountSubscriber_1 = require("./accounts/webSocketUserAccountSubscriber");
@@ -105,6 +104,11 @@ class User {
105
104
  const userAccount = this.getUserAccount();
106
105
  return this.getPerpPositionForUserAccount(userAccount, marketIndex);
107
106
  }
107
+ getPerpPositionOrEmpty(marketIndex) {
108
+ var _a;
109
+ const userAccount = this.getUserAccount();
110
+ return ((_a = this.getPerpPositionForUserAccount(userAccount, marketIndex)) !== null && _a !== void 0 ? _a : this.getEmptyPosition(marketIndex));
111
+ }
108
112
  getPerpPositionAndSlot(marketIndex) {
109
113
  const userAccount = this.getUserAccountAndSlot();
110
114
  const perpPosition = this.getPerpPositionForUserAccount(userAccount.data, marketIndex);
@@ -248,193 +252,16 @@ class User {
248
252
  */
249
253
  getPerpBidAsks(marketIndex) {
250
254
  const position = this.getPerpPosition(marketIndex);
251
- const [lpOpenBids, lpOpenAsks] = this.getLPBidAsks(marketIndex);
252
- const totalOpenBids = lpOpenBids.add(position.openBids);
253
- const totalOpenAsks = lpOpenAsks.add(position.openAsks);
255
+ const totalOpenBids = position.openBids;
256
+ const totalOpenAsks = position.openAsks;
254
257
  return [totalOpenBids, totalOpenAsks];
255
258
  }
256
- /**
257
- * calculates the open bids and asks for an lp
258
- * optionally pass in lpShares to see what bid/asks a user *would* take on
259
- * @returns : lp open bids
260
- * @returns : lp open asks
261
- */
262
- getLPBidAsks(marketIndex, lpShares) {
263
- const position = this.getPerpPosition(marketIndex);
264
- const lpSharesToCalc = lpShares !== null && lpShares !== void 0 ? lpShares : position === null || position === void 0 ? void 0 : position.lpShares;
265
- if (!lpSharesToCalc || lpSharesToCalc.eq(numericConstants_1.ZERO)) {
266
- return [numericConstants_1.ZERO, numericConstants_1.ZERO];
267
- }
268
- const market = this.driftClient.getPerpMarketAccount(marketIndex);
269
- const [marketOpenBids, marketOpenAsks] = (0, amm_1.calculateMarketOpenBidAsk)(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
270
- const lpOpenBids = marketOpenBids.mul(lpSharesToCalc).div(market.amm.sqrtK);
271
- const lpOpenAsks = marketOpenAsks.mul(lpSharesToCalc).div(market.amm.sqrtK);
272
- return [lpOpenBids, lpOpenAsks];
273
- }
274
- /**
275
- * calculates the market position if the lp position was settled
276
- * @returns : the settled userPosition
277
- * @returns : the dust base asset amount (ie, < stepsize)
278
- * @returns : pnl from settle
279
- */
280
- getPerpPositionWithLPSettle(marketIndex, originalPosition, burnLpShares = false, includeRemainderInBaseAmount = false) {
281
- var _a;
282
- originalPosition =
283
- (_a = originalPosition !== null && originalPosition !== void 0 ? originalPosition : this.getPerpPosition(marketIndex)) !== null && _a !== void 0 ? _a : this.getEmptyPosition(marketIndex);
284
- if (originalPosition.lpShares.eq(numericConstants_1.ZERO)) {
285
- return [originalPosition, numericConstants_1.ZERO, numericConstants_1.ZERO];
286
- }
287
- const position = this.getClonedPosition(originalPosition);
288
- const market = this.driftClient.getPerpMarketAccount(position.marketIndex);
289
- if (market.amm.perLpBase != position.perLpBase) {
290
- // perLpBase = 1 => per 10 LP shares, perLpBase = -1 => per 0.1 LP shares
291
- const expoDiff = market.amm.perLpBase - position.perLpBase;
292
- const marketPerLpRebaseScalar = new anchor_1.BN(10 ** Math.abs(expoDiff));
293
- if (expoDiff > 0) {
294
- position.lastBaseAssetAmountPerLp =
295
- position.lastBaseAssetAmountPerLp.mul(marketPerLpRebaseScalar);
296
- position.lastQuoteAssetAmountPerLp =
297
- position.lastQuoteAssetAmountPerLp.mul(marketPerLpRebaseScalar);
298
- }
299
- else {
300
- position.lastBaseAssetAmountPerLp =
301
- position.lastBaseAssetAmountPerLp.div(marketPerLpRebaseScalar);
302
- position.lastQuoteAssetAmountPerLp =
303
- position.lastQuoteAssetAmountPerLp.div(marketPerLpRebaseScalar);
304
- }
305
- position.perLpBase = position.perLpBase + expoDiff;
306
- }
307
- const nShares = position.lpShares;
308
- // incorp unsettled funding on pre settled position
309
- const quoteFundingPnl = (0, position_1.calculateUnsettledFundingPnl)(market, position);
310
- let baseUnit = numericConstants_1.AMM_RESERVE_PRECISION;
311
- if (market.amm.perLpBase == position.perLpBase) {
312
- if (position.perLpBase >= 0 &&
313
- position.perLpBase <= numericConstants_1.AMM_RESERVE_PRECISION_EXP.toNumber()) {
314
- const marketPerLpRebase = new anchor_1.BN(10 ** market.amm.perLpBase);
315
- baseUnit = baseUnit.mul(marketPerLpRebase);
316
- }
317
- else if (position.perLpBase < 0 &&
318
- position.perLpBase >= -numericConstants_1.AMM_RESERVE_PRECISION_EXP.toNumber()) {
319
- const marketPerLpRebase = new anchor_1.BN(10 ** Math.abs(market.amm.perLpBase));
320
- baseUnit = baseUnit.div(marketPerLpRebase);
321
- }
322
- else {
323
- throw 'cannot calc';
324
- }
325
- }
326
- else {
327
- throw 'market.amm.perLpBase != position.perLpBase';
328
- }
329
- const deltaBaa = market.amm.baseAssetAmountPerLp
330
- .sub(position.lastBaseAssetAmountPerLp)
331
- .mul(nShares)
332
- .div(baseUnit);
333
- const deltaQaa = market.amm.quoteAssetAmountPerLp
334
- .sub(position.lastQuoteAssetAmountPerLp)
335
- .mul(nShares)
336
- .div(baseUnit);
337
- function sign(v) {
338
- return v.isNeg() ? new anchor_1.BN(-1) : new anchor_1.BN(1);
339
- }
340
- function standardize(amount, stepSize) {
341
- const remainder = amount.abs().mod(stepSize).mul(sign(amount));
342
- const standardizedAmount = amount.sub(remainder);
343
- return [standardizedAmount, remainder];
344
- }
345
- const [standardizedBaa, remainderBaa] = standardize(deltaBaa, market.amm.orderStepSize);
346
- position.remainderBaseAssetAmount += remainderBaa.toNumber();
347
- if (Math.abs(position.remainderBaseAssetAmount) >
348
- market.amm.orderStepSize.toNumber()) {
349
- const [newStandardizedBaa, newRemainderBaa] = standardize(new anchor_1.BN(position.remainderBaseAssetAmount), market.amm.orderStepSize);
350
- position.baseAssetAmount =
351
- position.baseAssetAmount.add(newStandardizedBaa);
352
- position.remainderBaseAssetAmount = newRemainderBaa.toNumber();
353
- }
354
- let dustBaseAssetValue = numericConstants_1.ZERO;
355
- if (burnLpShares && position.remainderBaseAssetAmount != 0) {
356
- const oraclePriceData = this.driftClient.getOracleDataForPerpMarket(position.marketIndex);
357
- dustBaseAssetValue = new anchor_1.BN(Math.abs(position.remainderBaseAssetAmount))
358
- .mul(oraclePriceData.price)
359
- .div(numericConstants_1.AMM_RESERVE_PRECISION)
360
- .add(numericConstants_1.ONE);
361
- }
362
- let updateType;
363
- if (position.baseAssetAmount.eq(numericConstants_1.ZERO)) {
364
- updateType = 'open';
365
- }
366
- else if (sign(position.baseAssetAmount).eq(sign(deltaBaa))) {
367
- updateType = 'increase';
368
- }
369
- else if (position.baseAssetAmount.abs().gt(deltaBaa.abs())) {
370
- updateType = 'reduce';
371
- }
372
- else if (position.baseAssetAmount.abs().eq(deltaBaa.abs())) {
373
- updateType = 'close';
374
- }
375
- else {
376
- updateType = 'flip';
377
- }
378
- let newQuoteEntry;
379
- let pnl;
380
- if (updateType == 'open' || updateType == 'increase') {
381
- newQuoteEntry = position.quoteEntryAmount.add(deltaQaa);
382
- pnl = numericConstants_1.ZERO;
383
- }
384
- else if (updateType == 'reduce' || updateType == 'close') {
385
- newQuoteEntry = position.quoteEntryAmount.sub(position.quoteEntryAmount
386
- .mul(deltaBaa.abs())
387
- .div(position.baseAssetAmount.abs()));
388
- pnl = position.quoteEntryAmount.sub(newQuoteEntry).add(deltaQaa);
389
- }
390
- else {
391
- newQuoteEntry = deltaQaa.sub(deltaQaa.mul(position.baseAssetAmount.abs()).div(deltaBaa.abs()));
392
- pnl = position.quoteEntryAmount.add(deltaQaa.sub(newQuoteEntry));
393
- }
394
- position.quoteEntryAmount = newQuoteEntry;
395
- position.baseAssetAmount = position.baseAssetAmount.add(standardizedBaa);
396
- position.quoteAssetAmount = position.quoteAssetAmount
397
- .add(deltaQaa)
398
- .add(quoteFundingPnl)
399
- .sub(dustBaseAssetValue);
400
- position.quoteBreakEvenAmount = position.quoteBreakEvenAmount
401
- .add(deltaQaa)
402
- .add(quoteFundingPnl)
403
- .sub(dustBaseAssetValue);
404
- // update open bids/asks
405
- const [marketOpenBids, marketOpenAsks] = (0, amm_1.calculateMarketOpenBidAsk)(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
406
- const lpOpenBids = marketOpenBids
407
- .mul(position.lpShares)
408
- .div(market.amm.sqrtK);
409
- const lpOpenAsks = marketOpenAsks
410
- .mul(position.lpShares)
411
- .div(market.amm.sqrtK);
412
- position.openBids = lpOpenBids.add(position.openBids);
413
- position.openAsks = lpOpenAsks.add(position.openAsks);
414
- // eliminate counting funding on settled position
415
- if (position.baseAssetAmount.gt(numericConstants_1.ZERO)) {
416
- position.lastCumulativeFundingRate = market.amm.cumulativeFundingRateLong;
417
- }
418
- else if (position.baseAssetAmount.lt(numericConstants_1.ZERO)) {
419
- position.lastCumulativeFundingRate =
420
- market.amm.cumulativeFundingRateShort;
421
- }
422
- else {
423
- position.lastCumulativeFundingRate = numericConstants_1.ZERO;
424
- }
425
- const remainderBeforeRemoval = new anchor_1.BN(position.remainderBaseAssetAmount);
426
- if (includeRemainderInBaseAmount) {
427
- position.baseAssetAmount = position.baseAssetAmount.add(remainderBeforeRemoval);
428
- position.remainderBaseAssetAmount = 0;
429
- }
430
- return [position, remainderBeforeRemoval, pnl];
431
- }
432
259
  /**
433
260
  * calculates Buying Power = free collateral / initial margin ratio
434
261
  * @returns : Precision QUOTE_PRECISION
435
262
  */
436
263
  getPerpBuyingPower(marketIndex, collateralBuffer = numericConstants_1.ZERO, enterHighLeverageMode = undefined) {
437
- const perpPosition = this.getPerpPositionWithLPSettle(marketIndex, undefined, true)[0];
264
+ const perpPosition = this.getPerpPositionOrEmpty(marketIndex);
438
265
  const perpMarket = this.driftClient.getPerpMarketAccount(marketIndex);
439
266
  const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
440
267
  const worstCaseBaseAssetAmount = perpPosition
@@ -480,8 +307,7 @@ class User {
480
307
  getActivePerpPositionsForUserAccount(userAccount) {
481
308
  return userAccount.perpPositions.filter((pos) => !pos.baseAssetAmount.eq(numericConstants_1.ZERO) ||
482
309
  !pos.quoteAssetAmount.eq(numericConstants_1.ZERO) ||
483
- !(pos.openOrders == 0) ||
484
- !pos.lpShares.eq(numericConstants_1.ZERO));
310
+ !(pos.openOrders == 0));
485
311
  }
486
312
  getActivePerpPositions() {
487
313
  const userAccount = this.getUserAccount();
@@ -522,9 +348,6 @@ class User {
522
348
  const oraclePriceData = this.getMMOracleDataForPerpMarket(market.marketIndex);
523
349
  const quoteSpotMarket = this.driftClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
524
350
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(market.quoteSpotMarketIndex);
525
- if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
526
- perpPosition = this.getPerpPositionWithLPSettle(perpPosition.marketIndex, undefined, !!withWeightMarginCategory)[0];
527
- }
528
351
  let positionUnrealizedPnl = (0, position_2.calculatePositionPNL)(market, perpPosition, withFunding, oraclePriceData);
529
352
  let quotePrice;
530
353
  if (strict && positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
@@ -758,10 +581,6 @@ class User {
758
581
  }
759
582
  calculateWeightedPerpPositionLiability(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict = false, enteringHighLeverage = undefined) {
760
583
  const market = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
761
- if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
762
- // is an lp, clone so we dont mutate the position
763
- perpPosition = this.getPerpPositionWithLPSettle(market.marketIndex, this.getClonedPosition(perpPosition), !!marginCategory)[0];
764
- }
765
584
  let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
766
585
  if ((0, types_1.isVariant)(market.status, 'settlement')) {
767
586
  valuationPrice = market.expiryPrice;
@@ -805,13 +624,6 @@ class User {
805
624
  .div(numericConstants_1.MARGIN_PRECISION);
806
625
  if (includeOpenOrders) {
807
626
  liabilityValue = liabilityValue.add(new anchor_1.BN(perpPosition.openOrders).mul(numericConstants_1.OPEN_ORDER_MARGIN_REQUIREMENT));
808
- if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
809
- liabilityValue = liabilityValue.add(anchor_1.BN.max(numericConstants_1.QUOTE_PRECISION, valuationPrice
810
- .mul(market.amm.orderStepSize)
811
- .mul(numericConstants_1.QUOTE_PRECISION)
812
- .div(numericConstants_1.AMM_RESERVE_PRECISION)
813
- .div(numericConstants_1.PRICE_PRECISION)));
814
- }
815
627
  }
816
628
  }
817
629
  return liabilityValue;
@@ -839,7 +651,7 @@ class User {
839
651
  * @returns : Precision QUOTE_PRECISION
840
652
  */
841
653
  getPerpPositionValue(marketIndex, oraclePriceData, includeOpenOrders = false) {
842
- const userPosition = this.getPerpPositionWithLPSettle(marketIndex, undefined, false, true)[0] || this.getEmptyPosition(marketIndex);
654
+ const userPosition = this.getPerpPositionOrEmpty(marketIndex);
843
655
  const market = this.driftClient.getPerpMarketAccount(userPosition.marketIndex);
844
656
  return (0, margin_2.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData, includeOpenOrders);
845
657
  }
@@ -848,7 +660,7 @@ class User {
848
660
  * @returns : Precision QUOTE_PRECISION
849
661
  */
850
662
  getPerpLiabilityValue(marketIndex, oraclePriceData, includeOpenOrders = false) {
851
- const userPosition = this.getPerpPositionWithLPSettle(marketIndex, undefined, false, true)[0] || this.getEmptyPosition(marketIndex);
663
+ const userPosition = this.getPerpPositionOrEmpty(marketIndex);
852
664
  const market = this.driftClient.getPerpMarketAccount(userPosition.marketIndex);
853
665
  if (includeOpenOrders) {
854
666
  return (0, margin_1.calculateWorstCasePerpLiabilityValue)(userPosition, market, oraclePriceData.price).worstCaseLiabilityValue;
@@ -1143,7 +955,7 @@ class User {
1143
955
  .find((market) => market.amm.oracle.equals(oracle));
1144
956
  const oraclePrice = this.driftClient.getOracleDataForSpotMarket(marketIndex).price;
1145
957
  if (perpMarketWithSameOracle) {
1146
- const perpPosition = this.getPerpPositionWithLPSettle(perpMarketWithSameOracle.marketIndex, undefined, true)[0];
958
+ const perpPosition = this.getPerpPositionOrEmpty(perpMarketWithSameOracle.marketIndex);
1147
959
  if (perpPosition) {
1148
960
  let freeCollateralDeltaForPerp = this.calculateFreeCollateralDeltaForPerp(perpMarketWithSameOracle, perpPosition, numericConstants_1.ZERO, oraclePrice);
1149
961
  if (freeCollateralDeltaForPerp) {
@@ -1184,8 +996,7 @@ class User {
1184
996
  const oracle = this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
1185
997
  const oraclePrice = this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
1186
998
  const market = this.driftClient.getPerpMarketAccount(marketIndex);
1187
- const currentPerpPosition = this.getPerpPositionWithLPSettle(marketIndex, undefined, true)[0] ||
1188
- this.getEmptyPosition(marketIndex);
999
+ const currentPerpPosition = this.getPerpPositionOrEmpty(marketIndex);
1189
1000
  positionBaseSizeChange = (0, orders_1.standardizeBaseAssetAmount)(positionBaseSizeChange, market.amm.orderStepSize);
1190
1001
  const freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(market, oraclePrice, currentPerpPosition, positionBaseSizeChange, estimatedEntryPrice, includeOpenOrders, enteringHighLeverage);
1191
1002
  freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
@@ -1344,7 +1155,7 @@ class User {
1344
1155
  * @returns : Precision PRICE_PRECISION
1345
1156
  */
1346
1157
  liquidationPriceAfterClose(positionMarketIndex, closeQuoteAmount, estimatedEntryPrice = numericConstants_1.ZERO) {
1347
- const currentPosition = this.getPerpPositionWithLPSettle(positionMarketIndex, undefined, true)[0] || this.getEmptyPosition(positionMarketIndex);
1158
+ const currentPosition = this.getPerpPositionOrEmpty(positionMarketIndex);
1348
1159
  const closeBaseAmount = currentPosition.baseAssetAmount
1349
1160
  .mul(closeQuoteAmount)
1350
1161
  .div(currentPosition.quoteAssetAmount.abs())
@@ -1373,8 +1184,7 @@ class User {
1373
1184
  getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide, isLp = false, enterHighLeverageMode = undefined) {
1374
1185
  let tradeSize = numericConstants_1.ZERO;
1375
1186
  let oppositeSideTradeSize = numericConstants_1.ZERO;
1376
- const currentPosition = this.getPerpPositionWithLPSettle(targetMarketIndex, undefined, true)[0] ||
1377
- this.getEmptyPosition(targetMarketIndex);
1187
+ const currentPosition = this.getPerpPositionOrEmpty(targetMarketIndex);
1378
1188
  const targetSide = (0, types_1.isVariant)(tradeSide, 'short') ? 'short' : 'long';
1379
1189
  const currentPositionSide = (currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.baseAssetAmount.isNeg())
1380
1190
  ? 'short'
@@ -1752,8 +1562,7 @@ class User {
1752
1562
  .div(netAssetValueAfterTrade);
1753
1563
  return newLeverage;
1754
1564
  }
1755
- const currentPosition = this.getPerpPositionWithLPSettle(targetMarketIndex)[0] ||
1756
- this.getEmptyPosition(targetMarketIndex);
1565
+ const currentPosition = this.getPerpPositionOrEmpty(targetMarketIndex);
1757
1566
  const perpMarket = this.driftClient.getPerpMarketAccount(targetMarketIndex);
1758
1567
  const oracleData = this.getOracleDataForPerpMarket(targetMarketIndex);
1759
1568
  let {
@@ -2016,12 +1825,11 @@ class User {
2016
1825
  };
2017
1826
  }
2018
1827
  getPerpPositionHealth({ marginCategory, perpPosition, oraclePriceData, quoteOraclePriceData, }) {
2019
- const settledLpPosition = this.getPerpPositionWithLPSettle(perpPosition.marketIndex, perpPosition)[0];
2020
1828
  const perpMarket = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
2021
1829
  const _oraclePriceData = oraclePriceData ||
2022
1830
  this.driftClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
2023
1831
  const oraclePrice = _oraclePriceData.price;
2024
- const { worstCaseBaseAssetAmount: worstCaseBaseAmount, worstCaseLiabilityValue, } = (0, margin_1.calculateWorstCasePerpLiabilityValue)(settledLpPosition, perpMarket, oraclePrice);
1832
+ const { worstCaseBaseAssetAmount: worstCaseBaseAmount, worstCaseLiabilityValue, } = (0, margin_1.calculateWorstCasePerpLiabilityValue)(perpPosition, perpMarket, oraclePrice);
2025
1833
  const marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio, this.isHighLeverageMode(marginCategory)));
2026
1834
  const _quoteOraclePriceData = quoteOraclePriceData ||
2027
1835
  this.driftClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
@@ -2031,13 +1839,6 @@ class User {
2031
1839
  .mul(marginRatio)
2032
1840
  .div(numericConstants_1.MARGIN_PRECISION);
2033
1841
  marginRequirement = marginRequirement.add(new anchor_1.BN(perpPosition.openOrders).mul(numericConstants_1.OPEN_ORDER_MARGIN_REQUIREMENT));
2034
- if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
2035
- marginRequirement = marginRequirement.add(anchor_1.BN.max(numericConstants_1.QUOTE_PRECISION, oraclePrice
2036
- .mul(perpMarket.amm.orderStepSize)
2037
- .mul(numericConstants_1.QUOTE_PRECISION)
2038
- .div(numericConstants_1.AMM_RESERVE_PRECISION)
2039
- .div(numericConstants_1.PRICE_PRECISION)));
2040
- }
2041
1842
  return {
2042
1843
  marketIndex: perpMarket.marketIndex,
2043
1844
  size: worstCaseBaseAmount,
@@ -2064,8 +1865,7 @@ class User {
2064
1865
  quoteOraclePriceData,
2065
1866
  }));
2066
1867
  const quoteSpotMarket = this.driftClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
2067
- const settledPerpPosition = this.getPerpPositionWithLPSettle(perpPosition.marketIndex, perpPosition)[0];
2068
- const positionUnrealizedPnl = (0, position_2.calculatePositionPNL)(perpMarket, settledPerpPosition, true, oraclePriceData);
1868
+ const positionUnrealizedPnl = (0, position_2.calculatePositionPNL)(perpMarket, perpPosition, true, oraclePriceData);
2069
1869
  let pnlWeight;
2070
1870
  if (positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
2071
1871
  pnlWeight = (0, market_1.calculateUnrealizedAssetWeight)(perpMarket, quoteSpotMarket, positionUnrealizedPnl, marginCategory, oraclePriceData);
@@ -2152,7 +1952,7 @@ class User {
2152
1952
  * @returns positionValue : Precision QUOTE_PRECISION
2153
1953
  */
2154
1954
  getTotalPerpPositionValueExcludingMarket(marketToIgnore, marginCategory, liquidationBuffer, includeOpenOrders) {
2155
- const currentPerpPosition = this.getPerpPositionWithLPSettle(marketToIgnore, undefined, !!marginCategory)[0] || this.getEmptyPosition(marketToIgnore);
1955
+ const currentPerpPosition = this.getPerpPositionOrEmpty(marketToIgnore);
2156
1956
  const oracleData = this.getOracleDataForPerpMarket(marketToIgnore);
2157
1957
  let currentPerpPositionValueUSDC = numericConstants_1.ZERO;
2158
1958
  if (currentPerpPosition) {
@@ -2,7 +2,7 @@
2
2
  /// <reference types="node" />
3
3
  import { DataAndSlot, AccountSubscriber, ResubOpts, BufferAndSlot } from './types';
4
4
  import { Program } from '@coral-xyz/anchor';
5
- import { AccountInfoBase, AccountInfoWithBase64EncodedData, AccountInfoWithBase58EncodedData, Rpc, RpcSubscriptions, SolanaRpcSubscriptionsApi, type Commitment } from 'gill';
5
+ import { AccountInfoBase, AccountInfoWithBase64EncodedData, AccountInfoWithBase58EncodedData, Rpc, RpcSubscriptions, SolanaRpcSubscriptionsApi, Commitment } from 'gill';
6
6
  import { PublicKey } from '@solana/web3.js';
7
7
  /**
8
8
  * WebSocketAccountSubscriberV2
@@ -1 +1 @@
1
- {"version":3,"file":"webSocketAccountSubscriberV2.d.ts","sourceRoot":"","sources":["../../../src/accounts/webSocketAccountSubscriberV2.ts"],"names":[],"mappings":";;AAAA,OAAO,EACN,WAAW,EACX,iBAAiB,EACjB,SAAS,EACT,aAAa,EACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAkB,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5D,OAA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