@kamino-finance/klend-sdk 6.0.5-beta.1 → 6.1.0-beta.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.
@@ -131,32 +131,36 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
131
131
  const obligationType = checkObligationType(obligationTypeTagOverride, collTokenMint, debtTokenMint, kaminoMarket);
132
132
 
133
133
  // Build the repay & withdraw collateral tx to get the number of accounts
134
- const klendIxs: LeverageIxsOutput = await buildDepositWithLeverageIxs(
135
- kaminoMarket,
136
- debtReserve,
137
- collReserve,
138
- owner,
139
- obligation ? obligation : obligationType,
140
- referrer,
141
- currentSlot,
142
- depositTokenIsSol,
143
- scopeRefreshConfig,
144
- calcs,
145
- budgetAndPriorityFeeIxs,
146
- {
147
- preActionIxs: [],
148
- swapIxs: [],
149
- lookupTables: [],
150
- quote: {
151
- priceAInB: new Decimal(0), // not used
152
- quoteResponse: undefined,
153
- },
154
- },
155
- strategy,
156
- collIsKtoken,
157
- useV2Ixs,
158
- elevationGroupOverride
159
- );
134
+ const klendIxs: LeverageIxsOutput = (
135
+ await buildDepositWithLeverageIxs(
136
+ kaminoMarket,
137
+ debtReserve,
138
+ collReserve,
139
+ owner,
140
+ obligation ? obligation : obligationType,
141
+ referrer,
142
+ currentSlot,
143
+ depositTokenIsSol,
144
+ scopeRefreshConfig,
145
+ calcs,
146
+ budgetAndPriorityFeeIxs,
147
+ [
148
+ {
149
+ preActionIxs: [],
150
+ swapIxs: [],
151
+ lookupTables: [],
152
+ quote: {
153
+ priceAInB: new Decimal(0), // not used
154
+ quoteResponse: undefined,
155
+ },
156
+ },
157
+ ],
158
+ strategy,
159
+ collIsKtoken,
160
+ useV2Ixs,
161
+ elevationGroupOverride
162
+ )
163
+ )[0];
160
164
 
161
165
  const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
162
166
 
@@ -378,42 +382,42 @@ export async function getDepositWithLeverageIxs<QuoteResponse>({
378
382
  const solTokenReserve = kaminoMarket.getReserveByMint(NATIVE_MINT);
379
383
  const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve!.getLiquidityMint());
380
384
 
381
- return Promise.all(
382
- swapsArray.map(async (swap) => {
383
- const ixs: LeverageIxsOutput = await buildDepositWithLeverageIxs(
384
- kaminoMarket,
385
- debtReserve!,
386
- collReserve!,
387
- owner,
388
- initialInputs.obligation,
389
- referrer,
390
- currentSlot,
391
- depositTokenIsSol,
392
- scopeRefreshConfig,
393
- initialInputs.calcs,
394
- budgetAndPriorityFeeIxs,
395
- {
396
- preActionIxs: [],
397
- swapIxs: swap.swapIxs,
398
- lookupTables: swap.lookupTables,
399
- quote: swap.quote,
400
- },
401
- initialInputs.strategy,
402
- initialInputs.collIsKtoken,
403
- useV2Ixs,
404
- elevationGroupOverride
405
- );
406
-
385
+ const depositWithLeverageIxs = await buildDepositWithLeverageIxs(
386
+ kaminoMarket,
387
+ debtReserve!,
388
+ collReserve!,
389
+ owner,
390
+ initialInputs.obligation,
391
+ referrer,
392
+ currentSlot,
393
+ depositTokenIsSol,
394
+ scopeRefreshConfig,
395
+ initialInputs.calcs,
396
+ budgetAndPriorityFeeIxs,
397
+ swapsArray.map((swap) => {
407
398
  return {
408
- ixs: ixs.instructions,
409
- flashLoanInfo: ixs.flashLoanInfo,
399
+ preActionIxs: [],
400
+ swapIxs: swap.swapIxs,
410
401
  lookupTables: swap.lookupTables,
411
- swapInputs,
412
- initialInputs,
413
- quote: swap.quote.quoteResponse,
402
+ quote: swap.quote,
414
403
  };
415
- })
404
+ }),
405
+ initialInputs.strategy,
406
+ initialInputs.collIsKtoken,
407
+ useV2Ixs,
408
+ elevationGroupOverride
416
409
  );
410
+
411
+ return depositWithLeverageIxs.map((depositWithLeverageIxs, index) => {
412
+ return {
413
+ ixs: depositWithLeverageIxs.instructions,
414
+ flashLoanInfo: depositWithLeverageIxs.flashLoanInfo,
415
+ lookupTables: swapsArray[index].lookupTables,
416
+ swapInputs,
417
+ initialInputs,
418
+ quote: swapsArray[index].quote.quoteResponse,
419
+ };
420
+ });
417
421
  }
418
422
 
419
423
  async function buildDepositWithLeverageIxs<QuoteResponse>(
@@ -428,12 +432,12 @@ async function buildDepositWithLeverageIxs<QuoteResponse>(
428
432
  scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
429
433
  calcs: DepositLeverageCalcsResult,
430
434
  budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
431
- swapQuoteIxs: SwapIxs<QuoteResponse>,
435
+ swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
432
436
  strategy: StrategyWithAddress | undefined,
433
437
  collIsKtoken: boolean,
434
438
  useV2Ixs: boolean,
435
439
  elevationGroupOverride?: number
436
- ): Promise<LeverageIxsOutput> {
440
+ ): Promise<LeverageIxsOutput[]> {
437
441
  const collTokenMint = collReserve.getLiquidityMint();
438
442
  const debtTokenMint = debtReserve.getLiquidityMint();
439
443
  const collTokenAta = getAssociatedTokenAddressSync(
@@ -519,29 +523,31 @@ async function buildDepositWithLeverageIxs<QuoteResponse>(
519
523
  currentSlot
520
524
  );
521
525
 
522
- // 4. Swap
523
- const { swapIxs } = swapQuoteIxs;
524
- const swapInstructions = removeBudgetIxs(swapIxs);
525
- const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
526
- const flashLoanInfo = {
527
- flashBorrowReserve: flashBorrowReserve.address,
528
- flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
529
- };
526
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
527
+ // 4. Swap
528
+ const { swapIxs } = swapQuoteIxs;
529
+ const swapInstructions = removeBudgetIxs(swapIxs);
530
+ const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
531
+ const flashLoanInfo = {
532
+ flashBorrowReserve: flashBorrowReserve.address,
533
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
534
+ };
530
535
 
531
- return {
532
- flashLoanInfo,
533
- instructions: [
534
- ...scopeRefreshIx,
535
- ...budgetIxs,
536
- ...createAtasIxs,
537
- ...fillWsolAtaIxs,
538
- ...[flashBorrowIx],
539
- ...(collIsKtoken ? swapInstructions : []),
540
- ...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
541
- ...(collIsKtoken ? [] : swapInstructions),
542
- ...[flashRepayIx],
543
- ],
544
- };
536
+ return {
537
+ flashLoanInfo,
538
+ instructions: [
539
+ ...scopeRefreshIx,
540
+ ...budgetIxs,
541
+ ...createAtasIxs,
542
+ ...fillWsolAtaIxs,
543
+ ...[flashBorrowIx],
544
+ ...(collIsKtoken ? swapInstructions : []),
545
+ ...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
546
+ ...(collIsKtoken ? [] : swapInstructions),
547
+ ...[flashRepayIx],
548
+ ],
549
+ };
550
+ });
545
551
  }
546
552
 
547
553
  export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
@@ -597,32 +603,36 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
597
603
  slippagePct
598
604
  );
599
605
 
600
- const klendIxs = await buildWithdrawWithLeverageIxs(
601
- kaminoMarket,
602
- debtReserve!,
603
- collReserve!,
604
- owner,
605
- obligation,
606
- referrer,
607
- currentSlot,
608
- isClosingPosition,
609
- inputTokenIsSol,
610
- scopeRefreshConfig,
611
- calcs,
612
- budgetAndPriorityFeeIxs,
613
- {
614
- preActionIxs: [],
615
- swapIxs: [],
616
- lookupTables: [],
617
- quote: {
618
- priceAInB: new Decimal(0), // not used
619
- quoteResponse: undefined,
620
- },
621
- },
622
- strategy,
623
- collIsKtoken,
624
- useV2Ixs
625
- );
606
+ const klendIxs = (
607
+ await buildWithdrawWithLeverageIxs(
608
+ kaminoMarket,
609
+ debtReserve!,
610
+ collReserve!,
611
+ owner,
612
+ obligation,
613
+ referrer,
614
+ currentSlot,
615
+ isClosingPosition,
616
+ inputTokenIsSol,
617
+ scopeRefreshConfig,
618
+ calcs,
619
+ budgetAndPriorityFeeIxs,
620
+ [
621
+ {
622
+ preActionIxs: [],
623
+ swapIxs: [],
624
+ lookupTables: [],
625
+ quote: {
626
+ priceAInB: new Decimal(0), // not used
627
+ quoteResponse: undefined,
628
+ },
629
+ },
630
+ ],
631
+ strategy,
632
+ collIsKtoken,
633
+ useV2Ixs
634
+ )
635
+ )[0];
626
636
 
627
637
  const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
628
638
 
@@ -756,43 +766,43 @@ export async function getWithdrawWithLeverageIxs<QuoteResponse>({
756
766
  }
757
767
  }
758
768
 
759
- return Promise.all(
760
- swapsArray.map(async (swap) => {
761
- const ixs: LeverageIxsOutput = await buildWithdrawWithLeverageIxs<QuoteResponse>(
762
- kaminoMarket,
763
- debtReserve!,
764
- collReserve!,
765
- owner,
766
- obligation,
767
- referrer,
768
- currentSlot,
769
- isClosingPosition,
770
- inputTokenIsSol,
771
- scopeRefreshConfig,
772
- initialInputs.calcs,
773
- budgetAndPriorityFeeIxs,
774
- {
775
- preActionIxs: [],
776
- swapIxs: swap.swapIxs,
777
- lookupTables: swap.lookupTables,
778
- quote: swap.quote,
779
- },
780
- initialInputs.strategy,
781
- initialInputs.collIsKtoken,
782
- useV2Ixs
783
- );
784
-
785
- // Send ixs and lookup tables
769
+ const withdrawWithLeverageIxs = await buildWithdrawWithLeverageIxs<QuoteResponse>(
770
+ kaminoMarket,
771
+ debtReserve!,
772
+ collReserve!,
773
+ owner,
774
+ obligation,
775
+ referrer,
776
+ currentSlot,
777
+ isClosingPosition,
778
+ inputTokenIsSol,
779
+ scopeRefreshConfig,
780
+ initialInputs.calcs,
781
+ budgetAndPriorityFeeIxs,
782
+ swapsArray.map((swap) => {
786
783
  return {
787
- ixs: ixs.instructions,
788
- flashLoanInfo: ixs.flashLoanInfo,
784
+ preActionIxs: [],
785
+ swapIxs: swap.swapIxs,
789
786
  lookupTables: swap.lookupTables,
790
- swapInputs,
791
- initialInputs: initialInputs,
792
- quote: swap.quote.quoteResponse,
787
+ quote: swap.quote,
793
788
  };
794
- })
789
+ }),
790
+ initialInputs.strategy,
791
+ initialInputs.collIsKtoken,
792
+ useV2Ixs
795
793
  );
794
+
795
+ // Send ixs and lookup tables
796
+ return withdrawWithLeverageIxs.map((ixs, index) => {
797
+ return {
798
+ ixs: ixs.instructions,
799
+ flashLoanInfo: ixs.flashLoanInfo,
800
+ lookupTables: swapsArray[index].lookupTables,
801
+ swapInputs,
802
+ initialInputs: initialInputs,
803
+ quote: swapsArray[index].quote.quoteResponse,
804
+ };
805
+ });
796
806
  }
797
807
 
798
808
  export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
@@ -808,11 +818,11 @@ export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
808
818
  scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
809
819
  calcs: WithdrawLeverageCalcsResult,
810
820
  budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
811
- swapQuoteIxs: SwapIxs<QuoteResponse>,
821
+ swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
812
822
  strategy: StrategyWithAddress | undefined,
813
823
  collIsKtoken: boolean,
814
824
  useV2Ixs: boolean
815
- ): Promise<LeverageIxsOutput> {
825
+ ): Promise<LeverageIxsOutput[]> {
816
826
  const collTokenMint = collReserve.getLiquidityMint();
817
827
  const debtTokenMint = debtReserve.getLiquidityMint();
818
828
  const debtTokenAta = getAssociatedTokenAddressSync(
@@ -897,25 +907,27 @@ export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
897
907
  referrer
898
908
  );
899
909
 
900
- const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
910
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
911
+ const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
901
912
 
902
- return {
903
- flashLoanInfo: {
904
- flashLoanFee: debtReserve.getFlashLoanFee(),
905
- flashBorrowReserve: debtReserve.address,
906
- },
907
- instructions: [
908
- ...scopeRefreshIx,
909
- ...budgetIxs,
910
- ...createAtasIxs,
911
- ...fillWsolAtaIxs,
912
- ...[flashBorrowIx],
913
- ...KaminoAction.actionToIxs(repayAndWithdrawAction),
914
- ...swapInstructions,
915
- ...[flashRepayIx],
916
- ...closeWsolAtaIxs,
917
- ],
918
- };
913
+ return {
914
+ flashLoanInfo: {
915
+ flashLoanFee: debtReserve.getFlashLoanFee(),
916
+ flashBorrowReserve: debtReserve.address,
917
+ },
918
+ instructions: [
919
+ ...scopeRefreshIx,
920
+ ...budgetIxs,
921
+ ...createAtasIxs,
922
+ ...fillWsolAtaIxs,
923
+ ...[flashBorrowIx],
924
+ ...KaminoAction.actionToIxs(repayAndWithdrawAction),
925
+ ...swapInstructions,
926
+ ...[flashRepayIx],
927
+ ...closeWsolAtaIxs,
928
+ ],
929
+ };
930
+ });
919
931
  }
920
932
 
921
933
  export async function getAdjustLeverageSwapInputs<QuoteResponse>({
@@ -988,30 +1000,34 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
988
1000
  );
989
1001
 
990
1002
  // Build the repay & withdraw collateral tx to get the number of accounts
991
- const klendIxs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
992
- owner,
993
- kaminoMarket,
994
- collTokenMint,
995
- debtTokenMint,
996
- obligation,
997
- referrer,
998
- currentSlot,
999
- calcs,
1000
- strategy,
1001
- scopeRefreshConfig,
1002
- collIsKtoken,
1003
- {
1004
- preActionIxs: [],
1005
- swapIxs: [],
1006
- lookupTables: [],
1007
- quote: {
1008
- priceAInB: new Decimal(0), // not used
1009
- quoteResponse: undefined,
1010
- },
1011
- },
1012
- budgetAndPriorityFeeIxs,
1013
- useV2Ixs
1014
- );
1003
+ const klendIxs: LeverageIxsOutput = (
1004
+ await buildIncreaseLeverageIxs(
1005
+ owner,
1006
+ kaminoMarket,
1007
+ collTokenMint,
1008
+ debtTokenMint,
1009
+ obligation,
1010
+ referrer,
1011
+ currentSlot,
1012
+ calcs,
1013
+ strategy,
1014
+ scopeRefreshConfig,
1015
+ collIsKtoken,
1016
+ [
1017
+ {
1018
+ preActionIxs: [],
1019
+ swapIxs: [],
1020
+ lookupTables: [],
1021
+ quote: {
1022
+ priceAInB: new Decimal(0), // not used
1023
+ quoteResponse: undefined,
1024
+ },
1025
+ },
1026
+ ],
1027
+ budgetAndPriorityFeeIxs,
1028
+ useV2Ixs
1029
+ )
1030
+ )[0];
1015
1031
 
1016
1032
  const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
1017
1033
 
@@ -1097,30 +1113,34 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1097
1113
  } else {
1098
1114
  const calcs = adjustWithdrawLeverageCalcs(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
1099
1115
 
1100
- const klendIxs: LeverageIxsOutput = await buildDecreaseLeverageIxs(
1101
- owner,
1102
- kaminoMarket,
1103
- collTokenMint,
1104
- debtTokenMint,
1105
- obligation,
1106
- referrer,
1107
- currentSlot,
1108
- calcs,
1109
- strategy,
1110
- scopeRefreshConfig,
1111
- collIsKtoken,
1112
- {
1113
- preActionIxs: [],
1114
- swapIxs: [],
1115
- lookupTables: [],
1116
- quote: {
1117
- priceAInB: new Decimal(0), // not used
1118
- quoteResponse: undefined,
1119
- },
1120
- },
1121
- budgetAndPriorityFeeIxs,
1122
- useV2Ixs
1123
- );
1116
+ const klendIxs: LeverageIxsOutput = (
1117
+ await buildDecreaseLeverageIxs(
1118
+ owner,
1119
+ kaminoMarket,
1120
+ collTokenMint,
1121
+ debtTokenMint,
1122
+ obligation,
1123
+ referrer,
1124
+ currentSlot,
1125
+ calcs,
1126
+ strategy,
1127
+ scopeRefreshConfig,
1128
+ collIsKtoken,
1129
+ [
1130
+ {
1131
+ preActionIxs: [],
1132
+ swapIxs: [],
1133
+ lookupTables: [],
1134
+ quote: {
1135
+ priceAInB: new Decimal(0), // not used
1136
+ quoteResponse: undefined,
1137
+ },
1138
+ },
1139
+ ],
1140
+ budgetAndPriorityFeeIxs,
1141
+ useV2Ixs
1142
+ )
1143
+ )[0];
1124
1144
 
1125
1145
  const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
1126
1146
 
@@ -1255,39 +1275,39 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
1255
1275
 
1256
1276
  const swapsArray = await depositSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
1257
1277
 
1258
- return Promise.all(
1259
- swapsArray.map(async (swap) => {
1260
- const ixs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
1261
- owner,
1262
- kaminoMarket,
1263
- collTokenMint,
1264
- debtTokenMint,
1265
- obligation,
1266
- referrer,
1267
- currentSlot,
1268
- initialInputs.calcs,
1269
- initialInputs.strategy,
1270
- scopeRefreshConfig,
1271
- initialInputs.collIsKtoken,
1272
- {
1273
- preActionIxs: [],
1274
- swapIxs: swap.swapIxs,
1275
- lookupTables: swap.lookupTables,
1276
- quote: swap.quote,
1277
- },
1278
- budgetAndPriorityFeeIxs,
1279
- useV2Ixs
1280
- );
1278
+ const increaseLeverageIxs = await buildIncreaseLeverageIxs(
1279
+ owner,
1280
+ kaminoMarket,
1281
+ collTokenMint,
1282
+ debtTokenMint,
1283
+ obligation,
1284
+ referrer,
1285
+ currentSlot,
1286
+ initialInputs.calcs,
1287
+ initialInputs.strategy,
1288
+ scopeRefreshConfig,
1289
+ initialInputs.collIsKtoken,
1290
+ swapsArray.map((swap) => {
1281
1291
  return {
1282
- ixs: ixs.instructions,
1283
- flashLoanInfo: ixs.flashLoanInfo,
1292
+ preActionIxs: [],
1293
+ swapIxs: swap.swapIxs,
1284
1294
  lookupTables: swap.lookupTables,
1285
- swapInputs,
1286
- initialInputs,
1287
- quote: swap.quote.quoteResponse,
1295
+ quote: swap.quote,
1288
1296
  };
1289
- })
1297
+ }),
1298
+ budgetAndPriorityFeeIxs,
1299
+ useV2Ixs
1290
1300
  );
1301
+ return increaseLeverageIxs.map((ixs, index) => {
1302
+ return {
1303
+ ixs: ixs.instructions,
1304
+ flashLoanInfo: ixs.flashLoanInfo,
1305
+ lookupTables: swapsArray[index].lookupTables,
1306
+ swapInputs,
1307
+ initialInputs,
1308
+ quote: swapsArray[index].quote.quoteResponse,
1309
+ };
1310
+ });
1291
1311
  } else {
1292
1312
  console.log('Decreasing leverage');
1293
1313
 
@@ -1305,40 +1325,40 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
1305
1325
  // 5. Get swap ixs
1306
1326
  const swapsArray = await withdrawSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
1307
1327
 
1308
- return Promise.all(
1309
- swapsArray.map(async (swap) => {
1310
- const ixs: LeverageIxsOutput = await buildDecreaseLeverageIxs(
1311
- owner,
1312
- kaminoMarket,
1313
- collTokenMint,
1314
- debtTokenMint,
1315
- obligation,
1316
- referrer,
1317
- currentSlot,
1318
- initialInputs.calcs,
1319
- initialInputs.strategy,
1320
- scopeRefreshConfig,
1321
- initialInputs.collIsKtoken,
1322
- {
1323
- preActionIxs: [],
1324
- swapIxs: swap.swapIxs,
1325
- lookupTables: swap.lookupTables,
1326
- quote: swap.quote,
1327
- },
1328
- budgetAndPriorityFeeIxs,
1329
- useV2Ixs
1330
- );
1331
-
1328
+ const decreaseLeverageIxs = await buildDecreaseLeverageIxs(
1329
+ owner,
1330
+ kaminoMarket,
1331
+ collTokenMint,
1332
+ debtTokenMint,
1333
+ obligation,
1334
+ referrer,
1335
+ currentSlot,
1336
+ initialInputs.calcs,
1337
+ initialInputs.strategy,
1338
+ scopeRefreshConfig,
1339
+ initialInputs.collIsKtoken,
1340
+ swapsArray.map((swap) => {
1332
1341
  return {
1333
- ixs: ixs.instructions,
1334
- flashLoanInfo: ixs.flashLoanInfo,
1342
+ preActionIxs: [],
1343
+ swapIxs: swap.swapIxs,
1335
1344
  lookupTables: swap.lookupTables,
1336
- swapInputs,
1337
- initialInputs,
1338
- quote: swap.quote.quoteResponse,
1345
+ quote: swap.quote,
1339
1346
  };
1340
- })
1347
+ }),
1348
+ budgetAndPriorityFeeIxs,
1349
+ useV2Ixs
1341
1350
  );
1351
+
1352
+ return decreaseLeverageIxs.map((ixs, index) => {
1353
+ return {
1354
+ ixs: ixs.instructions,
1355
+ flashLoanInfo: ixs.flashLoanInfo,
1356
+ lookupTables: swapsArray[index].lookupTables,
1357
+ swapInputs,
1358
+ initialInputs,
1359
+ quote: swapsArray[index].quote.quoteResponse,
1360
+ };
1361
+ });
1342
1362
  }
1343
1363
  }
1344
1364
 
@@ -1357,10 +1377,10 @@ async function buildIncreaseLeverageIxs<QuoteResponse>(
1357
1377
  strategy: StrategyWithAddress | undefined,
1358
1378
  scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
1359
1379
  collIsKtoken: boolean,
1360
- swapQuoteIxs: SwapIxs<QuoteResponse>,
1380
+ swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
1361
1381
  budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
1362
1382
  useV2Ixs: boolean
1363
- ): Promise<LeverageIxsOutput> {
1383
+ ): Promise<LeverageIxsOutput[]> {
1364
1384
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
1365
1385
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
1366
1386
  const debtTokenAta = getAssociatedTokenAddressSync(
@@ -1442,30 +1462,32 @@ async function buildIncreaseLeverageIxs<QuoteResponse>(
1442
1462
  currentSlot
1443
1463
  );
1444
1464
 
1445
- const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
1446
-
1447
- const ixs = [
1448
- ...scopeRefreshIx,
1449
- ...budgetIxs,
1450
- ...createAtasIxs,
1451
- ...[flashBorrowIx],
1452
- ...(collIsKtoken ? swapInstructions : []),
1453
- ...KaminoAction.actionToIxs(depositAction),
1454
- ...KaminoAction.actionToIxs(borrowAction),
1455
- ...(collIsKtoken ? [] : swapInstructions),
1456
- ...[flashRepayIx],
1457
- ];
1465
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
1466
+ const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
1458
1467
 
1459
- const flashBorrowReserve = !collIsKtoken ? collReserve! : debtReserve!;
1460
- const res: LeverageIxsOutput = {
1461
- flashLoanInfo: {
1462
- flashBorrowReserve: flashBorrowReserve.address,
1463
- flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
1464
- },
1465
- instructions: ixs,
1466
- };
1468
+ const ixs = [
1469
+ ...scopeRefreshIx,
1470
+ ...budgetIxs,
1471
+ ...createAtasIxs,
1472
+ ...[flashBorrowIx],
1473
+ ...(collIsKtoken ? swapInstructions : []),
1474
+ ...KaminoAction.actionToIxs(depositAction),
1475
+ ...KaminoAction.actionToIxs(borrowAction),
1476
+ ...(collIsKtoken ? [] : swapInstructions),
1477
+ ...[flashRepayIx],
1478
+ ];
1467
1479
 
1468
- return res;
1480
+ const flashBorrowReserve = !collIsKtoken ? collReserve! : debtReserve!;
1481
+ const res: LeverageIxsOutput = {
1482
+ flashLoanInfo: {
1483
+ flashBorrowReserve: flashBorrowReserve.address,
1484
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
1485
+ },
1486
+ instructions: ixs,
1487
+ };
1488
+
1489
+ return res;
1490
+ });
1469
1491
  }
1470
1492
 
1471
1493
  /**
@@ -1483,10 +1505,10 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
1483
1505
  strategy: StrategyWithAddress | undefined,
1484
1506
  scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
1485
1507
  collIsKtoken: boolean,
1486
- swapQuoteIxs: SwapIxs<QuoteResponse>,
1508
+ swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
1487
1509
  budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
1488
1510
  useV2Ixs: boolean
1489
- ): Promise<LeverageIxsOutput> {
1511
+ ): Promise<LeverageIxsOutput[]> {
1490
1512
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
1491
1513
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
1492
1514
  const debtTokenAta = getAssociatedTokenAddressSync(
@@ -1578,30 +1600,32 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
1578
1600
  currentSlot
1579
1601
  );
1580
1602
 
1581
- const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
1582
-
1583
- const ixs = [
1584
- ...scopeRefreshIx,
1585
- ...budgetIxs,
1586
- ...createAtasIxs,
1587
- ...fillWsolAtaIxs,
1588
- ...[flashBorrowIx],
1589
- ...KaminoAction.actionToIxs(repayAction),
1590
- ...KaminoAction.actionToIxs(withdrawAction),
1591
- ...swapInstructions,
1592
- ...[flashRepayIx],
1593
- ...closeWsolAtaIxs,
1594
- ];
1603
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
1604
+ const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
1595
1605
 
1596
- const res: LeverageIxsOutput = {
1597
- flashLoanInfo: {
1598
- flashBorrowReserve: debtReserve!.address,
1599
- flashLoanFee: debtReserve!.getFlashLoanFee(),
1600
- },
1601
- instructions: ixs,
1602
- };
1606
+ const ixs = [
1607
+ ...scopeRefreshIx,
1608
+ ...budgetIxs,
1609
+ ...createAtasIxs,
1610
+ ...fillWsolAtaIxs,
1611
+ ...[flashBorrowIx],
1612
+ ...KaminoAction.actionToIxs(repayAction),
1613
+ ...KaminoAction.actionToIxs(withdrawAction),
1614
+ ...swapInstructions,
1615
+ ...[flashRepayIx],
1616
+ ...closeWsolAtaIxs,
1617
+ ];
1603
1618
 
1604
- return res;
1619
+ const res: LeverageIxsOutput = {
1620
+ flashLoanInfo: {
1621
+ flashBorrowReserve: debtReserve!.address,
1622
+ flashLoanFee: debtReserve!.getFlashLoanFee(),
1623
+ },
1624
+ instructions: ixs,
1625
+ };
1626
+
1627
+ return res;
1628
+ });
1605
1629
  }
1606
1630
 
1607
1631
  export const getSetupIxs = async (