@kamino-finance/klend-sdk 5.11.3-beta.1 → 5.11.4

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 (65) hide show
  1. package/dist/classes/action.d.ts +23 -23
  2. package/dist/classes/action.d.ts.map +1 -1
  3. package/dist/classes/action.js +122 -195
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/index.d.ts +1 -0
  6. package/dist/classes/index.d.ts.map +1 -1
  7. package/dist/classes/index.js +1 -0
  8. package/dist/classes/index.js.map +1 -1
  9. package/dist/classes/manager.d.ts +6 -1
  10. package/dist/classes/manager.d.ts.map +1 -1
  11. package/dist/classes/manager.js +16 -1
  12. package/dist/classes/manager.js.map +1 -1
  13. package/dist/classes/market.d.ts +3 -3
  14. package/dist/classes/market.d.ts.map +1 -1
  15. package/dist/classes/market.js +30 -16
  16. package/dist/classes/market.js.map +1 -1
  17. package/dist/classes/obligation.d.ts +2 -0
  18. package/dist/classes/obligation.d.ts.map +1 -1
  19. package/dist/classes/obligation.js +5 -0
  20. package/dist/classes/obligation.js.map +1 -1
  21. package/dist/classes/vault.js +1 -1
  22. package/dist/classes/vault.js.map +1 -1
  23. package/dist/lending_operations/repay_with_collateral_operations.d.ts +4 -4
  24. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  25. package/dist/lending_operations/repay_with_collateral_operations.js +10 -8
  26. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  27. package/dist/lending_operations/swap_collateral_operations.d.ts +2 -2
  28. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  29. package/dist/lending_operations/swap_collateral_operations.js +11 -6
  30. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  31. package/dist/leverage/calcs.d.ts +5 -10
  32. package/dist/leverage/calcs.d.ts.map +1 -1
  33. package/dist/leverage/calcs.js +6 -13
  34. package/dist/leverage/calcs.js.map +1 -1
  35. package/dist/leverage/operations.d.ts +9 -7
  36. package/dist/leverage/operations.d.ts.map +1 -1
  37. package/dist/leverage/operations.js +78 -72
  38. package/dist/leverage/operations.js.map +1 -1
  39. package/dist/leverage/types.d.ts +4 -4
  40. package/dist/leverage/types.d.ts.map +1 -1
  41. package/dist/utils/ObligationType.d.ts +1 -1
  42. package/dist/utils/ObligationType.d.ts.map +1 -1
  43. package/dist/utils/managerTypes.d.ts.map +1 -1
  44. package/dist/utils/managerTypes.js +7 -52
  45. package/dist/utils/managerTypes.js.map +1 -1
  46. package/dist/utils/oracle.d.ts +3 -3
  47. package/dist/utils/oracle.d.ts.map +1 -1
  48. package/dist/utils/oracle.js +4 -3
  49. package/dist/utils/oracle.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/classes/action.ts +143 -211
  52. package/src/classes/index.ts +1 -0
  53. package/src/classes/manager.ts +32 -1
  54. package/src/classes/market.ts +34 -25
  55. package/src/classes/obligation.ts +6 -0
  56. package/src/classes/vault.ts +1 -1
  57. package/src/client.ts +8 -3
  58. package/src/lending_operations/repay_with_collateral_operations.ts +15 -11
  59. package/src/lending_operations/swap_collateral_operations.ts +19 -7
  60. package/src/leverage/calcs.ts +2 -18
  61. package/src/leverage/operations.ts +114 -72
  62. package/src/leverage/types.ts +4 -4
  63. package/src/utils/ObligationType.ts +1 -1
  64. package/src/utils/managerTypes.ts +10 -52
  65. package/src/utils/oracle.ts +7 -6
@@ -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,30 @@ 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);
220
+ if (!axn.referrer.equals(web3_js_1.PublicKey.default)) {
221
+ const referrerTokenState = (0, utils_1.referrerTokenStatePda)(axn.referrer, axn.reserve.address, axn.kaminoMarket.programId)[0];
222
+ const account = await axn.kaminoMarket.getConnection().getAccountInfo(referrerTokenState);
223
+ if (!account) {
224
+ axn.addInitReferrerTokenStateIx(axn.reserve, referrerTokenState);
225
+ }
259
226
  }
260
- await axn.addSupportIxs('borrow', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable, undefined, overrideElevationGroupRequest);
227
+ await axn.addSupportIxs('borrow', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, undefined, overrideElevationGroupRequest);
261
228
  if (useV2Ixs) {
262
229
  axn.addBorrowIxV2();
263
230
  }
@@ -267,69 +234,42 @@ class KaminoAction {
267
234
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
268
235
  return axn;
269
236
  }
270
- static async buildDepositReserveLiquidityTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
237
+ static async buildDepositReserveLiquidityTxns(kaminoMarket, amount, mint, owner, obligation, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
271
238
  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' }) {
239
+ requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
273
240
  const axn = await KaminoAction.initialize('mint', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
274
241
  const addInitObligationForFarm = true;
275
242
  if (extraComputeBudget > 0) {
276
243
  axn.addComputeBudgetIxn(extraComputeBudget);
277
244
  }
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);
245
+ await axn.addSupportIxs('mint', includeAtaIxns, requestElevationGroup, false, addInitObligationForFarm, false, scopeRefreshConfig, false);
288
246
  axn.addDepositReserveLiquidityIx();
289
247
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
290
248
  return axn;
291
249
  }
292
- static async buildRedeemReserveCollateralTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
250
+ static async buildRedeemReserveCollateralTxns(kaminoMarket, amount, mint, owner, obligation, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
293
251
  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' }) {
252
+ requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
295
253
  const axn = await KaminoAction.initialize('redeem', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
296
254
  const addInitObligationForFarm = true;
297
255
  if (extraComputeBudget > 0) {
298
256
  axn.addComputeBudgetIxn(extraComputeBudget);
299
257
  }
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);
258
+ await axn.addSupportIxs('redeem', includeAtaIxns, requestElevationGroup, false, addInitObligationForFarm, false, scopeRefreshConfig, false);
310
259
  axn.addRedeemReserveCollateralIx();
311
260
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
312
261
  return axn;
313
262
  }
314
- static async buildDepositObligationCollateralTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
263
+ static async buildDepositObligationCollateralTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
315
264
  includeAtaIxns = true, // if true it includes create and close wsol and token atas
316
265
  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' }) {
266
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
318
267
  const axn = await KaminoAction.initialize('depositCollateral', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
319
268
  const addInitObligationForFarm = true;
320
269
  if (extraComputeBudget > 0) {
321
270
  axn.addComputeBudgetIxn(extraComputeBudget);
322
271
  }
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);
272
+ await axn.addSupportIxs('depositCollateral', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable);
333
273
  if (useV2Ixs) {
334
274
  axn.addDepositObligationCollateralIxV2();
335
275
  }
@@ -339,10 +279,10 @@ class KaminoAction {
339
279
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
340
280
  return axn;
341
281
  }
342
- static async buildDepositAndBorrowTxns(kaminoMarket, depositAmount, depositMint, borrowAmount, borrowMint, payer, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
282
+ static async buildDepositAndBorrowTxns(kaminoMarket, depositAmount, depositMint, borrowAmount, borrowMint, payer, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
343
283
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
344
284
  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' }) {
285
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
346
286
  const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'depositAndBorrow', depositAmount, depositMint, borrowMint, payer, payer, obligation, borrowAmount, referrer, currentSlot);
347
287
  const addInitObligationForFarmForDeposit = true;
348
288
  const addInitObligationForFarmForBorrow = false;
@@ -350,17 +290,14 @@ class KaminoAction {
350
290
  if (extraComputeBudget > 0) {
351
291
  axn.addComputeBudgetIxn(extraComputeBudget);
352
292
  }
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);
293
+ if (!axn.referrer.equals(web3_js_1.PublicKey.default)) {
294
+ const referrerTokenState = (0, utils_1.referrerTokenStatePda)(axn.referrer, axn.outflowReserve.address, axn.kaminoMarket.programId)[0];
295
+ const account = await axn.kaminoMarket.getConnection().getAccountInfo(referrerTokenState);
296
+ if (!account) {
297
+ axn.addInitReferrerTokenStateIx(axn.outflowReserve, referrerTokenState);
298
+ }
362
299
  }
363
- await axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForDeposit, useV2Ixs, createLookupTable, twoTokenAction);
300
+ await axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForDeposit, useV2Ixs, undefined, createLookupTable, twoTokenAction);
364
301
  if (useV2Ixs) {
365
302
  await axn.addDepositAndBorrowIxV2();
366
303
  }
@@ -369,12 +306,24 @@ class KaminoAction {
369
306
  }
370
307
  await axn.addInBetweenIxs('depositAndBorrow', includeAtaIxns, requestElevationGroup, addInitObligationForFarmForBorrow, useV2Ixs);
371
308
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
309
+ // Create the scope refresh ixn in here to ensure it's the first ixn in the txn
310
+ const allReserves = new utils_1.PublicKeySet([
311
+ ...axn.depositReserves,
312
+ ...axn.borrowReserves,
313
+ axn.reserve.address,
314
+ ...(axn.outflowReserve ? [axn.outflowReserve.address] : []),
315
+ ...(axn.preLoadedDepositReservesSameTx ? axn.preLoadedDepositReservesSameTx : []),
316
+ ]).toArray();
317
+ const tokenIds = (0, market_1.getTokenIdsForScopeRefresh)(axn.kaminoMarket, allReserves);
318
+ if (tokenIds.length > 0 && scopeRefreshConfig) {
319
+ await axn.addScopeRefreshIxs(scopeRefreshConfig.scope, tokenIds, scopeRefreshConfig.scopeFeed);
320
+ }
372
321
  return axn;
373
322
  }
374
- static async buildRepayAndWithdrawTxns(kaminoMarket, repayAmount, repayMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
323
+ static async buildRepayAndWithdrawTxns(kaminoMarket, repayAmount, repayMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
375
324
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
376
325
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
377
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
326
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default) {
378
327
  const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'repayAndWithdraw', repayAmount, repayMint, withdrawMint, payer, payer, obligation, withdrawAmount, referrer, currentSlot);
379
328
  const addInitObligationForFarmForRepay = true;
380
329
  const addInitObligationForFarmForWithdraw = false;
@@ -382,17 +331,7 @@ class KaminoAction {
382
331
  if (extraComputeBudget > 0) {
383
332
  axn.addComputeBudgetIxn(extraComputeBudget);
384
333
  }
385
- const allReserves = new utils_1.PublicKeySet([
386
- ...axn.depositReserves,
387
- ...axn.borrowReserves,
388
- axn.reserve.address,
389
- axn.outflowReserve.address,
390
- ]).toArray();
391
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
392
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
393
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
394
- }
395
- await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForRepay, useV2Ixs, createLookupTable, twoTokenAction);
334
+ await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForRepay, useV2Ixs, undefined, createLookupTable, twoTokenAction);
396
335
  const withdrawCollateralAmount = axn.getWithdrawCollateralAmount(axn.outflowReserve, axn.outflowAmount);
397
336
  if (useV2Ixs) {
398
337
  await axn.addRepayAndWithdrawIxsV2(withdrawCollateralAmount);
@@ -402,13 +341,25 @@ class KaminoAction {
402
341
  }
403
342
  await axn.addInBetweenIxs('repayAndWithdraw', includeAtaIxns, requestElevationGroup, addInitObligationForFarmForWithdraw, useV2Ixs);
404
343
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
344
+ // Create the scope refresh ixn in here to ensure it's the first ixn in the txn
345
+ const allReserves = new utils_1.PublicKeySet([
346
+ ...axn.depositReserves,
347
+ ...axn.borrowReserves,
348
+ axn.reserve.address,
349
+ ...(axn.outflowReserve ? [axn.outflowReserve.address] : []),
350
+ ...(axn.preLoadedDepositReservesSameTx ? axn.preLoadedDepositReservesSameTx : []),
351
+ ]).toArray();
352
+ const tokenIds = (0, market_1.getTokenIdsForScopeRefresh)(axn.kaminoMarket, allReserves);
353
+ if (tokenIds.length > 0 && scopeRefreshConfig) {
354
+ await axn.addScopeRefreshIxs(scopeRefreshConfig.scope, tokenIds, scopeRefreshConfig.scopeFeed);
355
+ }
405
356
  return axn;
406
357
  }
407
- static async buildWithdrawTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
358
+ static async buildWithdrawTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
408
359
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
409
360
  requestElevationGroup = false, // to be requested *after* the withdraw
410
361
  includeUserMetadata = true, // if true it includes user metadata
411
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = undefined, overrideElevationGroupRequest,
362
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, overrideElevationGroupRequest,
412
363
  // Optional customizations which may be needed if the obligation was mutated by some previous ixn.
413
364
  obligationCustomizations) {
414
365
  const axn = await KaminoAction.initialize('withdraw', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
@@ -417,16 +368,7 @@ class KaminoAction {
417
368
  axn.addComputeBudgetIxn(extraComputeBudget);
418
369
  }
419
370
  axn.depositReserves.push(...(obligationCustomizations?.addedDepositReserves || []));
420
- const allReserves = new utils_1.PublicKeySet([
421
- ...axn.depositReserves,
422
- ...axn.borrowReserves,
423
- axn.reserve.address,
424
- ]).toArray();
425
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
426
- if (tokenIds.length > 0 && scopeRefresh && scopeRefresh.includeScopeRefresh) {
427
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
428
- }
429
- await axn.addSupportIxs('withdraw', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable, false, overrideElevationGroupRequest);
371
+ await axn.addSupportIxs('withdraw', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, false, overrideElevationGroupRequest);
430
372
  const collateralAmount = axn.getWithdrawCollateralAmount(axn.reserve, axn.amount);
431
373
  if (useV2Ixs) {
432
374
  await axn.addWithdrawIxV2(collateralAmount);
@@ -452,22 +394,13 @@ class KaminoAction {
452
394
  * @param includeUserMetadata - if true it includes user metadata
453
395
  * @param referrer
454
396
  */
455
- 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' }) {
397
+ 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) {
456
398
  const axn = await KaminoAction.initialize('repay', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot, payer);
457
399
  const addInitObligationForFarm = true;
458
400
  if (extraComputeBudget > 0) {
459
401
  axn.addComputeBudgetIxn(extraComputeBudget);
460
402
  }
461
- const allReserves = new utils_1.PublicKeySet([
462
- ...axn.depositReserves,
463
- ...axn.borrowReserves,
464
- axn.reserve.address,
465
- ]).toArray();
466
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
467
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
468
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
469
- }
470
- await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable);
403
+ await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable);
471
404
  if (useV2Ixs) {
472
405
  await axn.addRepayIxV2();
473
406
  }
@@ -477,26 +410,16 @@ class KaminoAction {
477
410
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
478
411
  return axn;
479
412
  }
480
- static async buildLiquidateTxns(kaminoMarket, amount, minCollateralReceiveAmount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
413
+ static async buildLiquidateTxns(kaminoMarket, amount, minCollateralReceiveAmount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
481
414
  includeAtaIxns = true, // if true it includes create and close wsol and token atas, and creates all other token atas if they don't exist
482
415
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
483
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, maxAllowedLtvOverridePercent = 0, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
416
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, maxAllowedLtvOverridePercent = 0, currentSlot = 0) {
484
417
  const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'liquidate', amount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, minCollateralReceiveAmount, referrer, currentSlot);
485
418
  const addInitObligationForFarm = true;
486
419
  if (extraComputeBudget > 0) {
487
420
  axn.addComputeBudgetIxn(extraComputeBudget);
488
421
  }
489
- const allReserves = new utils_1.PublicKeySet([
490
- ...axn.depositReserves,
491
- ...axn.borrowReserves,
492
- axn.reserve.address,
493
- axn.outflowReserve.address,
494
- ]).toArray();
495
- const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
496
- if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
497
- await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
498
- }
499
- await axn.addSupportIxs('liquidate', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable);
422
+ await axn.addSupportIxs('liquidate', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable);
500
423
  if (useV2Ixs) {
501
424
  await axn.addLiquidateIxV2(maxAllowedLtvOverridePercent);
502
425
  }
@@ -510,6 +433,13 @@ class KaminoAction {
510
433
  const { axn, createAtaIxs } = await KaminoAction.initializeWithdrawReferrerFees(tokenMint, owner, kaminoMarket, currentSlot);
511
434
  axn.preTxnIxs.push(...createAtaIxs);
512
435
  axn.preTxnIxsLabels.push(`createAtasIxs[${axn.userTokenAccountAddress.toString()}]`);
436
+ if (!axn.referrer.equals(web3_js_1.PublicKey.default)) {
437
+ const referrerTokenState = (0, utils_1.referrerTokenStatePda)(axn.referrer, axn.reserve.address, axn.kaminoMarket.programId)[0];
438
+ const account = await axn.kaminoMarket.getConnection().getAccountInfo(referrerTokenState);
439
+ if (!account) {
440
+ axn.addInitReferrerTokenStateIx(axn.reserve, referrerTokenState);
441
+ }
442
+ }
513
443
  axn.addRefreshReserveIxs([axn.reserve.address]);
514
444
  axn.addWithdrawReferrerFeesIxs();
515
445
  return axn;
@@ -635,8 +565,8 @@ class KaminoAction {
635
565
  addDepositIxV2() {
636
566
  const farmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
637
567
  ? {
638
- obligationFarmUserState: lib_1.PROGRAM_ID,
639
- reserveFarmState: lib_1.PROGRAM_ID,
568
+ obligationFarmUserState: this.kaminoMarket.programId,
569
+ reserveFarmState: this.kaminoMarket.programId,
640
570
  }
641
571
  : {
642
572
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
@@ -685,8 +615,8 @@ class KaminoAction {
685
615
  addDepositObligationCollateralIxV2() {
686
616
  const farmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
687
617
  ? {
688
- obligationFarmUserState: lib_1.PROGRAM_ID,
689
- reserveFarmState: lib_1.PROGRAM_ID,
618
+ obligationFarmUserState: this.kaminoMarket.programId,
619
+ reserveFarmState: this.kaminoMarket.programId,
690
620
  }
691
621
  : {
692
622
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
@@ -748,8 +678,8 @@ class KaminoAction {
748
678
  });
749
679
  const farmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
750
680
  ? {
751
- obligationFarmUserState: lib_1.PROGRAM_ID,
752
- reserveFarmState: lib_1.PROGRAM_ID,
681
+ obligationFarmUserState: this.kaminoMarket.programId,
682
+ reserveFarmState: this.kaminoMarket.programId,
753
683
  }
754
684
  : {
755
685
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
@@ -806,8 +736,8 @@ class KaminoAction {
806
736
  async addWithdrawIxV2(collateralAmount) {
807
737
  const farmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
808
738
  ? {
809
- obligationFarmUserState: lib_1.PROGRAM_ID,
810
- reserveFarmState: lib_1.PROGRAM_ID,
739
+ obligationFarmUserState: this.kaminoMarket.programId,
740
+ reserveFarmState: this.kaminoMarket.programId,
811
741
  }
812
742
  : {
813
743
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
@@ -866,8 +796,8 @@ class KaminoAction {
866
796
  const depositReservesList = this.getAdditionalDepositReservesList();
867
797
  const farmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
868
798
  ? {
869
- obligationFarmUserState: lib_1.PROGRAM_ID,
870
- reserveFarmState: lib_1.PROGRAM_ID,
799
+ obligationFarmUserState: this.kaminoMarket.programId,
800
+ reserveFarmState: this.kaminoMarket.programId,
871
801
  }
872
802
  : {
873
803
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
@@ -957,8 +887,8 @@ class KaminoAction {
957
887
  async addDepositAndBorrowIxV2() {
958
888
  const collateralFarmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
959
889
  ? {
960
- obligationFarmUserState: lib_1.PROGRAM_ID,
961
- reserveFarmState: lib_1.PROGRAM_ID,
890
+ obligationFarmUserState: this.kaminoMarket.programId,
891
+ reserveFarmState: this.kaminoMarket.programId,
962
892
  }
963
893
  : {
964
894
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
@@ -1006,8 +936,8 @@ class KaminoAction {
1006
936
  });
1007
937
  const debtFarmsAccounts = this.outflowReserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
1008
938
  ? {
1009
- obligationFarmUserState: lib_1.PROGRAM_ID,
1010
- reserveFarmState: lib_1.PROGRAM_ID,
939
+ obligationFarmUserState: this.kaminoMarket.programId,
940
+ reserveFarmState: this.kaminoMarket.programId,
1011
941
  }
1012
942
  : {
1013
943
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.outflowReserve.state.farmDebt)[0],
@@ -1095,8 +1025,8 @@ class KaminoAction {
1095
1025
  });
1096
1026
  const debtFarmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
1097
1027
  ? {
1098
- obligationFarmUserState: lib_1.PROGRAM_ID,
1099
- reserveFarmState: lib_1.PROGRAM_ID,
1028
+ obligationFarmUserState: this.kaminoMarket.programId,
1029
+ reserveFarmState: this.kaminoMarket.programId,
1100
1030
  }
1101
1031
  : {
1102
1032
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
@@ -1133,8 +1063,8 @@ class KaminoAction {
1133
1063
  }
1134
1064
  const collateralFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
1135
1065
  ? {
1136
- obligationFarmUserState: lib_1.PROGRAM_ID,
1137
- reserveFarmState: lib_1.PROGRAM_ID,
1066
+ obligationFarmUserState: this.kaminoMarket.programId,
1067
+ reserveFarmState: this.kaminoMarket.programId,
1138
1068
  }
1139
1069
  : {
1140
1070
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.outflowReserve.state.farmCollateral)[0],
@@ -1222,8 +1152,8 @@ class KaminoAction {
1222
1152
  });
1223
1153
  const collateralFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
1224
1154
  ? {
1225
- obligationFarmUserState: lib_1.PROGRAM_ID,
1226
- reserveFarmState: lib_1.PROGRAM_ID,
1155
+ obligationFarmUserState: this.kaminoMarket.programId,
1156
+ reserveFarmState: this.kaminoMarket.programId,
1227
1157
  }
1228
1158
  : {
1229
1159
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.outflowReserve.state.farmCollateral)[0],
@@ -1231,8 +1161,8 @@ class KaminoAction {
1231
1161
  };
1232
1162
  const debtFarmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
1233
1163
  ? {
1234
- obligationFarmUserState: lib_1.PROGRAM_ID,
1235
- reserveFarmState: lib_1.PROGRAM_ID,
1164
+ obligationFarmUserState: this.kaminoMarket.programId,
1165
+ reserveFarmState: this.kaminoMarket.programId,
1236
1166
  }
1237
1167
  : {
1238
1168
  obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
@@ -1521,7 +1451,7 @@ class KaminoAction {
1521
1451
  }
1522
1452
  }
1523
1453
  }
1524
- async addSupportIxs(action, includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable, twoTokenAction = false, overrideElevationGroupRequest) {
1454
+ async addSupportIxs(action, includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, twoTokenAction = false, overrideElevationGroupRequest) {
1525
1455
  if (!['mint', 'redeem'].includes(action)) {
1526
1456
  const [, ownerUserMetadata] = await this.kaminoMarket.getUserMetadata(this.owner);
1527
1457
  if (!ownerUserMetadata && includeUserMetadata) {
@@ -1534,16 +1464,20 @@ class KaminoAction {
1534
1464
  }
1535
1465
  this.addInitUserMetadataIxs(lookupTable);
1536
1466
  }
1537
- if (['borrow', 'withdrawReferrerFees'].includes(action)) {
1538
- await this.addInitReferrerTokenStateIx(this.reserve);
1539
- }
1540
- // depositAndBorrow
1541
- if (action === 'deposit' && this.outflowReserve) {
1542
- await this.addInitReferrerTokenStateIx(this.outflowReserve);
1543
- }
1544
1467
  await this.addInitObligationIxs();
1545
1468
  }
1546
1469
  await this.addSupportIxsWithoutInitObligation(action, includeAtaIxns, useV2Ixs, 'setup', requestElevationGroup, addInitObligationForFarm, twoTokenAction, overrideElevationGroupRequest);
1470
+ const allReserves = new utils_1.PublicKeySet([
1471
+ ...this.depositReserves,
1472
+ ...this.borrowReserves,
1473
+ this.reserve.address,
1474
+ ...(this.outflowReserve ? [this.outflowReserve.address] : []),
1475
+ ...(this.preLoadedDepositReservesSameTx ? this.preLoadedDepositReservesSameTx : []),
1476
+ ]).toArray();
1477
+ const tokenIds = (0, market_1.getTokenIdsForScopeRefresh)(this.kaminoMarket, allReserves);
1478
+ if (tokenIds.length > 0 && scopeRefreshConfig) {
1479
+ await this.addScopeRefreshIxs(scopeRefreshConfig.scope, tokenIds, scopeRefreshConfig.scopeFeed);
1480
+ }
1547
1481
  }
1548
1482
  static optionalAccount(pubkey, programId = lib_1.PROGRAM_ID) {
1549
1483
  if ((0, utils_1.isNotNullPubkey)(pubkey)) {
@@ -1836,25 +1770,18 @@ class KaminoAction {
1836
1770
  this.setupIxs.push(initUserMetadataIx);
1837
1771
  this.setupIxsLabels.push(`initUserMetadata[${userMetadataAddress.toString()}]`);
1838
1772
  }
1839
- async addInitReferrerTokenStateIx(reserve) {
1840
- if (this.referrer.equals(web3_js_1.PublicKey.default)) {
1841
- return;
1842
- }
1843
- const referrerTokenState = (0, utils_1.referrerTokenStatePda)(this.referrer, reserve.address, this.kaminoMarket.programId)[0];
1844
- const account = await this.kaminoMarket.getConnection().getAccountInfo(referrerTokenState);
1845
- if (!account) {
1846
- const initReferrerTokenStateIx = (0, instructions_1.initReferrerTokenState)({
1847
- lendingMarket: this.kaminoMarket.getAddress(),
1848
- payer: this.owner,
1849
- reserve: reserve.address,
1850
- referrer: this.referrer,
1851
- referrerTokenState,
1852
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
1853
- systemProgram: web3_js_1.SystemProgram.programId,
1854
- }, this.kaminoMarket.programId);
1855
- this.setupIxs.unshift(initReferrerTokenStateIx);
1856
- this.setupIxsLabels.unshift(`InitReferrerTokenState[${referrerTokenState.toString()} res=${reserve.address}]`);
1857
- }
1773
+ addInitReferrerTokenStateIx(reserve, referrerTokenState) {
1774
+ const initReferrerTokenStateIx = (0, instructions_1.initReferrerTokenState)({
1775
+ lendingMarket: this.kaminoMarket.getAddress(),
1776
+ payer: this.owner,
1777
+ reserve: reserve.address,
1778
+ referrer: this.referrer,
1779
+ referrerTokenState,
1780
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
1781
+ systemProgram: web3_js_1.SystemProgram.programId,
1782
+ }, this.kaminoMarket.programId);
1783
+ this.setupIxs.unshift(initReferrerTokenStateIx);
1784
+ this.setupIxsLabels.unshift(`InitReferrerTokenState[${referrerTokenState.toString()} res=${reserve.address}]`);
1858
1785
  }
1859
1786
  addWithdrawReferrerFeesIxs() {
1860
1787
  const referrerTokenStateAddress = (0, utils_1.referrerTokenStatePda)(this.owner, this.reserve.address, this.kaminoMarket.programId)[0];