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