@drift-labs/sdk 2.145.0-beta.3 → 2.146.0-alpha.13

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.
Files changed (99) hide show
  1. package/.env +4 -0
  2. package/VERSION +1 -1
  3. package/lib/browser/accounts/grpcMultiUserAccountSubscriber.js +8 -1
  4. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.d.ts +99 -7
  5. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.js +435 -144
  6. package/lib/browser/adminClient.d.ts +5 -1
  7. package/lib/browser/adminClient.js +57 -23
  8. package/lib/browser/constants/numericConstants.d.ts +2 -0
  9. package/lib/browser/constants/numericConstants.js +5 -1
  10. package/lib/browser/constants/perpMarkets.js +0 -2
  11. package/lib/browser/decode/user.js +4 -0
  12. package/lib/browser/driftClient.d.ts +25 -10
  13. package/lib/browser/driftClient.js +238 -41
  14. package/lib/browser/driftClientConfig.d.ts +7 -2
  15. package/lib/browser/idl/drift.json +245 -22
  16. package/lib/browser/index.d.ts +4 -0
  17. package/lib/browser/index.js +9 -1
  18. package/lib/browser/marginCalculation.d.ts +86 -0
  19. package/lib/browser/marginCalculation.js +209 -0
  20. package/lib/browser/math/margin.d.ts +1 -1
  21. package/lib/browser/math/margin.js +8 -1
  22. package/lib/browser/math/position.d.ts +1 -0
  23. package/lib/browser/math/position.js +10 -2
  24. package/lib/browser/math/spotPosition.d.ts +1 -1
  25. package/lib/browser/math/spotPosition.js +3 -2
  26. package/lib/browser/math/superStake.d.ts +3 -2
  27. package/lib/browser/types.d.ts +13 -0
  28. package/lib/browser/types.js +12 -1
  29. package/lib/browser/user.d.ts +59 -11
  30. package/lib/browser/user.js +348 -43
  31. package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts.map +1 -1
  32. package/lib/node/accounts/grpcMultiUserAccountSubscriber.js +8 -1
  33. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +99 -7
  34. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +1 -1
  35. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.js +435 -144
  36. package/lib/node/adminClient.d.ts +5 -1
  37. package/lib/node/adminClient.d.ts.map +1 -1
  38. package/lib/node/adminClient.js +57 -23
  39. package/lib/node/constants/numericConstants.d.ts +2 -0
  40. package/lib/node/constants/numericConstants.d.ts.map +1 -1
  41. package/lib/node/constants/numericConstants.js +5 -1
  42. package/lib/node/constants/perpMarkets.d.ts.map +1 -1
  43. package/lib/node/constants/perpMarkets.js +0 -2
  44. package/lib/node/decode/user.d.ts.map +1 -1
  45. package/lib/node/decode/user.js +4 -0
  46. package/lib/node/driftClient.d.ts +25 -10
  47. package/lib/node/driftClient.d.ts.map +1 -1
  48. package/lib/node/driftClient.js +238 -41
  49. package/lib/node/driftClientConfig.d.ts +7 -2
  50. package/lib/node/driftClientConfig.d.ts.map +1 -1
  51. package/lib/node/idl/drift.json +245 -22
  52. package/lib/node/index.d.ts +4 -0
  53. package/lib/node/index.d.ts.map +1 -1
  54. package/lib/node/index.js +9 -1
  55. package/lib/node/marginCalculation.d.ts +87 -0
  56. package/lib/node/marginCalculation.d.ts.map +1 -0
  57. package/lib/node/marginCalculation.js +209 -0
  58. package/lib/node/math/margin.d.ts +1 -1
  59. package/lib/node/math/margin.d.ts.map +1 -1
  60. package/lib/node/math/margin.js +8 -1
  61. package/lib/node/math/position.d.ts +1 -0
  62. package/lib/node/math/position.d.ts.map +1 -1
  63. package/lib/node/math/position.js +10 -2
  64. package/lib/node/math/spotPosition.d.ts +1 -1
  65. package/lib/node/math/spotPosition.d.ts.map +1 -1
  66. package/lib/node/math/spotPosition.js +3 -2
  67. package/lib/node/math/superStake.d.ts +3 -2
  68. package/lib/node/math/superStake.d.ts.map +1 -1
  69. package/lib/node/types.d.ts +13 -0
  70. package/lib/node/types.d.ts.map +1 -1
  71. package/lib/node/types.js +12 -1
  72. package/lib/node/user.d.ts +59 -11
  73. package/lib/node/user.d.ts.map +1 -1
  74. package/lib/node/user.js +348 -43
  75. package/package.json +1 -1
  76. package/scripts/deposit-isolated-positions.ts +110 -0
  77. package/scripts/single-grpc-client-test.ts +71 -21
  78. package/scripts/withdraw-isolated-positions.ts +174 -0
  79. package/src/accounts/grpcMultiUserAccountSubscriber.ts +8 -1
  80. package/src/accounts/webSocketProgramAccountSubscriberV2.ts +566 -167
  81. package/src/adminClient.ts +74 -25
  82. package/src/constants/numericConstants.ts +5 -0
  83. package/src/constants/perpMarkets.ts +0 -3
  84. package/src/decode/user.ts +7 -1
  85. package/src/driftClient.ts +465 -52
  86. package/src/driftClientConfig.ts +15 -8
  87. package/src/idl/drift.json +246 -23
  88. package/src/index.ts +4 -0
  89. package/src/margin/README.md +143 -0
  90. package/src/marginCalculation.ts +306 -0
  91. package/src/math/margin.ts +13 -1
  92. package/src/math/position.ts +12 -2
  93. package/src/math/spotPosition.ts +6 -2
  94. package/src/types.ts +16 -0
  95. package/src/user.ts +623 -81
  96. package/tests/amm/test.ts +1 -1
  97. package/tests/dlob/helpers.ts +6 -3
  98. package/tests/user/getMarginCalculation.ts +405 -0
  99. package/tests/user/test.ts +0 -7
@@ -540,8 +540,8 @@ export class AdminClient extends DriftClient {
540
540
  curveUpdateIntensity,
541
541
  ammJitIntensity,
542
542
  name,
543
- mustInitializeAmmCache,
544
- lpPoolId
543
+ lpPoolId,
544
+ mustInitializeAmmCache
545
545
  );
546
546
  const tx = await this.buildTransaction(initializeMarketIxs);
547
547
 
@@ -588,8 +588,8 @@ export class AdminClient extends DriftClient {
588
588
  curveUpdateIntensity = 0,
589
589
  ammJitIntensity = 0,
590
590
  name = DEFAULT_MARKET_NAME,
591
- includeInitAmmCacheIx = false,
592
- lpPoolId: number = 0
591
+ lpPoolId: number = 0,
592
+ includeInitAmmCacheIx = false
593
593
  ): Promise<TransactionInstruction[]> {
594
594
  const perpMarketPublicKey = await getPerpMarketPublicKey(
595
595
  this.program.programId,
@@ -597,6 +597,7 @@ export class AdminClient extends DriftClient {
597
597
  );
598
598
 
599
599
  const ixs: TransactionInstruction[] = [];
600
+
600
601
  if (includeInitAmmCacheIx) {
601
602
  ixs.push(await this.getInitializeAmmCacheIx());
602
603
  }
@@ -663,9 +664,35 @@ export class AdminClient extends DriftClient {
663
664
  return await this.program.instruction.initializeAmmCache({
664
665
  accounts: {
665
666
  state: await this.getStatePublicKey(),
666
- admin: this.isSubscribed
667
- ? this.getStateAccount().admin
668
- : this.wallet.publicKey,
667
+ admin: this.useHotWalletAdmin
668
+ ? this.wallet.publicKey
669
+ : this.getStateAccount().admin,
670
+ ammCache: getAmmCachePublicKey(this.program.programId),
671
+ rent: SYSVAR_RENT_PUBKEY,
672
+ systemProgram: anchor.web3.SystemProgram.programId,
673
+ },
674
+ });
675
+ }
676
+
677
+ public async resizeAmmCache(
678
+ txParams?: TxParams
679
+ ): Promise<TransactionSignature> {
680
+ const initializeAmmCacheIx = await this.getResizeAmmCacheIx();
681
+
682
+ const tx = await this.buildTransaction(initializeAmmCacheIx, txParams);
683
+
684
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
685
+
686
+ return txSig;
687
+ }
688
+
689
+ public async getResizeAmmCacheIx(): Promise<TransactionInstruction> {
690
+ return await this.program.instruction.resizeAmmCache({
691
+ accounts: {
692
+ state: await this.getStatePublicKey(),
693
+ admin: this.useHotWalletAdmin
694
+ ? this.wallet.publicKey
695
+ : this.getStateAccount().admin,
669
696
  ammCache: getAmmCachePublicKey(this.program.programId),
670
697
  rent: SYSVAR_RENT_PUBKEY,
671
698
  systemProgram: anchor.web3.SystemProgram.programId,
@@ -673,6 +700,28 @@ export class AdminClient extends DriftClient {
673
700
  });
674
701
  }
675
702
 
703
+ public async deleteAmmCache(
704
+ txParams?: TxParams
705
+ ): Promise<TransactionSignature> {
706
+ const deleteAmmCacheIx = await this.getDeleteAmmCacheIx();
707
+
708
+ const tx = await this.buildTransaction(deleteAmmCacheIx, txParams);
709
+
710
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
711
+
712
+ return txSig;
713
+ }
714
+
715
+ public async getDeleteAmmCacheIx(): Promise<TransactionInstruction> {
716
+ return await this.program.instruction.deleteAmmCache({
717
+ accounts: {
718
+ state: await this.getStatePublicKey(),
719
+ admin: this.getStateAccount().admin,
720
+ ammCache: getAmmCachePublicKey(this.program.programId),
721
+ },
722
+ });
723
+ }
724
+
676
725
  public async updateInitialAmmCacheInfo(
677
726
  perpMarketIndexes: number[],
678
727
  txParams?: TxParams
@@ -699,9 +748,9 @@ export class AdminClient extends DriftClient {
699
748
  return await this.program.instruction.updateInitialAmmCacheInfo({
700
749
  accounts: {
701
750
  state: await this.getStatePublicKey(),
702
- admin: this.isSubscribed
703
- ? this.getStateAccount().admin
704
- : this.wallet.publicKey,
751
+ admin: this.useHotWalletAdmin
752
+ ? this.wallet.publicKey
753
+ : this.getStateAccount().admin,
705
754
  ammCache: getAmmCachePublicKey(this.program.programId),
706
755
  },
707
756
  remainingAccounts,
@@ -759,9 +808,9 @@ export class AdminClient extends DriftClient {
759
808
  {
760
809
  accounts: {
761
810
  state: await this.getStatePublicKey(),
762
- admin: this.isSubscribed
763
- ? this.getStateAccount().admin
764
- : this.wallet.publicKey,
811
+ admin: this.useHotWalletAdmin
812
+ ? this.wallet.publicKey
813
+ : this.getStateAccount().admin,
765
814
  ammCache: getAmmCachePublicKey(this.program.programId),
766
815
  },
767
816
  }
@@ -783,9 +832,9 @@ export class AdminClient extends DriftClient {
783
832
  return this.program.instruction.resetAmmCache({
784
833
  accounts: {
785
834
  state: await this.getStatePublicKey(),
786
- admin: this.isSubscribed
787
- ? this.getStateAccount().admin
788
- : this.wallet.publicKey,
835
+ admin: this.useHotWalletAdmin
836
+ ? this.wallet.publicKey
837
+ : this.getStateAccount().admin,
789
838
  ammCache: getAmmCachePublicKey(this.program.programId),
790
839
  systemProgram: anchor.web3.SystemProgram.programId,
791
840
  },
@@ -5508,9 +5557,9 @@ export class AdminClient extends DriftClient {
5508
5557
  {
5509
5558
  accounts: {
5510
5559
  constituent,
5511
- admin: this.isSubscribed
5512
- ? this.getStateAccount().admin
5513
- : this.wallet.publicKey,
5560
+ admin: this.useHotWalletAdmin
5561
+ ? this.wallet.publicKey
5562
+ : this.getStateAccount().admin,
5514
5563
  state: await this.getStatePublicKey(),
5515
5564
  },
5516
5565
  }
@@ -6398,9 +6447,9 @@ export class AdminClient extends DriftClient {
6398
6447
  lpExchangeFeeExcluscionScalar ?? null,
6399
6448
  {
6400
6449
  accounts: {
6401
- admin: this.isSubscribed
6402
- ? this.getStateAccount().admin
6403
- : this.wallet.publicKey,
6450
+ admin: this.useHotWalletAdmin
6451
+ ? this.wallet.publicKey
6452
+ : this.getStateAccount().admin,
6404
6453
  state: await this.getStatePublicKey(),
6405
6454
  perpMarket: this.getPerpMarketAccount(marketIndex).pubkey,
6406
6455
  },
@@ -6429,9 +6478,9 @@ export class AdminClient extends DriftClient {
6429
6478
  pausedOperations,
6430
6479
  {
6431
6480
  accounts: {
6432
- admin: this.isSubscribed
6433
- ? this.getStateAccount().admin
6434
- : this.wallet.publicKey,
6481
+ admin: this.useHotWalletAdmin
6482
+ ? this.wallet.publicKey
6483
+ : this.getStateAccount().admin,
6435
6484
  state: await this.getStatePublicKey(),
6436
6485
  perpMarket: this.getPerpMarketAccount(marketIndex).pubkey,
6437
6486
  },
@@ -1,5 +1,6 @@
1
1
  import { LAMPORTS_PER_SOL } from '@solana/web3.js';
2
2
  import { BN } from '@coral-xyz/anchor';
3
+ import { BigNum } from '../factory/bigNum';
3
4
 
4
5
  export const ZERO = new BN(0);
5
6
  export const ONE = new BN(1);
@@ -116,3 +117,7 @@ export const FUEL_START_TS = new BN(1723147200); // unix timestamp
116
117
  export const MAX_PREDICTION_PRICE = PRICE_PRECISION;
117
118
 
118
119
  export const GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
120
+
121
+ // integer constants
122
+ export const MAX_I64 = BigNum.fromPrint('9223372036854775807').val;
123
+ export const MIN_I64 = BigNum.fromPrint('-9223372036854775808').val;
@@ -1297,9 +1297,6 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
1297
1297
  oracle: new PublicKey('GAzR3C5cn7gGVvuqJB57wSYTPWP3n2Lw4mRJRxvTvqYy'),
1298
1298
  launchTs: 1747318237000,
1299
1299
  oracleSource: OracleSource.PYTH_LAZER,
1300
- pythFeedId:
1301
- '0x6d74813ee17291d5be18a355fe4d43fd300d625caea6554d49f740e7d112141e',
1302
- pythLazerId: 1571,
1303
1300
  },
1304
1301
  {
1305
1302
  fullName: 'PUMP',
@@ -84,6 +84,11 @@ export function decodeUser(buffer: Buffer): UserAccount {
84
84
  const quoteAssetAmount = readSignedBigInt64LE(buffer, offset + 16);
85
85
  const lpShares = readUnsignedBigInt64LE(buffer, offset + 64);
86
86
  const openOrders = buffer.readUInt8(offset + 94);
87
+ const positionFlag = buffer.readUInt8(offset + 95);
88
+ const isolatedPositionScaledBalance = readUnsignedBigInt64LE(
89
+ buffer,
90
+ offset + 96
91
+ );
87
92
 
88
93
  if (
89
94
  baseAssetAmount.eq(ZERO) &&
@@ -117,7 +122,6 @@ export function decodeUser(buffer: Buffer): UserAccount {
117
122
  offset += 3;
118
123
  const perLpBase = buffer.readUInt8(offset);
119
124
  offset += 1;
120
-
121
125
  perpPositions.push({
122
126
  lastCumulativeFundingRate,
123
127
  baseAssetAmount,
@@ -135,6 +139,8 @@ export function decodeUser(buffer: Buffer): UserAccount {
135
139
  openOrders,
136
140
  perLpBase,
137
141
  maxMarginRatio,
142
+ positionFlag,
143
+ isolatedPositionScaledBalance,
138
144
  });
139
145
  }
140
146