@kamino-finance/klend-sdk 4.0.2 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/classes/action.d.ts +3 -1
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +62 -61
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/obligation.d.ts +9 -0
- package/dist/classes/obligation.d.ts.map +1 -1
- package/dist/classes/obligation.js +8 -0
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/reserve.d.ts +3 -1
- package/dist/classes/reserve.d.ts.map +1 -1
- package/dist/classes/reserve.js +32 -0
- package/dist/classes/reserve.js.map +1 -1
- package/dist/classes/shared.d.ts +8 -0
- package/dist/classes/shared.d.ts.map +1 -1
- package/dist/classes/shared.js +6 -1
- package/dist/classes/shared.js.map +1 -1
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +19 -5
- package/dist/classes/vault.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_calcs.d.ts +18 -1
- package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_calcs.js +62 -0
- package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts +27 -43
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +99 -152
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/leverage/operations.d.ts +14 -3
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +184 -128
- package/dist/leverage/operations.js.map +1 -1
- package/dist/utils/ata.d.ts +12 -3
- package/dist/utils/ata.d.ts.map +1 -1
- package/dist/utils/ata.js +26 -39
- package/dist/utils/ata.js.map +1 -1
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +0 -2
- package/dist/utils/index.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 +18 -0
- package/dist/utils/instruction.js.map +1 -1
- package/package.json +3 -3
- package/src/classes/action.ts +92 -98
- package/src/classes/obligation.ts +15 -6
- package/src/classes/reserve.ts +48 -1
- package/src/classes/shared.ts +10 -0
- package/src/classes/vault.ts +23 -19
- package/src/lending_operations/repay_with_collateral_calcs.ts +98 -1
- package/src/lending_operations/repay_with_collateral_operations.ts +233 -253
- package/src/leverage/operations.ts +227 -140
- package/src/utils/ata.ts +33 -56
- package/src/utils/index.ts +0 -2
- package/src/utils/instruction.ts +22 -0
- package/dist/utils/layout.d.ts +0 -14
- package/dist/utils/layout.d.ts.map +0 -1
- package/dist/utils/layout.js +0 -123
- package/dist/utils/layout.js.map +0 -1
- package/dist/utils/syncNative.d.ts +0 -11
- package/dist/utils/syncNative.d.ts.map +0 -1
- package/dist/utils/syncNative.js +0 -45
- package/dist/utils/syncNative.js.map +0 -1
- package/src/global.d.ts +0 -1
- package/src/utils/layout.ts +0 -118
- package/src/utils/syncNative.ts +0 -22
|
@@ -131,9 +131,7 @@ const getDepositWithLeverageSwapInputs = (props) => {
|
|
|
131
131
|
});
|
|
132
132
|
return {
|
|
133
133
|
swapInputs: {
|
|
134
|
-
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(calcs.swapDebtTokenIn, debtReserve.state.liquidity.mintDecimals.toNumber())
|
|
135
|
-
.ceil()
|
|
136
|
-
.toNumber(),
|
|
134
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(calcs.swapDebtTokenIn, debtReserve.state.liquidity.mintDecimals.toNumber()).ceil(),
|
|
137
135
|
inputMint: debtTokenMint,
|
|
138
136
|
outputMint: collTokenMint,
|
|
139
137
|
},
|
|
@@ -186,8 +184,7 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
186
184
|
initDepositInSol: calcs.initDepositInSol,
|
|
187
185
|
}));
|
|
188
186
|
// 1. Create atas & budget txns
|
|
189
|
-
let mintsToCreateAtas
|
|
190
|
-
let mintsToCreateAtasTokenPrograms = [];
|
|
187
|
+
let mintsToCreateAtas;
|
|
191
188
|
if (collIsKtoken) {
|
|
192
189
|
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
193
190
|
? strategy.strategy.tokenBMint
|
|
@@ -199,24 +196,43 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
199
196
|
: strategy.strategy.tokenATokenProgram.equals(web3_js_1.PublicKey.default)
|
|
200
197
|
? spl_token_1.TOKEN_PROGRAM_ID
|
|
201
198
|
: strategy.strategy.tokenATokenProgram;
|
|
202
|
-
mintsToCreateAtas = [
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
199
|
+
mintsToCreateAtas = [
|
|
200
|
+
{
|
|
201
|
+
mint: collTokenMint,
|
|
202
|
+
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
mint: debtTokenMint,
|
|
206
|
+
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
mint: collReserve.getCTokenMint(),
|
|
210
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
mint: secondTokenAta,
|
|
214
|
+
tokenProgram: secondTokenTokenProgarm,
|
|
215
|
+
},
|
|
208
216
|
];
|
|
209
217
|
}
|
|
210
218
|
else {
|
|
211
|
-
mintsToCreateAtas = [
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
219
|
+
mintsToCreateAtas = [
|
|
220
|
+
{
|
|
221
|
+
mint: collTokenMint,
|
|
222
|
+
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
mint: debtTokenMint,
|
|
226
|
+
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
mint: collReserve.getCTokenMint(),
|
|
230
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
231
|
+
},
|
|
216
232
|
];
|
|
217
233
|
}
|
|
218
234
|
const budgetIxns = budgetAndPriorityFeeIxns || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
219
|
-
const { atas: [collTokenAta, debtTokenAta],
|
|
235
|
+
const { atas: [collTokenAta, debtTokenAta], createAtaIxs, } = await (0, utils_1.getAtasWithCreateIxnsIfMissing)(connection, user, mintsToCreateAtas);
|
|
220
236
|
// TODO: this needs to work the other way around also
|
|
221
237
|
// TODO: marius test this with shorting leverage and with leverage looping
|
|
222
238
|
const fillWsolAtaIxns = [];
|
|
@@ -226,7 +242,7 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
226
242
|
// 1. Flash borrow & repay the collateral amount needed for given leverage
|
|
227
243
|
// if user deposits coll, then we borrow the diff, else we borrow the entire amount
|
|
228
244
|
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
229
|
-
borrowIxnIndex: budgetIxns.length +
|
|
245
|
+
borrowIxnIndex: budgetIxns.length + createAtaIxs.length + fillWsolAtaIxns.length,
|
|
230
246
|
walletPublicKey: user,
|
|
231
247
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
232
248
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -264,7 +280,7 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
264
280
|
console.log('Expected to swap in', !collIsKtoken ? calcs.swapDebtTokenIn.toNumber().toString() : calcsKtoken.singleSidedDeposit.toNumber().toString(), 'debt for', !collIsKtoken ? calcs.swapCollTokenExpectedOut.toString() : calcsKtoken.requiredCollateral.toNumber().toString(), 'coll');
|
|
265
281
|
const ixns = [
|
|
266
282
|
...budgetIxns,
|
|
267
|
-
...
|
|
283
|
+
...createAtaIxs,
|
|
268
284
|
...fillWsolAtaIxns,
|
|
269
285
|
...[flashBorrowIxn],
|
|
270
286
|
...kaminoDepositAndBorrowAction.setupIxs,
|
|
@@ -273,7 +289,6 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
273
289
|
...[kaminoDepositAndBorrowAction.lendingIxs[1]],
|
|
274
290
|
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
275
291
|
...[flashRepayIxn],
|
|
276
|
-
...closeAtasIxns,
|
|
277
292
|
];
|
|
278
293
|
const uniqueAccounts = new utils_1.PublicKeySet([]);
|
|
279
294
|
ixns.forEach((ixn) => {
|
|
@@ -287,7 +302,11 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
287
302
|
return {
|
|
288
303
|
ixns: [],
|
|
289
304
|
lookupTablesAddresses: [],
|
|
290
|
-
swapInputs: {
|
|
305
|
+
swapInputs: {
|
|
306
|
+
inputAmountLamports: new decimal_js_1.default('0'),
|
|
307
|
+
inputMint: web3_js_1.PublicKey.default,
|
|
308
|
+
outputMint: web3_js_1.PublicKey.default,
|
|
309
|
+
},
|
|
291
310
|
totalKlendAccounts: totalKlendAccounts,
|
|
292
311
|
};
|
|
293
312
|
}
|
|
@@ -309,13 +328,11 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
309
328
|
expectedDebtTokenAtaBalance = await (0, utils_2.getExpectedTokenBalanceAfterBorrow)(connection, debtTokenMint, user, (0, classes_2.numberToLamportsDecimal)(futureBalanceInAta.toDecimalPlaces(debtReserve.stats.decimals), debtReserve.stats.decimals), debtReserve.state.liquidity.mintDecimals.toNumber());
|
|
310
329
|
}
|
|
311
330
|
const swapInputs = {
|
|
312
|
-
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.swapDebtTokenIn : calcsKtoken.singleSidedDeposit, debtReserve.stats.decimals)
|
|
313
|
-
.ceil()
|
|
314
|
-
.toNumber(),
|
|
331
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.swapDebtTokenIn : calcsKtoken.singleSidedDeposit, debtReserve.stats.decimals).ceil(),
|
|
315
332
|
inputMint: debtTokenMint,
|
|
316
333
|
outputMint: collTokenMint,
|
|
317
334
|
};
|
|
318
|
-
const [swapIxns, lookupTablesAddresses] = await depositSwapper(swapInputs.inputAmountLamports, swapInputs.inputMint, swapInputs.outputMint, slippagePct.toNumber(), expectedDebtTokenAtaBalance);
|
|
335
|
+
const [swapIxns, lookupTablesAddresses] = await depositSwapper(swapInputs.inputAmountLamports.toNumber(), swapInputs.inputMint, swapInputs.outputMint, slippagePct.toNumber(), expectedDebtTokenAtaBalance);
|
|
319
336
|
if (collIsKtoken) {
|
|
320
337
|
if (strategy?.strategy.strategyLookupTable) {
|
|
321
338
|
lookupTablesAddresses.push(strategy?.strategy.strategyLookupTable);
|
|
@@ -329,7 +346,7 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
329
346
|
return {
|
|
330
347
|
ixns: [
|
|
331
348
|
...budgetIxns,
|
|
332
|
-
...
|
|
349
|
+
...createAtaIxs,
|
|
333
350
|
...fillWsolAtaIxns,
|
|
334
351
|
...[flashBorrowIxn],
|
|
335
352
|
...kaminoDepositAndBorrowAction.setupIxs,
|
|
@@ -339,7 +356,6 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
339
356
|
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
340
357
|
...swapInstructions,
|
|
341
358
|
...[flashRepayIxn],
|
|
342
|
-
...closeAtasIxns,
|
|
343
359
|
],
|
|
344
360
|
lookupTablesAddresses,
|
|
345
361
|
swapInputs,
|
|
@@ -350,7 +366,7 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
350
366
|
return {
|
|
351
367
|
ixns: [
|
|
352
368
|
...budgetIxns,
|
|
353
|
-
...
|
|
369
|
+
...createAtaIxs,
|
|
354
370
|
...fillWsolAtaIxns,
|
|
355
371
|
...[flashBorrowIxn],
|
|
356
372
|
...swapInstructions,
|
|
@@ -360,7 +376,6 @@ const getDepositWithLeverageIxns = async (props) => {
|
|
|
360
376
|
...[kaminoDepositAndBorrowAction.lendingIxs[1]],
|
|
361
377
|
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
362
378
|
...[flashRepayIxn],
|
|
363
|
-
...closeAtasIxns,
|
|
364
379
|
],
|
|
365
380
|
lookupTablesAddresses,
|
|
366
381
|
swapInputs,
|
|
@@ -399,9 +414,7 @@ const getWithdrawWithLeverageSwapInputs = (props) => {
|
|
|
399
414
|
const collTokenSwapIn = selectedTokenIsCollToken ? swapAmountIfWithdrawingColl : swapAmountIfWithdrawingDebt;
|
|
400
415
|
return {
|
|
401
416
|
swapInputs: {
|
|
402
|
-
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(collTokenSwapIn, collReserve.state.liquidity.mintDecimals.toNumber())
|
|
403
|
-
.ceil()
|
|
404
|
-
.toNumber(),
|
|
417
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(collTokenSwapIn, collReserve.state.liquidity.mintDecimals.toNumber()).ceil(),
|
|
405
418
|
inputMint: collTokenMint,
|
|
406
419
|
outputMint: debtTokenMint,
|
|
407
420
|
},
|
|
@@ -475,42 +488,59 @@ const getWithdrawWithLeverageIxns = async (props) => {
|
|
|
475
488
|
const strategy = collIsKtoken ? await kamino?.getStrategyByKTokenMint(collTokenMint) : undefined;
|
|
476
489
|
console.log('Expecting to swap', collTokenSwapIn.toString(), 'coll for', debtTokenExpectedSwapOut.toString(), 'debt');
|
|
477
490
|
// 1. Create atas & budget txns & user metadata
|
|
478
|
-
let mintsToCreateAtas
|
|
479
|
-
let mintsToCreateAtasTokenPrograms = [];
|
|
491
|
+
let mintsToCreateAtas;
|
|
480
492
|
if (collIsKtoken) {
|
|
481
493
|
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
482
494
|
? strategy.strategy.tokenBMint
|
|
483
495
|
: strategy.strategy.tokenAMint;
|
|
484
|
-
const
|
|
496
|
+
const secondTokenTokenProgram = strategy?.strategy.tokenAMint.equals(debtTokenMint)
|
|
485
497
|
? strategy.strategy.tokenBTokenProgram.equals(web3_js_1.PublicKey.default)
|
|
486
498
|
? spl_token_1.TOKEN_PROGRAM_ID
|
|
487
499
|
: strategy.strategy.tokenBTokenProgram
|
|
488
500
|
: strategy.strategy.tokenATokenProgram.equals(web3_js_1.PublicKey.default)
|
|
489
501
|
? spl_token_1.TOKEN_PROGRAM_ID
|
|
490
502
|
: strategy.strategy.tokenATokenProgram;
|
|
491
|
-
mintsToCreateAtas = [
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
503
|
+
mintsToCreateAtas = [
|
|
504
|
+
{
|
|
505
|
+
mint: collTokenMint,
|
|
506
|
+
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
507
|
+
},
|
|
508
|
+
{
|
|
509
|
+
mint: debtTokenMint,
|
|
510
|
+
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
511
|
+
},
|
|
512
|
+
{
|
|
513
|
+
mint: collReserve.getCTokenMint(),
|
|
514
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
515
|
+
},
|
|
516
|
+
{
|
|
517
|
+
mint: secondTokenAta,
|
|
518
|
+
tokenProgram: secondTokenTokenProgram,
|
|
519
|
+
},
|
|
497
520
|
];
|
|
498
521
|
}
|
|
499
522
|
else {
|
|
500
|
-
mintsToCreateAtas = [
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
523
|
+
mintsToCreateAtas = [
|
|
524
|
+
{
|
|
525
|
+
mint: collTokenMint,
|
|
526
|
+
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
527
|
+
},
|
|
528
|
+
{
|
|
529
|
+
mint: debtTokenMint,
|
|
530
|
+
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
531
|
+
},
|
|
532
|
+
{
|
|
533
|
+
mint: collReserve.getCTokenMint(),
|
|
534
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
535
|
+
},
|
|
505
536
|
];
|
|
506
537
|
}
|
|
507
|
-
const { atas: [, debtTokenAta],
|
|
538
|
+
const { atas: [, debtTokenAta], createAtaIxs, } = await (0, utils_1.getAtasWithCreateIxnsIfMissing)(connection, user, mintsToCreateAtas);
|
|
508
539
|
const closeWsolAtaIxns = [];
|
|
509
540
|
if (depositTokenIsSol || debtTokenMint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
510
541
|
const wsolAta = (0, utils_1.getAssociatedTokenAddress)(utils_1.WRAPPED_SOL_MINT, user, false);
|
|
511
542
|
closeWsolAtaIxns.push((0, spl_token_1.createCloseAccountInstruction)(wsolAta, user, user, [], spl_token_1.TOKEN_PROGRAM_ID));
|
|
512
543
|
}
|
|
513
|
-
closeAtasIxns.push(...closeWsolAtaIxns);
|
|
514
544
|
const budgetIxns = budgetAndPriorityFeeIxns || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
515
545
|
// TODO: marius test this with shorting leverage and with leverage looping
|
|
516
546
|
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
@@ -524,7 +554,7 @@ const getWithdrawWithLeverageIxns = async (props) => {
|
|
|
524
554
|
// We borrow exactly how much we need to repay
|
|
525
555
|
// and repay that + flash amount fee
|
|
526
556
|
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
527
|
-
borrowIxnIndex: budgetIxns.length +
|
|
557
|
+
borrowIxnIndex: budgetIxns.length + createAtaIxs.length + fillWsolAtaIxns.length,
|
|
528
558
|
walletPublicKey: user,
|
|
529
559
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
530
560
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -540,7 +570,7 @@ const getWithdrawWithLeverageIxns = async (props) => {
|
|
|
540
570
|
isClosingPosition, referrer, { includeScopeRefresh: true, scopeFeed: scopeFeed });
|
|
541
571
|
const klendIxns = [
|
|
542
572
|
...budgetIxns,
|
|
543
|
-
...
|
|
573
|
+
...createAtaIxs,
|
|
544
574
|
...fillWsolAtaIxns,
|
|
545
575
|
...[flashBorrowIxn],
|
|
546
576
|
...repayAndWithdrawAction.setupIxs,
|
|
@@ -549,21 +579,20 @@ const getWithdrawWithLeverageIxns = async (props) => {
|
|
|
549
579
|
...[repayAndWithdrawAction.lendingIxs[1]],
|
|
550
580
|
...repayAndWithdrawAction.cleanupIxs,
|
|
551
581
|
...[flashRepayIxn],
|
|
552
|
-
...
|
|
582
|
+
...closeWsolAtaIxns,
|
|
553
583
|
];
|
|
554
|
-
const
|
|
555
|
-
|
|
556
|
-
ixn.keys.forEach((key) => {
|
|
557
|
-
uniqueAccounts.add(key.pubkey);
|
|
558
|
-
});
|
|
559
|
-
});
|
|
560
|
-
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
584
|
+
const uniqueAccs = (0, utils_1.uniqueAccounts)(klendIxns);
|
|
585
|
+
const totalKlendAccounts = uniqueAccs.length;
|
|
561
586
|
// return early to avoid extra swapper calls
|
|
562
587
|
if (getTotalKlendAccountsOnly) {
|
|
563
588
|
return {
|
|
564
589
|
ixns: [],
|
|
565
590
|
lookupTablesAddresses: [],
|
|
566
|
-
swapInputs: {
|
|
591
|
+
swapInputs: {
|
|
592
|
+
inputAmountLamports: new decimal_js_1.default('0'),
|
|
593
|
+
inputMint: web3_js_1.PublicKey.default,
|
|
594
|
+
outputMint: web3_js_1.PublicKey.default,
|
|
595
|
+
},
|
|
567
596
|
totalKlendAccounts: totalKlendAccounts,
|
|
568
597
|
};
|
|
569
598
|
}
|
|
@@ -578,11 +607,11 @@ const getWithdrawWithLeverageIxns = async (props) => {
|
|
|
578
607
|
withdrawSwapper = swapper;
|
|
579
608
|
}
|
|
580
609
|
const swapInputs = {
|
|
581
|
-
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(collTokenSwapIn, collReserve.stats.decimals).ceil()
|
|
610
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(collTokenSwapIn, collReserve.stats.decimals).ceil(),
|
|
582
611
|
inputMint: collTokenMint,
|
|
583
612
|
outputMint: debtTokenMint,
|
|
584
613
|
};
|
|
585
|
-
const [swapIxns, lookupTablesAddresses] = await withdrawSwapper(swapInputs.inputAmountLamports, swapInputs.inputMint, swapInputs.outputMint, slippagePct);
|
|
614
|
+
const [swapIxns, lookupTablesAddresses] = await withdrawSwapper(swapInputs.inputAmountLamports.toNumber(), swapInputs.inputMint, swapInputs.outputMint, slippagePct);
|
|
586
615
|
// TODO MARIUS: remove first instruction that is setBudget ixn
|
|
587
616
|
if (collIsKtoken) {
|
|
588
617
|
if (strategy?.strategy.strategyLookupTable) {
|
|
@@ -595,7 +624,7 @@ const getWithdrawWithLeverageIxns = async (props) => {
|
|
|
595
624
|
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapIxns, []);
|
|
596
625
|
const ixns = [
|
|
597
626
|
...budgetIxns,
|
|
598
|
-
...
|
|
627
|
+
...createAtaIxs,
|
|
599
628
|
...fillWsolAtaIxns,
|
|
600
629
|
...[flashBorrowIxn],
|
|
601
630
|
...repayAndWithdrawAction.setupIxs,
|
|
@@ -605,7 +634,7 @@ const getWithdrawWithLeverageIxns = async (props) => {
|
|
|
605
634
|
...repayAndWithdrawAction.cleanupIxs,
|
|
606
635
|
...swapInstructions,
|
|
607
636
|
...[flashRepayIxn],
|
|
608
|
-
...
|
|
637
|
+
...closeWsolAtaIxns,
|
|
609
638
|
];
|
|
610
639
|
// Send ixns and lookup tables
|
|
611
640
|
return {
|
|
@@ -636,9 +665,7 @@ const getAdjustLeverageSwapInputs = (props) => {
|
|
|
636
665
|
.div(priceDebtToColl);
|
|
637
666
|
return {
|
|
638
667
|
swapInputs: {
|
|
639
|
-
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(borrowAmount, debtReserve.state.liquidity.mintDecimals.toNumber())
|
|
640
|
-
.ceil()
|
|
641
|
-
.toNumber(),
|
|
668
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(borrowAmount, debtReserve.state.liquidity.mintDecimals.toNumber()).ceil(),
|
|
642
669
|
inputMint: debtTokenMint,
|
|
643
670
|
outputMint: collTokenMint,
|
|
644
671
|
},
|
|
@@ -650,9 +677,7 @@ const getAdjustLeverageSwapInputs = (props) => {
|
|
|
650
677
|
.mul(1 + slippagePct / 100);
|
|
651
678
|
return {
|
|
652
679
|
swapInputs: {
|
|
653
|
-
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(withdrawAmountWithSlippageAndFlashLoanFee, collReserve.state.liquidity.mintDecimals.toNumber())
|
|
654
|
-
.ceil()
|
|
655
|
-
.toNumber(),
|
|
680
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(withdrawAmountWithSlippageAndFlashLoanFee, collReserve.state.liquidity.mintDecimals.toNumber()).ceil(),
|
|
656
681
|
inputMint: collTokenMint,
|
|
657
682
|
outputMint: debtTokenMint,
|
|
658
683
|
},
|
|
@@ -672,7 +697,7 @@ const getAdjustLeverageIxns = async (props) => {
|
|
|
672
697
|
obligation.getDepositByMint(collReserve.getLiquidityMint()) !== undefined)[0];
|
|
673
698
|
const currentLeverage = userObligation.refreshedStats.leverage;
|
|
674
699
|
const isDepositViaLeverage = targetLeverage.gte(new decimal_js_1.default(currentLeverage));
|
|
675
|
-
let flashLoanFee
|
|
700
|
+
let flashLoanFee;
|
|
676
701
|
if (isDepositViaLeverage) {
|
|
677
702
|
flashLoanFee = collReserve.getFlashLoanFee() || new decimal_js_1.default(0);
|
|
678
703
|
}
|
|
@@ -776,8 +801,7 @@ const getIncreaseLeverageIxns = async (props) => {
|
|
|
776
801
|
const strategy = collIsKtoken ? await kamino?.getStrategyByKTokenMint(collTokenMint) : undefined;
|
|
777
802
|
// 1. Create atas & budget txns
|
|
778
803
|
const budgetIxns = budgetAndPriorityFeeIxns || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
779
|
-
let mintsToCreateAtas
|
|
780
|
-
let mintsToCreateAtasTokenPrograms = [];
|
|
804
|
+
let mintsToCreateAtas;
|
|
781
805
|
if (collIsKtoken) {
|
|
782
806
|
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
783
807
|
? strategy.strategy.tokenBMint
|
|
@@ -789,23 +813,42 @@ const getIncreaseLeverageIxns = async (props) => {
|
|
|
789
813
|
: strategy.strategy.tokenATokenProgram.equals(web3_js_1.PublicKey.default)
|
|
790
814
|
? spl_token_1.TOKEN_PROGRAM_ID
|
|
791
815
|
: strategy.strategy.tokenATokenProgram;
|
|
792
|
-
mintsToCreateAtas = [
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
816
|
+
mintsToCreateAtas = [
|
|
817
|
+
{
|
|
818
|
+
mint: collTokenMint,
|
|
819
|
+
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
820
|
+
},
|
|
821
|
+
{
|
|
822
|
+
mint: debtTokenMint,
|
|
823
|
+
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
824
|
+
},
|
|
825
|
+
{
|
|
826
|
+
mint: collReserve.getCTokenMint(),
|
|
827
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
828
|
+
},
|
|
829
|
+
{
|
|
830
|
+
mint: secondTokenAta,
|
|
831
|
+
tokenProgram: secondTokenTokenProgarm,
|
|
832
|
+
},
|
|
798
833
|
];
|
|
799
834
|
}
|
|
800
835
|
else {
|
|
801
|
-
mintsToCreateAtas = [
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
836
|
+
mintsToCreateAtas = [
|
|
837
|
+
{
|
|
838
|
+
mint: collTokenMint,
|
|
839
|
+
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
840
|
+
},
|
|
841
|
+
{
|
|
842
|
+
mint: debtTokenMint,
|
|
843
|
+
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
844
|
+
},
|
|
845
|
+
{
|
|
846
|
+
mint: collReserve.getCTokenMint(),
|
|
847
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
848
|
+
},
|
|
806
849
|
];
|
|
807
850
|
}
|
|
808
|
-
const { atas: [collTokenAta, debtTokenAta],
|
|
851
|
+
const { atas: [collTokenAta, debtTokenAta], createAtaIxs, } = await (0, utils_1.getAtasWithCreateIxnsIfMissing)(connection, user, mintsToCreateAtas);
|
|
809
852
|
// 2. Create borrow flash loan instruction
|
|
810
853
|
// used if coll is Ktoken and we borrow debt token instead
|
|
811
854
|
const amountToFashBorrowDebt = depositAmount
|
|
@@ -814,7 +857,7 @@ const getIncreaseLeverageIxns = async (props) => {
|
|
|
814
857
|
.toDecimalPlaces(debtReserve.stats.decimals, decimal_js_1.default.ROUND_UP);
|
|
815
858
|
// .toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber());
|
|
816
859
|
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
817
|
-
borrowIxnIndex: budgetIxns.length +
|
|
860
|
+
borrowIxnIndex: budgetIxns.length + createAtaIxs.length, // TODO: how about user metadata ixns
|
|
818
861
|
walletPublicKey: user,
|
|
819
862
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
820
863
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -851,7 +894,7 @@ const getIncreaseLeverageIxns = async (props) => {
|
|
|
851
894
|
referrer, currentSlot, { includeScopeRefresh: true, scopeFeed: scopeFeed });
|
|
852
895
|
const klendIxns = [
|
|
853
896
|
...budgetIxns,
|
|
854
|
-
...
|
|
897
|
+
...createAtaIxs,
|
|
855
898
|
...[flashBorrowIxn],
|
|
856
899
|
...depositAction.setupIxs,
|
|
857
900
|
...depositAction.lendingIxs,
|
|
@@ -860,7 +903,6 @@ const getIncreaseLeverageIxns = async (props) => {
|
|
|
860
903
|
...borrowAction.lendingIxs,
|
|
861
904
|
...borrowAction.cleanupIxs,
|
|
862
905
|
...[flashRepayIxn],
|
|
863
|
-
...closeAtasIxns,
|
|
864
906
|
];
|
|
865
907
|
const uniqueAccounts = new utils_1.PublicKeySet([]);
|
|
866
908
|
klendIxns.forEach((ixn) => {
|
|
@@ -874,7 +916,11 @@ const getIncreaseLeverageIxns = async (props) => {
|
|
|
874
916
|
return {
|
|
875
917
|
ixns: [],
|
|
876
918
|
lookupTablesAddresses: [],
|
|
877
|
-
swapInputs: {
|
|
919
|
+
swapInputs: {
|
|
920
|
+
inputAmountLamports: new decimal_js_1.default('0'),
|
|
921
|
+
inputMint: web3_js_1.PublicKey.default,
|
|
922
|
+
outputMint: web3_js_1.PublicKey.default,
|
|
923
|
+
},
|
|
878
924
|
totalKlendAccounts: totalKlendAccounts,
|
|
879
925
|
};
|
|
880
926
|
}
|
|
@@ -891,18 +937,16 @@ const getIncreaseLeverageIxns = async (props) => {
|
|
|
891
937
|
depositSwapper = swapper;
|
|
892
938
|
}
|
|
893
939
|
const swapInputs = {
|
|
894
|
-
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? borrowAmount : amountToFashBorrowDebt, debtReserve.stats.decimals)
|
|
895
|
-
.ceil()
|
|
896
|
-
.toNumber(),
|
|
940
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? borrowAmount : amountToFashBorrowDebt, debtReserve.stats.decimals).ceil(),
|
|
897
941
|
inputMint: debtTokenMint,
|
|
898
942
|
outputMint: collTokenMint,
|
|
899
943
|
};
|
|
900
|
-
const [swapIxns, lookupTablesAddresses] = await depositSwapper(swapInputs.inputAmountLamports, swapInputs.inputMint, swapInputs.outputMint, slippagePct, expectedDebtTokenAtaBalance);
|
|
944
|
+
const [swapIxns, lookupTablesAddresses] = await depositSwapper(swapInputs.inputAmountLamports.toNumber(), swapInputs.inputMint, swapInputs.outputMint, slippagePct, expectedDebtTokenAtaBalance);
|
|
901
945
|
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapIxns, []);
|
|
902
946
|
const ixns = !collIsKtoken
|
|
903
947
|
? [
|
|
904
948
|
...budgetIxns,
|
|
905
|
-
...
|
|
949
|
+
...createAtaIxs,
|
|
906
950
|
...[flashBorrowIxn],
|
|
907
951
|
...depositAction.setupIxs,
|
|
908
952
|
...depositAction.lendingIxs,
|
|
@@ -912,11 +956,10 @@ const getIncreaseLeverageIxns = async (props) => {
|
|
|
912
956
|
...borrowAction.cleanupIxs,
|
|
913
957
|
...swapInstructions,
|
|
914
958
|
...[flashRepayIxn],
|
|
915
|
-
...closeAtasIxns,
|
|
916
959
|
]
|
|
917
960
|
: [
|
|
918
961
|
...budgetIxns,
|
|
919
|
-
...
|
|
962
|
+
...createAtaIxs,
|
|
920
963
|
...[flashBorrowIxn],
|
|
921
964
|
...swapInstructions,
|
|
922
965
|
...depositAction.setupIxs,
|
|
@@ -926,7 +969,6 @@ const getIncreaseLeverageIxns = async (props) => {
|
|
|
926
969
|
...borrowAction.lendingIxs,
|
|
927
970
|
...borrowAction.cleanupIxs,
|
|
928
971
|
...[flashRepayIxn],
|
|
929
|
-
...closeAtasIxns,
|
|
930
972
|
];
|
|
931
973
|
ixns.forEach((ixn, i) => {
|
|
932
974
|
console.log(`ixn ${i + 1}: ${ixn.programId.toString()}`);
|
|
@@ -961,8 +1003,7 @@ const getDecreaseLeverageIxns = async (props) => {
|
|
|
961
1003
|
const strategy = collIsKtoken ? await kamino?.getStrategyByKTokenMint(collTokenMint) : undefined;
|
|
962
1004
|
// 1. Create atas & budget txns
|
|
963
1005
|
const budgetIxns = budgetAndPriorityFeeIxns || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
964
|
-
let mintsToCreateAtas
|
|
965
|
-
let mintsToCreateAtasTokenPrograms = [];
|
|
1006
|
+
let mintsToCreateAtas;
|
|
966
1007
|
if (collIsKtoken) {
|
|
967
1008
|
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
968
1009
|
? strategy.strategy.tokenBMint
|
|
@@ -974,23 +1015,42 @@ const getDecreaseLeverageIxns = async (props) => {
|
|
|
974
1015
|
: strategy.strategy.tokenATokenProgram.equals(web3_js_1.PublicKey.default)
|
|
975
1016
|
? spl_token_1.TOKEN_PROGRAM_ID
|
|
976
1017
|
: strategy.strategy.tokenATokenProgram;
|
|
977
|
-
mintsToCreateAtas = [
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
1018
|
+
mintsToCreateAtas = [
|
|
1019
|
+
{
|
|
1020
|
+
mint: collTokenMint,
|
|
1021
|
+
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
1022
|
+
},
|
|
1023
|
+
{
|
|
1024
|
+
mint: debtTokenMint,
|
|
1025
|
+
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
1026
|
+
},
|
|
1027
|
+
{
|
|
1028
|
+
mint: collReserve.getCTokenMint(),
|
|
1029
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1030
|
+
},
|
|
1031
|
+
{
|
|
1032
|
+
mint: secondTokenAta,
|
|
1033
|
+
tokenProgram: secondTokenTokenProgarm,
|
|
1034
|
+
},
|
|
983
1035
|
];
|
|
984
1036
|
}
|
|
985
1037
|
else {
|
|
986
|
-
mintsToCreateAtas = [
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
1038
|
+
mintsToCreateAtas = [
|
|
1039
|
+
{
|
|
1040
|
+
mint: collTokenMint,
|
|
1041
|
+
tokenProgram: collReserve.getLiquidityTokenProgram(),
|
|
1042
|
+
},
|
|
1043
|
+
{
|
|
1044
|
+
mint: debtTokenMint,
|
|
1045
|
+
tokenProgram: debtReserve.getLiquidityTokenProgram(),
|
|
1046
|
+
},
|
|
1047
|
+
{
|
|
1048
|
+
mint: collReserve.getCTokenMint(),
|
|
1049
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1050
|
+
},
|
|
991
1051
|
];
|
|
992
1052
|
}
|
|
993
|
-
const { atas: [, debtTokenAta],
|
|
1053
|
+
const { atas: [, debtTokenAta], createAtaIxs, } = await (0, utils_1.getAtasWithCreateIxnsIfMissing)(connection, user, mintsToCreateAtas);
|
|
994
1054
|
// TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
|
|
995
1055
|
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
996
1056
|
const closeWsolAtaIxns = [];
|
|
@@ -998,7 +1058,6 @@ const getDecreaseLeverageIxns = async (props) => {
|
|
|
998
1058
|
const wsolAta = await (0, utils_1.getAssociatedTokenAddress)(utils_1.WRAPPED_SOL_MINT, user, false);
|
|
999
1059
|
closeWsolAtaIxns.push((0, spl_token_1.createCloseAccountInstruction)(wsolAta, user, user, [], spl_token_1.TOKEN_PROGRAM_ID));
|
|
1000
1060
|
}
|
|
1001
|
-
closeAtasIxns.push(...closeWsolAtaIxns);
|
|
1002
1061
|
const fillWsolAtaIxns = [];
|
|
1003
1062
|
if (debtTokenMint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
1004
1063
|
const halfSolBalance = (await connection.getBalance(user)) / web3_js_1.LAMPORTS_PER_SOL / 2;
|
|
@@ -1007,7 +1066,7 @@ const getDecreaseLeverageIxns = async (props) => {
|
|
|
1007
1066
|
}
|
|
1008
1067
|
// 3. Flash borrow & repay amount to repay (debt)
|
|
1009
1068
|
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
1010
|
-
borrowIxnIndex: budgetIxns.length +
|
|
1069
|
+
borrowIxnIndex: budgetIxns.length + createAtaIxs.length + fillWsolAtaIxns.length,
|
|
1011
1070
|
walletPublicKey: user,
|
|
1012
1071
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
1013
1072
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -1042,7 +1101,7 @@ const getDecreaseLeverageIxns = async (props) => {
|
|
|
1042
1101
|
referrer, currentSlot, { includeScopeRefresh: true, scopeFeed: scopeFeed });
|
|
1043
1102
|
const klendIxns = [
|
|
1044
1103
|
...budgetIxns,
|
|
1045
|
-
...
|
|
1104
|
+
...createAtaIxs,
|
|
1046
1105
|
...fillWsolAtaIxns,
|
|
1047
1106
|
...[flashBorrowIxn],
|
|
1048
1107
|
...repayAction.setupIxs,
|
|
@@ -1052,21 +1111,20 @@ const getDecreaseLeverageIxns = async (props) => {
|
|
|
1052
1111
|
...withdrawAction.lendingIxs,
|
|
1053
1112
|
...withdrawAction.cleanupIxs,
|
|
1054
1113
|
...[flashRepayIxn],
|
|
1055
|
-
...
|
|
1114
|
+
...closeWsolAtaIxns,
|
|
1056
1115
|
];
|
|
1057
|
-
const
|
|
1058
|
-
|
|
1059
|
-
ixn.keys.forEach((key) => {
|
|
1060
|
-
uniqueAccounts.add(key.pubkey);
|
|
1061
|
-
});
|
|
1062
|
-
});
|
|
1063
|
-
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
1116
|
+
const uniqueAccs = (0, utils_1.uniqueAccounts)(klendIxns);
|
|
1117
|
+
const totalKlendAccounts = uniqueAccs.length;
|
|
1064
1118
|
// return early to avoid extra swapper calls
|
|
1065
1119
|
if (getTotalKlendAccountsOnly) {
|
|
1066
1120
|
return {
|
|
1067
1121
|
ixns: [],
|
|
1068
1122
|
lookupTablesAddresses: [],
|
|
1069
|
-
swapInputs: {
|
|
1123
|
+
swapInputs: {
|
|
1124
|
+
inputAmountLamports: new decimal_js_1.default('0'),
|
|
1125
|
+
inputMint: web3_js_1.PublicKey.default,
|
|
1126
|
+
outputMint: web3_js_1.PublicKey.default,
|
|
1127
|
+
},
|
|
1070
1128
|
totalKlendAccounts: totalKlendAccounts,
|
|
1071
1129
|
};
|
|
1072
1130
|
}
|
|
@@ -1081,18 +1139,16 @@ const getDecreaseLeverageIxns = async (props) => {
|
|
|
1081
1139
|
withdrawSwapper = swapper;
|
|
1082
1140
|
}
|
|
1083
1141
|
const swapInputs = {
|
|
1084
|
-
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(withdrawAmountWithSlippageAndFlashLoanFee, collReserve.stats.decimals)
|
|
1085
|
-
.ceil()
|
|
1086
|
-
.toNumber(),
|
|
1142
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(withdrawAmountWithSlippageAndFlashLoanFee, collReserve.stats.decimals).ceil(),
|
|
1087
1143
|
inputMint: collTokenMint,
|
|
1088
1144
|
outputMint: debtTokenMint,
|
|
1089
1145
|
};
|
|
1090
1146
|
// 5. Get swap ixns
|
|
1091
|
-
const [swapIxns, lookupTablesAddresses] = await withdrawSwapper(swapInputs.inputAmountLamports, swapInputs.inputMint, swapInputs.outputMint, slippagePct);
|
|
1147
|
+
const [swapIxns, lookupTablesAddresses] = await withdrawSwapper(swapInputs.inputAmountLamports.toNumber(), swapInputs.inputMint, swapInputs.outputMint, slippagePct);
|
|
1092
1148
|
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapIxns, []);
|
|
1093
1149
|
const ixns = [
|
|
1094
1150
|
...budgetIxns,
|
|
1095
|
-
...
|
|
1151
|
+
...createAtaIxs,
|
|
1096
1152
|
...fillWsolAtaIxns,
|
|
1097
1153
|
...[flashBorrowIxn],
|
|
1098
1154
|
...repayAction.setupIxs,
|
|
@@ -1103,7 +1159,7 @@ const getDecreaseLeverageIxns = async (props) => {
|
|
|
1103
1159
|
...withdrawAction.cleanupIxs,
|
|
1104
1160
|
...swapInstructions,
|
|
1105
1161
|
...[flashRepayIxn],
|
|
1106
|
-
...
|
|
1162
|
+
...closeWsolAtaIxns,
|
|
1107
1163
|
];
|
|
1108
1164
|
ixns.forEach((ixn, i) => {
|
|
1109
1165
|
console.log(`ixn ${i + 1}: ${ixn.programId.toString()}`);
|