@kamino-finance/klend-sdk 5.11.3-beta.0 → 5.11.3-beta.1

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 (87) hide show
  1. package/dist/classes/action.d.ts +23 -23
  2. package/dist/classes/action.d.ts.map +1 -1
  3. package/dist/classes/action.js +149 -69
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/manager.d.ts +1 -1
  6. package/dist/classes/manager.js +1 -1
  7. package/dist/classes/market.d.ts +3 -3
  8. package/dist/classes/market.d.ts.map +1 -1
  9. package/dist/classes/market.js +16 -30
  10. package/dist/classes/market.js.map +1 -1
  11. package/dist/classes/obligation.d.ts +0 -2
  12. package/dist/classes/obligation.d.ts.map +1 -1
  13. package/dist/classes/obligation.js +0 -5
  14. package/dist/classes/obligation.js.map +1 -1
  15. package/dist/classes/vault.js +14 -14
  16. package/dist/classes/vault.js.map +1 -1
  17. package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts +3 -3
  18. package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts.map +1 -1
  19. package/dist/idl_codegen_kamino_vault/accounts/VaultState.js +6 -6
  20. package/dist/idl_codegen_kamino_vault/accounts/VaultState.js.map +1 -1
  21. package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.d.ts +1 -1
  22. package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.d.ts.map +1 -1
  23. package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.js +1 -1
  24. package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.js.map +1 -1
  25. package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.d.ts +1 -1
  26. package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.d.ts.map +1 -1
  27. package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.js +1 -1
  28. package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.js.map +1 -1
  29. package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.d.ts +1 -1
  30. package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.d.ts.map +1 -1
  31. package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.js +1 -1
  32. package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.js.map +1 -1
  33. package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.d.ts +1 -1
  34. package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.d.ts.map +1 -1
  35. package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.js +1 -1
  36. package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.js.map +1 -1
  37. package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.d.ts +1 -1
  38. package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.d.ts.map +1 -1
  39. package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.js +1 -1
  40. package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.js.map +1 -1
  41. package/dist/lending_operations/repay_with_collateral_operations.d.ts +4 -4
  42. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  43. package/dist/lending_operations/repay_with_collateral_operations.js +8 -10
  44. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  45. package/dist/lending_operations/swap_collateral_operations.d.ts +2 -2
  46. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  47. package/dist/lending_operations/swap_collateral_operations.js +6 -11
  48. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  49. package/dist/leverage/calcs.d.ts +10 -5
  50. package/dist/leverage/calcs.d.ts.map +1 -1
  51. package/dist/leverage/calcs.js +13 -6
  52. package/dist/leverage/calcs.js.map +1 -1
  53. package/dist/leverage/operations.d.ts +7 -9
  54. package/dist/leverage/operations.d.ts.map +1 -1
  55. package/dist/leverage/operations.js +72 -78
  56. package/dist/leverage/operations.js.map +1 -1
  57. package/dist/leverage/types.d.ts +4 -4
  58. package/dist/leverage/types.d.ts.map +1 -1
  59. package/dist/utils/ObligationType.d.ts +1 -1
  60. package/dist/utils/ObligationType.d.ts.map +1 -1
  61. package/dist/utils/managerTypes.d.ts.map +1 -1
  62. package/dist/utils/managerTypes.js +52 -7
  63. package/dist/utils/managerTypes.js.map +1 -1
  64. package/dist/utils/oracle.d.ts +3 -3
  65. package/dist/utils/oracle.d.ts.map +1 -1
  66. package/package.json +2 -2
  67. package/src/classes/action.ts +162 -75
  68. package/src/classes/manager.ts +1 -1
  69. package/src/classes/market.ts +25 -34
  70. package/src/classes/obligation.ts +0 -6
  71. package/src/classes/vault.ts +14 -14
  72. package/src/client.ts +3 -8
  73. package/src/idl_codegen_kamino_vault/accounts/VaultState.ts +8 -8
  74. package/src/idl_codegen_kamino_vault/instructions/giveUpPendingFees.ts +2 -2
  75. package/src/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.ts +2 -2
  76. package/src/idl_codegen_kamino_vault/instructions/updateSharesMetadata.ts +2 -2
  77. package/src/idl_codegen_kamino_vault/instructions/updateVaultConfig.ts +2 -2
  78. package/src/idl_codegen_kamino_vault/instructions/withdrawPendingFees.ts +2 -2
  79. package/src/idl_kamino_vault.json +7 -7
  80. package/src/lending_operations/repay_with_collateral_operations.ts +11 -15
  81. package/src/lending_operations/swap_collateral_operations.ts +7 -19
  82. package/src/leverage/calcs.ts +18 -2
  83. package/src/leverage/operations.ts +72 -114
  84. package/src/leverage/types.ts +4 -4
  85. package/src/utils/ObligationType.ts +1 -1
  86. package/src/utils/managerTypes.ts +52 -10
  87. package/src/utils/oracle.ts +2 -2
@@ -35,6 +35,7 @@ export interface LeverageFormsCalcsArgs {
35
35
  targetLeverage: Decimal;
36
36
  activeTab: FormTabs;
37
37
  flashBorrowReserveFlashLoanFeePercentage: Decimal;
38
+ borrowFee: Decimal;
38
39
  debtBorrowFactorPct: Decimal;
39
40
  priceCollToDebt: Decimal;
40
41
  priceDebtToColl: Decimal;
@@ -62,6 +63,7 @@ export async function calculateMultiplyEffects(
62
63
  targetLeverage,
63
64
  activeTab,
64
65
  flashBorrowReserveFlashLoanFeePercentage,
66
+ borrowFee,
65
67
  debtBorrowFactorPct,
66
68
  priceCollToDebt,
67
69
  priceDebtToColl,
@@ -80,6 +82,7 @@ export async function calculateMultiplyEffects(
80
82
  selectedTokenMint,
81
83
  collTokenMint: collTokenMint,
82
84
  flashLoanFee: flashBorrowReserveFlashLoanFeePercentage,
85
+ borrowFee,
83
86
  });
84
87
 
85
88
  // calculate estimations for withdraw operation
@@ -106,6 +109,7 @@ export async function calculateMultiplyEffects(
106
109
  totalDeposited: new Decimal(deposited),
107
110
  totalBorrowed: new Decimal(borrowed),
108
111
  flashLoanFee: flashBorrowReserveFlashLoanFeePercentage, // TODO: is this the right flash borrow?
112
+ borrowFee,
109
113
  });
110
114
 
111
115
  if (logEstimations) {
@@ -191,11 +195,13 @@ export const calcBorrowAmount = ({
191
195
  targetLeverage,
192
196
  priceCollToDebt,
193
197
  flashBorrowFee,
198
+ borrowFee,
194
199
  }: {
195
200
  depositTokenAmount: Decimal;
196
201
  targetLeverage: Decimal;
197
202
  priceCollToDebt: Decimal;
198
203
  flashBorrowFee: Decimal;
204
+ borrowFee: Decimal;
199
205
  }) => {
200
206
  const initialCollAmountInCollToken = depositTokenAmount;
201
207
 
@@ -204,7 +210,8 @@ export const calcBorrowAmount = ({
204
210
  const finalDebtAmountInDebtToken = finalDebtAmountInCollToken.mul(priceCollToDebt);
205
211
 
206
212
  const flashFeeFactor = new Decimal(1).add(flashBorrowFee);
207
- const debtTokenToBorrow = finalDebtAmountInDebtToken.mul(flashFeeFactor);
213
+ const borrowFeeFactor = new Decimal(1).add(borrowFee);
214
+ const debtTokenToBorrow = finalDebtAmountInDebtToken.mul(flashFeeFactor).mul(borrowFeeFactor);
208
215
 
209
216
  return debtTokenToBorrow;
210
217
  };
@@ -274,6 +281,7 @@ export function calcWithdrawAmounts(params: WithdrawParams): WithdrawResult {
274
281
  priceCollToDebt: new Decimal(priceCollToDebt),
275
282
  targetLeverage: new Decimal(targetLeverage),
276
283
  flashBorrowFee: new Decimal(0),
284
+ borrowFee: new Decimal(0),
277
285
  });
278
286
 
279
287
  const adjustDepositPosition = currentDepositPosition.minus(targetDeposit);
@@ -293,6 +301,7 @@ interface UseEstimateAdjustAmountsProps {
293
301
  totalDeposited: Decimal;
294
302
  totalBorrowed: Decimal;
295
303
  flashLoanFee: Decimal;
304
+ borrowFee: Decimal;
296
305
  }
297
306
 
298
307
  /**
@@ -303,7 +312,7 @@ interface UseEstimateAdjustAmountsProps {
303
312
  */
304
313
  export const estimateAdjustMode = (
305
314
  priceCollToDebt: Decimal,
306
- { targetLeverage, totalDeposited, totalBorrowed, flashLoanFee }: UseEstimateAdjustAmountsProps
315
+ { targetLeverage, totalDeposited, totalBorrowed, flashLoanFee, borrowFee }: UseEstimateAdjustAmountsProps
307
316
  ) => {
308
317
  return calcAdjustAmounts({
309
318
  currentBorrowPosition: totalBorrowed,
@@ -311,6 +320,7 @@ export const estimateAdjustMode = (
311
320
  priceCollToDebt,
312
321
  targetLeverage,
313
322
  flashLoanFee,
323
+ borrowFee,
314
324
  });
315
325
  };
316
326
 
@@ -320,6 +330,7 @@ export interface AdjustLeverageParams {
320
330
  currentDepositPosition: Decimal;
321
331
  priceCollToDebt: Decimal;
322
332
  flashLoanFee: Decimal;
333
+ borrowFee: Decimal;
323
334
  }
324
335
 
325
336
  interface AdjustLeverageResult {
@@ -343,6 +354,7 @@ export function calcAdjustAmounts({
343
354
  currentDepositPosition,
344
355
  priceCollToDebt,
345
356
  flashLoanFee,
357
+ borrowFee,
346
358
  }: AdjustLeverageParams): AdjustLeverageResult {
347
359
  const initialDeposit = currentDepositPosition.minus(currentBorrowPosition.div(priceCollToDebt));
348
360
  const targetDeposit = initialDeposit.mul(targetLeverage);
@@ -352,6 +364,7 @@ export function calcAdjustAmounts({
352
364
  priceCollToDebt: new Decimal(priceCollToDebt),
353
365
  targetLeverage: new Decimal(targetLeverage),
354
366
  flashBorrowFee: flashLoanFee,
367
+ borrowFee,
355
368
  });
356
369
 
357
370
  const adjustDepositPosition = targetDeposit.minus(currentDepositPosition);
@@ -371,6 +384,7 @@ interface UseTransactionInfoStats {
371
384
  selectedTokenMint: PublicKey;
372
385
  collTokenMint: PublicKey;
373
386
  flashLoanFee: Decimal;
387
+ borrowFee: Decimal;
374
388
  slippagePct?: Decimal;
375
389
  }
376
390
 
@@ -384,6 +398,7 @@ export const estimateDepositMode = ({
384
398
  selectedTokenMint,
385
399
  collTokenMint,
386
400
  flashLoanFee,
401
+ borrowFee,
387
402
  slippagePct = new Decimal(0),
388
403
  }: UseTransactionInfoStats) => {
389
404
  const isDepositingCollToken = selectedTokenMint.equals(collTokenMint);
@@ -398,6 +413,7 @@ export const estimateDepositMode = ({
398
413
  targetLeverage: new Decimal(targetLeverage),
399
414
  priceCollToDebt: new Decimal(priceCollToDebt),
400
415
  flashBorrowFee: new Decimal(flashLoanFee),
416
+ borrowFee: new Decimal(borrowFee),
401
417
  });
402
418
 
403
419
  const slippageFactor = new Decimal(1).add(slippagePct.div(new Decimal(100)));
@@ -6,8 +6,6 @@ import {
6
6
  KaminoObligation,
7
7
  KaminoReserve,
8
8
  lamportsToNumberDecimal as fromLamports,
9
- getTokenIdsForScopeRefresh,
10
- isKaminoObligation,
11
9
  } from '../classes';
12
10
  import { getFlashLoanInstructions } from './instructions';
13
11
 
@@ -17,9 +15,7 @@ import {
17
15
  MultiplyObligation,
18
16
  ObligationType,
19
17
  ObligationTypeTag,
20
- PublicKeySet,
21
18
  SOL_DECIMALS,
22
- ScopePriceRefreshConfig,
23
19
  U64_MAX,
24
20
  createAtasIdempotent,
25
21
  getAssociatedTokenAddress,
@@ -84,7 +80,7 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
84
80
  selectedTokenMint,
85
81
  kamino,
86
82
  obligationTypeTagOverride,
87
- scopeRefreshConfig,
83
+ scopeFeed,
88
84
  budgetAndPriorityFeeIxs,
89
85
  quoteBufferBps,
90
86
  priceAinB,
@@ -146,7 +142,7 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
146
142
  referrer,
147
143
  currentSlot,
148
144
  depositTokenIsSol,
149
- scopeRefreshConfig,
145
+ scopeFeed,
150
146
  calcs,
151
147
  budgetAndPriorityFeeIxs,
152
148
  {
@@ -313,7 +309,7 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
313
309
  selectedTokenMint,
314
310
  kamino,
315
311
  obligationTypeTagOverride,
316
- scopeRefreshConfig,
312
+ scopeFeed,
317
313
  budgetAndPriorityFeeIxs,
318
314
  quoteBufferBps,
319
315
  priceAinB,
@@ -338,7 +334,7 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
338
334
  selectedTokenMint,
339
335
  kamino,
340
336
  obligationTypeTagOverride,
341
- scopeRefreshConfig,
337
+ scopeFeed,
342
338
  budgetAndPriorityFeeIxs,
343
339
  quoteBufferBps,
344
340
  priceAinB,
@@ -390,7 +386,7 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
390
386
  referrer,
391
387
  currentSlot,
392
388
  depositTokenIsSol,
393
- scopeRefreshConfig,
389
+ scopeFeed,
394
390
  initialInputs.calcs,
395
391
  budgetAndPriorityFeeIxs,
396
392
  {
@@ -421,7 +417,7 @@ async function buildDepositWithLeverageIxns(
421
417
  referrer: PublicKey,
422
418
  currentSlot: number,
423
419
  depositTokenIsSol: boolean,
424
- scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
420
+ scopeFeed: string | undefined,
425
421
  calcs: DepositLeverageCalcsResult,
426
422
  budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
427
423
  swapQuoteIxs: SwapIxs,
@@ -446,7 +442,7 @@ async function buildDepositWithLeverageIxns(
446
442
  debtReserve.getLiquidityTokenProgram()
447
443
  );
448
444
 
449
- // 1. Create atas & budget ixns
445
+ // 1. Create atas & budget txns
450
446
  let mintsToCreateAtas: Array<{ mint: PublicKey; tokenProgram: PublicKey }>;
451
447
  if (collIsKtoken) {
452
448
  const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
@@ -507,13 +503,10 @@ async function buildDepositWithLeverageIxns(
507
503
  );
508
504
  }
509
505
 
510
- const scopeRefreshIxn = await getScopeRefreshIx(market, collReserve, debtReserve, obligation, scopeRefreshConfig);
511
-
512
506
  // 2. Flash borrow & repay the collateral amount needed for given leverage
513
507
  // if user deposits coll, then we borrow the diff, else we borrow the entire amount
514
508
  const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
515
- borrowIxnIndex:
516
- budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
509
+ borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length,
517
510
  walletPublicKey: owner,
518
511
  lendingMarketAuthority: market.getLendingMarketAuthority(),
519
512
  lendingMarketAddress: market.getAddress(),
@@ -530,6 +523,7 @@ async function buildDepositWithLeverageIxns(
530
523
  });
531
524
 
532
525
  // 3. Deposit initial tokens + borrowed tokens into reserve
526
+ const scopeRefresh = scopeFeed ? { includeScopeRefresh: true, scopeFeed: scopeFeed } : undefined;
533
527
  const kaminoDepositAndBorrowAction = await KaminoAction.buildDepositAndBorrowTxns(
534
528
  market,
535
529
  toLamports(!collIsKtoken ? calcs.collTokenToDeposit : calcs.collTokenToDeposit, collReserve.stats.decimals)
@@ -543,14 +537,14 @@ async function buildDepositWithLeverageIxns(
543
537
  owner,
544
538
  obligation!,
545
539
  useV2Ixs,
546
- undefined,
547
540
  0,
548
541
  false,
549
542
  elevationGroupOverride === 0 ? false : true, // emode
550
543
  false, // to be checked and created in a setup tx in the UI
551
544
  false, // to be checked and created in a setup tx in the UI
552
545
  referrer,
553
- currentSlot
546
+ currentSlot,
547
+ scopeRefresh
554
548
  );
555
549
 
556
550
  // 4. Swap
@@ -559,18 +553,18 @@ async function buildDepositWithLeverageIxns(
559
553
 
560
554
  if (!collIsKtoken) {
561
555
  return [
562
- ...scopeRefreshIxn,
563
556
  ...budgetIxns,
564
557
  ...atasAndCreateIxns.map((x) => x.createAtaIx),
565
558
  ...fillWsolAtaIxns,
566
559
  ...[flashBorrowIxn],
567
- ...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
560
+ ...kaminoDepositAndBorrowAction.setupIxs,
561
+ ...KaminoAction.actionToLendingIxs(kaminoDepositAndBorrowAction),
562
+ ...kaminoDepositAndBorrowAction.cleanupIxs,
568
563
  ...swapInstructions,
569
564
  ...[flashRepayIxn],
570
565
  ];
571
566
  } else {
572
567
  return [
573
- ...scopeRefreshIxn,
574
568
  ...budgetIxns,
575
569
  ...atasAndCreateIxns.map((x) => x.createAtaIx),
576
570
  ...fillWsolAtaIxns,
@@ -601,7 +595,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
601
595
  selectedTokenMint,
602
596
  budgetAndPriorityFeeIxs,
603
597
  kamino,
604
- scopeRefreshConfig,
598
+ scopeFeed,
605
599
  quoteBufferBps,
606
600
  isKtoken,
607
601
  quoter,
@@ -647,7 +641,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
647
641
  currentSlot,
648
642
  isClosingPosition,
649
643
  depositTokenIsSol,
650
- scopeRefreshConfig,
644
+ scopeFeed,
651
645
  calcs,
652
646
  budgetAndPriorityFeeIxs,
653
647
  {
@@ -735,7 +729,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
735
729
  selectedTokenMint,
736
730
  budgetAndPriorityFeeIxs,
737
731
  kamino,
738
- scopeRefreshConfig,
732
+ scopeFeed,
739
733
  quoteBufferBps,
740
734
  isKtoken,
741
735
  quoter,
@@ -764,7 +758,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
764
758
  selectedTokenMint,
765
759
  budgetAndPriorityFeeIxs,
766
760
  kamino,
767
- scopeRefreshConfig,
761
+ scopeFeed,
768
762
  quoteBufferBps,
769
763
  isKtoken,
770
764
  quoter,
@@ -810,7 +804,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
810
804
  currentSlot,
811
805
  isClosingPosition,
812
806
  depositTokenIsSol,
813
- scopeRefreshConfig,
807
+ scopeFeed,
814
808
  initialInputs.calcs,
815
809
  budgetAndPriorityFeeIxs,
816
810
  {
@@ -842,7 +836,7 @@ export async function buildWithdrawWithLeverageIxns(
842
836
  currentSlot: number,
843
837
  isClosingPosition: boolean,
844
838
  depositTokenIsSol: boolean,
845
- scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
839
+ scopeFeed: string | undefined,
846
840
  calcs: WithdrawLeverageCalcsResult,
847
841
  budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
848
842
  swapQuoteIxs: SwapIxs,
@@ -916,7 +910,7 @@ export async function buildWithdrawWithLeverageIxns(
916
910
 
917
911
  const budgetIxns = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxns(3000000);
918
912
 
919
- // TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
913
+ // TODO: Might be worth removing as it's only needed for Ktokens
920
914
  // This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
921
915
  const fillWsolAtaIxns: TransactionInstruction[] = [];
922
916
  if (debtTokenMint.equals(NATIVE_MINT)) {
@@ -931,14 +925,11 @@ export async function buildWithdrawWithLeverageIxns(
931
925
  );
932
926
  }
933
927
 
934
- const scopeRefreshIxn = await getScopeRefreshIx(market, collReserve, debtReserve, obligation, scopeRefreshConfig);
935
-
936
928
  // 2. Prepare the flash borrow and flash repay amounts and ixns
937
929
  // We borrow exactly how much we need to repay
938
930
  // and repay that + flash amount fee
939
931
  const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
940
- borrowIxnIndex:
941
- budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
932
+ borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length,
942
933
  walletPublicKey: owner,
943
934
  lendingMarketAuthority: market.getLendingMarketAuthority(),
944
935
  lendingMarketAddress: market.getAddress(),
@@ -951,7 +942,7 @@ export async function buildWithdrawWithLeverageIxns(
951
942
  programId: market.programId,
952
943
  });
953
944
 
954
- // 3. Repay borrowed tokens and Withdraw tokens from reserve that will be swapped to repay flash loan
945
+ // 6. Repay borrowed tokens and Withdraw tokens from reserve that will be swapped to repay flash loan
955
946
  const repayAndWithdrawAction = await KaminoAction.buildRepayAndWithdrawTxns(
956
947
  market,
957
948
  isClosingPosition ? U64_MAX : toLamports(calcs.repayAmount, debtReserve!.stats.decimals).floor().toString(),
@@ -964,24 +955,25 @@ export async function buildWithdrawWithLeverageIxns(
964
955
  currentSlot,
965
956
  obligation,
966
957
  useV2Ixs,
967
- undefined,
968
958
  0,
969
959
  false,
970
960
  false, // to be checked and created in a setup tx in the UI (won't be the case for withdraw anyway as this would be created in deposit)
971
961
  false, // to be checked and created in a setup tx in the UI (won't be the case for withdraw anyway as this would be created in deposit)
972
962
  isClosingPosition,
973
- referrer
963
+ referrer,
964
+ { includeScopeRefresh: true, scopeFeed: scopeFeed! }
974
965
  );
975
966
 
976
967
  const swapInstructions = removeBudgetAndAtaIxns(swapQuoteIxs.swapIxs, []);
977
968
 
978
969
  return [
979
- ...scopeRefreshIxn,
980
970
  ...budgetIxns,
981
971
  ...atasAndCreateIxns.map((x) => x.createAtaIx),
982
972
  ...fillWsolAtaIxns,
983
973
  ...[flashBorrowIxn],
984
- ...KaminoAction.actionToIxs(repayAndWithdrawAction),
974
+ ...repayAndWithdrawAction.setupIxs,
975
+ ...KaminoAction.actionToLendingIxs(repayAndWithdrawAction),
976
+ ...repayAndWithdrawAction.cleanupIxs,
985
977
  ...swapInstructions,
986
978
  ...[flashRepayIxn],
987
979
  ...closeWsolAtaIxns,
@@ -1004,7 +996,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1004
996
  slippagePct,
1005
997
  budgetAndPriorityFeeIxs,
1006
998
  kamino,
1007
- scopeRefreshConfig,
999
+ scopeFeed,
1008
1000
  quoteBufferBps,
1009
1001
  isKtoken,
1010
1002
  quoter,
@@ -1024,10 +1016,13 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1024
1016
  const currentLeverage = obligation.refreshedStats.leverage;
1025
1017
  const isDepositViaLeverage = targetLeverage.gte(new Decimal(currentLeverage));
1026
1018
  let flashLoanFee;
1019
+ let borrowFee;
1027
1020
  if (isDepositViaLeverage) {
1028
1021
  flashLoanFee = collReserve.getFlashLoanFee() || new Decimal(0);
1022
+ borrowFee = collReserve.getBorrowFee() || new Decimal(0);
1029
1023
  } else {
1030
1024
  flashLoanFee = debtReserve.getFlashLoanFee() || new Decimal(0);
1025
+ borrowFee = debtReserve.getBorrowFee() || new Decimal(0);
1031
1026
  }
1032
1027
 
1033
1028
  const { adjustDepositPosition, adjustBorrowPosition } = calcAdjustAmounts({
@@ -1036,6 +1031,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1036
1031
  targetLeverage: targetLeverage,
1037
1032
  priceCollToDebt: priceCollToDebt,
1038
1033
  flashLoanFee: new Decimal(flashLoanFee),
1034
+ borrowFee,
1039
1035
  });
1040
1036
 
1041
1037
  const isDeposit = adjustDepositPosition.gte(0) && adjustBorrowPosition.gte(0);
@@ -1067,7 +1063,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1067
1063
  currentSlot,
1068
1064
  calcs,
1069
1065
  strategy,
1070
- scopeRefreshConfig,
1066
+ scopeFeed,
1071
1067
  collIsKtoken,
1072
1068
  {
1073
1069
  preActionIxs: [],
@@ -1103,6 +1099,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1103
1099
  targetLeverage: targetLeverage,
1104
1100
  priceCollToDebt: new Decimal(1).div(swapQuote.priceAInB),
1105
1101
  flashLoanFee: new Decimal(flashLoanFee),
1102
+ borrowFee,
1106
1103
  });
1107
1104
 
1108
1105
  const calcsQuotePrice = await adjustDepositLeverageCalcs(
@@ -1171,7 +1168,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1171
1168
  currentSlot,
1172
1169
  calcs,
1173
1170
  strategy,
1174
- scopeRefreshConfig,
1171
+ scopeFeed,
1175
1172
  collIsKtoken,
1176
1173
  {
1177
1174
  preActionIxs: [],
@@ -1207,6 +1204,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
1207
1204
  targetLeverage: targetLeverage,
1208
1205
  priceCollToDebt: swapQuote.priceAInB,
1209
1206
  flashLoanFee: new Decimal(flashLoanFee),
1207
+ borrowFee,
1210
1208
  });
1211
1209
 
1212
1210
  const calcsQuotePrice = adjustWithdrawLeverageCalcs(
@@ -1259,7 +1257,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
1259
1257
  slippagePct,
1260
1258
  budgetAndPriorityFeeIxs,
1261
1259
  kamino,
1262
- scopeRefreshConfig,
1260
+ scopeFeed,
1263
1261
  quoteBufferBps,
1264
1262
  priceAinB,
1265
1263
  isKtoken,
@@ -1283,7 +1281,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
1283
1281
  slippagePct,
1284
1282
  budgetAndPriorityFeeIxs,
1285
1283
  kamino,
1286
- scopeRefreshConfig,
1284
+ scopeFeed,
1287
1285
  quoteBufferBps,
1288
1286
  priceAinB,
1289
1287
  isKtoken,
@@ -1329,7 +1327,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
1329
1327
  currentSlot,
1330
1328
  initialInputs.calcs,
1331
1329
  initialInputs.strategy,
1332
- scopeRefreshConfig,
1330
+ scopeFeed,
1333
1331
  initialInputs.collIsKtoken,
1334
1332
  {
1335
1333
  preActionIxs: [],
@@ -1376,7 +1374,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
1376
1374
  currentSlot,
1377
1375
  initialInputs.calcs,
1378
1376
  initialInputs.strategy,
1379
- scopeRefreshConfig,
1377
+ scopeFeed,
1380
1378
  initialInputs.collIsKtoken,
1381
1379
  {
1382
1380
  preActionIxs: [],
@@ -1409,7 +1407,7 @@ async function buildIncreaseLeverageIxns(
1409
1407
  currentSlot: number,
1410
1408
  calcs: AdjustLeverageCalcsResult,
1411
1409
  strategy: StrategyWithAddress | undefined,
1412
- scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
1410
+ scopeFeed: string | undefined,
1413
1411
  collIsKtoken: boolean,
1414
1412
  swapQuoteIxs: SwapIxs,
1415
1413
  budgetAndPriorityFeeIxns: TransactionInstruction[] | undefined,
@@ -1481,17 +1479,9 @@ async function buildIncreaseLeverageIxns(
1481
1479
 
1482
1480
  const atasAndCreateIxns = createAtasIdempotent(owner, mintsToCreateAtas);
1483
1481
 
1484
- const scopeRefreshIxn = await getScopeRefreshIx(
1485
- kaminoMarket,
1486
- collReserve!,
1487
- debtReserve!,
1488
- obligation,
1489
- scopeRefreshConfig
1490
- );
1491
-
1492
1482
  // 2. Create borrow flash loan instruction
1493
1483
  const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
1494
- borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + (scopeRefreshIxn.length > 0 ? 1 : 0), // TODO: how about user metadata ixns
1484
+ borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length, // TODO: how about user metadata ixns
1495
1485
  walletPublicKey: owner,
1496
1486
  lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
1497
1487
  lendingMarketAddress: kaminoMarket.getAddress(),
@@ -1514,14 +1504,14 @@ async function buildIncreaseLeverageIxns(
1514
1504
  owner,
1515
1505
  obligation,
1516
1506
  useV2Ixs,
1517
- undefined,
1518
1507
  0,
1519
1508
  false,
1520
1509
  false,
1521
1510
  false, // 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)
1522
1511
  false, // 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)
1523
1512
  referrer,
1524
- currentSlot
1513
+ currentSlot,
1514
+ { includeScopeRefresh: true, scopeFeed: scopeFeed! }
1525
1515
  );
1526
1516
 
1527
1517
  // 4. Borrow tokens in borrow token reserve that will be swapped to repay flash loan
@@ -1532,37 +1522,43 @@ async function buildIncreaseLeverageIxns(
1532
1522
  owner,
1533
1523
  obligation,
1534
1524
  useV2Ixs,
1535
- undefined,
1536
1525
  0,
1537
1526
  false,
1538
1527
  false,
1539
1528
  false, // 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)
1540
1529
  false, // 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)
1541
1530
  referrer,
1542
- currentSlot
1531
+ currentSlot,
1532
+ { includeScopeRefresh: true, scopeFeed: scopeFeed! }
1543
1533
  );
1544
1534
 
1545
1535
  const swapInstructions = removeBudgetAndAtaIxns(swapQuoteIxs.swapIxs, []);
1546
1536
 
1547
1537
  const ixs = !collIsKtoken
1548
1538
  ? [
1549
- ...scopeRefreshIxn,
1550
1539
  ...budgetIxns,
1551
1540
  ...atasAndCreateIxns.map((x) => x.createAtaIx),
1552
1541
  ...[flashBorrowIxn],
1553
- ...KaminoAction.actionToIxs(depositAction),
1554
- ...KaminoAction.actionToIxs(borrowAction),
1542
+ ...depositAction.setupIxs,
1543
+ ...depositAction.lendingIxs,
1544
+ ...depositAction.cleanupIxs,
1545
+ ...borrowAction.setupIxs,
1546
+ ...borrowAction.lendingIxs,
1547
+ ...borrowAction.cleanupIxs,
1555
1548
  ...swapInstructions,
1556
1549
  ...[flashRepayIxn],
1557
1550
  ]
1558
1551
  : [
1559
- ...scopeRefreshIxn,
1560
1552
  ...budgetIxns,
1561
1553
  ...atasAndCreateIxns.map((x) => x.createAtaIx),
1562
1554
  ...[flashBorrowIxn],
1563
1555
  ...swapInstructions,
1564
- ...KaminoAction.actionToIxs(depositAction),
1565
- ...KaminoAction.actionToIxs(borrowAction),
1556
+ ...depositAction.setupIxs,
1557
+ ...depositAction.lendingIxs,
1558
+ ...depositAction.cleanupIxs,
1559
+ ...borrowAction.setupIxs,
1560
+ ...borrowAction.lendingIxs,
1561
+ ...borrowAction.cleanupIxs,
1566
1562
  ...[flashRepayIxn],
1567
1563
  ];
1568
1564
 
@@ -1582,7 +1578,7 @@ async function buildDecreaseLeverageIxns(
1582
1578
  currentSlot: number,
1583
1579
  calcs: AdjustLeverageCalcsResult,
1584
1580
  strategy: StrategyWithAddress | undefined,
1585
- scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
1581
+ scopeFeed: string | undefined,
1586
1582
  collIsKtoken: boolean,
1587
1583
  swapQuoteIxs: SwapIxs,
1588
1584
  budgetAndPriorityFeeIxns: TransactionInstruction[] | undefined,
@@ -1663,18 +1659,9 @@ async function buildDecreaseLeverageIxns(
1663
1659
  );
1664
1660
  }
1665
1661
 
1666
- const scopeRefreshIxn = await getScopeRefreshIx(
1667
- kaminoMarket,
1668
- collReserve!,
1669
- debtReserve!,
1670
- obligation,
1671
- scopeRefreshConfig
1672
- );
1673
-
1674
1662
  // 3. Flash borrow & repay amount to repay (debt)
1675
1663
  const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
1676
- borrowIxnIndex:
1677
- budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
1664
+ borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length,
1678
1665
  walletPublicKey: owner,
1679
1666
  lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
1680
1667
  lendingMarketAddress: kaminoMarket.getAddress(),
@@ -1688,6 +1675,7 @@ async function buildDecreaseLeverageIxns(
1688
1675
  });
1689
1676
 
1690
1677
  // 4. Actually do the repay of the flash borrowed amounts
1678
+ const scopeRefresh = scopeFeed ? { includeScopeRefresh: true, scopeFeed: scopeFeed } : undefined;
1691
1679
  const repayAction = await KaminoAction.buildRepayTxns(
1692
1680
  kaminoMarket,
1693
1681
  toLamports(Decimal.abs(calcs.adjustBorrowPosition), debtReserve!.stats.decimals).floor().toString(),
@@ -1695,7 +1683,6 @@ async function buildDecreaseLeverageIxns(
1695
1683
  owner,
1696
1684
  obligation,
1697
1685
  useV2Ixs,
1698
- undefined,
1699
1686
  currentSlot,
1700
1687
  undefined,
1701
1688
  0,
@@ -1703,7 +1690,8 @@ async function buildDecreaseLeverageIxns(
1703
1690
  false,
1704
1691
  false, // 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)
1705
1692
  false, // 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)
1706
- referrer
1693
+ referrer,
1694
+ scopeRefresh
1707
1695
  );
1708
1696
 
1709
1697
  // 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
@@ -1714,26 +1702,29 @@ async function buildDecreaseLeverageIxns(
1714
1702
  owner,
1715
1703
  obligation,
1716
1704
  useV2Ixs,
1717
- undefined,
1718
1705
  0,
1719
1706
  false,
1720
1707
  false,
1721
1708
  false, // 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)
1722
1709
  false, // 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)
1723
1710
  referrer,
1724
- currentSlot
1711
+ currentSlot,
1712
+ { includeScopeRefresh: true, scopeFeed: scopeFeed! }
1725
1713
  );
1726
1714
 
1727
1715
  const swapInstructions = removeBudgetAndAtaIxns(swapQuoteIxs.swapIxs, []);
1728
1716
 
1729
1717
  const ixns = [
1730
- ...scopeRefreshIxn,
1731
1718
  ...budgetIxns,
1732
1719
  ...atasAndCreateIxns.map((x) => x.createAtaIx),
1733
1720
  ...fillWsolAtaIxns,
1734
1721
  ...[flashBorrowIxn],
1735
- ...KaminoAction.actionToIxs(repayAction),
1736
- ...KaminoAction.actionToIxs(withdrawAction),
1722
+ ...repayAction.setupIxs,
1723
+ ...repayAction.lendingIxs,
1724
+ ...repayAction.cleanupIxs,
1725
+ ...withdrawAction.setupIxs,
1726
+ ...withdrawAction.lendingIxs,
1727
+ ...withdrawAction.cleanupIxs,
1737
1728
  ...swapInstructions,
1738
1729
  ...[flashRepayIxn],
1739
1730
  ...closeWsolAtaIxns,
@@ -1741,36 +1732,3 @@ async function buildDecreaseLeverageIxns(
1741
1732
 
1742
1733
  return ixns;
1743
1734
  }
1744
-
1745
- export const getScopeRefreshIx = async (
1746
- market: KaminoMarket,
1747
- collReserve: KaminoReserve,
1748
- debtReserve: KaminoReserve,
1749
- obligation: KaminoObligation | ObligationType | undefined,
1750
- scopeRefreshConfig: ScopePriceRefreshConfig | undefined
1751
- ): Promise<TransactionInstruction[]> => {
1752
- const allReserves =
1753
- obligation && isKaminoObligation(obligation)
1754
- ? new PublicKeySet<PublicKey>([
1755
- ...obligation.getDeposits().map((x) => x.reserveAddress),
1756
- ...obligation.getBorrows().map((x) => x.reserveAddress),
1757
- collReserve.address,
1758
- debtReserve.address,
1759
- ]).toArray()
1760
- : new PublicKeySet<PublicKey>([collReserve.address, debtReserve.address]).toArray();
1761
- const tokenIds = getTokenIdsForScopeRefresh(market, allReserves);
1762
-
1763
- const scopeRefreshIxns: TransactionInstruction[] = [];
1764
- if (tokenIds.length > 0 && scopeRefreshConfig) {
1765
- scopeRefreshIxns.push(
1766
- await scopeRefreshConfig.scope.refreshPriceListIx(
1767
- {
1768
- feed: scopeRefreshConfig.scopeFeed,
1769
- },
1770
- tokenIds
1771
- )
1772
- );
1773
- }
1774
-
1775
- return scopeRefreshIxns;
1776
- };