@kamino-finance/klend-sdk 6.0.5-beta.2 → 6.0.5-beta.21

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.
Files changed (67) hide show
  1. package/dist/classes/action.d.ts +1 -1
  2. package/dist/classes/action.d.ts.map +1 -1
  3. package/dist/classes/action.js +32 -16
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/manager.d.ts +29 -18
  6. package/dist/classes/manager.d.ts.map +1 -1
  7. package/dist/classes/manager.js +66 -49
  8. package/dist/classes/manager.js.map +1 -1
  9. package/dist/classes/market.d.ts +12 -11
  10. package/dist/classes/market.d.ts.map +1 -1
  11. package/dist/classes/market.js +77 -37
  12. package/dist/classes/market.js.map +1 -1
  13. package/dist/classes/obligation.d.ts +32 -2
  14. package/dist/classes/obligation.d.ts.map +1 -1
  15. package/dist/classes/obligation.js +150 -24
  16. package/dist/classes/obligation.js.map +1 -1
  17. package/dist/classes/vault.d.ts +5 -3
  18. package/dist/classes/vault.d.ts.map +1 -1
  19. package/dist/classes/vault.js +8 -6
  20. package/dist/classes/vault.js.map +1 -1
  21. package/dist/client_kamino_manager.d.ts.map +1 -1
  22. package/dist/client_kamino_manager.js +30 -22
  23. package/dist/client_kamino_manager.js.map +1 -1
  24. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  25. package/dist/lending_operations/repay_with_collateral_operations.js +36 -32
  26. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  27. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  28. package/dist/lending_operations/swap_collateral_operations.js +4 -4
  29. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  30. package/dist/leverage/operations.d.ts +4 -3
  31. package/dist/leverage/operations.d.ts.map +1 -1
  32. package/dist/leverage/operations.js +186 -154
  33. package/dist/leverage/operations.js.map +1 -1
  34. package/dist/leverage/types.d.ts +1 -0
  35. package/dist/leverage/types.d.ts.map +1 -1
  36. package/dist/utils/managerTypes.d.ts +1 -2
  37. package/dist/utils/managerTypes.d.ts.map +1 -1
  38. package/dist/utils/managerTypes.js +9 -9
  39. package/dist/utils/managerTypes.js.map +1 -1
  40. package/dist/utils/obligations.d.ts +5 -0
  41. package/dist/utils/obligations.d.ts.map +1 -0
  42. package/dist/utils/obligations.js +53 -0
  43. package/dist/utils/obligations.js.map +1 -0
  44. package/dist/utils/oracle.d.ts +3 -3
  45. package/dist/utils/oracle.d.ts.map +1 -1
  46. package/dist/utils/oracle.js +2 -2
  47. package/dist/utils/oracle.js.map +1 -1
  48. package/dist/utils/pubkey.d.ts +1 -0
  49. package/dist/utils/pubkey.d.ts.map +1 -1
  50. package/dist/utils/pubkey.js +10 -0
  51. package/dist/utils/pubkey.js.map +1 -1
  52. package/package.json +3 -3
  53. package/src/classes/action.ts +32 -20
  54. package/src/classes/manager.ts +87 -54
  55. package/src/classes/market.ts +132 -52
  56. package/src/classes/obligation.ts +201 -36
  57. package/src/classes/vault.ts +17 -6
  58. package/src/client.ts +4 -4
  59. package/src/client_kamino_manager.ts +40 -35
  60. package/src/lending_operations/repay_with_collateral_operations.ts +76 -72
  61. package/src/lending_operations/swap_collateral_operations.ts +13 -11
  62. package/src/leverage/operations.ts +362 -328
  63. package/src/leverage/types.ts +1 -0
  64. package/src/utils/managerTypes.ts +1 -2
  65. package/src/utils/obligations.ts +69 -0
  66. package/src/utils/oracle.ts +5 -4
  67. package/src/utils/pubkey.ts +9 -0
@@ -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 = await buildRepayWithCollateralIxs(
121
- kaminoMarket,
122
- debtReserve,
123
- collReserve,
124
- obligation,
125
- referrer,
126
- currentSlot,
127
- budgetAndPriorityFeeIxs,
128
- scopeRefreshConfig,
129
- {
130
- preActionIxs: [],
131
- swapIxs: [],
132
- lookupTables: [],
133
- quote: {} as SwapQuote<QuoteResponse>,
134
- },
135
- isClosingPosition,
136
- repayAmountLamports,
137
- inputAmountLamports,
138
- useV2Ixs
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
- return Promise.all(
242
- swapResponses.map(async (swapResponse) => {
243
- const ixs: LeverageIxsOutput = await buildRepayWithCollateralIxs(
244
- kaminoMarket,
245
- debtReserve,
246
- collReserve,
247
- obligation,
248
- referrer,
249
- currentSlot,
250
- budgetAndPriorityFeeIxs,
251
- scopeRefreshConfig,
252
- swapResponse,
253
- isClosingPosition,
254
- debtRepayAmountLamports,
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
- swapQuoteIxs: SwapIxs<QuoteResponse>,
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
- const { preActionIxs, swapIxs } = swapQuoteIxs;
365
- const swapInstructions = removeBudgetIxs(swapIxs);
366
-
367
- const ixs = [
368
- ...scopeRefreshIxn,
369
- ...budgetIxs,
370
- ...atasAndIxs.map((x) => x.createAtaIx),
371
- flashBorrowIx,
372
- ...preActionIxs,
373
- ...KaminoAction.actionToIxs(repayAndWithdrawAction),
374
- ...swapInstructions,
375
- flashRepayIx,
376
- ];
377
-
378
- const res: LeverageIxsOutput = {
379
- flashLoanInfo: {
380
- flashBorrowReserve: debtReserve.address,
381
- flashLoanFee: debtReserve.getFlashLoanFee(),
382
- },
383
- instructions: ixs,
384
- };
385
-
386
- return res;
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
- const fakeKlendIxs = await getKlendIxs(args, FAKE_TARGET_COLL_SWAP_OUT_AMOUNT, context);
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
  }