@drift-labs/sdk 2.145.0-beta.2 → 2.145.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.
Files changed (73) hide show
  1. package/VERSION +1 -1
  2. package/lib/browser/accounts/types.d.ts +13 -1
  3. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.d.ts +53 -0
  4. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.js +453 -0
  5. package/lib/browser/addresses/pda.d.ts +9 -0
  6. package/lib/browser/addresses/pda.js +60 -1
  7. package/lib/browser/adminClient.d.ts +160 -8
  8. package/lib/browser/adminClient.js +754 -18
  9. package/lib/browser/constituentMap/constituentMap.d.ts +64 -0
  10. package/lib/browser/constituentMap/constituentMap.js +170 -0
  11. package/lib/browser/constituentMap/pollingConstituentAccountSubscriber.d.ts +24 -0
  12. package/lib/browser/constituentMap/pollingConstituentAccountSubscriber.js +60 -0
  13. package/lib/browser/constituentMap/webSocketConstituentAccountSubscriber.d.ts +24 -0
  14. package/lib/browser/constituentMap/webSocketConstituentAccountSubscriber.js +58 -0
  15. package/lib/browser/driftClient.d.ts +89 -2
  16. package/lib/browser/driftClient.js +486 -27
  17. package/lib/browser/driftClientConfig.d.ts +2 -7
  18. package/lib/browser/idl/drift.json +4304 -1380
  19. package/lib/browser/index.d.ts +1 -4
  20. package/lib/browser/index.js +2 -9
  21. package/lib/browser/memcmp.d.ts +3 -1
  22. package/lib/browser/memcmp.js +19 -1
  23. package/lib/browser/types.d.ts +147 -0
  24. package/lib/browser/types.js +13 -1
  25. package/lib/node/accounts/types.d.ts +13 -1
  26. package/lib/node/accounts/types.d.ts.map +1 -1
  27. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +54 -0
  28. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +1 -0
  29. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.js +453 -0
  30. package/lib/node/addresses/pda.d.ts +9 -0
  31. package/lib/node/addresses/pda.d.ts.map +1 -1
  32. package/lib/node/addresses/pda.js +60 -1
  33. package/lib/node/adminClient.d.ts +160 -8
  34. package/lib/node/adminClient.d.ts.map +1 -1
  35. package/lib/node/adminClient.js +754 -18
  36. package/lib/node/constituentMap/constituentMap.d.ts +65 -0
  37. package/lib/node/constituentMap/constituentMap.d.ts.map +1 -0
  38. package/lib/node/constituentMap/constituentMap.js +170 -0
  39. package/lib/node/constituentMap/pollingConstituentAccountSubscriber.d.ts +25 -0
  40. package/lib/node/constituentMap/pollingConstituentAccountSubscriber.d.ts.map +1 -0
  41. package/lib/node/constituentMap/pollingConstituentAccountSubscriber.js +60 -0
  42. package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.d.ts +25 -0
  43. package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.d.ts.map +1 -0
  44. package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.js +58 -0
  45. package/lib/node/driftClient.d.ts +89 -2
  46. package/lib/node/driftClient.d.ts.map +1 -1
  47. package/lib/node/driftClient.js +486 -27
  48. package/lib/node/driftClientConfig.d.ts +2 -7
  49. package/lib/node/driftClientConfig.d.ts.map +1 -1
  50. package/lib/node/idl/drift.json +4304 -1380
  51. package/lib/node/index.d.ts +1 -4
  52. package/lib/node/index.d.ts.map +1 -1
  53. package/lib/node/index.js +2 -9
  54. package/lib/node/memcmp.d.ts +3 -1
  55. package/lib/node/memcmp.d.ts.map +1 -1
  56. package/lib/node/memcmp.js +19 -1
  57. package/lib/node/types.d.ts +147 -0
  58. package/lib/node/types.d.ts.map +1 -1
  59. package/lib/node/types.js +13 -1
  60. package/package.json +1 -1
  61. package/src/accounts/types.ts +20 -0
  62. package/src/accounts/webSocketProgramAccountSubscriberV2.ts +596 -0
  63. package/src/addresses/pda.ts +115 -1
  64. package/src/adminClient.ts +1612 -41
  65. package/src/constituentMap/constituentMap.ts +285 -0
  66. package/src/constituentMap/pollingConstituentAccountSubscriber.ts +97 -0
  67. package/src/constituentMap/webSocketConstituentAccountSubscriber.ts +112 -0
  68. package/src/driftClient.ts +1097 -17
  69. package/src/driftClientConfig.ts +8 -15
  70. package/src/idl/drift.json +4304 -1380
  71. package/src/index.ts +1 -4
  72. package/src/memcmp.ts +23 -1
  73. package/src/types.ts +160 -0
@@ -205,10 +205,13 @@ class AdminClient extends driftClient_1.DriftClient {
205
205
  },
206
206
  });
207
207
  }
208
- async initializePerpMarket(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME) {
208
+ async initializePerpMarket(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME, lpPoolId = 0) {
209
209
  const currentPerpMarketIndex = this.getStateAccount().numberOfMarkets;
210
- const initializeMarketIx = await this.getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, name);
211
- const tx = await this.buildTransaction(initializeMarketIx);
210
+ const ammCachePublicKey = (0, pda_1.getAmmCachePublicKey)(this.program.programId);
211
+ const ammCacheAccount = await this.connection.getAccountInfo(ammCachePublicKey);
212
+ const mustInitializeAmmCache = (ammCacheAccount === null || ammCacheAccount === void 0 ? void 0 : ammCacheAccount.data) == null;
213
+ const initializeMarketIxs = await this.getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, name, mustInitializeAmmCache, lpPoolId);
214
+ const tx = await this.buildTransaction(initializeMarketIxs);
212
215
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
213
216
  while (this.getStateAccount().numberOfMarkets <= currentPerpMarketIndex) {
214
217
  await this.fetchAccounts();
@@ -221,10 +224,14 @@ class AdminClient extends driftClient_1.DriftClient {
221
224
  await this.accountSubscriber.setPerpOracleMap();
222
225
  return txSig;
223
226
  }
224
- async getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME) {
227
+ async getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME, includeInitAmmCacheIx = false, lpPoolId = 0) {
225
228
  const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
229
+ const ixs = [];
230
+ if (includeInitAmmCacheIx) {
231
+ ixs.push(await this.getInitializeAmmCacheIx());
232
+ }
226
233
  const nameBuffer = (0, userName_1.encodeName)(name);
227
- return await this.program.instruction.initializePerpMarket(marketIndex, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, nameBuffer, {
234
+ const initPerpIx = await this.program.instruction.initializePerpMarket(marketIndex, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, nameBuffer, lpPoolId, {
228
235
  accounts: {
229
236
  state: await this.getStatePublicKey(),
230
237
  admin: this.isSubscribed
@@ -232,11 +239,98 @@ class AdminClient extends driftClient_1.DriftClient {
232
239
  : this.wallet.publicKey,
233
240
  oracle: priceOracle,
234
241
  perpMarket: perpMarketPublicKey,
242
+ ammCache: (0, pda_1.getAmmCachePublicKey)(this.program.programId),
243
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
244
+ systemProgram: anchor.web3.SystemProgram.programId,
245
+ },
246
+ });
247
+ ixs.push(initPerpIx);
248
+ return ixs;
249
+ }
250
+ async initializeAmmCache(txParams) {
251
+ const initializeAmmCacheIx = await this.getInitializeAmmCacheIx();
252
+ const tx = await this.buildTransaction(initializeAmmCacheIx, txParams);
253
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
254
+ return txSig;
255
+ }
256
+ async getInitializeAmmCacheIx() {
257
+ return await this.program.instruction.initializeAmmCache({
258
+ accounts: {
259
+ state: await this.getStatePublicKey(),
260
+ admin: this.isSubscribed
261
+ ? this.getStateAccount().admin
262
+ : this.wallet.publicKey,
263
+ ammCache: (0, pda_1.getAmmCachePublicKey)(this.program.programId),
235
264
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
236
265
  systemProgram: anchor.web3.SystemProgram.programId,
237
266
  },
238
267
  });
239
268
  }
269
+ async updateInitialAmmCacheInfo(perpMarketIndexes, txParams) {
270
+ const initializeAmmCacheIx = await this.getUpdateInitialAmmCacheInfoIx(perpMarketIndexes);
271
+ const tx = await this.buildTransaction(initializeAmmCacheIx, txParams);
272
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
273
+ return txSig;
274
+ }
275
+ async getUpdateInitialAmmCacheInfoIx(perpMarketIndexes) {
276
+ const remainingAccounts = this.getRemainingAccounts({
277
+ userAccounts: [],
278
+ readablePerpMarketIndex: perpMarketIndexes,
279
+ readableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
280
+ });
281
+ return await this.program.instruction.updateInitialAmmCacheInfo({
282
+ accounts: {
283
+ state: await this.getStatePublicKey(),
284
+ admin: this.isSubscribed
285
+ ? this.getStateAccount().admin
286
+ : this.wallet.publicKey,
287
+ ammCache: (0, pda_1.getAmmCachePublicKey)(this.program.programId),
288
+ },
289
+ remainingAccounts,
290
+ });
291
+ }
292
+ async overrideAmmCacheInfo(perpMarketIndex, params, txParams) {
293
+ const initializeAmmCacheIx = await this.getOverrideAmmCacheInfoIx(perpMarketIndex, params);
294
+ const tx = await this.buildTransaction(initializeAmmCacheIx, txParams);
295
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
296
+ return txSig;
297
+ }
298
+ async getOverrideAmmCacheInfoIx(perpMarketIndex, params) {
299
+ return this.program.instruction.overrideAmmCacheInfo(perpMarketIndex, Object.assign({}, {
300
+ quoteOwedFromLpPool: null,
301
+ lastSettleSlot: null,
302
+ lastFeePoolTokenAmount: null,
303
+ lastNetPnlPoolTokenAmount: null,
304
+ ammPositionScalar: null,
305
+ ammInventoryLimit: null,
306
+ }, params), {
307
+ accounts: {
308
+ state: await this.getStatePublicKey(),
309
+ admin: this.isSubscribed
310
+ ? this.getStateAccount().admin
311
+ : this.wallet.publicKey,
312
+ ammCache: (0, pda_1.getAmmCachePublicKey)(this.program.programId),
313
+ },
314
+ });
315
+ }
316
+ async resetAmmCache(txParams) {
317
+ const initializeAmmCacheIx = await this.getResetAmmCacheIx();
318
+ const tx = await this.buildTransaction(initializeAmmCacheIx, txParams);
319
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
320
+ return txSig;
321
+ }
322
+ async getResetAmmCacheIx() {
323
+ return this.program.instruction.resetAmmCache({
324
+ accounts: {
325
+ state: await this.getStatePublicKey(),
326
+ admin: this.isSubscribed
327
+ ? this.getStateAccount().admin
328
+ : this.wallet.publicKey,
329
+ ammCache: (0, pda_1.getAmmCachePublicKey)(this.program.programId),
330
+ systemProgram: anchor.web3.SystemProgram.programId,
331
+ },
332
+ });
333
+ }
240
334
  async initializePredictionMarket(perpMarketIndex) {
241
335
  const updatePerpMarketConcentrationCoefIx = await this.getInitializePredictionMarketIx(perpMarketIndex);
242
336
  const tx = await this.buildTransaction(updatePerpMarketConcentrationCoefIx);
@@ -365,6 +459,41 @@ class AdminClient extends driftClient_1.DriftClient {
365
459
  },
366
460
  });
367
461
  }
462
+ async updatePerpMarketLpPoolId(perpMarketIndex, lpPoolId) {
463
+ const updatePerpMarketLpPoolIIx = await this.getUpdatePerpMarketLpPoolIdIx(perpMarketIndex, lpPoolId);
464
+ const tx = await this.buildTransaction(updatePerpMarketLpPoolIIx);
465
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
466
+ return txSig;
467
+ }
468
+ async getUpdatePerpMarketLpPoolIdIx(perpMarketIndex, lpPoolId) {
469
+ return await this.program.instruction.updatePerpMarketLpPoolId(lpPoolId, {
470
+ accounts: {
471
+ state: await this.getStatePublicKey(),
472
+ admin: this.isSubscribed
473
+ ? this.getStateAccount().admin
474
+ : this.wallet.publicKey,
475
+ perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
476
+ },
477
+ });
478
+ }
479
+ async updatePerpMarketLpPoolStatus(perpMarketIndex, lpStatus) {
480
+ const updatePerpMarketLpPoolStatusIx = await this.getUpdatePerpMarketLpPoolStatusIx(perpMarketIndex, lpStatus);
481
+ const tx = await this.buildTransaction(updatePerpMarketLpPoolStatusIx);
482
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
483
+ return txSig;
484
+ }
485
+ async getUpdatePerpMarketLpPoolStatusIx(perpMarketIndex, lpStatus) {
486
+ return await this.program.instruction.updatePerpMarketLpPoolStatus(lpStatus, {
487
+ accounts: {
488
+ state: await this.getStatePublicKey(),
489
+ admin: this.isSubscribed
490
+ ? this.getStateAccount().admin
491
+ : this.wallet.publicKey,
492
+ perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
493
+ ammCache: (0, pda_1.getAmmCachePublicKey)(this.program.programId),
494
+ },
495
+ });
496
+ }
368
497
  async moveAmmToPrice(perpMarketIndex, targetPrice) {
369
498
  const moveAmmPriceIx = await this.getMoveAmmToPriceIx(perpMarketIndex, targetPrice);
370
499
  const tx = await this.buildTransaction(moveAmmPriceIx);
@@ -455,6 +584,13 @@ class AdminClient extends driftClient_1.DriftClient {
455
584
  }
456
585
  async getDepositIntoPerpMarketFeePoolIx(perpMarketIndex, amount, sourceVault) {
457
586
  const spotMarket = this.getQuoteSpotMarketAccount();
587
+ const remainingAccounts = [
588
+ {
589
+ pubkey: spotMarket.mint,
590
+ isWritable: false,
591
+ isSigner: false,
592
+ },
593
+ ];
458
594
  return await this.program.instruction.depositIntoPerpMarketFeePool(amount, {
459
595
  accounts: {
460
596
  admin: this.isSubscribed
@@ -468,6 +604,7 @@ class AdminClient extends driftClient_1.DriftClient {
468
604
  spotMarketVault: spotMarket.vault,
469
605
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
470
606
  },
607
+ remainingAccounts,
471
608
  });
472
609
  }
473
610
  async updatePerpMarketPnlPool(perpMarketIndex, amount) {
@@ -1077,6 +1214,7 @@ class AdminClient extends driftClient_1.DriftClient {
1077
1214
  perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
1078
1215
  oracle: oracle,
1079
1216
  oldOracle: this.getPerpMarketAccount(perpMarketIndex).amm.oracle,
1217
+ ammCache: (0, pda_1.getAmmCachePublicKey)(this.program.programId),
1080
1218
  },
1081
1219
  });
1082
1220
  }
@@ -1450,6 +1588,7 @@ class AdminClient extends driftClient_1.DriftClient {
1450
1588
  : this.wallet.publicKey,
1451
1589
  state: await this.getStatePublicKey(),
1452
1590
  perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
1591
+ ammCache: (0, pda_1.getAmmCachePublicKey)(this.program.programId),
1453
1592
  },
1454
1593
  });
1455
1594
  }
@@ -2148,9 +2287,9 @@ class AdminClient extends driftClient_1.DriftClient {
2148
2287
  async getZeroMMOracleFieldsIx(marketIndex) {
2149
2288
  return await this.program.instruction.zeroMmOracleFields({
2150
2289
  accounts: {
2151
- admin: this.isSubscribed
2152
- ? this.getStateAccount().admin
2153
- : this.wallet.publicKey,
2290
+ admin: this.useHotWalletAdmin
2291
+ ? this.wallet.publicKey
2292
+ : this.getStateAccount().admin,
2154
2293
  state: await this.getStatePublicKey(),
2155
2294
  perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex),
2156
2295
  },
@@ -2172,6 +2311,38 @@ class AdminClient extends driftClient_1.DriftClient {
2172
2311
  },
2173
2312
  });
2174
2313
  }
2314
+ async updateFeatureBitFlagsBuilderCodes(enable) {
2315
+ const updateFeatureBitFlagsBuilderCodesIx = await this.getUpdateFeatureBitFlagsBuilderCodesIx(enable);
2316
+ const tx = await this.buildTransaction(updateFeatureBitFlagsBuilderCodesIx);
2317
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2318
+ return txSig;
2319
+ }
2320
+ async getUpdateFeatureBitFlagsBuilderCodesIx(enable) {
2321
+ return this.program.instruction.updateFeatureBitFlagsBuilderCodes(enable, {
2322
+ accounts: {
2323
+ admin: this.useHotWalletAdmin
2324
+ ? this.wallet.publicKey
2325
+ : this.getStateAccount().admin,
2326
+ state: await this.getStatePublicKey(),
2327
+ },
2328
+ });
2329
+ }
2330
+ async updateFeatureBitFlagsBuilderReferral(enable) {
2331
+ const updateFeatureBitFlagsBuilderReferralIx = await this.getUpdateFeatureBitFlagsBuilderReferralIx(enable);
2332
+ const tx = await this.buildTransaction(updateFeatureBitFlagsBuilderReferralIx);
2333
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2334
+ return txSig;
2335
+ }
2336
+ async getUpdateFeatureBitFlagsBuilderReferralIx(enable) {
2337
+ return this.program.instruction.updateFeatureBitFlagsBuilderReferral(enable, {
2338
+ accounts: {
2339
+ admin: this.useHotWalletAdmin
2340
+ ? this.wallet.publicKey
2341
+ : this.getStateAccount().admin,
2342
+ state: await this.getStatePublicKey(),
2343
+ },
2344
+ });
2345
+ }
2175
2346
  async updateFeatureBitFlagsMedianTriggerPrice(enable) {
2176
2347
  const updateFeatureBitFlagsMedianTriggerPriceIx = await this.getUpdateFeatureBitFlagsMedianTriggerPriceIx(enable);
2177
2348
  const tx = await this.buildTransaction(updateFeatureBitFlagsMedianTriggerPriceIx);
@@ -2233,14 +2404,14 @@ class AdminClient extends driftClient_1.DriftClient {
2233
2404
  },
2234
2405
  });
2235
2406
  }
2236
- async updateFeatureBitFlagsBuilderCodes(enable) {
2237
- const updateFeatureBitFlagsBuilderCodesIx = await this.getUpdateFeatureBitFlagsBuilderCodesIx(enable);
2238
- const tx = await this.buildTransaction(updateFeatureBitFlagsBuilderCodesIx);
2407
+ async updateFeatureBitFlagsSettleLpPool(enable) {
2408
+ const updateFeatureBitFlagsSettleLpPoolIx = await this.getUpdateFeatureBitFlagsSettleLpPoolIx(enable);
2409
+ const tx = await this.buildTransaction(updateFeatureBitFlagsSettleLpPoolIx);
2239
2410
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
2240
2411
  return txSig;
2241
2412
  }
2242
- async getUpdateFeatureBitFlagsBuilderCodesIx(enable) {
2243
- return this.program.instruction.updateFeatureBitFlagsBuilderCodes(enable, {
2413
+ async getUpdateFeatureBitFlagsSettleLpPoolIx(enable) {
2414
+ return await this.program.instruction.updateFeatureBitFlagsSettleLpPool(enable, {
2244
2415
  accounts: {
2245
2416
  admin: this.useHotWalletAdmin
2246
2417
  ? this.wallet.publicKey
@@ -2249,14 +2420,30 @@ class AdminClient extends driftClient_1.DriftClient {
2249
2420
  },
2250
2421
  });
2251
2422
  }
2252
- async updateFeatureBitFlagsBuilderReferral(enable) {
2253
- const updateFeatureBitFlagsBuilderReferralIx = await this.getUpdateFeatureBitFlagsBuilderReferralIx(enable);
2254
- const tx = await this.buildTransaction(updateFeatureBitFlagsBuilderReferralIx);
2423
+ async updateFeatureBitFlagsSwapLpPool(enable) {
2424
+ const updateFeatureBitFlagsSettleLpPoolIx = await this.getUpdateFeatureBitFlagsSwapLpPoolIx(enable);
2425
+ const tx = await this.buildTransaction(updateFeatureBitFlagsSettleLpPoolIx);
2255
2426
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
2256
2427
  return txSig;
2257
2428
  }
2258
- async getUpdateFeatureBitFlagsBuilderReferralIx(enable) {
2259
- return this.program.instruction.updateFeatureBitFlagsBuilderReferral(enable, {
2429
+ async getUpdateFeatureBitFlagsSwapLpPoolIx(enable) {
2430
+ return await this.program.instruction.updateFeatureBitFlagsSwapLpPool(enable, {
2431
+ accounts: {
2432
+ admin: this.useHotWalletAdmin
2433
+ ? this.wallet.publicKey
2434
+ : this.getStateAccount().admin,
2435
+ state: await this.getStatePublicKey(),
2436
+ },
2437
+ });
2438
+ }
2439
+ async updateFeatureBitFlagsMintRedeemLpPool(enable) {
2440
+ const updateFeatureBitFlagsSettleLpPoolIx = await this.getUpdateFeatureBitFlagsMintRedeemLpPoolIx(enable);
2441
+ const tx = await this.buildTransaction(updateFeatureBitFlagsSettleLpPoolIx);
2442
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2443
+ return txSig;
2444
+ }
2445
+ async getUpdateFeatureBitFlagsMintRedeemLpPoolIx(enable) {
2446
+ return await this.program.instruction.updateFeatureBitFlagsMintRedeemLpPool(enable, {
2260
2447
  accounts: {
2261
2448
  admin: this.useHotWalletAdmin
2262
2449
  ? this.wallet.publicKey
@@ -2282,5 +2469,554 @@ class AdminClient extends driftClient_1.DriftClient {
2282
2469
  },
2283
2470
  });
2284
2471
  }
2472
+ async initializeLpPool(lpPoolId, minMintFee, maxAum, maxSettleQuoteAmountPerMarket, mint, whitelistMint) {
2473
+ const ixs = await this.getInitializeLpPoolIx(lpPoolId, minMintFee, maxAum, maxSettleQuoteAmountPerMarket, mint, whitelistMint);
2474
+ const tx = await this.buildTransaction(ixs);
2475
+ const { txSig } = await this.sendTransaction(tx, [mint]);
2476
+ return txSig;
2477
+ }
2478
+ async getInitializeLpPoolIx(lpPoolId, minMintFee, maxAum, maxSettleQuoteAmountPerMarket, mint, whitelistMint) {
2479
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2480
+ const ammConstituentMapping = (0, pda_1.getAmmConstituentMappingPublicKey)(this.program.programId, lpPool);
2481
+ const constituentTargetBase = (0, pda_1.getConstituentTargetBasePublicKey)(this.program.programId, lpPool);
2482
+ const lamports = await this.program.provider.connection.getMinimumBalanceForRentExemption(spl_token_1.MINT_SIZE);
2483
+ const createMintAccountIx = web3_js_1.SystemProgram.createAccount({
2484
+ fromPubkey: this.wallet.publicKey,
2485
+ newAccountPubkey: mint.publicKey,
2486
+ space: spl_token_1.MINT_SIZE,
2487
+ lamports: Math.min(0.05 * web3_js_1.LAMPORTS_PER_SOL, lamports), // should be 0.0014616 ? but bankrun returns 10 SOL
2488
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
2489
+ });
2490
+ const createMintIx = (0, spl_token_1.createInitializeMint2Instruction)(mint.publicKey, 6, lpPool, null, spl_token_1.TOKEN_PROGRAM_ID);
2491
+ return [
2492
+ createMintAccountIx,
2493
+ createMintIx,
2494
+ this.program.instruction.initializeLpPool(lpPoolId, minMintFee, maxAum, maxSettleQuoteAmountPerMarket, whitelistMint !== null && whitelistMint !== void 0 ? whitelistMint : web3_js_1.PublicKey.default, {
2495
+ accounts: {
2496
+ admin: this.wallet.publicKey,
2497
+ lpPool,
2498
+ lpPoolTokenVault: (0, pda_1.getLpPoolTokenVaultPublicKey)(this.program.programId, lpPool),
2499
+ constituentCorrelations: (0, pda_1.getConstituentCorrelationsPublicKey)(this.program.programId, lpPool),
2500
+ ammConstituentMapping,
2501
+ constituentTargetBase,
2502
+ mint: mint.publicKey,
2503
+ state: await this.getStatePublicKey(),
2504
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2505
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
2506
+ systemProgram: web3_js_1.SystemProgram.programId,
2507
+ },
2508
+ signers: [mint],
2509
+ }),
2510
+ ];
2511
+ }
2512
+ async initializeConstituent(lpPoolId, initializeConstituentParams) {
2513
+ const ixs = await this.getInitializeConstituentIx(lpPoolId, initializeConstituentParams);
2514
+ const tx = await this.buildTransaction(ixs);
2515
+ const { txSig } = await this.sendTransaction(tx, []);
2516
+ return txSig;
2517
+ }
2518
+ async getInitializeConstituentIx(lpPoolId, initializeConstituentParams) {
2519
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2520
+ const spotMarketIndex = initializeConstituentParams.spotMarketIndex;
2521
+ const constituentTargetBase = (0, pda_1.getConstituentTargetBasePublicKey)(this.program.programId, lpPool);
2522
+ const constituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool, spotMarketIndex);
2523
+ const spotMarketAccount = this.getSpotMarketAccount(spotMarketIndex);
2524
+ return [
2525
+ this.program.instruction.initializeConstituent(spotMarketIndex, initializeConstituentParams.decimals, initializeConstituentParams.maxWeightDeviation, initializeConstituentParams.swapFeeMin, initializeConstituentParams.swapFeeMax, initializeConstituentParams.maxBorrowTokenAmount, initializeConstituentParams.oracleStalenessThreshold, initializeConstituentParams.costToTrade, initializeConstituentParams.constituentDerivativeIndex != null
2526
+ ? initializeConstituentParams.constituentDerivativeIndex
2527
+ : null, initializeConstituentParams.constituentDerivativeDepegThreshold != null
2528
+ ? initializeConstituentParams.constituentDerivativeDepegThreshold
2529
+ : numericConstants_1.ZERO, initializeConstituentParams.constituentDerivativeIndex != null
2530
+ ? initializeConstituentParams.derivativeWeight
2531
+ : numericConstants_1.ZERO, initializeConstituentParams.volatility != null
2532
+ ? initializeConstituentParams.volatility
2533
+ : 10, initializeConstituentParams.gammaExecution != null
2534
+ ? initializeConstituentParams.gammaExecution
2535
+ : 2, initializeConstituentParams.gammaInventory != null
2536
+ ? initializeConstituentParams.gammaInventory
2537
+ : 2, initializeConstituentParams.xi != null
2538
+ ? initializeConstituentParams.xi
2539
+ : 2, initializeConstituentParams.constituentCorrelations, {
2540
+ accounts: {
2541
+ admin: this.wallet.publicKey,
2542
+ lpPool,
2543
+ constituentTargetBase,
2544
+ constituent,
2545
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
2546
+ systemProgram: web3_js_1.SystemProgram.programId,
2547
+ state: await this.getStatePublicKey(),
2548
+ spotMarketMint: spotMarketAccount.mint,
2549
+ constituentVault: (0, pda_1.getConstituentVaultPublicKey)(this.program.programId, lpPool, spotMarketIndex),
2550
+ constituentCorrelations: (0, pda_1.getConstituentCorrelationsPublicKey)(this.program.programId, lpPool),
2551
+ spotMarket: spotMarketAccount.pubkey,
2552
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2553
+ },
2554
+ signers: [],
2555
+ }),
2556
+ ];
2557
+ }
2558
+ async updateConstituentStatus(constituent, constituentStatus) {
2559
+ const updateConstituentStatusIx = await this.getUpdateConstituentStatusIx(constituent, constituentStatus);
2560
+ const tx = await this.buildTransaction(updateConstituentStatusIx);
2561
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2562
+ return txSig;
2563
+ }
2564
+ async getUpdateConstituentStatusIx(constituent, constituentStatus) {
2565
+ return await this.program.instruction.updateConstituentStatus(constituentStatus, {
2566
+ accounts: {
2567
+ constituent,
2568
+ admin: this.isSubscribed
2569
+ ? this.getStateAccount().admin
2570
+ : this.wallet.publicKey,
2571
+ state: await this.getStatePublicKey(),
2572
+ },
2573
+ });
2574
+ }
2575
+ async updateConstituentPausedOperations(constituent, pausedOperations) {
2576
+ const updateConstituentPausedOperationsIx = await this.getUpdateConstituentPausedOperationsIx(constituent, pausedOperations);
2577
+ const tx = await this.buildTransaction(updateConstituentPausedOperationsIx);
2578
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2579
+ return txSig;
2580
+ }
2581
+ async getUpdateConstituentPausedOperationsIx(constituent, pausedOperations) {
2582
+ return await this.program.instruction.updateConstituentPausedOperations(pausedOperations, {
2583
+ accounts: {
2584
+ constituent,
2585
+ admin: this.isSubscribed
2586
+ ? this.getStateAccount().admin
2587
+ : this.wallet.publicKey,
2588
+ state: await this.getStatePublicKey(),
2589
+ },
2590
+ });
2591
+ }
2592
+ async updateConstituentParams(lpPoolId, constituentPublicKey, updateConstituentParams) {
2593
+ const ixs = await this.getUpdateConstituentParamsIx(lpPoolId, constituentPublicKey, updateConstituentParams);
2594
+ const tx = await this.buildTransaction(ixs);
2595
+ const { txSig } = await this.sendTransaction(tx, []);
2596
+ return txSig;
2597
+ }
2598
+ async getUpdateConstituentParamsIx(lpPoolId, constituentPublicKey, updateConstituentParams) {
2599
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2600
+ return [
2601
+ this.program.instruction.updateConstituentParams(Object.assign({
2602
+ maxWeightDeviation: null,
2603
+ swapFeeMin: null,
2604
+ swapFeeMax: null,
2605
+ maxBorrowTokenAmount: null,
2606
+ oracleStalenessThreshold: null,
2607
+ costToTradeBps: null,
2608
+ stablecoinWeight: null,
2609
+ derivativeWeight: null,
2610
+ constituentDerivativeIndex: null,
2611
+ volatility: null,
2612
+ gammaExecution: null,
2613
+ gammaInventory: null,
2614
+ xi: null,
2615
+ }, updateConstituentParams), {
2616
+ accounts: {
2617
+ admin: this.wallet.publicKey,
2618
+ constituent: constituentPublicKey,
2619
+ state: await this.getStatePublicKey(),
2620
+ lpPool,
2621
+ constituentTargetBase: (0, pda_1.getConstituentTargetBasePublicKey)(this.program.programId, lpPool),
2622
+ },
2623
+ signers: [],
2624
+ }),
2625
+ ];
2626
+ }
2627
+ async updateLpPoolParams(lpPoolId, updateLpPoolParams) {
2628
+ const ixs = await this.getUpdateLpPoolParamsIx(lpPoolId, updateLpPoolParams);
2629
+ const tx = await this.buildTransaction(ixs);
2630
+ const { txSig } = await this.sendTransaction(tx, []);
2631
+ return txSig;
2632
+ }
2633
+ async getUpdateLpPoolParamsIx(lpPoolId, updateLpPoolParams) {
2634
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2635
+ return [
2636
+ this.program.instruction.updateLpPoolParams(Object.assign({
2637
+ maxSettleQuoteAmount: null,
2638
+ volatility: null,
2639
+ gammaExecution: null,
2640
+ xi: null,
2641
+ whitelistMint: null,
2642
+ maxAum: null,
2643
+ }, updateLpPoolParams), {
2644
+ accounts: {
2645
+ admin: this.wallet.publicKey,
2646
+ state: await this.getStatePublicKey(),
2647
+ lpPool,
2648
+ },
2649
+ signers: [],
2650
+ }),
2651
+ ];
2652
+ }
2653
+ async addAmmConstituentMappingData(lpPoolId, addAmmConstituentMappingData) {
2654
+ const ixs = await this.getAddAmmConstituentMappingDataIx(lpPoolId, addAmmConstituentMappingData);
2655
+ const tx = await this.buildTransaction(ixs);
2656
+ const { txSig } = await this.sendTransaction(tx, []);
2657
+ return txSig;
2658
+ }
2659
+ async getAddAmmConstituentMappingDataIx(lpPoolId, addAmmConstituentMappingData) {
2660
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2661
+ const ammConstituentMapping = (0, pda_1.getAmmConstituentMappingPublicKey)(this.program.programId, lpPool);
2662
+ const constituentTargetBase = (0, pda_1.getConstituentTargetBasePublicKey)(this.program.programId, lpPool);
2663
+ return [
2664
+ this.program.instruction.addAmmConstituentMappingData(addAmmConstituentMappingData, {
2665
+ accounts: {
2666
+ admin: this.wallet.publicKey,
2667
+ lpPool,
2668
+ ammConstituentMapping,
2669
+ constituentTargetBase,
2670
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
2671
+ systemProgram: web3_js_1.SystemProgram.programId,
2672
+ state: await this.getStatePublicKey(),
2673
+ },
2674
+ }),
2675
+ ];
2676
+ }
2677
+ async updateAmmConstituentMappingData(lpPoolId, addAmmConstituentMappingData) {
2678
+ const ixs = await this.getUpdateAmmConstituentMappingDataIx(lpPoolId, addAmmConstituentMappingData);
2679
+ const tx = await this.buildTransaction(ixs);
2680
+ const { txSig } = await this.sendTransaction(tx, []);
2681
+ return txSig;
2682
+ }
2683
+ async getUpdateAmmConstituentMappingDataIx(lpPoolId, addAmmConstituentMappingData) {
2684
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2685
+ const ammConstituentMapping = (0, pda_1.getAmmConstituentMappingPublicKey)(this.program.programId, lpPool);
2686
+ return [
2687
+ this.program.instruction.updateAmmConstituentMappingData(addAmmConstituentMappingData, {
2688
+ accounts: {
2689
+ admin: this.wallet.publicKey,
2690
+ lpPool,
2691
+ ammConstituentMapping,
2692
+ systemProgram: web3_js_1.SystemProgram.programId,
2693
+ state: await this.getStatePublicKey(),
2694
+ },
2695
+ }),
2696
+ ];
2697
+ }
2698
+ async removeAmmConstituentMappingData(lpPoolId, perpMarketIndex, constituentIndex) {
2699
+ const ixs = await this.getRemoveAmmConstituentMappingDataIx(lpPoolId, perpMarketIndex, constituentIndex);
2700
+ const tx = await this.buildTransaction(ixs);
2701
+ const { txSig } = await this.sendTransaction(tx, []);
2702
+ return txSig;
2703
+ }
2704
+ async getRemoveAmmConstituentMappingDataIx(lpPoolId, perpMarketIndex, constituentIndex) {
2705
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2706
+ const ammConstituentMapping = (0, pda_1.getAmmConstituentMappingPublicKey)(this.program.programId, lpPool);
2707
+ return [
2708
+ this.program.instruction.removeAmmConstituentMappingData(perpMarketIndex, constituentIndex, {
2709
+ accounts: {
2710
+ admin: this.wallet.publicKey,
2711
+ lpPool,
2712
+ ammConstituentMapping,
2713
+ systemProgram: web3_js_1.SystemProgram.programId,
2714
+ state: await this.getStatePublicKey(),
2715
+ },
2716
+ }),
2717
+ ];
2718
+ }
2719
+ async updateConstituentCorrelationData(lpPoolId, index1, index2, correlation) {
2720
+ const ixs = await this.getUpdateConstituentCorrelationDataIx(lpPoolId, index1, index2, correlation);
2721
+ const tx = await this.buildTransaction(ixs);
2722
+ const { txSig } = await this.sendTransaction(tx, []);
2723
+ return txSig;
2724
+ }
2725
+ async getUpdateConstituentCorrelationDataIx(lpPoolId, index1, index2, correlation) {
2726
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2727
+ return [
2728
+ this.program.instruction.updateConstituentCorrelationData(index1, index2, correlation, {
2729
+ accounts: {
2730
+ admin: this.wallet.publicKey,
2731
+ lpPool,
2732
+ constituentCorrelations: (0, pda_1.getConstituentCorrelationsPublicKey)(this.program.programId, lpPool),
2733
+ state: await this.getStatePublicKey(),
2734
+ },
2735
+ }),
2736
+ ];
2737
+ }
2738
+ /**
2739
+ * Get the drift begin_swap and end_swap instructions
2740
+ *
2741
+ * @param outMarketIndex the market index of the token you're buying
2742
+ * @param inMarketIndex the market index of the token you're selling
2743
+ * @param amountIn the amount of the token to sell
2744
+ * @param inTokenAccount the token account to move the tokens being sold (admin signer ata for lp swap)
2745
+ * @param outTokenAccount the token account to receive the tokens being bought (admin signer ata for lp swap)
2746
+ * @param limitPrice the limit price of the swap
2747
+ * @param reduceOnly
2748
+ * @param userAccountPublicKey optional, specify a custom userAccountPublicKey to use instead of getting the current user account; can be helpful if the account is being created within the current tx
2749
+ */
2750
+ async getSwapIx({ lpPoolId, outMarketIndex, inMarketIndex, amountIn, inTokenAccount, outTokenAccount, limitPrice, reduceOnly, userAccountPublicKey, }, lpSwap) {
2751
+ if (!lpSwap) {
2752
+ return super.getSwapIx({
2753
+ outMarketIndex,
2754
+ inMarketIndex,
2755
+ amountIn,
2756
+ inTokenAccount,
2757
+ outTokenAccount,
2758
+ limitPrice,
2759
+ reduceOnly,
2760
+ userAccountPublicKey,
2761
+ });
2762
+ }
2763
+ const outSpotMarket = this.getSpotMarketAccount(outMarketIndex);
2764
+ const inSpotMarket = this.getSpotMarketAccount(inMarketIndex);
2765
+ const outTokenProgram = this.getTokenProgramForSpotMarket(outSpotMarket);
2766
+ const inTokenProgram = this.getTokenProgramForSpotMarket(inSpotMarket);
2767
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2768
+ const outConstituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool, outMarketIndex);
2769
+ const inConstituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool, inMarketIndex);
2770
+ const outConstituentTokenAccount = (0, pda_1.getConstituentVaultPublicKey)(this.program.programId, lpPool, outMarketIndex);
2771
+ const inConstituentTokenAccount = (0, pda_1.getConstituentVaultPublicKey)(this.program.programId, lpPool, inMarketIndex);
2772
+ const beginSwapIx = this.program.instruction.beginLpSwap(inMarketIndex, outMarketIndex, amountIn, {
2773
+ accounts: {
2774
+ state: await this.getStatePublicKey(),
2775
+ admin: this.wallet.publicKey,
2776
+ signerOutTokenAccount: outTokenAccount,
2777
+ signerInTokenAccount: inTokenAccount,
2778
+ constituentOutTokenAccount: outConstituentTokenAccount,
2779
+ constituentInTokenAccount: inConstituentTokenAccount,
2780
+ outConstituent,
2781
+ inConstituent,
2782
+ lpPool,
2783
+ instructions: anchor.web3.SYSVAR_INSTRUCTIONS_PUBKEY,
2784
+ tokenProgram: inTokenProgram,
2785
+ },
2786
+ });
2787
+ const remainingAccounts = [];
2788
+ remainingAccounts.push({
2789
+ pubkey: outTokenProgram,
2790
+ isWritable: false,
2791
+ isSigner: false,
2792
+ });
2793
+ const endSwapIx = this.program.instruction.endLpSwap(inMarketIndex, outMarketIndex, {
2794
+ accounts: {
2795
+ state: await this.getStatePublicKey(),
2796
+ admin: this.wallet.publicKey,
2797
+ signerOutTokenAccount: outTokenAccount,
2798
+ signerInTokenAccount: inTokenAccount,
2799
+ constituentOutTokenAccount: outConstituentTokenAccount,
2800
+ constituentInTokenAccount: inConstituentTokenAccount,
2801
+ outConstituent,
2802
+ inConstituent,
2803
+ lpPool,
2804
+ tokenProgram: inTokenProgram,
2805
+ instructions: anchor.web3.SYSVAR_INSTRUCTIONS_PUBKEY,
2806
+ },
2807
+ remainingAccounts,
2808
+ });
2809
+ return { beginSwapIx, endSwapIx };
2810
+ }
2811
+ async getLpJupiterSwapIxV6({ jupiterClient, outMarketIndex, inMarketIndex, amount, slippageBps, swapMode, onlyDirectRoutes, quote, lpPoolId, }) {
2812
+ const outMarket = this.getSpotMarketAccount(outMarketIndex);
2813
+ const inMarket = this.getSpotMarketAccount(inMarketIndex);
2814
+ if (!quote) {
2815
+ const fetchedQuote = await jupiterClient.getQuote({
2816
+ inputMint: inMarket.mint,
2817
+ outputMint: outMarket.mint,
2818
+ amount,
2819
+ slippageBps,
2820
+ swapMode,
2821
+ onlyDirectRoutes,
2822
+ });
2823
+ quote = fetchedQuote;
2824
+ }
2825
+ if (!quote) {
2826
+ throw new Error("Could not fetch Jupiter's quote. Please try again.");
2827
+ }
2828
+ const isExactOut = swapMode === 'ExactOut' || quote.swapMode === 'ExactOut';
2829
+ const amountIn = new anchor_1.BN(quote.inAmount);
2830
+ const exactOutBufferedAmountIn = amountIn.muln(1001).divn(1000); // Add 10bp buffer
2831
+ const transaction = await jupiterClient.getSwap({
2832
+ quote,
2833
+ userPublicKey: this.provider.wallet.publicKey,
2834
+ slippageBps,
2835
+ });
2836
+ const { transactionMessage, lookupTables } = await jupiterClient.getTransactionMessageAndLookupTables({
2837
+ transaction,
2838
+ });
2839
+ const jupiterInstructions = jupiterClient.getJupiterInstructions({
2840
+ transactionMessage,
2841
+ inputMint: inMarket.mint,
2842
+ outputMint: outMarket.mint,
2843
+ });
2844
+ const preInstructions = [];
2845
+ const tokenProgram = this.getTokenProgramForSpotMarket(outMarket);
2846
+ const outAssociatedTokenAccount = await this.getAssociatedTokenAccount(outMarket.marketIndex, false, tokenProgram);
2847
+ const outAccountInfo = await this.connection.getAccountInfo(outAssociatedTokenAccount);
2848
+ if (!outAccountInfo) {
2849
+ preInstructions.push(this.createAssociatedTokenAccountIdempotentInstruction(outAssociatedTokenAccount, this.provider.wallet.publicKey, this.provider.wallet.publicKey, outMarket.mint, tokenProgram));
2850
+ }
2851
+ const inTokenProgram = this.getTokenProgramForSpotMarket(inMarket);
2852
+ const inAssociatedTokenAccount = await this.getAssociatedTokenAccount(inMarket.marketIndex, false, inTokenProgram);
2853
+ const inAccountInfo = await this.connection.getAccountInfo(inAssociatedTokenAccount);
2854
+ if (!inAccountInfo) {
2855
+ preInstructions.push(this.createAssociatedTokenAccountIdempotentInstruction(inAssociatedTokenAccount, this.provider.wallet.publicKey, this.provider.wallet.publicKey, inMarket.mint, tokenProgram));
2856
+ }
2857
+ const { beginSwapIx, endSwapIx } = await this.getSwapIx({
2858
+ lpPoolId,
2859
+ outMarketIndex,
2860
+ inMarketIndex,
2861
+ amountIn: isExactOut ? exactOutBufferedAmountIn : amountIn,
2862
+ inTokenAccount: inAssociatedTokenAccount,
2863
+ outTokenAccount: outAssociatedTokenAccount,
2864
+ });
2865
+ const ixs = [
2866
+ ...preInstructions,
2867
+ beginSwapIx,
2868
+ ...jupiterInstructions,
2869
+ endSwapIx,
2870
+ ];
2871
+ return { ixs, lookupTables };
2872
+ }
2873
+ async getDevnetLpSwapIxs(amountIn, amountOut, externalUserAuthority, externalUserInTokenAccount, externalUserOutTokenAccount, inSpotMarketIndex, outSpotMarketIndex) {
2874
+ const inSpotMarketAccount = this.getSpotMarketAccount(inSpotMarketIndex);
2875
+ const outSpotMarketAccount = this.getSpotMarketAccount(outSpotMarketIndex);
2876
+ const outTokenAccount = await this.getAssociatedTokenAccount(outSpotMarketAccount.marketIndex, false, (0, pda_1.getTokenProgramForSpotMarket)(outSpotMarketAccount));
2877
+ const inTokenAccount = await this.getAssociatedTokenAccount(inSpotMarketAccount.marketIndex, false, (0, pda_1.getTokenProgramForSpotMarket)(inSpotMarketAccount));
2878
+ const externalCreateInTokenAccountIx = this.createAssociatedTokenAccountIdempotentInstruction(externalUserInTokenAccount, this.wallet.publicKey, externalUserAuthority, this.getSpotMarketAccount(inSpotMarketIndex).mint);
2879
+ const externalCreateOutTokenAccountIx = this.createAssociatedTokenAccountIdempotentInstruction(externalUserOutTokenAccount, this.wallet.publicKey, externalUserAuthority, this.getSpotMarketAccount(outSpotMarketIndex).mint);
2880
+ const outTransferIx = (0, spl_token_1.createTransferCheckedInstruction)(externalUserOutTokenAccount, outSpotMarketAccount.mint, outTokenAccount, externalUserAuthority, amountOut.toNumber(), outSpotMarketAccount.decimals, undefined, (0, pda_1.getTokenProgramForSpotMarket)(outSpotMarketAccount));
2881
+ const inTransferIx = (0, spl_token_1.createTransferCheckedInstruction)(inTokenAccount, inSpotMarketAccount.mint, externalUserInTokenAccount, this.wallet.publicKey, amountIn.toNumber(), inSpotMarketAccount.decimals, undefined, (0, pda_1.getTokenProgramForSpotMarket)(inSpotMarketAccount));
2882
+ const ixs = [
2883
+ externalCreateInTokenAccountIx,
2884
+ externalCreateOutTokenAccountIx,
2885
+ outTransferIx,
2886
+ inTransferIx,
2887
+ ];
2888
+ return ixs;
2889
+ }
2890
+ async getAllDevnetLpSwapIxs(lpPoolId, inMarketIndex, outMarketIndex, inAmount, minOutAmount, externalUserAuthority) {
2891
+ const { beginSwapIx, endSwapIx } = await this.getSwapIx({
2892
+ lpPoolId,
2893
+ inMarketIndex,
2894
+ outMarketIndex,
2895
+ amountIn: inAmount,
2896
+ inTokenAccount: await this.getAssociatedTokenAccount(inMarketIndex, false),
2897
+ outTokenAccount: await this.getAssociatedTokenAccount(outMarketIndex, false),
2898
+ }, true);
2899
+ const devnetLpSwapIxs = await this.getDevnetLpSwapIxs(inAmount, minOutAmount, externalUserAuthority, await this.getAssociatedTokenAccount(inMarketIndex, false, (0, pda_1.getTokenProgramForSpotMarket)(this.getSpotMarketAccount(inMarketIndex)), externalUserAuthority), await this.getAssociatedTokenAccount(outMarketIndex, false, (0, pda_1.getTokenProgramForSpotMarket)(this.getSpotMarketAccount(outMarketIndex)), externalUserAuthority), inMarketIndex, outMarketIndex);
2900
+ return [
2901
+ beginSwapIx,
2902
+ ...devnetLpSwapIxs,
2903
+ endSwapIx,
2904
+ ];
2905
+ }
2906
+ async depositWithdrawToProgramVault(lpPoolId, depositMarketIndex, borrowMarketIndex, amountToDeposit, amountToBorrow) {
2907
+ const { depositIx, withdrawIx } = await this.getDepositWithdrawToProgramVaultIxs(lpPoolId, depositMarketIndex, borrowMarketIndex, amountToDeposit, amountToBorrow);
2908
+ const tx = await this.buildTransaction([depositIx, withdrawIx]);
2909
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2910
+ return txSig;
2911
+ }
2912
+ async getDepositWithdrawToProgramVaultIxs(lpPoolId, depositMarketIndex, borrowMarketIndex, amountToDeposit, amountToBorrow) {
2913
+ const lpPool = (0, pda_1.getLpPoolPublicKey)(this.program.programId, lpPoolId);
2914
+ const depositSpotMarket = this.getSpotMarketAccount(depositMarketIndex);
2915
+ const withdrawSpotMarket = this.getSpotMarketAccount(borrowMarketIndex);
2916
+ const depositTokenProgram = this.getTokenProgramForSpotMarket(depositSpotMarket);
2917
+ const withdrawTokenProgram = this.getTokenProgramForSpotMarket(withdrawSpotMarket);
2918
+ const depositConstituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool, depositMarketIndex);
2919
+ const withdrawConstituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool, borrowMarketIndex);
2920
+ const depositConstituentTokenAccount = (0, pda_1.getConstituentVaultPublicKey)(this.program.programId, lpPool, depositMarketIndex);
2921
+ const withdrawConstituentTokenAccount = (0, pda_1.getConstituentVaultPublicKey)(this.program.programId, lpPool, borrowMarketIndex);
2922
+ const depositIx = this.program.instruction.depositToProgramVault(amountToDeposit, {
2923
+ accounts: {
2924
+ state: await this.getStatePublicKey(),
2925
+ admin: this.wallet.publicKey,
2926
+ constituent: depositConstituent,
2927
+ constituentTokenAccount: depositConstituentTokenAccount,
2928
+ spotMarket: depositSpotMarket.pubkey,
2929
+ spotMarketVault: depositSpotMarket.vault,
2930
+ tokenProgram: depositTokenProgram,
2931
+ mint: depositSpotMarket.mint,
2932
+ oracle: depositSpotMarket.oracle,
2933
+ },
2934
+ });
2935
+ const withdrawIx = this.program.instruction.withdrawFromProgramVault(amountToBorrow, {
2936
+ accounts: {
2937
+ state: await this.getStatePublicKey(),
2938
+ admin: this.wallet.publicKey,
2939
+ constituent: withdrawConstituent,
2940
+ constituentTokenAccount: withdrawConstituentTokenAccount,
2941
+ spotMarket: withdrawSpotMarket.pubkey,
2942
+ spotMarketVault: withdrawSpotMarket.vault,
2943
+ tokenProgram: withdrawTokenProgram,
2944
+ mint: withdrawSpotMarket.mint,
2945
+ driftSigner: (0, pda_1.getDriftSignerPublicKey)(this.program.programId),
2946
+ oracle: withdrawSpotMarket.oracle,
2947
+ },
2948
+ });
2949
+ return { depositIx, withdrawIx };
2950
+ }
2951
+ async depositToProgramVault(lpPoolId, depositMarketIndex, amountToDeposit) {
2952
+ const depositIx = await this.getDepositToProgramVaultIx(lpPoolId, depositMarketIndex, amountToDeposit);
2953
+ const tx = await this.buildTransaction([depositIx]);
2954
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2955
+ return txSig;
2956
+ }
2957
+ async withdrawFromProgramVault(lpPoolId, borrowMarketIndex, amountToWithdraw) {
2958
+ const withdrawIx = await this.getWithdrawFromProgramVaultIx(lpPoolId, borrowMarketIndex, amountToWithdraw);
2959
+ const tx = await this.buildTransaction([withdrawIx]);
2960
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2961
+ return txSig;
2962
+ }
2963
+ async getDepositToProgramVaultIx(lpPoolId, depositMarketIndex, amountToDeposit) {
2964
+ const { depositIx } = await this.getDepositWithdrawToProgramVaultIxs(lpPoolId, depositMarketIndex, depositMarketIndex, amountToDeposit, new anchor_1.BN(0));
2965
+ return depositIx;
2966
+ }
2967
+ async getWithdrawFromProgramVaultIx(lpPoolId, borrowMarketIndex, amountToWithdraw) {
2968
+ const { withdrawIx } = await this.getDepositWithdrawToProgramVaultIxs(lpPoolId, borrowMarketIndex, borrowMarketIndex, new anchor_1.BN(0), amountToWithdraw);
2969
+ return withdrawIx;
2970
+ }
2971
+ async updatePerpMarketLpPoolFeeTransferScalar(marketIndex, lpFeeTransferScalar, lpExchangeFeeExcluscionScalar) {
2972
+ const ix = await this.getUpdatePerpMarketLpPoolFeeTransferScalarIx(marketIndex, lpFeeTransferScalar, lpExchangeFeeExcluscionScalar);
2973
+ const tx = await this.buildTransaction(ix);
2974
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2975
+ return txSig;
2976
+ }
2977
+ async getUpdatePerpMarketLpPoolFeeTransferScalarIx(marketIndex, lpFeeTransferScalar, lpExchangeFeeExcluscionScalar) {
2978
+ return this.program.instruction.updatePerpMarketLpPoolFeeTransferScalar(lpFeeTransferScalar !== null && lpFeeTransferScalar !== void 0 ? lpFeeTransferScalar : null, lpExchangeFeeExcluscionScalar !== null && lpExchangeFeeExcluscionScalar !== void 0 ? lpExchangeFeeExcluscionScalar : null, {
2979
+ accounts: {
2980
+ admin: this.isSubscribed
2981
+ ? this.getStateAccount().admin
2982
+ : this.wallet.publicKey,
2983
+ state: await this.getStatePublicKey(),
2984
+ perpMarket: this.getPerpMarketAccount(marketIndex).pubkey,
2985
+ },
2986
+ });
2987
+ }
2988
+ async updatePerpMarketLpPoolPausedOperations(marketIndex, pausedOperations) {
2989
+ const ix = await this.getUpdatePerpMarketLpPoolPausedOperationsIx(marketIndex, pausedOperations);
2990
+ const tx = await this.buildTransaction(ix);
2991
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
2992
+ return txSig;
2993
+ }
2994
+ async getUpdatePerpMarketLpPoolPausedOperationsIx(marketIndex, pausedOperations) {
2995
+ return this.program.instruction.updatePerpMarketLpPoolPausedOperations(pausedOperations, {
2996
+ accounts: {
2997
+ admin: this.isSubscribed
2998
+ ? this.getStateAccount().admin
2999
+ : this.wallet.publicKey,
3000
+ state: await this.getStatePublicKey(),
3001
+ perpMarket: this.getPerpMarketAccount(marketIndex).pubkey,
3002
+ },
3003
+ });
3004
+ }
3005
+ async mintLpWhitelistToken(lpPool, authority) {
3006
+ const ix = await this.getMintLpWhitelistTokenIx(lpPool, authority);
3007
+ const tx = await this.buildTransaction(ix);
3008
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
3009
+ return txSig;
3010
+ }
3011
+ async getMintLpWhitelistTokenIx(lpPool, authority) {
3012
+ const mintAmount = 1000;
3013
+ const associatedTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(lpPool.whitelistMint, authority, false);
3014
+ const ixs = [];
3015
+ const createInstruction = this.createAssociatedTokenAccountIdempotentInstruction(associatedTokenAccount, this.wallet.publicKey, authority, lpPool.whitelistMint);
3016
+ ixs.push(createInstruction);
3017
+ const mintToInstruction = (0, spl_token_1.createMintToInstruction)(lpPool.whitelistMint, associatedTokenAccount, this.wallet.publicKey, mintAmount, [], spl_token_1.TOKEN_PROGRAM_ID);
3018
+ ixs.push(mintToInstruction);
3019
+ return ixs;
3020
+ }
2285
3021
  }
2286
3022
  exports.AdminClient = AdminClient;