@kamino-finance/klend-sdk 7.0.7 → 7.0.8

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 (32) hide show
  1. package/dist/classes/action.d.ts +1 -1
  2. package/dist/classes/action.d.ts.map +1 -1
  3. package/dist/classes/action.js +15 -15
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/obligation.js +7 -6
  6. package/dist/classes/obligation.js.map +1 -1
  7. package/dist/classes/vault.d.ts.map +1 -1
  8. package/dist/classes/vault.js +0 -1
  9. package/dist/classes/vault.js.map +1 -1
  10. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  11. package/dist/lending_operations/repay_with_collateral_operations.js +36 -32
  12. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  13. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  14. package/dist/lending_operations/swap_collateral_operations.js +6 -6
  15. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  16. package/dist/leverage/operations.d.ts +4 -3
  17. package/dist/leverage/operations.d.ts.map +1 -1
  18. package/dist/leverage/operations.js +175 -149
  19. package/dist/leverage/operations.js.map +1 -1
  20. package/dist/leverage/types.d.ts +1 -0
  21. package/dist/leverage/types.d.ts.map +1 -1
  22. package/dist/manager/client_kamino_manager.js +2 -2
  23. package/dist/manager/client_kamino_manager.js.map +1 -1
  24. package/package.json +1 -1
  25. package/src/classes/action.ts +15 -15
  26. package/src/classes/obligation.ts +9 -9
  27. package/src/classes/vault.ts +0 -1
  28. package/src/lending_operations/repay_with_collateral_operations.ts +78 -74
  29. package/src/lending_operations/swap_collateral_operations.ts +15 -13
  30. package/src/leverage/operations.ts +350 -318
  31. package/src/leverage/types.ts +1 -0
  32. package/src/manager/client_kamino_manager.ts +2 -2
@@ -69,6 +69,8 @@ import { findAssociatedTokenPda, getCloseAccountInstruction } from '@solana-prog
69
69
  import { LAMPORTS_PER_SOL } from '../utils/consts';
70
70
  import { fetchAddressLookupTable } from '@solana-program/address-lookup-table';
71
71
 
72
+ export const WITHDRAW_SLOT_OFFSET = 150; // Offset for the withdraw slot to underestimate the exchange rate
73
+
72
74
  export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
73
75
  owner,
74
76
  kaminoMarket,
@@ -129,32 +131,36 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
129
131
  const obligationType = checkObligationType(obligationTypeTagOverride, collTokenMint, debtTokenMint, kaminoMarket);
130
132
 
131
133
  // Build the repay & withdraw collateral tx to get the number of accounts
132
- const klendIxs: LeverageIxsOutput = await buildDepositWithLeverageIxs(
133
- kaminoMarket,
134
- debtReserve,
135
- collReserve,
136
- owner,
137
- obligation ? obligation : obligationType,
138
- referrer,
139
- currentSlot,
140
- depositTokenIsSol,
141
- scopeRefreshConfig,
142
- calcs,
143
- budgetAndPriorityFeeIxs,
144
- {
145
- preActionIxs: [],
146
- swapIxs: [],
147
- lookupTables: [],
148
- quote: {
149
- priceAInB: new Decimal(0), // not used
150
- quoteResponse: undefined,
151
- },
152
- },
153
- strategy,
154
- collIsKtoken,
155
- useV2Ixs,
156
- elevationGroupOverride
157
- );
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];
158
164
 
159
165
  const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
160
166
 
@@ -376,42 +382,42 @@ export async function getDepositWithLeverageIxs<QuoteResponse>({
376
382
  const solTokenReserve = kaminoMarket.getReserveByMint(WRAPPED_SOL_MINT);
377
383
  const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint === solTokenReserve!.getLiquidityMint();
378
384
 
379
- return Promise.all(
380
- swapsArray.map(async (swap) => {
381
- const ixs: LeverageIxsOutput = await buildDepositWithLeverageIxs(
382
- kaminoMarket,
383
- debtReserve!,
384
- collReserve!,
385
- owner,
386
- initialInputs.obligation,
387
- referrer,
388
- currentSlot,
389
- depositTokenIsSol,
390
- scopeRefreshConfig,
391
- initialInputs.calcs,
392
- budgetAndPriorityFeeIxs,
393
- {
394
- preActionIxs: [],
395
- swapIxs: swap.swapIxs,
396
- lookupTables: swap.lookupTables,
397
- quote: swap.quote,
398
- },
399
- initialInputs.strategy,
400
- initialInputs.collIsKtoken,
401
- useV2Ixs,
402
- elevationGroupOverride
403
- );
404
-
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) => {
405
398
  return {
406
- ixs: ixs.instructions,
407
- flashLoanInfo: ixs.flashLoanInfo,
399
+ preActionIxs: [],
400
+ swapIxs: swap.swapIxs,
408
401
  lookupTables: swap.lookupTables,
409
- swapInputs,
410
- initialInputs,
411
- quote: swap.quote.quoteResponse,
402
+ quote: swap.quote,
412
403
  };
413
- })
404
+ }),
405
+ initialInputs.strategy,
406
+ initialInputs.collIsKtoken,
407
+ useV2Ixs,
408
+ elevationGroupOverride
414
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
+ });
415
421
  }
416
422
 
417
423
  async function buildDepositWithLeverageIxs<QuoteResponse>(
@@ -426,12 +432,12 @@ async function buildDepositWithLeverageIxs<QuoteResponse>(
426
432
  scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
427
433
  calcs: DepositLeverageCalcsResult,
428
434
  budgetAndPriorityFeeIxs: IInstruction[] | undefined,
429
- swapQuoteIxs: SwapIxs<QuoteResponse>,
435
+ swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
430
436
  strategy: StrategyWithAddress | undefined,
431
437
  collIsKtoken: boolean,
432
438
  useV2Ixs: boolean,
433
439
  elevationGroupOverride?: number
434
- ): Promise<LeverageIxsOutput> {
440
+ ): Promise<LeverageIxsOutput[]> {
435
441
  const collTokenMint = collReserve.getLiquidityMint();
436
442
  const debtTokenMint = debtReserve.getLiquidityMint();
437
443
  const [[collTokenAta], [debtTokenAta]] = await Promise.all([
@@ -516,29 +522,31 @@ async function buildDepositWithLeverageIxs<QuoteResponse>(
516
522
  currentSlot
517
523
  );
518
524
 
519
- // 4. Swap
520
- const { swapIxs } = swapQuoteIxs;
521
- const swapInstructions = removeBudgetIxs(swapIxs);
522
- const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
523
- const flashLoanInfo = {
524
- flashBorrowReserve: flashBorrowReserve.address,
525
- flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
526
- };
525
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
526
+ // 4. Swap
527
+ const { swapIxs } = swapQuoteIxs;
528
+ const swapInstructions = removeBudgetIxs(swapIxs);
529
+ const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
530
+ const flashLoanInfo = {
531
+ flashBorrowReserve: flashBorrowReserve.address,
532
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
533
+ };
527
534
 
528
- return {
529
- flashLoanInfo,
530
- instructions: [
531
- ...scopeRefreshIx,
532
- ...budgetIxs,
533
- ...createAtasIxs,
534
- ...fillWsolAtaIxs,
535
- ...[flashBorrowIx],
536
- ...(collIsKtoken ? swapInstructions : []),
537
- ...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
538
- ...(collIsKtoken ? [] : swapInstructions),
539
- ...[flashRepayIx],
540
- ],
541
- };
535
+ return {
536
+ flashLoanInfo,
537
+ instructions: [
538
+ ...scopeRefreshIx,
539
+ ...budgetIxs,
540
+ ...createAtasIxs,
541
+ ...fillWsolAtaIxs,
542
+ ...[flashBorrowIx],
543
+ ...(collIsKtoken ? swapInstructions : []),
544
+ ...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
545
+ ...(collIsKtoken ? [] : swapInstructions),
546
+ ...[flashRepayIx],
547
+ ],
548
+ };
549
+ });
542
550
  }
543
551
 
544
552
  export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
@@ -594,32 +602,36 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
594
602
  slippagePct
595
603
  );
596
604
 
597
- const klendIxs = await buildWithdrawWithLeverageIxs(
598
- kaminoMarket,
599
- debtReserve!,
600
- collReserve!,
601
- owner,
602
- obligation,
603
- referrer,
604
- currentSlot,
605
- isClosingPosition,
606
- inputTokenIsSol,
607
- scopeRefreshConfig,
608
- calcs,
609
- budgetAndPriorityFeeIxs,
610
- {
611
- preActionIxs: [],
612
- swapIxs: [],
613
- lookupTables: [],
614
- quote: {
615
- priceAInB: new Decimal(0), // not used
616
- quoteResponse: undefined,
617
- },
618
- },
619
- strategy,
620
- collIsKtoken,
621
- useV2Ixs
622
- );
605
+ const klendIxs = (
606
+ await buildWithdrawWithLeverageIxs(
607
+ kaminoMarket,
608
+ debtReserve!,
609
+ collReserve!,
610
+ owner,
611
+ obligation,
612
+ referrer,
613
+ currentSlot,
614
+ isClosingPosition,
615
+ inputTokenIsSol,
616
+ scopeRefreshConfig,
617
+ calcs,
618
+ budgetAndPriorityFeeIxs,
619
+ [
620
+ {
621
+ preActionIxs: [],
622
+ swapIxs: [],
623
+ lookupTables: [],
624
+ quote: {
625
+ priceAInB: new Decimal(0), // not used
626
+ quoteResponse: undefined,
627
+ },
628
+ },
629
+ ],
630
+ strategy,
631
+ collIsKtoken,
632
+ useV2Ixs
633
+ )
634
+ )[0];
623
635
 
624
636
  const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
625
637
 
@@ -753,43 +765,43 @@ export async function getWithdrawWithLeverageIxs<QuoteResponse>({
753
765
  }
754
766
  }
755
767
 
756
- return Promise.all(
757
- swapsArray.map(async (swap) => {
758
- const ixs: LeverageIxsOutput = await buildWithdrawWithLeverageIxs<QuoteResponse>(
759
- kaminoMarket,
760
- debtReserve!,
761
- collReserve!,
762
- owner,
763
- obligation,
764
- referrer,
765
- currentSlot,
766
- isClosingPosition,
767
- inputTokenIsSol,
768
- scopeRefreshConfig,
769
- initialInputs.calcs,
770
- budgetAndPriorityFeeIxs,
771
- {
772
- preActionIxs: [],
773
- swapIxs: swap.swapIxs,
774
- lookupTables: swap.lookupTables,
775
- quote: swap.quote,
776
- },
777
- initialInputs.strategy,
778
- initialInputs.collIsKtoken,
779
- useV2Ixs
780
- );
781
-
782
- // Send ixs and lookup tables
768
+ const withdrawWithLeverageIxs = await buildWithdrawWithLeverageIxs<QuoteResponse>(
769
+ kaminoMarket,
770
+ debtReserve!,
771
+ collReserve!,
772
+ owner,
773
+ obligation,
774
+ referrer,
775
+ currentSlot,
776
+ isClosingPosition,
777
+ inputTokenIsSol,
778
+ scopeRefreshConfig,
779
+ initialInputs.calcs,
780
+ budgetAndPriorityFeeIxs,
781
+ swapsArray.map((swap) => {
783
782
  return {
784
- ixs: ixs.instructions,
785
- flashLoanInfo: ixs.flashLoanInfo,
783
+ preActionIxs: [],
784
+ swapIxs: swap.swapIxs,
786
785
  lookupTables: swap.lookupTables,
787
- swapInputs,
788
- initialInputs: initialInputs,
789
- quote: swap.quote.quoteResponse,
786
+ quote: swap.quote,
790
787
  };
791
- })
788
+ }),
789
+ initialInputs.strategy,
790
+ initialInputs.collIsKtoken,
791
+ useV2Ixs
792
792
  );
793
+
794
+ // Send ixs and lookup tables
795
+ return withdrawWithLeverageIxs.map((ixs, index) => {
796
+ return {
797
+ ixs: ixs.instructions,
798
+ flashLoanInfo: ixs.flashLoanInfo,
799
+ lookupTables: swapsArray[index].lookupTables,
800
+ swapInputs,
801
+ initialInputs: initialInputs,
802
+ quote: swapsArray[index].quote.quoteResponse,
803
+ };
804
+ });
793
805
  }
794
806
 
795
807
  export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
@@ -805,11 +817,11 @@ export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
805
817
  scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
806
818
  calcs: WithdrawLeverageCalcsResult,
807
819
  budgetAndPriorityFeeIxs: IInstruction[] | undefined,
808
- swapQuoteIxs: SwapIxs<QuoteResponse>,
820
+ swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
809
821
  strategy: StrategyWithAddress | undefined,
810
822
  collIsKtoken: boolean,
811
823
  useV2Ixs: boolean
812
- ): Promise<LeverageIxsOutput> {
824
+ ): Promise<LeverageIxsOutput[]> {
813
825
  const collTokenMint = collReserve.getLiquidityMint();
814
826
  const debtTokenMint = debtReserve.getLiquidityMint();
815
827
  const debtTokenAta = await getAssociatedTokenAddress(
@@ -903,25 +915,27 @@ export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
903
915
  referrer
904
916
  );
905
917
 
906
- const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
918
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
919
+ const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
907
920
 
908
- return {
909
- flashLoanInfo: {
910
- flashLoanFee: debtReserve.getFlashLoanFee(),
911
- flashBorrowReserve: debtReserve.address,
912
- },
913
- instructions: [
914
- ...scopeRefreshIx,
915
- ...budgetIxs,
916
- ...createAtasIxs,
917
- ...fillWsolAtaIxs,
918
- ...[flashBorrowIx],
919
- ...KaminoAction.actionToIxs(repayAndWithdrawAction),
920
- ...swapInstructions,
921
- ...[flashRepayIx],
922
- ...closeWsolAtaIxs,
923
- ],
924
- };
921
+ return {
922
+ flashLoanInfo: {
923
+ flashLoanFee: debtReserve.getFlashLoanFee(),
924
+ flashBorrowReserve: debtReserve.address,
925
+ },
926
+ instructions: [
927
+ ...scopeRefreshIx,
928
+ ...budgetIxs,
929
+ ...createAtasIxs,
930
+ ...fillWsolAtaIxs,
931
+ ...[flashBorrowIx],
932
+ ...KaminoAction.actionToIxs(repayAndWithdrawAction),
933
+ ...swapInstructions,
934
+ ...[flashRepayIx],
935
+ ...closeWsolAtaIxs,
936
+ ],
937
+ };
938
+ });
925
939
  }
926
940
 
927
941
  export async function getAdjustLeverageSwapInputs<QuoteResponse>({
@@ -945,6 +959,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
945
959
  isKtoken,
946
960
  quoter,
947
961
  useV2Ixs,
962
+ withdrawSlotOffset,
948
963
  }: AdjustLeverageSwapInputsProps<QuoteResponse>): Promise<{
949
964
  swapInputs: SwapInputs;
950
965
  flashLoanInfo: FlashLoanInfo;
@@ -994,30 +1009,34 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
994
1009
  );
995
1010
 
996
1011
  // Build the repay & withdraw collateral tx to get the number of accounts
997
- const klendIxs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
998
- owner,
999
- kaminoMarket,
1000
- collTokenMint,
1001
- debtTokenMint,
1002
- obligation,
1003
- referrer,
1004
- currentSlot,
1005
- calcs,
1006
- strategy,
1007
- scopeRefreshConfig,
1008
- collIsKtoken,
1009
- {
1010
- preActionIxs: [],
1011
- swapIxs: [],
1012
- lookupTables: [],
1013
- quote: {
1014
- priceAInB: new Decimal(0), // not used
1015
- quoteResponse: undefined,
1016
- },
1017
- },
1018
- budgetAndPriorityFeeIxs,
1019
- useV2Ixs
1020
- );
1012
+ const klendIxs: LeverageIxsOutput = (
1013
+ await buildIncreaseLeverageIxs(
1014
+ owner,
1015
+ kaminoMarket,
1016
+ collTokenMint,
1017
+ debtTokenMint,
1018
+ obligation,
1019
+ referrer,
1020
+ currentSlot,
1021
+ calcs,
1022
+ strategy,
1023
+ scopeRefreshConfig,
1024
+ collIsKtoken,
1025
+ [
1026
+ {
1027
+ preActionIxs: [],
1028
+ swapIxs: [],
1029
+ lookupTables: [],
1030
+ quote: {
1031
+ priceAInB: new Decimal(0), // not used
1032
+ quoteResponse: undefined,
1033
+ },
1034
+ },
1035
+ ],
1036
+ budgetAndPriorityFeeIxs,
1037
+ useV2Ixs
1038
+ )
1039
+ )[0];
1021
1040
 
1022
1041
  const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
1023
1042
 
@@ -1103,30 +1122,35 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1103
1122
  } else {
1104
1123
  const calcs = adjustWithdrawLeverageCalcs(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
1105
1124
 
1106
- const klendIxs: LeverageIxsOutput = await buildDecreaseLeverageIxs(
1107
- owner,
1108
- kaminoMarket,
1109
- collTokenMint,
1110
- debtTokenMint,
1111
- obligation,
1112
- referrer,
1113
- currentSlot,
1114
- calcs,
1115
- strategy,
1116
- scopeRefreshConfig,
1117
- collIsKtoken,
1118
- {
1119
- preActionIxs: [],
1120
- swapIxs: [],
1121
- lookupTables: [],
1122
- quote: {
1123
- priceAInB: new Decimal(0), // not used
1124
- quoteResponse: undefined,
1125
- },
1126
- },
1127
- budgetAndPriorityFeeIxs,
1128
- useV2Ixs
1129
- );
1125
+ const klendIxs: LeverageIxsOutput = (
1126
+ await buildDecreaseLeverageIxs(
1127
+ owner,
1128
+ kaminoMarket,
1129
+ collTokenMint,
1130
+ debtTokenMint,
1131
+ obligation,
1132
+ referrer,
1133
+ currentSlot,
1134
+ calcs,
1135
+ strategy,
1136
+ scopeRefreshConfig,
1137
+ collIsKtoken,
1138
+ [
1139
+ {
1140
+ preActionIxs: [],
1141
+ swapIxs: [],
1142
+ lookupTables: [],
1143
+ quote: {
1144
+ priceAInB: new Decimal(0), // not used
1145
+ quoteResponse: undefined,
1146
+ },
1147
+ },
1148
+ ],
1149
+ budgetAndPriorityFeeIxs,
1150
+ useV2Ixs,
1151
+ withdrawSlotOffset
1152
+ )
1153
+ )[0];
1130
1154
 
1131
1155
  const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
1132
1156
 
@@ -1213,6 +1237,7 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
1213
1237
  quoter,
1214
1238
  swapper,
1215
1239
  useV2Ixs,
1240
+ withdrawSlotOffset,
1216
1241
  }: AdjustLeverageProps<QuoteResponse>): Promise<Array<AdjustLeverageIxsResponse<QuoteResponse>>> {
1217
1242
  const { swapInputs, initialInputs } = await getAdjustLeverageSwapInputs({
1218
1243
  owner,
@@ -1261,39 +1286,39 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
1261
1286
 
1262
1287
  const swapsArray = await depositSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
1263
1288
 
1264
- return Promise.all(
1265
- swapsArray.map(async (swap) => {
1266
- const ixs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
1267
- owner,
1268
- kaminoMarket,
1269
- collTokenMint,
1270
- debtTokenMint,
1271
- obligation,
1272
- referrer,
1273
- currentSlot,
1274
- initialInputs.calcs,
1275
- initialInputs.strategy,
1276
- scopeRefreshConfig,
1277
- initialInputs.collIsKtoken,
1278
- {
1279
- preActionIxs: [],
1280
- swapIxs: swap.swapIxs,
1281
- lookupTables: swap.lookupTables,
1282
- quote: swap.quote,
1283
- },
1284
- budgetAndPriorityFeeIxs,
1285
- useV2Ixs
1286
- );
1289
+ const increaseLeverageIxs = await buildIncreaseLeverageIxs(
1290
+ owner,
1291
+ kaminoMarket,
1292
+ collTokenMint,
1293
+ debtTokenMint,
1294
+ obligation,
1295
+ referrer,
1296
+ currentSlot,
1297
+ initialInputs.calcs,
1298
+ initialInputs.strategy,
1299
+ scopeRefreshConfig,
1300
+ initialInputs.collIsKtoken,
1301
+ swapsArray.map((swap) => {
1287
1302
  return {
1288
- ixs: ixs.instructions,
1289
- flashLoanInfo: ixs.flashLoanInfo,
1303
+ preActionIxs: [],
1304
+ swapIxs: swap.swapIxs,
1290
1305
  lookupTables: swap.lookupTables,
1291
- swapInputs,
1292
- initialInputs,
1293
- quote: swap.quote.quoteResponse,
1306
+ quote: swap.quote,
1294
1307
  };
1295
- })
1308
+ }),
1309
+ budgetAndPriorityFeeIxs,
1310
+ useV2Ixs
1296
1311
  );
1312
+ return increaseLeverageIxs.map((ixs, index) => {
1313
+ return {
1314
+ ixs: ixs.instructions,
1315
+ flashLoanInfo: ixs.flashLoanInfo,
1316
+ lookupTables: swapsArray[index].lookupTables,
1317
+ swapInputs,
1318
+ initialInputs,
1319
+ quote: swapsArray[index].quote.quoteResponse,
1320
+ };
1321
+ });
1297
1322
  } else {
1298
1323
  console.log('Decreasing leverage');
1299
1324
 
@@ -1311,40 +1336,41 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
1311
1336
  // 5. Get swap ixs
1312
1337
  const swapsArray = await withdrawSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
1313
1338
 
1314
- return Promise.all(
1315
- swapsArray.map(async (swap) => {
1316
- const ixs: LeverageIxsOutput = await buildDecreaseLeverageIxs(
1317
- owner,
1318
- kaminoMarket,
1319
- collTokenMint,
1320
- debtTokenMint,
1321
- obligation,
1322
- referrer,
1323
- currentSlot,
1324
- initialInputs.calcs,
1325
- initialInputs.strategy,
1326
- scopeRefreshConfig,
1327
- initialInputs.collIsKtoken,
1328
- {
1329
- preActionIxs: [],
1330
- swapIxs: swap.swapIxs,
1331
- lookupTables: swap.lookupTables,
1332
- quote: swap.quote,
1333
- },
1334
- budgetAndPriorityFeeIxs,
1335
- useV2Ixs
1336
- );
1337
-
1339
+ const decreaseLeverageIxs = await buildDecreaseLeverageIxs(
1340
+ owner,
1341
+ kaminoMarket,
1342
+ collTokenMint,
1343
+ debtTokenMint,
1344
+ obligation,
1345
+ referrer,
1346
+ currentSlot,
1347
+ initialInputs.calcs,
1348
+ initialInputs.strategy,
1349
+ scopeRefreshConfig,
1350
+ initialInputs.collIsKtoken,
1351
+ swapsArray.map((swap) => {
1338
1352
  return {
1339
- ixs: ixs.instructions,
1340
- flashLoanInfo: ixs.flashLoanInfo,
1353
+ preActionIxs: [],
1354
+ swapIxs: swap.swapIxs,
1341
1355
  lookupTables: swap.lookupTables,
1342
- swapInputs,
1343
- initialInputs,
1344
- quote: swap.quote.quoteResponse,
1356
+ quote: swap.quote,
1345
1357
  };
1346
- })
1358
+ }),
1359
+ budgetAndPriorityFeeIxs,
1360
+ useV2Ixs,
1361
+ withdrawSlotOffset
1347
1362
  );
1363
+
1364
+ return decreaseLeverageIxs.map((ixs, index) => {
1365
+ return {
1366
+ ixs: ixs.instructions,
1367
+ flashLoanInfo: ixs.flashLoanInfo,
1368
+ lookupTables: swapsArray[index].lookupTables,
1369
+ swapInputs,
1370
+ initialInputs,
1371
+ quote: swapsArray[index].quote.quoteResponse,
1372
+ };
1373
+ });
1348
1374
  }
1349
1375
  }
1350
1376
 
@@ -1363,10 +1389,10 @@ async function buildIncreaseLeverageIxs<QuoteResponse>(
1363
1389
  strategy: StrategyWithAddress | undefined,
1364
1390
  scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
1365
1391
  collIsKtoken: boolean,
1366
- swapQuoteIxs: SwapIxs<QuoteResponse>,
1392
+ swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
1367
1393
  budgetAndPriorityFeeIxs: IInstruction[] | undefined,
1368
1394
  useV2Ixs: boolean
1369
- ): Promise<LeverageIxsOutput> {
1395
+ ): Promise<LeverageIxsOutput[]> {
1370
1396
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
1371
1397
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
1372
1398
  const [debtTokenAta, collTokenAta] = await Promise.all([
@@ -1440,30 +1466,32 @@ async function buildIncreaseLeverageIxs<QuoteResponse>(
1440
1466
  currentSlot
1441
1467
  );
1442
1468
 
1443
- const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
1444
-
1445
- const ixs = [
1446
- ...scopeRefreshIx,
1447
- ...budgetIxs,
1448
- ...createAtasIxs,
1449
- ...[flashBorrowIx],
1450
- ...(collIsKtoken ? swapInstructions : []),
1451
- ...KaminoAction.actionToIxs(depositAction),
1452
- ...KaminoAction.actionToIxs(borrowAction),
1453
- ...(collIsKtoken ? [] : swapInstructions),
1454
- ...[flashRepayIx],
1455
- ];
1469
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
1470
+ const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
1456
1471
 
1457
- const flashBorrowReserve = !collIsKtoken ? collReserve! : debtReserve!;
1458
- const res: LeverageIxsOutput = {
1459
- flashLoanInfo: {
1460
- flashBorrowReserve: flashBorrowReserve.address,
1461
- flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
1462
- },
1463
- instructions: ixs,
1464
- };
1472
+ const ixs = [
1473
+ ...scopeRefreshIx,
1474
+ ...budgetIxs,
1475
+ ...createAtasIxs,
1476
+ ...[flashBorrowIx],
1477
+ ...(collIsKtoken ? swapInstructions : []),
1478
+ ...KaminoAction.actionToIxs(depositAction),
1479
+ ...KaminoAction.actionToIxs(borrowAction),
1480
+ ...(collIsKtoken ? [] : swapInstructions),
1481
+ ...[flashRepayIx],
1482
+ ];
1483
+
1484
+ const flashBorrowReserve = !collIsKtoken ? collReserve! : debtReserve!;
1485
+ const res: LeverageIxsOutput = {
1486
+ flashLoanInfo: {
1487
+ flashBorrowReserve: flashBorrowReserve.address,
1488
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
1489
+ },
1490
+ instructions: ixs,
1491
+ };
1465
1492
 
1466
- return res;
1493
+ return res;
1494
+ });
1467
1495
  }
1468
1496
 
1469
1497
  /**
@@ -1481,10 +1509,11 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
1481
1509
  strategy: StrategyWithAddress | undefined,
1482
1510
  scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
1483
1511
  collIsKtoken: boolean,
1484
- swapQuoteIxs: SwapIxs<QuoteResponse>,
1512
+ swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
1485
1513
  budgetAndPriorityFeeIxs: IInstruction[] | undefined,
1486
- useV2Ixs: boolean
1487
- ): Promise<LeverageIxsOutput> {
1514
+ useV2Ixs: boolean,
1515
+ withdrawSlotOffset: number = WITHDRAW_SLOT_OFFSET
1516
+ ): Promise<LeverageIxsOutput[]> {
1488
1517
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
1489
1518
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
1490
1519
  const [debtTokenAta] = await findAssociatedTokenPda({
@@ -1574,6 +1603,7 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
1574
1603
  referrer
1575
1604
  );
1576
1605
 
1606
+ const withdrawSlot = currentSlot - BigInt(withdrawSlotOffset);
1577
1607
  // 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
1578
1608
  const withdrawAction = await KaminoAction.buildWithdrawTxns(
1579
1609
  kaminoMarket,
@@ -1588,33 +1618,35 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
1588
1618
  false,
1589
1619
  { skipInitialization: true, skipLutCreation: true }, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
1590
1620
  referrer,
1591
- currentSlot
1621
+ withdrawSlot
1592
1622
  );
1593
1623
 
1594
- const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
1595
-
1596
- const ixs = [
1597
- ...scopeRefreshIx,
1598
- ...budgetIxs,
1599
- ...createAtasIxs,
1600
- ...fillWsolAtaIxs,
1601
- ...[flashBorrowIx],
1602
- ...KaminoAction.actionToIxs(repayAction),
1603
- ...KaminoAction.actionToIxs(withdrawAction),
1604
- ...swapInstructions,
1605
- ...[flashRepayIx],
1606
- ...closeWsolAtaIxs,
1607
- ];
1624
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
1625
+ const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
1608
1626
 
1609
- const res: LeverageIxsOutput = {
1610
- flashLoanInfo: {
1611
- flashBorrowReserve: debtReserve!.address,
1612
- flashLoanFee: debtReserve!.getFlashLoanFee(),
1613
- },
1614
- instructions: ixs,
1615
- };
1627
+ const ixs = [
1628
+ ...scopeRefreshIx,
1629
+ ...budgetIxs,
1630
+ ...createAtasIxs,
1631
+ ...fillWsolAtaIxs,
1632
+ ...[flashBorrowIx],
1633
+ ...KaminoAction.actionToIxs(repayAction),
1634
+ ...KaminoAction.actionToIxs(withdrawAction),
1635
+ ...swapInstructions,
1636
+ ...[flashRepayIx],
1637
+ ...closeWsolAtaIxs,
1638
+ ];
1616
1639
 
1617
- return res;
1640
+ const res: LeverageIxsOutput = {
1641
+ flashLoanInfo: {
1642
+ flashBorrowReserve: debtReserve!.address,
1643
+ flashLoanFee: debtReserve!.getFlashLoanFee(),
1644
+ },
1645
+ instructions: ixs,
1646
+ };
1647
+
1648
+ return res;
1649
+ });
1618
1650
  }
1619
1651
 
1620
1652
  export const getSetupIxs = async (