@kamino-finance/klend-sdk 7.0.7 → 7.0.9

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 (36) 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 +5 -4
  23. package/dist/manager/client_kamino_manager.js.map +1 -1
  24. package/dist/utils/farmUtils.d.ts.map +1 -1
  25. package/dist/utils/farmUtils.js +1 -4
  26. package/dist/utils/farmUtils.js.map +1 -1
  27. package/package.json +1 -1
  28. package/src/classes/action.ts +15 -15
  29. package/src/classes/obligation.ts +9 -9
  30. package/src/classes/vault.ts +0 -1
  31. package/src/lending_operations/repay_with_collateral_operations.ts +78 -74
  32. package/src/lending_operations/swap_collateral_operations.ts +15 -13
  33. package/src/leverage/operations.ts +350 -318
  34. package/src/leverage/types.ts +1 -0
  35. package/src/manager/client_kamino_manager.ts +5 -4
  36. package/src/utils/farmUtils.ts +1 -4
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getScopeRefreshIx = exports.getSetupIxs = void 0;
6
+ exports.getScopeRefreshIx = exports.getSetupIxs = exports.WITHDRAW_SLOT_OFFSET = void 0;
7
7
  exports.getDepositWithLeverageSwapInputs = getDepositWithLeverageSwapInputs;
8
8
  exports.getDepositWithLeverageIxs = getDepositWithLeverageIxs;
9
9
  exports.getWithdrawWithLeverageSwapInputs = getWithdrawWithLeverageSwapInputs;
@@ -24,6 +24,7 @@ const token_1 = require("@solana-program/token");
24
24
  const token_2022_1 = require("@solana-program/token-2022");
25
25
  const consts_1 = require("../utils/consts");
26
26
  const address_lookup_table_1 = require("@solana-program/address-lookup-table");
27
+ exports.WITHDRAW_SLOT_OFFSET = 150; // Offset for the withdraw slot to underestimate the exchange rate
27
28
  async function getDepositWithLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, depositAmount, priceDebtToColl, slippagePct, obligation, referrer, currentSlot, targetLeverage, selectedTokenMint, kamino, obligationTypeTagOverride, scopeRefreshConfig, budgetAndPriorityFeeIxs, quoteBufferBps, priceAinB, isKtoken, quoter, useV2Ixs, elevationGroupOverride, }) {
28
29
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
29
30
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
@@ -37,15 +38,17 @@ async function getDepositWithLeverageSwapInputs({ owner, kaminoMarket, debtToken
37
38
  console.log('Ops Calcs', (0, classes_1.toJson)(calcs));
38
39
  const obligationType = checkObligationType(obligationTypeTagOverride, collTokenMint, debtTokenMint, kaminoMarket);
39
40
  // Build the repay & withdraw collateral tx to get the number of accounts
40
- const klendIxs = await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation ? obligation : obligationType, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, {
41
- preActionIxs: [],
42
- swapIxs: [],
43
- lookupTables: [],
44
- quote: {
45
- priceAInB: new decimal_js_1.default(0), // not used
46
- quoteResponse: undefined,
41
+ const klendIxs = (await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation ? obligation : obligationType, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, [
42
+ {
43
+ preActionIxs: [],
44
+ swapIxs: [],
45
+ lookupTables: [],
46
+ quote: {
47
+ priceAInB: new decimal_js_1.default(0), // not used
48
+ quoteResponse: undefined,
49
+ },
47
50
  },
48
- }, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride);
51
+ ], strategy, collIsKtoken, useV2Ixs, elevationGroupOverride))[0];
49
52
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
50
53
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.swapDebtTokenIn : calcs.singleSidedDepositKtokenOnly, debtReserve.stats.decimals).ceil();
51
54
  const swapInputsForQuote = {
@@ -170,24 +173,26 @@ async function getDepositWithLeverageIxs({ owner, kaminoMarket, debtTokenMint, c
170
173
  const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
171
174
  const solTokenReserve = kaminoMarket.getReserveByMint(utils_1.WRAPPED_SOL_MINT);
172
175
  const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint === solTokenReserve.getLiquidityMint();
173
- return Promise.all(swapsArray.map(async (swap) => {
174
- const ixs = await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, initialInputs.obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, {
176
+ const depositWithLeverageIxs = await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, initialInputs.obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, swapsArray.map((swap) => {
177
+ return {
175
178
  preActionIxs: [],
176
179
  swapIxs: swap.swapIxs,
177
180
  lookupTables: swap.lookupTables,
178
181
  quote: swap.quote,
179
- }, initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs, elevationGroupOverride);
182
+ };
183
+ }), initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs, elevationGroupOverride);
184
+ return depositWithLeverageIxs.map((depositWithLeverageIxs, index) => {
180
185
  return {
181
- ixs: ixs.instructions,
182
- flashLoanInfo: ixs.flashLoanInfo,
183
- lookupTables: swap.lookupTables,
186
+ ixs: depositWithLeverageIxs.instructions,
187
+ flashLoanInfo: depositWithLeverageIxs.flashLoanInfo,
188
+ lookupTables: swapsArray[index].lookupTables,
184
189
  swapInputs,
185
190
  initialInputs,
186
- quote: swap.quote.quoteResponse,
191
+ quote: swapsArray[index].quote.quoteResponse,
187
192
  };
188
- }));
193
+ });
189
194
  }
190
- async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxs, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride) {
195
+ async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxsArray, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride) {
191
196
  const collTokenMint = collReserve.getLiquidityMint();
192
197
  const debtTokenMint = debtReserve.getLiquidityMint();
193
198
  const [[collTokenAta], [debtTokenAta]] = await Promise.all([
@@ -231,28 +236,30 @@ async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, own
231
236
  .toString(), debtTokenMint, owner, obligation, useV2Ixs, undefined, 0, false, elevationGroupOverride === 0 ? false : true, // emode
232
237
  { skipInitialization: true, skipLutCreation: true }, // to be checked and created in a setup tx in the UI
233
238
  referrer, currentSlot);
234
- // 4. Swap
235
- const { swapIxs } = swapQuoteIxs;
236
- const swapInstructions = (0, utils_1.removeBudgetIxs)(swapIxs);
237
- const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
238
- const flashLoanInfo = {
239
- flashBorrowReserve: flashBorrowReserve.address,
240
- flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
241
- };
242
- return {
243
- flashLoanInfo,
244
- instructions: [
245
- ...scopeRefreshIx,
246
- ...budgetIxs,
247
- ...createAtasIxs,
248
- ...fillWsolAtaIxs,
249
- ...[flashBorrowIx],
250
- ...(collIsKtoken ? swapInstructions : []),
251
- ...classes_1.KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
252
- ...(collIsKtoken ? [] : swapInstructions),
253
- ...[flashRepayIx],
254
- ],
255
- };
239
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
240
+ // 4. Swap
241
+ const { swapIxs } = swapQuoteIxs;
242
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapIxs);
243
+ const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
244
+ const flashLoanInfo = {
245
+ flashBorrowReserve: flashBorrowReserve.address,
246
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
247
+ };
248
+ return {
249
+ flashLoanInfo,
250
+ instructions: [
251
+ ...scopeRefreshIx,
252
+ ...budgetIxs,
253
+ ...createAtasIxs,
254
+ ...fillWsolAtaIxs,
255
+ ...[flashBorrowIx],
256
+ ...(collIsKtoken ? swapInstructions : []),
257
+ ...classes_1.KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
258
+ ...(collIsKtoken ? [] : swapInstructions),
259
+ ...[flashRepayIx],
260
+ ],
261
+ };
262
+ });
256
263
  }
257
264
  async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, deposited, borrowed, obligation, referrer, currentSlot, withdrawAmount, priceCollToDebt, slippagePct, isClosingPosition, selectedTokenMint, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, isKtoken, quoter, useV2Ixs, }) {
258
265
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
@@ -263,15 +270,17 @@ async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtToke
263
270
  const strategy = collIsKtoken ? (await kamino.getStrategyByKTokenMint(collTokenMint)) : undefined;
264
271
  const inputTokenIsSol = selectedTokenMint === utils_1.WRAPPED_SOL_MINT;
265
272
  const calcs = (0, calcs_1.withdrawLeverageCalcs)(kaminoMarket, collReserve, debtReserve, priceCollToDebt, withdrawAmount, deposited, borrowed, currentSlot, isClosingPosition, selectedTokenIsCollToken, selectedTokenMint, obligation, flashLoanFee, slippagePct);
266
- const klendIxs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, {
267
- preActionIxs: [],
268
- swapIxs: [],
269
- lookupTables: [],
270
- quote: {
271
- priceAInB: new decimal_js_1.default(0), // not used
272
- quoteResponse: undefined,
273
+ const klendIxs = (await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, [
274
+ {
275
+ preActionIxs: [],
276
+ swapIxs: [],
277
+ lookupTables: [],
278
+ quote: {
279
+ priceAInB: new decimal_js_1.default(0), // not used
280
+ quoteResponse: undefined,
281
+ },
273
282
  },
274
- }, strategy, collIsKtoken, useV2Ixs);
283
+ ], strategy, collIsKtoken, useV2Ixs))[0];
275
284
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
276
285
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(calcs.collTokenSwapIn, collReserve.getMintDecimals()).ceil();
277
286
  const swapInputsForQuote = {
@@ -352,25 +361,27 @@ async function getWithdrawWithLeverageIxs({ owner, kaminoMarket, debtTokenMint,
352
361
  console.log('Strategy lookup table not found');
353
362
  }
354
363
  }
355
- return Promise.all(swapsArray.map(async (swap) => {
356
- const ixs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, {
364
+ const withdrawWithLeverageIxs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, swapsArray.map((swap) => {
365
+ return {
357
366
  preActionIxs: [],
358
367
  swapIxs: swap.swapIxs,
359
368
  lookupTables: swap.lookupTables,
360
369
  quote: swap.quote,
361
- }, initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs);
362
- // Send ixs and lookup tables
370
+ };
371
+ }), initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs);
372
+ // Send ixs and lookup tables
373
+ return withdrawWithLeverageIxs.map((ixs, index) => {
363
374
  return {
364
375
  ixs: ixs.instructions,
365
376
  flashLoanInfo: ixs.flashLoanInfo,
366
- lookupTables: swap.lookupTables,
377
+ lookupTables: swapsArray[index].lookupTables,
367
378
  swapInputs,
368
379
  initialInputs: initialInputs,
369
- quote: swap.quote.quoteResponse,
380
+ quote: swapsArray[index].quote.quoteResponse,
370
381
  };
371
- }));
382
+ });
372
383
  }
373
- async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxs, strategy, collIsKtoken, useV2Ixs) {
384
+ async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxsArray, strategy, collIsKtoken, useV2Ixs) {
374
385
  const collTokenMint = collReserve.getLiquidityMint();
375
386
  const debtTokenMint = debtReserve.getLiquidityMint();
376
387
  const debtTokenAta = await (0, utils_1.getAssociatedTokenAddress)(debtTokenMint, owner.address, debtReserve.getLiquidityTokenProgram());
@@ -414,26 +425,28 @@ async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, ow
414
425
  ? utils_1.U64_MAX
415
426
  : (0, classes_2.numberToLamportsDecimal)(calcs.depositTokenWithdrawAmount, collReserve.stats.decimals).ceil().toString(), collTokenMint, owner, currentSlot, obligation, useV2Ixs, undefined, 0, false, false, { skipInitialization: true, skipLutCreation: true }, // 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)
416
427
  referrer);
417
- const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
418
- return {
419
- flashLoanInfo: {
420
- flashLoanFee: debtReserve.getFlashLoanFee(),
421
- flashBorrowReserve: debtReserve.address,
422
- },
423
- instructions: [
424
- ...scopeRefreshIx,
425
- ...budgetIxs,
426
- ...createAtasIxs,
427
- ...fillWsolAtaIxs,
428
- ...[flashBorrowIx],
429
- ...classes_1.KaminoAction.actionToIxs(repayAndWithdrawAction),
430
- ...swapInstructions,
431
- ...[flashRepayIx],
432
- ...closeWsolAtaIxs,
433
- ],
434
- };
428
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
429
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
430
+ return {
431
+ flashLoanInfo: {
432
+ flashLoanFee: debtReserve.getFlashLoanFee(),
433
+ flashBorrowReserve: debtReserve.address,
434
+ },
435
+ instructions: [
436
+ ...scopeRefreshIx,
437
+ ...budgetIxs,
438
+ ...createAtasIxs,
439
+ ...fillWsolAtaIxs,
440
+ ...[flashBorrowIx],
441
+ ...classes_1.KaminoAction.actionToIxs(repayAndWithdrawAction),
442
+ ...swapInstructions,
443
+ ...[flashRepayIx],
444
+ ...closeWsolAtaIxs,
445
+ ],
446
+ };
447
+ });
435
448
  }
436
- async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, isKtoken, quoter, useV2Ixs, }) {
449
+ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, isKtoken, quoter, useV2Ixs, withdrawSlotOffset, }) {
437
450
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
438
451
  const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
439
452
  const deposited = (0, classes_1.lamportsToNumberDecimal)(depositedLamports, collReserve.stats.decimals);
@@ -464,15 +477,17 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
464
477
  if (isDeposit) {
465
478
  const calcs = await (0, calcs_1.adjustDepositLeverageCalcs)(kaminoMarket, owner.address, debtReserve, adjustDepositPosition, adjustBorrowPosition, priceDebtToColl, flashLoanFee, slippagePct, collIsKtoken);
466
479
  // Build the repay & withdraw collateral tx to get the number of accounts
467
- const klendIxs = await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, {
468
- preActionIxs: [],
469
- swapIxs: [],
470
- lookupTables: [],
471
- quote: {
472
- priceAInB: new decimal_js_1.default(0), // not used
473
- quoteResponse: undefined,
480
+ const klendIxs = (await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, [
481
+ {
482
+ preActionIxs: [],
483
+ swapIxs: [],
484
+ lookupTables: [],
485
+ quote: {
486
+ priceAInB: new decimal_js_1.default(0), // not used
487
+ quoteResponse: undefined,
488
+ },
474
489
  },
475
- }, budgetAndPriorityFeeIxs, useV2Ixs);
490
+ ], budgetAndPriorityFeeIxs, useV2Ixs))[0];
476
491
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
477
492
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.borrowAmount : calcs.amountToFlashBorrowDebt, debtReserve.stats.decimals).ceil();
478
493
  const swapInputsForQuote = {
@@ -518,15 +533,17 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
518
533
  }
519
534
  else {
520
535
  const calcs = (0, calcs_1.adjustWithdrawLeverageCalcs)(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
521
- const klendIxs = await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, {
522
- preActionIxs: [],
523
- swapIxs: [],
524
- lookupTables: [],
525
- quote: {
526
- priceAInB: new decimal_js_1.default(0), // not used
527
- quoteResponse: undefined,
536
+ const klendIxs = (await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, [
537
+ {
538
+ preActionIxs: [],
539
+ swapIxs: [],
540
+ lookupTables: [],
541
+ quote: {
542
+ priceAInB: new decimal_js_1.default(0), // not used
543
+ quoteResponse: undefined,
544
+ },
528
545
  },
529
- }, budgetAndPriorityFeeIxs, useV2Ixs);
546
+ ], budgetAndPriorityFeeIxs, useV2Ixs, withdrawSlotOffset))[0];
530
547
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
531
548
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(calcs.withdrawAmountWithSlippageAndFlashLoanFee, collReserve.state.liquidity.mintDecimals.toNumber()).ceil();
532
549
  const swapInputsForQuote = {
@@ -567,7 +584,7 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
567
584
  };
568
585
  }
569
586
  }
570
- async function getAdjustLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, priceAinB, isKtoken, quoter, swapper, useV2Ixs, }) {
587
+ async function getAdjustLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, priceAinB, isKtoken, quoter, swapper, useV2Ixs, withdrawSlotOffset, }) {
571
588
  const { swapInputs, initialInputs } = await getAdjustLeverageSwapInputs({
572
589
  owner,
573
590
  kaminoMarket,
@@ -604,22 +621,24 @@ async function getAdjustLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTo
604
621
  depositSwapper = swapper;
605
622
  }
606
623
  const swapsArray = await depositSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
607
- return Promise.all(swapsArray.map(async (swap) => {
608
- const ixs = await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, {
624
+ const increaseLeverageIxs = await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, swapsArray.map((swap) => {
625
+ return {
609
626
  preActionIxs: [],
610
627
  swapIxs: swap.swapIxs,
611
628
  lookupTables: swap.lookupTables,
612
629
  quote: swap.quote,
613
- }, budgetAndPriorityFeeIxs, useV2Ixs);
630
+ };
631
+ }), budgetAndPriorityFeeIxs, useV2Ixs);
632
+ return increaseLeverageIxs.map((ixs, index) => {
614
633
  return {
615
634
  ixs: ixs.instructions,
616
635
  flashLoanInfo: ixs.flashLoanInfo,
617
- lookupTables: swap.lookupTables,
636
+ lookupTables: swapsArray[index].lookupTables,
618
637
  swapInputs,
619
638
  initialInputs,
620
- quote: swap.quote.quoteResponse,
639
+ quote: swapsArray[index].quote.quoteResponse,
621
640
  };
622
- }));
641
+ });
623
642
  }
624
643
  else {
625
644
  console.log('Decreasing leverage');
@@ -635,28 +654,30 @@ async function getAdjustLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTo
635
654
  }
636
655
  // 5. Get swap ixs
637
656
  const swapsArray = await withdrawSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
638
- return Promise.all(swapsArray.map(async (swap) => {
639
- const ixs = await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, {
657
+ const decreaseLeverageIxs = await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, swapsArray.map((swap) => {
658
+ return {
640
659
  preActionIxs: [],
641
660
  swapIxs: swap.swapIxs,
642
661
  lookupTables: swap.lookupTables,
643
662
  quote: swap.quote,
644
- }, budgetAndPriorityFeeIxs, useV2Ixs);
663
+ };
664
+ }), budgetAndPriorityFeeIxs, useV2Ixs, withdrawSlotOffset);
665
+ return decreaseLeverageIxs.map((ixs, index) => {
645
666
  return {
646
667
  ixs: ixs.instructions,
647
668
  flashLoanInfo: ixs.flashLoanInfo,
648
- lookupTables: swap.lookupTables,
669
+ lookupTables: swapsArray[index].lookupTables,
649
670
  swapInputs,
650
671
  initialInputs,
651
- quote: swap.quote.quoteResponse,
672
+ quote: swapsArray[index].quote.quoteResponse,
652
673
  };
653
- }));
674
+ });
654
675
  }
655
676
  }
656
677
  /**
657
678
  * Deposit and borrow tokens if leverage increased
658
679
  */
659
- async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxs, useV2Ixs) {
680
+ async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxsArray, budgetAndPriorityFeeIxs, useV2Ixs) {
660
681
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
661
682
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
662
683
  const [debtTokenAta, collTokenAta] = await Promise.all([
@@ -683,32 +704,34 @@ async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
683
704
  // 4. Borrow tokens in borrow token reserve that will be swapped to repay flash loan
684
705
  const borrowAction = await classes_1.KaminoAction.buildBorrowTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(calcs.borrowAmount, debtReserve.stats.decimals).ceil().toString(), debtTokenMint, owner, obligation, useV2Ixs, undefined, 0, false, false, { 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)
685
706
  referrer, currentSlot);
686
- const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
687
- const ixs = [
688
- ...scopeRefreshIx,
689
- ...budgetIxs,
690
- ...createAtasIxs,
691
- ...[flashBorrowIx],
692
- ...(collIsKtoken ? swapInstructions : []),
693
- ...classes_1.KaminoAction.actionToIxs(depositAction),
694
- ...classes_1.KaminoAction.actionToIxs(borrowAction),
695
- ...(collIsKtoken ? [] : swapInstructions),
696
- ...[flashRepayIx],
697
- ];
698
- const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
699
- const res = {
700
- flashLoanInfo: {
701
- flashBorrowReserve: flashBorrowReserve.address,
702
- flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
703
- },
704
- instructions: ixs,
705
- };
706
- return res;
707
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
708
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
709
+ const ixs = [
710
+ ...scopeRefreshIx,
711
+ ...budgetIxs,
712
+ ...createAtasIxs,
713
+ ...[flashBorrowIx],
714
+ ...(collIsKtoken ? swapInstructions : []),
715
+ ...classes_1.KaminoAction.actionToIxs(depositAction),
716
+ ...classes_1.KaminoAction.actionToIxs(borrowAction),
717
+ ...(collIsKtoken ? [] : swapInstructions),
718
+ ...[flashRepayIx],
719
+ ];
720
+ const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
721
+ const res = {
722
+ flashLoanInfo: {
723
+ flashBorrowReserve: flashBorrowReserve.address,
724
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
725
+ },
726
+ instructions: ixs,
727
+ };
728
+ return res;
729
+ });
707
730
  }
708
731
  /**
709
732
  * Withdraw and repay tokens if leverage decreased
710
733
  */
711
- async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxs, useV2Ixs) {
734
+ async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxsArray, budgetAndPriorityFeeIxs, useV2Ixs, withdrawSlotOffset = exports.WITHDRAW_SLOT_OFFSET) {
712
735
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
713
736
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
714
737
  const [debtTokenAta] = await (0, token_2022_1.findAssociatedTokenPda)({
@@ -752,30 +775,33 @@ async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
752
775
  // 4. Actually do the repay of the flash borrowed amounts
753
776
  const repayAction = await classes_1.KaminoAction.buildRepayTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(decimal_js_1.default.abs(calcs.adjustBorrowPosition), debtReserve.stats.decimals).floor().toString(), debtTokenMint, owner, obligation, useV2Ixs, undefined, currentSlot, undefined, 0, false, false, { 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)
754
777
  referrer);
778
+ const withdrawSlot = currentSlot - BigInt(withdrawSlotOffset);
755
779
  // 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
756
780
  const withdrawAction = await classes_1.KaminoAction.buildWithdrawTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(calcs.withdrawAmountWithSlippageAndFlashLoanFee, collReserve.stats.decimals).ceil().toString(), collTokenMint, owner, obligation, useV2Ixs, undefined, 0, false, false, { 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)
757
- referrer, currentSlot);
758
- const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
759
- const ixs = [
760
- ...scopeRefreshIx,
761
- ...budgetIxs,
762
- ...createAtasIxs,
763
- ...fillWsolAtaIxs,
764
- ...[flashBorrowIx],
765
- ...classes_1.KaminoAction.actionToIxs(repayAction),
766
- ...classes_1.KaminoAction.actionToIxs(withdrawAction),
767
- ...swapInstructions,
768
- ...[flashRepayIx],
769
- ...closeWsolAtaIxs,
770
- ];
771
- const res = {
772
- flashLoanInfo: {
773
- flashBorrowReserve: debtReserve.address,
774
- flashLoanFee: debtReserve.getFlashLoanFee(),
775
- },
776
- instructions: ixs,
777
- };
778
- return res;
781
+ referrer, withdrawSlot);
782
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
783
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
784
+ const ixs = [
785
+ ...scopeRefreshIx,
786
+ ...budgetIxs,
787
+ ...createAtasIxs,
788
+ ...fillWsolAtaIxs,
789
+ ...[flashBorrowIx],
790
+ ...classes_1.KaminoAction.actionToIxs(repayAction),
791
+ ...classes_1.KaminoAction.actionToIxs(withdrawAction),
792
+ ...swapInstructions,
793
+ ...[flashRepayIx],
794
+ ...closeWsolAtaIxs,
795
+ ];
796
+ const res = {
797
+ flashLoanInfo: {
798
+ flashBorrowReserve: debtReserve.address,
799
+ flashLoanFee: debtReserve.getFlashLoanFee(),
800
+ },
801
+ instructions: ixs,
802
+ };
803
+ return res;
804
+ });
779
805
  }
780
806
  const getSetupIxs = async (owner, kaminoMarket, obligation, collTokenMint, collReserve, collIsKtoken, debtTokenMint, debtReserve, strategy, scopeRefreshConfig, budgetAndPriorityFeeIxs) => {
781
807
  const budgetIxs = budgetAndPriorityFeeIxs || (0, utils_1.getComputeBudgetAndPriorityFeeIxs)(3000000);