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

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.
@@ -1 +1 @@
1
- {"version":3,"file":"operations.d.ts","sourceRoot":"","sources":["../../src/leverage/operations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,SAAS,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAEtF,OAAO,EAEL,YAAY,EACZ,gBAAgB,EAChB,aAAa,EAKd,MAAM,YAAY,CAAC;AAIpB,OAAO,EAGL,cAAc,EAId,uBAAuB,EASxB,MAAM,UAAU,CAAC;AAelB,OAAO,EAAU,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAG7E,OAAO,EAEL,2BAA2B,EAC3B,yBAAyB,EACzB,mBAAmB,EACnB,6BAA6B,EAE7B,4BAA4B,EAC5B,wBAAwB,EACxB,kCAAkC,EAClC,0BAA0B,EAE1B,UAAU,EACV,OAAO,EAEP,2BAA2B,EAC3B,6BAA6B,EAC7B,2BAA2B,EAC3B,yBAAyB,EACzB,mCAAmC,EACnC,iBAAiB,EACjB,aAAa,EACd,MAAM,SAAS,CAAC;AAEjB,wBAAsB,gCAAgC,CAAC,aAAa,EAAE,EACpE,KAAK,EACL,YAAY,EACZ,aAAa,EACb,aAAa,EACb,aAAa,EACb,eAAe,EACf,WAAW,EACX,UAAU,EACV,QAAQ,EACR,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,MAAM,EACN,yBAAyB,EACzB,kBAAkB,EAClB,uBAAuB,EACvB,cAAc,EACd,SAAS,EACT,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,sBAAsB,GACvB,EAAE,kCAAkC,CAAC,aAAa,CAAC,GAAG,OAAO,CAAC;IAC7D,aAAa,EAAE,aAAa,CAAC;IAC7B,UAAU,EAAE,UAAU,CAAC;IACvB,aAAa,EAAE,4BAA4B,CAAC,aAAa,CAAC,CAAC;CAC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AAiB,EACjB,gBAAgB,EAChB,QAAQ,EACR,WAAW,EACX,cAAc,EACd,eAAe,EACf,eAAe,EACf,WAAW,EACX,uBAAuB,EACvB,MAAM,EACN,kBAAkB,EAClB,cAAc,EACd,SAAS,EACT,QAAQ,EACR,MAAM,EACN,OAAO,EACP,QAAQ,GACT,EAAE,mBAAmB,CAAC,aAAa,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,yBAAyB,CAAC,aAAa,CAAC,CAAC,CAAC,CAqI/F;AAwQD,eAAO,MAAM,WAAW,UACf,SAAS,gBACF,YAAY,cACd,gBAAgB,GAAG,cAAc,GAAG,SAAS,iBAC1C,SAAS,eACX,aAAa,gBACZ,OAAO,iBACN,SAAS,eACX,aAAa,YAChB,mBAAmB,GAAG,SAAS,sBACrB,uBAAuB,GAAG,SAAS,2BAC9B,sBAAsB,EAAE,GAAG,SAAS;;;;EA4B9D,CAAC;AAEF,eAAO,MAAM,iBAAiB,WACpB,YAAY,eACP,aAAa,eACb,aAAa,cACd,gBAAgB,GAAG,cAAc,GAAG,SAAS,sBACrC,uBAAuB,GAAG,SAAS,KACtD,OAAO,CAAC,sBAAsB,EAAE,CAyBlC,CAAC"}
1
+ {"version":3,"file":"operations.d.ts","sourceRoot":"","sources":["../../src/leverage/operations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,SAAS,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAEtF,OAAO,EAEL,YAAY,EACZ,gBAAgB,EAChB,aAAa,EAKd,MAAM,YAAY,CAAC;AAIpB,OAAO,EAGL,cAAc,EAId,uBAAuB,EASxB,MAAM,UAAU,CAAC;AAelB,OAAO,EAAU,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAG7E,OAAO,EAEL,2BAA2B,EAC3B,yBAAyB,EACzB,mBAAmB,EACnB,6BAA6B,EAE7B,4BAA4B,EAC5B,wBAAwB,EACxB,kCAAkC,EAClC,0BAA0B,EAE1B,UAAU,EACV,OAAO,EAEP,2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,cACd,gBAAgB,GAAG,cAAc,GAAG,SAAS,sBACrC,uBAAuB,GAAG,SAAS,KACtD,OAAO,CAAC,sBAAsB,EAAE,CAyBlC,CAAC"}
@@ -34,15 +34,17 @@ async function getDepositWithLeverageSwapInputs({ owner, kaminoMarket, debtToken
34
34
  console.log('Ops Calcs', (0, classes_1.toJson)(calcs));
35
35
  const obligationType = checkObligationType(obligationTypeTagOverride, collTokenMint, debtTokenMint, kaminoMarket);
36
36
  // 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,
37
+ const klendIxs = (await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation ? obligation : obligationType, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, [
38
+ {
39
+ preActionIxs: [],
40
+ swapIxs: [],
41
+ lookupTables: [],
42
+ quote: {
43
+ priceAInB: new decimal_js_1.default(0), // not used
44
+ quoteResponse: undefined,
45
+ },
44
46
  },
45
- }, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride);
47
+ ], strategy, collIsKtoken, useV2Ixs, elevationGroupOverride))[0];
46
48
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
47
49
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.swapDebtTokenIn : calcs.singleSidedDepositKtokenOnly, debtReserve.stats.decimals).ceil();
48
50
  const swapInputsForQuote = {
@@ -167,24 +169,26 @@ async function getDepositWithLeverageIxs({ owner, kaminoMarket, debtTokenMint, c
167
169
  const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
168
170
  const solTokenReserve = kaminoMarket.getReserveByMint(spl_token_1.NATIVE_MINT);
169
171
  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, {
172
+ const depositWithLeverageIxs = await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, initialInputs.obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, swapsArray.map((swap) => {
173
+ return {
172
174
  preActionIxs: [],
173
175
  swapIxs: swap.swapIxs,
174
176
  lookupTables: swap.lookupTables,
175
177
  quote: swap.quote,
176
- }, initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs, elevationGroupOverride);
178
+ };
179
+ }), initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs, elevationGroupOverride);
180
+ return depositWithLeverageIxs.map((depositWithLeverageIxs, index) => {
177
181
  return {
178
- ixs: ixs.instructions,
179
- flashLoanInfo: ixs.flashLoanInfo,
180
- lookupTables: swap.lookupTables,
182
+ ixs: depositWithLeverageIxs.instructions,
183
+ flashLoanInfo: depositWithLeverageIxs.flashLoanInfo,
184
+ lookupTables: swapsArray[index].lookupTables,
181
185
  swapInputs,
182
186
  initialInputs,
183
- quote: swap.quote.quoteResponse,
187
+ quote: swapsArray[index].quote.quoteResponse,
184
188
  };
185
- }));
189
+ });
186
190
  }
187
- async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxs, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride) {
191
+ async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxsArray, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride) {
188
192
  const collTokenMint = collReserve.getLiquidityMint();
189
193
  const debtTokenMint = debtReserve.getLiquidityMint();
190
194
  const collTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collTokenMint, owner, false, collReserve.getLiquidityTokenProgram());
@@ -218,28 +222,30 @@ async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, own
218
222
  .toString(), debtTokenMint, owner, obligation, useV2Ixs, undefined, 0, false, elevationGroupOverride === 0 ? false : true, // emode
219
223
  { skipInitialization: true, skipLutCreation: true }, // to be checked and created in a setup tx in the UI
220
224
  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
- };
225
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
226
+ // 4. Swap
227
+ const { swapIxs } = swapQuoteIxs;
228
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapIxs);
229
+ const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
230
+ const flashLoanInfo = {
231
+ flashBorrowReserve: flashBorrowReserve.address,
232
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
233
+ };
234
+ return {
235
+ flashLoanInfo,
236
+ instructions: [
237
+ ...scopeRefreshIx,
238
+ ...budgetIxs,
239
+ ...createAtasIxs,
240
+ ...fillWsolAtaIxs,
241
+ ...[flashBorrowIx],
242
+ ...(collIsKtoken ? swapInstructions : []),
243
+ ...classes_1.KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
244
+ ...(collIsKtoken ? [] : swapInstructions),
245
+ ...[flashRepayIx],
246
+ ],
247
+ };
248
+ });
243
249
  }
244
250
  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
251
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
@@ -250,15 +256,17 @@ async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtToke
250
256
  const strategy = collIsKtoken ? (await kamino.getStrategyByKTokenMint(collTokenMint)) : undefined;
251
257
  const inputTokenIsSol = selectedTokenMint.equals(spl_token_1.NATIVE_MINT);
252
258
  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,
259
+ const klendIxs = (await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, [
260
+ {
261
+ preActionIxs: [],
262
+ swapIxs: [],
263
+ lookupTables: [],
264
+ quote: {
265
+ priceAInB: new decimal_js_1.default(0), // not used
266
+ quoteResponse: undefined,
267
+ },
260
268
  },
261
- }, strategy, collIsKtoken, useV2Ixs);
269
+ ], strategy, collIsKtoken, useV2Ixs))[0];
262
270
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
263
271
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(calcs.collTokenSwapIn, collReserve.getMintDecimals()).ceil();
264
272
  const swapInputsForQuote = {
@@ -339,25 +347,27 @@ async function getWithdrawWithLeverageIxs({ owner, kaminoMarket, debtTokenMint,
339
347
  console.log('Strategy lookup table not found');
340
348
  }
341
349
  }
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, {
350
+ const withdrawWithLeverageIxs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, swapsArray.map((swap) => {
351
+ return {
344
352
  preActionIxs: [],
345
353
  swapIxs: swap.swapIxs,
346
354
  lookupTables: swap.lookupTables,
347
355
  quote: swap.quote,
348
- }, initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs);
349
- // Send ixs and lookup tables
356
+ };
357
+ }), initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs);
358
+ // Send ixs and lookup tables
359
+ return withdrawWithLeverageIxs.map((ixs, index) => {
350
360
  return {
351
361
  ixs: ixs.instructions,
352
362
  flashLoanInfo: ixs.flashLoanInfo,
353
- lookupTables: swap.lookupTables,
363
+ lookupTables: swapsArray[index].lookupTables,
354
364
  swapInputs,
355
365
  initialInputs: initialInputs,
356
- quote: swap.quote.quoteResponse,
366
+ quote: swapsArray[index].quote.quoteResponse,
357
367
  };
358
- }));
368
+ });
359
369
  }
360
- async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxs, strategy, collIsKtoken, useV2Ixs) {
370
+ async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxsArray, strategy, collIsKtoken, useV2Ixs) {
361
371
  const collTokenMint = collReserve.getLiquidityMint();
362
372
  const debtTokenMint = debtReserve.getLiquidityMint();
363
373
  const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
@@ -397,24 +407,26 @@ async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, ow
397
407
  ? utils_1.U64_MAX
398
408
  : (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
409
  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
- };
410
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
411
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
412
+ return {
413
+ flashLoanInfo: {
414
+ flashLoanFee: debtReserve.getFlashLoanFee(),
415
+ flashBorrowReserve: debtReserve.address,
416
+ },
417
+ instructions: [
418
+ ...scopeRefreshIx,
419
+ ...budgetIxs,
420
+ ...createAtasIxs,
421
+ ...fillWsolAtaIxs,
422
+ ...[flashBorrowIx],
423
+ ...classes_1.KaminoAction.actionToIxs(repayAndWithdrawAction),
424
+ ...swapInstructions,
425
+ ...[flashRepayIx],
426
+ ...closeWsolAtaIxs,
427
+ ],
428
+ };
429
+ });
418
430
  }
419
431
  async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, isKtoken, quoter, useV2Ixs, }) {
420
432
  const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
@@ -447,15 +459,17 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
447
459
  if (isDeposit) {
448
460
  const calcs = await (0, calcs_1.adjustDepositLeverageCalcs)(kaminoMarket, owner, debtReserve, adjustDepositPosition, adjustBorrowPosition, priceDebtToColl, flashLoanFee, slippagePct, collIsKtoken);
449
461
  // 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,
462
+ const klendIxs = (await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, [
463
+ {
464
+ preActionIxs: [],
465
+ swapIxs: [],
466
+ lookupTables: [],
467
+ quote: {
468
+ priceAInB: new decimal_js_1.default(0), // not used
469
+ quoteResponse: undefined,
470
+ },
457
471
  },
458
- }, budgetAndPriorityFeeIxs, useV2Ixs);
472
+ ], budgetAndPriorityFeeIxs, useV2Ixs))[0];
459
473
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
460
474
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.borrowAmount : calcs.amountToFlashBorrowDebt, debtReserve.stats.decimals).ceil();
461
475
  const swapInputsForQuote = {
@@ -501,15 +515,17 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
501
515
  }
502
516
  else {
503
517
  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,
518
+ const klendIxs = (await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, [
519
+ {
520
+ preActionIxs: [],
521
+ swapIxs: [],
522
+ lookupTables: [],
523
+ quote: {
524
+ priceAInB: new decimal_js_1.default(0), // not used
525
+ quoteResponse: undefined,
526
+ },
511
527
  },
512
- }, budgetAndPriorityFeeIxs, useV2Ixs);
528
+ ], budgetAndPriorityFeeIxs, useV2Ixs))[0];
513
529
  const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
514
530
  const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(calcs.withdrawAmountWithSlippageAndFlashLoanFee, collReserve.state.liquidity.mintDecimals.toNumber()).ceil();
515
531
  const swapInputsForQuote = {
@@ -587,22 +603,24 @@ async function getAdjustLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTo
587
603
  depositSwapper = swapper;
588
604
  }
589
605
  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, {
606
+ const increaseLeverageIxs = await buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, swapsArray.map((swap) => {
607
+ return {
592
608
  preActionIxs: [],
593
609
  swapIxs: swap.swapIxs,
594
610
  lookupTables: swap.lookupTables,
595
611
  quote: swap.quote,
596
- }, budgetAndPriorityFeeIxs, useV2Ixs);
612
+ };
613
+ }), budgetAndPriorityFeeIxs, useV2Ixs);
614
+ return increaseLeverageIxs.map((ixs, index) => {
597
615
  return {
598
616
  ixs: ixs.instructions,
599
617
  flashLoanInfo: ixs.flashLoanInfo,
600
- lookupTables: swap.lookupTables,
618
+ lookupTables: swapsArray[index].lookupTables,
601
619
  swapInputs,
602
620
  initialInputs,
603
- quote: swap.quote.quoteResponse,
621
+ quote: swapsArray[index].quote.quoteResponse,
604
622
  };
605
- }));
623
+ });
606
624
  }
607
625
  else {
608
626
  console.log('Decreasing leverage');
@@ -618,28 +636,30 @@ async function getAdjustLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTo
618
636
  }
619
637
  // 5. Get swap ixs
620
638
  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, {
639
+ const decreaseLeverageIxs = await buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeRefreshConfig, initialInputs.collIsKtoken, swapsArray.map((swap) => {
640
+ return {
623
641
  preActionIxs: [],
624
642
  swapIxs: swap.swapIxs,
625
643
  lookupTables: swap.lookupTables,
626
644
  quote: swap.quote,
627
- }, budgetAndPriorityFeeIxs, useV2Ixs);
645
+ };
646
+ }), budgetAndPriorityFeeIxs, useV2Ixs);
647
+ return decreaseLeverageIxs.map((ixs, index) => {
628
648
  return {
629
649
  ixs: ixs.instructions,
630
650
  flashLoanInfo: ixs.flashLoanInfo,
631
- lookupTables: swap.lookupTables,
651
+ lookupTables: swapsArray[index].lookupTables,
632
652
  swapInputs,
633
653
  initialInputs,
634
- quote: swap.quote.quoteResponse,
654
+ quote: swapsArray[index].quote.quoteResponse,
635
655
  };
636
- }));
656
+ });
637
657
  }
638
658
  }
639
659
  /**
640
660
  * Deposit and borrow tokens if leverage increased
641
661
  */
642
- async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxs, useV2Ixs) {
662
+ async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxsArray, budgetAndPriorityFeeIxs, useV2Ixs) {
643
663
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
644
664
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
645
665
  const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
@@ -664,32 +684,34 @@ async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
664
684
  // 4. Borrow tokens in borrow token reserve that will be swapped to repay flash loan
665
685
  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
686
  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;
687
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
688
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
689
+ const ixs = [
690
+ ...scopeRefreshIx,
691
+ ...budgetIxs,
692
+ ...createAtasIxs,
693
+ ...[flashBorrowIx],
694
+ ...(collIsKtoken ? swapInstructions : []),
695
+ ...classes_1.KaminoAction.actionToIxs(depositAction),
696
+ ...classes_1.KaminoAction.actionToIxs(borrowAction),
697
+ ...(collIsKtoken ? [] : swapInstructions),
698
+ ...[flashRepayIx],
699
+ ];
700
+ const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
701
+ const res = {
702
+ flashLoanInfo: {
703
+ flashBorrowReserve: flashBorrowReserve.address,
704
+ flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
705
+ },
706
+ instructions: ixs,
707
+ };
708
+ return res;
709
+ });
688
710
  }
689
711
  /**
690
712
  * Withdraw and repay tokens if leverage decreased
691
713
  */
692
- async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxs, useV2Ixs) {
714
+ async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxsArray, budgetAndPriorityFeeIxs, useV2Ixs) {
693
715
  const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
694
716
  const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
695
717
  const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
@@ -726,27 +748,29 @@ async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
726
748
  // 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
727
749
  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
750
  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;
751
+ return swapQuoteIxsArray.map((swapQuoteIxs) => {
752
+ const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
753
+ const ixs = [
754
+ ...scopeRefreshIx,
755
+ ...budgetIxs,
756
+ ...createAtasIxs,
757
+ ...fillWsolAtaIxs,
758
+ ...[flashBorrowIx],
759
+ ...classes_1.KaminoAction.actionToIxs(repayAction),
760
+ ...classes_1.KaminoAction.actionToIxs(withdrawAction),
761
+ ...swapInstructions,
762
+ ...[flashRepayIx],
763
+ ...closeWsolAtaIxs,
764
+ ];
765
+ const res = {
766
+ flashLoanInfo: {
767
+ flashBorrowReserve: debtReserve.address,
768
+ flashLoanFee: debtReserve.getFlashLoanFee(),
769
+ },
770
+ instructions: ixs,
771
+ };
772
+ return res;
773
+ });
750
774
  }
751
775
  const getSetupIxs = async (owner, kaminoMarket, obligation, collTokenMint, collReserve, collIsKtoken, debtTokenMint, debtReserve, strategy, scopeRefreshConfig, budgetAndPriorityFeeIxs) => {
752
776
  const budgetIxs = budgetAndPriorityFeeIxs || (0, utils_1.getComputeBudgetAndPriorityFeeIxs)(3000000);