@kamino-finance/klend-sdk 5.11.12-beta.0 → 5.11.13

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 (68) hide show
  1. package/README_KAMINO_MANAGER.md +2 -2
  2. package/dist/classes/manager.d.ts +7 -0
  3. package/dist/classes/manager.d.ts.map +1 -1
  4. package/dist/classes/manager.js +9 -0
  5. package/dist/classes/manager.js.map +1 -1
  6. package/dist/classes/reserve.d.ts +1 -7
  7. package/dist/classes/reserve.d.ts.map +1 -1
  8. package/dist/classes/reserve.js +255 -145
  9. package/dist/classes/reserve.js.map +1 -1
  10. package/dist/classes/utils.d.ts +1 -1
  11. package/dist/classes/utils.d.ts.map +1 -1
  12. package/dist/classes/utils.js +2 -8
  13. package/dist/classes/utils.js.map +1 -1
  14. package/dist/classes/vault.d.ts +7 -0
  15. package/dist/classes/vault.d.ts.map +1 -1
  16. package/dist/classes/vault.js +21 -2
  17. package/dist/classes/vault.js.map +1 -1
  18. package/dist/client_kamino_manager.d.ts.map +1 -1
  19. package/dist/client_kamino_manager.js +130 -57
  20. package/dist/client_kamino_manager.js.map +1 -1
  21. package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts +9 -6
  22. package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts.map +1 -1
  23. package/dist/idl_codegen_kamino_vault/accounts/VaultState.js +19 -13
  24. package/dist/idl_codegen_kamino_vault/accounts/VaultState.js.map +1 -1
  25. package/dist/idl_codegen_kamino_vault/errors/custom.d.ts +9 -1
  26. package/dist/idl_codegen_kamino_vault/errors/custom.d.ts.map +1 -1
  27. package/dist/idl_codegen_kamino_vault/errors/custom.js +15 -1
  28. package/dist/idl_codegen_kamino_vault/errors/custom.js.map +1 -1
  29. package/dist/idl_codegen_kamino_vault/instructions/index.d.ts +2 -0
  30. package/dist/idl_codegen_kamino_vault/instructions/index.d.ts.map +1 -1
  31. package/dist/idl_codegen_kamino_vault/instructions/index.js +3 -1
  32. package/dist/idl_codegen_kamino_vault/instructions/index.js.map +1 -1
  33. package/dist/idl_codegen_kamino_vault/instructions/removeAllocation.d.ts +8 -0
  34. package/dist/idl_codegen_kamino_vault/instructions/removeAllocation.d.ts.map +1 -0
  35. package/dist/idl_codegen_kamino_vault/instructions/removeAllocation.js +17 -0
  36. package/dist/idl_codegen_kamino_vault/instructions/removeAllocation.js.map +1 -0
  37. package/dist/idl_codegen_kamino_vault/programId.d.ts.map +1 -1
  38. package/dist/idl_codegen_kamino_vault/programId.js +1 -1
  39. package/dist/idl_codegen_kamino_vault/programId.js.map +1 -1
  40. package/dist/lending_operations/repay_with_collateral_operations.d.ts +3 -1
  41. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  42. package/dist/lending_operations/repay_with_collateral_operations.js +13 -3
  43. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  44. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  45. package/dist/lending_operations/swap_collateral_operations.js +4 -0
  46. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  47. package/dist/leverage/operations.d.ts +5 -2
  48. package/dist/leverage/operations.d.ts.map +1 -1
  49. package/dist/leverage/operations.js +83 -44
  50. package/dist/leverage/operations.js.map +1 -1
  51. package/dist/leverage/types.d.ts +11 -0
  52. package/dist/leverage/types.d.ts.map +1 -1
  53. package/package.json +2 -2
  54. package/src/classes/manager.ts +13 -0
  55. package/src/classes/reserve.ts +319 -177
  56. package/src/classes/utils.ts +3 -9
  57. package/src/classes/vault.ts +33 -2
  58. package/src/client_kamino_manager.ts +164 -57
  59. package/src/idl_codegen_kamino_vault/accounts/VaultState.ts +25 -17
  60. package/src/idl_codegen_kamino_vault/errors/custom.ts +17 -0
  61. package/src/idl_codegen_kamino_vault/instructions/index.ts +2 -0
  62. package/src/idl_codegen_kamino_vault/instructions/removeAllocation.ts +26 -0
  63. package/src/idl_codegen_kamino_vault/programId.ts +1 -2
  64. package/src/idl_kamino_vault.json +38 -3
  65. package/src/lending_operations/repay_with_collateral_operations.ts +22 -6
  66. package/src/lending_operations/swap_collateral_operations.ts +12 -1
  67. package/src/leverage/operations.ts +101 -55
  68. package/src/leverage/types.ts +13 -0
@@ -730,6 +730,27 @@
730
730
  "type": "u64"
731
731
  }
732
732
  ]
733
+ },
734
+ {
735
+ "name": "removeAllocation",
736
+ "accounts": [
737
+ {
738
+ "name": "vaultAdminAuthority",
739
+ "isMut": true,
740
+ "isSigner": true
741
+ },
742
+ {
743
+ "name": "vaultState",
744
+ "isMut": true,
745
+ "isSigner": false
746
+ },
747
+ {
748
+ "name": "reserve",
749
+ "isMut": false,
750
+ "isSigner": false
751
+ }
752
+ ],
753
+ "args": []
733
754
  }
734
755
  ],
735
756
  "accounts": [
@@ -936,7 +957,16 @@
936
957
  {
937
958
  "defined": "VaultAllocation"
938
959
  },
939
- 10
960
+ 25
961
+ ]
962
+ }
963
+ },
964
+ {
965
+ "name": "padding1",
966
+ "type": {
967
+ "array": [
968
+ "u128",
969
+ 256
940
970
  ]
941
971
  }
942
972
  },
@@ -998,7 +1028,7 @@
998
1028
  "type": "u64"
999
1029
  },
1000
1030
  {
1001
- "name": "padding1",
1031
+ "name": "padding2",
1002
1032
  "type": "u64"
1003
1033
  },
1004
1034
  {
@@ -1006,7 +1036,7 @@
1006
1036
  "type": "publicKey"
1007
1037
  },
1008
1038
  {
1009
- "name": "padding2",
1039
+ "name": "padding3",
1010
1040
  "type": {
1011
1041
  "array": [
1012
1042
  "u128",
@@ -2187,6 +2217,11 @@
2187
2217
  "code": 7046,
2188
2218
  "name": "WrongAdminOrAllocationAdmin",
2189
2219
  "msg": "Wrong admin or allocation admin"
2220
+ },
2221
+ {
2222
+ "code": 7047,
2223
+ "name": "ReserveHasNonZeroAllocationOrCTokens",
2224
+ "msg": "Reserve has non-zero allocation or ctokens so cannot be removed"
2190
2225
  }
2191
2226
  ]
2192
2227
  }
@@ -7,6 +7,8 @@ import {
7
7
  SwapIxsProvider,
8
8
  SwapQuoteProvider,
9
9
  getScopeRefreshIx,
10
+ LeverageIxnsOutput,
11
+ FlashLoanInfo,
10
12
  } from '../leverage';
11
13
  import {
12
14
  createAtasIdempotent,
@@ -23,6 +25,7 @@ import { calcMaxWithdrawCollateral, calcRepayAmountWithSlippage } from './repay_
23
25
  export type RepayWithCollIxsResponse<QuoteResponse> = {
24
26
  ixs: TransactionInstruction[];
25
27
  lookupTables: AddressLookupTableAccount[];
28
+ flashLoanInfo: FlashLoanInfo;
26
29
  swapInputs: SwapInputs;
27
30
  initialInputs: RepayWithCollInitialInputs<QuoteResponse>;
28
31
  };
@@ -77,6 +80,7 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
77
80
  useV2Ixs,
78
81
  }: RepayWithCollSwapInputsProps<QuoteResponse>): Promise<{
79
82
  swapInputs: SwapInputs;
83
+ flashLoanInfo: FlashLoanInfo;
80
84
  initialInputs: RepayWithCollInitialInputs<QuoteResponse>;
81
85
  }> {
82
86
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
@@ -124,7 +128,7 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
124
128
  const inputAmountLamports = Decimal.min(withdrawableCollLamports, maxCollNeededFromOracle);
125
129
 
126
130
  // Build the repay & withdraw collateral tx to get the number of accounts
127
- const klendIxs = await buildRepayWithCollateralIxs(
131
+ const klendIxs: LeverageIxnsOutput = await buildRepayWithCollateralIxs(
128
132
  kaminoMarket,
129
133
  debtReserve,
130
134
  collReserve,
@@ -143,7 +147,7 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
143
147
  inputAmountLamports,
144
148
  useV2Ixs
145
149
  );
146
- const uniqueKlendAccounts = uniqueAccounts(klendIxs);
150
+ const uniqueKlendAccounts = uniqueAccounts(klendIxs.instructions);
147
151
 
148
152
  const swapQuoteInputs: SwapInputs = {
149
153
  inputAmountLamports,
@@ -169,6 +173,7 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
169
173
  outputMint: debtTokenMint,
170
174
  amountDebtAtaBalance: new Decimal(0), // only used for kTokens
171
175
  },
176
+ flashLoanInfo: klendIxs.flashLoanInfo,
172
177
  initialInputs: {
173
178
  debtRepayAmountLamports: repayAmountLamports,
174
179
  flashRepayAmountLamports,
@@ -251,7 +256,7 @@ export async function getRepayWithCollIxs<QuoteResponse>({
251
256
  );
252
257
 
253
258
  const swapResponse = await swapper(swapInputs, initialInputs.klendAccounts, swapQuote);
254
- const ixs = await buildRepayWithCollateralIxs(
259
+ const ixs: LeverageIxnsOutput = await buildRepayWithCollateralIxs(
255
260
  kaminoMarket,
256
261
  debtReserve,
257
262
  collReserve,
@@ -268,9 +273,10 @@ export async function getRepayWithCollIxs<QuoteResponse>({
268
273
  );
269
274
 
270
275
  return {
271
- ixs,
276
+ ixs: ixs.instructions,
272
277
  lookupTables: swapResponse.lookupTables,
273
278
  swapInputs,
279
+ flashLoanInfo: ixs.flashLoanInfo,
274
280
  initialInputs,
275
281
  };
276
282
  }
@@ -289,7 +295,7 @@ async function buildRepayWithCollateralIxs(
289
295
  debtRepayAmountLamports: Decimal,
290
296
  collWithdrawLamports: Decimal,
291
297
  useV2Ixs: boolean
292
- ): Promise<TransactionInstruction[]> {
298
+ ): Promise<LeverageIxnsOutput> {
293
299
  // 1. Create atas & budget txns
294
300
  const budgetIxns = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxns(1_400_000);
295
301
 
@@ -367,7 +373,7 @@ async function buildRepayWithCollateralIxs(
367
373
  const { preActionIxs, swapIxs } = swapQuoteIxs;
368
374
  const swapInstructions = removeBudgetAndAtaIxns(swapIxs, []);
369
375
 
370
- return [
376
+ const ixns = [
371
377
  ...scopeRefreshIxn,
372
378
  ...budgetIxns,
373
379
  ...atasAndIxs.map((x) => x.createAtaIx),
@@ -377,6 +383,16 @@ async function buildRepayWithCollateralIxs(
377
383
  ...swapInstructions,
378
384
  flashRepayIxn,
379
385
  ];
386
+
387
+ const res: LeverageIxnsOutput = {
388
+ flashLoanInfo: {
389
+ flashBorrowReserve: debtReserve.address,
390
+ flashLoanFee: debtReserve.getFlashLoanFee(),
391
+ },
392
+ instructions: ixns,
393
+ };
394
+
395
+ return res;
380
396
  }
381
397
 
382
398
  export const getMaxWithdrawLtvCheck = (obligation: KaminoObligation) => {
@@ -6,7 +6,13 @@ import {
6
6
  KaminoObligation,
7
7
  KaminoReserve,
8
8
  } from '../classes';
9
- import { getFlashLoanInstructions, getScopeRefreshIx, SwapIxsProvider, SwapQuoteProvider } from '../leverage';
9
+ import {
10
+ FlashLoanInfo,
11
+ getFlashLoanInstructions,
12
+ getScopeRefreshIx,
13
+ SwapIxsProvider,
14
+ SwapQuoteProvider,
15
+ } from '../leverage';
10
16
  import {
11
17
  createAtasIdempotent,
12
18
  DEFAULT_MAX_COMPUTE_UNITS,
@@ -251,6 +257,7 @@ type SwapCollKlendIxns = {
251
257
  withdrawSourceCollIxns: TransactionInstruction[];
252
258
  targetCollFlashRepayIxn: TransactionInstruction;
253
259
  cleanupIxns: TransactionInstruction[];
260
+ flashLoanInfo: FlashLoanInfo;
254
261
  simulationDetails: {
255
262
  targetCollFlashBorrowedAmount: Decimal;
256
263
  };
@@ -295,6 +302,10 @@ async function getKlendIxns(
295
302
 
296
303
  return {
297
304
  setupIxns,
305
+ flashLoanInfo: {
306
+ flashBorrowReserve: context.targetCollReserve.address,
307
+ flashLoanFee: context.targetCollReserve.getFlashLoanFee(),
308
+ },
298
309
  targetCollFlashBorrowIxn,
299
310
  depositTargetCollIxns: depositTargetCollIxns.ixns,
300
311
  withdrawSourceCollIxns,
@@ -67,6 +67,8 @@ import {
67
67
  WithdrawLeverageIxsResponse,
68
68
  WithdrawWithLeverageProps,
69
69
  WithdrawWithLeverageSwapInputsProps,
70
+ LeverageIxnsOutput,
71
+ FlashLoanInfo,
70
72
  } from './types';
71
73
 
72
74
  export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
@@ -93,6 +95,7 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
93
95
  useV2Ixs,
94
96
  elevationGroupOverride,
95
97
  }: DepositWithLeverageSwapInputsProps<QuoteResponse>): Promise<{
98
+ flashLoanInfo: FlashLoanInfo;
96
99
  swapInputs: SwapInputs;
97
100
  initialInputs: DepositLeverageInitialInputs<QuoteResponse>;
98
101
  }> {
@@ -137,7 +140,7 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
137
140
  }
138
141
 
139
142
  // Build the repay & withdraw collateral tx to get the number of accounts
140
- const klendIxs = await buildDepositWithLeverageIxns(
143
+ const klendIxs: LeverageIxnsOutput = await buildDepositWithLeverageIxns(
141
144
  kaminoMarket,
142
145
  debtReserve,
143
146
  collReserve,
@@ -160,7 +163,7 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
160
163
  elevationGroupOverride
161
164
  );
162
165
 
163
- const uniqueKlendAccounts = uniqueAccounts(klendIxs);
166
+ const uniqueKlendAccounts = uniqueAccounts(klendIxs.instructions);
164
167
 
165
168
  const swapInputAmount = toLamports(
166
169
  !collIsKtoken ? calcs.swapDebtTokenIn : calcs.singleSidedDepositKtokenOnly,
@@ -229,6 +232,7 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
229
232
  outputMint: collTokenMint,
230
233
  amountDebtAtaBalance: expectedDebtTokenAtaBalance,
231
234
  },
235
+ flashLoanInfo: klendIxs.flashLoanInfo,
232
236
  initialInputs: {
233
237
  calcs: quotePriceCalcs,
234
238
  swapQuote,
@@ -381,7 +385,7 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
381
385
  const solTokenReserve = kaminoMarket.getReserveByMint(NATIVE_MINT);
382
386
  const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve!.getLiquidityMint());
383
387
 
384
- const ixs = await buildDepositWithLeverageIxns(
388
+ const ixs: LeverageIxnsOutput = await buildDepositWithLeverageIxns(
385
389
  kaminoMarket,
386
390
  debtReserve!,
387
391
  collReserve!,
@@ -405,7 +409,8 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
405
409
  );
406
410
 
407
411
  return {
408
- ixs,
412
+ ixs: ixs.instructions,
413
+ flashLoanInfo: ixs.flashLoanInfo,
409
414
  lookupTables,
410
415
  swapInputs,
411
416
  initialInputs,
@@ -429,7 +434,7 @@ async function buildDepositWithLeverageIxns(
429
434
  collIsKtoken: boolean,
430
435
  useV2Ixs: boolean,
431
436
  elevationGroupOverride?: number
432
- ): Promise<TransactionInstruction[]> {
437
+ ): Promise<LeverageIxnsOutput> {
433
438
  const budgetIxns = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxns(3000000);
434
439
  const collTokenMint = collReserve.getLiquidityMint();
435
440
  const debtTokenMint = debtReserve.getLiquidityMint();
@@ -556,31 +561,42 @@ async function buildDepositWithLeverageIxns(
556
561
  // 4. Swap
557
562
  const { swapIxs } = swapQuoteIxs;
558
563
  const swapInstructions = removeBudgetAndAtaIxns(swapIxs, []);
564
+ const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
565
+ const flashLoanInfo = {
566
+ flashBorrowReserve: flashBorrowReserve.address,
567
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
568
+ };
559
569
 
560
570
  if (!collIsKtoken) {
561
- return [
562
- ...scopeRefreshIxn,
563
- ...budgetIxns,
564
- ...atasAndCreateIxns.map((x) => x.createAtaIx),
565
- ...fillWsolAtaIxns,
566
- ...[flashBorrowIxn],
567
- ...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
568
- ...swapInstructions,
569
- ...[flashRepayIxn],
570
- ];
571
+ return {
572
+ flashLoanInfo,
573
+ instructions: [
574
+ ...scopeRefreshIxn,
575
+ ...budgetIxns,
576
+ ...atasAndCreateIxns.map((x) => x.createAtaIx),
577
+ ...fillWsolAtaIxns,
578
+ ...[flashBorrowIxn],
579
+ ...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
580
+ ...swapInstructions,
581
+ ...[flashRepayIxn],
582
+ ],
583
+ };
571
584
  } else {
572
- return [
573
- ...scopeRefreshIxn,
574
- ...budgetIxns,
575
- ...atasAndCreateIxns.map((x) => x.createAtaIx),
576
- ...fillWsolAtaIxns,
577
- ...[flashBorrowIxn],
578
- ...swapInstructions,
579
- ...kaminoDepositAndBorrowAction.setupIxs,
580
- ...KaminoAction.actionToLendingIxs(kaminoDepositAndBorrowAction),
581
- ...kaminoDepositAndBorrowAction.cleanupIxs,
582
- ...[flashRepayIxn],
583
- ];
585
+ return {
586
+ flashLoanInfo,
587
+ instructions: [
588
+ ...scopeRefreshIxn,
589
+ ...budgetIxns,
590
+ ...atasAndCreateIxns.map((x) => x.createAtaIx),
591
+ ...fillWsolAtaIxns,
592
+ ...[flashBorrowIxn],
593
+ ...swapInstructions,
594
+ ...kaminoDepositAndBorrowAction.setupIxs,
595
+ ...KaminoAction.actionToLendingIxs(kaminoDepositAndBorrowAction),
596
+ ...kaminoDepositAndBorrowAction.cleanupIxs,
597
+ ...[flashRepayIxn],
598
+ ],
599
+ };
584
600
  }
585
601
  }
586
602
 
@@ -608,6 +624,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
608
624
  useV2Ixs,
609
625
  }: WithdrawWithLeverageSwapInputsProps<QuoteResponse>): Promise<{
610
626
  swapInputs: SwapInputs;
627
+ flashLoanInfo: FlashLoanInfo;
611
628
  initialInputs: WithdrawLeverageInitialInputs<QuoteResponse>;
612
629
  }> {
613
630
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
@@ -660,7 +677,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
660
677
  useV2Ixs
661
678
  );
662
679
 
663
- const uniqueKlendAccounts = uniqueAccounts(klendIxs);
680
+ const uniqueKlendAccounts = uniqueAccounts(klendIxs.instructions);
664
681
 
665
682
  const swapInputAmount = toLamports(
666
683
  calcs.collTokenSwapIn,
@@ -706,6 +723,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
706
723
  outputMint: debtTokenMint,
707
724
  amountDebtAtaBalance: new Decimal(0), // Only needed for ktokens deposits
708
725
  },
726
+ flashLoanInfo: klendIxs.flashLoanInfo,
709
727
  initialInputs: {
710
728
  calcs: calcsQuotePrice,
711
729
  swapQuote,
@@ -800,7 +818,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
800
818
  }
801
819
  }
802
820
 
803
- const ixs = await buildWithdrawWithLeverageIxns(
821
+ const ixs: LeverageIxnsOutput = await buildWithdrawWithLeverageIxns(
804
822
  kaminoMarket,
805
823
  debtReserve!,
806
824
  collReserve!,
@@ -825,7 +843,8 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
825
843
 
826
844
  // Send ixns and lookup tables
827
845
  return {
828
- ixs,
846
+ ixs: ixs.instructions,
847
+ flashLoanInfo: ixs.flashLoanInfo,
829
848
  lookupTables,
830
849
  swapInputs,
831
850
  initialInputs: initialInputs,
@@ -849,7 +868,7 @@ export async function buildWithdrawWithLeverageIxns(
849
868
  strategy: StrategyWithAddress | undefined,
850
869
  collIsKtoken: boolean,
851
870
  useV2Ixs: boolean
852
- ): Promise<TransactionInstruction[]> {
871
+ ): Promise<LeverageIxnsOutput> {
853
872
  const collTokenMint = collReserve.getLiquidityMint();
854
873
  const debtTokenMint = debtReserve.getLiquidityMint();
855
874
  const debtTokenAta = getAssociatedTokenAddressSync(
@@ -975,17 +994,23 @@ export async function buildWithdrawWithLeverageIxns(
975
994
 
976
995
  const swapInstructions = removeBudgetAndAtaIxns(swapQuoteIxs.swapIxs, []);
977
996
 
978
- return [
979
- ...scopeRefreshIxn,
980
- ...budgetIxns,
981
- ...atasAndCreateIxns.map((x) => x.createAtaIx),
982
- ...fillWsolAtaIxns,
983
- ...[flashBorrowIxn],
984
- ...KaminoAction.actionToIxs(repayAndWithdrawAction),
985
- ...swapInstructions,
986
- ...[flashRepayIxn],
987
- ...closeWsolAtaIxns,
988
- ];
997
+ return {
998
+ flashLoanInfo: {
999
+ flashLoanFee: debtReserve.getFlashLoanFee(),
1000
+ flashBorrowReserve: debtReserve.address,
1001
+ },
1002
+ instructions: [
1003
+ ...scopeRefreshIxn,
1004
+ ...budgetIxns,
1005
+ ...atasAndCreateIxns.map((x) => x.createAtaIx),
1006
+ ...fillWsolAtaIxns,
1007
+ ...[flashBorrowIxn],
1008
+ ...KaminoAction.actionToIxs(repayAndWithdrawAction),
1009
+ ...swapInstructions,
1010
+ ...[flashRepayIxn],
1011
+ ...closeWsolAtaIxns,
1012
+ ],
1013
+ };
989
1014
  }
990
1015
 
991
1016
  export async function getAdjustLeverageSwapInputs<QuoteResponse>({
@@ -1011,6 +1036,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1011
1036
  useV2Ixs,
1012
1037
  }: AdjustLeverageSwapInputsProps<QuoteResponse>): Promise<{
1013
1038
  swapInputs: SwapInputs;
1039
+ flashLoanInfo: FlashLoanInfo;
1014
1040
  initialInputs: AdjustLeverageInitialInputs<QuoteResponse>;
1015
1041
  }> {
1016
1042
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint)!;
@@ -1057,7 +1083,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1057
1083
  );
1058
1084
 
1059
1085
  // Build the repay & withdraw collateral tx to get the number of accounts
1060
- const klendIxs = await buildIncreaseLeverageIxns(
1086
+ const klendIxs: LeverageIxnsOutput = await buildIncreaseLeverageIxns(
1061
1087
  owner,
1062
1088
  kaminoMarket,
1063
1089
  collTokenMint,
@@ -1078,7 +1104,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1078
1104
  useV2Ixs
1079
1105
  );
1080
1106
 
1081
- const uniqueKlendAccounts = uniqueAccounts(klendIxs);
1107
+ const uniqueKlendAccounts = uniqueAccounts(klendIxs.instructions);
1082
1108
 
1083
1109
  const swapInputAmount = toLamports(
1084
1110
  !collIsKtoken ? calcs.borrowAmount : calcs.amountToFlashBorrowDebt,
@@ -1147,6 +1173,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1147
1173
  outputMint: collTokenMint,
1148
1174
  amountDebtAtaBalance: expectedDebtTokenAtaBalance,
1149
1175
  },
1176
+ flashLoanInfo: klendIxs.flashLoanInfo,
1150
1177
  initialInputs: {
1151
1178
  calcs: calcsQuotePrice,
1152
1179
  swapQuote,
@@ -1161,7 +1188,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1161
1188
  } else {
1162
1189
  const calcs = adjustWithdrawLeverageCalcs(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
1163
1190
 
1164
- const klendIxs = await buildDecreaseLeverageIxns(
1191
+ const klendIxs: LeverageIxnsOutput = await buildDecreaseLeverageIxns(
1165
1192
  owner,
1166
1193
  kaminoMarket,
1167
1194
  collTokenMint,
@@ -1182,7 +1209,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1182
1209
  useV2Ixs
1183
1210
  );
1184
1211
 
1185
- const uniqueKlendAccounts = uniqueAccounts(klendIxs);
1212
+ const uniqueKlendAccounts = uniqueAccounts(klendIxs.instructions);
1186
1213
 
1187
1214
  const swapInputAmount = toLamports(
1188
1215
  calcs.withdrawAmountWithSlippageAndFlashLoanFee,
@@ -1229,6 +1256,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1229
1256
  outputMint: debtTokenMint,
1230
1257
  amountDebtAtaBalance: new Decimal(0), // Only needed for ktokens deposits
1231
1258
  },
1259
+ flashLoanInfo: klendIxs.flashLoanInfo,
1232
1260
  initialInputs: {
1233
1261
  calcs: calcsQuotePrice,
1234
1262
  swapQuote,
@@ -1318,8 +1346,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
1318
1346
  initialInputs.swapQuote
1319
1347
  );
1320
1348
 
1321
- // TODO: marius why are we not using both adjustDepositPosition & adjustBorrowPosition
1322
- const ixs = await buildIncreaseLeverageIxns(
1349
+ const ixs: LeverageIxnsOutput = await buildIncreaseLeverageIxns(
1323
1350
  owner,
1324
1351
  kaminoMarket,
1325
1352
  collTokenMint,
@@ -1340,7 +1367,8 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
1340
1367
  useV2Ixs
1341
1368
  );
1342
1369
  return {
1343
- ixs,
1370
+ ixs: ixs.instructions,
1371
+ flashLoanInfo: ixs.flashLoanInfo,
1344
1372
  lookupTables,
1345
1373
  swapInputs,
1346
1374
  initialInputs,
@@ -1366,7 +1394,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
1366
1394
  initialInputs.swapQuote
1367
1395
  );
1368
1396
 
1369
- const ixs = await buildDecreaseLeverageIxns(
1397
+ const ixs: LeverageIxnsOutput = await buildDecreaseLeverageIxns(
1370
1398
  owner,
1371
1399
  kaminoMarket,
1372
1400
  collTokenMint,
@@ -1388,7 +1416,8 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
1388
1416
  );
1389
1417
 
1390
1418
  return {
1391
- ixs,
1419
+ ixs: ixs.instructions,
1420
+ flashLoanInfo: ixs.flashLoanInfo,
1392
1421
  lookupTables,
1393
1422
  swapInputs,
1394
1423
  initialInputs,
@@ -1414,7 +1443,7 @@ async function buildIncreaseLeverageIxns(
1414
1443
  swapQuoteIxs: SwapIxs,
1415
1444
  budgetAndPriorityFeeIxns: TransactionInstruction[] | undefined,
1416
1445
  useV2Ixs: boolean
1417
- ): Promise<TransactionInstruction[]> {
1446
+ ): Promise<LeverageIxnsOutput> {
1418
1447
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
1419
1448
  const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
1420
1449
  const debtTokenAta = getAssociatedTokenAddressSync(
@@ -1566,7 +1595,16 @@ async function buildIncreaseLeverageIxns(
1566
1595
  ...[flashRepayIxn],
1567
1596
  ];
1568
1597
 
1569
- return ixs;
1598
+ const flashBorrowReserve = !collIsKtoken ? collReserve! : debtReserve!;
1599
+ const res: LeverageIxnsOutput = {
1600
+ flashLoanInfo: {
1601
+ flashBorrowReserve: flashBorrowReserve.address,
1602
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
1603
+ },
1604
+ instructions: ixs,
1605
+ };
1606
+
1607
+ return res;
1570
1608
  }
1571
1609
 
1572
1610
  /**
@@ -1587,7 +1625,7 @@ async function buildDecreaseLeverageIxns(
1587
1625
  swapQuoteIxs: SwapIxs,
1588
1626
  budgetAndPriorityFeeIxns: TransactionInstruction[] | undefined,
1589
1627
  useV2Ixs: boolean
1590
- ): Promise<TransactionInstruction[]> {
1628
+ ): Promise<LeverageIxnsOutput> {
1591
1629
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
1592
1630
  const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
1593
1631
  const debtTokenAta = getAssociatedTokenAddressSync(
@@ -1739,7 +1777,15 @@ async function buildDecreaseLeverageIxns(
1739
1777
  ...closeWsolAtaIxns,
1740
1778
  ];
1741
1779
 
1742
- return ixns;
1780
+ const res: LeverageIxnsOutput = {
1781
+ flashLoanInfo: {
1782
+ flashBorrowReserve: debtReserve!.address,
1783
+ flashLoanFee: debtReserve!.getFlashLoanFee(),
1784
+ },
1785
+ instructions: ixns,
1786
+ };
1787
+
1788
+ return res;
1743
1789
  }
1744
1790
 
1745
1791
  export const getScopeRefreshIx = async (
@@ -30,6 +30,16 @@ export type PriceAinBProvider = (mintA: PublicKey, mintB: PublicKey) => Promise<
30
30
 
31
31
  export type IsKtokenProvider = (token: PublicKey | string) => Promise<boolean>;
32
32
 
33
+ export type FlashLoanInfo = {
34
+ flashBorrowReserve: PublicKey;
35
+ flashLoanFee: Decimal;
36
+ };
37
+
38
+ export type LeverageIxnsOutput = {
39
+ instructions: TransactionInstruction[];
40
+ flashLoanInfo: FlashLoanInfo;
41
+ };
42
+
33
43
  export type SwapInputs = {
34
44
  inputAmountLamports: Decimal;
35
45
  minOutAmountLamports?: Decimal;
@@ -53,6 +63,7 @@ export type DepositLeverageIxsResponse<QuoteResponse> = {
53
63
  ixs: TransactionInstruction[];
54
64
  lookupTables: AddressLookupTableAccount[];
55
65
  swapInputs: SwapInputs;
66
+ flashLoanInfo: FlashLoanInfo;
56
67
  initialInputs: DepositLeverageInitialInputs<QuoteResponse>;
57
68
  };
58
69
 
@@ -113,6 +124,7 @@ export type WithdrawLeverageIxsResponse<QuoteResponse> = {
113
124
  ixs: TransactionInstruction[];
114
125
  lookupTables: AddressLookupTableAccount[];
115
126
  swapInputs: SwapInputs;
127
+ flashLoanInfo: FlashLoanInfo;
116
128
  initialInputs: WithdrawLeverageInitialInputs<QuoteResponse>;
117
129
  };
118
130
 
@@ -166,6 +178,7 @@ export type AdjustLeverageIxsResponse<QuoteResponse> = {
166
178
  ixs: TransactionInstruction[];
167
179
  lookupTables: AddressLookupTableAccount[];
168
180
  swapInputs: SwapInputs;
181
+ flashLoanInfo: FlashLoanInfo;
169
182
  initialInputs: AdjustLeverageInitialInputs<QuoteResponse>;
170
183
  };
171
184