@kamino-finance/klend-sdk 5.13.0 → 5.13.2

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 (116) hide show
  1. package/dist/classes/action.d.ts +74 -49
  2. package/dist/classes/action.d.ts.map +1 -1
  3. package/dist/classes/action.js +167 -115
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/fraction.d.ts +2 -0
  6. package/dist/classes/fraction.d.ts.map +1 -1
  7. package/dist/classes/fraction.js +6 -0
  8. package/dist/classes/fraction.js.map +1 -1
  9. package/dist/classes/index.d.ts +3 -2
  10. package/dist/classes/index.d.ts.map +1 -1
  11. package/dist/classes/index.js +3 -2
  12. package/dist/classes/index.js.map +1 -1
  13. package/dist/classes/manager.d.ts +7 -7
  14. package/dist/classes/manager.d.ts.map +1 -1
  15. package/dist/classes/manager.js +46 -46
  16. package/dist/classes/manager.js.map +1 -1
  17. package/dist/classes/obligation.d.ts.map +1 -1
  18. package/dist/classes/obligation.js +1 -6
  19. package/dist/classes/obligation.js.map +1 -1
  20. package/dist/classes/obligationOrder.d.ts +1 -1
  21. package/dist/classes/obligationOrder.d.ts.map +1 -1
  22. package/dist/classes/reserve.d.ts +2 -2
  23. package/dist/classes/reserve.d.ts.map +1 -1
  24. package/dist/classes/reserve.js +71 -74
  25. package/dist/classes/reserve.js.map +1 -1
  26. package/dist/classes/shared.d.ts +13 -0
  27. package/dist/classes/shared.d.ts.map +1 -1
  28. package/dist/classes/utils.d.ts +1 -9
  29. package/dist/classes/utils.d.ts.map +1 -1
  30. package/dist/classes/utils.js +4 -51
  31. package/dist/classes/utils.js.map +1 -1
  32. package/dist/classes/vault.d.ts +5 -3
  33. package/dist/classes/vault.d.ts.map +1 -1
  34. package/dist/classes/vault.js +53 -41
  35. package/dist/classes/vault.js.map +1 -1
  36. package/dist/classes/{types.d.ts → vault_types.d.ts} +2 -15
  37. package/dist/classes/vault_types.d.ts.map +1 -0
  38. package/dist/classes/{types.js → vault_types.js} +1 -1
  39. package/dist/classes/vault_types.js.map +1 -0
  40. package/dist/client_kamino_manager.d.ts.map +1 -1
  41. package/dist/client_kamino_manager.js +25 -26
  42. package/dist/client_kamino_manager.js.map +1 -1
  43. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  44. package/dist/lending_operations/repay_with_collateral_operations.js +8 -8
  45. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  46. package/dist/lending_operations/swap_collateral_operations.d.ts +6 -6
  47. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  48. package/dist/lending_operations/swap_collateral_operations.js +63 -65
  49. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  50. package/dist/leverage/calcs.d.ts +0 -1
  51. package/dist/leverage/calcs.d.ts.map +1 -1
  52. package/dist/leverage/calcs.js +5 -8
  53. package/dist/leverage/calcs.js.map +1 -1
  54. package/dist/leverage/operations.d.ts +5 -5
  55. package/dist/leverage/operations.d.ts.map +1 -1
  56. package/dist/leverage/operations.js +80 -87
  57. package/dist/leverage/operations.js.map +1 -1
  58. package/dist/leverage/types.d.ts +1 -1
  59. package/dist/leverage/types.d.ts.map +1 -1
  60. package/dist/leverage/utils.d.ts +2 -2
  61. package/dist/leverage/utils.d.ts.map +1 -1
  62. package/dist/leverage/utils.js +5 -5
  63. package/dist/leverage/utils.js.map +1 -1
  64. package/dist/referrals/instructions.d.ts +3 -3
  65. package/dist/referrals/instructions.d.ts.map +1 -1
  66. package/dist/referrals/instructions.js +10 -10
  67. package/dist/referrals/instructions.js.map +1 -1
  68. package/dist/referrals/operations.js +6 -6
  69. package/dist/referrals/operations.js.map +1 -1
  70. package/dist/utils/ata.d.ts +3 -3
  71. package/dist/utils/ata.d.ts.map +1 -1
  72. package/dist/utils/ata.js +22 -22
  73. package/dist/utils/ata.js.map +1 -1
  74. package/dist/utils/instruction.d.ts +2 -2
  75. package/dist/utils/instruction.d.ts.map +1 -1
  76. package/dist/utils/instruction.js +12 -12
  77. package/dist/utils/instruction.js.map +1 -1
  78. package/dist/utils/lookupTable.js +6 -6
  79. package/dist/utils/lookupTable.js.map +1 -1
  80. package/dist/utils/userMetadata.d.ts +1 -1
  81. package/dist/utils/userMetadata.d.ts.map +1 -1
  82. package/dist/utils/userMetadata.js +3 -3
  83. package/dist/utils/userMetadata.js.map +1 -1
  84. package/package.json +1 -1
  85. package/src/classes/action.ts +198 -150
  86. package/src/classes/fraction.ts +7 -0
  87. package/src/classes/index.ts +5 -2
  88. package/src/classes/manager.ts +54 -51
  89. package/src/classes/obligation.ts +2 -7
  90. package/src/classes/obligationOrder.ts +1 -1
  91. package/src/classes/reserve.ts +128 -137
  92. package/src/classes/shared.ts +15 -0
  93. package/src/classes/utils.ts +3 -52
  94. package/src/classes/vault.ts +56 -42
  95. package/src/classes/{types.ts → vault_types.ts} +1 -16
  96. package/src/client.ts +16 -16
  97. package/src/client_kamino_manager.ts +25 -25
  98. package/src/lending_operations/repay_with_collateral_operations.ts +13 -15
  99. package/src/lending_operations/swap_collateral_operations.ts +87 -89
  100. package/src/leverage/calcs.ts +2 -6
  101. package/src/leverage/operations.ts +91 -97
  102. package/src/leverage/types.ts +1 -1
  103. package/src/leverage/utils.ts +5 -5
  104. package/src/referrals/instructions.ts +6 -6
  105. package/src/referrals/operations.ts +9 -9
  106. package/src/utils/ata.ts +18 -18
  107. package/src/utils/instruction.ts +10 -10
  108. package/src/utils/lookupTable.ts +6 -6
  109. package/src/utils/userMetadata.ts +1 -1
  110. package/dist/classes/types.d.ts.map +0 -1
  111. package/dist/classes/types.js.map +0 -1
  112. package/dist/classes/types_utils.d.ts +0 -14
  113. package/dist/classes/types_utils.d.ts.map +0 -1
  114. package/dist/classes/types_utils.js +0 -50
  115. package/dist/classes/types_utils.js.map +0 -1
  116. package/src/classes/types_utils.ts +0 -49
@@ -7,13 +7,13 @@ import {
7
7
  SwapIxsProvider,
8
8
  SwapQuoteProvider,
9
9
  getScopeRefreshIx,
10
- LeverageIxnsOutput,
10
+ LeverageIxsOutput,
11
11
  FlashLoanInfo,
12
12
  } from '../leverage';
13
13
  import {
14
14
  createAtasIdempotent,
15
- getComputeBudgetAndPriorityFeeIxns,
16
- removeBudgetAndAtaIxns,
15
+ getComputeBudgetAndPriorityFeeIxs,
16
+ removeBudgetAndAtaIxs,
17
17
  ScopePriceRefreshConfig,
18
18
  U64_MAX,
19
19
  uniqueAccountsWithProgramIds,
@@ -128,7 +128,7 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
128
128
  const inputAmountLamports = Decimal.min(withdrawableCollLamports, maxCollNeededFromOracle);
129
129
 
130
130
  // Build the repay & withdraw collateral tx to get the number of accounts
131
- const klendIxs: LeverageIxnsOutput = await buildRepayWithCollateralIxs(
131
+ const klendIxs: LeverageIxsOutput = await buildRepayWithCollateralIxs(
132
132
  kaminoMarket,
133
133
  debtReserve,
134
134
  collReserve,
@@ -256,7 +256,7 @@ export async function getRepayWithCollIxs<QuoteResponse>({
256
256
  );
257
257
 
258
258
  const swapResponse = await swapper(swapInputs, initialInputs.klendAccounts, swapQuote);
259
- const ixs: LeverageIxnsOutput = await buildRepayWithCollateralIxs(
259
+ const ixs: LeverageIxsOutput = await buildRepayWithCollateralIxs(
260
260
  kaminoMarket,
261
261
  debtReserve,
262
262
  collReserve,
@@ -295,9 +295,9 @@ async function buildRepayWithCollateralIxs(
295
295
  debtRepayAmountLamports: Decimal,
296
296
  collWithdrawLamports: Decimal,
297
297
  useV2Ixs: boolean
298
- ): Promise<LeverageIxnsOutput> {
298
+ ): Promise<LeverageIxsOutput> {
299
299
  // 1. Create atas & budget txns
300
- const budgetIxns = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxns(1_400_000);
300
+ const budgetIxs = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(1_400_000);
301
301
 
302
302
  const atas = [
303
303
  { mint: collReserve.getLiquidityMint(), tokenProgram: collReserve.getLiquidityTokenProgram() },
@@ -311,7 +311,7 @@ async function buildRepayWithCollateralIxs(
311
311
 
312
312
  // 2. Flash borrow & repay the debt to repay amount needed
313
313
  const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
314
- borrowIxnIndex: budgetIxns.length + atasAndIxs.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
314
+ borrowIxnIndex: budgetIxs.length + atasAndIxs.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
315
315
  walletPublicKey: obligation.state.owner,
316
316
  lendingMarketAuthority: market.getLendingMarketAuthority(),
317
317
  lendingMarketAddress: market.getAddress(),
@@ -346,7 +346,6 @@ async function buildRepayWithCollateralIxs(
346
346
  false,
347
347
  requestElevationGroup,
348
348
  undefined,
349
- undefined,
350
349
  referrer
351
350
  );
352
351
  } else {
@@ -364,18 +363,17 @@ async function buildRepayWithCollateralIxs(
364
363
  false,
365
364
  requestElevationGroup,
366
365
  undefined,
367
- undefined,
368
366
  referrer
369
367
  );
370
368
  }
371
369
 
372
370
  // 4. Swap collateral to debt to repay flash loan
373
371
  const { preActionIxs, swapIxs } = swapQuoteIxs;
374
- const swapInstructions = removeBudgetAndAtaIxns(swapIxs, []);
372
+ const swapInstructions = removeBudgetAndAtaIxs(swapIxs, []);
375
373
 
376
- const ixns = [
374
+ const ixs = [
377
375
  ...scopeRefreshIxn,
378
- ...budgetIxns,
376
+ ...budgetIxs,
379
377
  ...atasAndIxs.map((x) => x.createAtaIx),
380
378
  flashBorrowIxn,
381
379
  ...preActionIxs,
@@ -384,12 +382,12 @@ async function buildRepayWithCollateralIxs(
384
382
  flashRepayIxn,
385
383
  ];
386
384
 
387
- const res: LeverageIxnsOutput = {
385
+ const res: LeverageIxsOutput = {
388
386
  flashLoanInfo: {
389
387
  flashBorrowReserve: debtReserve.address,
390
388
  flashLoanFee: debtReserve.getFlashLoanFee(),
391
389
  },
392
- instructions: ixns,
390
+ instructions: ixs,
393
391
  };
394
392
 
395
393
  return res;
@@ -17,7 +17,7 @@ import {
17
17
  createAtasIdempotent,
18
18
  DEFAULT_MAX_COMPUTE_UNITS,
19
19
  getAssociatedTokenAddress,
20
- getComputeBudgetAndPriorityFeeIxns,
20
+ getComputeBudgetAndPriorityFeeIxs,
21
21
  PublicKeySet,
22
22
  ScopePriceRefreshConfig,
23
23
  U64_MAX,
@@ -28,9 +28,9 @@ import Decimal from 'decimal.js';
28
28
  import { createCloseAccountInstruction, NATIVE_MINT, TOKEN_PROGRAM_ID } from '@solana/spl-token';
29
29
 
30
30
  /**
31
- * Inputs to the `getSwapCollIxns()` operation.
31
+ * Inputs to the `getSwapCollIxs()` operation.
32
32
  */
33
- export interface SwapCollIxnsInputs<QuoteResponse> {
33
+ export interface SwapCollIxsInputs<QuoteResponse> {
34
34
  /**
35
35
  * The amount of source collateral to be swapped-in for the target collateral.
36
36
  * This value will be treated exactly (i.e. slippage is not applied here) and thus must not exceed the collateral's
@@ -66,7 +66,7 @@ export interface SwapCollIxnsInputs<QuoteResponse> {
66
66
  obligation: KaminoObligation;
67
67
  referrer: PublicKey;
68
68
  currentSlot: number;
69
- budgetAndPriorityFeeIxns?: TransactionInstruction[];
69
+ budgetAndPriorityFeeIxs?: TransactionInstruction[];
70
70
  scopeRefreshConfig?: ScopePriceRefreshConfig;
71
71
  useV2Ixs: boolean;
72
72
  quoter: SwapQuoteProvider<QuoteResponse>;
@@ -75,9 +75,9 @@ export interface SwapCollIxnsInputs<QuoteResponse> {
75
75
  }
76
76
 
77
77
  /**
78
- * Outputs from the `getSwapCollIxns()` operation.
78
+ * Outputs from the `getSwapCollIxs()` operation.
79
79
  */
80
- export interface SwapCollIxnsOutputs<QuoteResponse> {
80
+ export interface SwapCollIxsOutputs<QuoteResponse> {
81
81
  /**
82
82
  * Instructions for on-chain execution.
83
83
  */
@@ -143,12 +143,12 @@ export interface SwapCollIxnsOutputs<QuoteResponse> {
143
143
  /**
144
144
  * Constructs instructions needed to partially/fully swap the given source collateral for some other collateral type.
145
145
  */
146
- export async function getSwapCollIxns<QuoteResponse>(
147
- inputs: SwapCollIxnsInputs<QuoteResponse>
148
- ): Promise<SwapCollIxnsOutputs<QuoteResponse>> {
146
+ export async function getSwapCollIxs<QuoteResponse>(
147
+ inputs: SwapCollIxsInputs<QuoteResponse>
148
+ ): Promise<SwapCollIxsOutputs<QuoteResponse>> {
149
149
  const [args, context] = extractArgsAndContext(inputs);
150
150
 
151
- // Conceptually, we need to construct the following ixns:
151
+ // Conceptually, we need to construct the following ixs:
152
152
  // 0. any set-up, like budgeting and ATAs
153
153
  // 1. `flash-borrowed target coll = targetCollReserve.flashBorrow()`
154
154
  // 2. `targetCollReserve.deposit(flash-borrowed target coll)`
@@ -157,38 +157,38 @@ export async function getSwapCollIxns<QuoteResponse>(
157
157
  // 5. `flashRepay(externally-swapped target coll)`
158
158
  // However, there is a cyclic dependency:
159
159
  // - To construct 4. (specifically, to query the external swap quote), we need to know all accounts used by Kamino's
160
- // own ixns.
160
+ // own ixs.
161
161
  // - To construct 1. (i.e. flash-borrow), we need to know the target collateral swap-out from 4.
162
162
 
163
- // Construct the Klend's own ixns with a fake swap-out (only to learn the klend accounts used):
164
- const fakeKlendIxns = await getKlendIxns(args, FAKE_TARGET_COLL_SWAP_OUT_AMOUNT, context);
165
- const klendAccounts = uniqueAccountsWithProgramIds(listIxns(fakeKlendIxns));
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);
165
+ const klendAccounts = uniqueAccountsWithProgramIds(listIxs(fakeKlendIxs));
166
166
 
167
- // Construct the external swap ixns (and learn the actual swap-out amount):
168
- const externalSwapIxns = await getExternalSwapIxns(args, klendAccounts, context);
167
+ // Construct the external swap ixs (and learn the actual swap-out amount):
168
+ const externalSwapIxs = await getExternalSwapIxs(args, klendAccounts, context);
169
169
 
170
170
  // We now have the full information needed to simulate the end-state, so let's check that the operation is legal:
171
171
  context.logger(
172
- `Expected to swap ${args.sourceCollSwapAmount} ${context.sourceCollReserve.symbol} collateral into ${externalSwapIxns.swapOutAmount} ${context.targetCollReserve.symbol} collateral`
172
+ `Expected to swap ${args.sourceCollSwapAmount} ${context.sourceCollReserve.symbol} collateral into ${externalSwapIxs.swapOutAmount} ${context.targetCollReserve.symbol} collateral`
173
173
  );
174
- checkResultingObligationValid(args, externalSwapIxns.swapOutAmount, context);
174
+ checkResultingObligationValid(args, externalSwapIxs.swapOutAmount, context);
175
175
 
176
- // Construct the Klend's own ixns with an actual swap-out amount:
177
- const klendIxns = await getKlendIxns(args, externalSwapIxns.swapOutAmount, context);
176
+ // Construct the Klend's own ixs with an actual swap-out amount:
177
+ const klendIxs = await getKlendIxs(args, externalSwapIxs.swapOutAmount, context);
178
178
 
179
179
  return {
180
- ixs: listIxns(klendIxns, externalSwapIxns.ixns),
181
- lookupTables: externalSwapIxns.luts,
180
+ ixs: listIxs(klendIxs, externalSwapIxs.ixs),
181
+ lookupTables: externalSwapIxs.luts,
182
182
  useV2Ixs: context.useV2Ixs,
183
183
  simulationDetails: {
184
184
  flashLoan: {
185
- targetCollFlashBorrowedAmount: klendIxns.simulationDetails.targetCollFlashBorrowedAmount,
186
- targetCollFlashRepaidAmount: externalSwapIxns.swapOutAmount,
185
+ targetCollFlashBorrowedAmount: klendIxs.simulationDetails.targetCollFlashBorrowedAmount,
186
+ targetCollFlashRepaidAmount: externalSwapIxs.swapOutAmount,
187
187
  },
188
188
  externalSwap: {
189
189
  sourceCollSwapInAmount: args.sourceCollSwapAmount, // repeated `/inputs.sourceCollSwapAmount`, only for clarity
190
- targetCollSwapOutAmount: externalSwapIxns.swapOutAmount, // repeated `../flashLoan.targetCollFlashRepaidAmount`, only for clarity
191
- quoteResponse: externalSwapIxns.simulationDetails.quoteResponse,
190
+ targetCollSwapOutAmount: externalSwapIxs.swapOutAmount, // repeated `../flashLoan.targetCollFlashRepaidAmount`, only for clarity
191
+ quoteResponse: externalSwapIxs.simulationDetails.quoteResponse,
192
192
  },
193
193
  },
194
194
  };
@@ -201,7 +201,7 @@ type SwapCollArgs = {
201
201
  };
202
202
 
203
203
  type SwapCollContext<QuoteResponse> = {
204
- budgetAndPriorityFeeIxns: TransactionInstruction[];
204
+ budgetAndPriorityFeeIxs: TransactionInstruction[];
205
205
  market: KaminoMarket;
206
206
  sourceCollReserve: KaminoReserve;
207
207
  targetCollReserve: KaminoReserve;
@@ -216,7 +216,7 @@ type SwapCollContext<QuoteResponse> = {
216
216
  };
217
217
 
218
218
  function extractArgsAndContext<QuoteResponse>(
219
- inputs: SwapCollIxnsInputs<QuoteResponse>
219
+ inputs: SwapCollIxsInputs<QuoteResponse>
220
220
  ): [SwapCollArgs, SwapCollContext<QuoteResponse>] {
221
221
  if (inputs.sourceCollTokenMint.equals(inputs.targetCollTokenMint)) {
222
222
  throw new Error(`Cannot swap from/to the same collateral`);
@@ -231,8 +231,8 @@ function extractArgsAndContext<QuoteResponse>(
231
231
  newElevationGroup: inputs.market.getExistingElevationGroup(inputs.newElevationGroup, 'Newly-requested'),
232
232
  },
233
233
  {
234
- budgetAndPriorityFeeIxns:
235
- inputs.budgetAndPriorityFeeIxns || getComputeBudgetAndPriorityFeeIxns(DEFAULT_MAX_COMPUTE_UNITS),
234
+ budgetAndPriorityFeeIxs:
235
+ inputs.budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(DEFAULT_MAX_COMPUTE_UNITS),
236
236
  sourceCollReserve: inputs.market.getExistingReserveByMint(inputs.sourceCollTokenMint, 'Current collateral'),
237
237
  targetCollReserve: inputs.market.getExistingReserveByMint(inputs.targetCollTokenMint, 'Target collateral'),
238
238
  logger: console.log,
@@ -248,28 +248,28 @@ function extractArgsAndContext<QuoteResponse>(
248
248
  ];
249
249
  }
250
250
 
251
- const FAKE_TARGET_COLL_SWAP_OUT_AMOUNT = new Decimal(1); // see the lengthy `getSwapCollIxns()` impl comment
251
+ const FAKE_TARGET_COLL_SWAP_OUT_AMOUNT = new Decimal(1); // see the lengthy `getSwapCollIxs()` impl comment
252
252
 
253
- type SwapCollKlendIxns = {
254
- setupIxns: TransactionInstruction[];
253
+ type SwapCollKlendIxs = {
254
+ setupIxs: TransactionInstruction[];
255
255
  targetCollFlashBorrowIxn: TransactionInstruction;
256
- depositTargetCollIxns: TransactionInstruction[];
257
- withdrawSourceCollIxns: TransactionInstruction[];
256
+ depositTargetCollIxs: TransactionInstruction[];
257
+ withdrawSourceCollIxs: TransactionInstruction[];
258
258
  targetCollFlashRepayIxn: TransactionInstruction;
259
- cleanupIxns: TransactionInstruction[];
259
+ cleanupIxs: TransactionInstruction[];
260
260
  flashLoanInfo: FlashLoanInfo;
261
261
  simulationDetails: {
262
262
  targetCollFlashBorrowedAmount: Decimal;
263
263
  };
264
264
  };
265
265
 
266
- async function getKlendIxns(
266
+ async function getKlendIxs(
267
267
  args: SwapCollArgs,
268
268
  targetCollSwapOutAmount: Decimal,
269
269
  context: SwapCollContext<any>
270
- ): Promise<SwapCollKlendIxns> {
271
- const { ataCreationIxns, targetCollAta } = getAtaCreationIxns(context);
272
- const setupIxns = [...context.budgetAndPriorityFeeIxns, ...ataCreationIxns];
270
+ ): Promise<SwapCollKlendIxs> {
271
+ const { ataCreationIxs, targetCollAta } = getAtaCreationIxs(context);
272
+ const setupIxs = [...context.budgetAndPriorityFeeIxs, ...ataCreationIxs];
273
273
 
274
274
  const scopeRefreshIxn = await getScopeRefreshIx(
275
275
  context.market,
@@ -280,37 +280,37 @@ async function getKlendIxns(
280
280
  );
281
281
 
282
282
  if (scopeRefreshIxn) {
283
- setupIxns.unshift(...scopeRefreshIxn);
283
+ setupIxs.unshift(...scopeRefreshIxn);
284
284
  }
285
285
 
286
286
  const targetCollFlashBorrowedAmount = calculateTargetCollFlashBorrowedAmount(targetCollSwapOutAmount, context);
287
- const { targetCollFlashBorrowIxn, targetCollFlashRepayIxn } = getTargetCollFlashLoanIxns(
287
+ const { targetCollFlashBorrowIxn, targetCollFlashRepayIxn } = getTargetCollFlashLoanIxs(
288
288
  targetCollFlashBorrowedAmount,
289
- setupIxns.length,
289
+ setupIxs.length,
290
290
  targetCollAta,
291
291
  context
292
292
  );
293
293
 
294
- const depositTargetCollIxns = await getDepositTargetCollIxns(targetCollFlashBorrowedAmount, context);
295
- const withdrawSourceCollIxns = await getWithdrawSourceCollIxns(
294
+ const depositTargetCollIxs = await getDepositTargetCollIxs(targetCollFlashBorrowedAmount, context);
295
+ const withdrawSourceCollIxs = await getWithdrawSourceCollIxs(
296
296
  args,
297
- depositTargetCollIxns.removesElevationGroup,
297
+ depositTargetCollIxs.removesElevationGroup,
298
298
  context
299
299
  );
300
300
 
301
- const cleanupIxns = getAtaCloseIxns(context);
301
+ const cleanupIxs = getAtaCloseIxs(context);
302
302
 
303
303
  return {
304
- setupIxns,
304
+ setupIxs,
305
305
  flashLoanInfo: {
306
306
  flashBorrowReserve: context.targetCollReserve.address,
307
307
  flashLoanFee: context.targetCollReserve.getFlashLoanFee(),
308
308
  },
309
309
  targetCollFlashBorrowIxn,
310
- depositTargetCollIxns: depositTargetCollIxns.ixns,
311
- withdrawSourceCollIxns,
310
+ depositTargetCollIxs: depositTargetCollIxs.ixs,
311
+ withdrawSourceCollIxs,
312
312
  targetCollFlashRepayIxn,
313
- cleanupIxns,
313
+ cleanupIxs,
314
314
  simulationDetails: {
315
315
  targetCollFlashBorrowedAmount,
316
316
  },
@@ -332,8 +332,8 @@ function calculateTargetCollFlashBorrowedAmount(
332
332
  return targetCollFlashRepaidAmount.sub(targetCollFlashLoanFee);
333
333
  }
334
334
 
335
- function getAtaCreationIxns(context: SwapCollContext<any>) {
336
- const atasAndAtaCreationIxns = createAtasIdempotent(context.obligation.state.owner, [
335
+ function getAtaCreationIxs(context: SwapCollContext<any>) {
336
+ const atasAndAtaCreationIxs = createAtasIdempotent(context.obligation.state.owner, [
337
337
  {
338
338
  mint: context.sourceCollReserve.getLiquidityMint(),
339
339
  tokenProgram: context.sourceCollReserve.getLiquidityTokenProgram(),
@@ -344,25 +344,25 @@ function getAtaCreationIxns(context: SwapCollContext<any>) {
344
344
  },
345
345
  ]);
346
346
  return {
347
- ataCreationIxns: atasAndAtaCreationIxns.map((tuple) => tuple.createAtaIx),
348
- targetCollAta: atasAndAtaCreationIxns[1].ata,
347
+ ataCreationIxs: atasAndAtaCreationIxs.map((tuple) => tuple.createAtaIx),
348
+ targetCollAta: atasAndAtaCreationIxs[1].ata,
349
349
  };
350
350
  }
351
351
 
352
- function getAtaCloseIxns(context: SwapCollContext<any>) {
353
- const ataCloseIxns: TransactionInstruction[] = [];
352
+ function getAtaCloseIxs(context: SwapCollContext<any>) {
353
+ const ataCloseIxs: TransactionInstruction[] = [];
354
354
  if (
355
355
  context.sourceCollReserve.getLiquidityMint().equals(NATIVE_MINT) ||
356
356
  context.targetCollReserve.getLiquidityMint().equals(NATIVE_MINT)
357
357
  ) {
358
358
  const owner = context.obligation.state.owner;
359
359
  const wsolAta = getAssociatedTokenAddress(NATIVE_MINT, owner, false);
360
- ataCloseIxns.push(createCloseAccountInstruction(wsolAta, owner, owner, [], TOKEN_PROGRAM_ID));
360
+ ataCloseIxs.push(createCloseAccountInstruction(wsolAta, owner, owner, [], TOKEN_PROGRAM_ID));
361
361
  }
362
- return ataCloseIxns;
362
+ return ataCloseIxs;
363
363
  }
364
364
 
365
- function getTargetCollFlashLoanIxns(
365
+ function getTargetCollFlashLoanIxs(
366
366
  targetCollAmount: Decimal,
367
367
  flashBorrowIxnIndex: number,
368
368
  destinationAta: PublicKey,
@@ -386,15 +386,15 @@ function getTargetCollFlashLoanIxns(
386
386
  return { targetCollFlashBorrowIxn, targetCollFlashRepayIxn };
387
387
  }
388
388
 
389
- type DepositTargetCollIxns = {
389
+ type DepositTargetCollIxs = {
390
390
  removesElevationGroup: boolean;
391
- ixns: TransactionInstruction[];
391
+ ixs: TransactionInstruction[];
392
392
  };
393
393
 
394
- async function getDepositTargetCollIxns(
394
+ async function getDepositTargetCollIxs(
395
395
  targetCollAmount: Decimal,
396
396
  context: SwapCollContext<any>
397
- ): Promise<DepositTargetCollIxns> {
397
+ ): Promise<DepositTargetCollIxs> {
398
398
  const removesElevationGroup = mustRemoveElevationGroupBeforeDeposit(context);
399
399
  const depositCollAction = await KaminoAction.buildDepositTxns(
400
400
  context.market,
@@ -403,18 +403,17 @@ async function getDepositTargetCollIxns(
403
403
  context.obligation.state.owner,
404
404
  context.obligation,
405
405
  context.useV2Ixs,
406
- undefined, // we create the scope refresh ixn outside of KaminoAction
406
+ undefined, // we create the scope refresh ix outside of KaminoAction
407
407
  0, // no extra compute budget
408
- false, // we do not need ATA ixns here (we construct and close them ourselves)
408
+ false, // we do not need ATA ixs here (we construct and close them ourselves)
409
409
  removesElevationGroup, // we may need to (temporarily) remove the elevation group; the same or a different one will be set on withdraw, if requested
410
- false, // we are dealing with an existing obligation, no need to create user metadata
411
- false, // we do not need to create a lookup table, dealing with an existing obligation
410
+ { skipInitialization: true, skipLutCreation: true }, // we are dealing with an existing obligation, no need to create user metadata
412
411
  context.referrer,
413
412
  context.currentSlot,
414
413
  removesElevationGroup ? 0 : undefined // only applicable when removing the group
415
414
  );
416
415
  return {
417
- ixns: KaminoAction.actionToIxs(depositCollAction),
416
+ ixs: KaminoAction.actionToIxs(depositCollAction),
418
417
  removesElevationGroup,
419
418
  };
420
419
  }
@@ -437,7 +436,7 @@ function mustRemoveElevationGroupBeforeDeposit(context: SwapCollContext<any>): b
437
436
  return false; // the obligation has some elevation group and the new collateral can be added to it
438
437
  }
439
438
 
440
- async function getWithdrawSourceCollIxns(
439
+ async function getWithdrawSourceCollIxs(
441
440
  args: SwapCollArgs,
442
441
  depositRemovedElevationGroup: boolean,
443
442
  context: SwapCollContext<any>
@@ -453,12 +452,11 @@ async function getWithdrawSourceCollIxns(
453
452
  context.obligation.state.owner,
454
453
  context.obligation,
455
454
  context.useV2Ixs,
456
- undefined, // we create the scope refresh ixn outside of KaminoAction
455
+ undefined, // we create the scope refresh ix outside of KaminoAction
457
456
  0, // no extra compute budget
458
- false, // we do not need ATA ixns here (we construct and close them ourselves)
457
+ false, // we do not need ATA ixs here (we construct and close them ourselves)
459
458
  requestedElevationGroup !== undefined, // the `elevationGroupIdToRequestAfterWithdraw()` has already decided on this
460
- false, // we are dealing with an existing obligation, no need to create user metadata
461
- false, // we do not need to create a lookup table, dealing with an existing obligation
459
+ { skipInitialization: true, skipLutCreation: true }, // we are dealing with an existing obligation, no need to create user metadata
462
460
  context.referrer,
463
461
  context.currentSlot,
464
462
  requestedElevationGroup,
@@ -499,20 +497,20 @@ function elevationGroupIdToRequestAfterWithdraw(
499
497
  }
500
498
  }
501
499
 
502
- type ExternalSwapIxns<QuoteResponse> = {
500
+ type ExternalSwapIxs<QuoteResponse> = {
503
501
  swapOutAmount: Decimal;
504
- ixns: TransactionInstruction[];
502
+ ixs: TransactionInstruction[];
505
503
  luts: AddressLookupTableAccount[];
506
504
  simulationDetails: {
507
505
  quoteResponse?: QuoteResponse;
508
506
  };
509
507
  };
510
508
 
511
- async function getExternalSwapIxns<QuoteResponse>(
509
+ async function getExternalSwapIxs<QuoteResponse>(
512
510
  args: SwapCollArgs,
513
511
  klendAccounts: PublicKey[],
514
512
  context: SwapCollContext<QuoteResponse>
515
- ): Promise<ExternalSwapIxns<QuoteResponse>> {
513
+ ): Promise<ExternalSwapIxs<QuoteResponse>> {
516
514
  const externalSwapInputs = {
517
515
  inputAmountLamports: args.sourceCollSwapAmount.mul(context.sourceCollReserve.getMintFactor()),
518
516
  inputMint: context.sourceCollReserve.getLiquidityMint(),
@@ -521,12 +519,12 @@ async function getExternalSwapIxns<QuoteResponse>(
521
519
  };
522
520
  const externalSwapQuote = await context.quoter(externalSwapInputs, klendAccounts);
523
521
  const swapOutAmount = externalSwapQuote.priceAInB.mul(args.sourceCollSwapAmount);
524
- const externalSwapIxnsAndLuts = await context.swapper(externalSwapInputs, klendAccounts, externalSwapQuote);
522
+ const externalSwapIxsAndLuts = await context.swapper(externalSwapInputs, klendAccounts, externalSwapQuote);
525
523
  // Note: we can ignore the returned `preActionIxs` field - we do not request any of them from the swapper.
526
524
  return {
527
525
  swapOutAmount,
528
- ixns: externalSwapIxnsAndLuts.swapIxs,
529
- luts: externalSwapIxnsAndLuts.lookupTables,
526
+ ixs: externalSwapIxsAndLuts.swapIxs,
527
+ luts: externalSwapIxsAndLuts.lookupTables,
530
528
  simulationDetails: {
531
529
  quoteResponse: externalSwapQuote.quoteResponse,
532
530
  },
@@ -608,14 +606,14 @@ function checkResultingObligationValid(
608
606
  }
609
607
  }
610
608
 
611
- function listIxns(klendIxns: SwapCollKlendIxns, externalSwapIxns?: TransactionInstruction[]): TransactionInstruction[] {
609
+ function listIxs(klendIxs: SwapCollKlendIxs, externalSwapIxs?: TransactionInstruction[]): TransactionInstruction[] {
612
610
  return [
613
- ...klendIxns.setupIxns,
614
- klendIxns.targetCollFlashBorrowIxn,
615
- ...klendIxns.depositTargetCollIxns,
616
- ...klendIxns.withdrawSourceCollIxns,
617
- ...(externalSwapIxns || []),
618
- klendIxns.targetCollFlashRepayIxn,
619
- ...klendIxns.cleanupIxns,
611
+ ...klendIxs.setupIxs,
612
+ klendIxs.targetCollFlashBorrowIxn,
613
+ ...klendIxs.depositTargetCollIxs,
614
+ ...klendIxs.withdrawSourceCollIxs,
615
+ ...(externalSwapIxs || []),
616
+ klendIxs.targetCollFlashRepayIxn,
617
+ ...klendIxs.cleanupIxs,
620
618
  ];
621
619
  }
@@ -1,7 +1,7 @@
1
1
  import { PublicKey } from '@solana/web3.js';
2
2
  import Decimal from 'decimal.js';
3
3
  import { collToLamportsDecimal, Kamino, StrategyWithAddress, TokenAmounts } from '@kamino-finance/kliquidity-sdk';
4
- import { KaminoMarket, KaminoObligation, KaminoReserve } from '../classes';
4
+ import { KaminoMarket, KaminoObligation, KaminoReserve, toJson } from '../classes';
5
5
  import { getExpectedTokenBalanceAfterBorrow } from './utils';
6
6
  import {
7
7
  AdjustLeverageCalcsResult,
@@ -11,10 +11,6 @@ import {
11
11
  } from './types';
12
12
  import { fuzzyEqual } from '../utils';
13
13
 
14
- export const toJson = (object: any): string => {
15
- return JSON.stringify(object, null, 2);
16
- };
17
-
18
14
  const closingPositionDiffTolerance = 0.0001;
19
15
 
20
16
  export enum FormTabs {
@@ -683,7 +679,7 @@ export function withdrawLeverageCalcs(
683
679
  .mul(irSlippageBpsForDebt.add('0.1').div('10_000').add('1'))
684
680
  .toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber()!, Decimal.ROUND_CEIL);
685
681
 
686
- // 6. Get swap ixns
682
+ // 6. Get swap ixs
687
683
  // 5. Get swap estimations to understand how much we need to borrow from borrow reserve
688
684
  // prevent withdrawing more then deposited if we close position
689
685
  const depositTokenWithdrawAmount = !isClosingPosition