@drift-labs/sdk 2.23.0 → 2.24.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.
@@ -7,7 +7,8 @@ export declare class AdminClient extends DriftClient {
7
7
  initialize(usdcMint: PublicKey, _adminControlsPrices: boolean): Promise<[TransactionSignature]>;
8
8
  initializeSpotMarket(mint: PublicKey, optimalUtilization: number, optimalRate: number, maxRate: number, oracle: PublicKey, oracleSource: OracleSource, initialAssetWeight: number, maintenanceAssetWeight: number, initialLiabilityWeight: number, maintenanceLiabilityWeight: number, imfFactor?: number, liquidatorFee?: number, activeStatus?: boolean, name?: string): Promise<TransactionSignature>;
9
9
  initializeSerumFulfillmentConfig(marketIndex: number, serumMarket: PublicKey, serumProgram: PublicKey): Promise<TransactionSignature>;
10
- initializePerpMarket(priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, marginRatioInitial?: number, marginRatioMaintenance?: number, liquidatorFee?: number, activeStatus?: boolean, name?: string): Promise<TransactionSignature>;
10
+ initializePerpMarket(marketIndex: number, priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, marginRatioInitial?: number, marginRatioMaintenance?: number, liquidatorFee?: number, activeStatus?: boolean, name?: string): Promise<TransactionSignature>;
11
+ deleteInitializedPerpMarket(marketIndex: number): Promise<TransactionSignature>;
11
12
  moveAmmPrice(perpMarketIndex: number, baseAssetReserve: BN, quoteAssetReserve: BN, sqrtK?: BN): Promise<TransactionSignature>;
12
13
  updateK(perpMarketIndex: number, sqrtK: BN): Promise<TransactionSignature>;
13
14
  updatePerpMarketConcentrationScale(perpMarketIndex: number, concentrationScale: BN): Promise<TransactionSignature>;
@@ -107,11 +107,11 @@ class AdminClient extends driftClient_1.DriftClient {
107
107
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
108
108
  return txSig;
109
109
  }
110
- async initializePerpMarket(priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, activeStatus = true, name = userName_1.DEFAULT_MARKET_NAME) {
110
+ async initializePerpMarket(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, activeStatus = true, name = userName_1.DEFAULT_MARKET_NAME) {
111
111
  const currentPerpMarketIndex = this.getStateAccount().numberOfMarkets;
112
112
  const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, currentPerpMarketIndex);
113
113
  const nameBuffer = (0, userName_1.encodeName)(name);
114
- const initializeMarketTx = await this.program.transaction.initializePerpMarket(baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, marginRatioInitial, marginRatioMaintenance, liquidatorFee, activeStatus, nameBuffer, {
114
+ const initializeMarketTx = await this.program.transaction.initializePerpMarket(marketIndex, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, marginRatioInitial, marginRatioMaintenance, liquidatorFee, activeStatus, nameBuffer, {
115
115
  accounts: {
116
116
  state: await this.getStatePublicKey(),
117
117
  admin: this.wallet.publicKey,
@@ -132,6 +132,18 @@ class AdminClient extends driftClient_1.DriftClient {
132
132
  });
133
133
  return txSig;
134
134
  }
135
+ async deleteInitializedPerpMarket(marketIndex) {
136
+ const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
137
+ const deleteInitializeMarketTx = await this.program.transaction.deleteInitializedPerpMarket(marketIndex, {
138
+ accounts: {
139
+ state: await this.getStatePublicKey(),
140
+ admin: this.wallet.publicKey,
141
+ perpMarket: perpMarketPublicKey,
142
+ },
143
+ });
144
+ const { txSig } = await this.sendTransaction(deleteInitializeMarketTx, [], this.opts);
145
+ return txSig;
146
+ }
135
147
  async moveAmmPrice(perpMarketIndex, baseAssetReserve, quoteAssetReserve, sqrtK) {
136
148
  const marketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
137
149
  if (sqrtK == undefined) {
@@ -46,7 +46,7 @@ exports.DevnetPerpMarkets = [
46
46
  },
47
47
  {
48
48
  fullName: 'Bonk',
49
- category: ['Meme'],
49
+ category: ['Meme', 'Dog'],
50
50
  symbol: '1MBONK-PERP',
51
51
  baseAssetSymbol: '1MBONK',
52
52
  marketIndex: 4,
@@ -74,6 +74,16 @@ exports.DevnetPerpMarkets = [
74
74
  launchTs: 1679501812000,
75
75
  oracleSource: __1.OracleSource.PYTH,
76
76
  },
77
+ {
78
+ fullName: 'Doge',
79
+ category: ['Meme', 'Dog'],
80
+ symbol: 'DOGE-PERP',
81
+ baseAssetSymbol: 'DOGE',
82
+ marketIndex: 7,
83
+ oracle: new web3_js_1.PublicKey('4L6YhY8VvUgmqG5MvJkUJATtzB2rFqdrJwQCmFLv4Jzy'),
84
+ launchTs: 1680808053000,
85
+ oracleSource: __1.OracleSource.PYTH,
86
+ },
77
87
  ];
78
88
  exports.MainnetPerpMarkets = [
79
89
  {
@@ -146,6 +156,16 @@ exports.MainnetPerpMarkets = [
146
156
  launchTs: 1679501812000,
147
157
  oracleSource: __1.OracleSource.PYTH,
148
158
  },
159
+ {
160
+ fullName: 'Doge',
161
+ category: ['Meme', 'Dog'],
162
+ symbol: 'DOGE-PERP',
163
+ baseAssetSymbol: 'DOGE',
164
+ marketIndex: 7,
165
+ oracle: new web3_js_1.PublicKey('FsSM3s38PX9K7Dn6eGzuE29S2Dsk1Sss1baytTQdCaQj'),
166
+ launchTs: 1680808053000,
167
+ oracleSource: __1.OracleSource.PYTH,
168
+ },
149
169
  ];
150
170
  exports.PerpMarkets = {
151
171
  devnet: exports.DevnetPerpMarkets,
@@ -104,6 +104,7 @@ export declare class DriftClient {
104
104
  updateUserDelegate(delegate: PublicKey, subAccountId?: number): Promise<TransactionSignature>;
105
105
  fetchAllUserAccounts(includeIdle?: boolean): Promise<ProgramAccount<UserAccount>[]>;
106
106
  getUserAccountsForDelegate(delegate: PublicKey): Promise<UserAccount[]>;
107
+ getUserAccountsAndAddressesForAuthority(authority: PublicKey): Promise<ProgramAccount<UserAccount>[]>;
107
108
  getUserAccountsForAuthority(authority: PublicKey): Promise<UserAccount[]>;
108
109
  getReferredUserStatsAccountsByReferrer(referrer: PublicKey): Promise<UserStatsAccount[]>;
109
110
  getReferrerNameAccountsForAuthority(authority: PublicKey): Promise<ReferrerNameAccount[]>;
@@ -324,7 +325,25 @@ export declare class DriftClient {
324
325
  getOracleDataForSpotMarket(marketIndex: number): OraclePriceData;
325
326
  initializeInsuranceFundStake(marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
326
327
  getInitializeInsuranceFundStakeIx(marketIndex: number): Promise<TransactionInstruction>;
327
- addInsuranceFundStake(marketIndex: number, amount: BN, collateralAccountPublicKey: PublicKey): Promise<TransactionSignature>;
328
+ getAddInsuranceFundStakeIx(marketIndex: number, amount: BN, collateralAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
329
+ /**
330
+ * Add to an insurance fund stake and optionally initialize the account
331
+ */
332
+ addInsuranceFundStake({ marketIndex, amount, collateralAccountPublicKey, initializeStakeAccount, }: {
333
+ /**
334
+ * Spot market index
335
+ */
336
+ marketIndex: number;
337
+ amount: BN;
338
+ /**
339
+ * The account where the funds to stake come from. Usually an associated token account
340
+ */
341
+ collateralAccountPublicKey: PublicKey;
342
+ /**
343
+ * Add instructions to initialize the staking account -- required if its the first time the currrent authority has staked in this market
344
+ */
345
+ initializeStakeAccount?: boolean;
346
+ }): Promise<TransactionSignature>;
328
347
  requestRemoveInsuranceFundStake(marketIndex: number, amount: BN): Promise<TransactionSignature>;
329
348
  cancelRequestRemoveInsuranceFundStake(marketIndex: number): Promise<TransactionSignature>;
330
349
  removeInsuranceFundStake(marketIndex: number, collateralAccountPublicKey: PublicKey): Promise<TransactionSignature>;
@@ -458,6 +458,18 @@ class DriftClient {
458
458
  ]);
459
459
  return programAccounts.map((programAccount) => programAccount.account);
460
460
  }
461
+ async getUserAccountsAndAddressesForAuthority(authority) {
462
+ const programAccounts = await this.program.account.user.all([
463
+ {
464
+ memcmp: {
465
+ offset: 8,
466
+ /** data to match, as base-58 encoded string and limited to less than 129 bytes */
467
+ bytes: bs58_1.default.encode(authority.toBuffer()),
468
+ },
469
+ },
470
+ ]);
471
+ return programAccounts.map((programAccount) => programAccount);
472
+ }
461
473
  async getUserAccountsForAuthority(authority) {
462
474
  const programAccounts = await this.program.account.user.all([
463
475
  {
@@ -2406,7 +2418,7 @@ class DriftClient {
2406
2418
  },
2407
2419
  });
2408
2420
  }
2409
- async addInsuranceFundStake(marketIndex, amount, collateralAccountPublicKey) {
2421
+ async getAddInsuranceFundStakeIx(marketIndex, amount, collateralAccountPublicKey) {
2410
2422
  const spotMarket = this.getSpotMarketAccount(marketIndex);
2411
2423
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
2412
2424
  const remainingAccounts = this.getRemainingAccounts({
@@ -2414,7 +2426,7 @@ class DriftClient {
2414
2426
  useMarketLastSlotCache: true,
2415
2427
  writableSpotMarketIndexes: [marketIndex],
2416
2428
  });
2417
- const tx = await this.program.transaction.addInsuranceFundStake(marketIndex, amount, {
2429
+ const ix = this.program.instruction.addInsuranceFundStake(marketIndex, amount, {
2418
2430
  accounts: {
2419
2431
  state: await this.getStatePublicKey(),
2420
2432
  spotMarket: spotMarket.pubkey,
@@ -2429,7 +2441,39 @@ class DriftClient {
2429
2441
  },
2430
2442
  remainingAccounts,
2431
2443
  });
2432
- const { txSig } = await this.sendTransaction(tx, [], this.opts);
2444
+ return ix;
2445
+ }
2446
+ /**
2447
+ * Add to an insurance fund stake and optionally initialize the account
2448
+ */
2449
+ async addInsuranceFundStake({ marketIndex, amount, collateralAccountPublicKey, initializeStakeAccount, }) {
2450
+ const tx = new web3_js_1.Transaction();
2451
+ const additionalSigners = [];
2452
+ const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
2453
+ const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
2454
+ const createWSOLTokenAccount = isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
2455
+ let tokenAccount;
2456
+ if (createWSOLTokenAccount) {
2457
+ const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(amount, true);
2458
+ tokenAccount = pubkey;
2459
+ ixs.forEach((ix) => {
2460
+ tx.add(ix);
2461
+ });
2462
+ signers.forEach((signer) => additionalSigners.push(signer));
2463
+ }
2464
+ else {
2465
+ tokenAccount = collateralAccountPublicKey;
2466
+ }
2467
+ if (initializeStakeAccount) {
2468
+ const initializeIx = await this.getInitializeInsuranceFundStakeIx(marketIndex);
2469
+ tx.add(initializeIx);
2470
+ }
2471
+ const addFundsIx = await this.getAddInsuranceFundStakeIx(marketIndex, amount, tokenAccount);
2472
+ tx.add(addFundsIx);
2473
+ if (createWSOLTokenAccount) {
2474
+ tx.add(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
2475
+ }
2476
+ const { txSig } = await this.sendTransaction(tx, additionalSigners, this.opts);
2433
2477
  return txSig;
2434
2478
  }
2435
2479
  async requestRemoveInsuranceFundStake(marketIndex, amount) {
@@ -2477,14 +2521,31 @@ class DriftClient {
2477
2521
  return txSig;
2478
2522
  }
2479
2523
  async removeInsuranceFundStake(marketIndex, collateralAccountPublicKey) {
2524
+ const tx = new web3_js_1.Transaction();
2480
2525
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
2481
2526
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
2527
+ const tokenAccount = collateralAccountPublicKey;
2528
+ // Todo wsol remove iF stake... how do we determine the amount?
2529
+ // const amount = // get balance here...?
2530
+ // const additionalSigners: Array<Signer> = [];
2531
+ // const isSolMarket = spotMarketAccount.mint.equals(WRAPPED_SOL_MINT);
2532
+ // const createWSOLTokenAccount =
2533
+ // isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
2534
+ // if (createWSOLTokenAccount) {
2535
+ // const { ixs, signers, pubkey } =
2536
+ // await this.getWrappedSolAccountCreationIxs(amount, true);
2537
+ // tokenAccount = pubkey;
2538
+ // ixs.forEach((ix) => {
2539
+ // tx.add(ix);
2540
+ // });
2541
+ // signers.forEach((signer) => additionalSigners.push(signer));
2542
+ // }
2482
2543
  const remainingAccounts = this.getRemainingAccounts({
2483
2544
  userAccounts: [this.getUserAccount()],
2484
2545
  useMarketLastSlotCache: true,
2485
2546
  writableSpotMarketIndexes: [marketIndex],
2486
2547
  });
2487
- const tx = await this.program.transaction.removeInsuranceFundStake(marketIndex, {
2548
+ const removeStakeIx = await this.program.instruction.removeInsuranceFundStake(marketIndex, {
2488
2549
  accounts: {
2489
2550
  state: await this.getStatePublicKey(),
2490
2551
  spotMarket: spotMarketAccount.pubkey,
@@ -2493,11 +2554,12 @@ class DriftClient {
2493
2554
  authority: this.wallet.publicKey,
2494
2555
  insuranceFundVault: spotMarketAccount.insuranceFund.vault,
2495
2556
  driftSigner: this.getSignerPublicKey(),
2496
- userTokenAccount: collateralAccountPublicKey,
2557
+ userTokenAccount: tokenAccount,
2497
2558
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2498
2559
  },
2499
2560
  remainingAccounts,
2500
2561
  });
2562
+ tx.add(removeStakeIx);
2501
2563
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
2502
2564
  return txSig;
2503
2565
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.23.0",
2
+ "version": "2.24.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -2283,6 +2283,10 @@
2283
2283
  }
2284
2284
  ],
2285
2285
  "args": [
2286
+ {
2287
+ "name": "marketIndex",
2288
+ "type": "u16"
2289
+ },
2286
2290
  {
2287
2291
  "name": "ammBaseAssetReserve",
2288
2292
  "type": "u128"
@@ -2332,6 +2336,32 @@
2332
2336
  }
2333
2337
  ]
2334
2338
  },
2339
+ {
2340
+ "name": "deleteInitializedPerpMarket",
2341
+ "accounts": [
2342
+ {
2343
+ "name": "admin",
2344
+ "isMut": true,
2345
+ "isSigner": true
2346
+ },
2347
+ {
2348
+ "name": "state",
2349
+ "isMut": true,
2350
+ "isSigner": false
2351
+ },
2352
+ {
2353
+ "name": "perpMarket",
2354
+ "isMut": true,
2355
+ "isSigner": false
2356
+ }
2357
+ ],
2358
+ "args": [
2359
+ {
2360
+ "name": "marketIndex",
2361
+ "type": "u16"
2362
+ }
2363
+ ]
2364
+ },
2335
2365
  {
2336
2366
  "name": "moveAmmPrice",
2337
2367
  "accounts": [
@@ -8826,6 +8856,11 @@
8826
8856
  "code": 6239,
8827
8857
  "name": "RevertFill",
8828
8858
  "msg": "RevertFill"
8859
+ },
8860
+ {
8861
+ "code": 6240,
8862
+ "name": "InvalidMarketAccountforDeletion",
8863
+ "msg": "Invalid MarketAccount for Deletion"
8829
8864
  }
8830
8865
  ]
8831
8866
  }
package/lib/types.d.ts CHANGED
@@ -991,6 +991,7 @@ export type OracleGuardRails = {
991
991
  };
992
992
  export type MarginCategory = 'Initial' | 'Maintenance';
993
993
  export type InsuranceFundStake = {
994
+ costBasis: BN;
994
995
  marketIndex: number;
995
996
  authority: PublicKey;
996
997
  ifShares: BN;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.23.0",
3
+ "version": "2.24.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -182,6 +182,7 @@ export class AdminClient extends DriftClient {
182
182
  }
183
183
 
184
184
  public async initializePerpMarket(
185
+ marketIndex: number,
185
186
  priceOracle: PublicKey,
186
187
  baseAssetReserve: BN,
187
188
  quoteAssetReserve: BN,
@@ -203,6 +204,7 @@ export class AdminClient extends DriftClient {
203
204
  const nameBuffer = encodeName(name);
204
205
  const initializeMarketTx =
205
206
  await this.program.transaction.initializePerpMarket(
207
+ marketIndex,
206
208
  baseAssetReserve,
207
209
  quoteAssetReserve,
208
210
  periodicity,
@@ -243,6 +245,32 @@ export class AdminClient extends DriftClient {
243
245
  return txSig;
244
246
  }
245
247
 
248
+ public async deleteInitializedPerpMarket(
249
+ marketIndex: number
250
+ ): Promise<TransactionSignature> {
251
+ const perpMarketPublicKey = await getPerpMarketPublicKey(
252
+ this.program.programId,
253
+ marketIndex
254
+ );
255
+
256
+ const deleteInitializeMarketTx =
257
+ await this.program.transaction.deleteInitializedPerpMarket(marketIndex, {
258
+ accounts: {
259
+ state: await this.getStatePublicKey(),
260
+ admin: this.wallet.publicKey,
261
+ perpMarket: perpMarketPublicKey,
262
+ },
263
+ });
264
+
265
+ const { txSig } = await this.sendTransaction(
266
+ deleteInitializeMarketTx,
267
+ [],
268
+ this.opts
269
+ );
270
+
271
+ return txSig;
272
+ }
273
+
246
274
  public async moveAmmPrice(
247
275
  perpMarketIndex: number,
248
276
  baseAssetReserve: BN,
@@ -56,7 +56,7 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
56
56
  },
57
57
  {
58
58
  fullName: 'Bonk',
59
- category: ['Meme'],
59
+ category: ['Meme', 'Dog'],
60
60
  symbol: '1MBONK-PERP',
61
61
  baseAssetSymbol: '1MBONK',
62
62
  marketIndex: 4,
@@ -84,6 +84,16 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
84
84
  launchTs: 1679501812000, //todo
85
85
  oracleSource: OracleSource.PYTH,
86
86
  },
87
+ {
88
+ fullName: 'Doge',
89
+ category: ['Meme', 'Dog'],
90
+ symbol: 'DOGE-PERP',
91
+ baseAssetSymbol: 'DOGE',
92
+ marketIndex: 7,
93
+ oracle: new PublicKey('4L6YhY8VvUgmqG5MvJkUJATtzB2rFqdrJwQCmFLv4Jzy'),
94
+ launchTs: 1680808053000,
95
+ oracleSource: OracleSource.PYTH,
96
+ },
87
97
  ];
88
98
 
89
99
  export const MainnetPerpMarkets: PerpMarketConfig[] = [
@@ -157,6 +167,16 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
157
167
  launchTs: 1679501812000, //todo
158
168
  oracleSource: OracleSource.PYTH,
159
169
  },
170
+ {
171
+ fullName: 'Doge',
172
+ category: ['Meme', 'Dog'],
173
+ symbol: 'DOGE-PERP',
174
+ baseAssetSymbol: 'DOGE',
175
+ marketIndex: 7,
176
+ oracle: new PublicKey('FsSM3s38PX9K7Dn6eGzuE29S2Dsk1Sss1baytTQdCaQj'),
177
+ launchTs: 1680808053000,
178
+ oracleSource: OracleSource.PYTH,
179
+ },
160
180
  ];
161
181
 
162
182
  export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
@@ -766,6 +766,24 @@ export class DriftClient {
766
766
  );
767
767
  }
768
768
 
769
+ public async getUserAccountsAndAddressesForAuthority(
770
+ authority: PublicKey
771
+ ): Promise<ProgramAccount<UserAccount>[]> {
772
+ const programAccounts = await this.program.account.user.all([
773
+ {
774
+ memcmp: {
775
+ offset: 8,
776
+ /** data to match, as base-58 encoded string and limited to less than 129 bytes */
777
+ bytes: bs58.encode(authority.toBuffer()),
778
+ },
779
+ },
780
+ ]);
781
+
782
+ return programAccounts.map(
783
+ (programAccount) => programAccount as ProgramAccount<UserAccount>
784
+ );
785
+ }
786
+
769
787
  public async getUserAccountsForAuthority(
770
788
  authority: PublicKey
771
789
  ): Promise<UserAccount[]> {
@@ -4205,11 +4223,11 @@ export class DriftClient {
4205
4223
  );
4206
4224
  }
4207
4225
 
4208
- public async addInsuranceFundStake(
4226
+ public async getAddInsuranceFundStakeIx(
4209
4227
  marketIndex: number,
4210
4228
  amount: BN,
4211
4229
  collateralAccountPublicKey: PublicKey
4212
- ): Promise<TransactionSignature> {
4230
+ ): Promise<TransactionInstruction> {
4213
4231
  const spotMarket = this.getSpotMarketAccount(marketIndex);
4214
4232
  const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
4215
4233
  this.program.programId,
@@ -4223,7 +4241,7 @@ export class DriftClient {
4223
4241
  writableSpotMarketIndexes: [marketIndex],
4224
4242
  });
4225
4243
 
4226
- const tx = await this.program.transaction.addInsuranceFundStake(
4244
+ const ix = this.program.instruction.addInsuranceFundStake(
4227
4245
  marketIndex,
4228
4246
  amount,
4229
4247
  {
@@ -4243,7 +4261,88 @@ export class DriftClient {
4243
4261
  }
4244
4262
  );
4245
4263
 
4246
- const { txSig } = await this.sendTransaction(tx, [], this.opts);
4264
+ return ix;
4265
+ }
4266
+
4267
+ /**
4268
+ * Add to an insurance fund stake and optionally initialize the account
4269
+ */
4270
+ public async addInsuranceFundStake({
4271
+ marketIndex,
4272
+ amount,
4273
+ collateralAccountPublicKey,
4274
+ initializeStakeAccount,
4275
+ }: {
4276
+ /**
4277
+ * Spot market index
4278
+ */
4279
+ marketIndex: number;
4280
+ amount: BN;
4281
+ /**
4282
+ * The account where the funds to stake come from. Usually an associated token account
4283
+ */
4284
+ collateralAccountPublicKey: PublicKey;
4285
+ /**
4286
+ * Add instructions to initialize the staking account -- required if its the first time the currrent authority has staked in this market
4287
+ */
4288
+ initializeStakeAccount?: boolean;
4289
+ }): Promise<TransactionSignature> {
4290
+ const tx = new Transaction();
4291
+
4292
+ const additionalSigners: Array<Signer> = [];
4293
+ const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
4294
+ const isSolMarket = spotMarketAccount.mint.equals(WRAPPED_SOL_MINT);
4295
+ const createWSOLTokenAccount =
4296
+ isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
4297
+
4298
+ let tokenAccount;
4299
+
4300
+ if (createWSOLTokenAccount) {
4301
+ const { ixs, signers, pubkey } =
4302
+ await this.getWrappedSolAccountCreationIxs(amount, true);
4303
+ tokenAccount = pubkey;
4304
+ ixs.forEach((ix) => {
4305
+ tx.add(ix);
4306
+ });
4307
+
4308
+ signers.forEach((signer) => additionalSigners.push(signer));
4309
+ } else {
4310
+ tokenAccount = collateralAccountPublicKey;
4311
+ }
4312
+
4313
+ if (initializeStakeAccount) {
4314
+ const initializeIx = await this.getInitializeInsuranceFundStakeIx(
4315
+ marketIndex
4316
+ );
4317
+ tx.add(initializeIx);
4318
+ }
4319
+
4320
+ const addFundsIx = await this.getAddInsuranceFundStakeIx(
4321
+ marketIndex,
4322
+ amount,
4323
+ tokenAccount
4324
+ );
4325
+
4326
+ tx.add(addFundsIx);
4327
+
4328
+ if (createWSOLTokenAccount) {
4329
+ tx.add(
4330
+ Token.createCloseAccountInstruction(
4331
+ TOKEN_PROGRAM_ID,
4332
+ tokenAccount,
4333
+ this.wallet.publicKey,
4334
+ this.wallet.publicKey,
4335
+ []
4336
+ )
4337
+ );
4338
+ }
4339
+
4340
+ const { txSig } = await this.sendTransaction(
4341
+ tx,
4342
+ additionalSigners,
4343
+ this.opts
4344
+ );
4345
+
4247
4346
  return txSig;
4248
4347
  }
4249
4348
 
@@ -4324,12 +4423,33 @@ export class DriftClient {
4324
4423
  marketIndex: number,
4325
4424
  collateralAccountPublicKey: PublicKey
4326
4425
  ): Promise<TransactionSignature> {
4426
+ const tx = new Transaction();
4327
4427
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
4328
4428
  const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
4329
4429
  this.program.programId,
4330
4430
  this.wallet.publicKey,
4331
4431
  marketIndex
4332
4432
  );
4433
+ const tokenAccount = collateralAccountPublicKey;
4434
+
4435
+ // Todo wsol remove iF stake... how do we determine the amount?
4436
+ // const amount = // get balance here...?
4437
+
4438
+ // const additionalSigners: Array<Signer> = [];
4439
+ // const isSolMarket = spotMarketAccount.mint.equals(WRAPPED_SOL_MINT);
4440
+ // const createWSOLTokenAccount =
4441
+ // isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
4442
+
4443
+ // if (createWSOLTokenAccount) {
4444
+ // const { ixs, signers, pubkey } =
4445
+ // await this.getWrappedSolAccountCreationIxs(amount, true);
4446
+ // tokenAccount = pubkey;
4447
+ // ixs.forEach((ix) => {
4448
+ // tx.add(ix);
4449
+ // });
4450
+
4451
+ // signers.forEach((signer) => additionalSigners.push(signer));
4452
+ // }
4333
4453
 
4334
4454
  const remainingAccounts = this.getRemainingAccounts({
4335
4455
  userAccounts: [this.getUserAccount()],
@@ -4337,9 +4457,8 @@ export class DriftClient {
4337
4457
  writableSpotMarketIndexes: [marketIndex],
4338
4458
  });
4339
4459
 
4340
- const tx = await this.program.transaction.removeInsuranceFundStake(
4341
- marketIndex,
4342
- {
4460
+ const removeStakeIx =
4461
+ await this.program.instruction.removeInsuranceFundStake(marketIndex, {
4343
4462
  accounts: {
4344
4463
  state: await this.getStatePublicKey(),
4345
4464
  spotMarket: spotMarketAccount.pubkey,
@@ -4348,12 +4467,13 @@ export class DriftClient {
4348
4467
  authority: this.wallet.publicKey,
4349
4468
  insuranceFundVault: spotMarketAccount.insuranceFund.vault,
4350
4469
  driftSigner: this.getSignerPublicKey(),
4351
- userTokenAccount: collateralAccountPublicKey,
4470
+ userTokenAccount: tokenAccount,
4352
4471
  tokenProgram: TOKEN_PROGRAM_ID,
4353
4472
  },
4354
4473
  remainingAccounts,
4355
- }
4356
- );
4474
+ });
4475
+
4476
+ tx.add(removeStakeIx);
4357
4477
 
4358
4478
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
4359
4479
  return txSig;
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.23.0",
2
+ "version": "2.24.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -2283,6 +2283,10 @@
2283
2283
  }
2284
2284
  ],
2285
2285
  "args": [
2286
+ {
2287
+ "name": "marketIndex",
2288
+ "type": "u16"
2289
+ },
2286
2290
  {
2287
2291
  "name": "ammBaseAssetReserve",
2288
2292
  "type": "u128"
@@ -2332,6 +2336,32 @@
2332
2336
  }
2333
2337
  ]
2334
2338
  },
2339
+ {
2340
+ "name": "deleteInitializedPerpMarket",
2341
+ "accounts": [
2342
+ {
2343
+ "name": "admin",
2344
+ "isMut": true,
2345
+ "isSigner": true
2346
+ },
2347
+ {
2348
+ "name": "state",
2349
+ "isMut": true,
2350
+ "isSigner": false
2351
+ },
2352
+ {
2353
+ "name": "perpMarket",
2354
+ "isMut": true,
2355
+ "isSigner": false
2356
+ }
2357
+ ],
2358
+ "args": [
2359
+ {
2360
+ "name": "marketIndex",
2361
+ "type": "u16"
2362
+ }
2363
+ ]
2364
+ },
2335
2365
  {
2336
2366
  "name": "moveAmmPrice",
2337
2367
  "accounts": [
@@ -8826,6 +8856,11 @@
8826
8856
  "code": 6239,
8827
8857
  "name": "RevertFill",
8828
8858
  "msg": "RevertFill"
8859
+ },
8860
+ {
8861
+ "code": 6240,
8862
+ "name": "InvalidMarketAccountforDeletion",
8863
+ "msg": "Invalid MarketAccount for Deletion"
8829
8864
  }
8830
8865
  ]
8831
8866
  }
package/src/types.ts CHANGED
@@ -974,6 +974,8 @@ export type OracleGuardRails = {
974
974
  export type MarginCategory = 'Initial' | 'Maintenance';
975
975
 
976
976
  export type InsuranceFundStake = {
977
+ costBasis: BN;
978
+
977
979
  marketIndex: number;
978
980
  authority: PublicKey;
979
981