@kamino-finance/klend-sdk 5.11.2-beta.0 → 5.11.3-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/classes/action.d.ts +25 -35
  2. package/dist/classes/action.d.ts.map +1 -1
  3. package/dist/classes/action.js +92 -388
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/lut_utils.d.ts +29 -0
  6. package/dist/classes/lut_utils.d.ts.map +1 -0
  7. package/dist/classes/lut_utils.js +62 -0
  8. package/dist/classes/lut_utils.js.map +1 -0
  9. package/dist/classes/manager.d.ts +1 -1
  10. package/dist/classes/manager.js +1 -1
  11. package/dist/classes/market.d.ts +3 -3
  12. package/dist/classes/market.d.ts.map +1 -1
  13. package/dist/classes/market.js +30 -16
  14. package/dist/classes/market.js.map +1 -1
  15. package/dist/classes/obligation.d.ts +3 -1
  16. package/dist/classes/obligation.d.ts.map +1 -1
  17. package/dist/classes/obligation.js +6 -1
  18. package/dist/classes/obligation.js.map +1 -1
  19. package/dist/classes/vault.js +6 -6
  20. package/dist/classes/vault.js.map +1 -1
  21. package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -1
  22. package/dist/lending_operations/repay_with_collateral_calcs.js +5 -9
  23. package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
  24. package/dist/lending_operations/repay_with_collateral_operations.d.ts +4 -9
  25. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  26. package/dist/lending_operations/repay_with_collateral_operations.js +10 -33
  27. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  28. package/dist/lending_operations/swap_collateral_operations.d.ts +2 -2
  29. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  30. package/dist/lending_operations/swap_collateral_operations.js +11 -6
  31. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  32. package/dist/leverage/operations.d.ts +9 -7
  33. package/dist/leverage/operations.d.ts.map +1 -1
  34. package/dist/leverage/operations.js +78 -66
  35. package/dist/leverage/operations.js.map +1 -1
  36. package/dist/leverage/types.d.ts +4 -4
  37. package/dist/leverage/types.d.ts.map +1 -1
  38. package/dist/utils/ObligationType.d.ts +1 -1
  39. package/dist/utils/ObligationType.d.ts.map +1 -1
  40. package/dist/utils/lookupTable.d.ts +0 -27
  41. package/dist/utils/lookupTable.d.ts.map +1 -1
  42. package/dist/utils/lookupTable.js +0 -58
  43. package/dist/utils/lookupTable.js.map +1 -1
  44. package/dist/utils/managerTypes.d.ts.map +1 -1
  45. package/dist/utils/managerTypes.js +7 -52
  46. package/dist/utils/managerTypes.js.map +1 -1
  47. package/dist/utils/oracle.d.ts +3 -3
  48. package/dist/utils/oracle.d.ts.map +1 -1
  49. package/dist/utils/seeds.d.ts +1 -11
  50. package/dist/utils/seeds.d.ts.map +1 -1
  51. package/dist/utils/seeds.js +3 -13
  52. package/dist/utils/seeds.js.map +1 -1
  53. package/dist/utils/userMetadata.js +6 -6
  54. package/dist/utils/userMetadata.js.map +1 -1
  55. package/package.json +2 -2
  56. package/src/classes/action.ts +116 -532
  57. package/src/classes/lut_utils.ts +63 -0
  58. package/src/classes/manager.ts +1 -1
  59. package/src/classes/market.ts +34 -25
  60. package/src/classes/obligation.ts +7 -1
  61. package/src/classes/vault.ts +1 -1
  62. package/src/client.ts +8 -3
  63. package/src/lending_operations/repay_with_collateral_calcs.ts +5 -14
  64. package/src/lending_operations/repay_with_collateral_operations.ts +33 -72
  65. package/src/lending_operations/swap_collateral_operations.ts +19 -7
  66. package/src/leverage/operations.ts +114 -66
  67. package/src/leverage/types.ts +4 -4
  68. package/src/utils/ObligationType.ts +1 -1
  69. package/src/utils/lookupTable.ts +0 -62
  70. package/src/utils/managerTypes.ts +10 -52
  71. package/src/utils/oracle.ts +2 -2
  72. package/src/utils/seeds.ts +4 -14
  73. package/src/utils/userMetadata.ts +14 -14
@@ -10,12 +10,12 @@ const bn_js_1 = __importDefault(require("bn.js"));
10
10
  const decimal_js_1 = __importDefault(require("decimal.js"));
11
11
  const instructions_1 = require("../idl_codegen/instructions");
12
12
  const utils_1 = require("../utils");
13
+ const market_1 = require("./market");
13
14
  const obligation_1 = require("./obligation");
14
15
  const types_1 = require("../idl_codegen/types");
15
16
  const farms_sdk_1 = require("@kamino-finance/farms-sdk");
16
17
  const ObligationType_1 = require("../utils/ObligationType");
17
18
  const lib_1 = require("../lib");
18
- const scope_sdk_1 = require("@kamino-finance/scope-sdk");
19
19
  const SOL_PADDING_FOR_INTEREST = new bn_js_1.default('1000000');
20
20
  class KaminoAction {
21
21
  kaminoMarket;
@@ -180,27 +180,18 @@ class KaminoAction {
180
180
  axn.addRequestElevationIx(elevationGroup, 'setup');
181
181
  return axn;
182
182
  }
183
- static async buildDepositTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
183
+ static async buildDepositTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
184
184
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
185
185
  requestElevationGroup = false, // to be requested *before* the deposit
186
186
  includeUserMetadata = true, // if true it includes user metadata
187
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }, overrideElevationGroupRequest = undefined // if set, when an elevationgroup request is made, it will use this value
187
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, overrideElevationGroupRequest = undefined // if set, when an elevationgroup request is made, it will use this value
188
188
  ) {
189
189
  const axn = await KaminoAction.initialize('deposit', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
190
190
  const addInitObligationForFarm = true;
191
191
  if (extraComputeBudget > 0) {
192
192
  axn.addComputeBudgetIxn(extraComputeBudget);
193
193
  }
194
- const allReserves = new utils_1.PublicKeySet([
195
- ...axn.depositReserves,
196
- ...axn.borrowReserves,
197
- axn.reserve.address,
198
- ]).toArray();
199
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
200
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
201
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
202
- }
203
- await axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable, undefined, overrideElevationGroupRequest);
194
+ await axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, undefined, overrideElevationGroupRequest);
204
195
  if (useV2Ixs) {
205
196
  axn.addDepositIxV2();
206
197
  }
@@ -210,54 +201,23 @@ class KaminoAction {
210
201
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
211
202
  return axn;
212
203
  }
213
- getTokenIdsForScopeRefresh(kaminoMarket, reserves) {
214
- const tokenIds = [];
215
- for (const reserveAddress of reserves) {
216
- const reserve = kaminoMarket.getReserveByAddress(reserveAddress);
217
- if (!reserve) {
218
- throw new Error(`Reserve not found for reserve ${reserveAddress.toBase58()}`);
219
- }
220
- if (!reserve.state.config.tokenInfo.scopeConfiguration.priceFeed.equals(web3_js_1.PublicKey.default)) {
221
- reserve.state.config.tokenInfo.scopeConfiguration.priceChain.map((x) => {
222
- if (x !== scope_sdk_1.U16_MAX) {
223
- tokenIds.push(x);
224
- }
225
- });
226
- reserve.state.config.tokenInfo.scopeConfiguration.twapChain.map((x) => {
227
- if (x !== scope_sdk_1.U16_MAX) {
228
- tokenIds.push(x);
229
- }
230
- });
231
- }
232
- }
233
- return tokenIds;
234
- }
235
- async addScopeRefreshIxs(tokens, feed = 'hubble') {
236
- this.preTxnIxsLabels.unshift(`refreshScopePrices`);
237
- this.preTxnIxs.unshift(await this.kaminoMarket.scope.refreshPriceListIx({
204
+ async addScopeRefreshIxs(scope, tokens, feed = 'hubble') {
205
+ this.setupIxsLabels.unshift(`refreshScopePrices`);
206
+ this.setupIxs.unshift(await scope.refreshPriceListIx({
238
207
  feed: feed,
239
208
  }, tokens));
240
209
  }
241
- static async buildBorrowTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
210
+ static async buildBorrowTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
242
211
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
243
212
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
244
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }, overrideElevationGroupRequest = undefined // if set, when an elevationgroup request is made, it will use this value
213
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, overrideElevationGroupRequest = undefined // if set, when an elevationgroup request is made, it will use this value
245
214
  ) {
246
215
  const axn = await KaminoAction.initialize('borrow', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
247
216
  const addInitObligationForFarm = true;
248
217
  if (extraComputeBudget > 0) {
249
218
  axn.addComputeBudgetIxn(extraComputeBudget);
250
219
  }
251
- const allReserves = new utils_1.PublicKeySet([
252
- ...axn.depositReserves,
253
- ...axn.borrowReserves,
254
- axn.reserve.address,
255
- ]).toArray();
256
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
257
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
258
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
259
- }
260
- await axn.addSupportIxs('borrow', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable, undefined, overrideElevationGroupRequest);
220
+ await axn.addSupportIxs('borrow', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, undefined, overrideElevationGroupRequest);
261
221
  if (useV2Ixs) {
262
222
  axn.addBorrowIxV2();
263
223
  }
@@ -267,69 +227,42 @@ class KaminoAction {
267
227
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
268
228
  return axn;
269
229
  }
270
- static async buildDepositReserveLiquidityTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
230
+ static async buildDepositReserveLiquidityTxns(kaminoMarket, amount, mint, owner, obligation, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
271
231
  includeAtaIxns = true, // if true it includes create and close wsol and token atas
272
- requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
232
+ requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
273
233
  const axn = await KaminoAction.initialize('mint', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
274
234
  const addInitObligationForFarm = true;
275
235
  if (extraComputeBudget > 0) {
276
236
  axn.addComputeBudgetIxn(extraComputeBudget);
277
237
  }
278
- const allReserves = new utils_1.PublicKeySet([
279
- ...axn.depositReserves,
280
- ...axn.borrowReserves,
281
- axn.reserve.address,
282
- ]).toArray();
283
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
284
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
285
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
286
- }
287
- await axn.addSupportIxs('mint', includeAtaIxns, requestElevationGroup, false, addInitObligationForFarm, false, false);
238
+ await axn.addSupportIxs('mint', includeAtaIxns, requestElevationGroup, false, addInitObligationForFarm, false, scopeRefreshConfig, false);
288
239
  axn.addDepositReserveLiquidityIx();
289
240
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
290
241
  return axn;
291
242
  }
292
- static async buildRedeemReserveCollateralTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
243
+ static async buildRedeemReserveCollateralTxns(kaminoMarket, amount, mint, owner, obligation, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
293
244
  includeAtaIxns = true, // if true it includes create and close wsol and token atas
294
- requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
245
+ requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
295
246
  const axn = await KaminoAction.initialize('redeem', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
296
247
  const addInitObligationForFarm = true;
297
248
  if (extraComputeBudget > 0) {
298
249
  axn.addComputeBudgetIxn(extraComputeBudget);
299
250
  }
300
- const allReserves = new utils_1.PublicKeySet([
301
- ...axn.depositReserves,
302
- ...axn.borrowReserves,
303
- axn.reserve.address,
304
- ]).toArray();
305
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
306
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
307
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
308
- }
309
- await axn.addSupportIxs('redeem', includeAtaIxns, requestElevationGroup, false, addInitObligationForFarm, false, false);
251
+ await axn.addSupportIxs('redeem', includeAtaIxns, requestElevationGroup, false, addInitObligationForFarm, false, scopeRefreshConfig, false);
310
252
  axn.addRedeemReserveCollateralIx();
311
253
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
312
254
  return axn;
313
255
  }
314
- static async buildDepositObligationCollateralTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
256
+ static async buildDepositObligationCollateralTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
315
257
  includeAtaIxns = true, // if true it includes create and close wsol and token atas
316
258
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
317
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
259
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
318
260
  const axn = await KaminoAction.initialize('depositCollateral', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
319
261
  const addInitObligationForFarm = true;
320
262
  if (extraComputeBudget > 0) {
321
263
  axn.addComputeBudgetIxn(extraComputeBudget);
322
264
  }
323
- const allReserves = new utils_1.PublicKeySet([
324
- ...axn.depositReserves,
325
- ...axn.borrowReserves,
326
- axn.reserve.address,
327
- ]).toArray();
328
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
329
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
330
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
331
- }
332
- await axn.addSupportIxs('depositCollateral', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable);
265
+ await axn.addSupportIxs('depositCollateral', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable);
333
266
  if (useV2Ixs) {
334
267
  axn.addDepositObligationCollateralIxV2();
335
268
  }
@@ -339,10 +272,10 @@ class KaminoAction {
339
272
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
340
273
  return axn;
341
274
  }
342
- static async buildDepositAndBorrowTxns(kaminoMarket, depositAmount, depositMint, borrowAmount, borrowMint, payer, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
275
+ static async buildDepositAndBorrowTxns(kaminoMarket, depositAmount, depositMint, borrowAmount, borrowMint, payer, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
343
276
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
344
277
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
345
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
278
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
346
279
  const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'depositAndBorrow', depositAmount, depositMint, borrowMint, payer, payer, obligation, borrowAmount, referrer, currentSlot);
347
280
  const addInitObligationForFarmForDeposit = true;
348
281
  const addInitObligationForFarmForBorrow = false;
@@ -350,17 +283,7 @@ class KaminoAction {
350
283
  if (extraComputeBudget > 0) {
351
284
  axn.addComputeBudgetIxn(extraComputeBudget);
352
285
  }
353
- const allReserves = new utils_1.PublicKeySet([
354
- ...axn.depositReserves,
355
- ...axn.borrowReserves,
356
- axn.reserve.address,
357
- axn.outflowReserve.address,
358
- ]).toArray();
359
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
360
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
361
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
362
- }
363
- await axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForDeposit, useV2Ixs, createLookupTable, twoTokenAction);
286
+ await axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForDeposit, useV2Ixs, undefined, createLookupTable, twoTokenAction);
364
287
  if (useV2Ixs) {
365
288
  await axn.addDepositAndBorrowIxV2();
366
289
  }
@@ -369,62 +292,24 @@ class KaminoAction {
369
292
  }
370
293
  await axn.addInBetweenIxs('depositAndBorrow', includeAtaIxns, requestElevationGroup, addInitObligationForFarmForBorrow, useV2Ixs);
371
294
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
372
- return axn;
373
- }
374
- static async buildDepositAndWithdrawV2Txns(kaminoMarket, depositAmount, depositMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
375
- includeAtaIxns = true, // if true it includes create and close wsol and token atas,
376
- requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
377
- referrer = web3_js_1.PublicKey.default, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
378
- const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'depositAndWithdraw', depositAmount, depositMint, withdrawMint, payer, payer, obligation, withdrawAmount, referrer, currentSlot);
379
- const addInitObligationForFarm = true;
380
- const twoTokenAction = true;
381
- if (extraComputeBudget > 0) {
382
- axn.addComputeBudgetIxn(extraComputeBudget);
383
- }
295
+ // Create the scope refresh ixn in here to ensure it's the first ixn in the txn
384
296
  const allReserves = new utils_1.PublicKeySet([
385
297
  ...axn.depositReserves,
386
298
  ...axn.borrowReserves,
387
299
  axn.reserve.address,
388
- axn.outflowReserve.address,
300
+ ...(axn.outflowReserve ? [axn.outflowReserve.address] : []),
301
+ ...(axn.preLoadedDepositReservesSameTx ? axn.preLoadedDepositReservesSameTx : []),
389
302
  ]).toArray();
390
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
391
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
392
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
303
+ const tokenIds = (0, market_1.getTokenIdsForScopeRefresh)(axn.kaminoMarket, allReserves);
304
+ if (tokenIds.length > 0 && scopeRefreshConfig) {
305
+ await axn.addScopeRefreshIxs(scopeRefreshConfig.scope, tokenIds, scopeRefreshConfig.scopeFeed);
393
306
  }
394
- await axn.addSupportIxs('depositAndWithdraw', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, true, twoTokenAction);
395
- const withdrawCollateralAmount = axn.getWithdrawCollateralAmount(axn.outflowReserve, axn.outflowAmount);
396
- axn.addDepositAndWithdrawV2Ixs(withdrawCollateralAmount);
397
307
  return axn;
398
308
  }
399
- static async buildRepayAndWithdrawV2Txns(kaminoMarket, repayAmount, repayMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
309
+ static async buildRepayAndWithdrawTxns(kaminoMarket, repayAmount, repayMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
400
310
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
401
311
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
402
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
403
- const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'repayAndWithdrawV2', repayAmount, repayMint, withdrawMint, payer, payer, obligation, withdrawAmount, referrer, currentSlot);
404
- const addInitObligationForFarm = true;
405
- const twoTokenAction = true;
406
- if (extraComputeBudget > 0) {
407
- axn.addComputeBudgetIxn(extraComputeBudget);
408
- }
409
- const allReserves = new utils_1.PublicKeySet([
410
- ...axn.depositReserves,
411
- ...axn.borrowReserves,
412
- axn.reserve.address,
413
- axn.outflowReserve.address,
414
- ]).toArray();
415
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
416
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
417
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
418
- }
419
- await axn.addSupportIxs('repayAndWithdrawV2', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, true, createLookupTable, twoTokenAction);
420
- const withdrawCollateralAmount = axn.getWithdrawCollateralAmount(axn.outflowReserve, axn.outflowAmount);
421
- axn.addRepayAndWithdrawV2Ixs(withdrawCollateralAmount);
422
- return axn;
423
- }
424
- static async buildRepayAndWithdrawTxns(kaminoMarket, repayAmount, repayMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
425
- includeAtaIxns = true, // if true it includes create and close wsol and token atas,
426
- requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
427
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
312
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default) {
428
313
  const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'repayAndWithdraw', repayAmount, repayMint, withdrawMint, payer, payer, obligation, withdrawAmount, referrer, currentSlot);
429
314
  const addInitObligationForFarmForRepay = true;
430
315
  const addInitObligationForFarmForWithdraw = false;
@@ -432,17 +317,7 @@ class KaminoAction {
432
317
  if (extraComputeBudget > 0) {
433
318
  axn.addComputeBudgetIxn(extraComputeBudget);
434
319
  }
435
- const allReserves = new utils_1.PublicKeySet([
436
- ...axn.depositReserves,
437
- ...axn.borrowReserves,
438
- axn.reserve.address,
439
- axn.outflowReserve.address,
440
- ]).toArray();
441
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
442
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
443
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
444
- }
445
- await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForRepay, useV2Ixs, createLookupTable, twoTokenAction);
320
+ await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForRepay, useV2Ixs, undefined, createLookupTable, twoTokenAction);
446
321
  const withdrawCollateralAmount = axn.getWithdrawCollateralAmount(axn.outflowReserve, axn.outflowAmount);
447
322
  if (useV2Ixs) {
448
323
  await axn.addRepayAndWithdrawIxsV2(withdrawCollateralAmount);
@@ -452,13 +327,25 @@ class KaminoAction {
452
327
  }
453
328
  await axn.addInBetweenIxs('repayAndWithdraw', includeAtaIxns, requestElevationGroup, addInitObligationForFarmForWithdraw, useV2Ixs);
454
329
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
330
+ // Create the scope refresh ixn in here to ensure it's the first ixn in the txn
331
+ const allReserves = new utils_1.PublicKeySet([
332
+ ...axn.depositReserves,
333
+ ...axn.borrowReserves,
334
+ axn.reserve.address,
335
+ ...(axn.outflowReserve ? [axn.outflowReserve.address] : []),
336
+ ...(axn.preLoadedDepositReservesSameTx ? axn.preLoadedDepositReservesSameTx : []),
337
+ ]).toArray();
338
+ const tokenIds = (0, market_1.getTokenIdsForScopeRefresh)(axn.kaminoMarket, allReserves);
339
+ if (tokenIds.length > 0 && scopeRefreshConfig) {
340
+ await axn.addScopeRefreshIxs(scopeRefreshConfig.scope, tokenIds, scopeRefreshConfig.scopeFeed);
341
+ }
455
342
  return axn;
456
343
  }
457
- static async buildWithdrawTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
344
+ static async buildWithdrawTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
458
345
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
459
346
  requestElevationGroup = false, // to be requested *after* the withdraw
460
347
  includeUserMetadata = true, // if true it includes user metadata
461
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = undefined, overrideElevationGroupRequest,
348
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, overrideElevationGroupRequest,
462
349
  // Optional customizations which may be needed if the obligation was mutated by some previous ixn.
463
350
  obligationCustomizations) {
464
351
  const axn = await KaminoAction.initialize('withdraw', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
@@ -467,16 +354,7 @@ class KaminoAction {
467
354
  axn.addComputeBudgetIxn(extraComputeBudget);
468
355
  }
469
356
  axn.depositReserves.push(...(obligationCustomizations?.addedDepositReserves || []));
470
- const allReserves = new utils_1.PublicKeySet([
471
- ...axn.depositReserves,
472
- ...axn.borrowReserves,
473
- axn.reserve.address,
474
- ]).toArray();
475
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
476
- if (tokenIds.length > 0 && scopeRefresh && scopeRefresh.includeScopeRefresh) {
477
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
478
- }
479
- await axn.addSupportIxs('withdraw', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable, false, overrideElevationGroupRequest);
357
+ await axn.addSupportIxs('withdraw', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, false, overrideElevationGroupRequest);
480
358
  const collateralAmount = axn.getWithdrawCollateralAmount(axn.reserve, axn.amount);
481
359
  if (useV2Ixs) {
482
360
  await axn.addWithdrawIxV2(collateralAmount);
@@ -502,22 +380,13 @@ class KaminoAction {
502
380
  * @param includeUserMetadata - if true it includes user metadata
503
381
  * @param referrer
504
382
  */
505
- static async buildRepayTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, currentSlot, payer = undefined, extraComputeBudget = 1_000_000, includeAtaIxns = true, requestElevationGroup = false, includeUserMetadata = true, createLookupTable = true, referrer = web3_js_1.PublicKey.default, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
383
+ static async buildRepayTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, currentSlot, payer = undefined, extraComputeBudget = 1_000_000, includeAtaIxns = true, requestElevationGroup = false, includeUserMetadata = true, createLookupTable = true, referrer = web3_js_1.PublicKey.default) {
506
384
  const axn = await KaminoAction.initialize('repay', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot, payer);
507
385
  const addInitObligationForFarm = true;
508
386
  if (extraComputeBudget > 0) {
509
387
  axn.addComputeBudgetIxn(extraComputeBudget);
510
388
  }
511
- const allReserves = new utils_1.PublicKeySet([
512
- ...axn.depositReserves,
513
- ...axn.borrowReserves,
514
- axn.reserve.address,
515
- ]).toArray();
516
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
517
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
518
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
519
- }
520
- await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable);
389
+ await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable);
521
390
  if (useV2Ixs) {
522
391
  await axn.addRepayIxV2();
523
392
  }
@@ -527,26 +396,16 @@ class KaminoAction {
527
396
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
528
397
  return axn;
529
398
  }
530
- static async buildLiquidateTxns(kaminoMarket, amount, minCollateralReceiveAmount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
399
+ static async buildLiquidateTxns(kaminoMarket, amount, minCollateralReceiveAmount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
531
400
  includeAtaIxns = true, // if true it includes create and close wsol and token atas, and creates all other token atas if they don't exist
532
401
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
533
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, maxAllowedLtvOverridePercent = 0, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
402
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, maxAllowedLtvOverridePercent = 0, currentSlot = 0) {
534
403
  const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'liquidate', amount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, minCollateralReceiveAmount, referrer, currentSlot);
535
404
  const addInitObligationForFarm = true;
536
405
  if (extraComputeBudget > 0) {
537
406
  axn.addComputeBudgetIxn(extraComputeBudget);
538
407
  }
539
- const allReserves = new utils_1.PublicKeySet([
540
- ...axn.depositReserves,
541
- ...axn.borrowReserves,
542
- axn.reserve.address,
543
- axn.outflowReserve.address,
544
- ]).toArray();
545
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
546
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
547
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
548
- }
549
- await axn.addSupportIxs('liquidate', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable);
408
+ await axn.addSupportIxs('liquidate', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable);
550
409
  if (useV2Ixs) {
551
410
  await axn.addLiquidateIxV2(maxAllowedLtvOverridePercent);
552
411
  }
@@ -689,7 +548,7 @@ class KaminoAction {
689
548
  reserveFarmState: lib_1.PROGRAM_ID,
690
549
  }
691
550
  : {
692
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmCollateral, this.getObligationPda()),
551
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
693
552
  reserveFarmState: this.reserve.state.farmCollateral,
694
553
  };
695
554
  this.lendingIxsLabels.push(`depositReserveLiquidityAndObligationCollateralV2`);
@@ -739,7 +598,7 @@ class KaminoAction {
739
598
  reserveFarmState: lib_1.PROGRAM_ID,
740
599
  }
741
600
  : {
742
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmCollateral, this.getObligationPda()),
601
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
743
602
  reserveFarmState: this.reserve.state.farmCollateral,
744
603
  };
745
604
  this.lendingIxsLabels.push(`depositObligationCollateralV2`);
@@ -802,7 +661,7 @@ class KaminoAction {
802
661
  reserveFarmState: lib_1.PROGRAM_ID,
803
662
  }
804
663
  : {
805
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
664
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
806
665
  reserveFarmState: this.reserve.state.farmDebt,
807
666
  };
808
667
  const borrowIx = (0, instructions_1.borrowObligationLiquidityV2)({
@@ -860,7 +719,7 @@ class KaminoAction {
860
719
  reserveFarmState: lib_1.PROGRAM_ID,
861
720
  }
862
721
  : {
863
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmCollateral, this.getObligationPda()),
722
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
864
723
  reserveFarmState: this.reserve.state.farmCollateral,
865
724
  };
866
725
  this.lendingIxsLabels.push(`withdrawObligationCollateralAndRedeemReserveCollateralV2`);
@@ -920,7 +779,7 @@ class KaminoAction {
920
779
  reserveFarmState: lib_1.PROGRAM_ID,
921
780
  }
922
781
  : {
923
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
782
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
924
783
  reserveFarmState: this.reserve.state.farmDebt,
925
784
  };
926
785
  const depositReserveAccountMetas = depositReservesList.map((reserve) => {
@@ -948,165 +807,6 @@ class KaminoAction {
948
807
  this.obligation.state.elevationGroup > 0 ? repayIx.keys.concat([...depositReserveAccountMetas]) : repayIx.keys;
949
808
  this.lendingIxs.push(repayIx);
950
809
  }
951
- addRepayAndWithdrawV2Ixs(withdrawCollateralAmount) {
952
- this.lendingIxsLabels.push(`repayAndWithdrawAndRedeem(repayReserve=${this.reserve.address})(withdrawReserve=${this.outflowReserve.address})(obligation=${this.getObligationPda()})`);
953
- const depositReservesList = this.getAdditionalDepositReservesList();
954
- const depositReserveAccountMetas = depositReservesList.map((reserve) => {
955
- return { pubkey: reserve, isSigner: false, isWritable: true };
956
- });
957
- const borrowReserveAccountMetas = this.borrowReserves.map((reserve) => {
958
- return { pubkey: reserve, isSigner: false, isWritable: true };
959
- });
960
- if (!this.outflowAmount) {
961
- throw new Error(`outflowAmount not set`);
962
- }
963
- if (!this.outflowReserve) {
964
- throw new Error(`outflowReserve not set`);
965
- }
966
- if (!this.additionalTokenAccountAddress) {
967
- throw new Error(`additionalTokenAccountAddress not set`);
968
- }
969
- const collateralFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
970
- ? {
971
- obligationFarmUserState: this.kaminoMarket.programId,
972
- reserveFarmState: this.kaminoMarket.programId,
973
- }
974
- : {
975
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmCollateral, this.getObligationPda()),
976
- reserveFarmState: this.outflowReserve.state.farmCollateral,
977
- };
978
- const debtFarmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
979
- ? {
980
- obligationFarmUserState: this.kaminoMarket.programId,
981
- reserveFarmState: this.kaminoMarket.programId,
982
- }
983
- : {
984
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
985
- reserveFarmState: this.reserve.state.farmDebt,
986
- };
987
- const repayAndWithdrawIx = (0, instructions_1.repayAndWithdrawAndRedeem)({
988
- repayAmount: this.amount,
989
- withdrawCollateralAmount,
990
- }, {
991
- repayAccounts: {
992
- owner: this.owner,
993
- obligation: this.getObligationPda(),
994
- lendingMarket: this.kaminoMarket.getAddress(),
995
- repayReserve: this.reserve.address,
996
- reserveLiquidityMint: this.reserve.getLiquidityMint(),
997
- userSourceLiquidity: this.userTokenAccountAddress,
998
- reserveDestinationLiquidity: this.reserve.state.liquidity.supplyVault,
999
- tokenProgram: this.reserve.getLiquidityTokenProgram(),
1000
- instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
1001
- },
1002
- withdrawAccounts: {
1003
- owner: this.owner,
1004
- obligation: this.getObligationPda(),
1005
- lendingMarket: this.kaminoMarket.getAddress(),
1006
- lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
1007
- withdrawReserve: this.outflowReserve.address,
1008
- reserveLiquidityMint: this.outflowReserve.getLiquidityMint(),
1009
- reserveCollateralMint: this.outflowReserve.getCTokenMint(),
1010
- reserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
1011
- reserveSourceCollateral: this.outflowReserve.state.collateral.supplyVault,
1012
- userDestinationLiquidity: this.additionalTokenAccountAddress,
1013
- placeholderUserDestinationCollateral: this.kaminoMarket.programId,
1014
- collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1015
- liquidityTokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
1016
- instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
1017
- },
1018
- collateralFarmsAccounts,
1019
- debtFarmsAccounts,
1020
- farmsProgram: farms_sdk_1.farmsId,
1021
- }, this.kaminoMarket.programId);
1022
- repayAndWithdrawIx.keys = repayAndWithdrawIx.keys.concat([
1023
- ...depositReserveAccountMetas,
1024
- ...borrowReserveAccountMetas,
1025
- ]);
1026
- this.lendingIxs.push(repayAndWithdrawIx);
1027
- }
1028
- addDepositAndWithdrawV2Ixs(withdrawCollateralAmount) {
1029
- this.lendingIxsLabels.push(`depositAndWithdrawV2(depositReserve=${this.reserve.address})(withdrawReserve=${this.outflowReserve.address})(obligation=${this.getObligationPda()})`);
1030
- const depositReservesList = this.getAdditionalDepositReservesList();
1031
- const depositReserveAccountMetas = depositReservesList.map((reserve) => {
1032
- return { pubkey: reserve, isSigner: false, isWritable: true };
1033
- });
1034
- const borrowReserveAccountMetas = this.borrowReserves.map((reserve) => {
1035
- return { pubkey: reserve, isSigner: false, isWritable: true };
1036
- });
1037
- if (!this.outflowAmount) {
1038
- throw new Error(`outflowAmount not set`);
1039
- }
1040
- if (!this.outflowReserve) {
1041
- throw new Error(`outflowReserve not set`);
1042
- }
1043
- if (!this.additionalTokenAccountAddress) {
1044
- throw new Error(`additionalTokenAccountAddress not set`);
1045
- }
1046
- const depositFarmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
1047
- ? {
1048
- obligationFarmUserState: this.kaminoMarket.programId,
1049
- reserveFarmState: this.kaminoMarket.programId,
1050
- }
1051
- : {
1052
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
1053
- reserveFarmState: this.reserve.state.farmCollateral,
1054
- };
1055
- const withdrawFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
1056
- ? {
1057
- obligationFarmUserState: this.kaminoMarket.programId,
1058
- reserveFarmState: this.kaminoMarket.programId,
1059
- }
1060
- : {
1061
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmCollateral, this.getObligationPda()),
1062
- reserveFarmState: this.outflowReserve.state.farmCollateral,
1063
- };
1064
- const depositAndWithdrawIx = (0, instructions_1.depositAndWithdraw)({
1065
- liquidityAmount: this.amount,
1066
- withdrawCollateralAmount,
1067
- }, {
1068
- depositAccounts: {
1069
- owner: this.owner,
1070
- obligation: this.getObligationPda(),
1071
- lendingMarket: this.kaminoMarket.getAddress(),
1072
- lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
1073
- reserve: this.reserve.address,
1074
- reserveLiquidityMint: this.reserve.getLiquidityMint(),
1075
- reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
1076
- reserveCollateralMint: this.reserve.getCTokenMint(),
1077
- reserveDestinationDepositCollateral: this.reserve.state.collateral.supplyVault, // destinationCollateral
1078
- userSourceLiquidity: this.userTokenAccountAddress,
1079
- placeholderUserDestinationCollateral: this.kaminoMarket.programId,
1080
- collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1081
- liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
1082
- instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
1083
- },
1084
- withdrawAccounts: {
1085
- owner: this.owner,
1086
- obligation: this.getObligationPda(),
1087
- lendingMarket: this.kaminoMarket.getAddress(),
1088
- lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
1089
- withdrawReserve: this.outflowReserve.address,
1090
- reserveLiquidityMint: this.outflowReserve.getLiquidityMint(),
1091
- reserveCollateralMint: this.outflowReserve.getCTokenMint(),
1092
- reserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
1093
- reserveSourceCollateral: this.outflowReserve.state.collateral.supplyVault,
1094
- userDestinationLiquidity: this.additionalTokenAccountAddress,
1095
- placeholderUserDestinationCollateral: this.kaminoMarket.programId,
1096
- collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1097
- liquidityTokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
1098
- instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
1099
- },
1100
- depositFarmsAccounts,
1101
- withdrawFarmsAccounts,
1102
- farmsProgram: farms_sdk_1.farmsId,
1103
- }, this.kaminoMarket.programId);
1104
- depositAndWithdrawIx.keys = depositAndWithdrawIx.keys.concat([
1105
- ...depositReserveAccountMetas,
1106
- ...borrowReserveAccountMetas,
1107
- ]);
1108
- this.lendingIxs.push(depositAndWithdrawIx);
1109
- }
1110
810
  async addDepositAndBorrowIx() {
1111
811
  this.lendingIxsLabels.push(`depositReserveLiquidityAndObligationCollateral`);
1112
812
  this.lendingIxsLabels.push(`borrowObligationLiquidity`);
@@ -1170,7 +870,7 @@ class KaminoAction {
1170
870
  reserveFarmState: lib_1.PROGRAM_ID,
1171
871
  }
1172
872
  : {
1173
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmCollateral, this.getObligationPda()),
873
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
1174
874
  reserveFarmState: this.reserve.state.farmCollateral,
1175
875
  };
1176
876
  this.lendingIxsLabels.push(`depositReserveLiquidityAndObligationCollateralV2`);
@@ -1219,7 +919,7 @@ class KaminoAction {
1219
919
  reserveFarmState: lib_1.PROGRAM_ID,
1220
920
  }
1221
921
  : {
1222
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmDebt, this.getObligationPda()),
922
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.outflowReserve.state.farmDebt)[0],
1223
923
  reserveFarmState: this.outflowReserve.state.farmDebt,
1224
924
  };
1225
925
  const borrowIx = (0, instructions_1.borrowObligationLiquidityV2)({
@@ -1308,7 +1008,7 @@ class KaminoAction {
1308
1008
  reserveFarmState: lib_1.PROGRAM_ID,
1309
1009
  }
1310
1010
  : {
1311
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
1011
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
1312
1012
  reserveFarmState: this.reserve.state.farmDebt,
1313
1013
  };
1314
1014
  const repayIx = (0, instructions_1.repayObligationLiquidityV2)({
@@ -1346,7 +1046,7 @@ class KaminoAction {
1346
1046
  reserveFarmState: lib_1.PROGRAM_ID,
1347
1047
  }
1348
1048
  : {
1349
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmCollateral, this.getObligationPda()),
1049
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.outflowReserve.state.farmCollateral)[0],
1350
1050
  reserveFarmState: this.outflowReserve.state.farmCollateral,
1351
1051
  };
1352
1052
  this.lendingIxs.push((0, instructions_1.withdrawObligationCollateralAndRedeemReserveCollateralV2)({
@@ -1372,7 +1072,7 @@ class KaminoAction {
1372
1072
  farmsProgram: farms_sdk_1.farmsId,
1373
1073
  }, this.kaminoMarket.programId));
1374
1074
  }
1375
- addLiquidateIx(maxAllowedLtvOverridePercent = 0) {
1075
+ async addLiquidateIx(maxAllowedLtvOverridePercent = 0) {
1376
1076
  this.lendingIxsLabels.push(`liquidateObligationAndRedeemReserveCollateral`);
1377
1077
  if (!this.outflowReserve) {
1378
1078
  throw Error(`Withdraw reserve during liquidation is not defined`);
@@ -1435,7 +1135,7 @@ class KaminoAction {
1435
1135
  reserveFarmState: lib_1.PROGRAM_ID,
1436
1136
  }
1437
1137
  : {
1438
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmCollateral, this.getObligationPda()),
1138
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.outflowReserve.state.farmCollateral)[0],
1439
1139
  reserveFarmState: this.outflowReserve.state.farmCollateral,
1440
1140
  };
1441
1141
  const debtFarmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
@@ -1444,7 +1144,7 @@ class KaminoAction {
1444
1144
  reserveFarmState: lib_1.PROGRAM_ID,
1445
1145
  }
1446
1146
  : {
1447
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
1147
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
1448
1148
  reserveFarmState: this.reserve.state.farmDebt,
1449
1149
  };
1450
1150
  const liquidateIx = (0, instructions_1.liquidateObligationAndRedeemReserveCollateralV2)({
@@ -1512,7 +1212,6 @@ class KaminoAction {
1512
1212
  'repay',
1513
1213
  'depositAndBorrow',
1514
1214
  'repayAndWithdraw',
1515
- 'repayAndWithdrawV2',
1516
1215
  'refreshObligation',
1517
1216
  ].includes(action)) {
1518
1217
  // The support ixns in order are:
@@ -1528,14 +1227,11 @@ class KaminoAction {
1528
1227
  // 7. Ixn to refresh the `debt` farm of the obligation
1529
1228
  // 8. Ixn to refresh the `collateral` farm of the obligation
1530
1229
  let currentReserves = [];
1531
- if (action === 'liquidate' ||
1532
- action === 'depositAndBorrow' ||
1533
- action === 'repayAndWithdraw' ||
1534
- action === 'repayAndWithdrawV2') {
1230
+ if (action === 'liquidate' || action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
1535
1231
  if (!this.outflowReserve) {
1536
1232
  throw new Error('outflowReserve is undefined');
1537
1233
  }
1538
- if (action === 'depositAndBorrow' || action === 'repayAndWithdraw' || action === 'repayAndWithdrawV2') {
1234
+ if (action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
1539
1235
  currentReserves = [this.reserve, this.outflowReserve];
1540
1236
  if (action === 'depositAndBorrow') {
1541
1237
  if (this.obligation) {
@@ -1579,10 +1275,7 @@ class KaminoAction {
1579
1275
  await this.addInitObligationForFarm(this.outflowReserve, types_1.ReserveFarmKind.Debt, addAsSupportIx);
1580
1276
  }
1581
1277
  }
1582
- else if (action === 'repayAndWithdraw' ||
1583
- action === 'borrow' ||
1584
- action === 'repay' ||
1585
- action === 'repayAndWithdrawV2') {
1278
+ else if (action === 'repayAndWithdraw' || action === 'borrow' || action === 'repay') {
1586
1279
  // todo - probably don't need to add both debt and collateral for everything here
1587
1280
  await this.addInitObligationForFarm(this.reserve, types_1.ReserveFarmKind.Debt, addAsSupportIx);
1588
1281
  if (this.outflowReserve) {
@@ -1613,7 +1306,7 @@ class KaminoAction {
1613
1306
  this.addRefreshObligationIx(addAsSupportIx);
1614
1307
  }
1615
1308
  if (requestElevationGroup) {
1616
- if (action === 'repay' || action === 'repayAndWithdrawV2') {
1309
+ if (action === 'repay') {
1617
1310
  const repayObligationLiquidity = this.obligation.borrows.get(this.reserve.address);
1618
1311
  if (!repayObligationLiquidity) {
1619
1312
  throw new Error(`Could not find debt reserve ${this.reserve.address} in obligation`);
@@ -1737,7 +1430,7 @@ class KaminoAction {
1737
1430
  }
1738
1431
  }
1739
1432
  }
1740
- async addSupportIxs(action, includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable, twoTokenAction = false, overrideElevationGroupRequest) {
1433
+ async addSupportIxs(action, includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, twoTokenAction = false, overrideElevationGroupRequest) {
1741
1434
  if (!['mint', 'redeem'].includes(action)) {
1742
1435
  const [, ownerUserMetadata] = await this.kaminoMarket.getUserMetadata(this.owner);
1743
1436
  if (!ownerUserMetadata && includeUserMetadata) {
@@ -1757,9 +1450,20 @@ class KaminoAction {
1757
1450
  if (action === 'deposit' && this.outflowReserve) {
1758
1451
  await this.addInitReferrerTokenStateIx(this.outflowReserve);
1759
1452
  }
1760
- this.addInitObligationIxs();
1453
+ await this.addInitObligationIxs();
1761
1454
  }
1762
1455
  await this.addSupportIxsWithoutInitObligation(action, includeAtaIxns, useV2Ixs, 'setup', requestElevationGroup, addInitObligationForFarm, twoTokenAction, overrideElevationGroupRequest);
1456
+ const allReserves = new utils_1.PublicKeySet([
1457
+ ...this.depositReserves,
1458
+ ...this.borrowReserves,
1459
+ this.reserve.address,
1460
+ ...(this.outflowReserve ? [this.outflowReserve.address] : []),
1461
+ ...(this.preLoadedDepositReservesSameTx ? this.preLoadedDepositReservesSameTx : []),
1462
+ ]).toArray();
1463
+ const tokenIds = (0, market_1.getTokenIdsForScopeRefresh)(this.kaminoMarket, allReserves);
1464
+ if (tokenIds.length > 0 && scopeRefreshConfig) {
1465
+ await this.addScopeRefreshIxs(scopeRefreshConfig.scope, tokenIds, scopeRefreshConfig.scopeFeed);
1466
+ }
1763
1467
  }
1764
1468
  static optionalAccount(pubkey, programId = lib_1.PROGRAM_ID) {
1765
1469
  if ((0, utils_1.isNotNullPubkey)(pubkey)) {
@@ -1913,7 +1617,7 @@ class KaminoAction {
1913
1617
  farms.push([
1914
1618
  types_1.ReserveFarmKind.Collateral,
1915
1619
  kaminoReserve.state.farmCollateral,
1916
- (0, utils_1.obligationFarmStatePda)(kaminoReserve.state.farmCollateral, this.getObligationPda()),
1620
+ (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), kaminoReserve.state.farmCollateral)[0],
1917
1621
  kaminoReserve,
1918
1622
  ]);
1919
1623
  }
@@ -1921,7 +1625,7 @@ class KaminoAction {
1921
1625
  farms.push([
1922
1626
  types_1.ReserveFarmKind.Debt,
1923
1627
  kaminoReserve.state.farmDebt,
1924
- (0, utils_1.obligationFarmStatePda)(kaminoReserve.state.farmDebt, this.getObligationPda()),
1628
+ (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), kaminoReserve.state.farmDebt)[0],
1925
1629
  kaminoReserve,
1926
1630
  ]);
1927
1631
  }
@@ -1971,17 +1675,17 @@ class KaminoAction {
1971
1675
  async addInitObligationForFarm(reserve, mode, addAsSupportIx = 'setup') {
1972
1676
  const farms = [];
1973
1677
  if (mode === types_1.ReserveFarmKind.Collateral && (0, utils_1.isNotNullPubkey)(reserve.state.farmCollateral)) {
1974
- const pda = (0, utils_1.obligationFarmStatePda)(reserve.state.farmCollateral, this.getObligationPda());
1975
- const account = await this.kaminoMarket.getConnection().getAccountInfo(pda);
1678
+ const userPda = (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), reserve.state.farmCollateral)[0];
1679
+ const account = await this.kaminoMarket.getConnection().getAccountInfo(userPda);
1976
1680
  if (!account) {
1977
- farms.push([types_1.ReserveFarmKind.Collateral.discriminator, reserve.state.farmCollateral, pda]);
1681
+ farms.push([types_1.ReserveFarmKind.Collateral.discriminator, reserve.state.farmCollateral, userPda]);
1978
1682
  }
1979
1683
  }
1980
1684
  if (mode === types_1.ReserveFarmKind.Debt && (0, utils_1.isNotNullPubkey)(reserve.state.farmDebt)) {
1981
- const pda = (0, utils_1.obligationFarmStatePda)(reserve.state.farmDebt, this.getObligationPda());
1982
- const account = await this.kaminoMarket.getConnection().getAccountInfo(pda);
1685
+ const userPda = (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), reserve.state.farmDebt)[0];
1686
+ const account = await this.kaminoMarket.getConnection().getAccountInfo(userPda);
1983
1687
  if (!account) {
1984
- farms.push([types_1.ReserveFarmKind.Debt.discriminator, reserve.state.farmDebt, pda]);
1688
+ farms.push([types_1.ReserveFarmKind.Debt.discriminator, reserve.state.farmDebt, userPda]);
1985
1689
  }
1986
1690
  }
1987
1691
  farms.forEach((arg) => {
@@ -2010,7 +1714,7 @@ class KaminoAction {
2010
1714
  }
2011
1715
  });
2012
1716
  }
2013
- addInitObligationIxs() {
1717
+ async addInitObligationIxs() {
2014
1718
  if (!this.obligation) {
2015
1719
  const obligationPda = this.getObligationPda();
2016
1720
  const [userMetadataAddress, _bump] = (0, utils_1.userMetadataPda)(this.owner, this.kaminoMarket.programId);
@@ -2152,13 +1856,14 @@ class KaminoAction {
2152
1856
  return;
2153
1857
  }
2154
1858
  let safeRepay = new bn_js_1.default(this.amount);
2155
- if (this.obligation && (action === 'repay' || action === 'repayAndWithdrawV2') && this.amount.eq(new bn_js_1.default(utils_1.U64_MAX))) {
1859
+ if (this.obligation && action === 'repay' && this.amount.eq(new bn_js_1.default(utils_1.U64_MAX))) {
2156
1860
  const borrow = this.obligation.state.borrows.find((borrow) => borrow.borrowReserve.toString() === this.reserve.address.toString());
2157
1861
  if (!borrow) {
2158
1862
  throw Error(`Unable to find obligation borrow to repay for ${this.obligation.state.owner.toBase58()}`);
2159
1863
  }
2160
1864
  const cumulativeBorrowRateObligation = obligation_1.KaminoObligation.getCumulativeBorrowRate(borrow);
2161
1865
  const cumulativeBorrowRateReserve = this.reserve.getEstimatedCumulativeBorrowRate(this.currentSlot, this.kaminoMarket.state.referralFeeBps);
1866
+ // TODO: shouldn't this calc be added to all other stuff as well?
2162
1867
  safeRepay = new bn_js_1.default(Math.floor(obligation_1.KaminoObligation.getBorrowAmount(borrow)
2163
1868
  .mul(cumulativeBorrowRateReserve)
2164
1869
  .div(cumulativeBorrowRateObligation)
@@ -2177,7 +1882,6 @@ class KaminoAction {
2177
1882
  // Add rent exemption lamports for WSOL accounts that need to be pre-funded for inflow/send transactions
2178
1883
  const sendAction = action === 'deposit' ||
2179
1884
  action === 'repay' ||
2180
- action === 'repayAndWithdrawV2' ||
2181
1885
  action === 'mint' ||
2182
1886
  (action === 'liquidate' && this.secondaryMint?.equals(spl_token_1.NATIVE_MINT)); // only sync WSOL amount if liquidator repays SOL which is secondaryMint
2183
1887
  const transferLamportsIx = web3_js_1.SystemProgram.transfer({
@@ -2242,7 +1946,7 @@ class KaminoAction {
2242
1946
  primaryMint = inflowTokenMint;
2243
1947
  secondaryMint = outflowTokenMint;
2244
1948
  }
2245
- else if (action === 'repayAndWithdraw' || action === 'repayAndWithdrawV2') {
1949
+ else if (action === 'repayAndWithdraw') {
2246
1950
  primaryMint = inflowTokenMint;
2247
1951
  secondaryMint = outflowTokenMint;
2248
1952
  userTokenAccountAddress = userInflowTokenAccountAddress;