@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.
- package/dist/classes/action.d.ts +1 -1
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +32 -16
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/manager.d.ts +29 -18
- package/dist/classes/manager.d.ts.map +1 -1
- package/dist/classes/manager.js +66 -49
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/market.d.ts +12 -11
- package/dist/classes/market.d.ts.map +1 -1
- package/dist/classes/market.js +77 -37
- package/dist/classes/market.js.map +1 -1
- package/dist/classes/vault.d.ts +5 -3
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +8 -6
- package/dist/classes/vault.js.map +1 -1
- package/dist/client_kamino_manager.d.ts.map +1 -1
- package/dist/client_kamino_manager.js +30 -22
- package/dist/client_kamino_manager.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 +4 -4
- 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 +186 -154
- 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/utils/managerTypes.d.ts +1 -2
- package/dist/utils/managerTypes.d.ts.map +1 -1
- package/dist/utils/managerTypes.js +9 -9
- package/dist/utils/managerTypes.js.map +1 -1
- package/dist/utils/obligations.d.ts +5 -0
- package/dist/utils/obligations.d.ts.map +1 -0
- package/dist/utils/obligations.js +53 -0
- package/dist/utils/obligations.js.map +1 -0
- package/dist/utils/oracle.d.ts +3 -3
- package/dist/utils/oracle.d.ts.map +1 -1
- package/dist/utils/oracle.js +2 -2
- package/dist/utils/oracle.js.map +1 -1
- package/dist/utils/pubkey.d.ts +1 -0
- package/dist/utils/pubkey.d.ts.map +1 -1
- package/dist/utils/pubkey.js +10 -0
- package/dist/utils/pubkey.js.map +1 -1
- package/package.json +3 -3
- package/src/classes/action.ts +32 -20
- package/src/classes/manager.ts +87 -53
- package/src/classes/market.ts +132 -52
- package/src/classes/vault.ts +17 -6
- package/src/client.ts +4 -4
- package/src/client_kamino_manager.ts +40 -35
- package/src/lending_operations/repay_with_collateral_operations.ts +76 -72
- package/src/lending_operations/swap_collateral_operations.ts +13 -11
- package/src/leverage/operations.ts +362 -328
- package/src/leverage/types.ts +1 -0
- package/src/utils/managerTypes.ts +1 -2
- package/src/utils/obligations.ts +69 -0
- package/src/utils/oracle.ts +5 -4
- 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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
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
|
-
|
|
171
|
-
|
|
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
|
-
}
|
|
179
|
+
};
|
|
180
|
+
}), initialInputs.strategy, initialInputs.collIsKtoken, useV2Ixs, elevationGroupOverride);
|
|
181
|
+
return depositWithLeverageIxs.map((depositWithLeverageIxs, index) => {
|
|
177
182
|
return {
|
|
178
|
-
ixs:
|
|
179
|
-
flashLoanInfo:
|
|
180
|
-
lookupTables:
|
|
183
|
+
ixs: depositWithLeverageIxs.instructions,
|
|
184
|
+
flashLoanInfo: depositWithLeverageIxs.flashLoanInfo,
|
|
185
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
181
186
|
swapInputs,
|
|
182
187
|
initialInputs,
|
|
183
|
-
quote:
|
|
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,
|
|
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
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
|
|
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
|
-
|
|
343
|
-
|
|
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
|
-
}
|
|
349
|
-
|
|
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:
|
|
364
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
354
365
|
swapInputs,
|
|
355
366
|
initialInputs: initialInputs,
|
|
356
|
-
quote:
|
|
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,
|
|
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
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
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
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
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
|
-
|
|
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
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
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
|
-
|
|
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
|
-
|
|
591
|
-
|
|
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
|
-
}
|
|
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:
|
|
619
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
601
620
|
swapInputs,
|
|
602
621
|
initialInputs,
|
|
603
|
-
quote:
|
|
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
|
-
|
|
622
|
-
|
|
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
|
-
}
|
|
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:
|
|
652
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
632
653
|
swapInputs,
|
|
633
654
|
initialInputs,
|
|
634
|
-
quote:
|
|
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,
|
|
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
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
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,
|
|
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,
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
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
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
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
|
};
|