@drift-labs/sdk 2.4.0 → 2.6.0-beta.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/dlob/DLOB.js CHANGED
@@ -171,8 +171,11 @@ class DLOB {
171
171
  if (order.baseAssetAmountFilled.eq(cumulativeBaseAssetAmountFilled)) {
172
172
  return;
173
173
  }
174
- order.baseAssetAmountFilled = cumulativeBaseAssetAmountFilled;
175
- (_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.update(order, userAccount);
174
+ const newOrder = {
175
+ ...order,
176
+ };
177
+ newOrder.baseAssetAmountFilled = cumulativeBaseAssetAmountFilled;
178
+ (_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.update(newOrder, userAccount);
176
179
  if (onUpdate) {
177
180
  onUpdate();
178
181
  }
@@ -187,6 +187,9 @@
187
187
  },
188
188
  {
189
189
  "name": "TriggerLimit"
190
+ },
191
+ {
192
+ "name": "Oracle"
190
193
  }
191
194
  ]
192
195
  }
@@ -358,9 +358,10 @@ class DriftClient {
358
358
  });
359
359
  }
360
360
  async updateUserMarginTradingEnabled(marginTradingEnabled, subAccountId = 0) {
361
+ const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, subAccountId);
361
362
  return await this.program.rpc.updateUserMarginTradingEnabled(subAccountId, marginTradingEnabled, {
362
363
  accounts: {
363
- user: await this.getUserAccountPublicKey(),
364
+ user: userAccountPublicKey,
364
365
  authority: this.wallet.publicKey,
365
366
  },
366
367
  });
@@ -63,7 +63,7 @@ class EventSubscriber {
63
63
  this.awaitTxResolver.get(txSig)();
64
64
  this.awaitTxResolver.delete(txSig);
65
65
  }
66
- if (slot > this.lastSeenSlot) {
66
+ if (!this.lastSeenSlot || slot > this.lastSeenSlot) {
67
67
  this.lastSeenTxSig = txSig;
68
68
  }
69
69
  if (this.lastSeenBlockTime === undefined ||
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.4.0",
2
+ "version": "2.6.0-beta.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -87,9 +87,7 @@ function calculateClaimablePnl(market, spotMarket, perpPosition, oraclePriceData
87
87
  let unsettledPnl = unrealizedPnl.add(fundingPnL);
88
88
  if (unrealizedPnl.gt(numericConstants_1.ZERO)) {
89
89
  const excessPnlPool = __1.BN.max(numericConstants_1.ZERO, (0, market_1.calculateNetUserPnlImbalance)(market, spotMarket, oraclePriceData).mul(new __1.BN(-1)));
90
- const maxPositivePnl = __1.BN.max(perpPosition.quoteAssetAmount
91
- .sub(perpPosition.quoteEntryAmount)
92
- .add(excessPnlPool), numericConstants_1.ZERO);
90
+ const maxPositivePnl = __1.BN.max(perpPosition.quoteAssetAmount.sub(perpPosition.quoteEntryAmount), numericConstants_1.ZERO).add(excessPnlPool);
93
91
  unsettledPnl = __1.BN.min(maxPositivePnl, unrealizedPnl);
94
92
  }
95
93
  return unsettledPnl;
package/lib/types.d.ts CHANGED
@@ -855,6 +855,7 @@ export declare type UserAccount = {
855
855
  cumulativePerpFunding: BN;
856
856
  liquidationMarginFreed: BN;
857
857
  liquidationStartSlot: BN;
858
+ isMarginTradingEnabled: boolean;
858
859
  };
859
860
  export declare type SpotPosition = {
860
861
  marketIndex: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.4.0",
3
+ "version": "2.6.0-beta.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -57,7 +57,7 @@
57
57
  "prettier": "^2.4.1",
58
58
  "ts-node": "^10.8.0"
59
59
  },
60
- "description": "SDK for Drift Protocol v1",
60
+ "description": "SDK for Drift Protocol",
61
61
  "engines": {
62
62
  "node": ">=12"
63
63
  }
package/src/dlob/DLOB.ts CHANGED
@@ -270,8 +270,12 @@ export class DLOB {
270
270
  return;
271
271
  }
272
272
 
273
- order.baseAssetAmountFilled = cumulativeBaseAssetAmountFilled;
274
- this.getListForOrder(order)?.update(order, userAccount);
273
+ const newOrder = {
274
+ ...order,
275
+ };
276
+ newOrder.baseAssetAmountFilled = cumulativeBaseAssetAmountFilled;
277
+
278
+ this.getListForOrder(order)?.update(newOrder, userAccount);
275
279
 
276
280
  if (onUpdate) {
277
281
  onUpdate();
@@ -187,6 +187,9 @@
187
187
  },
188
188
  {
189
189
  "name": "TriggerLimit"
190
+ },
191
+ {
192
+ "name": "Oracle"
190
193
  }
191
194
  ]
192
195
  }
@@ -590,12 +590,17 @@ export class DriftClient {
590
590
  marginTradingEnabled: boolean,
591
591
  subAccountId = 0
592
592
  ): Promise<TransactionSignature> {
593
+ const userAccountPublicKey = getUserAccountPublicKeySync(
594
+ this.program.programId,
595
+ this.wallet.publicKey,
596
+ subAccountId
597
+ );
593
598
  return await this.program.rpc.updateUserMarginTradingEnabled(
594
599
  subAccountId,
595
600
  marginTradingEnabled,
596
601
  {
597
602
  accounts: {
598
- user: await this.getUserAccountPublicKey(),
603
+ user: userAccountPublicKey,
599
604
  authority: this.wallet.publicKey,
600
605
  },
601
606
  }
@@ -109,7 +109,7 @@ export class EventSubscriber {
109
109
  this.awaitTxResolver.delete(txSig);
110
110
  }
111
111
 
112
- if (slot > this.lastSeenSlot) {
112
+ if (!this.lastSeenSlot || slot > this.lastSeenSlot) {
113
113
  this.lastSeenTxSig = txSig;
114
114
  }
115
115
 
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.4.0",
2
+ "version": "2.6.0-beta.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -151,11 +151,9 @@ export function calculateClaimablePnl(
151
151
  );
152
152
 
153
153
  const maxPositivePnl = BN.max(
154
- perpPosition.quoteAssetAmount
155
- .sub(perpPosition.quoteEntryAmount)
156
- .add(excessPnlPool),
154
+ perpPosition.quoteAssetAmount.sub(perpPosition.quoteEntryAmount),
157
155
  ZERO
158
- );
156
+ ).add(excessPnlPool);
159
157
 
160
158
  unsettledPnl = BN.min(maxPositivePnl, unrealizedPnl);
161
159
  }
package/src/types.ts CHANGED
@@ -795,6 +795,7 @@ export type UserAccount = {
795
795
  cumulativePerpFunding: BN;
796
796
  liquidationMarginFreed: BN;
797
797
  liquidationStartSlot: BN;
798
+ isMarginTradingEnabled: boolean;
798
799
  };
799
800
 
800
801
  export type SpotPosition = {