@drift-labs/sdk 2.145.0-beta.2 → 2.145.0-beta.3

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 +4303 -1379
  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 +4303 -1379
  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 +4303 -1379
  71. package/src/index.ts +1 -4
  72. package/src/memcmp.ts +23 -1
  73. package/src/types.ts +160 -0
@@ -65,6 +65,10 @@ import {
65
65
  SignedMsgOrderParamsDelegateMessage,
66
66
  TokenProgramFlag,
67
67
  PostOnlyParams,
68
+ LPPoolAccount,
69
+ ConstituentAccount,
70
+ ConstituentTargetBaseAccount,
71
+ AmmCache,
68
72
  } from './types';
69
73
  import driftIDL from './idl/drift.json';
70
74
 
@@ -115,6 +119,15 @@ import {
115
119
  getIfRebalanceConfigPublicKey,
116
120
  getRevenueShareAccountPublicKey,
117
121
  getRevenueShareEscrowAccountPublicKey,
122
+ getConstituentTargetBasePublicKey,
123
+ getAmmConstituentMappingPublicKey,
124
+ getLpPoolPublicKey,
125
+ getConstituentPublicKey,
126
+ getAmmCachePublicKey,
127
+ getLpPoolTokenVaultPublicKey,
128
+ getConstituentVaultPublicKey,
129
+ getConstituentCorrelationsPublicKey,
130
+ getLpPoolTokenTokenAccountPublicKey,
118
131
  } from './addresses/pda';
119
132
  import {
120
133
  DataAndSlot,
@@ -140,6 +153,7 @@ import { decodeName, DEFAULT_USER_NAME, encodeName } from './userName';
140
153
  import { MMOraclePriceData, OraclePriceData } from './oracles/types';
141
154
  import { DriftClientConfig } from './driftClientConfig';
142
155
  import { PollingDriftClientAccountSubscriber } from './accounts/pollingDriftClientAccountSubscriber';
156
+ import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDriftClientAccountSubscriber';
143
157
  import { RetryTxSender } from './tx/retryTxSender';
144
158
  import { User } from './user';
145
159
  import { UserSubscriptionConfig } from './userConfig';
@@ -198,8 +212,7 @@ import { getOracleId } from './oracles/oracleId';
198
212
  import { SignedMsgOrderParams } from './types';
199
213
  import { sha256 } from '@noble/hashes/sha256';
200
214
  import { getOracleConfidenceFromMMOracleData } from './oracles/utils';
201
- import { Commitment } from 'gill';
202
- import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDriftClientAccountSubscriber';
215
+ import { ConstituentMap } from './constituentMap/constituentMap';
203
216
  import { hasBuilder } from './math/orders';
204
217
  import { RevenueShareEscrowMap } from './userMap/revenueShareEscrowMap';
205
218
  import {
@@ -393,8 +406,6 @@ export class DriftClient {
393
406
  resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
394
407
  logResubMessages: config.accountSubscription?.logResubMessages,
395
408
  commitment: config.accountSubscription?.commitment,
396
- programUserAccountSubscriber:
397
- config.accountSubscription?.programUserAccountSubscriber,
398
409
  };
399
410
  this.userStatsAccountSubscriptionConfig = {
400
411
  type: 'websocket',
@@ -463,10 +474,7 @@ export class DriftClient {
463
474
  }
464
475
  );
465
476
  } else {
466
- const accountSubscriberClass =
467
- config.accountSubscription?.driftClientAccountSubscriber ??
468
- WebSocketDriftClientAccountSubscriber;
469
- this.accountSubscriber = new accountSubscriberClass(
477
+ this.accountSubscriber = new WebSocketDriftClientAccountSubscriber(
470
478
  this.program,
471
479
  config.perpMarketIndexes ?? [],
472
480
  config.spotMarketIndexes ?? [],
@@ -477,7 +485,9 @@ export class DriftClient {
477
485
  resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
478
486
  logResubMessages: config.accountSubscription?.logResubMessages,
479
487
  },
480
- config.accountSubscription?.commitment as Commitment
488
+ config.accountSubscription?.commitment,
489
+ config.accountSubscription?.perpMarketAccountSubscriber,
490
+ config.accountSubscription?.oracleAccountSubscriber
481
491
  );
482
492
  }
483
493
  this.eventEmitter = this.accountSubscriber.eventEmitter;
@@ -638,8 +648,7 @@ export class DriftClient {
638
648
  public getSpotMarketAccount(
639
649
  marketIndex: number
640
650
  ): SpotMarketAccount | undefined {
641
- return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)
642
- ?.data;
651
+ return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
643
652
  }
644
653
 
645
654
  /**
@@ -650,8 +659,7 @@ export class DriftClient {
650
659
  marketIndex: number
651
660
  ): Promise<SpotMarketAccount | undefined> {
652
661
  await this.accountSubscriber.fetch();
653
- return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)
654
- ?.data;
662
+ return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
655
663
  }
656
664
 
657
665
  public getSpotMarketAccounts(): SpotMarketAccount[] {
@@ -2810,17 +2818,19 @@ export class DriftClient {
2810
2818
  public async getAssociatedTokenAccount(
2811
2819
  marketIndex: number,
2812
2820
  useNative = true,
2813
- tokenProgram = TOKEN_PROGRAM_ID
2821
+ tokenProgram = TOKEN_PROGRAM_ID,
2822
+ authority = this.wallet.publicKey,
2823
+ allowOwnerOffCurve = false
2814
2824
  ): Promise<PublicKey> {
2815
2825
  const spotMarket = this.getSpotMarketAccount(marketIndex);
2816
2826
  if (useNative && spotMarket.mint.equals(WRAPPED_SOL_MINT)) {
2817
- return this.wallet.publicKey;
2827
+ return authority;
2818
2828
  }
2819
2829
  const mint = spotMarket.mint;
2820
2830
  return await getAssociatedTokenAddress(
2821
2831
  mint,
2822
- this.wallet.publicKey,
2823
- undefined,
2832
+ authority,
2833
+ allowOwnerOffCurve,
2824
2834
  tokenProgram
2825
2835
  );
2826
2836
  }
@@ -11235,6 +11245,1076 @@ export class DriftClient {
11235
11245
  });
11236
11246
  }
11237
11247
 
11248
+ public async getLpPoolAccount(lpPoolId: number): Promise<LPPoolAccount> {
11249
+ return (await this.program.account.lpPool.fetch(
11250
+ getLpPoolPublicKey(this.program.programId, lpPoolId)
11251
+ )) as LPPoolAccount;
11252
+ }
11253
+
11254
+ public async getConstituentTargetBaseAccount(
11255
+ lpPoolId: number
11256
+ ): Promise<ConstituentTargetBaseAccount> {
11257
+ return (await this.program.account.constituentTargetBase.fetch(
11258
+ getConstituentTargetBasePublicKey(
11259
+ this.program.programId,
11260
+ getLpPoolPublicKey(this.program.programId, lpPoolId)
11261
+ )
11262
+ )) as ConstituentTargetBaseAccount;
11263
+ }
11264
+
11265
+ public async getAmmCache(): Promise<AmmCache> {
11266
+ return (await this.program.account.ammCache.fetch(
11267
+ getAmmCachePublicKey(this.program.programId)
11268
+ )) as AmmCache;
11269
+ }
11270
+
11271
+ public async updateLpConstituentTargetBase(
11272
+ lpPoolId: number,
11273
+ constituents: PublicKey[],
11274
+ txParams?: TxParams
11275
+ ): Promise<TransactionSignature> {
11276
+ const { txSig } = await this.sendTransaction(
11277
+ await this.buildTransaction(
11278
+ await this.getUpdateLpConstituentTargetBaseIx(lpPoolId, constituents),
11279
+ txParams
11280
+ ),
11281
+ [],
11282
+ this.opts
11283
+ );
11284
+ return txSig;
11285
+ }
11286
+
11287
+ public async getUpdateLpConstituentTargetBaseIx(
11288
+ lpPoolId: number,
11289
+ constituents: PublicKey[]
11290
+ ): Promise<TransactionInstruction> {
11291
+ const lpPool = getLpPoolPublicKey(this.program.programId, lpPoolId);
11292
+ const ammConstituentMappingPublicKey = getAmmConstituentMappingPublicKey(
11293
+ this.program.programId,
11294
+ lpPool
11295
+ );
11296
+ const constituentTargetBase = getConstituentTargetBasePublicKey(
11297
+ this.program.programId,
11298
+ lpPool
11299
+ );
11300
+
11301
+ const ammCache = getAmmCachePublicKey(this.program.programId);
11302
+
11303
+ const remainingAccounts = constituents.map((constituent) => {
11304
+ return {
11305
+ isWritable: false,
11306
+ isSigner: false,
11307
+ pubkey: constituent,
11308
+ };
11309
+ });
11310
+
11311
+ return this.program.instruction.updateLpConstituentTargetBase({
11312
+ accounts: {
11313
+ keeper: this.wallet.publicKey,
11314
+ lpPool,
11315
+ ammConstituentMapping: ammConstituentMappingPublicKey,
11316
+ constituentTargetBase,
11317
+ state: await this.getStatePublicKey(),
11318
+ ammCache,
11319
+ },
11320
+ remainingAccounts,
11321
+ });
11322
+ }
11323
+
11324
+ public async updateLpPoolAum(
11325
+ lpPool: LPPoolAccount,
11326
+ spotMarketIndexOfConstituents: number[],
11327
+ txParams?: TxParams
11328
+ ): Promise<TransactionSignature> {
11329
+ const { txSig } = await this.sendTransaction(
11330
+ await this.buildTransaction(
11331
+ await this.getUpdateLpPoolAumIxs(lpPool, spotMarketIndexOfConstituents),
11332
+ txParams
11333
+ ),
11334
+ [],
11335
+ this.opts
11336
+ );
11337
+ return txSig;
11338
+ }
11339
+
11340
+ public async getUpdateLpPoolAumIxs(
11341
+ lpPool: LPPoolAccount,
11342
+ spotMarketIndexOfConstituents: number[]
11343
+ ): Promise<TransactionInstruction> {
11344
+ const remainingAccounts = this.getRemainingAccounts({
11345
+ userAccounts: [],
11346
+ readableSpotMarketIndexes: spotMarketIndexOfConstituents,
11347
+ });
11348
+ remainingAccounts.push(
11349
+ ...spotMarketIndexOfConstituents.map((index) => {
11350
+ return {
11351
+ pubkey: getConstituentPublicKey(
11352
+ this.program.programId,
11353
+ lpPool.pubkey,
11354
+ index
11355
+ ),
11356
+ isSigner: false,
11357
+ isWritable: true,
11358
+ };
11359
+ })
11360
+ );
11361
+ return this.program.instruction.updateLpPoolAum({
11362
+ accounts: {
11363
+ keeper: this.wallet.publicKey,
11364
+ lpPool: lpPool.pubkey,
11365
+ state: await this.getStatePublicKey(),
11366
+ constituentTargetBase: getConstituentTargetBasePublicKey(
11367
+ this.program.programId,
11368
+ lpPool.pubkey
11369
+ ),
11370
+ ammCache: getAmmCachePublicKey(this.program.programId),
11371
+ },
11372
+ remainingAccounts,
11373
+ });
11374
+ }
11375
+
11376
+ public async updateAmmCache(
11377
+ perpMarketIndexes: number[],
11378
+ txParams?: TxParams
11379
+ ): Promise<TransactionSignature> {
11380
+ const { txSig } = await this.sendTransaction(
11381
+ await this.buildTransaction(
11382
+ await this.getUpdateAmmCacheIx(perpMarketIndexes),
11383
+ txParams
11384
+ ),
11385
+ [],
11386
+ this.opts
11387
+ );
11388
+ return txSig;
11389
+ }
11390
+
11391
+ public async getUpdateAmmCacheIx(
11392
+ perpMarketIndexes: number[]
11393
+ ): Promise<TransactionInstruction> {
11394
+ if (perpMarketIndexes.length > 50) {
11395
+ throw new Error('Cant update more than 50 markets at once');
11396
+ }
11397
+
11398
+ const remainingAccounts = this.getRemainingAccounts({
11399
+ userAccounts: [],
11400
+ readablePerpMarketIndex: perpMarketIndexes,
11401
+ });
11402
+
11403
+ return this.program.instruction.updateAmmCache({
11404
+ accounts: {
11405
+ state: await this.getStatePublicKey(),
11406
+ keeper: this.wallet.publicKey,
11407
+ ammCache: getAmmCachePublicKey(this.program.programId),
11408
+ quoteMarket: this.getSpotMarketAccount(0).pubkey,
11409
+ },
11410
+ remainingAccounts,
11411
+ });
11412
+ }
11413
+
11414
+ public async updateConstituentOracleInfo(
11415
+ constituent: ConstituentAccount
11416
+ ): Promise<TransactionSignature> {
11417
+ const { txSig } = await this.sendTransaction(
11418
+ await this.buildTransaction(
11419
+ await this.getUpdateConstituentOracleInfoIx(constituent),
11420
+ undefined
11421
+ ),
11422
+ [],
11423
+ this.opts
11424
+ );
11425
+ return txSig;
11426
+ }
11427
+
11428
+ public async getUpdateConstituentOracleInfoIx(
11429
+ constituent: ConstituentAccount
11430
+ ): Promise<TransactionInstruction> {
11431
+ const spotMarket = this.getSpotMarketAccount(constituent.spotMarketIndex);
11432
+ return this.program.instruction.updateConstituentOracleInfo({
11433
+ accounts: {
11434
+ keeper: this.wallet.publicKey,
11435
+ constituent: constituent.pubkey,
11436
+ state: await this.getStatePublicKey(),
11437
+ oracle: spotMarket.oracle,
11438
+ spotMarket: spotMarket.pubkey,
11439
+ },
11440
+ });
11441
+ }
11442
+
11443
+ public async lpPoolSwap(
11444
+ inMarketIndex: number,
11445
+ outMarketIndex: number,
11446
+ inAmount: BN,
11447
+ minOutAmount: BN,
11448
+ lpPool: PublicKey,
11449
+ userAuthority: PublicKey,
11450
+ txParams?: TxParams
11451
+ ): Promise<TransactionSignature> {
11452
+ const { txSig } = await this.sendTransaction(
11453
+ await this.buildTransaction(
11454
+ await this.getLpPoolSwapIx(
11455
+ inMarketIndex,
11456
+ outMarketIndex,
11457
+ inAmount,
11458
+ minOutAmount,
11459
+ lpPool,
11460
+ userAuthority
11461
+ ),
11462
+ txParams
11463
+ ),
11464
+ [],
11465
+ this.opts
11466
+ );
11467
+ return txSig;
11468
+ }
11469
+
11470
+ public async getLpPoolSwapIx(
11471
+ inMarketIndex: number,
11472
+ outMarketIndex: number,
11473
+ inAmount: BN,
11474
+ minOutAmount: BN,
11475
+ lpPool: PublicKey,
11476
+ userAuthority: PublicKey
11477
+ ): Promise<TransactionInstruction> {
11478
+ const remainingAccounts = this.getRemainingAccounts({
11479
+ userAccounts: [],
11480
+ readableSpotMarketIndexes: [inMarketIndex, outMarketIndex],
11481
+ });
11482
+
11483
+ const constituentInTokenAccount = getConstituentVaultPublicKey(
11484
+ this.program.programId,
11485
+ lpPool,
11486
+ inMarketIndex
11487
+ );
11488
+ const constituentOutTokenAccount = getConstituentVaultPublicKey(
11489
+ this.program.programId,
11490
+ lpPool,
11491
+ outMarketIndex
11492
+ );
11493
+ const userInTokenAccount = await getAssociatedTokenAddress(
11494
+ this.getSpotMarketAccount(inMarketIndex).mint,
11495
+ userAuthority
11496
+ );
11497
+ const userOutTokenAccount = await getAssociatedTokenAddress(
11498
+ this.getSpotMarketAccount(outMarketIndex).mint,
11499
+ userAuthority
11500
+ );
11501
+ const inConstituent = getConstituentPublicKey(
11502
+ this.program.programId,
11503
+ lpPool,
11504
+ inMarketIndex
11505
+ );
11506
+ const outConstituent = getConstituentPublicKey(
11507
+ this.program.programId,
11508
+ lpPool,
11509
+ outMarketIndex
11510
+ );
11511
+ const inMarketMint = this.getSpotMarketAccount(inMarketIndex).mint;
11512
+ const outMarketMint = this.getSpotMarketAccount(outMarketIndex).mint;
11513
+
11514
+ const constituentTargetBase = getConstituentTargetBasePublicKey(
11515
+ this.program.programId,
11516
+ lpPool
11517
+ );
11518
+
11519
+ return this.program.instruction.lpPoolSwap(
11520
+ inMarketIndex,
11521
+ outMarketIndex,
11522
+ inAmount,
11523
+ minOutAmount,
11524
+ {
11525
+ remainingAccounts,
11526
+ accounts: {
11527
+ state: await this.getStatePublicKey(),
11528
+ lpPool,
11529
+ constituentTargetBase,
11530
+ constituentInTokenAccount,
11531
+ constituentOutTokenAccount,
11532
+ constituentCorrelations: getConstituentCorrelationsPublicKey(
11533
+ this.program.programId,
11534
+ lpPool
11535
+ ),
11536
+ userInTokenAccount,
11537
+ userOutTokenAccount,
11538
+ inConstituent,
11539
+ outConstituent,
11540
+ inMarketMint,
11541
+ outMarketMint,
11542
+ authority: this.wallet.publicKey,
11543
+ tokenProgram: TOKEN_PROGRAM_ID,
11544
+ },
11545
+ }
11546
+ );
11547
+ }
11548
+
11549
+ public async viewLpPoolSwapFees(
11550
+ inMarketIndex: number,
11551
+ outMarketIndex: number,
11552
+ inAmount: BN,
11553
+ inTargetWeight: BN,
11554
+ outTargetWeight: BN,
11555
+ lpPool: PublicKey,
11556
+ constituentTargetBase: PublicKey,
11557
+ constituentInTokenAccount: PublicKey,
11558
+ constituentOutTokenAccount: PublicKey,
11559
+ inConstituent: PublicKey,
11560
+ outConstituent: PublicKey,
11561
+ txParams?: TxParams
11562
+ ): Promise<TransactionSignature> {
11563
+ const { txSig } = await this.sendTransaction(
11564
+ await this.buildTransaction(
11565
+ await this.getViewLpPoolSwapFeesIx(
11566
+ inMarketIndex,
11567
+ outMarketIndex,
11568
+ inAmount,
11569
+ inTargetWeight,
11570
+ outTargetWeight,
11571
+ lpPool,
11572
+ constituentTargetBase,
11573
+ constituentInTokenAccount,
11574
+ constituentOutTokenAccount,
11575
+ inConstituent,
11576
+ outConstituent
11577
+ ),
11578
+ txParams
11579
+ ),
11580
+ [],
11581
+ this.opts
11582
+ );
11583
+ return txSig;
11584
+ }
11585
+
11586
+ public async getViewLpPoolSwapFeesIx(
11587
+ inMarketIndex: number,
11588
+ outMarketIndex: number,
11589
+ inAmount: BN,
11590
+ inTargetWeight: BN,
11591
+ outTargetWeight: BN,
11592
+ lpPool: PublicKey,
11593
+ constituentTargetBase: PublicKey,
11594
+ constituentInTokenAccount: PublicKey,
11595
+ constituentOutTokenAccount: PublicKey,
11596
+ inConstituent: PublicKey,
11597
+ outConstituent: PublicKey
11598
+ ): Promise<TransactionInstruction> {
11599
+ const remainingAccounts = this.getRemainingAccounts({
11600
+ userAccounts: [],
11601
+ readableSpotMarketIndexes: [inMarketIndex, outMarketIndex],
11602
+ });
11603
+
11604
+ return this.program.instruction.viewLpPoolSwapFees(
11605
+ inMarketIndex,
11606
+ outMarketIndex,
11607
+ inAmount,
11608
+ inTargetWeight,
11609
+ outTargetWeight,
11610
+ {
11611
+ remainingAccounts,
11612
+ accounts: {
11613
+ driftSigner: this.getSignerPublicKey(),
11614
+ state: await this.getStatePublicKey(),
11615
+ lpPool,
11616
+ constituentTargetBase,
11617
+ constituentInTokenAccount,
11618
+ constituentOutTokenAccount,
11619
+ constituentCorrelations: getConstituentCorrelationsPublicKey(
11620
+ this.program.programId,
11621
+ lpPool
11622
+ ),
11623
+ inConstituent,
11624
+ outConstituent,
11625
+ authority: this.wallet.publicKey,
11626
+ tokenProgram: TOKEN_PROGRAM_ID,
11627
+ },
11628
+ }
11629
+ );
11630
+ }
11631
+
11632
+ public async getCreateLpPoolTokenAccountIx(
11633
+ lpPool: LPPoolAccount
11634
+ ): Promise<TransactionInstruction> {
11635
+ const lpMint = lpPool.mint;
11636
+ const userLpTokenAccount = await getLpPoolTokenTokenAccountPublicKey(
11637
+ lpMint,
11638
+ this.wallet.publicKey
11639
+ );
11640
+
11641
+ return this.createAssociatedTokenAccountIdempotentInstruction(
11642
+ userLpTokenAccount,
11643
+ this.wallet.publicKey,
11644
+ this.wallet.publicKey,
11645
+ lpMint
11646
+ );
11647
+ }
11648
+
11649
+ public async createLpPoolTokenAccount(
11650
+ lpPool: LPPoolAccount,
11651
+ txParams?: TxParams
11652
+ ): Promise<TransactionSignature> {
11653
+ const { txSig } = await this.sendTransaction(
11654
+ await this.buildTransaction(
11655
+ await this.getCreateLpPoolTokenAccountIx(lpPool),
11656
+ txParams
11657
+ ),
11658
+ [],
11659
+ this.opts
11660
+ );
11661
+ return txSig;
11662
+ }
11663
+
11664
+ public async lpPoolAddLiquidity({
11665
+ inMarketIndex,
11666
+ inAmount,
11667
+ minMintAmount,
11668
+ lpPool,
11669
+ txParams,
11670
+ }: {
11671
+ inMarketIndex: number;
11672
+ inAmount: BN;
11673
+ minMintAmount: BN;
11674
+ lpPool: LPPoolAccount;
11675
+ txParams?: TxParams;
11676
+ }): Promise<TransactionSignature> {
11677
+ const { txSig } = await this.sendTransaction(
11678
+ await this.buildTransaction(
11679
+ await this.getLpPoolAddLiquidityIx({
11680
+ inMarketIndex,
11681
+ inAmount,
11682
+ minMintAmount,
11683
+ lpPool,
11684
+ }),
11685
+ txParams
11686
+ ),
11687
+ [],
11688
+ this.opts
11689
+ );
11690
+ return txSig;
11691
+ }
11692
+
11693
+ public async getLpPoolAddLiquidityIx({
11694
+ inMarketIndex,
11695
+ inAmount,
11696
+ minMintAmount,
11697
+ lpPool,
11698
+ }: {
11699
+ inMarketIndex: number;
11700
+ inAmount: BN;
11701
+ minMintAmount: BN;
11702
+ lpPool: LPPoolAccount;
11703
+ }): Promise<TransactionInstruction[]> {
11704
+ const ixs: TransactionInstruction[] = [];
11705
+ const remainingAccounts = this.getRemainingAccounts({
11706
+ userAccounts: [],
11707
+ writableSpotMarketIndexes: [inMarketIndex],
11708
+ });
11709
+
11710
+ const spotMarket = this.getSpotMarketAccount(inMarketIndex);
11711
+ const inMarketMint = spotMarket.mint;
11712
+ const isSolMarket = inMarketMint.equals(WRAPPED_SOL_MINT);
11713
+
11714
+ let wSolTokenAccount: PublicKey | undefined;
11715
+ if (isSolMarket) {
11716
+ const { ixs: wSolIxs, pubkey } =
11717
+ await this.getWrappedSolAccountCreationIxs(inAmount, true);
11718
+ wSolTokenAccount = pubkey;
11719
+ ixs.push(...wSolIxs);
11720
+ }
11721
+
11722
+ const inConstituent = getConstituentPublicKey(
11723
+ this.program.programId,
11724
+ lpPool.pubkey,
11725
+ inMarketIndex
11726
+ );
11727
+ const userInTokenAccount =
11728
+ wSolTokenAccount ??
11729
+ (await this.getAssociatedTokenAccount(inMarketIndex, false));
11730
+ const constituentInTokenAccount = getConstituentVaultPublicKey(
11731
+ this.program.programId,
11732
+ lpPool.pubkey,
11733
+ inMarketIndex
11734
+ );
11735
+ const lpMint = lpPool.mint;
11736
+ const userLpTokenAccount = await getLpPoolTokenTokenAccountPublicKey(
11737
+ lpMint,
11738
+ this.wallet.publicKey
11739
+ );
11740
+ if (!(await this.checkIfAccountExists(userLpTokenAccount))) {
11741
+ ixs.push(
11742
+ this.createAssociatedTokenAccountIdempotentInstruction(
11743
+ userLpTokenAccount,
11744
+ this.wallet.publicKey,
11745
+ this.wallet.publicKey,
11746
+ lpMint
11747
+ )
11748
+ );
11749
+ }
11750
+
11751
+ const constituentTargetBase = getConstituentTargetBasePublicKey(
11752
+ this.program.programId,
11753
+ lpPool.pubkey
11754
+ );
11755
+
11756
+ if (!lpPool.whitelistMint.equals(PublicKey.default)) {
11757
+ const associatedTokenPublicKey = await getAssociatedTokenAddress(
11758
+ lpPool.whitelistMint,
11759
+ this.wallet.publicKey
11760
+ );
11761
+ remainingAccounts.push({
11762
+ pubkey: associatedTokenPublicKey,
11763
+ isWritable: false,
11764
+ isSigner: false,
11765
+ });
11766
+ }
11767
+
11768
+ const lpPoolAddLiquidityIx = this.program.instruction.lpPoolAddLiquidity(
11769
+ inMarketIndex,
11770
+ inAmount,
11771
+ minMintAmount,
11772
+ {
11773
+ remainingAccounts,
11774
+ accounts: {
11775
+ state: await this.getStatePublicKey(),
11776
+ lpPool: lpPool.pubkey,
11777
+ authority: this.wallet.publicKey,
11778
+ inMarketMint,
11779
+ inConstituent,
11780
+ userInTokenAccount,
11781
+ constituentInTokenAccount,
11782
+ userLpTokenAccount,
11783
+ lpMint,
11784
+ lpPoolTokenVault: getLpPoolTokenVaultPublicKey(
11785
+ this.program.programId,
11786
+ lpPool.pubkey
11787
+ ),
11788
+ constituentTargetBase,
11789
+ tokenProgram: TOKEN_PROGRAM_ID,
11790
+ },
11791
+ }
11792
+ );
11793
+ ixs.push(lpPoolAddLiquidityIx);
11794
+
11795
+ if (isSolMarket && wSolTokenAccount) {
11796
+ ixs.push(
11797
+ createCloseAccountInstruction(
11798
+ wSolTokenAccount,
11799
+ this.wallet.publicKey,
11800
+ this.wallet.publicKey
11801
+ )
11802
+ );
11803
+ }
11804
+ return [...ixs];
11805
+ }
11806
+
11807
+ public async viewLpPoolAddLiquidityFees({
11808
+ inMarketIndex,
11809
+ inAmount,
11810
+ lpPool,
11811
+ txParams,
11812
+ }: {
11813
+ inMarketIndex: number;
11814
+ inAmount: BN;
11815
+ lpPool: LPPoolAccount;
11816
+ txParams?: TxParams;
11817
+ }): Promise<TransactionSignature> {
11818
+ const { txSig } = await this.sendTransaction(
11819
+ await this.buildTransaction(
11820
+ await this.getViewLpPoolAddLiquidityFeesIx({
11821
+ inMarketIndex,
11822
+ inAmount,
11823
+ lpPool,
11824
+ }),
11825
+ txParams
11826
+ ),
11827
+ [],
11828
+ this.opts
11829
+ );
11830
+ return txSig;
11831
+ }
11832
+
11833
+ public async getViewLpPoolAddLiquidityFeesIx({
11834
+ inMarketIndex,
11835
+ inAmount,
11836
+ lpPool,
11837
+ }: {
11838
+ inMarketIndex: number;
11839
+ inAmount: BN;
11840
+ lpPool: LPPoolAccount;
11841
+ }): Promise<TransactionInstruction> {
11842
+ const remainingAccounts = this.getRemainingAccounts({
11843
+ userAccounts: [],
11844
+ readableSpotMarketIndexes: [inMarketIndex],
11845
+ });
11846
+
11847
+ const spotMarket = this.getSpotMarketAccount(inMarketIndex);
11848
+ const inMarketMint = spotMarket.mint;
11849
+ const inConstituent = getConstituentPublicKey(
11850
+ this.program.programId,
11851
+ lpPool.pubkey,
11852
+ inMarketIndex
11853
+ );
11854
+ const lpMint = lpPool.mint;
11855
+
11856
+ const constituentTargetBase = getConstituentTargetBasePublicKey(
11857
+ this.program.programId,
11858
+ lpPool.pubkey
11859
+ );
11860
+
11861
+ return this.program.instruction.viewLpPoolAddLiquidityFees(
11862
+ inMarketIndex,
11863
+ inAmount,
11864
+ {
11865
+ accounts: {
11866
+ state: await this.getStatePublicKey(),
11867
+ lpPool: lpPool.pubkey,
11868
+ authority: this.wallet.publicKey,
11869
+ inMarketMint,
11870
+ inConstituent,
11871
+ lpMint,
11872
+ constituentTargetBase,
11873
+ },
11874
+ remainingAccounts,
11875
+ }
11876
+ );
11877
+ }
11878
+
11879
+ public async lpPoolRemoveLiquidity({
11880
+ outMarketIndex,
11881
+ lpToBurn,
11882
+ minAmountOut,
11883
+ lpPool,
11884
+ txParams,
11885
+ }: {
11886
+ outMarketIndex: number;
11887
+ lpToBurn: BN;
11888
+ minAmountOut: BN;
11889
+ lpPool: LPPoolAccount;
11890
+ txParams?: TxParams;
11891
+ }): Promise<TransactionSignature> {
11892
+ const { txSig } = await this.sendTransaction(
11893
+ await this.buildTransaction(
11894
+ await this.getLpPoolRemoveLiquidityIx({
11895
+ outMarketIndex,
11896
+ lpToBurn,
11897
+ minAmountOut,
11898
+ lpPool,
11899
+ }),
11900
+ txParams
11901
+ ),
11902
+ [],
11903
+ this.opts
11904
+ );
11905
+ return txSig;
11906
+ }
11907
+
11908
+ public async getLpPoolRemoveLiquidityIx({
11909
+ outMarketIndex,
11910
+ lpToBurn,
11911
+ minAmountOut,
11912
+ lpPool,
11913
+ }: {
11914
+ outMarketIndex: number;
11915
+ lpToBurn: BN;
11916
+ minAmountOut: BN;
11917
+ lpPool: LPPoolAccount;
11918
+ }): Promise<TransactionInstruction[]> {
11919
+ const ixs: TransactionInstruction[] = [];
11920
+ const remainingAccounts = this.getRemainingAccounts({
11921
+ userAccounts: [],
11922
+ writableSpotMarketIndexes: [outMarketIndex],
11923
+ });
11924
+
11925
+ const spotMarket = this.getSpotMarketAccount(outMarketIndex);
11926
+ const outMarketMint = spotMarket.mint;
11927
+ const outConstituent = getConstituentPublicKey(
11928
+ this.program.programId,
11929
+ lpPool.pubkey,
11930
+ outMarketIndex
11931
+ );
11932
+ if (outMarketMint.equals(WRAPPED_SOL_MINT)) {
11933
+ ixs.push(
11934
+ createAssociatedTokenAccountIdempotentInstruction(
11935
+ this.wallet.publicKey,
11936
+ await this.getAssociatedTokenAccount(outMarketIndex, false),
11937
+ this.wallet.publicKey,
11938
+ WRAPPED_SOL_MINT
11939
+ )
11940
+ );
11941
+ }
11942
+ const userOutTokenAccount = await this.getAssociatedTokenAccount(
11943
+ outMarketIndex,
11944
+ false
11945
+ );
11946
+ const constituentOutTokenAccount = getConstituentVaultPublicKey(
11947
+ this.program.programId,
11948
+ lpPool.pubkey,
11949
+ outMarketIndex
11950
+ );
11951
+ const lpMint = lpPool.mint;
11952
+ const userLpTokenAccount = await getAssociatedTokenAddress(
11953
+ lpMint,
11954
+ this.wallet.publicKey,
11955
+ true
11956
+ );
11957
+
11958
+ const constituentTargetBase = getConstituentTargetBasePublicKey(
11959
+ this.program.programId,
11960
+ lpPool.pubkey
11961
+ );
11962
+
11963
+ ixs.push(
11964
+ this.program.instruction.lpPoolRemoveLiquidity(
11965
+ outMarketIndex,
11966
+ lpToBurn,
11967
+ minAmountOut,
11968
+ {
11969
+ remainingAccounts,
11970
+ accounts: {
11971
+ driftSigner: this.getSignerPublicKey(),
11972
+ state: await this.getStatePublicKey(),
11973
+ lpPool: lpPool.pubkey,
11974
+ authority: this.wallet.publicKey,
11975
+ outMarketMint,
11976
+ outConstituent,
11977
+ userOutTokenAccount,
11978
+ constituentOutTokenAccount,
11979
+ userLpTokenAccount,
11980
+ spotMarketTokenAccount: spotMarket.vault,
11981
+ lpMint,
11982
+ lpPoolTokenVault: getLpPoolTokenVaultPublicKey(
11983
+ this.program.programId,
11984
+ lpPool.pubkey
11985
+ ),
11986
+ constituentTargetBase,
11987
+ tokenProgram: TOKEN_PROGRAM_ID,
11988
+ ammCache: getAmmCachePublicKey(this.program.programId),
11989
+ },
11990
+ }
11991
+ )
11992
+ );
11993
+ return ixs;
11994
+ }
11995
+
11996
+ public async viewLpPoolRemoveLiquidityFees({
11997
+ outMarketIndex,
11998
+ lpToBurn,
11999
+ lpPool,
12000
+ txParams,
12001
+ }: {
12002
+ outMarketIndex: number;
12003
+ lpToBurn: BN;
12004
+ lpPool: LPPoolAccount;
12005
+ txParams?: TxParams;
12006
+ }): Promise<TransactionSignature> {
12007
+ const { txSig } = await this.sendTransaction(
12008
+ await this.buildTransaction(
12009
+ await this.getViewLpPoolRemoveLiquidityFeesIx({
12010
+ outMarketIndex,
12011
+ lpToBurn,
12012
+ lpPool,
12013
+ }),
12014
+ txParams
12015
+ ),
12016
+ [],
12017
+ this.opts
12018
+ );
12019
+ return txSig;
12020
+ }
12021
+
12022
+ public async getViewLpPoolRemoveLiquidityFeesIx({
12023
+ outMarketIndex,
12024
+ lpToBurn,
12025
+ lpPool,
12026
+ }: {
12027
+ outMarketIndex: number;
12028
+ lpToBurn: BN;
12029
+ lpPool: LPPoolAccount;
12030
+ }): Promise<TransactionInstruction> {
12031
+ const remainingAccounts = this.getRemainingAccounts({
12032
+ userAccounts: [],
12033
+ writableSpotMarketIndexes: [outMarketIndex],
12034
+ });
12035
+
12036
+ const spotMarket = this.getSpotMarketAccount(outMarketIndex);
12037
+ const outMarketMint = spotMarket.mint;
12038
+ const outConstituent = getConstituentPublicKey(
12039
+ this.program.programId,
12040
+ lpPool.pubkey,
12041
+ outMarketIndex
12042
+ );
12043
+ const lpMint = lpPool.mint;
12044
+ const constituentTargetBase = getConstituentTargetBasePublicKey(
12045
+ this.program.programId,
12046
+ lpPool.pubkey
12047
+ );
12048
+
12049
+ return this.program.instruction.viewLpPoolRemoveLiquidityFees(
12050
+ outMarketIndex,
12051
+ lpToBurn,
12052
+ {
12053
+ remainingAccounts,
12054
+ accounts: {
12055
+ state: await this.getStatePublicKey(),
12056
+ lpPool: lpPool.pubkey,
12057
+ authority: this.wallet.publicKey,
12058
+ outMarketMint,
12059
+ outConstituent,
12060
+ lpMint,
12061
+ constituentTargetBase,
12062
+ },
12063
+ }
12064
+ );
12065
+ }
12066
+
12067
+ public async getAllLpPoolAddLiquidityIxs(
12068
+ {
12069
+ inMarketIndex,
12070
+ inAmount,
12071
+ minMintAmount,
12072
+ lpPool,
12073
+ }: {
12074
+ inMarketIndex: number;
12075
+ inAmount: BN;
12076
+ minMintAmount: BN;
12077
+ lpPool: LPPoolAccount;
12078
+ },
12079
+ constituentMap: ConstituentMap,
12080
+ includeUpdateConstituentOracleInfo = true,
12081
+ view = false
12082
+ ): Promise<TransactionInstruction[]> {
12083
+ const ixs: TransactionInstruction[] = [];
12084
+
12085
+ ixs.push(
12086
+ ...(await this.getAllUpdateLpPoolAumIxs(
12087
+ lpPool,
12088
+ constituentMap,
12089
+ includeUpdateConstituentOracleInfo
12090
+ ))
12091
+ );
12092
+
12093
+ if (view) {
12094
+ ixs.push(
12095
+ await this.getViewLpPoolAddLiquidityFeesIx({
12096
+ inMarketIndex,
12097
+ inAmount,
12098
+ lpPool,
12099
+ })
12100
+ );
12101
+ } else {
12102
+ ixs.push(
12103
+ ...(await this.getLpPoolAddLiquidityIx({
12104
+ inMarketIndex,
12105
+ inAmount,
12106
+ minMintAmount,
12107
+ lpPool,
12108
+ }))
12109
+ );
12110
+ }
12111
+
12112
+ return ixs;
12113
+ }
12114
+
12115
+ public async getAllLpPoolRemoveLiquidityIxs(
12116
+ {
12117
+ outMarketIndex,
12118
+ lpToBurn,
12119
+ minAmountOut,
12120
+ lpPool,
12121
+ }: {
12122
+ outMarketIndex: number;
12123
+ lpToBurn: BN;
12124
+ minAmountOut: BN;
12125
+ lpPool: LPPoolAccount;
12126
+ },
12127
+ constituentMap: ConstituentMap,
12128
+ includeUpdateConstituentOracleInfo = true,
12129
+ view = false
12130
+ ): Promise<TransactionInstruction[]> {
12131
+ const ixs: TransactionInstruction[] = [];
12132
+ ixs.push(
12133
+ ...(await this.getAllSettlePerpToLpPoolIxs(
12134
+ lpPool.lpPoolId,
12135
+ this.getPerpMarketAccounts()
12136
+ .filter((marketAccount) => marketAccount.lpStatus > 0)
12137
+ .map((marketAccount) => marketAccount.marketIndex)
12138
+ ))
12139
+ );
12140
+ ixs.push(
12141
+ ...(await this.getAllUpdateLpPoolAumIxs(
12142
+ lpPool,
12143
+ constituentMap,
12144
+ includeUpdateConstituentOracleInfo
12145
+ ))
12146
+ );
12147
+ if (view) {
12148
+ ixs.push(
12149
+ await this.getViewLpPoolRemoveLiquidityFeesIx({
12150
+ outMarketIndex,
12151
+ lpToBurn,
12152
+ lpPool,
12153
+ })
12154
+ );
12155
+ } else {
12156
+ ixs.push(
12157
+ ...(await this.getLpPoolRemoveLiquidityIx({
12158
+ outMarketIndex,
12159
+ lpToBurn,
12160
+ minAmountOut,
12161
+ lpPool,
12162
+ }))
12163
+ );
12164
+ }
12165
+
12166
+ return ixs;
12167
+ }
12168
+
12169
+ public async getAllUpdateLpPoolAumIxs(
12170
+ lpPool: LPPoolAccount,
12171
+ constituentMap: ConstituentMap,
12172
+ includeUpdateConstituentOracleInfo = true
12173
+ ): Promise<TransactionInstruction[]> {
12174
+ const ixs: TransactionInstruction[] = [];
12175
+ const constituents: ConstituentAccount[] = Array.from(
12176
+ constituentMap.values()
12177
+ );
12178
+
12179
+ if (includeUpdateConstituentOracleInfo) {
12180
+ for (const constituent of constituents) {
12181
+ ixs.push(await this.getUpdateConstituentOracleInfoIx(constituent));
12182
+ }
12183
+ }
12184
+
12185
+ const spotMarketIndexes = constituents.map(
12186
+ (constituent) => constituent.spotMarketIndex
12187
+ );
12188
+ ixs.push(await this.getUpdateLpPoolAumIxs(lpPool, spotMarketIndexes));
12189
+ return ixs;
12190
+ }
12191
+
12192
+ public async getAllUpdateConstituentTargetBaseIxs(
12193
+ perpMarketIndexes: number[],
12194
+ lpPool: LPPoolAccount,
12195
+ constituentMap: ConstituentMap,
12196
+ includeUpdateConstituentOracleInfo = true
12197
+ ): Promise<TransactionInstruction[]> {
12198
+ const ixs: TransactionInstruction[] = [];
12199
+
12200
+ ixs.push(await this.getUpdateAmmCacheIx(perpMarketIndexes));
12201
+
12202
+ const constituents: ConstituentAccount[] = Array.from(
12203
+ constituentMap.values()
12204
+ );
12205
+
12206
+ if (includeUpdateConstituentOracleInfo) {
12207
+ for (const constituent of constituents) {
12208
+ ixs.push(await this.getUpdateConstituentOracleInfoIx(constituent));
12209
+ }
12210
+ }
12211
+
12212
+ ixs.push(
12213
+ await this.getUpdateLpConstituentTargetBaseIx(
12214
+ lpPool.lpPoolId,
12215
+ Array.from(constituentMap.values()).map(
12216
+ (constituent) => constituent.pubkey
12217
+ )
12218
+ )
12219
+ );
12220
+
12221
+ ixs.push(
12222
+ ...(await this.getAllUpdateLpPoolAumIxs(lpPool, constituentMap, false))
12223
+ );
12224
+
12225
+ return ixs;
12226
+ }
12227
+
12228
+ async getAllLpPoolSwapIxs(
12229
+ lpPool: LPPoolAccount,
12230
+ constituentMap: ConstituentMap,
12231
+ inMarketIndex: number,
12232
+ outMarketIndex: number,
12233
+ inAmount: BN,
12234
+ minOutAmount: BN,
12235
+ userAuthority: PublicKey
12236
+ ): Promise<TransactionInstruction[]> {
12237
+ const ixs: TransactionInstruction[] = [];
12238
+ ixs.push(...(await this.getAllUpdateLpPoolAumIxs(lpPool, constituentMap)));
12239
+ ixs.push(
12240
+ await this.getLpPoolSwapIx(
12241
+ inMarketIndex,
12242
+ outMarketIndex,
12243
+ inAmount,
12244
+ minOutAmount,
12245
+ lpPool.pubkey,
12246
+ userAuthority
12247
+ )
12248
+ );
12249
+ return ixs;
12250
+ }
12251
+
12252
+ async settlePerpToLpPool(
12253
+ lpPoolId: number,
12254
+ perpMarketIndexes: number[]
12255
+ ): Promise<TransactionSignature> {
12256
+ const { txSig } = await this.sendTransaction(
12257
+ await this.buildTransaction(
12258
+ await this.getSettlePerpToLpPoolIx(lpPoolId, perpMarketIndexes),
12259
+ undefined
12260
+ ),
12261
+ [],
12262
+ this.opts
12263
+ );
12264
+ return txSig;
12265
+ }
12266
+
12267
+ public async getSettlePerpToLpPoolIx(
12268
+ lpPoolId: number,
12269
+ perpMarketIndexes: number[]
12270
+ ): Promise<TransactionInstruction> {
12271
+ const remainingAccounts = [];
12272
+ remainingAccounts.push(
12273
+ ...perpMarketIndexes.map((index) => {
12274
+ return {
12275
+ pubkey: this.getPerpMarketAccount(index).pubkey,
12276
+ isSigner: false,
12277
+ isWritable: true,
12278
+ };
12279
+ })
12280
+ );
12281
+ const quoteSpotMarketAccount = this.getQuoteSpotMarketAccount();
12282
+ const lpPool = getLpPoolPublicKey(this.program.programId, lpPoolId);
12283
+ return this.program.instruction.settlePerpToLpPool({
12284
+ accounts: {
12285
+ driftSigner: this.getSignerPublicKey(),
12286
+ state: await this.getStatePublicKey(),
12287
+ keeper: this.wallet.publicKey,
12288
+ ammCache: getAmmCachePublicKey(this.program.programId),
12289
+ quoteMarket: quoteSpotMarketAccount.pubkey,
12290
+ constituent: getConstituentPublicKey(this.program.programId, lpPool, 0),
12291
+ constituentQuoteTokenAccount: getConstituentVaultPublicKey(
12292
+ this.program.programId,
12293
+ lpPool,
12294
+ 0
12295
+ ),
12296
+ lpPool,
12297
+ quoteTokenVault: quoteSpotMarketAccount.vault,
12298
+ tokenProgram: this.getTokenProgramForSpotMarket(quoteSpotMarketAccount),
12299
+ },
12300
+ remainingAccounts,
12301
+ });
12302
+ }
12303
+
12304
+ public async getAllSettlePerpToLpPoolIxs(
12305
+ lpPoolId: number,
12306
+ marketIndexes: number[]
12307
+ ): Promise<TransactionInstruction[]> {
12308
+ const ixs: TransactionInstruction[] = [];
12309
+ ixs.push(await this.getUpdateAmmCacheIx(marketIndexes));
12310
+ ixs.push(await this.getSettlePerpToLpPoolIx(lpPoolId, marketIndexes));
12311
+ return ixs;
12312
+ }
12313
+
12314
+ /**
12315
+ * Below here are the transaction sending functions
12316
+ */
12317
+
11238
12318
  private handleSignedTransaction(signedTxs: SignedTxData[]) {
11239
12319
  if (this.enableMetricsEvents && this.metricsEventEmitter) {
11240
12320
  this.metricsEventEmitter.emit('txSigned', signedTxs);