@kamino-finance/klend-sdk 5.11.5-beta.0 → 5.11.6-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 (145) 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 +225 -350
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/index.d.ts +0 -1
  6. package/dist/classes/index.d.ts.map +1 -1
  7. package/dist/classes/index.js +0 -1
  8. package/dist/classes/index.js.map +1 -1
  9. package/dist/classes/lut_utils.d.ts +29 -0
  10. package/dist/classes/lut_utils.d.ts.map +1 -0
  11. package/dist/classes/lut_utils.js +62 -0
  12. package/dist/classes/lut_utils.js.map +1 -0
  13. package/dist/classes/manager.d.ts +1 -6
  14. package/dist/classes/manager.d.ts.map +1 -1
  15. package/dist/classes/manager.js +1 -17
  16. package/dist/classes/manager.js.map +1 -1
  17. package/dist/classes/market.d.ts +3 -3
  18. package/dist/classes/market.d.ts.map +1 -1
  19. package/dist/classes/market.js +16 -30
  20. package/dist/classes/market.js.map +1 -1
  21. package/dist/classes/obligation.d.ts +1 -3
  22. package/dist/classes/obligation.d.ts.map +1 -1
  23. package/dist/classes/obligation.js +1 -6
  24. package/dist/classes/obligation.js.map +1 -1
  25. package/dist/classes/vault.d.ts +0 -1
  26. package/dist/classes/vault.d.ts.map +1 -1
  27. package/dist/classes/vault.js +22 -25
  28. package/dist/classes/vault.js.map +1 -1
  29. package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts +3 -6
  30. package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts.map +1 -1
  31. package/dist/idl_codegen_kamino_vault/accounts/VaultState.js +7 -13
  32. package/dist/idl_codegen_kamino_vault/accounts/VaultState.js.map +1 -1
  33. package/dist/idl_codegen_kamino_vault/errors/custom.d.ts +1 -25
  34. package/dist/idl_codegen_kamino_vault/errors/custom.d.ts.map +1 -1
  35. package/dist/idl_codegen_kamino_vault/errors/custom.js +1 -43
  36. package/dist/idl_codegen_kamino_vault/errors/custom.js.map +1 -1
  37. package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.d.ts +1 -1
  38. package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.d.ts.map +1 -1
  39. package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.js +1 -1
  40. package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.js.map +1 -1
  41. package/dist/idl_codegen_kamino_vault/instructions/initVault.d.ts +0 -1
  42. package/dist/idl_codegen_kamino_vault/instructions/initVault.d.ts.map +1 -1
  43. package/dist/idl_codegen_kamino_vault/instructions/initVault.js +0 -1
  44. package/dist/idl_codegen_kamino_vault/instructions/initVault.js.map +1 -1
  45. package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.d.ts +1 -1
  46. package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.d.ts.map +1 -1
  47. package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.js +1 -1
  48. package/dist/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.js.map +1 -1
  49. package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.d.ts +1 -1
  50. package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.d.ts.map +1 -1
  51. package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.js +1 -1
  52. package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.js.map +1 -1
  53. package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.d.ts +1 -1
  54. package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.d.ts.map +1 -1
  55. package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.js +1 -1
  56. package/dist/idl_codegen_kamino_vault/instructions/updateSharesMetadata.js.map +1 -1
  57. package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.d.ts +1 -1
  58. package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.d.ts.map +1 -1
  59. package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.js +1 -1
  60. package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.js.map +1 -1
  61. package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.d.ts +1 -1
  62. package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.d.ts.map +1 -1
  63. package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.js +1 -1
  64. package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.js.map +1 -1
  65. package/dist/idl_codegen_kamino_vault/types/VaultConfigField.d.ts +0 -13
  66. package/dist/idl_codegen_kamino_vault/types/VaultConfigField.d.ts.map +1 -1
  67. package/dist/idl_codegen_kamino_vault/types/VaultConfigField.js +1 -25
  68. package/dist/idl_codegen_kamino_vault/types/VaultConfigField.js.map +1 -1
  69. package/dist/idl_codegen_kamino_vault/types/index.d.ts +2 -2
  70. package/dist/idl_codegen_kamino_vault/types/index.d.ts.map +1 -1
  71. package/dist/idl_codegen_kamino_vault/types/index.js.map +1 -1
  72. package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -1
  73. package/dist/lending_operations/repay_with_collateral_calcs.js +5 -9
  74. package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
  75. package/dist/lending_operations/repay_with_collateral_operations.d.ts +4 -9
  76. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  77. package/dist/lending_operations/repay_with_collateral_operations.js +8 -35
  78. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  79. package/dist/lending_operations/swap_collateral_operations.d.ts +2 -2
  80. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  81. package/dist/lending_operations/swap_collateral_operations.js +6 -11
  82. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  83. package/dist/leverage/operations.d.ts +7 -9
  84. package/dist/leverage/operations.d.ts.map +1 -1
  85. package/dist/leverage/operations.js +66 -78
  86. package/dist/leverage/operations.js.map +1 -1
  87. package/dist/leverage/types.d.ts +4 -4
  88. package/dist/leverage/types.d.ts.map +1 -1
  89. package/dist/utils/ObligationType.d.ts +1 -1
  90. package/dist/utils/ObligationType.d.ts.map +1 -1
  91. package/dist/utils/constants.d.ts +0 -1
  92. package/dist/utils/constants.d.ts.map +1 -1
  93. package/dist/utils/constants.js +1 -2
  94. package/dist/utils/constants.js.map +1 -1
  95. package/dist/utils/lookupTable.d.ts +0 -27
  96. package/dist/utils/lookupTable.d.ts.map +1 -1
  97. package/dist/utils/lookupTable.js +0 -58
  98. package/dist/utils/lookupTable.js.map +1 -1
  99. package/dist/utils/managerTypes.d.ts.map +1 -1
  100. package/dist/utils/managerTypes.js +52 -7
  101. package/dist/utils/managerTypes.js.map +1 -1
  102. package/dist/utils/oracle.d.ts +3 -3
  103. package/dist/utils/oracle.d.ts.map +1 -1
  104. package/dist/utils/oracle.js +3 -4
  105. package/dist/utils/oracle.js.map +1 -1
  106. package/dist/utils/seeds.d.ts +1 -11
  107. package/dist/utils/seeds.d.ts.map +1 -1
  108. package/dist/utils/seeds.js +3 -13
  109. package/dist/utils/seeds.js.map +1 -1
  110. package/dist/utils/userMetadata.d.ts.map +1 -1
  111. package/dist/utils/userMetadata.js +7 -8
  112. package/dist/utils/userMetadata.js.map +1 -1
  113. package/package.json +2 -2
  114. package/src/classes/action.ts +261 -504
  115. package/src/classes/index.ts +0 -1
  116. package/src/classes/lut_utils.ts +63 -0
  117. package/src/classes/manager.ts +1 -30
  118. package/src/classes/market.ts +25 -34
  119. package/src/classes/obligation.ts +1 -7
  120. package/src/classes/vault.ts +23 -24
  121. package/src/client.ts +3 -8
  122. package/src/idl_codegen_kamino_vault/accounts/VaultState.ts +9 -17
  123. package/src/idl_codegen_kamino_vault/errors/custom.ts +0 -42
  124. package/src/idl_codegen_kamino_vault/instructions/giveUpPendingFees.ts +2 -2
  125. package/src/idl_codegen_kamino_vault/instructions/initVault.ts +0 -2
  126. package/src/idl_codegen_kamino_vault/instructions/initializeSharesMetadata.ts +2 -2
  127. package/src/idl_codegen_kamino_vault/instructions/updateReserveAllocation.ts +2 -2
  128. package/src/idl_codegen_kamino_vault/instructions/updateSharesMetadata.ts +2 -2
  129. package/src/idl_codegen_kamino_vault/instructions/updateVaultConfig.ts +2 -2
  130. package/src/idl_codegen_kamino_vault/instructions/withdrawPendingFees.ts +2 -2
  131. package/src/idl_codegen_kamino_vault/types/VaultConfigField.ts +0 -30
  132. package/src/idl_codegen_kamino_vault/types/index.ts +0 -2
  133. package/src/idl_kamino_vault.json +8 -35
  134. package/src/lending_operations/repay_with_collateral_calcs.ts +5 -14
  135. package/src/lending_operations/repay_with_collateral_operations.ts +29 -76
  136. package/src/lending_operations/swap_collateral_operations.ts +7 -19
  137. package/src/leverage/operations.ts +66 -114
  138. package/src/leverage/types.ts +4 -4
  139. package/src/utils/ObligationType.ts +1 -1
  140. package/src/utils/constants.ts +0 -1
  141. package/src/utils/lookupTable.ts +0 -62
  142. package/src/utils/managerTypes.ts +52 -10
  143. package/src/utils/oracle.ts +6 -7
  144. package/src/utils/seeds.ts +4 -14
  145. package/src/utils/userMetadata.ts +15 -16
@@ -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");
14
13
  const obligation_1 = require("./obligation");
15
14
  const types_1 = require("../idl_codegen/types");
16
15
  const farms_sdk_1 = require("@kamino-finance/farms-sdk");
17
16
  const ObligationType_1 = require("../utils/ObligationType");
18
17
  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,18 +180,27 @@ class KaminoAction {
180
180
  axn.addRequestElevationIx(elevationGroup, 'setup');
181
181
  return axn;
182
182
  }
183
- static async buildDepositTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
183
+ static async buildDepositTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, 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, 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, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }, 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
- await axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, undefined, overrideElevationGroupRequest);
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);
195
204
  if (useV2Ixs) {
196
205
  axn.addDepositIxV2();
197
206
  }
@@ -201,30 +210,54 @@ class KaminoAction {
201
210
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
202
211
  return axn;
203
212
  }
204
- async addScopeRefreshIxs(scope, tokens, feed = 'hubble') {
205
- this.setupIxsLabels.unshift(`refreshScopePrices`);
206
- this.setupIxs.unshift(await scope.refreshPriceListIx({
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({
207
238
  feed: feed,
208
239
  }, tokens));
209
240
  }
210
- static async buildBorrowTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
241
+ static async buildBorrowTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
211
242
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
212
243
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
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
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
214
245
  ) {
215
246
  const axn = await KaminoAction.initialize('borrow', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
216
247
  const addInitObligationForFarm = true;
217
248
  if (extraComputeBudget > 0) {
218
249
  axn.addComputeBudgetIxn(extraComputeBudget);
219
250
  }
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
- }
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);
226
259
  }
227
- await axn.addSupportIxs('borrow', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, undefined, overrideElevationGroupRequest);
260
+ await axn.addSupportIxs('borrow', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable, undefined, overrideElevationGroupRequest);
228
261
  if (useV2Ixs) {
229
262
  axn.addBorrowIxV2();
230
263
  }
@@ -234,42 +267,69 @@ class KaminoAction {
234
267
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
235
268
  return axn;
236
269
  }
237
- static async buildDepositReserveLiquidityTxns(kaminoMarket, amount, mint, owner, obligation, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
270
+ static async buildDepositReserveLiquidityTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
238
271
  includeAtaIxns = true, // if true it includes create and close wsol and token atas
239
- requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
272
+ requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
240
273
  const axn = await KaminoAction.initialize('mint', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
241
274
  const addInitObligationForFarm = true;
242
275
  if (extraComputeBudget > 0) {
243
276
  axn.addComputeBudgetIxn(extraComputeBudget);
244
277
  }
245
- await axn.addSupportIxs('mint', includeAtaIxns, requestElevationGroup, false, addInitObligationForFarm, false, scopeRefreshConfig, false);
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);
246
288
  axn.addDepositReserveLiquidityIx();
247
289
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
248
290
  return axn;
249
291
  }
250
- static async buildRedeemReserveCollateralTxns(kaminoMarket, amount, mint, owner, obligation, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
292
+ static async buildRedeemReserveCollateralTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
251
293
  includeAtaIxns = true, // if true it includes create and close wsol and token atas
252
- requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
294
+ requestElevationGroup = false, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
253
295
  const axn = await KaminoAction.initialize('redeem', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
254
296
  const addInitObligationForFarm = true;
255
297
  if (extraComputeBudget > 0) {
256
298
  axn.addComputeBudgetIxn(extraComputeBudget);
257
299
  }
258
- await axn.addSupportIxs('redeem', includeAtaIxns, requestElevationGroup, false, addInitObligationForFarm, false, scopeRefreshConfig, false);
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);
259
310
  axn.addRedeemReserveCollateralIx();
260
311
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
261
312
  return axn;
262
313
  }
263
- static async buildDepositObligationCollateralTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
314
+ static async buildDepositObligationCollateralTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
264
315
  includeAtaIxns = true, // if true it includes create and close wsol and token atas
265
316
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
266
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
317
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
267
318
  const axn = await KaminoAction.initialize('depositCollateral', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
268
319
  const addInitObligationForFarm = true;
269
320
  if (extraComputeBudget > 0) {
270
321
  axn.addComputeBudgetIxn(extraComputeBudget);
271
322
  }
272
- await axn.addSupportIxs('depositCollateral', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable);
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);
273
333
  if (useV2Ixs) {
274
334
  axn.addDepositObligationCollateralIxV2();
275
335
  }
@@ -279,10 +339,10 @@ class KaminoAction {
279
339
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
280
340
  return axn;
281
341
  }
282
- static async buildDepositAndBorrowTxns(kaminoMarket, depositAmount, depositMint, borrowAmount, borrowMint, payer, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
342
+ static async buildDepositAndBorrowTxns(kaminoMarket, depositAmount, depositMint, borrowAmount, borrowMint, payer, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
283
343
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
284
344
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
285
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
345
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
286
346
  const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'depositAndBorrow', depositAmount, depositMint, borrowMint, payer, payer, obligation, borrowAmount, referrer, currentSlot);
287
347
  const addInitObligationForFarmForDeposit = true;
288
348
  const addInitObligationForFarmForBorrow = false;
@@ -290,14 +350,17 @@ class KaminoAction {
290
350
  if (extraComputeBudget > 0) {
291
351
  axn.addComputeBudgetIxn(extraComputeBudget);
292
352
  }
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
- }
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);
299
362
  }
300
- await axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForDeposit, useV2Ixs, undefined, createLookupTable, twoTokenAction);
363
+ await axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForDeposit, useV2Ixs, createLookupTable, twoTokenAction);
301
364
  if (useV2Ixs) {
302
365
  await axn.addDepositAndBorrowIxV2();
303
366
  }
@@ -306,54 +369,12 @@ class KaminoAction {
306
369
  }
307
370
  await axn.addInBetweenIxs('depositAndBorrow', includeAtaIxns, requestElevationGroup, addInitObligationForFarmForBorrow, useV2Ixs);
308
371
  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
- }
321
372
  return axn;
322
373
  }
323
- static async buildDepositAndWithdrawV2Txns(kaminoMarket, depositAmount, depositMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
374
+ static async buildRepayAndWithdrawTxns(kaminoMarket, repayAmount, repayMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
324
375
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
325
376
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
326
- createLookupTable = true, referrer = web3_js_1.PublicKey.default) {
327
- const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'depositAndWithdraw', depositAmount, depositMint, withdrawMint, payer, payer, obligation, withdrawAmount, referrer, currentSlot);
328
- const addInitObligationForFarm = true;
329
- const twoTokenAction = true;
330
- if (extraComputeBudget > 0) {
331
- axn.addComputeBudgetIxn(extraComputeBudget);
332
- }
333
- await axn.addSupportIxs('depositAndWithdraw', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, true, scopeRefreshConfig, createLookupTable, twoTokenAction);
334
- const withdrawCollateralAmount = axn.getWithdrawCollateralAmount(axn.outflowReserve, axn.outflowAmount);
335
- axn.addDepositAndWithdrawV2Ixs(withdrawCollateralAmount);
336
- return axn;
337
- }
338
- static async buildRepayAndWithdrawV2Txns(kaminoMarket, repayAmount, repayMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
339
- includeAtaIxns = true, // if true it includes create and close wsol and token atas,
340
- requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
341
- createLookupTable = true, referrer = web3_js_1.PublicKey.default) {
342
- const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'repayAndWithdrawV2', repayAmount, repayMint, withdrawMint, payer, payer, obligation, withdrawAmount, referrer, currentSlot);
343
- const addInitObligationForFarm = true;
344
- const twoTokenAction = true;
345
- if (extraComputeBudget > 0) {
346
- axn.addComputeBudgetIxn(extraComputeBudget);
347
- }
348
- await axn.addSupportIxs('repayAndWithdrawV2', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, true, scopeRefreshConfig, createLookupTable, twoTokenAction);
349
- const withdrawCollateralAmount = axn.getWithdrawCollateralAmount(axn.outflowReserve, axn.outflowAmount);
350
- axn.addRepayAndWithdrawV2Ixs(withdrawCollateralAmount);
351
- return axn;
352
- }
353
- static async buildRepayAndWithdrawTxns(kaminoMarket, repayAmount, repayMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
354
- includeAtaIxns = true, // if true it includes create and close wsol and token atas,
355
- requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
356
- createLookupTable = true, referrer = web3_js_1.PublicKey.default) {
377
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
357
378
  const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'repayAndWithdraw', repayAmount, repayMint, withdrawMint, payer, payer, obligation, withdrawAmount, referrer, currentSlot);
358
379
  const addInitObligationForFarmForRepay = true;
359
380
  const addInitObligationForFarmForWithdraw = false;
@@ -361,7 +382,17 @@ class KaminoAction {
361
382
  if (extraComputeBudget > 0) {
362
383
  axn.addComputeBudgetIxn(extraComputeBudget);
363
384
  }
364
- await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForRepay, useV2Ixs, undefined, createLookupTable, twoTokenAction);
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);
365
396
  const withdrawCollateralAmount = axn.getWithdrawCollateralAmount(axn.outflowReserve, axn.outflowAmount);
366
397
  if (useV2Ixs) {
367
398
  await axn.addRepayAndWithdrawIxsV2(withdrawCollateralAmount);
@@ -371,25 +402,13 @@ class KaminoAction {
371
402
  }
372
403
  await axn.addInBetweenIxs('repayAndWithdraw', includeAtaIxns, requestElevationGroup, addInitObligationForFarmForWithdraw, useV2Ixs);
373
404
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
374
- // Create the scope refresh ixn in here to ensure it's the first ixn in the txn
375
- const allReserves = new utils_1.PublicKeySet([
376
- ...axn.depositReserves,
377
- ...axn.borrowReserves,
378
- axn.reserve.address,
379
- ...(axn.outflowReserve ? [axn.outflowReserve.address] : []),
380
- ...(axn.preLoadedDepositReservesSameTx ? axn.preLoadedDepositReservesSameTx : []),
381
- ]).toArray();
382
- const tokenIds = (0, market_1.getTokenIdsForScopeRefresh)(axn.kaminoMarket, allReserves);
383
- if (tokenIds.length > 0 && scopeRefreshConfig) {
384
- await axn.addScopeRefreshIxs(scopeRefreshConfig.scope, tokenIds, scopeRefreshConfig.scopeFeed);
385
- }
386
405
  return axn;
387
406
  }
388
- static async buildWithdrawTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
407
+ static async buildWithdrawTxns(kaminoMarket, amount, mint, owner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
389
408
  includeAtaIxns = true, // if true it includes create and close wsol and token atas,
390
409
  requestElevationGroup = false, // to be requested *after* the withdraw
391
410
  includeUserMetadata = true, // if true it includes user metadata
392
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, overrideElevationGroupRequest,
411
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, currentSlot = 0, scopeRefresh = undefined, overrideElevationGroupRequest,
393
412
  // Optional customizations which may be needed if the obligation was mutated by some previous ixn.
394
413
  obligationCustomizations) {
395
414
  const axn = await KaminoAction.initialize('withdraw', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
@@ -398,7 +417,16 @@ class KaminoAction {
398
417
  axn.addComputeBudgetIxn(extraComputeBudget);
399
418
  }
400
419
  axn.depositReserves.push(...(obligationCustomizations?.addedDepositReserves || []));
401
- await axn.addSupportIxs('withdraw', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, false, overrideElevationGroupRequest);
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);
402
430
  const collateralAmount = axn.getWithdrawCollateralAmount(axn.reserve, axn.amount);
403
431
  if (useV2Ixs) {
404
432
  await axn.addWithdrawIxV2(collateralAmount);
@@ -424,13 +452,22 @@ class KaminoAction {
424
452
  * @param includeUserMetadata - if true it includes user metadata
425
453
  * @param referrer
426
454
  */
427
- 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) {
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' }) {
428
456
  const axn = await KaminoAction.initialize('repay', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot, payer);
429
457
  const addInitObligationForFarm = true;
430
458
  if (extraComputeBudget > 0) {
431
459
  axn.addComputeBudgetIxn(extraComputeBudget);
432
460
  }
433
- await axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable);
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);
434
471
  if (useV2Ixs) {
435
472
  await axn.addRepayIxV2();
436
473
  }
@@ -440,16 +477,26 @@ class KaminoAction {
440
477
  axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
441
478
  return axn;
442
479
  }
443
- static async buildLiquidateTxns(kaminoMarket, amount, minCollateralReceiveAmount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, useV2Ixs, scopeRefreshConfig, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
480
+ static async buildLiquidateTxns(kaminoMarket, amount, minCollateralReceiveAmount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, useV2Ixs, extraComputeBudget = 1_000_000, // if > 0 then adds the ixn
444
481
  includeAtaIxns = true, // if true it includes create and close wsol and token atas, and creates all other token atas if they don't exist
445
482
  requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
446
- createLookupTable = true, referrer = web3_js_1.PublicKey.default, maxAllowedLtvOverridePercent = 0, currentSlot = 0) {
483
+ createLookupTable = true, referrer = web3_js_1.PublicKey.default, maxAllowedLtvOverridePercent = 0, currentSlot = 0, scopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }) {
447
484
  const axn = await KaminoAction.initializeMultiTokenAction(kaminoMarket, 'liquidate', amount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, minCollateralReceiveAmount, referrer, currentSlot);
448
485
  const addInitObligationForFarm = true;
449
486
  if (extraComputeBudget > 0) {
450
487
  axn.addComputeBudgetIxn(extraComputeBudget);
451
488
  }
452
- await axn.addSupportIxs('liquidate', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable);
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);
453
500
  if (useV2Ixs) {
454
501
  await axn.addLiquidateIxV2(maxAllowedLtvOverridePercent);
455
502
  }
@@ -463,13 +510,6 @@ class KaminoAction {
463
510
  const { axn, createAtaIxs } = await KaminoAction.initializeWithdrawReferrerFees(tokenMint, owner, kaminoMarket, currentSlot);
464
511
  axn.preTxnIxs.push(...createAtaIxs);
465
512
  axn.preTxnIxsLabels.push(`createAtasIxs[${axn.userTokenAccountAddress.toString()}]`);
466
- if (!axn.referrer.equals(web3_js_1.PublicKey.default)) {
467
- const referrerTokenState = (0, utils_1.referrerTokenStatePda)(axn.referrer, axn.reserve.address, axn.kaminoMarket.programId)[0];
468
- const account = await axn.kaminoMarket.getConnection().getAccountInfo(referrerTokenState);
469
- if (!account) {
470
- axn.addInitReferrerTokenStateIx(axn.reserve, referrerTokenState);
471
- }
472
- }
473
513
  axn.addRefreshReserveIxs([axn.reserve.address]);
474
514
  axn.addWithdrawReferrerFeesIxs();
475
515
  return axn;
@@ -595,11 +635,11 @@ class KaminoAction {
595
635
  addDepositIxV2() {
596
636
  const farmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
597
637
  ? {
598
- obligationFarmUserState: this.kaminoMarket.programId,
599
- reserveFarmState: this.kaminoMarket.programId,
638
+ obligationFarmUserState: lib_1.PROGRAM_ID,
639
+ reserveFarmState: lib_1.PROGRAM_ID,
600
640
  }
601
641
  : {
602
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmCollateral, this.getObligationPda()),
642
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
603
643
  reserveFarmState: this.reserve.state.farmCollateral,
604
644
  };
605
645
  this.lendingIxsLabels.push(`depositReserveLiquidityAndObligationCollateralV2`);
@@ -645,11 +685,11 @@ class KaminoAction {
645
685
  addDepositObligationCollateralIxV2() {
646
686
  const farmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
647
687
  ? {
648
- obligationFarmUserState: this.kaminoMarket.programId,
649
- reserveFarmState: this.kaminoMarket.programId,
688
+ obligationFarmUserState: lib_1.PROGRAM_ID,
689
+ reserveFarmState: lib_1.PROGRAM_ID,
650
690
  }
651
691
  : {
652
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmCollateral, this.getObligationPda()),
692
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
653
693
  reserveFarmState: this.reserve.state.farmCollateral,
654
694
  };
655
695
  this.lendingIxsLabels.push(`depositObligationCollateralV2`);
@@ -708,11 +748,11 @@ class KaminoAction {
708
748
  });
709
749
  const farmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
710
750
  ? {
711
- obligationFarmUserState: this.kaminoMarket.programId,
712
- reserveFarmState: this.kaminoMarket.programId,
751
+ obligationFarmUserState: lib_1.PROGRAM_ID,
752
+ reserveFarmState: lib_1.PROGRAM_ID,
713
753
  }
714
754
  : {
715
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
755
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
716
756
  reserveFarmState: this.reserve.state.farmDebt,
717
757
  };
718
758
  const borrowIx = (0, instructions_1.borrowObligationLiquidityV2)({
@@ -766,11 +806,11 @@ class KaminoAction {
766
806
  async addWithdrawIxV2(collateralAmount) {
767
807
  const farmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
768
808
  ? {
769
- obligationFarmUserState: this.kaminoMarket.programId,
770
- reserveFarmState: this.kaminoMarket.programId,
809
+ obligationFarmUserState: lib_1.PROGRAM_ID,
810
+ reserveFarmState: lib_1.PROGRAM_ID,
771
811
  }
772
812
  : {
773
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmCollateral, this.getObligationPda()),
813
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
774
814
  reserveFarmState: this.reserve.state.farmCollateral,
775
815
  };
776
816
  this.lendingIxsLabels.push(`withdrawObligationCollateralAndRedeemReserveCollateralV2`);
@@ -826,11 +866,11 @@ class KaminoAction {
826
866
  const depositReservesList = this.getAdditionalDepositReservesList();
827
867
  const farmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
828
868
  ? {
829
- obligationFarmUserState: this.kaminoMarket.programId,
830
- reserveFarmState: this.kaminoMarket.programId,
869
+ obligationFarmUserState: lib_1.PROGRAM_ID,
870
+ reserveFarmState: lib_1.PROGRAM_ID,
831
871
  }
832
872
  : {
833
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
873
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
834
874
  reserveFarmState: this.reserve.state.farmDebt,
835
875
  };
836
876
  const depositReserveAccountMetas = depositReservesList.map((reserve) => {
@@ -858,165 +898,6 @@ class KaminoAction {
858
898
  this.obligation.state.elevationGroup > 0 ? repayIx.keys.concat([...depositReserveAccountMetas]) : repayIx.keys;
859
899
  this.lendingIxs.push(repayIx);
860
900
  }
861
- addRepayAndWithdrawV2Ixs(withdrawCollateralAmount) {
862
- this.lendingIxsLabels.push(`repayAndWithdrawAndRedeem(repayReserve=${this.reserve.address})(withdrawReserve=${this.outflowReserve.address})(obligation=${this.getObligationPda()})`);
863
- const depositReservesList = this.getAdditionalDepositReservesList();
864
- const depositReserveAccountMetas = depositReservesList.map((reserve) => {
865
- return { pubkey: reserve, isSigner: false, isWritable: true };
866
- });
867
- const borrowReserveAccountMetas = this.borrowReserves.map((reserve) => {
868
- return { pubkey: reserve, isSigner: false, isWritable: true };
869
- });
870
- if (!this.outflowAmount) {
871
- throw new Error(`outflowAmount not set`);
872
- }
873
- if (!this.outflowReserve) {
874
- throw new Error(`outflowReserve not set`);
875
- }
876
- if (!this.additionalTokenAccountAddress) {
877
- throw new Error(`additionalTokenAccountAddress not set`);
878
- }
879
- const collateralFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
880
- ? {
881
- obligationFarmUserState: this.kaminoMarket.programId,
882
- reserveFarmState: this.kaminoMarket.programId,
883
- }
884
- : {
885
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmCollateral, this.getObligationPda()),
886
- reserveFarmState: this.outflowReserve.state.farmCollateral,
887
- };
888
- const debtFarmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
889
- ? {
890
- obligationFarmUserState: this.kaminoMarket.programId,
891
- reserveFarmState: this.kaminoMarket.programId,
892
- }
893
- : {
894
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
895
- reserveFarmState: this.reserve.state.farmDebt,
896
- };
897
- const repayAndWithdrawIx = (0, instructions_1.repayAndWithdrawAndRedeem)({
898
- repayAmount: this.amount,
899
- withdrawCollateralAmount,
900
- }, {
901
- repayAccounts: {
902
- owner: this.owner,
903
- obligation: this.getObligationPda(),
904
- lendingMarket: this.kaminoMarket.getAddress(),
905
- repayReserve: this.reserve.address,
906
- reserveLiquidityMint: this.reserve.getLiquidityMint(),
907
- userSourceLiquidity: this.userTokenAccountAddress,
908
- reserveDestinationLiquidity: this.reserve.state.liquidity.supplyVault,
909
- tokenProgram: this.reserve.getLiquidityTokenProgram(),
910
- instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
911
- },
912
- withdrawAccounts: {
913
- owner: this.owner,
914
- obligation: this.getObligationPda(),
915
- lendingMarket: this.kaminoMarket.getAddress(),
916
- lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
917
- withdrawReserve: this.outflowReserve.address,
918
- reserveLiquidityMint: this.outflowReserve.getLiquidityMint(),
919
- reserveCollateralMint: this.outflowReserve.getCTokenMint(),
920
- reserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
921
- reserveSourceCollateral: this.outflowReserve.state.collateral.supplyVault,
922
- userDestinationLiquidity: this.additionalTokenAccountAddress,
923
- placeholderUserDestinationCollateral: this.kaminoMarket.programId,
924
- collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
925
- liquidityTokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
926
- instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
927
- },
928
- collateralFarmsAccounts,
929
- debtFarmsAccounts,
930
- farmsProgram: farms_sdk_1.farmsId,
931
- }, this.kaminoMarket.programId);
932
- repayAndWithdrawIx.keys = repayAndWithdrawIx.keys.concat([
933
- ...depositReserveAccountMetas,
934
- ...borrowReserveAccountMetas,
935
- ]);
936
- this.lendingIxs.push(repayAndWithdrawIx);
937
- }
938
- addDepositAndWithdrawV2Ixs(withdrawCollateralAmount) {
939
- this.lendingIxsLabels.push(`depositAndWithdrawV2(depositReserve=${this.reserve.address})(withdrawReserve=${this.outflowReserve.address})(obligation=${this.getObligationPda()})`);
940
- const depositReservesList = this.getAdditionalDepositReservesList();
941
- const depositReserveAccountMetas = depositReservesList.map((reserve) => {
942
- return { pubkey: reserve, isSigner: false, isWritable: true };
943
- });
944
- const borrowReserveAccountMetas = this.borrowReserves.map((reserve) => {
945
- return { pubkey: reserve, isSigner: false, isWritable: true };
946
- });
947
- if (!this.outflowAmount) {
948
- throw new Error(`outflowAmount not set`);
949
- }
950
- if (!this.outflowReserve) {
951
- throw new Error(`outflowReserve not set`);
952
- }
953
- if (!this.additionalTokenAccountAddress) {
954
- throw new Error(`additionalTokenAccountAddress not set`);
955
- }
956
- const depositFarmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
957
- ? {
958
- obligationFarmUserState: this.kaminoMarket.programId,
959
- reserveFarmState: this.kaminoMarket.programId,
960
- }
961
- : {
962
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmCollateral, this.getObligationPda()),
963
- reserveFarmState: this.reserve.state.farmCollateral,
964
- };
965
- const withdrawFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
966
- ? {
967
- obligationFarmUserState: this.kaminoMarket.programId,
968
- reserveFarmState: this.kaminoMarket.programId,
969
- }
970
- : {
971
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmCollateral, this.getObligationPda()),
972
- reserveFarmState: this.outflowReserve.state.farmCollateral,
973
- };
974
- const depositAndWithdrawIx = (0, instructions_1.depositAndWithdraw)({
975
- liquidityAmount: this.amount,
976
- withdrawCollateralAmount,
977
- }, {
978
- depositAccounts: {
979
- owner: this.owner,
980
- obligation: this.getObligationPda(),
981
- lendingMarket: this.kaminoMarket.getAddress(),
982
- lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
983
- reserve: this.reserve.address,
984
- reserveLiquidityMint: this.reserve.getLiquidityMint(),
985
- reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
986
- reserveCollateralMint: this.reserve.getCTokenMint(),
987
- reserveDestinationDepositCollateral: this.reserve.state.collateral.supplyVault, // destinationCollateral
988
- userSourceLiquidity: this.userTokenAccountAddress,
989
- placeholderUserDestinationCollateral: this.kaminoMarket.programId,
990
- collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
991
- liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
992
- instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
993
- },
994
- withdrawAccounts: {
995
- owner: this.owner,
996
- obligation: this.getObligationPda(),
997
- lendingMarket: this.kaminoMarket.getAddress(),
998
- lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
999
- withdrawReserve: this.outflowReserve.address,
1000
- reserveLiquidityMint: this.outflowReserve.getLiquidityMint(),
1001
- reserveCollateralMint: this.outflowReserve.getCTokenMint(),
1002
- reserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
1003
- reserveSourceCollateral: this.outflowReserve.state.collateral.supplyVault,
1004
- userDestinationLiquidity: this.additionalTokenAccountAddress,
1005
- placeholderUserDestinationCollateral: this.kaminoMarket.programId,
1006
- collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1007
- liquidityTokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
1008
- instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
1009
- },
1010
- depositFarmsAccounts,
1011
- withdrawFarmsAccounts,
1012
- farmsProgram: farms_sdk_1.farmsId,
1013
- }, this.kaminoMarket.programId);
1014
- depositAndWithdrawIx.keys = depositAndWithdrawIx.keys.concat([
1015
- ...depositReserveAccountMetas,
1016
- ...borrowReserveAccountMetas,
1017
- ]);
1018
- this.lendingIxs.push(depositAndWithdrawIx);
1019
- }
1020
901
  async addDepositAndBorrowIx() {
1021
902
  this.lendingIxsLabels.push(`depositReserveLiquidityAndObligationCollateral`);
1022
903
  this.lendingIxsLabels.push(`borrowObligationLiquidity`);
@@ -1076,11 +957,11 @@ class KaminoAction {
1076
957
  async addDepositAndBorrowIxV2() {
1077
958
  const collateralFarmsAccounts = this.reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
1078
959
  ? {
1079
- obligationFarmUserState: this.kaminoMarket.programId,
1080
- reserveFarmState: this.kaminoMarket.programId,
960
+ obligationFarmUserState: lib_1.PROGRAM_ID,
961
+ reserveFarmState: lib_1.PROGRAM_ID,
1081
962
  }
1082
963
  : {
1083
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmCollateral, this.getObligationPda()),
964
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmCollateral)[0],
1084
965
  reserveFarmState: this.reserve.state.farmCollateral,
1085
966
  };
1086
967
  this.lendingIxsLabels.push(`depositReserveLiquidityAndObligationCollateralV2`);
@@ -1125,11 +1006,11 @@ class KaminoAction {
1125
1006
  });
1126
1007
  const debtFarmsAccounts = this.outflowReserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
1127
1008
  ? {
1128
- obligationFarmUserState: this.kaminoMarket.programId,
1129
- reserveFarmState: this.kaminoMarket.programId,
1009
+ obligationFarmUserState: lib_1.PROGRAM_ID,
1010
+ reserveFarmState: lib_1.PROGRAM_ID,
1130
1011
  }
1131
1012
  : {
1132
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmDebt, this.getObligationPda()),
1013
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.outflowReserve.state.farmDebt)[0],
1133
1014
  reserveFarmState: this.outflowReserve.state.farmDebt,
1134
1015
  };
1135
1016
  const borrowIx = (0, instructions_1.borrowObligationLiquidityV2)({
@@ -1214,11 +1095,11 @@ class KaminoAction {
1214
1095
  });
1215
1096
  const debtFarmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
1216
1097
  ? {
1217
- obligationFarmUserState: this.kaminoMarket.programId,
1218
- reserveFarmState: this.kaminoMarket.programId,
1098
+ obligationFarmUserState: lib_1.PROGRAM_ID,
1099
+ reserveFarmState: lib_1.PROGRAM_ID,
1219
1100
  }
1220
1101
  : {
1221
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
1102
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
1222
1103
  reserveFarmState: this.reserve.state.farmDebt,
1223
1104
  };
1224
1105
  const repayIx = (0, instructions_1.repayObligationLiquidityV2)({
@@ -1252,11 +1133,11 @@ class KaminoAction {
1252
1133
  }
1253
1134
  const collateralFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
1254
1135
  ? {
1255
- obligationFarmUserState: this.kaminoMarket.programId,
1256
- reserveFarmState: this.kaminoMarket.programId,
1136
+ obligationFarmUserState: lib_1.PROGRAM_ID,
1137
+ reserveFarmState: lib_1.PROGRAM_ID,
1257
1138
  }
1258
1139
  : {
1259
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmCollateral, this.getObligationPda()),
1140
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.outflowReserve.state.farmCollateral)[0],
1260
1141
  reserveFarmState: this.outflowReserve.state.farmCollateral,
1261
1142
  };
1262
1143
  this.lendingIxs.push((0, instructions_1.withdrawObligationCollateralAndRedeemReserveCollateralV2)({
@@ -1282,7 +1163,7 @@ class KaminoAction {
1282
1163
  farmsProgram: farms_sdk_1.farmsId,
1283
1164
  }, this.kaminoMarket.programId));
1284
1165
  }
1285
- addLiquidateIx(maxAllowedLtvOverridePercent = 0) {
1166
+ async addLiquidateIx(maxAllowedLtvOverridePercent = 0) {
1286
1167
  this.lendingIxsLabels.push(`liquidateObligationAndRedeemReserveCollateral`);
1287
1168
  if (!this.outflowReserve) {
1288
1169
  throw Error(`Withdraw reserve during liquidation is not defined`);
@@ -1341,20 +1222,20 @@ class KaminoAction {
1341
1222
  });
1342
1223
  const collateralFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)
1343
1224
  ? {
1344
- obligationFarmUserState: this.kaminoMarket.programId,
1345
- reserveFarmState: this.kaminoMarket.programId,
1225
+ obligationFarmUserState: lib_1.PROGRAM_ID,
1226
+ reserveFarmState: lib_1.PROGRAM_ID,
1346
1227
  }
1347
1228
  : {
1348
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.outflowReserve.state.farmCollateral, this.getObligationPda()),
1229
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.outflowReserve.state.farmCollateral)[0],
1349
1230
  reserveFarmState: this.outflowReserve.state.farmCollateral,
1350
1231
  };
1351
1232
  const debtFarmsAccounts = this.reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)
1352
1233
  ? {
1353
- obligationFarmUserState: this.kaminoMarket.programId,
1354
- reserveFarmState: this.kaminoMarket.programId,
1234
+ obligationFarmUserState: lib_1.PROGRAM_ID,
1235
+ reserveFarmState: lib_1.PROGRAM_ID,
1355
1236
  }
1356
1237
  : {
1357
- obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.reserve.state.farmDebt, this.getObligationPda()),
1238
+ obligationFarmUserState: (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), this.reserve.state.farmDebt)[0],
1358
1239
  reserveFarmState: this.reserve.state.farmDebt,
1359
1240
  };
1360
1241
  const liquidateIx = (0, instructions_1.liquidateObligationAndRedeemReserveCollateralV2)({
@@ -1422,9 +1303,7 @@ class KaminoAction {
1422
1303
  'repay',
1423
1304
  'depositAndBorrow',
1424
1305
  'repayAndWithdraw',
1425
- 'repayAndWithdrawV2',
1426
1306
  'refreshObligation',
1427
- 'depositAndWithdraw',
1428
1307
  ].includes(action)) {
1429
1308
  // The support ixns in order are:
1430
1309
  // 0. Init obligation ixn
@@ -1439,14 +1318,11 @@ class KaminoAction {
1439
1318
  // 7. Ixn to refresh the `debt` farm of the obligation
1440
1319
  // 8. Ixn to refresh the `collateral` farm of the obligation
1441
1320
  let currentReserves = [];
1442
- if (action === 'liquidate' ||
1443
- action === 'depositAndBorrow' ||
1444
- action === 'repayAndWithdraw' ||
1445
- action === 'repayAndWithdrawV2') {
1321
+ if (action === 'liquidate' || action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
1446
1322
  if (!this.outflowReserve) {
1447
1323
  throw new Error('outflowReserve is undefined');
1448
1324
  }
1449
- if (action === 'depositAndBorrow' || action === 'repayAndWithdraw' || action === 'repayAndWithdrawV2') {
1325
+ if (action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
1450
1326
  currentReserves = [this.reserve, this.outflowReserve];
1451
1327
  if (action === 'depositAndBorrow') {
1452
1328
  if (this.obligation) {
@@ -1484,17 +1360,13 @@ class KaminoAction {
1484
1360
  else if (action === 'depositAndBorrow' ||
1485
1361
  action === 'depositCollateral' ||
1486
1362
  action === 'withdraw' ||
1487
- action === 'deposit' ||
1488
- action === 'depositAndWithdraw') {
1363
+ action === 'deposit') {
1489
1364
  await this.addInitObligationForFarm(this.reserve, types_1.ReserveFarmKind.Collateral, addAsSupportIx);
1490
- if (this.outflowReserve && action !== 'depositAndWithdraw') {
1365
+ if (this.outflowReserve) {
1491
1366
  await this.addInitObligationForFarm(this.outflowReserve, types_1.ReserveFarmKind.Debt, addAsSupportIx);
1492
1367
  }
1493
1368
  }
1494
- else if (action === 'repayAndWithdraw' ||
1495
- action === 'borrow' ||
1496
- action === 'repay' ||
1497
- action === 'repayAndWithdrawV2') {
1369
+ else if (action === 'repayAndWithdraw' || action === 'borrow' || action === 'repay') {
1498
1370
  // todo - probably don't need to add both debt and collateral for everything here
1499
1371
  await this.addInitObligationForFarm(this.reserve, types_1.ReserveFarmKind.Debt, addAsSupportIx);
1500
1372
  if (this.outflowReserve) {
@@ -1525,7 +1397,7 @@ class KaminoAction {
1525
1397
  this.addRefreshObligationIx(addAsSupportIx);
1526
1398
  }
1527
1399
  if (requestElevationGroup) {
1528
- if (action === 'repay' || action === 'repayAndWithdrawV2') {
1400
+ if (action === 'repay') {
1529
1401
  const repayObligationLiquidity = this.obligation.borrows.get(this.reserve.address);
1530
1402
  if (!repayObligationLiquidity) {
1531
1403
  throw new Error(`Could not find debt reserve ${this.reserve.address} in obligation`);
@@ -1649,7 +1521,7 @@ class KaminoAction {
1649
1521
  }
1650
1522
  }
1651
1523
  }
1652
- async addSupportIxs(action, includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, scopeRefreshConfig, createLookupTable, twoTokenAction = false, overrideElevationGroupRequest) {
1524
+ async addSupportIxs(action, includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, useV2Ixs, createLookupTable, twoTokenAction = false, overrideElevationGroupRequest) {
1653
1525
  if (!['mint', 'redeem'].includes(action)) {
1654
1526
  const [, ownerUserMetadata] = await this.kaminoMarket.getUserMetadata(this.owner);
1655
1527
  if (!ownerUserMetadata && includeUserMetadata) {
@@ -1662,20 +1534,16 @@ class KaminoAction {
1662
1534
  }
1663
1535
  this.addInitUserMetadataIxs(lookupTable);
1664
1536
  }
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
+ }
1665
1544
  await this.addInitObligationIxs();
1666
1545
  }
1667
1546
  await this.addSupportIxsWithoutInitObligation(action, includeAtaIxns, useV2Ixs, 'setup', requestElevationGroup, addInitObligationForFarm, twoTokenAction, overrideElevationGroupRequest);
1668
- const allReserves = new utils_1.PublicKeySet([
1669
- ...this.depositReserves,
1670
- ...this.borrowReserves,
1671
- this.reserve.address,
1672
- ...(this.outflowReserve ? [this.outflowReserve.address] : []),
1673
- ...(this.preLoadedDepositReservesSameTx ? this.preLoadedDepositReservesSameTx : []),
1674
- ]).toArray();
1675
- const tokenIds = (0, market_1.getTokenIdsForScopeRefresh)(this.kaminoMarket, allReserves);
1676
- if (tokenIds.length > 0 && scopeRefreshConfig) {
1677
- await this.addScopeRefreshIxs(scopeRefreshConfig.scope, tokenIds, scopeRefreshConfig.scopeFeed);
1678
- }
1679
1547
  }
1680
1548
  static optionalAccount(pubkey, programId = lib_1.PROGRAM_ID) {
1681
1549
  if ((0, utils_1.isNotNullPubkey)(pubkey)) {
@@ -1829,7 +1697,7 @@ class KaminoAction {
1829
1697
  farms.push([
1830
1698
  types_1.ReserveFarmKind.Collateral,
1831
1699
  kaminoReserve.state.farmCollateral,
1832
- (0, utils_1.obligationFarmStatePda)(kaminoReserve.state.farmCollateral, this.getObligationPda()),
1700
+ (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), kaminoReserve.state.farmCollateral)[0],
1833
1701
  kaminoReserve,
1834
1702
  ]);
1835
1703
  }
@@ -1837,7 +1705,7 @@ class KaminoAction {
1837
1705
  farms.push([
1838
1706
  types_1.ReserveFarmKind.Debt,
1839
1707
  kaminoReserve.state.farmDebt,
1840
- (0, utils_1.obligationFarmStatePda)(kaminoReserve.state.farmDebt, this.getObligationPda()),
1708
+ (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), kaminoReserve.state.farmDebt)[0],
1841
1709
  kaminoReserve,
1842
1710
  ]);
1843
1711
  }
@@ -1887,17 +1755,17 @@ class KaminoAction {
1887
1755
  async addInitObligationForFarm(reserve, mode, addAsSupportIx = 'setup') {
1888
1756
  const farms = [];
1889
1757
  if (mode === types_1.ReserveFarmKind.Collateral && (0, utils_1.isNotNullPubkey)(reserve.state.farmCollateral)) {
1890
- const pda = (0, utils_1.obligationFarmStatePda)(reserve.state.farmCollateral, this.getObligationPda());
1891
- const account = await this.kaminoMarket.getConnection().getAccountInfo(pda);
1758
+ const userPda = (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), reserve.state.farmCollateral)[0];
1759
+ const account = await this.kaminoMarket.getConnection().getAccountInfo(userPda);
1892
1760
  if (!account) {
1893
- farms.push([types_1.ReserveFarmKind.Collateral.discriminator, reserve.state.farmCollateral, pda]);
1761
+ farms.push([types_1.ReserveFarmKind.Collateral.discriminator, reserve.state.farmCollateral, userPda]);
1894
1762
  }
1895
1763
  }
1896
1764
  if (mode === types_1.ReserveFarmKind.Debt && (0, utils_1.isNotNullPubkey)(reserve.state.farmDebt)) {
1897
- const pda = (0, utils_1.obligationFarmStatePda)(reserve.state.farmDebt, this.getObligationPda());
1898
- const account = await this.kaminoMarket.getConnection().getAccountInfo(pda);
1765
+ const userPda = (0, utils_1.obligationFarmStatePda)(this.getObligationPda(), reserve.state.farmDebt)[0];
1766
+ const account = await this.kaminoMarket.getConnection().getAccountInfo(userPda);
1899
1767
  if (!account) {
1900
- farms.push([types_1.ReserveFarmKind.Debt.discriminator, reserve.state.farmDebt, pda]);
1768
+ farms.push([types_1.ReserveFarmKind.Debt.discriminator, reserve.state.farmDebt, userPda]);
1901
1769
  }
1902
1770
  }
1903
1771
  farms.forEach((arg) => {
@@ -1926,7 +1794,7 @@ class KaminoAction {
1926
1794
  }
1927
1795
  });
1928
1796
  }
1929
- addInitObligationIxs() {
1797
+ async addInitObligationIxs() {
1930
1798
  if (!this.obligation) {
1931
1799
  const obligationPda = this.getObligationPda();
1932
1800
  const [userMetadataAddress, _bump] = (0, utils_1.userMetadataPda)(this.owner, this.kaminoMarket.programId);
@@ -1968,18 +1836,25 @@ class KaminoAction {
1968
1836
  this.setupIxs.push(initUserMetadataIx);
1969
1837
  this.setupIxsLabels.push(`initUserMetadata[${userMetadataAddress.toString()}]`);
1970
1838
  }
1971
- addInitReferrerTokenStateIx(reserve, referrerTokenState) {
1972
- const initReferrerTokenStateIx = (0, instructions_1.initReferrerTokenState)({
1973
- lendingMarket: this.kaminoMarket.getAddress(),
1974
- payer: this.owner,
1975
- reserve: reserve.address,
1976
- referrer: this.referrer,
1977
- referrerTokenState,
1978
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
1979
- systemProgram: web3_js_1.SystemProgram.programId,
1980
- }, this.kaminoMarket.programId);
1981
- this.setupIxs.unshift(initReferrerTokenStateIx);
1982
- this.setupIxsLabels.unshift(`InitReferrerTokenState[${referrerTokenState.toString()} res=${reserve.address}]`);
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
+ }
1983
1858
  }
1984
1859
  addWithdrawReferrerFeesIxs() {
1985
1860
  const referrerTokenStateAddress = (0, utils_1.referrerTokenStatePda)(this.owner, this.reserve.address, this.kaminoMarket.programId)[0];
@@ -2061,13 +1936,14 @@ class KaminoAction {
2061
1936
  return;
2062
1937
  }
2063
1938
  let safeRepay = new bn_js_1.default(this.amount);
2064
- if (this.obligation && (action === 'repay' || action === 'repayAndWithdrawV2') && this.amount.eq(new bn_js_1.default(utils_1.U64_MAX))) {
1939
+ if (this.obligation && action === 'repay' && this.amount.eq(new bn_js_1.default(utils_1.U64_MAX))) {
2065
1940
  const borrow = this.obligation.state.borrows.find((borrow) => borrow.borrowReserve.toString() === this.reserve.address.toString());
2066
1941
  if (!borrow) {
2067
1942
  throw Error(`Unable to find obligation borrow to repay for ${this.obligation.state.owner.toBase58()}`);
2068
1943
  }
2069
1944
  const cumulativeBorrowRateObligation = obligation_1.KaminoObligation.getCumulativeBorrowRate(borrow);
2070
1945
  const cumulativeBorrowRateReserve = this.reserve.getEstimatedCumulativeBorrowRate(this.currentSlot, this.kaminoMarket.state.referralFeeBps);
1946
+ // TODO: shouldn't this calc be added to all other stuff as well?
2071
1947
  safeRepay = new bn_js_1.default(Math.floor(obligation_1.KaminoObligation.getBorrowAmount(borrow)
2072
1948
  .mul(cumulativeBorrowRateReserve)
2073
1949
  .div(cumulativeBorrowRateObligation)
@@ -2086,7 +1962,6 @@ class KaminoAction {
2086
1962
  // Add rent exemption lamports for WSOL accounts that need to be pre-funded for inflow/send transactions
2087
1963
  const sendAction = action === 'deposit' ||
2088
1964
  action === 'repay' ||
2089
- action === 'repayAndWithdrawV2' ||
2090
1965
  action === 'mint' ||
2091
1966
  (action === 'liquidate' && this.secondaryMint?.equals(spl_token_1.NATIVE_MINT)); // only sync WSOL amount if liquidator repays SOL which is secondaryMint
2092
1967
  const transferLamportsIx = web3_js_1.SystemProgram.transfer({
@@ -2151,7 +2026,7 @@ class KaminoAction {
2151
2026
  primaryMint = inflowTokenMint;
2152
2027
  secondaryMint = outflowTokenMint;
2153
2028
  }
2154
- else if (action === 'repayAndWithdraw' || action === 'repayAndWithdrawV2' || action === 'depositAndWithdraw') {
2029
+ else if (action === 'repayAndWithdraw') {
2155
2030
  primaryMint = inflowTokenMint;
2156
2031
  secondaryMint = outflowTokenMint;
2157
2032
  userTokenAccountAddress = userInflowTokenAccountAddress;