@kamino-finance/klend-sdk 7.0.6 → 7.0.8
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.
- package/dist/classes/action.d.ts +1 -1
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +15 -15
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/manager.d.ts +8 -0
- package/dist/classes/manager.d.ts.map +1 -1
- package/dist/classes/manager.js +10 -0
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/obligation.js +7 -6
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/vault.d.ts +8 -0
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +90 -1
- package/dist/classes/vault.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +36 -32
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.js +6 -6
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/operations.d.ts +4 -3
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +175 -149
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +1 -0
- package/dist/leverage/types.d.ts.map +1 -1
- package/dist/manager/client_kamino_manager.js +15 -2
- package/dist/manager/client_kamino_manager.js.map +1 -1
- package/package.json +1 -1
- package/src/classes/action.ts +15 -15
- package/src/classes/manager.ts +24 -1
- package/src/classes/obligation.ts +9 -9
- package/src/classes/vault.ts +100 -1
- package/src/lending_operations/repay_with_collateral_operations.ts +78 -74
- package/src/lending_operations/swap_collateral_operations.ts +15 -13
- package/src/leverage/operations.ts +350 -318
- package/src/leverage/types.ts +1 -0
- package/src/manager/client_kamino_manager.ts +19 -2
|
@@ -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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
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
|
-
|
|
174
|
-
|
|
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
|
-
}
|
|
182
|
+
};
|
|
183
|
+
}), initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs, elevationGroupOverride);
|
|
184
|
+
return depositWithLeverageIxs.map((depositWithLeverageIxs, index) => {
|
|
180
185
|
return {
|
|
181
|
-
ixs:
|
|
182
|
-
flashLoanInfo:
|
|
183
|
-
lookupTables:
|
|
186
|
+
ixs: depositWithLeverageIxs.instructions,
|
|
187
|
+
flashLoanInfo: depositWithLeverageIxs.flashLoanInfo,
|
|
188
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
184
189
|
swapInputs,
|
|
185
190
|
initialInputs,
|
|
186
|
-
quote:
|
|
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,
|
|
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
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
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
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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
|
-
|
|
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
|
-
|
|
356
|
-
|
|
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
|
-
}
|
|
362
|
-
|
|
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:
|
|
377
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
367
378
|
swapInputs,
|
|
368
379
|
initialInputs: initialInputs,
|
|
369
|
-
quote:
|
|
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,
|
|
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
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
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
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
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
|
-
|
|
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
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
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
|
-
|
|
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
|
-
|
|
608
|
-
|
|
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
|
-
}
|
|
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:
|
|
636
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
618
637
|
swapInputs,
|
|
619
638
|
initialInputs,
|
|
620
|
-
quote:
|
|
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
|
-
|
|
639
|
-
|
|
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
|
-
}
|
|
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:
|
|
669
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
649
670
|
swapInputs,
|
|
650
671
|
initialInputs,
|
|
651
|
-
quote:
|
|
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,
|
|
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
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
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,
|
|
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,
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
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);
|