@kamino-finance/klend-sdk 6.0.5-beta.2 → 6.0.5-beta.20

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 (62) 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 +32 -16
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/manager.d.ts +29 -18
  6. package/dist/classes/manager.d.ts.map +1 -1
  7. package/dist/classes/manager.js +66 -49
  8. package/dist/classes/manager.js.map +1 -1
  9. package/dist/classes/market.d.ts +12 -11
  10. package/dist/classes/market.d.ts.map +1 -1
  11. package/dist/classes/market.js +77 -37
  12. package/dist/classes/market.js.map +1 -1
  13. package/dist/classes/vault.d.ts +5 -3
  14. package/dist/classes/vault.d.ts.map +1 -1
  15. package/dist/classes/vault.js +8 -6
  16. package/dist/classes/vault.js.map +1 -1
  17. package/dist/client_kamino_manager.d.ts.map +1 -1
  18. package/dist/client_kamino_manager.js +30 -22
  19. package/dist/client_kamino_manager.js.map +1 -1
  20. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  21. package/dist/lending_operations/repay_with_collateral_operations.js +36 -32
  22. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  23. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  24. package/dist/lending_operations/swap_collateral_operations.js +4 -4
  25. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  26. package/dist/leverage/operations.d.ts +4 -3
  27. package/dist/leverage/operations.d.ts.map +1 -1
  28. package/dist/leverage/operations.js +186 -154
  29. package/dist/leverage/operations.js.map +1 -1
  30. package/dist/leverage/types.d.ts +1 -0
  31. package/dist/leverage/types.d.ts.map +1 -1
  32. package/dist/utils/managerTypes.d.ts +1 -2
  33. package/dist/utils/managerTypes.d.ts.map +1 -1
  34. package/dist/utils/managerTypes.js +9 -9
  35. package/dist/utils/managerTypes.js.map +1 -1
  36. package/dist/utils/obligations.d.ts +5 -0
  37. package/dist/utils/obligations.d.ts.map +1 -0
  38. package/dist/utils/obligations.js +53 -0
  39. package/dist/utils/obligations.js.map +1 -0
  40. package/dist/utils/oracle.d.ts +3 -3
  41. package/dist/utils/oracle.d.ts.map +1 -1
  42. package/dist/utils/oracle.js +2 -2
  43. package/dist/utils/oracle.js.map +1 -1
  44. package/dist/utils/pubkey.d.ts +1 -0
  45. package/dist/utils/pubkey.d.ts.map +1 -1
  46. package/dist/utils/pubkey.js +10 -0
  47. package/dist/utils/pubkey.js.map +1 -1
  48. package/package.json +3 -3
  49. package/src/classes/action.ts +32 -20
  50. package/src/classes/manager.ts +87 -53
  51. package/src/classes/market.ts +132 -52
  52. package/src/classes/vault.ts +17 -6
  53. package/src/client.ts +4 -4
  54. package/src/client_kamino_manager.ts +40 -35
  55. package/src/lending_operations/repay_with_collateral_operations.ts +76 -72
  56. package/src/lending_operations/swap_collateral_operations.ts +13 -11
  57. package/src/leverage/operations.ts +362 -328
  58. package/src/leverage/types.ts +1 -0
  59. package/src/utils/managerTypes.ts +1 -2
  60. package/src/utils/obligations.ts +69 -0
  61. package/src/utils/oracle.ts +5 -4
  62. package/src/utils/pubkey.ts +9 -0
@@ -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;
@@ -21,6 +21,7 @@ const calcs_1 = require("./calcs");
21
21
  const spl_token_1 = require("@solana/spl-token");
22
22
  const utils_2 = require("./utils");
23
23
  const CreationParameters_1 = require("@kamino-finance/kliquidity-sdk/dist/utils/CreationParameters");
24
+ exports.WITHDRAW_SLOT_OFFSET = 150; // Offset for the withdraw slot to ensure it is after the deposit slot
24
25
  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, }) {
25
26
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
26
27
  const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
@@ -34,15 +35,17 @@ async function getDepositWithLeverageSwapInputs({ owner, kaminoMarket, debtToken
34
35
  console.log('Ops Calcs', (0, classes_1.toJson)(calcs));
35
36
  const obligationType = checkObligationType(obligationTypeTagOverride, collTokenMint, debtTokenMint, kaminoMarket);
36
37
  // Build the repay & withdraw collateral tx to get the number of accounts
37
- const klendIxs = await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation ? obligation : obligationType, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, {
38
- preActionIxs: [],
39
- swapIxs: [],
40
- lookupTables: [],
41
- quote: {
42
- priceAInB: new decimal_js_1.default(0), // not used
43
- quoteResponse: undefined,
38
+ const klendIxs = (await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation ? obligation : obligationType, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, [
39
+ {
40
+ preActionIxs: [],
41
+ swapIxs: [],
42
+ lookupTables: [],
43
+ quote: {
44
+ priceAInB: new decimal_js_1.default(0), // not used
45
+ quoteResponse: undefined,
46
+ },
44
47
  },
45
- }, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride);
48
+ ], strategy, collIsKtoken, useV2Ixs, elevationGroupOverride))[0];
46
49
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
47
50
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.swapDebtTokenIn : calcs.singleSidedDepositKtokenOnly, debtReserve.stats.decimals).ceil();
48
51
  const swapInputsForQuote = {
@@ -167,24 +170,26 @@ async function getDepositWithLeverageIxs({ owner, kaminoMarket, debtTokenMint, c
167
170
  const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
168
171
  const solTokenReserve = kaminoMarket.getReserveByMint(spl_token_1.NATIVE_MINT);
169
172
  const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve.getLiquidityMint());
170
- return Promise.all(swapsArray.map(async (swap) => {
171
- const ixs = await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, initialInputs.obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, {
173
+ const depositWithLeverageIxs = await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, initialInputs.obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, swapsArray.map((swap) => {
174
+ return {
172
175
  preActionIxs: [],
173
176
  swapIxs: swap.swapIxs,
174
177
  lookupTables: swap.lookupTables,
175
178
  quote: swap.quote,
176
- }, initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs, elevationGroupOverride);
179
+ };
180
+ }), initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs, elevationGroupOverride);
181
+ return depositWithLeverageIxs.map((depositWithLeverageIxs, index) => {
177
182
  return {
178
- ixs: ixs.instructions,
179
- flashLoanInfo: ixs.flashLoanInfo,
180
- lookupTables: swap.lookupTables,
183
+ ixs: depositWithLeverageIxs.instructions,
184
+ flashLoanInfo: depositWithLeverageIxs.flashLoanInfo,
185
+ lookupTables: swapsArray[index].lookupTables,
181
186
  swapInputs,
182
187
  initialInputs,
183
- quote: swap.quote.quoteResponse,
188
+ quote: swapsArray[index].quote.quoteResponse,
184
189
  };
185
- }));
190
+ });
186
191
  }
187
- async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxs, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride) {
192
+ async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxsArray, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride) {
188
193
  const collTokenMint = collReserve.getLiquidityMint();
189
194
  const debtTokenMint = debtReserve.getLiquidityMint();
190
195
  const collTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collTokenMint, owner, false, collReserve.getLiquidityTokenProgram());
@@ -218,28 +223,30 @@ async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, own
218
223
  .toString(), debtTokenMint, owner, obligation, useV2Ixs, undefined, 0, false, elevationGroupOverride === 0 ? false : true, // emode
219
224
  { skipInitialization: true, skipLutCreation: true }, // to be checked and created in a setup tx in the UI
220
225
  referrer, currentSlot);
221
- // 4. Swap
222
- const { swapIxs } = swapQuoteIxs;
223
- const swapInstructions = (0, utils_1.removeBudgetIxs)(swapIxs);
224
- const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
225
- const flashLoanInfo = {
226
- flashBorrowReserve: flashBorrowReserve.address,
227
- flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
228
- };
229
- return {
230
- flashLoanInfo,
231
- instructions: [
232
- ...scopeRefreshIx,
233
- ...budgetIxs,
234
- ...createAtasIxs,
235
- ...fillWsolAtaIxs,
236
- ...[flashBorrowIx],
237
- ...(collIsKtoken ? swapInstructions : []),
238
- ...classes_1.KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
239
- ...(collIsKtoken ? [] : swapInstructions),
240
- ...[flashRepayIx],
241
- ],
242
- };
226
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
227
+ // 4. Swap
228
+ const { swapIxs } = swapQuoteIxs;
229
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapIxs);
230
+ const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
231
+ const flashLoanInfo = {
232
+ flashBorrowReserve: flashBorrowReserve.address,
233
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
234
+ };
235
+ return {
236
+ flashLoanInfo,
237
+ instructions: [
238
+ ...scopeRefreshIx,
239
+ ...budgetIxs,
240
+ ...createAtasIxs,
241
+ ...fillWsolAtaIxs,
242
+ ...[flashBorrowIx],
243
+ ...(collIsKtoken ? swapInstructions : []),
244
+ ...classes_1.KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
245
+ ...(collIsKtoken ? [] : swapInstructions),
246
+ ...[flashRepayIx],
247
+ ],
248
+ };
249
+ });
243
250
  }
244
251
  async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, deposited, borrowed, obligation, referrer, currentSlot, withdrawAmount, priceCollToDebt, slippagePct, isClosingPosition, selectedTokenMint, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, isKtoken, quoter, useV2Ixs, }) {
245
252
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
@@ -250,15 +257,17 @@ async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtToke
250
257
  const strategy = collIsKtoken ? (await kamino.getStrategyByKTokenMint(collTokenMint)) : undefined;
251
258
  const inputTokenIsSol = selectedTokenMint.equals(spl_token_1.NATIVE_MINT);
252
259
  const calcs = (0, calcs_1.withdrawLeverageCalcs)(kaminoMarket, collReserve, debtReserve, priceCollToDebt, withdrawAmount, deposited, borrowed, currentSlot, isClosingPosition, selectedTokenIsCollToken, selectedTokenMint, obligation, flashLoanFee, slippagePct);
253
- const klendIxs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, {
254
- preActionIxs: [],
255
- swapIxs: [],
256
- lookupTables: [],
257
- quote: {
258
- priceAInB: new decimal_js_1.default(0), // not used
259
- quoteResponse: undefined,
260
+ const klendIxs = (await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, [
261
+ {
262
+ preActionIxs: [],
263
+ swapIxs: [],
264
+ lookupTables: [],
265
+ quote: {
266
+ priceAInB: new decimal_js_1.default(0), // not used
267
+ quoteResponse: undefined,
268
+ },
260
269
  },
261
- }, strategy, collIsKtoken, useV2Ixs);
270
+ ], strategy, collIsKtoken, useV2Ixs))[0];
262
271
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
263
272
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(calcs.collTokenSwapIn, collReserve.getMintDecimals()).ceil();
264
273
  const swapInputsForQuote = {
@@ -339,25 +348,27 @@ async function getWithdrawWithLeverageIxs({ owner, kaminoMarket, debtTokenMint,
339
348
  console.log('Strategy lookup table not found');
340
349
  }
341
350
  }
342
- return Promise.all(swapsArray.map(async (swap) => {
343
- const ixs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, {
351
+ const withdrawWithLeverageIxs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, swapsArray.map((swap) => {
352
+ return {
344
353
  preActionIxs: [],
345
354
  swapIxs: swap.swapIxs,
346
355
  lookupTables: swap.lookupTables,
347
356
  quote: swap.quote,
348
- }, initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs);
349
- // Send ixs and lookup tables
357
+ };
358
+ }), initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs);
359
+ // Send ixs and lookup tables
360
+ return withdrawWithLeverageIxs.map((ixs, index) => {
350
361
  return {
351
362
  ixs: ixs.instructions,
352
363
  flashLoanInfo: ixs.flashLoanInfo,
353
- lookupTables: swap.lookupTables,
364
+ lookupTables: swapsArray[index].lookupTables,
354
365
  swapInputs,
355
366
  initialInputs: initialInputs,
356
- quote: swap.quote.quoteResponse,
367
+ quote: swapsArray[index].quote.quoteResponse,
357
368
  };
358
- }));
369
+ });
359
370
  }
360
- async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxs, strategy, collIsKtoken, useV2Ixs) {
371
+ async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxsArray, strategy, collIsKtoken, useV2Ixs) {
361
372
  const collTokenMint = collReserve.getLiquidityMint();
362
373
  const debtTokenMint = debtReserve.getLiquidityMint();
363
374
  const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
@@ -397,26 +408,28 @@ async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, ow
397
408
  ? utils_1.U64_MAX
398
409
  : (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)
399
410
  referrer);
400
- const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
401
- return {
402
- flashLoanInfo: {
403
- flashLoanFee: debtReserve.getFlashLoanFee(),
404
- flashBorrowReserve: debtReserve.address,
405
- },
406
- instructions: [
407
- ...scopeRefreshIx,
408
- ...budgetIxs,
409
- ...createAtasIxs,
410
- ...fillWsolAtaIxs,
411
- ...[flashBorrowIx],
412
- ...classes_1.KaminoAction.actionToIxs(repayAndWithdrawAction),
413
- ...swapInstructions,
414
- ...[flashRepayIx],
415
- ...closeWsolAtaIxs,
416
- ],
417
- };
411
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
412
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
413
+ return {
414
+ flashLoanInfo: {
415
+ flashLoanFee: debtReserve.getFlashLoanFee(),
416
+ flashBorrowReserve: debtReserve.address,
417
+ },
418
+ instructions: [
419
+ ...scopeRefreshIx,
420
+ ...budgetIxs,
421
+ ...createAtasIxs,
422
+ ...fillWsolAtaIxs,
423
+ ...[flashBorrowIx],
424
+ ...classes_1.KaminoAction.actionToIxs(repayAndWithdrawAction),
425
+ ...swapInstructions,
426
+ ...[flashRepayIx],
427
+ ...closeWsolAtaIxs,
428
+ ],
429
+ };
430
+ });
418
431
  }
419
- async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, isKtoken, quoter, useV2Ixs, }) {
432
+ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, isKtoken, quoter, useV2Ixs, withdrawSlotOffset, }) {
420
433
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
421
434
  const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
422
435
  const deposited = (0, classes_1.lamportsToNumberDecimal)(depositedLamports, collReserve.stats.decimals);
@@ -447,15 +460,17 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
447
460
  if (isDeposit) {
448
461
  const calcs = await (0, calcs_1.adjustDepositLeverageCalcs)(kaminoMarket, owner, debtReserve, adjustDepositPosition, adjustBorrowPosition, priceDebtToColl, flashLoanFee, slippagePct, collIsKtoken);
449
462
  // Build the repay & withdraw collateral tx to get the number of accounts
450
- const klendIxs = await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, {
451
- preActionIxs: [],
452
- swapIxs: [],
453
- lookupTables: [],
454
- quote: {
455
- priceAInB: new decimal_js_1.default(0), // not used
456
- quoteResponse: undefined,
463
+ const klendIxs = (await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, [
464
+ {
465
+ preActionIxs: [],
466
+ swapIxs: [],
467
+ lookupTables: [],
468
+ quote: {
469
+ priceAInB: new decimal_js_1.default(0), // not used
470
+ quoteResponse: undefined,
471
+ },
457
472
  },
458
- }, budgetAndPriorityFeeIxs, useV2Ixs);
473
+ ], budgetAndPriorityFeeIxs, useV2Ixs))[0];
459
474
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
460
475
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.borrowAmount : calcs.amountToFlashBorrowDebt, debtReserve.stats.decimals).ceil();
461
476
  const swapInputsForQuote = {
@@ -501,15 +516,17 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
501
516
  }
502
517
  else {
503
518
  const calcs = (0, calcs_1.adjustWithdrawLeverageCalcs)(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
504
- const klendIxs = await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, {
505
- preActionIxs: [],
506
- swapIxs: [],
507
- lookupTables: [],
508
- quote: {
509
- priceAInB: new decimal_js_1.default(0), // not used
510
- quoteResponse: undefined,
519
+ const klendIxs = (await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, [
520
+ {
521
+ preActionIxs: [],
522
+ swapIxs: [],
523
+ lookupTables: [],
524
+ quote: {
525
+ priceAInB: new decimal_js_1.default(0), // not used
526
+ quoteResponse: undefined,
527
+ },
511
528
  },
512
- }, budgetAndPriorityFeeIxs, useV2Ixs);
529
+ ], budgetAndPriorityFeeIxs, useV2Ixs, withdrawSlotOffset))[0];
513
530
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
514
531
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(calcs.withdrawAmountWithSlippageAndFlashLoanFee, collReserve.state.liquidity.mintDecimals.toNumber()).ceil();
515
532
  const swapInputsForQuote = {
@@ -550,7 +567,7 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
550
567
  };
551
568
  }
552
569
  }
553
- 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, }) {
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, withdrawSlotOffset, }) {
554
571
  const { swapInputs, initialInputs } = await getAdjustLeverageSwapInputs({
555
572
  owner,
556
573
  kaminoMarket,
@@ -587,22 +604,24 @@ async function getAdjustLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTo
587
604
  depositSwapper = swapper;
588
605
  }
589
606
  const swapsArray = await depositSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
590
- return Promise.all(swapsArray.map(async (swap) => {
591
- const ixs = await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, {
607
+ const increaseLeverageIxs = await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, swapsArray.map((swap) => {
608
+ return {
592
609
  preActionIxs: [],
593
610
  swapIxs: swap.swapIxs,
594
611
  lookupTables: swap.lookupTables,
595
612
  quote: swap.quote,
596
- }, budgetAndPriorityFeeIxs, useV2Ixs);
613
+ };
614
+ }), budgetAndPriorityFeeIxs, useV2Ixs);
615
+ return increaseLeverageIxs.map((ixs, index) => {
597
616
  return {
598
617
  ixs: ixs.instructions,
599
618
  flashLoanInfo: ixs.flashLoanInfo,
600
- lookupTables: swap.lookupTables,
619
+ lookupTables: swapsArray[index].lookupTables,
601
620
  swapInputs,
602
621
  initialInputs,
603
- quote: swap.quote.quoteResponse,
622
+ quote: swapsArray[index].quote.quoteResponse,
604
623
  };
605
- }));
624
+ });
606
625
  }
607
626
  else {
608
627
  console.log('Decreasing leverage');
@@ -618,28 +637,30 @@ async function getAdjustLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTo
618
637
  }
619
638
  // 5. Get swap ixs
620
639
  const swapsArray = await withdrawSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
621
- return Promise.all(swapsArray.map(async (swap) => {
622
- const ixs = await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, {
640
+ const decreaseLeverageIxs = await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, swapsArray.map((swap) => {
641
+ return {
623
642
  preActionIxs: [],
624
643
  swapIxs: swap.swapIxs,
625
644
  lookupTables: swap.lookupTables,
626
645
  quote: swap.quote,
627
- }, budgetAndPriorityFeeIxs, useV2Ixs);
646
+ };
647
+ }), budgetAndPriorityFeeIxs, useV2Ixs, withdrawSlotOffset);
648
+ return decreaseLeverageIxs.map((ixs, index) => {
628
649
  return {
629
650
  ixs: ixs.instructions,
630
651
  flashLoanInfo: ixs.flashLoanInfo,
631
- lookupTables: swap.lookupTables,
652
+ lookupTables: swapsArray[index].lookupTables,
632
653
  swapInputs,
633
654
  initialInputs,
634
- quote: swap.quote.quoteResponse,
655
+ quote: swapsArray[index].quote.quoteResponse,
635
656
  };
636
- }));
657
+ });
637
658
  }
638
659
  }
639
660
  /**
640
661
  * Deposit and borrow tokens if leverage increased
641
662
  */
642
- async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxs, useV2Ixs) {
663
+ async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxsArray, budgetAndPriorityFeeIxs, useV2Ixs) {
643
664
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
644
665
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
645
666
  const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
@@ -664,32 +685,34 @@ async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
664
685
  // 4. Borrow tokens in borrow token reserve that will be swapped to repay flash loan
665
686
  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)
666
687
  referrer, currentSlot);
667
- const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
668
- const ixs = [
669
- ...scopeRefreshIx,
670
- ...budgetIxs,
671
- ...createAtasIxs,
672
- ...[flashBorrowIx],
673
- ...(collIsKtoken ? swapInstructions : []),
674
- ...classes_1.KaminoAction.actionToIxs(depositAction),
675
- ...classes_1.KaminoAction.actionToIxs(borrowAction),
676
- ...(collIsKtoken ? [] : swapInstructions),
677
- ...[flashRepayIx],
678
- ];
679
- const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
680
- const res = {
681
- flashLoanInfo: {
682
- flashBorrowReserve: flashBorrowReserve.address,
683
- flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
684
- },
685
- instructions: ixs,
686
- };
687
- return res;
688
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
689
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
690
+ const ixs = [
691
+ ...scopeRefreshIx,
692
+ ...budgetIxs,
693
+ ...createAtasIxs,
694
+ ...[flashBorrowIx],
695
+ ...(collIsKtoken ? swapInstructions : []),
696
+ ...classes_1.KaminoAction.actionToIxs(depositAction),
697
+ ...classes_1.KaminoAction.actionToIxs(borrowAction),
698
+ ...(collIsKtoken ? [] : swapInstructions),
699
+ ...[flashRepayIx],
700
+ ];
701
+ const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
702
+ const res = {
703
+ flashLoanInfo: {
704
+ flashBorrowReserve: flashBorrowReserve.address,
705
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
706
+ },
707
+ instructions: ixs,
708
+ };
709
+ return res;
710
+ });
688
711
  }
689
712
  /**
690
713
  * Withdraw and repay tokens if leverage decreased
691
714
  */
692
- async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxs, useV2Ixs) {
715
+ async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxsArray, budgetAndPriorityFeeIxs, useV2Ixs, withdrawSlotOffset = exports.WITHDRAW_SLOT_OFFSET) {
693
716
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
694
717
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
695
718
  const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
@@ -723,30 +746,33 @@ async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
723
746
  // 4. Actually do the repay of the flash borrowed amounts
724
747
  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)
725
748
  referrer);
749
+ const withdrawSlot = currentSlot - withdrawSlotOffset;
726
750
  // 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
727
751
  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)
728
- referrer, currentSlot);
729
- const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
730
- const ixs = [
731
- ...scopeRefreshIx,
732
- ...budgetIxs,
733
- ...createAtasIxs,
734
- ...fillWsolAtaIxs,
735
- ...[flashBorrowIx],
736
- ...classes_1.KaminoAction.actionToIxs(repayAction),
737
- ...classes_1.KaminoAction.actionToIxs(withdrawAction),
738
- ...swapInstructions,
739
- ...[flashRepayIx],
740
- ...closeWsolAtaIxs,
741
- ];
742
- const res = {
743
- flashLoanInfo: {
744
- flashBorrowReserve: debtReserve.address,
745
- flashLoanFee: debtReserve.getFlashLoanFee(),
746
- },
747
- instructions: ixs,
748
- };
749
- return res;
752
+ referrer, withdrawSlot);
753
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
754
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
755
+ const ixs = [
756
+ ...scopeRefreshIx,
757
+ ...budgetIxs,
758
+ ...createAtasIxs,
759
+ ...fillWsolAtaIxs,
760
+ ...[flashBorrowIx],
761
+ ...classes_1.KaminoAction.actionToIxs(repayAction),
762
+ ...classes_1.KaminoAction.actionToIxs(withdrawAction),
763
+ ...swapInstructions,
764
+ ...[flashRepayIx],
765
+ ...closeWsolAtaIxs,
766
+ ];
767
+ const res = {
768
+ flashLoanInfo: {
769
+ flashBorrowReserve: debtReserve.address,
770
+ flashLoanFee: debtReserve.getFlashLoanFee(),
771
+ },
772
+ instructions: ixs,
773
+ };
774
+ return res;
775
+ });
750
776
  }
751
777
  const getSetupIxs = async (owner, kaminoMarket, obligation, collTokenMint, collReserve, collIsKtoken, debtTokenMint, debtReserve, strategy, scopeRefreshConfig, budgetAndPriorityFeeIxs) => {
752
778
  const budgetIxs = budgetAndPriorityFeeIxs || (0, utils_1.getComputeBudgetAndPriorityFeeIxs)(3000000);
@@ -769,12 +795,18 @@ const getScopeRefreshIx = async (market, collReserve, debtReserve, obligation, s
769
795
  debtReserve.address,
770
796
  ]).toArray()
771
797
  : new utils_1.PublicKeySet([collReserve.address, debtReserve.address]).toArray();
772
- const tokenIds = (0, classes_1.getTokenIdsForScopeRefresh)(market, allReserves);
773
798
  const scopeRefreshIxs = [];
774
- if (tokenIds.length > 0 && scopeRefreshConfig) {
775
- scopeRefreshIxs.push(await scopeRefreshConfig.scope.refreshPriceListIx({
776
- feed: scopeRefreshConfig.scopeFeed,
777
- }, tokenIds));
799
+ const scopeTokensMap = (0, classes_1.getTokenIdsForScopeRefresh)(market, allReserves);
800
+ if (scopeTokensMap.size > 0 && scopeRefreshConfig) {
801
+ for (const [configPubkey, config] of scopeRefreshConfig.scopeConfigurations) {
802
+ const tokenIds = scopeTokensMap.get(config.oraclePrices);
803
+ if (tokenIds && tokenIds.length > 0) {
804
+ const refreshIx = await scopeRefreshConfig.scope.refreshPriceListIx({ config: configPubkey }, tokenIds);
805
+ if (refreshIx) {
806
+ scopeRefreshIxs.push(refreshIx);
807
+ }
808
+ }
809
+ }
778
810
  }
779
811
  return scopeRefreshIxs;
780
812
  };