@kamino-finance/klend-sdk 5.13.9 → 5.13.11
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.map +1 -1
- package/dist/classes/action.js +0 -1
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/manager.d.ts +7 -1
- package/dist/classes/manager.d.ts.map +1 -1
- package/dist/classes/manager.js +12 -3
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/reserve.d.ts +4 -0
- package/dist/classes/reserve.d.ts.map +1 -1
- package/dist/classes/reserve.js +8 -2
- package/dist/classes/reserve.js.map +1 -1
- package/dist/classes/vault.d.ts +8 -1
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +31 -2
- package/dist/classes/vault.js.map +1 -1
- package/dist/client_kamino_manager.d.ts.map +1 -1
- package/dist/client_kamino_manager.js +10 -0
- package/dist/client_kamino_manager.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.js +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +5 -5
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.js +9 -9
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/calcs.d.ts +8 -8
- package/dist/leverage/calcs.d.ts.map +1 -1
- package/dist/leverage/calcs.js +25 -25
- package/dist/leverage/calcs.js.map +1 -1
- package/dist/leverage/instructions.d.ts +5 -5
- package/dist/leverage/instructions.d.ts.map +1 -1
- package/dist/leverage/instructions.js +6 -6
- package/dist/leverage/instructions.js.map +1 -1
- package/dist/leverage/operations.d.ts +5 -0
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +131 -304
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +35 -57
- package/dist/leverage/types.d.ts.map +1 -1
- package/dist/leverage/utils.d.ts +1 -1
- package/dist/leverage/utils.js +5 -5
- package/dist/leverage/utils.js.map +1 -1
- package/dist/utils/ata.d.ts +0 -1
- package/dist/utils/ata.d.ts.map +1 -1
- package/dist/utils/ata.js +0 -14
- package/dist/utils/ata.js.map +1 -1
- package/dist/utils/instruction.d.ts +1 -0
- package/dist/utils/instruction.d.ts.map +1 -1
- package/dist/utils/instruction.js +10 -0
- package/dist/utils/instruction.js.map +1 -1
- package/dist/utils/vault.d.ts +3 -0
- package/dist/utils/vault.d.ts.map +1 -0
- package/dist/utils/vault.js +50 -0
- package/dist/utils/vault.js.map +1 -0
- package/package.json +1 -1
- package/src/classes/action.ts +0 -1
- package/src/classes/manager.ts +13 -3
- package/src/classes/reserve.ts +9 -2
- package/src/classes/vault.ts +34 -2
- package/src/client_kamino_manager.ts +12 -0
- package/src/idl_codegen_kamino_vault/accounts/VaultState.ts +1 -1
- package/src/lending_operations/repay_with_collateral_operations.ts +6 -6
- package/src/lending_operations/swap_collateral_operations.ts +22 -24
- package/src/leverage/calcs.ts +26 -26
- package/src/leverage/instructions.ts +8 -8
- package/src/leverage/operations.ts +232 -325
- package/src/leverage/types.ts +38 -67
- package/src/leverage/utils.ts +5 -5
- package/src/utils/ata.ts +1 -26
- package/src/utils/instruction.ts +12 -0
- package/src/utils/vault.ts +51 -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 = void 0;
|
|
6
|
+
exports.getScopeRefreshIx = exports.getSetupIxs = void 0;
|
|
7
7
|
exports.getDepositWithLeverageSwapInputs = getDepositWithLeverageSwapInputs;
|
|
8
8
|
exports.getDepositWithLeverageIxs = getDepositWithLeverageIxs;
|
|
9
9
|
exports.getWithdrawWithLeverageSwapInputs = getWithdrawWithLeverageSwapInputs;
|
|
@@ -32,18 +32,7 @@ async function getDepositWithLeverageSwapInputs({ owner, kaminoMarket, debtToken
|
|
|
32
32
|
const strategy = collIsKtoken ? (await kamino.getStrategyByKTokenMint(collTokenMint)) : undefined;
|
|
33
33
|
const calcs = await getDepositWithLeverageCalcs(depositAmount, selectedTokenIsCollToken, collIsKtoken, depositTokenIsSol, priceDebtToColl, targetLeverage, slippagePct, flashLoanFee, kamino, strategy, debtTokenMint, priceAinB, debtReserve);
|
|
34
34
|
console.log('Ops Calcs', (0, classes_1.toJson)(calcs));
|
|
35
|
-
|
|
36
|
-
if (obligationTypeTagOverride === utils_1.ObligationTypeTag.Multiply) {
|
|
37
|
-
// multiply
|
|
38
|
-
obligationType = new utils_1.MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
39
|
-
}
|
|
40
|
-
else if (obligationTypeTagOverride === utils_1.ObligationTypeTag.Leverage) {
|
|
41
|
-
// leverage
|
|
42
|
-
obligationType = new utils_1.LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
throw Error('Obligation type tag not supported for leverage, please use 1 - multiply or 3 - leverage');
|
|
46
|
-
}
|
|
35
|
+
const obligationType = checkObligationType(obligationTypeTagOverride, collTokenMint, debtTokenMint, kaminoMarket);
|
|
47
36
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
48
37
|
const klendIxs = await buildDepositWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation ? obligation : obligationType, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, {
|
|
49
38
|
preActionIxs: [],
|
|
@@ -186,69 +175,20 @@ async function getDepositWithLeverageIxs({ owner, kaminoMarket, debtTokenMint, c
|
|
|
186
175
|
};
|
|
187
176
|
}
|
|
188
177
|
async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, depositTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, swapQuoteIxs, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride) {
|
|
189
|
-
const budgetIxs = budgetAndPriorityFeeIxs || (0, utils_1.getComputeBudgetAndPriorityFeeIxs)(3000000);
|
|
190
178
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
191
179
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
192
180
|
const collTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collTokenMint, owner, false, collReserve.getLiquidityTokenProgram());
|
|
193
181
|
const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
|
|
194
182
|
// 1. Create atas & budget ixs
|
|
195
|
-
|
|
196
|
-
if (collIsKtoken) {
|
|
197
|
-
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
198
|
-
? strategy.strategy.tokenBMint
|
|
199
|
-
: strategy.strategy.tokenAMint;
|
|
200
|
-
const secondTokenTokenProgarm = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
201
|
-
? strategy.strategy.tokenBTokenProgram.equals(web3_js_1.PublicKey.default)
|
|
202
|
-
? spl_token_1.TOKEN_PROGRAM_ID
|
|
203
|
-
: strategy.strategy.tokenBTokenProgram
|
|
204
|
-
: strategy.strategy.tokenATokenProgram.equals(web3_js_1.PublicKey.default)
|
|
205
|
-
? spl_token_1.TOKEN_PROGRAM_ID
|
|
206
|
-
: strategy.strategy.tokenATokenProgram;
|
|
207
|
-
mintsToCreateAtas = [
|
|
208
|
-
{
|
|
209
|
-
mint: collTokenMint,
|
|
210
|
-
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
211
|
-
},
|
|
212
|
-
{
|
|
213
|
-
mint: debtTokenMint,
|
|
214
|
-
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
215
|
-
},
|
|
216
|
-
{
|
|
217
|
-
mint: collReserve.getCTokenMint(),
|
|
218
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
mint: secondTokenAta,
|
|
222
|
-
tokenProgram: secondTokenTokenProgarm,
|
|
223
|
-
},
|
|
224
|
-
];
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
mintsToCreateAtas = [
|
|
228
|
-
{
|
|
229
|
-
mint: collTokenMint,
|
|
230
|
-
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
231
|
-
},
|
|
232
|
-
{
|
|
233
|
-
mint: debtTokenMint,
|
|
234
|
-
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
235
|
-
},
|
|
236
|
-
{
|
|
237
|
-
mint: collReserve.getCTokenMint(),
|
|
238
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
239
|
-
},
|
|
240
|
-
];
|
|
241
|
-
}
|
|
242
|
-
const atasAndCreateIxs = (0, utils_1.createAtasIdempotent)(owner, mintsToCreateAtas);
|
|
183
|
+
const { budgetIxs, createAtasIxs, scopeRefreshIx } = await (0, exports.getSetupIxs)(owner, market, obligation, collTokenMint, collReserve, collIsKtoken, debtTokenMint, debtReserve, strategy, scopeRefreshConfig, budgetAndPriorityFeeIxs);
|
|
243
184
|
const fillWsolAtaIxs = [];
|
|
244
185
|
if (depositTokenIsSol) {
|
|
245
186
|
fillWsolAtaIxs.push(...(0, utils_1.getTransferWsolIxs)(owner, (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, owner), (0, classes_2.numberToLamportsDecimal)(calcs.initDepositInSol, utils_1.SOL_DECIMALS).ceil()));
|
|
246
187
|
}
|
|
247
|
-
const scopeRefreshIxn = await (0, exports.getScopeRefreshIx)(market, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
248
188
|
// 2. Flash borrow & repay the collateral amount needed for given leverage
|
|
249
189
|
// if user deposits coll, then we borrow the diff, else we borrow the entire amount
|
|
250
|
-
const {
|
|
251
|
-
|
|
190
|
+
const { flashBorrowIx, flashRepayIx } = (0, instructions_1.getFlashLoanInstructions)({
|
|
191
|
+
borrowIxIndex: budgetIxs.length + createAtasIxs.length + fillWsolAtaIxs.length + (scopeRefreshIx.length > 0 ? 1 : 0),
|
|
252
192
|
walletPublicKey: owner,
|
|
253
193
|
lendingMarketAuthority: market.getLendingMarketAuthority(),
|
|
254
194
|
lendingMarketAddress: market.getAddress(),
|
|
@@ -270,44 +210,26 @@ async function buildDepositWithLeverageIxs(market, debtReserve, collReserve, own
|
|
|
270
210
|
referrer, currentSlot);
|
|
271
211
|
// 4. Swap
|
|
272
212
|
const { swapIxs } = swapQuoteIxs;
|
|
273
|
-
const swapInstructions = (0, utils_1.
|
|
213
|
+
const swapInstructions = (0, utils_1.removeBudgetIxs)(swapIxs);
|
|
274
214
|
const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
|
|
275
215
|
const flashLoanInfo = {
|
|
276
216
|
flashBorrowReserve: flashBorrowReserve.address,
|
|
277
217
|
flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
|
|
278
218
|
};
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
else {
|
|
295
|
-
return {
|
|
296
|
-
flashLoanInfo,
|
|
297
|
-
instructions: [
|
|
298
|
-
...scopeRefreshIxn,
|
|
299
|
-
...budgetIxs,
|
|
300
|
-
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
301
|
-
...fillWsolAtaIxs,
|
|
302
|
-
...[flashBorrowIxn],
|
|
303
|
-
...swapInstructions,
|
|
304
|
-
...kaminoDepositAndBorrowAction.setupIxs,
|
|
305
|
-
...classes_1.KaminoAction.actionToLendingIxs(kaminoDepositAndBorrowAction),
|
|
306
|
-
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
307
|
-
...[flashRepayIxn],
|
|
308
|
-
],
|
|
309
|
-
};
|
|
310
|
-
}
|
|
219
|
+
return {
|
|
220
|
+
flashLoanInfo,
|
|
221
|
+
instructions: [
|
|
222
|
+
...scopeRefreshIx,
|
|
223
|
+
...budgetIxs,
|
|
224
|
+
...createAtasIxs,
|
|
225
|
+
...fillWsolAtaIxs,
|
|
226
|
+
...[flashBorrowIx],
|
|
227
|
+
...(collIsKtoken ? swapInstructions : []),
|
|
228
|
+
...classes_1.KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
|
|
229
|
+
...(collIsKtoken ? [] : swapInstructions),
|
|
230
|
+
...[flashRepayIx],
|
|
231
|
+
],
|
|
232
|
+
};
|
|
311
233
|
}
|
|
312
234
|
async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, deposited, borrowed, obligation, referrer, currentSlot, withdrawAmount, priceCollToDebt, slippagePct, isClosingPosition, selectedTokenMint, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, isKtoken, quoter, useV2Ixs, }) {
|
|
313
235
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
@@ -316,25 +238,24 @@ async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtToke
|
|
|
316
238
|
const selectedTokenIsCollToken = selectedTokenMint.equals(collTokenMint);
|
|
317
239
|
const collIsKtoken = await isKtoken(collTokenMint);
|
|
318
240
|
const strategy = collIsKtoken ? (await kamino.getStrategyByKTokenMint(collTokenMint)) : undefined;
|
|
319
|
-
const
|
|
320
|
-
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve.getLiquidityMint());
|
|
241
|
+
const inputTokenIsSol = selectedTokenMint.equals(spl_token_1.NATIVE_MINT);
|
|
321
242
|
const calcs = (0, calcs_1.withdrawLeverageCalcs)(kaminoMarket, collReserve, debtReserve, priceCollToDebt, withdrawAmount, deposited, borrowed, currentSlot, isClosingPosition, selectedTokenIsCollToken, selectedTokenMint, obligation, flashLoanFee, slippagePct);
|
|
322
|
-
const klendIxs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition,
|
|
243
|
+
const klendIxs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, calcs, budgetAndPriorityFeeIxs, {
|
|
323
244
|
preActionIxs: [],
|
|
324
245
|
swapIxs: [],
|
|
325
246
|
lookupTables: [],
|
|
326
247
|
}, strategy, collIsKtoken, useV2Ixs);
|
|
327
248
|
const uniqueKlendAccounts = (0, utils_1.uniqueAccountsWithProgramIds)(klendIxs.instructions);
|
|
328
|
-
const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(calcs.collTokenSwapIn, collReserve.
|
|
249
|
+
const swapInputAmount = (0, classes_2.numberToLamportsDecimal)(calcs.collTokenSwapIn, collReserve.getMintDecimals()).ceil();
|
|
329
250
|
const swapInputsForQuote = {
|
|
330
251
|
inputAmountLamports: swapInputAmount.mul(new decimal_js_1.default(1).add(quoteBufferBps.div(CreationParameters_1.FullBPS))),
|
|
331
252
|
inputMint: collTokenMint,
|
|
332
253
|
outputMint: debtTokenMint,
|
|
333
|
-
amountDebtAtaBalance:
|
|
254
|
+
amountDebtAtaBalance: undefined, // Only needed for ktokens deposits
|
|
334
255
|
};
|
|
335
256
|
const swapQuote = await quoter(swapInputsForQuote, uniqueKlendAccounts);
|
|
336
257
|
const calcsQuotePrice = (0, calcs_1.withdrawLeverageCalcs)(kaminoMarket, collReserve, debtReserve, !collIsKtoken ? swapQuote.priceAInB : priceCollToDebt, withdrawAmount, deposited, borrowed, currentSlot, isClosingPosition, selectedTokenIsCollToken, selectedTokenMint, obligation, flashLoanFee, slippagePct);
|
|
337
|
-
const swapInputAmountQuotePrice = (0, classes_2.numberToLamportsDecimal)(calcsQuotePrice.collTokenSwapIn, collReserve.
|
|
258
|
+
const swapInputAmountQuotePrice = (0, classes_2.numberToLamportsDecimal)(calcsQuotePrice.collTokenSwapIn, collReserve.getMintDecimals()).ceil();
|
|
338
259
|
return {
|
|
339
260
|
swapInputs: {
|
|
340
261
|
inputAmountLamports: swapInputAmountQuotePrice,
|
|
@@ -358,8 +279,7 @@ async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtToke
|
|
|
358
279
|
async function getWithdrawWithLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, deposited, borrowed, referrer, currentSlot, withdrawAmount, priceCollToDebt, slippagePct, isClosingPosition, selectedTokenMint, budgetAndPriorityFeeIxs, kamino, scopeRefreshConfig, quoteBufferBps, isKtoken, quoter, swapper, useV2Ixs, }) {
|
|
359
280
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
360
281
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
361
|
-
const
|
|
362
|
-
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve.getLiquidityMint());
|
|
282
|
+
const inputTokenIsSol = selectedTokenMint.equals(spl_token_1.NATIVE_MINT);
|
|
363
283
|
const { swapInputs, initialInputs } = await getWithdrawWithLeverageSwapInputs({
|
|
364
284
|
owner,
|
|
365
285
|
kaminoMarket,
|
|
@@ -403,7 +323,7 @@ async function getWithdrawWithLeverageIxs({ owner, kaminoMarket, debtTokenMint,
|
|
|
403
323
|
console.log('Strategy lookup table not found');
|
|
404
324
|
}
|
|
405
325
|
}
|
|
406
|
-
const ixs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition,
|
|
326
|
+
const ixs = await buildWithdrawWithLeverageIxs(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, inputTokenIsSol, scopeRefreshConfig, initialInputs.calcs, budgetAndPriorityFeeIxs, {
|
|
407
327
|
preActionIxs: [],
|
|
408
328
|
swapIxs,
|
|
409
329
|
lookupTables,
|
|
@@ -422,60 +342,12 @@ async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, ow
|
|
|
422
342
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
423
343
|
const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
|
|
424
344
|
// 1. Create atas & budget txns & user metadata
|
|
425
|
-
|
|
426
|
-
if (collIsKtoken) {
|
|
427
|
-
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
428
|
-
? strategy.strategy.tokenBMint
|
|
429
|
-
: strategy.strategy.tokenAMint;
|
|
430
|
-
const secondTokenTokenProgram = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
431
|
-
? strategy.strategy.tokenBTokenProgram.equals(web3_js_1.PublicKey.default)
|
|
432
|
-
? spl_token_1.TOKEN_PROGRAM_ID
|
|
433
|
-
: strategy.strategy.tokenBTokenProgram
|
|
434
|
-
: strategy.strategy.tokenATokenProgram.equals(web3_js_1.PublicKey.default)
|
|
435
|
-
? spl_token_1.TOKEN_PROGRAM_ID
|
|
436
|
-
: strategy.strategy.tokenATokenProgram;
|
|
437
|
-
mintsToCreateAtas = [
|
|
438
|
-
{
|
|
439
|
-
mint: collTokenMint,
|
|
440
|
-
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
441
|
-
},
|
|
442
|
-
{
|
|
443
|
-
mint: debtTokenMint,
|
|
444
|
-
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
445
|
-
},
|
|
446
|
-
{
|
|
447
|
-
mint: collReserve.getCTokenMint(),
|
|
448
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
449
|
-
},
|
|
450
|
-
{
|
|
451
|
-
mint: secondTokenAta,
|
|
452
|
-
tokenProgram: secondTokenTokenProgram,
|
|
453
|
-
},
|
|
454
|
-
];
|
|
455
|
-
}
|
|
456
|
-
else {
|
|
457
|
-
mintsToCreateAtas = [
|
|
458
|
-
{
|
|
459
|
-
mint: collTokenMint,
|
|
460
|
-
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
461
|
-
},
|
|
462
|
-
{
|
|
463
|
-
mint: debtTokenMint,
|
|
464
|
-
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
465
|
-
},
|
|
466
|
-
{
|
|
467
|
-
mint: collReserve.getCTokenMint(),
|
|
468
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
469
|
-
},
|
|
470
|
-
];
|
|
471
|
-
}
|
|
472
|
-
const atasAndCreateIxs = (0, utils_1.createAtasIdempotent)(owner, mintsToCreateAtas);
|
|
345
|
+
const { budgetIxs, createAtasIxs, scopeRefreshIx } = await (0, exports.getSetupIxs)(owner, market, obligation, collTokenMint, collReserve, collIsKtoken, debtTokenMint, debtReserve, strategy, scopeRefreshConfig, budgetAndPriorityFeeIxs);
|
|
473
346
|
const closeWsolAtaIxs = [];
|
|
474
347
|
if (depositTokenIsSol || debtTokenMint.equals(spl_token_1.NATIVE_MINT)) {
|
|
475
348
|
const wsolAta = (0, utils_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, owner, false);
|
|
476
349
|
closeWsolAtaIxs.push((0, spl_token_1.createCloseAccountInstruction)(wsolAta, owner, owner, [], spl_token_1.TOKEN_PROGRAM_ID));
|
|
477
350
|
}
|
|
478
|
-
const budgetIxs = budgetAndPriorityFeeIxs || (0, utils_1.getComputeBudgetAndPriorityFeeIxs)(3000000);
|
|
479
351
|
// TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
|
|
480
352
|
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
481
353
|
const fillWsolAtaIxs = [];
|
|
@@ -484,12 +356,11 @@ async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, ow
|
|
|
484
356
|
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
485
357
|
fillWsolAtaIxs.push(...(0, utils_1.getTransferWsolIxs)(owner, (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, owner), (0, classes_2.numberToLamportsDecimal)(balanceToWrap, utils_1.SOL_DECIMALS).ceil()));
|
|
486
358
|
}
|
|
487
|
-
const scopeRefreshIxn = await (0, exports.getScopeRefreshIx)(market, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
488
359
|
// 2. Prepare the flash borrow and flash repay amounts and ixs
|
|
489
360
|
// We borrow exactly how much we need to repay
|
|
490
361
|
// and repay that + flash amount fee
|
|
491
|
-
const {
|
|
492
|
-
|
|
362
|
+
const { flashBorrowIx, flashRepayIx } = (0, instructions_1.getFlashLoanInstructions)({
|
|
363
|
+
borrowIxIndex: budgetIxs.length + createAtasIxs.length + fillWsolAtaIxs.length + (scopeRefreshIx.length > 0 ? 1 : 0),
|
|
493
364
|
walletPublicKey: owner,
|
|
494
365
|
lendingMarketAuthority: market.getLendingMarketAuthority(),
|
|
495
366
|
lendingMarketAddress: market.getAddress(),
|
|
@@ -506,21 +377,21 @@ async function buildWithdrawWithLeverageIxs(market, debtReserve, collReserve, ow
|
|
|
506
377
|
? utils_1.U64_MAX
|
|
507
378
|
: (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)
|
|
508
379
|
referrer);
|
|
509
|
-
const swapInstructions = (0, utils_1.
|
|
380
|
+
const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
|
|
510
381
|
return {
|
|
511
382
|
flashLoanInfo: {
|
|
512
383
|
flashLoanFee: debtReserve.getFlashLoanFee(),
|
|
513
384
|
flashBorrowReserve: debtReserve.address,
|
|
514
385
|
},
|
|
515
386
|
instructions: [
|
|
516
|
-
...
|
|
387
|
+
...scopeRefreshIx,
|
|
517
388
|
...budgetIxs,
|
|
518
|
-
...
|
|
389
|
+
...createAtasIxs,
|
|
519
390
|
...fillWsolAtaIxs,
|
|
520
|
-
...[
|
|
391
|
+
...[flashBorrowIx],
|
|
521
392
|
...classes_1.KaminoAction.actionToIxs(repayAndWithdrawAction),
|
|
522
393
|
...swapInstructions,
|
|
523
|
-
...[
|
|
394
|
+
...[flashRepayIx],
|
|
524
395
|
...closeWsolAtaIxs,
|
|
525
396
|
],
|
|
526
397
|
};
|
|
@@ -578,10 +449,10 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
578
449
|
flashLoanFee: new decimal_js_1.default(flashLoanFee),
|
|
579
450
|
});
|
|
580
451
|
const calcsQuotePrice = await (0, calcs_1.adjustDepositLeverageCalcs)(kaminoMarket, owner, debtReserve, adjustDepositPositionQuotePrice, adjustBorrowPositionQuotePrice, swapQuote.priceAInB, flashLoanFee, slippagePct, collIsKtoken);
|
|
581
|
-
const swapInputAmountQuotePrice = (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcsQuotePrice.borrowAmount : calcsQuotePrice.amountToFlashBorrowDebt, debtReserve.
|
|
452
|
+
const swapInputAmountQuotePrice = (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcsQuotePrice.borrowAmount : calcsQuotePrice.amountToFlashBorrowDebt, debtReserve.getMintDecimals()).ceil();
|
|
582
453
|
let expectedDebtTokenAtaBalance = new decimal_js_1.default(0);
|
|
583
454
|
if (collIsKtoken) {
|
|
584
|
-
expectedDebtTokenAtaBalance = await (0, utils_2.getExpectedTokenBalanceAfterBorrow)(kaminoMarket.getConnection(), debtTokenMint, owner, (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcsQuotePrice.borrowAmount : calcsQuotePrice.amountToFlashBorrowDebt, debtReserve.stats.decimals).floor(), debtReserve.
|
|
455
|
+
expectedDebtTokenAtaBalance = await (0, utils_2.getExpectedTokenBalanceAfterBorrow)(kaminoMarket.getConnection(), debtTokenMint, owner, (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcsQuotePrice.borrowAmount : calcsQuotePrice.amountToFlashBorrowDebt, debtReserve.stats.decimals).floor(), debtReserve.getMintDecimals());
|
|
585
456
|
}
|
|
586
457
|
return {
|
|
587
458
|
swapInputs: {
|
|
@@ -617,7 +488,7 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
617
488
|
inputAmountLamports: swapInputAmount.mul(new decimal_js_1.default(1).add(quoteBufferBps.div(CreationParameters_1.FullBPS))),
|
|
618
489
|
inputMint: collTokenMint,
|
|
619
490
|
outputMint: debtTokenMint,
|
|
620
|
-
amountDebtAtaBalance:
|
|
491
|
+
amountDebtAtaBalance: undefined, // Only needed for ktokens deposits
|
|
621
492
|
};
|
|
622
493
|
const swapQuote = await quoter(swapInputsForQuote, uniqueKlendAccounts);
|
|
623
494
|
const { adjustDepositPosition: adjustDepositPositionQuotePrice, adjustBorrowPosition: adjustBorrowPositionQuotePrice, } = (0, calcs_1.calcAdjustAmounts)({
|
|
@@ -628,14 +499,14 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
628
499
|
flashLoanFee: new decimal_js_1.default(flashLoanFee),
|
|
629
500
|
});
|
|
630
501
|
const calcsQuotePrice = (0, calcs_1.adjustWithdrawLeverageCalcs)(adjustDepositPositionQuotePrice, adjustBorrowPositionQuotePrice, flashLoanFee, slippagePct);
|
|
631
|
-
const swapInputAmountQuotePrice = (0, classes_2.numberToLamportsDecimal)(calcsQuotePrice.withdrawAmountWithSlippageAndFlashLoanFee, collReserve.
|
|
502
|
+
const swapInputAmountQuotePrice = (0, classes_2.numberToLamportsDecimal)(calcsQuotePrice.withdrawAmountWithSlippageAndFlashLoanFee, collReserve.getMintDecimals()).ceil();
|
|
632
503
|
return {
|
|
633
504
|
swapInputs: {
|
|
634
505
|
inputAmountLamports: swapInputAmountQuotePrice,
|
|
635
506
|
minOutAmountLamports: (0, classes_2.numberToLamportsDecimal)(calcsQuotePrice.adjustBorrowPosition.abs(), debtReserve.stats.decimals),
|
|
636
507
|
inputMint: collTokenMint,
|
|
637
508
|
outputMint: debtTokenMint,
|
|
638
|
-
amountDebtAtaBalance:
|
|
509
|
+
amountDebtAtaBalance: undefined, // Only needed for ktokens deposits
|
|
639
510
|
},
|
|
640
511
|
flashLoanInfo: klendIxs.flashLoanInfo,
|
|
641
512
|
initialInputs: {
|
|
@@ -733,64 +604,15 @@ async function getAdjustLeverageIxs({ owner, kaminoMarket, debtTokenMint, collTo
|
|
|
733
604
|
* Deposit and borrow tokens if leverage increased
|
|
734
605
|
*/
|
|
735
606
|
async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxs, useV2Ixs) {
|
|
736
|
-
const collReserve = kaminoMarket.
|
|
737
|
-
const debtReserve = kaminoMarket.
|
|
607
|
+
const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
|
|
608
|
+
const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
|
|
738
609
|
const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
|
|
739
610
|
const collTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collTokenMint, owner, false, collReserve.getLiquidityTokenProgram());
|
|
740
611
|
// 1. Create atas & budget txns
|
|
741
|
-
const budgetIxs =
|
|
742
|
-
let mintsToCreateAtas;
|
|
743
|
-
if (collIsKtoken) {
|
|
744
|
-
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
745
|
-
? strategy.strategy.tokenBMint
|
|
746
|
-
: strategy.strategy.tokenAMint;
|
|
747
|
-
const secondTokenTokenProgarm = strategy?.strategy.tokenAMint.equals(debtTokenMint)
|
|
748
|
-
? strategy.strategy.tokenBTokenProgram.equals(web3_js_1.PublicKey.default)
|
|
749
|
-
? spl_token_1.TOKEN_PROGRAM_ID
|
|
750
|
-
: strategy.strategy.tokenBTokenProgram
|
|
751
|
-
: strategy.strategy.tokenATokenProgram.equals(web3_js_1.PublicKey.default)
|
|
752
|
-
? spl_token_1.TOKEN_PROGRAM_ID
|
|
753
|
-
: strategy.strategy.tokenATokenProgram;
|
|
754
|
-
mintsToCreateAtas = [
|
|
755
|
-
{
|
|
756
|
-
mint: collTokenMint,
|
|
757
|
-
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
758
|
-
},
|
|
759
|
-
{
|
|
760
|
-
mint: debtTokenMint,
|
|
761
|
-
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
762
|
-
},
|
|
763
|
-
{
|
|
764
|
-
mint: collReserve.getCTokenMint(),
|
|
765
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
766
|
-
},
|
|
767
|
-
{
|
|
768
|
-
mint: secondTokenAta,
|
|
769
|
-
tokenProgram: secondTokenTokenProgarm,
|
|
770
|
-
},
|
|
771
|
-
];
|
|
772
|
-
}
|
|
773
|
-
else {
|
|
774
|
-
mintsToCreateAtas = [
|
|
775
|
-
{
|
|
776
|
-
mint: collTokenMint,
|
|
777
|
-
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
778
|
-
},
|
|
779
|
-
{
|
|
780
|
-
mint: debtTokenMint,
|
|
781
|
-
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
782
|
-
},
|
|
783
|
-
{
|
|
784
|
-
mint: collReserve.getCTokenMint(),
|
|
785
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
786
|
-
},
|
|
787
|
-
];
|
|
788
|
-
}
|
|
789
|
-
const atasAndCreateIxs = (0, utils_1.createAtasIdempotent)(owner, mintsToCreateAtas);
|
|
790
|
-
const scopeRefreshIxn = await (0, exports.getScopeRefreshIx)(kaminoMarket, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
612
|
+
const { budgetIxs, createAtasIxs, scopeRefreshIx } = await (0, exports.getSetupIxs)(owner, kaminoMarket, obligation, collTokenMint, collReserve, collIsKtoken, debtTokenMint, debtReserve, strategy, scopeRefreshConfig, budgetAndPriorityFeeIxs);
|
|
791
613
|
// 2. Create borrow flash loan instruction
|
|
792
|
-
const {
|
|
793
|
-
|
|
614
|
+
const { flashBorrowIx, flashRepayIx } = (0, instructions_1.getFlashLoanInstructions)({
|
|
615
|
+
borrowIxIndex: budgetIxs.length + createAtasIxs.length + (scopeRefreshIx.length > 0 ? 1 : 0), // TODO: how about user metadata ixs
|
|
794
616
|
walletPublicKey: owner,
|
|
795
617
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
796
618
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -806,28 +628,18 @@ async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
|
|
|
806
628
|
// 4. Borrow tokens in borrow token reserve that will be swapped to repay flash loan
|
|
807
629
|
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)
|
|
808
630
|
referrer, currentSlot);
|
|
809
|
-
const swapInstructions = (0, utils_1.
|
|
810
|
-
const ixs =
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
: [
|
|
822
|
-
...scopeRefreshIxn,
|
|
823
|
-
...budgetIxs,
|
|
824
|
-
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
825
|
-
...[flashBorrowIxn],
|
|
826
|
-
...swapInstructions,
|
|
827
|
-
...classes_1.KaminoAction.actionToIxs(depositAction),
|
|
828
|
-
...classes_1.KaminoAction.actionToIxs(borrowAction),
|
|
829
|
-
...[flashRepayIxn],
|
|
830
|
-
];
|
|
631
|
+
const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
|
|
632
|
+
const ixs = [
|
|
633
|
+
...scopeRefreshIx,
|
|
634
|
+
...budgetIxs,
|
|
635
|
+
...createAtasIxs,
|
|
636
|
+
...[flashBorrowIx],
|
|
637
|
+
...(collIsKtoken ? swapInstructions : []),
|
|
638
|
+
...classes_1.KaminoAction.actionToIxs(depositAction),
|
|
639
|
+
...classes_1.KaminoAction.actionToIxs(borrowAction),
|
|
640
|
+
...(collIsKtoken ? [] : swapInstructions),
|
|
641
|
+
...[flashRepayIx],
|
|
642
|
+
];
|
|
831
643
|
const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
|
|
832
644
|
const res = {
|
|
833
645
|
flashLoanInfo: {
|
|
@@ -842,59 +654,11 @@ async function buildIncreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
|
|
|
842
654
|
* Withdraw and repay tokens if leverage decreased
|
|
843
655
|
*/
|
|
844
656
|
async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeRefreshConfig, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxs, useV2Ixs) {
|
|
845
|
-
const collReserve = kaminoMarket.
|
|
846
|
-
const debtReserve = kaminoMarket.
|
|
657
|
+
const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
|
|
658
|
+
const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
|
|
847
659
|
const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
|
|
848
660
|
// 1. Create atas & budget txns
|
|
849
|
-
const budgetIxs =
|
|
850
|
-
let mintsToCreateAtas;
|
|
851
|
-
if (collIsKtoken) {
|
|
852
|
-
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
853
|
-
? strategy.strategy.tokenBMint
|
|
854
|
-
: strategy.strategy.tokenAMint;
|
|
855
|
-
const secondTokenTokenProgarm = strategy?.strategy.tokenAMint.equals(debtTokenMint)
|
|
856
|
-
? strategy.strategy.tokenBTokenProgram.equals(web3_js_1.PublicKey.default)
|
|
857
|
-
? spl_token_1.TOKEN_PROGRAM_ID
|
|
858
|
-
: strategy.strategy.tokenBTokenProgram
|
|
859
|
-
: strategy.strategy.tokenATokenProgram.equals(web3_js_1.PublicKey.default)
|
|
860
|
-
? spl_token_1.TOKEN_PROGRAM_ID
|
|
861
|
-
: strategy.strategy.tokenATokenProgram;
|
|
862
|
-
mintsToCreateAtas = [
|
|
863
|
-
{
|
|
864
|
-
mint: collTokenMint,
|
|
865
|
-
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
866
|
-
},
|
|
867
|
-
{
|
|
868
|
-
mint: debtTokenMint,
|
|
869
|
-
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
870
|
-
},
|
|
871
|
-
{
|
|
872
|
-
mint: collReserve.getCTokenMint(),
|
|
873
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
874
|
-
},
|
|
875
|
-
{
|
|
876
|
-
mint: secondTokenAta,
|
|
877
|
-
tokenProgram: secondTokenTokenProgarm,
|
|
878
|
-
},
|
|
879
|
-
];
|
|
880
|
-
}
|
|
881
|
-
else {
|
|
882
|
-
mintsToCreateAtas = [
|
|
883
|
-
{
|
|
884
|
-
mint: collTokenMint,
|
|
885
|
-
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
886
|
-
},
|
|
887
|
-
{
|
|
888
|
-
mint: debtTokenMint,
|
|
889
|
-
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
890
|
-
},
|
|
891
|
-
{
|
|
892
|
-
mint: collReserve.getCTokenMint(),
|
|
893
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
894
|
-
},
|
|
895
|
-
];
|
|
896
|
-
}
|
|
897
|
-
const atasAndCreateIxs = (0, utils_1.createAtasIdempotent)(owner, mintsToCreateAtas);
|
|
661
|
+
const { budgetIxs, createAtasIxs, scopeRefreshIx } = await (0, exports.getSetupIxs)(owner, kaminoMarket, obligation, collTokenMint, collReserve, collIsKtoken, debtTokenMint, debtReserve, strategy, scopeRefreshConfig, budgetAndPriorityFeeIxs);
|
|
898
662
|
// TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
|
|
899
663
|
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
900
664
|
const closeWsolAtaIxs = [];
|
|
@@ -906,10 +670,9 @@ async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
|
|
|
906
670
|
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
907
671
|
fillWsolAtaIxs.push(...(0, utils_1.getTransferWsolIxs)(owner, wsolAta, (0, classes_2.numberToLamportsDecimal)(balanceToWrap, debtReserve.stats.decimals).ceil()));
|
|
908
672
|
}
|
|
909
|
-
const scopeRefreshIxn = await (0, exports.getScopeRefreshIx)(kaminoMarket, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
910
673
|
// 3. Flash borrow & repay amount to repay (debt)
|
|
911
|
-
const {
|
|
912
|
-
|
|
674
|
+
const { flashBorrowIx, flashRepayIx } = (0, instructions_1.getFlashLoanInstructions)({
|
|
675
|
+
borrowIxIndex: budgetIxs.length + createAtasIxs.length + fillWsolAtaIxs.length + (scopeRefreshIx.length > 0 ? 1 : 0),
|
|
913
676
|
walletPublicKey: owner,
|
|
914
677
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
915
678
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -927,17 +690,17 @@ async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
|
|
|
927
690
|
// 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
|
|
928
691
|
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)
|
|
929
692
|
referrer, currentSlot);
|
|
930
|
-
const swapInstructions = (0, utils_1.
|
|
693
|
+
const swapInstructions = (0, utils_1.removeBudgetIxs)(swapQuoteIxs.swapIxs);
|
|
931
694
|
const ixs = [
|
|
932
|
-
...
|
|
695
|
+
...scopeRefreshIx,
|
|
933
696
|
...budgetIxs,
|
|
934
|
-
...
|
|
697
|
+
...createAtasIxs,
|
|
935
698
|
...fillWsolAtaIxs,
|
|
936
|
-
...[
|
|
699
|
+
...[flashBorrowIx],
|
|
937
700
|
...classes_1.KaminoAction.actionToIxs(repayAction),
|
|
938
701
|
...classes_1.KaminoAction.actionToIxs(withdrawAction),
|
|
939
702
|
...swapInstructions,
|
|
940
|
-
...[
|
|
703
|
+
...[flashRepayIx],
|
|
941
704
|
...closeWsolAtaIxs,
|
|
942
705
|
];
|
|
943
706
|
const res = {
|
|
@@ -949,6 +712,18 @@ async function buildDecreaseLeverageIxs(owner, kaminoMarket, collTokenMint, debt
|
|
|
949
712
|
};
|
|
950
713
|
return res;
|
|
951
714
|
}
|
|
715
|
+
const getSetupIxs = async (owner, kaminoMarket, obligation, collTokenMint, collReserve, collIsKtoken, debtTokenMint, debtReserve, strategy, scopeRefreshConfig, budgetAndPriorityFeeIxs) => {
|
|
716
|
+
const budgetIxs = budgetAndPriorityFeeIxs || (0, utils_1.getComputeBudgetAndPriorityFeeIxs)(3000000);
|
|
717
|
+
const mintsWithTokenPrograms = getTokenMintsWithTokenPrograms(collTokenMint, collReserve, debtTokenMint, debtReserve, collIsKtoken, strategy);
|
|
718
|
+
const createAtasIxs = (0, utils_1.createAtasIdempotent)(owner, mintsWithTokenPrograms).map((x) => x.createAtaIx);
|
|
719
|
+
const scopeRefreshIx = await (0, exports.getScopeRefreshIx)(kaminoMarket, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
720
|
+
return {
|
|
721
|
+
budgetIxs,
|
|
722
|
+
createAtasIxs,
|
|
723
|
+
scopeRefreshIx,
|
|
724
|
+
};
|
|
725
|
+
};
|
|
726
|
+
exports.getSetupIxs = getSetupIxs;
|
|
952
727
|
const getScopeRefreshIx = async (market, collReserve, debtReserve, obligation, scopeRefreshConfig) => {
|
|
953
728
|
const allReserves = obligation && (0, classes_1.isKaminoObligation)(obligation)
|
|
954
729
|
? new utils_1.PublicKeySet([
|
|
@@ -968,4 +743,56 @@ const getScopeRefreshIx = async (market, collReserve, debtReserve, obligation, s
|
|
|
968
743
|
return scopeRefreshIxs;
|
|
969
744
|
};
|
|
970
745
|
exports.getScopeRefreshIx = getScopeRefreshIx;
|
|
746
|
+
const checkObligationType = (obligationTypeTag, collTokenMint, debtTokenMint, kaminoMarket) => {
|
|
747
|
+
let obligationType;
|
|
748
|
+
if (obligationTypeTag === utils_1.ObligationTypeTag.Multiply) {
|
|
749
|
+
// multiply
|
|
750
|
+
obligationType = new utils_1.MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
751
|
+
}
|
|
752
|
+
else if (obligationTypeTag === utils_1.ObligationTypeTag.Leverage) {
|
|
753
|
+
// leverage
|
|
754
|
+
obligationType = new utils_1.LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
755
|
+
}
|
|
756
|
+
else {
|
|
757
|
+
throw Error('Obligation type tag not supported for leverage, please use 1 - multiply or 3 - leverage');
|
|
758
|
+
}
|
|
759
|
+
return obligationType;
|
|
760
|
+
};
|
|
761
|
+
const getTokenMintsWithTokenPrograms = (collTokenMint, collReserve, debtTokenMint, debtReserve, collIsKtoken, strategy) => {
|
|
762
|
+
const mintsWithTokenPrograms = [
|
|
763
|
+
{
|
|
764
|
+
mint: collTokenMint,
|
|
765
|
+
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
766
|
+
},
|
|
767
|
+
{
|
|
768
|
+
mint: debtTokenMint,
|
|
769
|
+
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
770
|
+
},
|
|
771
|
+
{
|
|
772
|
+
mint: collReserve.getCTokenMint(),
|
|
773
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
774
|
+
},
|
|
775
|
+
];
|
|
776
|
+
if (collIsKtoken) {
|
|
777
|
+
let kTokenAta;
|
|
778
|
+
let kTokenTokenProgram;
|
|
779
|
+
if (strategy.strategy.tokenAMint.equals(debtTokenMint)) {
|
|
780
|
+
kTokenAta = strategy.strategy.tokenBMint;
|
|
781
|
+
kTokenTokenProgram = strategy.strategy.tokenBTokenProgram.equals(web3_js_1.PublicKey.default)
|
|
782
|
+
? spl_token_1.TOKEN_PROGRAM_ID
|
|
783
|
+
: strategy.strategy.tokenBTokenProgram;
|
|
784
|
+
}
|
|
785
|
+
else {
|
|
786
|
+
kTokenAta = strategy.strategy.tokenAMint;
|
|
787
|
+
kTokenTokenProgram = strategy.strategy.tokenATokenProgram.equals(web3_js_1.PublicKey.default)
|
|
788
|
+
? spl_token_1.TOKEN_PROGRAM_ID
|
|
789
|
+
: strategy.strategy.tokenATokenProgram;
|
|
790
|
+
}
|
|
791
|
+
mintsWithTokenPrograms.push({
|
|
792
|
+
mint: kTokenAta,
|
|
793
|
+
tokenProgram: kTokenTokenProgram,
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
return mintsWithTokenPrograms;
|
|
797
|
+
};
|
|
971
798
|
//# sourceMappingURL=operations.js.map
|