@drift-labs/sdk 2.42.0-beta.9 → 2.43.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/user.js CHANGED
@@ -670,9 +670,7 @@ class User {
670
670
  * @returns : number (value from [0, 100])
671
671
  */
672
672
  getHealth() {
673
- const userAccount = this.getUserAccount();
674
- if ((0, types_1.isVariant)(userAccount.status, 'beingLiquidated') ||
675
- (0, types_1.isVariant)(userAccount.status, 'bankrupt')) {
673
+ if (this.isBeingLiquidated()) {
676
674
  return 0;
677
675
  }
678
676
  const totalCollateral = this.getTotalCollateral('Maintenance');
@@ -1004,13 +1002,12 @@ class User {
1004
1002
  };
1005
1003
  }
1006
1004
  isBeingLiquidated() {
1007
- return (0, types_1.isOneOfVariant)(this.getUserAccount().status, [
1008
- 'beingLiquidated',
1009
- 'bankrupt',
1010
- ]);
1005
+ return ((this.getUserAccount().status &
1006
+ (types_1.UserStatus.BEING_LIQUIDATED | types_1.UserStatus.BANKRUPT)) >
1007
+ 0);
1011
1008
  }
1012
1009
  isBankrupt() {
1013
- return (0, types_1.isVariant)(this.getUserAccount().status, 'bankrupt');
1010
+ return (this.getUserAccount().status & types_1.UserStatus.BANKRUPT) > 0;
1014
1011
  }
1015
1012
  /**
1016
1013
  * Checks if any user position cumulative funding differs from respective market cumulative funding
@@ -1335,7 +1332,7 @@ class User {
1335
1332
  const outContributionInitial = this.calculateSpotPositionFreeCollateralContribution(outSpotPosition, outStrictOraclePrice);
1336
1333
  const { totalAssetValue: outTotalAssetValueInitial, totalLiabilityValue: outTotalLiabilityValueInitial, } = this.calculateSpotPositionLeverageContribution(outSpotPosition, outStrictOraclePrice);
1337
1334
  const initialContribution = inContributionInitial.add(outContributionInitial);
1338
- const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } = this.getLeverageComponents(undefined, 'Initial');
1335
+ const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } = this.getLeverageComponents();
1339
1336
  if (!calculateSwap) {
1340
1337
  calculateSwap = (inSwap) => {
1341
1338
  return inSwap
@@ -1490,7 +1487,7 @@ class User {
1490
1487
  this.getEmptySpotPosition(outMarketIndex);
1491
1488
  const { totalAssetValue: inTotalAssetValueInitial, totalLiabilityValue: inTotalLiabilityValueInitial, } = this.calculateSpotPositionLeverageContribution(inSpotPosition, inStrictOraclePrice);
1492
1489
  const { totalAssetValue: outTotalAssetValueInitial, totalLiabilityValue: outTotalLiabilityValueInitial, } = this.calculateSpotPositionLeverageContribution(outSpotPosition, outStrictOraclePrice);
1493
- const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } = this.getLeverageComponents(undefined, 'Initial');
1490
+ const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } = this.getLeverageComponents();
1494
1491
  const inPositionAfter = this.cloneAndUpdateSpotPosition(inSpotPosition, inAmount.abs().neg(), inMarket);
1495
1492
  const outPositionAfter = this.cloneAndUpdateSpotPosition(outSpotPosition, outAmount.abs(), outMarket);
1496
1493
  const { totalAssetValue: inTotalAssetValueAfter, totalLiabilityValue: inTotalLiabilityValueAfter, } = this.calculateSpotPositionLeverageContribution(inPositionAfter, inStrictOraclePrice);
@@ -1741,11 +1738,20 @@ class User {
1741
1738
  depositAmount,
1742
1739
  };
1743
1740
  }
1744
- canMakeIdle(slot, slotsBeforeIdle) {
1741
+ canMakeIdle(slot) {
1745
1742
  const userAccount = this.getUserAccount();
1746
1743
  if (userAccount.idle) {
1747
1744
  return false;
1748
1745
  }
1746
+ const { totalAssetValue, totalLiabilityValue } = this.getSpotMarketAssetAndLiabilityValue();
1747
+ const equity = totalAssetValue.sub(totalLiabilityValue);
1748
+ let slotsBeforeIdle;
1749
+ if (equity.lt(numericConstants_1.QUOTE_PRECISION)) {
1750
+ slotsBeforeIdle = new _1.BN(9000); // 1 hour
1751
+ }
1752
+ else {
1753
+ slotsBeforeIdle = new _1.BN(1512000); // 1 week
1754
+ }
1749
1755
  const userLastActiveSlot = userAccount.lastActiveSlot;
1750
1756
  const slotsSinceLastActive = slot.sub(userLastActiveSlot);
1751
1757
  if (slotsSinceLastActive.lt(slotsBeforeIdle)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.42.0-beta.9",
3
+ "version": "2.43.0-beta.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -19,7 +19,7 @@ export class BulkAccountLoader {
19
19
  accountsToLoad = new Map<string, AccountToLoad>();
20
20
  bufferAndSlotMap = new Map<string, BufferAndSlot>();
21
21
  errorCallbacks = new Map<string, (e) => void>();
22
- intervalId?: NodeJS.Timer;
22
+ intervalId?: ReturnType<typeof setTimeout>;
23
23
  // to handle clients spamming load
24
24
  loadPromise?: Promise<void>;
25
25
  loadPromiseResolver: () => void;
package/src/config.ts CHANGED
@@ -26,6 +26,7 @@ type DriftConfig = {
26
26
  PERP_MARKETS: PerpMarketConfig[];
27
27
  SPOT_MARKETS: SpotMarketConfig[];
28
28
  MARKET_LOOKUP_TABLE: string;
29
+ SERUM_LOOKUP_TABLE?: string;
29
30
  };
30
31
 
31
32
  export type DriftEnv = 'devnet' | 'mainnet-beta';
@@ -60,6 +61,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
60
61
  PERP_MARKETS: MainnetPerpMarkets,
61
62
  SPOT_MARKETS: MainnetSpotMarkets,
62
63
  MARKET_LOOKUP_TABLE: 'D9cnvzswDikQDf53k4HpQ3KJ9y1Fv3HGGDFYMXnK5T6c',
64
+ SERUM_LOOKUP_TABLE: 'GPZkp76cJtNL2mphCvT6FXkJCVPpouidnacckR6rzKDN',
63
65
  },
64
66
  };
65
67
 
@@ -161,7 +161,27 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
161
161
  baseAssetSymbol: 'HNT',
162
162
  marketIndex: 14,
163
163
  oracle: new PublicKey('6Eg8YdfFJQF2HHonzPUBSCCmyUEhrStg9VBLK957sBe6'),
164
- launchTs: 16922949550000,
164
+ launchTs: 1692294955000,
165
+ oracleSource: OracleSource.PYTH,
166
+ },
167
+ {
168
+ fullName: 'INJ',
169
+ category: ['L1', 'Exchange'],
170
+ symbol: 'INJ-PERP',
171
+ baseAssetSymbol: 'INJ',
172
+ marketIndex: 15,
173
+ oracle: new PublicKey('44uRsNnT35kjkscSu59MxRr9CfkLZWf6gny8bWqUbVxE'),
174
+ launchTs: 1698074659000,
175
+ oracleSource: OracleSource.PYTH,
176
+ },
177
+ {
178
+ fullName: 'LINK',
179
+ category: ['Oracle'],
180
+ symbol: 'LINK-PERP',
181
+ baseAssetSymbol: 'LINK',
182
+ marketIndex: 16,
183
+ oracle: new PublicKey('9sGidS4qUXS2WvHZFhzw4df1jNd5TvUGZXZVsSjXo7UF'),
184
+ launchTs: 1698074659000,
165
185
  oracleSource: OracleSource.PYTH,
166
186
  },
167
187
  ];
@@ -314,7 +334,27 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
314
334
  baseAssetSymbol: 'HNT',
315
335
  marketIndex: 14,
316
336
  oracle: new PublicKey('7moA1i5vQUpfDwSpK6Pw9s56ahB7WFGidtbL2ujWrVvm'),
317
- launchTs: 16922949550000,
337
+ launchTs: 1692294955000,
338
+ oracleSource: OracleSource.PYTH,
339
+ },
340
+ {
341
+ fullName: 'INJ',
342
+ category: ['L1', 'Exchange'],
343
+ symbol: 'INJ-PERP',
344
+ baseAssetSymbol: 'INJ',
345
+ marketIndex: 15,
346
+ oracle: new PublicKey('9EdtbaivHQYA4Nh3XzGR6DwRaoorqXYnmpfsnFhvwuVj'),
347
+ launchTs: 1698074659000,
348
+ oracleSource: OracleSource.PYTH,
349
+ },
350
+ {
351
+ fullName: 'LINK',
352
+ category: ['Oracle'],
353
+ symbol: 'LINK-PERP',
354
+ baseAssetSymbol: 'LINK',
355
+ marketIndex: 16,
356
+ oracle: new PublicKey('ALdkqQDMfHNg77oCNskfX751kHys4KE7SFuZzuKaN536'),
357
+ launchTs: 1698074659000,
318
358
  oracleSource: OracleSource.PYTH,
319
359
  },
320
360
  ];
@@ -875,21 +875,48 @@ export class DriftClient {
875
875
  }
876
876
 
877
877
  public async updateUserCustomMarginRatio(
878
+ updates: { marginRatio: number; subAccountId: number }[]
879
+ ): Promise<TransactionSignature> {
880
+ const ixs = await Promise.all(
881
+ updates.map(async ({ marginRatio, subAccountId }) => {
882
+ const ix = await this.getUpdateUserCustomMarginRatioIx(
883
+ marginRatio,
884
+ subAccountId
885
+ );
886
+ return ix;
887
+ })
888
+ );
889
+
890
+ const tx = await this.buildTransaction(ixs, this.txParams);
891
+
892
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
893
+ return txSig;
894
+ }
895
+
896
+ public async getUpdateUserCustomMarginRatioIx(
878
897
  marginRatio: number,
879
898
  subAccountId = 0
880
- ): Promise<TransactionSignature> {
881
- const tx = await this.program.transaction.updateUserCustomMarginRatio(
899
+ ): Promise<TransactionInstruction> {
900
+ const userAccountPublicKey = getUserAccountPublicKeySync(
901
+ this.program.programId,
902
+ this.wallet.publicKey,
903
+ subAccountId
904
+ );
905
+
906
+ await this.addUser(subAccountId, this.wallet.publicKey);
907
+
908
+ const ix = this.program.instruction.updateUserCustomMarginRatio(
882
909
  subAccountId,
883
910
  marginRatio,
884
911
  {
885
912
  accounts: {
886
- user: await this.getUserAccountPublicKey(),
913
+ user: userAccountPublicKey,
887
914
  authority: this.wallet.publicKey,
888
915
  },
889
916
  }
890
917
  );
891
- const { txSig } = await this.sendTransaction(tx, [], this.opts);
892
- return txSig;
918
+
919
+ return ix;
893
920
  }
894
921
 
895
922
  public async getUpdateUserMarginTradingEnabledIx(
@@ -930,31 +957,18 @@ export class DriftClient {
930
957
  }
931
958
 
932
959
  public async updateUserMarginTradingEnabled(
933
- marginTradingEnabled: boolean,
934
- subAccountId = 0
960
+ updates: { marginTradingEnabled: boolean; subAccountId: number }[]
935
961
  ): Promise<TransactionSignature> {
936
- const userAccountPublicKey = getUserAccountPublicKeySync(
937
- this.program.programId,
938
- this.wallet.publicKey,
939
- subAccountId
962
+ const ixs = await Promise.all(
963
+ updates.map(async ({ marginTradingEnabled, subAccountId }) => {
964
+ return await this.getUpdateUserMarginTradingEnabledIx(
965
+ marginTradingEnabled,
966
+ subAccountId
967
+ );
968
+ })
940
969
  );
941
970
 
942
- await this.addUser(subAccountId, this.wallet.publicKey);
943
- const remainingAccounts = this.getRemainingAccounts({
944
- userAccounts: [this.getUserAccount(subAccountId)],
945
- });
946
-
947
- const tx = await this.program.transaction.updateUserMarginTradingEnabled(
948
- subAccountId,
949
- marginTradingEnabled,
950
- {
951
- accounts: {
952
- user: userAccountPublicKey,
953
- authority: this.wallet.publicKey,
954
- },
955
- remainingAccounts,
956
- }
957
- );
971
+ const tx = await this.buildTransaction(ixs, this.txParams);
958
972
 
959
973
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
960
974
  return txSig;
@@ -150,6 +150,9 @@ export class LogParser {
150
150
 
151
151
  public parseEventsFromLogs(event: Log): WrappedEvents {
152
152
  const records: WrappedEvents = [];
153
+
154
+ if (!event.logs) return records;
155
+
153
156
  // @ts-ignore
154
157
  const eventGenerator = this.program._events._eventParser.parseLogs(
155
158
  event.logs,
@@ -10,7 +10,7 @@ import { fetchLogs } from './fetchLogs';
10
10
 
11
11
  export class PollingLogProvider implements LogProvider {
12
12
  private finality: Finality;
13
- private intervalId: NodeJS.Timer;
13
+ private intervalId: ReturnType<typeof setTimeout>;
14
14
  private mostRecentSeenTx?: TransactionSignature;
15
15
  private mutex: number;
16
16
  private firstFetch = true;