@kamino-finance/klend-sdk 5.11.3-beta.0 → 5.11.3-beta.1
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 +23 -23
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +149 -69
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/manager.d.ts +1 -1
- package/dist/classes/manager.js +1 -1
- package/dist/classes/market.d.ts +3 -3
- package/dist/classes/market.d.ts.map +1 -1
- package/dist/classes/market.js +16 -30
- package/dist/classes/market.js.map +1 -1
- package/dist/classes/obligation.d.ts +0 -2
- package/dist/classes/obligation.d.ts.map +1 -1
- package/dist/classes/obligation.js +0 -5
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/vault.js +14 -14
- package/dist/classes/vault.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts +3 -3
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.js +6 -6
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.d.ts +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.js +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.d.ts +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.js +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.d.ts +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.js +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.d.ts +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.js +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.d.ts +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.js +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts +4 -4
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +8 -10
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.d.ts +2 -2
- package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.js +6 -11
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/calcs.d.ts +10 -5
- package/dist/leverage/calcs.d.ts.map +1 -1
- package/dist/leverage/calcs.js +13 -6
- package/dist/leverage/calcs.js.map +1 -1
- package/dist/leverage/operations.d.ts +7 -9
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +72 -78
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +4 -4
- package/dist/leverage/types.d.ts.map +1 -1
- package/dist/utils/ObligationType.d.ts +1 -1
- package/dist/utils/ObligationType.d.ts.map +1 -1
- package/dist/utils/managerTypes.d.ts.map +1 -1
- package/dist/utils/managerTypes.js +52 -7
- package/dist/utils/managerTypes.js.map +1 -1
- package/dist/utils/oracle.d.ts +3 -3
- package/dist/utils/oracle.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/classes/action.ts +162 -75
- package/src/classes/manager.ts +1 -1
- package/src/classes/market.ts +25 -34
- package/src/classes/obligation.ts +0 -6
- package/src/classes/vault.ts +14 -14
- package/src/client.ts +3 -8
- package/src/idl_codegen_kamino_vault/accounts/VaultState.ts +8 -8
- package/src/idl_codegen_kamino_vault/instructions/giveUpPendingFees.ts +2 -2
- package/src/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.ts +2 -2
- package/src/idl_codegen_kamino_vault/instructions/updateSharesMetadata.ts +2 -2
- package/src/idl_codegen_kamino_vault/instructions/updateVaultConfig.ts +2 -2
- package/src/idl_codegen_kamino_vault/instructions/withdrawPendingFees.ts +2 -2
- package/src/idl_kamino_vault.json +7 -7
- package/src/lending_operations/repay_with_collateral_operations.ts +11 -15
- package/src/lending_operations/swap_collateral_operations.ts +7 -19
- package/src/leverage/calcs.ts +18 -2
- package/src/leverage/operations.ts +72 -114
- package/src/leverage/types.ts +4 -4
- package/src/utils/ObligationType.ts +1 -1
- package/src/utils/managerTypes.ts +52 -10
- package/src/utils/oracle.ts +2 -2
|
@@ -3,7 +3,6 @@ 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;
|
|
7
6
|
exports.getDepositWithLeverageSwapInputs = getDepositWithLeverageSwapInputs;
|
|
8
7
|
exports.getDepositWithLeverageIxns = getDepositWithLeverageIxns;
|
|
9
8
|
exports.getWithdrawWithLeverageSwapInputs = getWithdrawWithLeverageSwapInputs;
|
|
@@ -21,7 +20,7 @@ const calcs_1 = require("./calcs");
|
|
|
21
20
|
const spl_token_1 = require("@solana/spl-token");
|
|
22
21
|
const utils_2 = require("./utils");
|
|
23
22
|
const CreationParameters_1 = require("@kamino-finance/kliquidity-sdk/dist/utils/CreationParameters");
|
|
24
|
-
async function getDepositWithLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, depositAmount, priceDebtToColl, slippagePct, obligation, referrer, currentSlot, targetLeverage, selectedTokenMint, kamino, obligationTypeTagOverride,
|
|
23
|
+
async function getDepositWithLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, depositAmount, priceDebtToColl, slippagePct, obligation, referrer, currentSlot, targetLeverage, selectedTokenMint, kamino, obligationTypeTagOverride, scopeFeed, budgetAndPriorityFeeIxs, quoteBufferBps, priceAinB, isKtoken, quoter, useV2Ixs, elevationGroupOverride, }) {
|
|
25
24
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
26
25
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
27
26
|
const solTokenReserve = kaminoMarket.getReserveByMint(spl_token_1.NATIVE_MINT);
|
|
@@ -45,7 +44,7 @@ async function getDepositWithLeverageSwapInputs({ owner, kaminoMarket, debtToken
|
|
|
45
44
|
throw Error('Obligation type tag not supported for leverage, please use 1 - multiply or 3 - leverage');
|
|
46
45
|
}
|
|
47
46
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
48
|
-
const klendIxs = await buildDepositWithLeverageIxns(kaminoMarket, debtReserve, collReserve, owner, obligation ? obligation : obligationType, referrer, currentSlot, depositTokenIsSol,
|
|
47
|
+
const klendIxs = await buildDepositWithLeverageIxns(kaminoMarket, debtReserve, collReserve, owner, obligation ? obligation : obligationType, referrer, currentSlot, depositTokenIsSol, scopeFeed, calcs, budgetAndPriorityFeeIxs, {
|
|
49
48
|
preActionIxs: [],
|
|
50
49
|
swapIxs: [],
|
|
51
50
|
lookupTables: [],
|
|
@@ -123,7 +122,7 @@ async function getDepositWithLeverageCalcs(depositAmount, selectedTokenIsCollTok
|
|
|
123
122
|
}
|
|
124
123
|
return calcs;
|
|
125
124
|
}
|
|
126
|
-
async function getDepositWithLeverageIxns({ owner, kaminoMarket, debtTokenMint, collTokenMint, depositAmount, priceDebtToColl, slippagePct, obligation, referrer, currentSlot, targetLeverage, selectedTokenMint, kamino, obligationTypeTagOverride,
|
|
125
|
+
async function getDepositWithLeverageIxns({ owner, kaminoMarket, debtTokenMint, collTokenMint, depositAmount, priceDebtToColl, slippagePct, obligation, referrer, currentSlot, targetLeverage, selectedTokenMint, kamino, obligationTypeTagOverride, scopeFeed, budgetAndPriorityFeeIxs, quoteBufferBps, priceAinB, isKtoken, quoter, swapper, elevationGroupOverride, useV2Ixs, }) {
|
|
127
126
|
const { swapInputs, initialInputs } = await getDepositWithLeverageSwapInputs({
|
|
128
127
|
owner,
|
|
129
128
|
kaminoMarket,
|
|
@@ -139,7 +138,7 @@ async function getDepositWithLeverageIxns({ owner, kaminoMarket, debtTokenMint,
|
|
|
139
138
|
selectedTokenMint,
|
|
140
139
|
kamino,
|
|
141
140
|
obligationTypeTagOverride,
|
|
142
|
-
|
|
141
|
+
scopeFeed,
|
|
143
142
|
budgetAndPriorityFeeIxs,
|
|
144
143
|
quoteBufferBps,
|
|
145
144
|
priceAinB,
|
|
@@ -171,7 +170,7 @@ async function getDepositWithLeverageIxns({ owner, kaminoMarket, debtTokenMint,
|
|
|
171
170
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
172
171
|
const solTokenReserve = kaminoMarket.getReserveByMint(spl_token_1.NATIVE_MINT);
|
|
173
172
|
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve.getLiquidityMint());
|
|
174
|
-
const ixs = await buildDepositWithLeverageIxns(kaminoMarket, debtReserve, collReserve, owner, initialInputs.obligation, referrer, currentSlot, depositTokenIsSol,
|
|
173
|
+
const ixs = await buildDepositWithLeverageIxns(kaminoMarket, debtReserve, collReserve, owner, initialInputs.obligation, referrer, currentSlot, depositTokenIsSol, scopeFeed, initialInputs.calcs, budgetAndPriorityFeeIxs, {
|
|
175
174
|
preActionIxs: [],
|
|
176
175
|
swapIxs: swapIxs,
|
|
177
176
|
lookupTables: lookupTables,
|
|
@@ -183,13 +182,13 @@ async function getDepositWithLeverageIxns({ owner, kaminoMarket, debtTokenMint,
|
|
|
183
182
|
initialInputs,
|
|
184
183
|
};
|
|
185
184
|
}
|
|
186
|
-
async function buildDepositWithLeverageIxns(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, depositTokenIsSol,
|
|
185
|
+
async function buildDepositWithLeverageIxns(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, depositTokenIsSol, scopeFeed, calcs, budgetAndPriorityFeeIxs, swapQuoteIxs, strategy, collIsKtoken, useV2Ixs, elevationGroupOverride) {
|
|
187
186
|
const budgetIxns = budgetAndPriorityFeeIxs || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
188
187
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
189
188
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
190
189
|
const collTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collTokenMint, owner, false, collReserve.getLiquidityTokenProgram());
|
|
191
190
|
const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
|
|
192
|
-
// 1. Create atas & budget
|
|
191
|
+
// 1. Create atas & budget txns
|
|
193
192
|
let mintsToCreateAtas;
|
|
194
193
|
if (collIsKtoken) {
|
|
195
194
|
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
@@ -242,11 +241,10 @@ async function buildDepositWithLeverageIxns(market, debtReserve, collReserve, ow
|
|
|
242
241
|
if (depositTokenIsSol) {
|
|
243
242
|
fillWsolAtaIxns.push(...(0, utils_1.getTransferWsolIxns)(owner, (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, owner), (0, classes_2.numberToLamportsDecimal)(calcs.initDepositInSol, utils_1.SOL_DECIMALS).ceil()));
|
|
244
243
|
}
|
|
245
|
-
const scopeRefreshIxn = await (0, exports.getScopeRefreshIx)(market, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
246
244
|
// 2. Flash borrow & repay the collateral amount needed for given leverage
|
|
247
245
|
// if user deposits coll, then we borrow the diff, else we borrow the entire amount
|
|
248
246
|
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
249
|
-
borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length
|
|
247
|
+
borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length,
|
|
250
248
|
walletPublicKey: owner,
|
|
251
249
|
lendingMarketAuthority: market.getLendingMarketAuthority(),
|
|
252
250
|
lendingMarketAddress: market.getAddress(),
|
|
@@ -259,32 +257,33 @@ async function buildDepositWithLeverageIxns(market, debtReserve, collReserve, ow
|
|
|
259
257
|
programId: market.programId,
|
|
260
258
|
});
|
|
261
259
|
// 3. Deposit initial tokens + borrowed tokens into reserve
|
|
260
|
+
const scopeRefresh = scopeFeed ? { includeScopeRefresh: true, scopeFeed: scopeFeed } : undefined;
|
|
262
261
|
const kaminoDepositAndBorrowAction = await classes_1.KaminoAction.buildDepositAndBorrowTxns(market, (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.collTokenToDeposit : calcs.collTokenToDeposit, collReserve.stats.decimals)
|
|
263
262
|
.floor()
|
|
264
263
|
.toString(), collTokenMint, (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.debtTokenToBorrow : calcs.debtTokenToBorrow, debtReserve.stats.decimals)
|
|
265
264
|
.ceil()
|
|
266
|
-
.toString(), debtTokenMint, owner, obligation, useV2Ixs,
|
|
265
|
+
.toString(), debtTokenMint, owner, obligation, useV2Ixs, 0, false, elevationGroupOverride === 0 ? false : true, // emode
|
|
267
266
|
false, // to be checked and created in a setup tx in the UI
|
|
268
267
|
false, // to be checked and created in a setup tx in the UI
|
|
269
|
-
referrer, currentSlot);
|
|
268
|
+
referrer, currentSlot, scopeRefresh);
|
|
270
269
|
// 4. Swap
|
|
271
270
|
const { swapIxs } = swapQuoteIxs;
|
|
272
271
|
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapIxs, []);
|
|
273
272
|
if (!collIsKtoken) {
|
|
274
273
|
return [
|
|
275
|
-
...scopeRefreshIxn,
|
|
276
274
|
...budgetIxns,
|
|
277
275
|
...atasAndCreateIxns.map((x) => x.createAtaIx),
|
|
278
276
|
...fillWsolAtaIxns,
|
|
279
277
|
...[flashBorrowIxn],
|
|
280
|
-
...
|
|
278
|
+
...kaminoDepositAndBorrowAction.setupIxs,
|
|
279
|
+
...classes_1.KaminoAction.actionToLendingIxs(kaminoDepositAndBorrowAction),
|
|
280
|
+
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
281
281
|
...swapInstructions,
|
|
282
282
|
...[flashRepayIxn],
|
|
283
283
|
];
|
|
284
284
|
}
|
|
285
285
|
else {
|
|
286
286
|
return [
|
|
287
|
-
...scopeRefreshIxn,
|
|
288
287
|
...budgetIxns,
|
|
289
288
|
...atasAndCreateIxns.map((x) => x.createAtaIx),
|
|
290
289
|
...fillWsolAtaIxns,
|
|
@@ -297,7 +296,7 @@ async function buildDepositWithLeverageIxns(market, debtReserve, collReserve, ow
|
|
|
297
296
|
];
|
|
298
297
|
}
|
|
299
298
|
}
|
|
300
|
-
async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, deposited, borrowed, obligation, referrer, currentSlot, withdrawAmount, priceCollToDebt, slippagePct, isClosingPosition, selectedTokenMint, budgetAndPriorityFeeIxs, kamino,
|
|
299
|
+
async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, deposited, borrowed, obligation, referrer, currentSlot, withdrawAmount, priceCollToDebt, slippagePct, isClosingPosition, selectedTokenMint, budgetAndPriorityFeeIxs, kamino, scopeFeed, quoteBufferBps, isKtoken, quoter, useV2Ixs, }) {
|
|
301
300
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
302
301
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
303
302
|
const flashLoanFee = debtReserve.getFlashLoanFee() || new decimal_js_1.default(0);
|
|
@@ -307,7 +306,7 @@ async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtToke
|
|
|
307
306
|
const solTokenReserve = kaminoMarket.getReserveByMint(spl_token_1.NATIVE_MINT);
|
|
308
307
|
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve.getLiquidityMint());
|
|
309
308
|
const calcs = (0, calcs_1.withdrawLeverageCalcs)(kaminoMarket, collReserve, debtReserve, priceCollToDebt, withdrawAmount, deposited, borrowed, currentSlot, isClosingPosition, selectedTokenIsCollToken, selectedTokenMint, obligation, flashLoanFee, slippagePct);
|
|
310
|
-
const klendIxs = await buildWithdrawWithLeverageIxns(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol,
|
|
309
|
+
const klendIxs = await buildWithdrawWithLeverageIxns(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol, scopeFeed, calcs, budgetAndPriorityFeeIxs, {
|
|
311
310
|
preActionIxs: [],
|
|
312
311
|
swapIxs: [],
|
|
313
312
|
lookupTables: [],
|
|
@@ -342,7 +341,7 @@ async function getWithdrawWithLeverageSwapInputs({ owner, kaminoMarket, debtToke
|
|
|
342
341
|
},
|
|
343
342
|
};
|
|
344
343
|
}
|
|
345
|
-
async function getWithdrawWithLeverageIxns({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, deposited, borrowed, referrer, currentSlot, withdrawAmount, priceCollToDebt, slippagePct, isClosingPosition, selectedTokenMint, budgetAndPriorityFeeIxs, kamino,
|
|
344
|
+
async function getWithdrawWithLeverageIxns({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, deposited, borrowed, referrer, currentSlot, withdrawAmount, priceCollToDebt, slippagePct, isClosingPosition, selectedTokenMint, budgetAndPriorityFeeIxs, kamino, scopeFeed, quoteBufferBps, isKtoken, quoter, swapper, useV2Ixs, }) {
|
|
346
345
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
347
346
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
348
347
|
const solTokenReserve = kaminoMarket.getReserveByMint(spl_token_1.NATIVE_MINT);
|
|
@@ -364,7 +363,7 @@ async function getWithdrawWithLeverageIxns({ owner, kaminoMarket, debtTokenMint,
|
|
|
364
363
|
selectedTokenMint,
|
|
365
364
|
budgetAndPriorityFeeIxs,
|
|
366
365
|
kamino,
|
|
367
|
-
|
|
366
|
+
scopeFeed,
|
|
368
367
|
quoteBufferBps,
|
|
369
368
|
isKtoken,
|
|
370
369
|
quoter,
|
|
@@ -390,7 +389,7 @@ async function getWithdrawWithLeverageIxns({ owner, kaminoMarket, debtTokenMint,
|
|
|
390
389
|
console.log('Strategy lookup table not found');
|
|
391
390
|
}
|
|
392
391
|
}
|
|
393
|
-
const ixs = await buildWithdrawWithLeverageIxns(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol,
|
|
392
|
+
const ixs = await buildWithdrawWithLeverageIxns(kaminoMarket, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol, scopeFeed, initialInputs.calcs, budgetAndPriorityFeeIxs, {
|
|
394
393
|
preActionIxs: [],
|
|
395
394
|
swapIxs,
|
|
396
395
|
lookupTables,
|
|
@@ -403,7 +402,7 @@ async function getWithdrawWithLeverageIxns({ owner, kaminoMarket, debtTokenMint,
|
|
|
403
402
|
initialInputs: initialInputs,
|
|
404
403
|
};
|
|
405
404
|
}
|
|
406
|
-
async function buildWithdrawWithLeverageIxns(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol,
|
|
405
|
+
async function buildWithdrawWithLeverageIxns(market, debtReserve, collReserve, owner, obligation, referrer, currentSlot, isClosingPosition, depositTokenIsSol, scopeFeed, calcs, budgetAndPriorityFeeIxs, swapQuoteIxs, strategy, collIsKtoken, useV2Ixs) {
|
|
407
406
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
408
407
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
409
408
|
const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
|
|
@@ -462,7 +461,7 @@ async function buildWithdrawWithLeverageIxns(market, debtReserve, collReserve, o
|
|
|
462
461
|
closeWsolAtaIxns.push((0, spl_token_1.createCloseAccountInstruction)(wsolAta, owner, owner, [], spl_token_1.TOKEN_PROGRAM_ID));
|
|
463
462
|
}
|
|
464
463
|
const budgetIxns = budgetAndPriorityFeeIxs || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
465
|
-
// TODO:
|
|
464
|
+
// TODO: Might be worth removing as it's only needed for Ktokens
|
|
466
465
|
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
467
466
|
const fillWsolAtaIxns = [];
|
|
468
467
|
if (debtTokenMint.equals(spl_token_1.NATIVE_MINT)) {
|
|
@@ -470,12 +469,11 @@ async function buildWithdrawWithLeverageIxns(market, debtReserve, collReserve, o
|
|
|
470
469
|
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
471
470
|
fillWsolAtaIxns.push(...(0, utils_1.getTransferWsolIxns)(owner, (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, owner), (0, classes_2.numberToLamportsDecimal)(balanceToWrap, utils_1.SOL_DECIMALS).ceil()));
|
|
472
471
|
}
|
|
473
|
-
const scopeRefreshIxn = await (0, exports.getScopeRefreshIx)(market, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
474
472
|
// 2. Prepare the flash borrow and flash repay amounts and ixns
|
|
475
473
|
// We borrow exactly how much we need to repay
|
|
476
474
|
// and repay that + flash amount fee
|
|
477
475
|
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
478
|
-
borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length
|
|
476
|
+
borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length,
|
|
479
477
|
walletPublicKey: owner,
|
|
480
478
|
lendingMarketAuthority: market.getLendingMarketAuthority(),
|
|
481
479
|
lendingMarketAddress: market.getAddress(),
|
|
@@ -487,26 +485,27 @@ async function buildWithdrawWithLeverageIxns(market, debtReserve, collReserve, o
|
|
|
487
485
|
referrerTokenState: market.programId,
|
|
488
486
|
programId: market.programId,
|
|
489
487
|
});
|
|
490
|
-
//
|
|
488
|
+
// 6. Repay borrowed tokens and Withdraw tokens from reserve that will be swapped to repay flash loan
|
|
491
489
|
const repayAndWithdrawAction = await classes_1.KaminoAction.buildRepayAndWithdrawTxns(market, isClosingPosition ? utils_1.U64_MAX : (0, classes_2.numberToLamportsDecimal)(calcs.repayAmount, debtReserve.stats.decimals).floor().toString(), debtTokenMint, isClosingPosition
|
|
492
490
|
? utils_1.U64_MAX
|
|
493
|
-
: (0, classes_2.numberToLamportsDecimal)(calcs.depositTokenWithdrawAmount, collReserve.stats.decimals).ceil().toString(), collTokenMint, owner, currentSlot, obligation, useV2Ixs,
|
|
491
|
+
: (0, classes_2.numberToLamportsDecimal)(calcs.depositTokenWithdrawAmount, collReserve.stats.decimals).ceil().toString(), collTokenMint, owner, currentSlot, obligation, useV2Ixs, 0, false, false, // 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)
|
|
494
492
|
false, // 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)
|
|
495
|
-
isClosingPosition, referrer);
|
|
493
|
+
isClosingPosition, referrer, { includeScopeRefresh: true, scopeFeed: scopeFeed });
|
|
496
494
|
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapQuoteIxs.swapIxs, []);
|
|
497
495
|
return [
|
|
498
|
-
...scopeRefreshIxn,
|
|
499
496
|
...budgetIxns,
|
|
500
497
|
...atasAndCreateIxns.map((x) => x.createAtaIx),
|
|
501
498
|
...fillWsolAtaIxns,
|
|
502
499
|
...[flashBorrowIxn],
|
|
503
|
-
...
|
|
500
|
+
...repayAndWithdrawAction.setupIxs,
|
|
501
|
+
...classes_1.KaminoAction.actionToLendingIxs(repayAndWithdrawAction),
|
|
502
|
+
...repayAndWithdrawAction.cleanupIxs,
|
|
504
503
|
...swapInstructions,
|
|
505
504
|
...[flashRepayIxn],
|
|
506
505
|
...closeWsolAtaIxns,
|
|
507
506
|
];
|
|
508
507
|
}
|
|
509
|
-
async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino,
|
|
508
|
+
async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino, scopeFeed, quoteBufferBps, isKtoken, quoter, useV2Ixs, }) {
|
|
510
509
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
511
510
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
512
511
|
const deposited = (0, classes_1.lamportsToNumberDecimal)(depositedLamports, collReserve.stats.decimals);
|
|
@@ -517,11 +516,14 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
517
516
|
const currentLeverage = obligation.refreshedStats.leverage;
|
|
518
517
|
const isDepositViaLeverage = targetLeverage.gte(new decimal_js_1.default(currentLeverage));
|
|
519
518
|
let flashLoanFee;
|
|
519
|
+
let borrowFee;
|
|
520
520
|
if (isDepositViaLeverage) {
|
|
521
521
|
flashLoanFee = collReserve.getFlashLoanFee() || new decimal_js_1.default(0);
|
|
522
|
+
borrowFee = collReserve.getBorrowFee() || new decimal_js_1.default(0);
|
|
522
523
|
}
|
|
523
524
|
else {
|
|
524
525
|
flashLoanFee = debtReserve.getFlashLoanFee() || new decimal_js_1.default(0);
|
|
526
|
+
borrowFee = debtReserve.getBorrowFee() || new decimal_js_1.default(0);
|
|
525
527
|
}
|
|
526
528
|
const { adjustDepositPosition, adjustBorrowPosition } = (0, calcs_1.calcAdjustAmounts)({
|
|
527
529
|
currentDepositPosition: deposited,
|
|
@@ -529,6 +531,7 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
529
531
|
targetLeverage: targetLeverage,
|
|
530
532
|
priceCollToDebt: priceCollToDebt,
|
|
531
533
|
flashLoanFee: new decimal_js_1.default(flashLoanFee),
|
|
534
|
+
borrowFee,
|
|
532
535
|
});
|
|
533
536
|
const isDeposit = adjustDepositPosition.gte(0) && adjustBorrowPosition.gte(0);
|
|
534
537
|
if (isDepositViaLeverage !== isDeposit) {
|
|
@@ -537,7 +540,7 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
537
540
|
if (isDeposit) {
|
|
538
541
|
const calcs = await (0, calcs_1.adjustDepositLeverageCalcs)(kaminoMarket, owner, debtReserve, adjustDepositPosition, adjustBorrowPosition, priceDebtToColl, flashLoanFee, slippagePct, collIsKtoken);
|
|
539
542
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
540
|
-
const klendIxs = await buildIncreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy,
|
|
543
|
+
const klendIxs = await buildIncreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeFeed, collIsKtoken, {
|
|
541
544
|
preActionIxs: [],
|
|
542
545
|
swapIxs: [],
|
|
543
546
|
lookupTables: [],
|
|
@@ -557,6 +560,7 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
557
560
|
targetLeverage: targetLeverage,
|
|
558
561
|
priceCollToDebt: new decimal_js_1.default(1).div(swapQuote.priceAInB),
|
|
559
562
|
flashLoanFee: new decimal_js_1.default(flashLoanFee),
|
|
563
|
+
borrowFee,
|
|
560
564
|
});
|
|
561
565
|
const calcsQuotePrice = await (0, calcs_1.adjustDepositLeverageCalcs)(kaminoMarket, owner, debtReserve, adjustDepositPositionQuotePrice, adjustBorrowPositionQuotePrice, swapQuote.priceAInB, flashLoanFee, slippagePct, collIsKtoken);
|
|
562
566
|
const swapInputAmountQuotePrice = (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcsQuotePrice.borrowAmount : calcsQuotePrice.amountToFlashBorrowDebt, debtReserve.state.liquidity.mintDecimals.toNumber()).ceil();
|
|
@@ -586,7 +590,7 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
586
590
|
}
|
|
587
591
|
else {
|
|
588
592
|
const calcs = (0, calcs_1.adjustWithdrawLeverageCalcs)(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
|
|
589
|
-
const klendIxs = await buildDecreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy,
|
|
593
|
+
const klendIxs = await buildDecreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeFeed, collIsKtoken, {
|
|
590
594
|
preActionIxs: [],
|
|
591
595
|
swapIxs: [],
|
|
592
596
|
lookupTables: [],
|
|
@@ -606,6 +610,7 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
606
610
|
targetLeverage: targetLeverage,
|
|
607
611
|
priceCollToDebt: swapQuote.priceAInB,
|
|
608
612
|
flashLoanFee: new decimal_js_1.default(flashLoanFee),
|
|
613
|
+
borrowFee,
|
|
609
614
|
});
|
|
610
615
|
const calcsQuotePrice = (0, calcs_1.adjustWithdrawLeverageCalcs)(adjustDepositPositionQuotePrice, adjustBorrowPositionQuotePrice, flashLoanFee, slippagePct);
|
|
611
616
|
const swapInputAmountQuotePrice = (0, classes_2.numberToLamportsDecimal)(calcsQuotePrice.withdrawAmountWithSlippageAndFlashLoanFee, collReserve.state.liquidity.mintDecimals.toNumber()).ceil();
|
|
@@ -630,7 +635,7 @@ async function getAdjustLeverageSwapInputs({ owner, kaminoMarket, debtTokenMint,
|
|
|
630
635
|
};
|
|
631
636
|
}
|
|
632
637
|
}
|
|
633
|
-
async function getAdjustLeverageIxns({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino,
|
|
638
|
+
async function getAdjustLeverageIxns({ owner, kaminoMarket, debtTokenMint, collTokenMint, obligation, depositedLamports, borrowedLamports, referrer, currentSlot, targetLeverage, priceCollToDebt, priceDebtToColl, slippagePct, budgetAndPriorityFeeIxs, kamino, scopeFeed, quoteBufferBps, priceAinB, isKtoken, quoter, swapper, useV2Ixs, }) {
|
|
634
639
|
const { swapInputs, initialInputs } = await getAdjustLeverageSwapInputs({
|
|
635
640
|
owner,
|
|
636
641
|
kaminoMarket,
|
|
@@ -647,7 +652,7 @@ async function getAdjustLeverageIxns({ owner, kaminoMarket, debtTokenMint, collT
|
|
|
647
652
|
slippagePct,
|
|
648
653
|
budgetAndPriorityFeeIxs,
|
|
649
654
|
kamino,
|
|
650
|
-
|
|
655
|
+
scopeFeed,
|
|
651
656
|
quoteBufferBps,
|
|
652
657
|
priceAinB,
|
|
653
658
|
isKtoken,
|
|
@@ -668,7 +673,7 @@ async function getAdjustLeverageIxns({ owner, kaminoMarket, debtTokenMint, collT
|
|
|
668
673
|
}
|
|
669
674
|
const { swapIxs, lookupTables } = await depositSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
670
675
|
// TODO: marius why are we not using both adjustDepositPosition & adjustBorrowPosition
|
|
671
|
-
const ixs = await buildIncreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy,
|
|
676
|
+
const ixs = await buildIncreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeFeed, initialInputs.collIsKtoken, {
|
|
672
677
|
preActionIxs: [],
|
|
673
678
|
swapIxs,
|
|
674
679
|
lookupTables,
|
|
@@ -694,7 +699,7 @@ async function getAdjustLeverageIxns({ owner, kaminoMarket, debtTokenMint, collT
|
|
|
694
699
|
}
|
|
695
700
|
// 5. Get swap ixns
|
|
696
701
|
const { swapIxs, lookupTables } = await withdrawSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
697
|
-
const ixs = await buildDecreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy,
|
|
702
|
+
const ixs = await buildDecreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, initialInputs.calcs, initialInputs.strategy, scopeFeed, initialInputs.collIsKtoken, {
|
|
698
703
|
preActionIxs: [],
|
|
699
704
|
swapIxs,
|
|
700
705
|
lookupTables,
|
|
@@ -710,7 +715,7 @@ async function getAdjustLeverageIxns({ owner, kaminoMarket, debtTokenMint, collT
|
|
|
710
715
|
/**
|
|
711
716
|
* Deposit and borrow tokens if leverage increased
|
|
712
717
|
*/
|
|
713
|
-
async function buildIncreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy,
|
|
718
|
+
async function buildIncreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeFeed, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxns, useV2Ixs) {
|
|
714
719
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
715
720
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
716
721
|
const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
|
|
@@ -765,10 +770,9 @@ async function buildIncreaseLeverageIxns(owner, kaminoMarket, collTokenMint, deb
|
|
|
765
770
|
];
|
|
766
771
|
}
|
|
767
772
|
const atasAndCreateIxns = (0, utils_1.createAtasIdempotent)(owner, mintsToCreateAtas);
|
|
768
|
-
const scopeRefreshIxn = await (0, exports.getScopeRefreshIx)(kaminoMarket, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
769
773
|
// 2. Create borrow flash loan instruction
|
|
770
774
|
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
771
|
-
borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length
|
|
775
|
+
borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length, // TODO: how about user metadata ixns
|
|
772
776
|
walletPublicKey: owner,
|
|
773
777
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
774
778
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -780,33 +784,39 @@ async function buildIncreaseLeverageIxns(owner, kaminoMarket, collTokenMint, deb
|
|
|
780
784
|
referrerTokenState: kaminoMarket.programId,
|
|
781
785
|
programId: kaminoMarket.programId,
|
|
782
786
|
});
|
|
783
|
-
const depositAction = await classes_1.KaminoAction.buildDepositTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(calcs.adjustDepositPosition, collReserve.stats.decimals).floor().toString(), collTokenMint, owner, obligation, useV2Ixs,
|
|
787
|
+
const depositAction = await classes_1.KaminoAction.buildDepositTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(calcs.adjustDepositPosition, collReserve.stats.decimals).floor().toString(), collTokenMint, owner, obligation, useV2Ixs, 0, false, false, false, // 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)
|
|
784
788
|
false, // 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)
|
|
785
|
-
referrer, currentSlot);
|
|
789
|
+
referrer, currentSlot, { includeScopeRefresh: true, scopeFeed: scopeFeed });
|
|
786
790
|
// 4. Borrow tokens in borrow token reserve that will be swapped to repay flash loan
|
|
787
|
-
const borrowAction = await classes_1.KaminoAction.buildBorrowTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(calcs.borrowAmount, debtReserve.stats.decimals).ceil().toString(), debtTokenMint, owner, obligation, useV2Ixs,
|
|
791
|
+
const borrowAction = await classes_1.KaminoAction.buildBorrowTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(calcs.borrowAmount, debtReserve.stats.decimals).ceil().toString(), debtTokenMint, owner, obligation, useV2Ixs, 0, false, false, false, // 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)
|
|
788
792
|
false, // 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)
|
|
789
|
-
referrer, currentSlot);
|
|
793
|
+
referrer, currentSlot, { includeScopeRefresh: true, scopeFeed: scopeFeed });
|
|
790
794
|
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapQuoteIxs.swapIxs, []);
|
|
791
795
|
const ixs = !collIsKtoken
|
|
792
796
|
? [
|
|
793
|
-
...scopeRefreshIxn,
|
|
794
797
|
...budgetIxns,
|
|
795
798
|
...atasAndCreateIxns.map((x) => x.createAtaIx),
|
|
796
799
|
...[flashBorrowIxn],
|
|
797
|
-
...
|
|
798
|
-
...
|
|
800
|
+
...depositAction.setupIxs,
|
|
801
|
+
...depositAction.lendingIxs,
|
|
802
|
+
...depositAction.cleanupIxs,
|
|
803
|
+
...borrowAction.setupIxs,
|
|
804
|
+
...borrowAction.lendingIxs,
|
|
805
|
+
...borrowAction.cleanupIxs,
|
|
799
806
|
...swapInstructions,
|
|
800
807
|
...[flashRepayIxn],
|
|
801
808
|
]
|
|
802
809
|
: [
|
|
803
|
-
...scopeRefreshIxn,
|
|
804
810
|
...budgetIxns,
|
|
805
811
|
...atasAndCreateIxns.map((x) => x.createAtaIx),
|
|
806
812
|
...[flashBorrowIxn],
|
|
807
813
|
...swapInstructions,
|
|
808
|
-
...
|
|
809
|
-
...
|
|
814
|
+
...depositAction.setupIxs,
|
|
815
|
+
...depositAction.lendingIxs,
|
|
816
|
+
...depositAction.cleanupIxs,
|
|
817
|
+
...borrowAction.setupIxs,
|
|
818
|
+
...borrowAction.lendingIxs,
|
|
819
|
+
...borrowAction.cleanupIxs,
|
|
810
820
|
...[flashRepayIxn],
|
|
811
821
|
];
|
|
812
822
|
return ixs;
|
|
@@ -814,7 +824,7 @@ async function buildIncreaseLeverageIxns(owner, kaminoMarket, collTokenMint, deb
|
|
|
814
824
|
/**
|
|
815
825
|
* Withdraw and repay tokens if leverage decreased
|
|
816
826
|
*/
|
|
817
|
-
async function buildDecreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy,
|
|
827
|
+
async function buildDecreaseLeverageIxns(owner, kaminoMarket, collTokenMint, debtTokenMint, obligation, referrer, currentSlot, calcs, strategy, scopeFeed, collIsKtoken, swapQuoteIxs, budgetAndPriorityFeeIxns, useV2Ixs) {
|
|
818
828
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
819
829
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
820
830
|
const debtTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtTokenMint, owner, false, debtReserve.getLiquidityTokenProgram());
|
|
@@ -879,10 +889,9 @@ async function buildDecreaseLeverageIxns(owner, kaminoMarket, collTokenMint, deb
|
|
|
879
889
|
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
880
890
|
fillWsolAtaIxns.push(...(0, utils_1.getTransferWsolIxns)(owner, wsolAta, (0, classes_2.numberToLamportsDecimal)(balanceToWrap, debtReserve.stats.decimals).ceil()));
|
|
881
891
|
}
|
|
882
|
-
const scopeRefreshIxn = await (0, exports.getScopeRefreshIx)(kaminoMarket, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
883
892
|
// 3. Flash borrow & repay amount to repay (debt)
|
|
884
893
|
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
885
|
-
borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length
|
|
894
|
+
borrowIxnIndex: budgetIxns.length + atasAndCreateIxns.length + fillWsolAtaIxns.length,
|
|
886
895
|
walletPublicKey: owner,
|
|
887
896
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
888
897
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -895,45 +904,30 @@ async function buildDecreaseLeverageIxns(owner, kaminoMarket, collTokenMint, deb
|
|
|
895
904
|
programId: kaminoMarket.programId,
|
|
896
905
|
});
|
|
897
906
|
// 4. Actually do the repay of the flash borrowed amounts
|
|
898
|
-
const
|
|
907
|
+
const scopeRefresh = scopeFeed ? { includeScopeRefresh: true, scopeFeed: scopeFeed } : undefined;
|
|
908
|
+
const repayAction = await classes_1.KaminoAction.buildRepayTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(decimal_js_1.default.abs(calcs.adjustBorrowPosition), debtReserve.stats.decimals).floor().toString(), debtTokenMint, owner, obligation, useV2Ixs, currentSlot, undefined, 0, false, false, false, // 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)
|
|
899
909
|
false, // 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)
|
|
900
|
-
referrer);
|
|
910
|
+
referrer, scopeRefresh);
|
|
901
911
|
// 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
|
|
902
|
-
const withdrawAction = await classes_1.KaminoAction.buildWithdrawTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(calcs.withdrawAmountWithSlippageAndFlashLoanFee, collReserve.stats.decimals).ceil().toString(), collTokenMint, owner, obligation, useV2Ixs,
|
|
912
|
+
const withdrawAction = await classes_1.KaminoAction.buildWithdrawTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(calcs.withdrawAmountWithSlippageAndFlashLoanFee, collReserve.stats.decimals).ceil().toString(), collTokenMint, owner, obligation, useV2Ixs, 0, false, false, false, // 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)
|
|
903
913
|
false, // 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)
|
|
904
|
-
referrer, currentSlot);
|
|
914
|
+
referrer, currentSlot, { includeScopeRefresh: true, scopeFeed: scopeFeed });
|
|
905
915
|
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapQuoteIxs.swapIxs, []);
|
|
906
916
|
const ixns = [
|
|
907
|
-
...scopeRefreshIxn,
|
|
908
917
|
...budgetIxns,
|
|
909
918
|
...atasAndCreateIxns.map((x) => x.createAtaIx),
|
|
910
919
|
...fillWsolAtaIxns,
|
|
911
920
|
...[flashBorrowIxn],
|
|
912
|
-
...
|
|
913
|
-
...
|
|
921
|
+
...repayAction.setupIxs,
|
|
922
|
+
...repayAction.lendingIxs,
|
|
923
|
+
...repayAction.cleanupIxs,
|
|
924
|
+
...withdrawAction.setupIxs,
|
|
925
|
+
...withdrawAction.lendingIxs,
|
|
926
|
+
...withdrawAction.cleanupIxs,
|
|
914
927
|
...swapInstructions,
|
|
915
928
|
...[flashRepayIxn],
|
|
916
929
|
...closeWsolAtaIxns,
|
|
917
930
|
];
|
|
918
931
|
return ixns;
|
|
919
932
|
}
|
|
920
|
-
const getScopeRefreshIx = async (market, collReserve, debtReserve, obligation, scopeRefreshConfig) => {
|
|
921
|
-
const allReserves = obligation && (0, classes_1.isKaminoObligation)(obligation)
|
|
922
|
-
? new utils_1.PublicKeySet([
|
|
923
|
-
...obligation.getDeposits().map((x) => x.reserveAddress),
|
|
924
|
-
...obligation.getBorrows().map((x) => x.reserveAddress),
|
|
925
|
-
collReserve.address,
|
|
926
|
-
debtReserve.address,
|
|
927
|
-
]).toArray()
|
|
928
|
-
: new utils_1.PublicKeySet([collReserve.address, debtReserve.address]).toArray();
|
|
929
|
-
const tokenIds = (0, classes_1.getTokenIdsForScopeRefresh)(market, allReserves);
|
|
930
|
-
const scopeRefreshIxns = [];
|
|
931
|
-
if (tokenIds.length > 0 && scopeRefreshConfig) {
|
|
932
|
-
scopeRefreshIxns.push(await scopeRefreshConfig.scope.refreshPriceListIx({
|
|
933
|
-
feed: scopeRefreshConfig.scopeFeed,
|
|
934
|
-
}, tokenIds));
|
|
935
|
-
}
|
|
936
|
-
return scopeRefreshIxns;
|
|
937
|
-
};
|
|
938
|
-
exports.getScopeRefreshIx = getScopeRefreshIx;
|
|
939
933
|
//# sourceMappingURL=operations.js.map
|