@kamino-finance/klend-sdk 6.0.5-beta.3 → 6.0.5-beta.5
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.map +1 -1
- package/dist/classes/market.js +2 -2
- 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 +5 -2
- 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
package/package.json
CHANGED
package/src/classes/market.ts
CHANGED
|
@@ -1214,8 +1214,11 @@ export class KaminoMarket {
|
|
|
1214
1214
|
for (const [feed, account] of oraclePrices) {
|
|
1215
1215
|
oraclePriceMap.set(feed, account);
|
|
1216
1216
|
}
|
|
1217
|
-
for (const reserve of this.reserves
|
|
1218
|
-
reserveOraclePrices.set(
|
|
1217
|
+
for (const [reserveAddress, reserve] of this.reserves) {
|
|
1218
|
+
reserveOraclePrices.set(
|
|
1219
|
+
reserveAddress,
|
|
1220
|
+
oraclePriceMap.get(reserve.state.config.tokenInfo.scopeConfiguration.priceFeed)!
|
|
1221
|
+
);
|
|
1219
1222
|
}
|
|
1220
1223
|
return reserveOraclePrices;
|
|
1221
1224
|
}
|
|
@@ -117,26 +117,30 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
|
|
|
117
117
|
const inputAmountLamports = Decimal.min(maxWithdrawableCollLamports, maxCollNeededFromOracle);
|
|
118
118
|
|
|
119
119
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
120
|
-
const klendIxs: LeverageIxsOutput =
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
120
|
+
const klendIxs: LeverageIxsOutput = (
|
|
121
|
+
await buildRepayWithCollateralIxs(
|
|
122
|
+
kaminoMarket,
|
|
123
|
+
debtReserve,
|
|
124
|
+
collReserve,
|
|
125
|
+
obligation,
|
|
126
|
+
referrer,
|
|
127
|
+
currentSlot,
|
|
128
|
+
budgetAndPriorityFeeIxs,
|
|
129
|
+
scopeRefreshConfig,
|
|
130
|
+
[
|
|
131
|
+
{
|
|
132
|
+
preActionIxs: [],
|
|
133
|
+
swapIxs: [],
|
|
134
|
+
lookupTables: [],
|
|
135
|
+
quote: {} as SwapQuote<QuoteResponse>,
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
isClosingPosition,
|
|
139
|
+
repayAmountLamports,
|
|
140
|
+
inputAmountLamports,
|
|
141
|
+
useV2Ixs
|
|
142
|
+
)
|
|
143
|
+
)[0];
|
|
140
144
|
const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
|
|
141
145
|
|
|
142
146
|
const swapQuoteInputs: SwapInputs = {
|
|
@@ -238,34 +242,32 @@ export async function getRepayWithCollIxs<QuoteResponse>({
|
|
|
238
242
|
|
|
239
243
|
const swapResponses = await swapper(swapInputs, initialInputs.klendAccounts, swapQuote);
|
|
240
244
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
swapInputs.inputAmountLamports,
|
|
256
|
-
useV2Ixs
|
|
257
|
-
);
|
|
258
|
-
|
|
259
|
-
return {
|
|
260
|
-
ixs: ixs.instructions,
|
|
261
|
-
lookupTables: swapResponse.lookupTables,
|
|
262
|
-
swapInputs,
|
|
263
|
-
flashLoanInfo: ixs.flashLoanInfo,
|
|
264
|
-
initialInputs,
|
|
265
|
-
quote: swapResponse.quote.quoteResponse,
|
|
266
|
-
};
|
|
267
|
-
})
|
|
245
|
+
const repayWithCollateralIxs = await buildRepayWithCollateralIxs(
|
|
246
|
+
kaminoMarket,
|
|
247
|
+
debtReserve,
|
|
248
|
+
collReserve,
|
|
249
|
+
obligation,
|
|
250
|
+
referrer,
|
|
251
|
+
currentSlot,
|
|
252
|
+
budgetAndPriorityFeeIxs,
|
|
253
|
+
scopeRefreshConfig,
|
|
254
|
+
swapResponses,
|
|
255
|
+
isClosingPosition,
|
|
256
|
+
debtRepayAmountLamports,
|
|
257
|
+
swapInputs.inputAmountLamports,
|
|
258
|
+
useV2Ixs
|
|
268
259
|
);
|
|
260
|
+
|
|
261
|
+
return repayWithCollateralIxs.map((ixs, index) => {
|
|
262
|
+
return {
|
|
263
|
+
ixs: ixs.instructions,
|
|
264
|
+
lookupTables: swapResponses[index].lookupTables,
|
|
265
|
+
swapInputs,
|
|
266
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
267
|
+
initialInputs,
|
|
268
|
+
quote: swapResponses[index].quote.quoteResponse,
|
|
269
|
+
};
|
|
270
|
+
});
|
|
269
271
|
}
|
|
270
272
|
|
|
271
273
|
async function buildRepayWithCollateralIxs<QuoteResponse>(
|
|
@@ -277,12 +279,12 @@ async function buildRepayWithCollateralIxs<QuoteResponse>(
|
|
|
277
279
|
currentSlot: number,
|
|
278
280
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
279
281
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
280
|
-
|
|
282
|
+
swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
|
|
281
283
|
isClosingPosition: boolean,
|
|
282
284
|
debtRepayAmountLamports: Decimal,
|
|
283
285
|
collWithdrawLamports: Decimal,
|
|
284
286
|
useV2Ixs: boolean
|
|
285
|
-
): Promise<LeverageIxsOutput> {
|
|
287
|
+
): Promise<LeverageIxsOutput[]> {
|
|
286
288
|
// 1. Create atas & budget txns
|
|
287
289
|
const budgetIxs = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(1_400_000);
|
|
288
290
|
|
|
@@ -361,29 +363,31 @@ async function buildRepayWithCollateralIxs<QuoteResponse>(
|
|
|
361
363
|
}
|
|
362
364
|
|
|
363
365
|
// 4. Swap collateral to debt to repay flash loan
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
366
|
+
return swapQuoteIxsArray.map((swapQuoteIxs) => {
|
|
367
|
+
const { preActionIxs, swapIxs } = swapQuoteIxs;
|
|
368
|
+
const swapInstructions = removeBudgetIxs(swapIxs);
|
|
369
|
+
|
|
370
|
+
const ixs = [
|
|
371
|
+
...scopeRefreshIxn,
|
|
372
|
+
...budgetIxs,
|
|
373
|
+
...atasAndIxs.map((x) => x.createAtaIx),
|
|
374
|
+
flashBorrowIx,
|
|
375
|
+
...preActionIxs,
|
|
376
|
+
...KaminoAction.actionToIxs(repayAndWithdrawAction),
|
|
377
|
+
...swapInstructions,
|
|
378
|
+
flashRepayIx,
|
|
379
|
+
];
|
|
380
|
+
|
|
381
|
+
const res: LeverageIxsOutput = {
|
|
382
|
+
flashLoanInfo: {
|
|
383
|
+
flashBorrowReserve: debtReserve.address,
|
|
384
|
+
flashLoanFee: debtReserve.getFlashLoanFee(),
|
|
385
|
+
},
|
|
386
|
+
instructions: ixs,
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
return res;
|
|
390
|
+
});
|
|
387
391
|
}
|
|
388
392
|
|
|
389
393
|
export const getMaxWithdrawLtvCheck = (
|
|
@@ -161,7 +161,16 @@ export async function getSwapCollIxs<QuoteResponse>(
|
|
|
161
161
|
// - To construct 1. (i.e. flash-borrow), we need to know the target collateral swap-out from 4.
|
|
162
162
|
|
|
163
163
|
// Construct the Klend's own ixs with a fake swap-out (only to learn the klend accounts used):
|
|
164
|
-
|
|
164
|
+
|
|
165
|
+
const scopeRefreshIxn = await getScopeRefreshIx(
|
|
166
|
+
context.market,
|
|
167
|
+
context.sourceCollReserve,
|
|
168
|
+
context.targetCollReserve,
|
|
169
|
+
context.obligation,
|
|
170
|
+
context.scopeRefreshConfig
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
const fakeKlendIxs = await getKlendIxs(args, FAKE_TARGET_COLL_SWAP_OUT_AMOUNT, context, scopeRefreshIxn);
|
|
165
174
|
const klendAccounts = uniqueAccountsWithProgramIds(listIxs(fakeKlendIxs));
|
|
166
175
|
|
|
167
176
|
// Construct the external swap ixs (and learn the actual swap-out amount):
|
|
@@ -176,7 +185,7 @@ export async function getSwapCollIxs<QuoteResponse>(
|
|
|
176
185
|
checkResultingObligationValid(args, externalSwapIxs.swapOutAmount, context);
|
|
177
186
|
|
|
178
187
|
// Construct the Klend's own ixs with an actual swap-out amount:
|
|
179
|
-
const klendIxs = await getKlendIxs(args, externalSwapIxs.swapOutAmount, context);
|
|
188
|
+
const klendIxs = await getKlendIxs(args, externalSwapIxs.swapOutAmount, context, scopeRefreshIxn);
|
|
180
189
|
|
|
181
190
|
return {
|
|
182
191
|
ixs: listIxs(klendIxs, externalSwapIxs.ixs),
|
|
@@ -270,19 +279,12 @@ type SwapCollKlendIxs = {
|
|
|
270
279
|
async function getKlendIxs(
|
|
271
280
|
args: SwapCollArgs,
|
|
272
281
|
targetCollSwapOutAmount: Decimal,
|
|
273
|
-
context: SwapCollContext<any
|
|
282
|
+
context: SwapCollContext<any>,
|
|
283
|
+
scopeRefreshIxn: TransactionInstruction[]
|
|
274
284
|
): Promise<SwapCollKlendIxs> {
|
|
275
285
|
const { ataCreationIxs, targetCollAta } = getAtaCreationIxs(context);
|
|
276
286
|
const setupIxs = [...context.budgetAndPriorityFeeIxs, ...ataCreationIxs];
|
|
277
287
|
|
|
278
|
-
const scopeRefreshIxn = await getScopeRefreshIx(
|
|
279
|
-
context.market,
|
|
280
|
-
context.sourceCollReserve,
|
|
281
|
-
context.targetCollReserve,
|
|
282
|
-
context.obligation,
|
|
283
|
-
context.scopeRefreshConfig
|
|
284
|
-
);
|
|
285
|
-
|
|
286
288
|
if (scopeRefreshIxn) {
|
|
287
289
|
setupIxs.unshift(...scopeRefreshIxn);
|
|
288
290
|
}
|