@drift-labs/sdk 2.49.0-beta.9 → 2.52.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.
Files changed (75) hide show
  1. package/VERSION +1 -1
  2. package/lib/accounts/{mockUserAccountSubscriber.d.ts → basicUserAccountSubscriber.d.ts} +6 -2
  3. package/lib/accounts/{mockUserAccountSubscriber.js → basicUserAccountSubscriber.js} +13 -6
  4. package/lib/accounts/bulkAccountLoader.js +7 -1
  5. package/lib/accounts/oneShotUserAccountSubscriber.d.ts +17 -0
  6. package/lib/accounts/oneShotUserAccountSubscriber.js +48 -0
  7. package/lib/accounts/pollingInsuranceFundStakeAccountSubscriber.js +1 -0
  8. package/lib/adminClient.d.ts +2 -0
  9. package/lib/adminClient.js +17 -0
  10. package/lib/constants/numericConstants.d.ts +3 -0
  11. package/lib/constants/numericConstants.js +4 -1
  12. package/lib/constants/perpMarkets.js +40 -0
  13. package/lib/constants/spotMarkets.js +10 -0
  14. package/lib/decode/user.d.ts +3 -0
  15. package/lib/decode/user.js +329 -0
  16. package/lib/driftClient.d.ts +6 -1
  17. package/lib/driftClient.js +35 -10
  18. package/lib/examples/loadDlob.js +10 -5
  19. package/lib/idl/drift.json +32 -2
  20. package/lib/index.d.ts +3 -1
  21. package/lib/index.js +3 -1
  22. package/lib/math/state.d.ts +5 -0
  23. package/lib/math/state.js +27 -0
  24. package/lib/math/superStake.d.ts +43 -0
  25. package/lib/math/superStake.js +64 -22
  26. package/lib/orderSubscriber/OrderSubscriber.d.ts +3 -0
  27. package/lib/orderSubscriber/OrderSubscriber.js +17 -3
  28. package/lib/orderSubscriber/WebsocketSubscription.d.ts +1 -1
  29. package/lib/orderSubscriber/WebsocketSubscription.js +8 -6
  30. package/lib/orderSubscriber/types.d.ts +4 -1
  31. package/lib/types.d.ts +2 -1
  32. package/lib/user.d.ts +2 -1
  33. package/lib/user.js +13 -5
  34. package/lib/userMap/PollingSubscription.d.ts +15 -0
  35. package/lib/userMap/PollingSubscription.js +28 -0
  36. package/lib/userMap/WebsocketSubscription.d.ts +23 -0
  37. package/lib/userMap/WebsocketSubscription.js +41 -0
  38. package/lib/userMap/userMap.d.ts +16 -18
  39. package/lib/userMap/userMap.js +73 -34
  40. package/lib/userMap/userMapConfig.d.ts +21 -0
  41. package/lib/userMap/userMapConfig.js +2 -0
  42. package/lib/userStats.d.ts +1 -0
  43. package/lib/userStats.js +3 -0
  44. package/package.json +2 -1
  45. package/src/accounts/{mockUserAccountSubscriber.ts → basicUserAccountSubscriber.ts} +12 -6
  46. package/src/accounts/bulkAccountLoader.ts +10 -3
  47. package/src/accounts/oneShotUserAccountSubscriber.ts +64 -0
  48. package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +1 -0
  49. package/src/adminClient.ts +27 -0
  50. package/src/constants/numericConstants.ts +4 -0
  51. package/src/constants/perpMarkets.ts +40 -0
  52. package/src/constants/spotMarkets.ts +10 -0
  53. package/src/decode/user.ts +358 -0
  54. package/src/driftClient.ts +62 -15
  55. package/src/examples/loadDlob.ts +11 -6
  56. package/src/idl/drift.json +33 -3
  57. package/src/index.ts +3 -1
  58. package/src/math/state.ts +26 -0
  59. package/src/math/superStake.ts +108 -20
  60. package/src/orderSubscriber/OrderSubscriber.ts +33 -7
  61. package/src/orderSubscriber/WebsocketSubscription.ts +17 -16
  62. package/src/orderSubscriber/types.ts +15 -2
  63. package/src/types.ts +2 -1
  64. package/src/user.ts +19 -6
  65. package/src/userMap/PollingSubscription.ts +48 -0
  66. package/src/userMap/WebsocketSubscription.ts +76 -0
  67. package/src/userMap/userMap.ts +105 -70
  68. package/src/userMap/userMapConfig.ts +34 -0
  69. package/src/userStats.ts +8 -0
  70. package/tests/amm/test.ts +6 -3
  71. package/tests/decode/test.ts +266 -0
  72. package/tests/decode/userAccountBufferStrings.ts +102 -0
  73. package/tests/dlob/helpers.ts +1 -0
  74. package/tests/dlob/test.ts +10 -8
  75. package/tests/user/helpers.ts +0 -1
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.49.0-beta.9
1
+ 2.52.0-beta.1
@@ -4,14 +4,18 @@ import { PublicKey } from '@solana/web3.js';
4
4
  import StrictEventEmitter from 'strict-event-emitter-types';
5
5
  import { EventEmitter } from 'events';
6
6
  import { UserAccount } from '../types';
7
- export declare class MockUserAccountSubscriber implements UserAccountSubscriber {
7
+ /**
8
+ * Basic implementation of UserAccountSubscriber. It will only take in UserAccount
9
+ * data during initialization and will not fetch or subscribe to updates.
10
+ */
11
+ export declare class BasicUserAccountSubscriber implements UserAccountSubscriber {
8
12
  isSubscribed: boolean;
9
13
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
10
14
  userAccountPublicKey: PublicKey;
11
15
  callbackId?: string;
12
16
  errorCallbackId?: string;
13
17
  user: DataAndSlot<UserAccount>;
14
- constructor(userAccountPublicKey: PublicKey, data: UserAccount, slot: number);
18
+ constructor(userAccountPublicKey: PublicKey, data?: UserAccount, slot?: number);
15
19
  subscribe(_userAccount?: UserAccount): Promise<boolean>;
16
20
  addToAccountLoader(): Promise<void>;
17
21
  fetch(): Promise<void>;
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MockUserAccountSubscriber = void 0;
3
+ exports.BasicUserAccountSubscriber = void 0;
4
4
  const events_1 = require("events");
5
- class MockUserAccountSubscriber {
5
+ /**
6
+ * Basic implementation of UserAccountSubscriber. It will only take in UserAccount
7
+ * data during initialization and will not fetch or subscribe to updates.
8
+ */
9
+ class BasicUserAccountSubscriber {
6
10
  constructor(userAccountPublicKey, data, slot) {
7
11
  this.isSubscribed = true;
8
12
  this.eventEmitter = new events_1.EventEmitter();
@@ -23,9 +27,12 @@ class MockUserAccountSubscriber {
23
27
  return this.user;
24
28
  }
25
29
  updateData(userAccount, slot) {
26
- this.user = { data: userAccount, slot };
27
- this.eventEmitter.emit('userAccountUpdate', userAccount);
28
- this.eventEmitter.emit('update');
30
+ var _a;
31
+ if (!this.user || slot >= ((_a = this.user.slot) !== null && _a !== void 0 ? _a : 0)) {
32
+ this.user = { data: userAccount, slot };
33
+ this.eventEmitter.emit('userAccountUpdate', userAccount);
34
+ this.eventEmitter.emit('update');
35
+ }
29
36
  }
30
37
  }
31
- exports.MockUserAccountSubscriber = MockUserAccountSubscriber;
38
+ exports.BasicUserAccountSubscriber = BasicUserAccountSubscriber;
@@ -47,6 +47,7 @@ class BulkAccountLoader {
47
47
  if (existingAccountToLoad) {
48
48
  existingAccountToLoad.callbacks.delete(callbackId);
49
49
  if (existingAccountToLoad.callbacks.size === 0) {
50
+ this.bufferAndSlotMap.delete(publicKey.toString());
50
51
  this.accountsToLoad.delete(existingAccountToLoad.publicKey.toString());
51
52
  }
52
53
  }
@@ -107,7 +108,9 @@ class BulkAccountLoader {
107
108
  const requests = new Array();
108
109
  for (const accountsToLoadChunk of accountsToLoadChunks) {
109
110
  const args = [
110
- accountsToLoadChunk.map((accountToLoad) => {
111
+ accountsToLoadChunk
112
+ .filter((accountToLoad) => accountToLoad.callbacks.size > 0)
113
+ .map((accountToLoad) => {
111
114
  return accountToLoad.publicKey.toBase58();
112
115
  }),
113
116
  { commitment: this.commitment },
@@ -137,6 +140,9 @@ class BulkAccountLoader {
137
140
  }
138
141
  const accountsToLoad = accountsToLoadChunks[i];
139
142
  accountsToLoad.forEach((accountToLoad, j) => {
143
+ if (accountToLoad.callbacks.size === 0) {
144
+ return;
145
+ }
140
146
  const key = accountToLoad.publicKey.toBase58();
141
147
  const oldRPCResponse = this.bufferAndSlotMap.get(key);
142
148
  if (oldRPCResponse && newSlot < oldRPCResponse.slot) {
@@ -0,0 +1,17 @@
1
+ import { Commitment, PublicKey } from '@solana/web3.js';
2
+ import { UserAccount } from '../types';
3
+ import { BasicUserAccountSubscriber } from './basicUserAccountSubscriber';
4
+ import { Program } from '@coral-xyz/anchor';
5
+ /**
6
+ * Simple implementation of UserAccountSubscriber. It will fetch the UserAccount
7
+ * date on subscribe (or call to fetch) if no account data is provided on init.
8
+ * Expect to use only 1 RPC call unless you call fetch repeatedly.
9
+ */
10
+ export declare class OneShotUserAccountSubscriber extends BasicUserAccountSubscriber {
11
+ program: Program;
12
+ commitment: Commitment;
13
+ constructor(program: Program, userAccountPublicKey: PublicKey, data?: UserAccount, slot?: number, commitment?: Commitment);
14
+ subscribe(userAccount?: UserAccount): Promise<boolean>;
15
+ fetchIfUnloaded(): Promise<void>;
16
+ fetch(): Promise<void>;
17
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OneShotUserAccountSubscriber = void 0;
4
+ const basicUserAccountSubscriber_1 = require("./basicUserAccountSubscriber");
5
+ /**
6
+ * Simple implementation of UserAccountSubscriber. It will fetch the UserAccount
7
+ * date on subscribe (or call to fetch) if no account data is provided on init.
8
+ * Expect to use only 1 RPC call unless you call fetch repeatedly.
9
+ */
10
+ class OneShotUserAccountSubscriber extends basicUserAccountSubscriber_1.BasicUserAccountSubscriber {
11
+ constructor(program, userAccountPublicKey, data, slot, commitment) {
12
+ super(userAccountPublicKey, data, slot);
13
+ this.program = program;
14
+ this.commitment = commitment !== null && commitment !== void 0 ? commitment : 'confirmed';
15
+ }
16
+ async subscribe(userAccount) {
17
+ if (userAccount) {
18
+ this.user = { data: userAccount, slot: this.user.slot };
19
+ return true;
20
+ }
21
+ await this.fetchIfUnloaded();
22
+ if (this.doesAccountExist()) {
23
+ this.eventEmitter.emit('update');
24
+ }
25
+ return true;
26
+ }
27
+ async fetchIfUnloaded() {
28
+ if (this.user.data === undefined) {
29
+ await this.fetch();
30
+ }
31
+ }
32
+ async fetch() {
33
+ var _a, _b;
34
+ try {
35
+ const dataAndContext = await this.program.account.user.fetchAndContext(this.userAccountPublicKey, this.commitment);
36
+ if (dataAndContext.context.slot > ((_b = (_a = this.user) === null || _a === void 0 ? void 0 : _a.slot) !== null && _b !== void 0 ? _b : 0)) {
37
+ this.user = {
38
+ data: dataAndContext.data,
39
+ slot: dataAndContext.context.slot,
40
+ };
41
+ }
42
+ }
43
+ catch (e) {
44
+ console.error(`OneShotUserAccountSubscriber.fetch() UserAccount does not exist: ${e.message}`);
45
+ }
46
+ }
47
+ }
48
+ exports.OneShotUserAccountSubscriber = OneShotUserAccountSubscriber;
@@ -22,6 +22,7 @@ class PollingInsuranceFundStakeAccountSubscriber {
22
22
  };
23
23
  }
24
24
  await this.addToAccountLoader();
25
+ await this.fetchIfUnloaded();
25
26
  if (this.doesAccountExist()) {
26
27
  this.eventEmitter.emit('update');
27
28
  }
@@ -37,6 +37,7 @@ export declare class AdminClient extends DriftClient {
37
37
  updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
38
38
  updateStateSettlementDuration(settlementDuration: number): Promise<TransactionSignature>;
39
39
  updateStateMaxNumberOfSubAccounts(maxNumberOfSubAccounts: number): Promise<TransactionSignature>;
40
+ updateStateMaxInitializeUserFee(maxInitializeUserFee: number): Promise<TransactionSignature>;
40
41
  updateWithdrawGuardThreshold(spotMarketIndex: number, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
41
42
  updateSpotMarketIfFactor(spotMarketIndex: number, userIfFactor: BN, totalIfFactor: BN): Promise<TransactionSignature>;
42
43
  updateSpotMarketRevenueSettlePeriod(spotMarketIndex: number, revenueSettlePeriod: BN): Promise<TransactionSignature>;
@@ -53,6 +54,7 @@ export declare class AdminClient extends DriftClient {
53
54
  updateSpotMarketOracle(spotMarketIndex: number, oracle: PublicKey, oracleSource: OracleSource): Promise<TransactionSignature>;
54
55
  updateSpotMarketOrdersEnabled(spotMarketIndex: number, ordersEnabled: boolean): Promise<TransactionSignature>;
55
56
  updateSerumFulfillmentConfigStatus(serumFulfillmentConfig: PublicKey, status: SpotFulfillmentConfigStatus): Promise<TransactionSignature>;
57
+ updatePhoenixFulfillmentConfigStatus(phoenixFulfillmentConfig: PublicKey, status: SpotFulfillmentConfigStatus): Promise<TransactionSignature>;
56
58
  updateSpotMarketExpiry(spotMarketIndex: number, expiryTs: BN): Promise<TransactionSignature>;
57
59
  updateWhitelistMint(whitelistMint?: PublicKey): Promise<TransactionSignature>;
58
60
  updateDiscountMint(discountMint: PublicKey): Promise<TransactionSignature>;
@@ -455,6 +455,14 @@ class AdminClient extends driftClient_1.DriftClient {
455
455
  },
456
456
  });
457
457
  }
458
+ async updateStateMaxInitializeUserFee(maxInitializeUserFee) {
459
+ return await this.program.rpc.updateStateMaxInitializeUserFee(maxInitializeUserFee, {
460
+ accounts: {
461
+ admin: this.wallet.publicKey,
462
+ state: await this.getStatePublicKey(),
463
+ },
464
+ });
465
+ }
458
466
  async updateWithdrawGuardThreshold(spotMarketIndex, withdrawGuardThreshold) {
459
467
  const tx = await this.program.transaction.updateWithdrawGuardThreshold(withdrawGuardThreshold, {
460
468
  accounts: {
@@ -618,6 +626,15 @@ class AdminClient extends driftClient_1.DriftClient {
618
626
  },
619
627
  });
620
628
  }
629
+ async updatePhoenixFulfillmentConfigStatus(phoenixFulfillmentConfig, status) {
630
+ return await this.program.rpc.phoenixFulfillmentConfigStatus(status, {
631
+ accounts: {
632
+ admin: this.wallet.publicKey,
633
+ state: await this.getStatePublicKey(),
634
+ phoenixFulfillmentConfig,
635
+ },
636
+ });
637
+ }
621
638
  async updateSpotMarketExpiry(spotMarketIndex, expiryTs) {
622
639
  return await this.program.rpc.updateSpotMarketExpiry(expiryTs, {
623
640
  accounts: {
@@ -61,3 +61,6 @@ export declare const LAMPORTS_PRECISION: BN;
61
61
  export declare const LAMPORTS_EXP: BN;
62
62
  export declare const OPEN_ORDER_MARGIN_REQUIREMENT: BN;
63
63
  export declare const DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT: BN;
64
+ export declare const ACCOUNT_AGE_DELETION_CUTOFF_SECONDS: number;
65
+ export declare const IDLE_TIME_SLOTS = 9000;
66
+ export declare const SLOT_TIME_ESTIMATE_MS = 400;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_DIV_PEG = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION_EXP = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_RATE_BUFFER_PRECISION = exports.FUNDING_RATE_PRECISION = exports.PRICE_PRECISION = exports.QUOTE_PRECISION = exports.LIQUIDATION_FEE_PRECISION = exports.SPOT_MARKET_IMF_PRECISION = exports.SPOT_MARKET_IMF_PRECISION_EXP = exports.SPOT_MARKET_BALANCE_PRECISION = exports.SPOT_MARKET_BALANCE_PRECISION_EXP = exports.SPOT_MARKET_WEIGHT_PRECISION = exports.SPOT_MARKET_UTILIZATION_PRECISION = exports.SPOT_MARKET_UTILIZATION_PRECISION_EXP = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP = exports.SPOT_MARKET_RATE_PRECISION = exports.SPOT_MARKET_RATE_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP = exports.PEG_PRECISION_EXP = exports.FUNDING_RATE_PRECISION_EXP = exports.PRICE_PRECISION_EXP = exports.FUNDING_RATE_BUFFER_PRECISION_EXP = exports.QUOTE_PRECISION_EXP = exports.CONCENTRATION_PRECISION = exports.PERCENTAGE_PRECISION = exports.PERCENTAGE_PRECISION_EXP = exports.MAX_LEVERAGE_ORDER_SIZE = exports.MAX_LEVERAGE = exports.TEN_MILLION = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.NINE = exports.EIGHT = exports.SEVEN = exports.SIX = exports.FIVE = exports.FOUR = exports.THREE = exports.TWO = exports.ONE = exports.ZERO = void 0;
4
- exports.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT = exports.OPEN_ORDER_MARGIN_REQUIREMENT = exports.LAMPORTS_EXP = exports.LAMPORTS_PRECISION = exports.QUOTE_SPOT_MARKET_INDEX = exports.ONE_YEAR = exports.ONE_HOUR = exports.FIVE_MINUTE = exports.FUNDING_RATE_OFFSET_DENOMINATOR = exports.LIQUIDATION_PCT_PRECISION = exports.BID_ASK_SPREAD_PRECISION = void 0;
4
+ exports.SLOT_TIME_ESTIMATE_MS = exports.IDLE_TIME_SLOTS = exports.ACCOUNT_AGE_DELETION_CUTOFF_SECONDS = exports.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT = exports.OPEN_ORDER_MARGIN_REQUIREMENT = exports.LAMPORTS_EXP = exports.LAMPORTS_PRECISION = exports.QUOTE_SPOT_MARKET_INDEX = exports.ONE_YEAR = exports.ONE_HOUR = exports.FIVE_MINUTE = exports.FUNDING_RATE_OFFSET_DENOMINATOR = exports.LIQUIDATION_PCT_PRECISION = exports.BID_ASK_SPREAD_PRECISION = void 0;
5
5
  const web3_js_1 = require("@solana/web3.js");
6
6
  const __1 = require("../");
7
7
  exports.ZERO = new __1.BN(0);
@@ -65,3 +65,6 @@ exports.LAMPORTS_PRECISION = new __1.BN(web3_js_1.LAMPORTS_PER_SOL);
65
65
  exports.LAMPORTS_EXP = new __1.BN(Math.log10(web3_js_1.LAMPORTS_PER_SOL));
66
66
  exports.OPEN_ORDER_MARGIN_REQUIREMENT = exports.QUOTE_PRECISION.div(new __1.BN(100));
67
67
  exports.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT = new __1.BN(-25).mul(exports.QUOTE_PRECISION);
68
+ exports.ACCOUNT_AGE_DELETION_CUTOFF_SECONDS = 60 * 60 * 24 * 13; // 13 days
69
+ exports.IDLE_TIME_SLOTS = 9000;
70
+ exports.SLOT_TIME_ESTIMATE_MS = 400;
@@ -194,6 +194,26 @@ exports.DevnetPerpMarkets = [
194
194
  launchTs: 1700542800000,
195
195
  oracleSource: __1.OracleSource.PYTH,
196
196
  },
197
+ {
198
+ fullName: 'Celestia',
199
+ category: ['Data'],
200
+ symbol: 'TIA-PERP',
201
+ baseAssetSymbol: 'TIA',
202
+ marketIndex: 19,
203
+ oracle: new web3_js_1.PublicKey('4GiL1Y6u6JkPb7ckakzJgc414h6P7qoYnEKFcd1YtSB9'),
204
+ launchTs: 1701880540000,
205
+ oracleSource: __1.OracleSource.PYTH,
206
+ },
207
+ {
208
+ fullName: 'Jito',
209
+ category: ['MEV'],
210
+ symbol: 'JTO-PERP',
211
+ baseAssetSymbol: 'JTO',
212
+ marketIndex: 20,
213
+ oracle: new web3_js_1.PublicKey('29xQnTzyyuRtgJ7RtSKEgBWwRzZqtrrKmyQQ5m3x629f'),
214
+ launchTs: 1701967240000,
215
+ oracleSource: __1.OracleSource.PYTH,
216
+ },
197
217
  ];
198
218
  exports.MainnetPerpMarkets = [
199
219
  {
@@ -386,6 +406,26 @@ exports.MainnetPerpMarkets = [
386
406
  launchTs: 1700542800000,
387
407
  oracleSource: __1.OracleSource.PYTH,
388
408
  },
409
+ {
410
+ fullName: 'Celestia',
411
+ category: ['Data'],
412
+ symbol: 'TIA-PERP',
413
+ baseAssetSymbol: 'TIA',
414
+ marketIndex: 19,
415
+ oracle: new web3_js_1.PublicKey('funeUsHgi2QKkLdUPASRLuYkaK8JaazCEz3HikbkhVt'),
416
+ launchTs: 1701880540000,
417
+ oracleSource: __1.OracleSource.PYTH,
418
+ },
419
+ {
420
+ fullName: 'Jito',
421
+ category: ['MEV'],
422
+ symbol: 'JTO-PERP',
423
+ baseAssetSymbol: 'JTO',
424
+ marketIndex: 20,
425
+ oracle: new web3_js_1.PublicKey('D8UUgr8a3aR3yUeHLu7v8FWK7E8Y5sSU7qrYBXUJXBQ5'),
426
+ launchTs: 1701967240000,
427
+ oracleSource: __1.OracleSource.PYTH,
428
+ },
389
429
  ];
390
430
  exports.PerpMarkets = {
391
431
  devnet: exports.DevnetPerpMarkets,
@@ -130,6 +130,16 @@ exports.MainnetSpotMarkets = [
130
130
  precisionExp: numericConstants_1.NINE,
131
131
  serumMarket: new web3_js_1.PublicKey('ARjaHVxGCQfTvvKjLd7U7srvk6orthZSE6uqWchCczZc'),
132
132
  },
133
+ {
134
+ symbol: 'JTO',
135
+ marketIndex: 9,
136
+ oracle: new web3_js_1.PublicKey('D8UUgr8a3aR3yUeHLu7v8FWK7E8Y5sSU7qrYBXUJXBQ5'),
137
+ oracleSource: __1.OracleSource.PYTH,
138
+ mint: new web3_js_1.PublicKey('jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL'),
139
+ precision: new __1.BN(10).pow(numericConstants_1.NINE),
140
+ precisionExp: numericConstants_1.NINE,
141
+ serumMarket: new web3_js_1.PublicKey('H87FfmHABiZLRGrDsXRZtqq25YpARzaokCzL1vMYGiep'),
142
+ },
133
143
  ];
134
144
  exports.SpotMarkets = {
135
145
  devnet: exports.DevnetSpotMarkets,
@@ -0,0 +1,3 @@
1
+ /// <reference types="node" />
2
+ import { UserAccount } from '../types';
3
+ export declare function decodeUser(buffer: Buffer): UserAccount;
@@ -0,0 +1,329 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeUser = void 0;
4
+ const types_1 = require("../types");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ const __1 = require("../");
7
+ const __2 = require("../");
8
+ function readUnsignedBigInt64LE(buffer, offset) {
9
+ return new __1.BN(buffer.subarray(offset, offset + 8), 10, 'le');
10
+ }
11
+ function readSignedBigInt64LE(buffer, offset) {
12
+ const unsignedValue = new __1.BN(buffer.subarray(offset, offset + 8), 10, 'le');
13
+ if (unsignedValue.testn(63)) {
14
+ const inverted = unsignedValue.notn(64).addn(1);
15
+ return inverted.neg();
16
+ }
17
+ else {
18
+ return unsignedValue;
19
+ }
20
+ }
21
+ function decodeUser(buffer) {
22
+ let offset = 8;
23
+ const authority = new web3_js_1.PublicKey(buffer.slice(offset, offset + 32));
24
+ offset += 32;
25
+ const delegate = new web3_js_1.PublicKey(buffer.slice(offset, offset + 32));
26
+ offset += 32;
27
+ const name = [];
28
+ for (let i = 0; i < 32; i++) {
29
+ name.push(buffer.readUint8(offset + i));
30
+ }
31
+ offset += 32;
32
+ const spotPositions = [];
33
+ for (let i = 0; i < 8; i++) {
34
+ const scaledBalance = readUnsignedBigInt64LE(buffer, offset);
35
+ const openOrders = buffer.readUInt8(offset + 35);
36
+ if (scaledBalance.eq(__2.ZERO) && openOrders === 0) {
37
+ offset += 40;
38
+ continue;
39
+ }
40
+ offset += 8;
41
+ const openBids = readSignedBigInt64LE(buffer, offset);
42
+ offset += 8;
43
+ const openAsks = readSignedBigInt64LE(buffer, offset);
44
+ offset += 8;
45
+ const cumulativeDeposits = readSignedBigInt64LE(buffer, offset);
46
+ offset += 8;
47
+ const marketIndex = buffer.readUInt16LE(offset);
48
+ offset += 2;
49
+ const balanceTypeNum = buffer.readUInt8(offset);
50
+ let balanceType;
51
+ if (balanceTypeNum === 0) {
52
+ balanceType = types_1.SpotBalanceType.DEPOSIT;
53
+ }
54
+ else {
55
+ balanceType = types_1.SpotBalanceType.BORROW;
56
+ }
57
+ offset += 6;
58
+ spotPositions.push({
59
+ scaledBalance,
60
+ openBids,
61
+ openAsks,
62
+ cumulativeDeposits,
63
+ marketIndex,
64
+ balanceType,
65
+ openOrders,
66
+ });
67
+ }
68
+ const perpPositions = [];
69
+ for (let i = 0; i < 8; i++) {
70
+ const baseAssetAmount = readSignedBigInt64LE(buffer, offset + 8);
71
+ const quoteAssetAmount = readSignedBigInt64LE(buffer, offset + 16);
72
+ const lpShares = readUnsignedBigInt64LE(buffer, offset + 64);
73
+ const openOrders = buffer.readUInt8(offset + 94);
74
+ if (baseAssetAmount.eq(__2.ZERO) &&
75
+ openOrders === 0 &&
76
+ quoteAssetAmount.eq(__2.ZERO) &&
77
+ lpShares.eq(__2.ZERO)) {
78
+ offset += 96;
79
+ continue;
80
+ }
81
+ const lastCumulativeFundingRate = readSignedBigInt64LE(buffer, offset);
82
+ offset += 24;
83
+ const quoteBreakEvenAmount = readSignedBigInt64LE(buffer, offset);
84
+ offset += 8;
85
+ const quoteEntryAmount = readSignedBigInt64LE(buffer, offset);
86
+ offset += 8;
87
+ const openBids = readSignedBigInt64LE(buffer, offset);
88
+ offset += 8;
89
+ const openAsks = readSignedBigInt64LE(buffer, offset);
90
+ offset += 8;
91
+ const settledPnl = readSignedBigInt64LE(buffer, offset);
92
+ offset += 16;
93
+ const lastBaseAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
94
+ offset += 8;
95
+ const lastQuoteAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
96
+ offset += 8;
97
+ const remainderBaseAssetAmount = buffer.readUint32LE(offset);
98
+ offset += 4;
99
+ const marketIndex = buffer.readUInt16LE(offset);
100
+ offset += 3;
101
+ const perLpBase = buffer.readUInt8(offset);
102
+ offset += 1;
103
+ perpPositions.push({
104
+ lastCumulativeFundingRate,
105
+ baseAssetAmount,
106
+ quoteAssetAmount,
107
+ quoteBreakEvenAmount,
108
+ quoteEntryAmount,
109
+ openBids,
110
+ openAsks,
111
+ settledPnl,
112
+ lpShares,
113
+ lastBaseAssetAmountPerLp,
114
+ lastQuoteAssetAmountPerLp,
115
+ remainderBaseAssetAmount,
116
+ marketIndex,
117
+ openOrders,
118
+ perLpBase,
119
+ });
120
+ }
121
+ const orders = [];
122
+ for (let i = 0; i < 32; i++) {
123
+ // skip order if it's not open
124
+ if (buffer.readUint8(offset + 82) === 0) {
125
+ offset += 96;
126
+ continue;
127
+ }
128
+ const slot = readUnsignedBigInt64LE(buffer, offset);
129
+ offset += 8;
130
+ const price = readUnsignedBigInt64LE(buffer, offset);
131
+ offset += 8;
132
+ const baseAssetAmount = readUnsignedBigInt64LE(buffer, offset);
133
+ offset += 8;
134
+ const baseAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
135
+ offset += 8;
136
+ const quoteAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
137
+ offset += 8;
138
+ const triggerPrice = readUnsignedBigInt64LE(buffer, offset);
139
+ offset += 8;
140
+ const auctionStartPrice = readSignedBigInt64LE(buffer, offset);
141
+ offset += 8;
142
+ const auctionEndPrice = readSignedBigInt64LE(buffer, offset);
143
+ offset += 8;
144
+ const maxTs = readSignedBigInt64LE(buffer, offset);
145
+ offset += 8;
146
+ const oraclePriceOffset = buffer.readInt32LE(offset);
147
+ offset += 4;
148
+ const orderId = buffer.readUInt32LE(offset);
149
+ offset += 4;
150
+ const marketIndex = buffer.readUInt16LE(offset);
151
+ offset += 2;
152
+ const orderStatusNum = buffer.readUInt8(offset);
153
+ let status;
154
+ if (orderStatusNum === 0) {
155
+ status = types_1.OrderStatus.INIT;
156
+ }
157
+ else if (orderStatusNum === 1) {
158
+ status = types_1.OrderStatus.OPEN;
159
+ }
160
+ offset += 1;
161
+ const orderTypeNum = buffer.readUInt8(offset);
162
+ let orderType;
163
+ if (orderTypeNum === 0) {
164
+ orderType = types_1.OrderType.MARKET;
165
+ }
166
+ else if (orderTypeNum === 1) {
167
+ orderType = types_1.OrderType.LIMIT;
168
+ }
169
+ else if (orderTypeNum === 2) {
170
+ orderType = types_1.OrderType.TRIGGER_MARKET;
171
+ }
172
+ else if (orderTypeNum === 3) {
173
+ orderType = types_1.OrderType.TRIGGER_LIMIT;
174
+ }
175
+ else if (orderTypeNum === 4) {
176
+ orderType = types_1.OrderType.ORACLE;
177
+ }
178
+ offset += 1;
179
+ const marketTypeNum = buffer.readUInt8(offset);
180
+ let marketType;
181
+ if (marketTypeNum === 0) {
182
+ marketType = types_1.MarketType.SPOT;
183
+ }
184
+ else {
185
+ marketType = types_1.MarketType.PERP;
186
+ }
187
+ offset += 1;
188
+ const userOrderId = buffer.readUint8(offset);
189
+ offset += 1;
190
+ const existingPositionDirectionNum = buffer.readUInt8(offset);
191
+ let existingPositionDirection;
192
+ if (existingPositionDirectionNum === 0) {
193
+ existingPositionDirection = types_1.PositionDirection.LONG;
194
+ }
195
+ else {
196
+ existingPositionDirection = types_1.PositionDirection.SHORT;
197
+ }
198
+ offset += 1;
199
+ const positionDirectionNum = buffer.readUInt8(offset);
200
+ let direction;
201
+ if (positionDirectionNum === 0) {
202
+ direction = types_1.PositionDirection.LONG;
203
+ }
204
+ else {
205
+ direction = types_1.PositionDirection.SHORT;
206
+ }
207
+ offset += 1;
208
+ const reduceOnly = buffer.readUInt8(offset) === 1;
209
+ offset += 1;
210
+ const postOnly = buffer.readUInt8(offset) === 1;
211
+ offset += 1;
212
+ const immediateOrCancel = buffer.readUInt8(offset) === 1;
213
+ offset += 1;
214
+ const triggerConditionNum = buffer.readUInt8(offset);
215
+ let triggerCondition;
216
+ if (triggerConditionNum === 0) {
217
+ triggerCondition = types_1.OrderTriggerCondition.ABOVE;
218
+ }
219
+ else if (triggerConditionNum === 1) {
220
+ triggerCondition = types_1.OrderTriggerCondition.BELOW;
221
+ }
222
+ else if (triggerConditionNum === 2) {
223
+ triggerCondition = types_1.OrderTriggerCondition.TRIGGERED_ABOVE;
224
+ }
225
+ else if (triggerConditionNum === 3) {
226
+ triggerCondition = types_1.OrderTriggerCondition.TRIGGERED_BELOW;
227
+ }
228
+ offset += 1;
229
+ const auctionDuration = buffer.readUInt8(offset);
230
+ offset += 1;
231
+ offset += 3; // padding
232
+ orders.push({
233
+ slot,
234
+ price,
235
+ baseAssetAmount,
236
+ quoteAssetAmount: undefined,
237
+ baseAssetAmountFilled,
238
+ quoteAssetAmountFilled,
239
+ triggerPrice,
240
+ auctionStartPrice,
241
+ auctionEndPrice,
242
+ maxTs,
243
+ oraclePriceOffset,
244
+ orderId,
245
+ marketIndex,
246
+ status,
247
+ orderType,
248
+ marketType,
249
+ userOrderId,
250
+ existingPositionDirection,
251
+ direction,
252
+ reduceOnly,
253
+ postOnly,
254
+ immediateOrCancel,
255
+ triggerCondition,
256
+ auctionDuration,
257
+ });
258
+ }
259
+ const lastAddPerpLpSharesTs = readSignedBigInt64LE(buffer, offset);
260
+ offset += 8;
261
+ const totalDeposits = readUnsignedBigInt64LE(buffer, offset);
262
+ offset += 8;
263
+ const totalWithdraws = readUnsignedBigInt64LE(buffer, offset);
264
+ offset += 8;
265
+ const totalSocialLoss = readUnsignedBigInt64LE(buffer, offset);
266
+ offset += 8;
267
+ const settledPerpPnl = readSignedBigInt64LE(buffer, offset);
268
+ offset += 8;
269
+ const cumulativeSpotFees = readSignedBigInt64LE(buffer, offset);
270
+ offset += 8;
271
+ const cumulativePerpFunding = readSignedBigInt64LE(buffer, offset);
272
+ offset += 8;
273
+ const liquidationMarginFreed = readUnsignedBigInt64LE(buffer, offset);
274
+ offset += 8;
275
+ const lastActiveSlot = readUnsignedBigInt64LE(buffer, offset);
276
+ offset += 8;
277
+ const nextOrderId = buffer.readUInt32LE(offset);
278
+ offset += 4;
279
+ const maxMarginRatio = buffer.readUInt32LE(offset);
280
+ offset += 4;
281
+ const nextLiquidationId = buffer.readUInt16LE(offset);
282
+ offset += 2;
283
+ const subAccountId = buffer.readUInt16LE(offset);
284
+ offset += 2;
285
+ const status = buffer.readUInt8(offset);
286
+ offset += 1;
287
+ const isMarginTradingEnabled = buffer.readUInt8(offset) === 1;
288
+ offset += 1;
289
+ const idle = buffer.readUInt8(offset) === 1;
290
+ offset += 1;
291
+ const openOrders = buffer.readUInt8(offset);
292
+ offset += 1;
293
+ const hasOpenOrder = buffer.readUInt8(offset) === 1;
294
+ offset += 1;
295
+ const openAuctions = buffer.readUInt8(offset);
296
+ offset += 1;
297
+ const hasOpenAuction = buffer.readUInt8(offset) === 1;
298
+ offset += 1;
299
+ // @ts-ignore
300
+ return {
301
+ authority,
302
+ delegate,
303
+ name,
304
+ spotPositions,
305
+ perpPositions,
306
+ orders,
307
+ lastAddPerpLpSharesTs,
308
+ totalDeposits,
309
+ totalWithdraws,
310
+ totalSocialLoss,
311
+ settledPerpPnl,
312
+ cumulativeSpotFees,
313
+ cumulativePerpFunding,
314
+ liquidationMarginFreed,
315
+ lastActiveSlot,
316
+ nextOrderId,
317
+ maxMarginRatio,
318
+ nextLiquidationId,
319
+ subAccountId,
320
+ status,
321
+ isMarginTradingEnabled,
322
+ idle,
323
+ openOrders,
324
+ hasOpenOrder,
325
+ openAuctions,
326
+ hasOpenAuction,
327
+ };
328
+ }
329
+ exports.decodeUser = decodeUser;
@@ -133,6 +133,7 @@ export declare class DriftClient {
133
133
  getUserAccountsForAuthority(authority: PublicKey): Promise<UserAccount[]>;
134
134
  getReferredUserStatsAccountsByReferrer(referrer: PublicKey): Promise<UserStatsAccount[]>;
135
135
  getReferrerNameAccountsForAuthority(authority: PublicKey): Promise<ReferrerNameAccount[]>;
136
+ getUserDeletionIx(userAccountPublicKey: PublicKey): Promise<anchor.web3.TransactionInstruction>;
136
137
  deleteUser(subAccountId?: number, txParams?: TxParams): Promise<TransactionSignature>;
137
138
  getUser(subAccountId?: number, authority?: PublicKey): User;
138
139
  hasUser(subAccountId?: number, authority?: PublicKey): boolean;
@@ -228,9 +229,12 @@ export declare class DriftClient {
228
229
  * @param subAccountId
229
230
  * @param name
230
231
  * @param fromSubAccountId
232
+ * @param referrerInfo
233
+ * @param donateAmount
234
+ * @param txParams
231
235
  * @returns
232
236
  */
233
- initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, marketIndex?: number, subAccountId?: number, name?: string, fromSubAccountId?: number, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
237
+ initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, marketIndex?: number, subAccountId?: number, name?: string, fromSubAccountId?: number, referrerInfo?: ReferrerInfo, donateAmount?: BN, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
234
238
  initializeUserAccountForDevnet(subAccountId: number, name: string, marketIndex: number, tokenFaucet: TokenFaucet, amount: BN, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
235
239
  /**
236
240
  * Withdraws from a user account. If deposit doesn't already exist, creates a borrow
@@ -619,6 +623,7 @@ export declare class DriftClient {
619
623
  settleRevenueToInsuranceFund(marketIndex: number): Promise<TransactionSignature>;
620
624
  resolvePerpPnlDeficit(spotMarketIndex: number, perpMarketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
621
625
  getResolvePerpPnlDeficitIx(spotMarketIndex: number, perpMarketIndex: number): Promise<TransactionInstruction>;
626
+ getDepositIntoSpotMarketRevenuePoolIx(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
622
627
  depositIntoSpotMarketRevenuePool(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionSignature>;
623
628
  getPerpMarketExtendedInfo(marketIndex: number): PerpMarketExtendedInfo;
624
629
  /**