@metamask-previews/earn-controller 0.14.0-preview-2e9de6b → 0.14.0-preview-7bb49dcb

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.
@@ -1,18 +1,18 @@
1
1
  "use strict";
2
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
- };
7
2
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
8
3
  if (kind === "m") throw new TypeError("Private method is not writable");
9
4
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
10
5
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11
6
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
7
  };
13
- var _EarnController_instances, _EarnController_stakeSDK, _EarnController_selectedNetworkClientId, _EarnController_stakingApiService, _EarnController_initializeSDK, _EarnController_getCurrentAccount, _EarnController_getCurrentChainId;
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _EarnController_instances, _EarnController_earnSDK, _EarnController_selectedNetworkClientId, _EarnController_earnApiService, _EarnController_addTransactionFn, _EarnController_supportedPooledStakingChains, _EarnController_initializeSDK, _EarnController_getCurrentAccount, _EarnController_getCurrentChainId;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.EarnController = exports.getDefaultEarnControllerState = exports.controllerName = void 0;
15
+ exports.EarnController = exports.getDefaultEarnControllerState = exports.DEFAULT_POOLED_STAKING_CHAIN_STATE = exports.DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES = exports.DEFAULT_LENDING_POSITION = exports.DEFAULT_LENDING_MARKET = exports.controllerName = void 0;
16
16
  const providers_1 = require("@ethersproject/providers");
17
17
  const base_controller_1 = require("@metamask/base-controller");
18
18
  const controller_utils_1 = require("@metamask/controller-utils");
@@ -24,6 +24,10 @@ const stakingTransactionTypes = new Set([
24
24
  transaction_controller_1.TransactionType.stakingUnstake,
25
25
  transaction_controller_1.TransactionType.stakingClaim,
26
26
  ]);
27
+ const lendingTransactionTypes = new Set([
28
+ transaction_controller_1.TransactionType.lendingDeposit,
29
+ 'lendingWithdraw',
30
+ ]);
27
31
  /**
28
32
  * Metadata for the EarnController.
29
33
  */
@@ -32,7 +36,7 @@ const earnControllerMetadata = {
32
36
  persist: true,
33
37
  anonymous: false,
34
38
  },
35
- stablecoin_lending: {
39
+ lending: {
36
40
  persist: true,
37
41
  anonymous: false,
38
42
  },
@@ -42,17 +46,42 @@ const earnControllerMetadata = {
42
46
  },
43
47
  };
44
48
  // === Default State ===
45
- const DEFAULT_STABLECOIN_VAULT = {
46
- symbol: '',
49
+ exports.DEFAULT_LENDING_MARKET = {
50
+ id: '',
51
+ chainId: 0,
52
+ protocol: '',
47
53
  name: '',
54
+ address: '',
55
+ tvlUnderlying: '0',
56
+ netSupplyRate: 0,
57
+ totalSupplyRate: 0,
58
+ underlying: {
59
+ address: '',
60
+ chainId: 0,
61
+ },
62
+ outputToken: {
63
+ address: '',
64
+ chainId: 0,
65
+ },
66
+ rewards: [
67
+ {
68
+ token: {
69
+ address: '',
70
+ chainId: 0,
71
+ },
72
+ rate: 0,
73
+ },
74
+ ],
75
+ };
76
+ exports.DEFAULT_LENDING_POSITION = {
77
+ id: '',
48
78
  chainId: 0,
49
- tokenAddress: '',
50
- vaultAddress: '',
51
- currentAPY: '0',
52
- supply: '0',
53
- liquidity: '0',
79
+ assets: '0',
80
+ marketId: '',
81
+ marketAddress: '',
82
+ protocol: '',
54
83
  };
55
- const DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES = {
84
+ exports.DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES = {
56
85
  oneDay: '0',
57
86
  oneWeek: '0',
58
87
  oneMonth: '0',
@@ -60,6 +89,24 @@ const DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES = {
60
89
  sixMonths: '0',
61
90
  oneYear: '0',
62
91
  };
92
+ exports.DEFAULT_POOLED_STAKING_CHAIN_STATE = {
93
+ pooledStakes: {
94
+ account: '',
95
+ lifetimeRewards: '0',
96
+ assets: '0',
97
+ exitRequests: [],
98
+ },
99
+ exchangeRate: '1',
100
+ vaultMetadata: {
101
+ apy: '0',
102
+ capacity: '0',
103
+ feePercent: 0,
104
+ totalAssets: '0',
105
+ vaultAddress: '0x0000000000000000000000000000000000000000',
106
+ },
107
+ vaultDailyApys: [],
108
+ vaultApyAverages: exports.DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES,
109
+ };
63
110
  /**
64
111
  * Gets the default state for the EarnController.
65
112
  *
@@ -68,26 +115,12 @@ const DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES = {
68
115
  function getDefaultEarnControllerState() {
69
116
  return {
70
117
  pooled_staking: {
71
- pooledStakes: {
72
- account: '',
73
- lifetimeRewards: '0',
74
- assets: '0',
75
- exitRequests: [],
76
- },
77
- exchangeRate: '1',
78
- vaultMetadata: {
79
- apy: '0',
80
- capacity: '0',
81
- feePercent: 0,
82
- totalAssets: '0',
83
- vaultAddress: '0x0000000000000000000000000000000000000000',
84
- },
85
- vaultDailyApys: [],
86
- vaultApyAverages: DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES,
87
118
  isEligible: false,
88
119
  },
89
- stablecoin_lending: {
90
- vaults: [DEFAULT_STABLECOIN_VAULT],
120
+ lending: {
121
+ markets: [exports.DEFAULT_LENDING_MARKET],
122
+ positions: [exports.DEFAULT_LENDING_POSITION],
123
+ isEligible: false,
91
124
  },
92
125
  lastUpdated: 0,
93
126
  };
@@ -98,7 +131,7 @@ exports.getDefaultEarnControllerState = getDefaultEarnControllerState;
98
131
  * EarnController manages DeFi earning opportunities across different protocols and chains.
99
132
  */
100
133
  class EarnController extends base_controller_1.BaseController {
101
- constructor({ messenger, state = {}, }) {
134
+ constructor({ messenger, state = {}, addTransactionFn, }) {
102
135
  super({
103
136
  name: exports.controllerName,
104
137
  metadata: earnControllerMetadata,
@@ -109,22 +142,37 @@ class EarnController extends base_controller_1.BaseController {
109
142
  },
110
143
  });
111
144
  _EarnController_instances.add(this);
112
- _EarnController_stakeSDK.set(this, null);
145
+ _EarnController_earnSDK.set(this, null);
113
146
  _EarnController_selectedNetworkClientId.set(this, void 0);
114
- _EarnController_stakingApiService.set(this, new stake_sdk_1.StakingApiService());
115
- __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this);
147
+ _EarnController_earnApiService.set(this, new stake_sdk_1.EarnApiService());
148
+ _EarnController_addTransactionFn.set(this, void 0);
149
+ _EarnController_supportedPooledStakingChains.set(this, void 0);
150
+ // temporary array of supported chains
151
+ // TODO: remove this once we have a more permanent solution
152
+ // from sdk or api to get lending and pooled staking chains
153
+ __classPrivateFieldSet(this, _EarnController_supportedPooledStakingChains, [1, 560048], "f");
154
+ __classPrivateFieldSet(this, _EarnController_addTransactionFn, addTransactionFn, "f");
155
+ __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this).catch(console.error);
116
156
  this.refreshPooledStakingData().catch(console.error);
157
+ this.refreshLendingData().catch(console.error);
117
158
  const { selectedNetworkClientId } = this.messagingSystem.call('NetworkController:getState');
118
159
  __classPrivateFieldSet(this, _EarnController_selectedNetworkClientId, selectedNetworkClientId, "f");
119
160
  // Listen for network changes
120
161
  this.messagingSystem.subscribe('NetworkController:stateChange', (networkControllerState) => {
121
162
  if (networkControllerState.selectedNetworkClientId !==
122
163
  __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
123
- __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this, networkControllerState.selectedNetworkClientId);
124
- this.refreshPooledStakingVaultMetadata().catch(console.error);
125
- this.refreshPooledStakingVaultDailyApys().catch(console.error);
126
- this.refreshPooledStakingVaultApyAverages().catch(console.error);
127
- this.refreshPooledStakes().catch(console.error);
164
+ const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this, networkControllerState.selectedNetworkClientId);
165
+ __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this, networkControllerState.selectedNetworkClientId).catch(console.error);
166
+ if (__classPrivateFieldGet(this, _EarnController_supportedPooledStakingChains, "f").includes(chainId)) {
167
+ // only refresh pool staking data for the chain we are switching to
168
+ this.refreshPooledStakingVaultMetadata(chainId).catch(console.error);
169
+ this.refreshPooledStakingVaultDailyApys(chainId).catch(console.error);
170
+ this.refreshPooledStakingVaultApyAverages(chainId).catch(console.error);
171
+ this.refreshPooledStakes({ chainId }).catch(console.error);
172
+ }
173
+ // refresh lending data for all chains
174
+ this.refreshLendingMarkets().catch(console.error);
175
+ this.refreshLendingPositions().catch(console.error);
128
176
  }
129
177
  __classPrivateFieldSet(this, _EarnController_selectedNetworkClientId, networkControllerState.selectedNetworkClientId, "f");
130
178
  });
@@ -136,8 +184,11 @@ class EarnController extends base_controller_1.BaseController {
136
184
  * Until this has been fixed, we rely on the event payload for the latest account instead of #getCurrentAccount().
137
185
  * Issue: https://github.com/MetaMask/accounts-planning/issues/887
138
186
  */
187
+ // TODO: temp solution, this will refresh lending eligibility also
188
+ // we could have a more general check, as what is happening is a compliance address check
139
189
  this.refreshStakingEligibility({ address }).catch(console.error);
140
190
  this.refreshPooledStakes({ address }).catch(console.error);
191
+ this.refreshLendingPositions({ address }).catch(console.error);
141
192
  });
142
193
  // Listen for confirmed staking transactions
143
194
  this.messagingSystem.subscribe('TransactionController:transactionConfirmed', (transactionMeta) => {
@@ -149,10 +200,15 @@ class EarnController extends base_controller_1.BaseController {
149
200
  const { type, originalType } = transactionMeta;
150
201
  const isStakingTransaction = stakingTransactionTypes.has(type) ||
151
202
  stakingTransactionTypes.has(originalType);
203
+ const isLendingTransaction = lendingTransactionTypes.has(type) ||
204
+ lendingTransactionTypes.has(originalType);
205
+ const sender = transactionMeta.txParams.from;
152
206
  if (isStakingTransaction) {
153
- const sender = transactionMeta.txParams.from;
154
207
  this.refreshPooledStakes({ resetCache: true, address: sender }).catch(console.error);
155
208
  }
209
+ if (isLendingTransaction) {
210
+ this.refreshLendingPositions({ address: sender }).catch(console.error);
211
+ }
156
212
  });
157
213
  }
158
214
  /**
@@ -163,18 +219,24 @@ class EarnController extends base_controller_1.BaseController {
163
219
  * @param options - Optional arguments
164
220
  * @param [options.resetCache] - Control whether the BE cache should be invalidated (optional).
165
221
  * @param [options.address] - The address to refresh pooled stakes for (optional).
222
+ * @param [options.chainId] - The chain id to refresh pooled stakes for (optional).
166
223
  * @returns A promise that resolves when the stakes data has been updated
167
224
  */
168
- async refreshPooledStakes({ resetCache = false, address, } = {}) {
225
+ async refreshPooledStakes({ resetCache = false, address, chainId, } = {}) {
169
226
  const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
170
227
  if (!addressToUse) {
171
228
  return;
172
229
  }
173
- const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
174
- const { accounts, exchangeRate } = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getPooledStakes([addressToUse], chainId, resetCache);
230
+ const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
231
+ const { accounts, exchangeRate } = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getPooledStakes([addressToUse], chainIdToUse, resetCache);
175
232
  this.update((state) => {
176
- state.pooled_staking.pooledStakes = accounts[0];
177
- state.pooled_staking.exchangeRate = exchangeRate;
233
+ const chainState = state.pooled_staking[chainIdToUse] ??
234
+ exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
235
+ state.pooled_staking[chainIdToUse] = {
236
+ ...chainState,
237
+ pooledStakes: accounts[0],
238
+ exchangeRate,
239
+ };
178
240
  });
179
241
  }
180
242
  /**
@@ -190,11 +252,12 @@ class EarnController extends base_controller_1.BaseController {
190
252
  if (!addressToCheck) {
191
253
  return;
192
254
  }
193
- const { eligible: isEligible } = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getPooledStakingEligibility([
255
+ const { eligible: isEligible } = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getPooledStakingEligibility([
194
256
  addressToCheck,
195
257
  ]);
196
258
  this.update((state) => {
197
259
  state.pooled_staking.isEligible = isEligible;
260
+ state.lending.isEligible = isEligible;
198
261
  });
199
262
  }
200
263
  /**
@@ -202,41 +265,59 @@ class EarnController extends base_controller_1.BaseController {
202
265
  * Updates the vault metadata in the controller state including APY, capacity,
203
266
  * fee percentage, total assets, and vault address.
204
267
  *
268
+ * @param chainId - The chain id to refresh pooled staking vault metadata for (optional).
205
269
  * @returns A promise that resolves when the vault metadata has been updated
206
270
  */
207
- async refreshPooledStakingVaultMetadata() {
208
- const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
209
- const vaultMetadata = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultData(chainId);
271
+ async refreshPooledStakingVaultMetadata(chainId) {
272
+ const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
273
+ const vaultMetadata = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getVaultData(chainIdToUse);
210
274
  this.update((state) => {
211
- state.pooled_staking.vaultMetadata = vaultMetadata;
275
+ const chainState = state.pooled_staking[chainIdToUse] ??
276
+ exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
277
+ state.pooled_staking[chainIdToUse] = {
278
+ ...chainState,
279
+ vaultMetadata,
280
+ };
212
281
  });
213
282
  }
214
283
  /**
215
284
  * Refreshes pooled staking vault daily apys for the current chain.
216
285
  * Updates the pooled staking vault daily apys controller state.
217
286
  *
287
+ * @param chainId - The chain id to refresh pooled staking vault daily apys for (optional).
218
288
  * @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 365).
219
289
  * @param order - The order in which to fetch pooled staking vault daily apys. Descending order fetches the latest N days (latest working backwards). Ascending order fetches the oldest N days (oldest working forwards) (defaults to 'desc').
220
290
  * @returns A promise that resolves when the pooled staking vault daily apys have been updated.
221
291
  */
222
- async refreshPooledStakingVaultDailyApys(days = 365, order = 'desc') {
223
- const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
224
- const vaultDailyApys = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultDailyApys(chainId, days, order);
292
+ async refreshPooledStakingVaultDailyApys(chainId, days = 365, order = 'desc') {
293
+ const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
294
+ const vaultDailyApys = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getVaultDailyApys(chainIdToUse, days, order);
225
295
  this.update((state) => {
226
- state.pooled_staking.vaultDailyApys = vaultDailyApys;
296
+ const chainState = state.pooled_staking[chainIdToUse] ??
297
+ exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
298
+ state.pooled_staking[chainIdToUse] = {
299
+ ...chainState,
300
+ vaultDailyApys,
301
+ };
227
302
  });
228
303
  }
229
304
  /**
230
305
  * Refreshes pooled staking vault apy averages for the current chain.
231
306
  * Updates the pooled staking vault apy averages controller state.
232
307
  *
308
+ * @param chainId - The chain id to refresh pooled staking vault apy averages for (optional).
233
309
  * @returns A promise that resolves when the pooled staking vault apy averages have been updated.
234
310
  */
235
- async refreshPooledStakingVaultApyAverages() {
236
- const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
237
- const vaultApyAverages = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultApyAverages(chainId);
311
+ async refreshPooledStakingVaultApyAverages(chainId) {
312
+ const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
313
+ const vaultApyAverages = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getVaultApyAverages(chainIdToUse);
238
314
  this.update((state) => {
239
- state.pooled_staking.vaultApyAverages = vaultApyAverages;
315
+ const chainState = state.pooled_staking[chainIdToUse] ??
316
+ exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
317
+ state.pooled_staking[chainIdToUse] = {
318
+ ...chainState,
319
+ vaultApyAverages,
320
+ };
240
321
  });
241
322
  }
242
323
  /**
@@ -251,39 +332,306 @@ class EarnController extends base_controller_1.BaseController {
251
332
  * @throws {Error} If any of the refresh operations fail, with concatenated error messages
252
333
  */
253
334
  async refreshPooledStakingData({ resetCache, address, } = {}) {
335
+ const errors = [];
336
+ for (const chainId of __classPrivateFieldGet(this, _EarnController_supportedPooledStakingChains, "f")) {
337
+ await Promise.all([
338
+ this.refreshPooledStakes({ resetCache, address, chainId }).catch((error) => {
339
+ errors.push(error);
340
+ }),
341
+ this.refreshStakingEligibility({ address }).catch((error) => {
342
+ errors.push(error);
343
+ }),
344
+ this.refreshPooledStakingVaultMetadata(chainId).catch((error) => {
345
+ errors.push(error);
346
+ }),
347
+ this.refreshPooledStakingVaultDailyApys(chainId).catch((error) => {
348
+ errors.push(error);
349
+ }),
350
+ this.refreshPooledStakingVaultApyAverages(chainId).catch((error) => {
351
+ errors.push(error);
352
+ }),
353
+ ]);
354
+ }
355
+ if (errors.length > 0) {
356
+ throw new Error(`Failed to refresh some staking data: ${errors
357
+ .map((e) => e.message)
358
+ .join(', ')}`);
359
+ }
360
+ }
361
+ /**
362
+ * Refreshes the lending markets data for all chains.
363
+ * Updates the lending markets in the controller state.
364
+ *
365
+ * @returns A promise that resolves when the lending markets have been updated
366
+ */
367
+ async refreshLendingMarkets() {
368
+ const markets = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getMarkets();
369
+ this.update((state) => {
370
+ state.lending.markets = markets;
371
+ });
372
+ }
373
+ /**
374
+ * Refreshes the lending positions for the current account.
375
+ * Updates the lending positions in the controller state.
376
+ *
377
+ * @param options - Optional arguments
378
+ * @param [options.address] - The address to refresh lending positions for (optional).
379
+ * @returns A promise that resolves when the lending positions have been updated
380
+ */
381
+ async refreshLendingPositions({ address, } = {}) {
382
+ const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
383
+ if (!addressToUse) {
384
+ return;
385
+ }
386
+ // linter complaining about this not being a promise, but it is
387
+ // TODO: figure out why this is not seen as a promise
388
+ const positions = await Promise.resolve(__classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getPositions(addressToUse));
389
+ this.update((state) => {
390
+ state.lending.positions = positions.map((position) => ({
391
+ ...position,
392
+ marketId: position.market.id,
393
+ marketAddress: position.market.address,
394
+ protocol: position.market.protocol,
395
+ }));
396
+ });
397
+ }
398
+ /**
399
+ * Refreshes the lending eligibility status for the current account.
400
+ * Updates the eligibility status in the controller state based on the location and address blocklist for compliance.
401
+ *
402
+ * @param options - Optional arguments
403
+ * @param [options.address] - The address to refresh lending eligibility for (optional).
404
+ * @returns A promise that resolves when the eligibility status has been updated
405
+ */
406
+ async refreshLendingEligibility({ address, } = {}) {
407
+ const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
408
+ // TODO: this is a temporary solution to refresh lending eligibility as
409
+ // the eligibility check is not yet implemented for lending
410
+ // this check will check the address against the same blocklist as the
411
+ // staking eligibility check
412
+ if (!addressToUse) {
413
+ return;
414
+ }
415
+ const { eligible: isEligible } = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getPooledStakingEligibility([
416
+ addressToUse,
417
+ ]);
418
+ this.update((state) => {
419
+ state.lending.isEligible = isEligible;
420
+ state.pooled_staking.isEligible = isEligible;
421
+ });
422
+ }
423
+ /**
424
+ * Refreshes all lending related data including markets, positions, and eligibility.
425
+ * This method allows partial success, meaning some data may update while other requests fail.
426
+ * All errors are collected and thrown as a single error message.
427
+ *
428
+ * @returns A promise that resolves when all possible data has been updated
429
+ * @throws {Error} If any of the refresh operations fail, with concatenated error messages
430
+ */
431
+ async refreshLendingData() {
254
432
  const errors = [];
255
433
  await Promise.all([
256
- this.refreshPooledStakes({ resetCache, address }).catch((error) => {
434
+ this.refreshLendingMarkets().catch((error) => {
257
435
  errors.push(error);
258
436
  }),
259
- this.refreshStakingEligibility({ address }).catch((error) => {
437
+ this.refreshLendingPositions().catch((error) => {
260
438
  errors.push(error);
261
439
  }),
262
- this.refreshPooledStakingVaultMetadata().catch((error) => {
263
- errors.push(error);
264
- }),
265
- this.refreshPooledStakingVaultDailyApys().catch((error) => {
266
- errors.push(error);
267
- }),
268
- this.refreshPooledStakingVaultApyAverages().catch((error) => {
440
+ this.refreshLendingEligibility().catch((error) => {
269
441
  errors.push(error);
270
442
  }),
271
443
  ]);
272
444
  if (errors.length > 0) {
273
- throw new Error(`Failed to refresh some staking data: ${errors
445
+ throw new Error(`Failed to refresh some lending data: ${errors
274
446
  .map((e) => e.message)
275
447
  .join(', ')}`);
276
448
  }
277
449
  }
450
+ /**
451
+ * Gets the lending position history for the current account.
452
+ *
453
+ * @param options - Optional arguments
454
+ * @param [options.address] - The address to get lending position history for (optional).
455
+ * @param [options.chainId] - The chain id to get lending position history for (optional).
456
+ * @param [options.positionId] - The position id to get lending position history for.
457
+ * @param [options.marketId] - The market id to get lending position history for.
458
+ * @param [options.marketAddress] - The market address to get lending position history for.
459
+ * @param [options.protocol] - The protocol to get lending position history for.
460
+ * @param [options.days] - The number of days to get lending position history for (optional).
461
+ * @returns A promise that resolves when the lending position history has been updated
462
+ */
463
+ getLendingPositionHistory({ address, chainId, positionId, marketId, marketAddress, protocol, days = 730, }) {
464
+ const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
465
+ const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
466
+ if (!addressToUse) {
467
+ return [];
468
+ }
469
+ return __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getPositionHistory(addressToUse, chainIdToUse, protocol, marketId, marketAddress, positionId, days);
470
+ }
471
+ /**
472
+ * Gets the lending market daily apys and averages for the current chain.
473
+ *
474
+ * @param options - Optional arguments
475
+ * @param [options.chainId] - The chain id to get lending market daily apys and averages for (optional).
476
+ * @param [options.protocol] - The protocol to get lending market daily apys and averages for.
477
+ * @param [options.marketId] - The market id to get lending market daily apys and averages for.
478
+ * @param [options.days] - The number of days to get lending market daily apys and averages for (optional).
479
+ * @returns A promise that resolves when the lending market daily apys and averages have been updated
480
+ */
481
+ getLendingMarketDailyApysAndAverages({ chainId, protocol, marketId, days = 365, }) {
482
+ const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
483
+ return __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getHistoricMarketApys(chainIdToUse, protocol, marketId, days);
484
+ }
485
+ /**
486
+ * Executes a lending deposit transaction.
487
+ *
488
+ * @param options - The options for the lending deposit transaction.
489
+ * @param options.amount - The amount to deposit.
490
+ * @param options.protocol - The protocol of the lending market.
491
+ * @param options.underlyingTokenAddress - The address of the underlying token.
492
+ * @param options.gasOptions - The gas options for the transaction.
493
+ * @param options.gasOptions.gasLimit - The gas limit for the transaction.
494
+ * @param options.gasOptions.gasBufferPct - The gas buffer percentage for the transaction.
495
+ * @param options.txOptions - The transaction options for the transaction.
496
+ * @returns A promise that resolves to the transaction hash.
497
+ */
498
+ async executeLendingDeposit({ amount, protocol, underlyingTokenAddress, gasOptions, txOptions, }) {
499
+ const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
500
+ const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeDepositTransactionData(amount, address, gasOptions);
501
+ if (!transactionData) {
502
+ throw new Error('Transaction data not found');
503
+ }
504
+ if (!__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
505
+ throw new Error('Selected network client id not found');
506
+ }
507
+ const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
508
+ ...transactionData,
509
+ value: transactionData.value.toString(),
510
+ chainId: (0, controller_utils_1.toHex)(__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this)),
511
+ gasLimit: String(transactionData.gasLimit),
512
+ }, {
513
+ ...txOptions,
514
+ networkClientId: __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f"),
515
+ });
516
+ return txHash;
517
+ }
518
+ /**
519
+ * Executes a lending withdraw transaction.
520
+ *
521
+ * @param options - The options for the lending withdraw transaction.
522
+ * @param options.amount - The amount to withdraw.
523
+ * @param options.protocol - The protocol of the lending market.
524
+ * @param options.underlyingTokenAddress - The address of the underlying token.
525
+ * @param options.gasOptions - The gas options for the transaction.
526
+ * @param options.gasOptions.gasLimit - The gas limit for the transaction.
527
+ * @param options.gasOptions.gasBufferPct - The gas buffer percentage for the transaction.
528
+ * @param options.txOptions - The transaction options for the transaction.
529
+ * @returns A promise that resolves to the transaction hash.
530
+ */
531
+ async executeLendingWithdraw({ amount, protocol, underlyingTokenAddress, gasOptions, txOptions, }) {
532
+ const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
533
+ const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeWithdrawTransactionData(amount, address, gasOptions);
534
+ if (!transactionData) {
535
+ throw new Error('Transaction data not found');
536
+ }
537
+ if (!__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
538
+ throw new Error('Selected network client id not found');
539
+ }
540
+ const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
541
+ ...transactionData,
542
+ value: transactionData.value.toString(),
543
+ chainId: (0, controller_utils_1.toHex)(__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this)),
544
+ gasLimit: String(transactionData.gasLimit),
545
+ }, {
546
+ ...txOptions,
547
+ networkClientId: __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f"),
548
+ });
549
+ return txHash;
550
+ }
551
+ /**
552
+ * Executes a lending token approve transaction.
553
+ *
554
+ * @param options - The options for the lending token approve transaction.
555
+ * @param options.amount - The amount to approve.
556
+ * @param options.protocol - The protocol of the lending market.
557
+ * @param options.underlyingTokenAddress - The address of the underlying token.
558
+ * @param options.gasOptions - The gas options for the transaction.
559
+ * @param options.gasOptions.gasLimit - The gas limit for the transaction.
560
+ * @param options.gasOptions.gasBufferPct - The gas buffer percentage for the transaction.
561
+ * @param options.txOptions - The transaction options for the transaction.
562
+ * @returns A promise that resolves to the transaction hash.
563
+ */
564
+ async executeLendingTokenApprove({ protocol, amount, underlyingTokenAddress, gasOptions, txOptions, }) {
565
+ const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
566
+ const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeUnderlyingTokenApproveTransactionData(amount, address, gasOptions);
567
+ if (!transactionData) {
568
+ throw new Error('Transaction data not found');
569
+ }
570
+ if (!__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
571
+ throw new Error('Selected network client id not found');
572
+ }
573
+ const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
574
+ ...transactionData,
575
+ value: transactionData.value.toString(),
576
+ chainId: (0, controller_utils_1.toHex)(__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this)),
577
+ gasLimit: String(transactionData.gasLimit),
578
+ }, {
579
+ ...txOptions,
580
+ networkClientId: __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f"),
581
+ });
582
+ return txHash;
583
+ }
584
+ /**
585
+ * Gets the allowance for a lending token.
586
+ *
587
+ * @param protocol - The protocol of the lending market.
588
+ * @param underlyingTokenAddress - The address of the underlying token.
589
+ * @returns A promise that resolves to the allowance.
590
+ */
591
+ async getLendingTokenAllowance(protocol, underlyingTokenAddress) {
592
+ const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
593
+ const allowance = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.underlyingTokenAllowance(address);
594
+ return allowance;
595
+ }
596
+ /**
597
+ * Gets the maximum withdraw amount for a lending token's output token or shares if no output token.
598
+ *
599
+ * @param protocol - The protocol of the lending market.
600
+ * @param underlyingTokenAddress - The address of the underlying token.
601
+ * @returns A promise that resolves to the maximum withdraw amount.
602
+ */
603
+ async getLendingTokenMaxWithdraw(protocol, underlyingTokenAddress) {
604
+ const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
605
+ const maxWithdraw = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.maxWithdraw(address);
606
+ return maxWithdraw;
607
+ }
608
+ /**
609
+ * Gets the maximum deposit amount for a lending token.
610
+ *
611
+ * @param protocol - The protocol of the lending market.
612
+ * @param underlyingTokenAddress - The address of the underlying token.
613
+ * @returns A promise that resolves to the maximum deposit amount.
614
+ */
615
+ async getLendingTokenMaxDeposit(protocol, underlyingTokenAddress) {
616
+ const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
617
+ const maxDeposit = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.maxDeposit(address);
618
+ return maxDeposit;
619
+ }
278
620
  }
279
621
  exports.EarnController = EarnController;
280
- _EarnController_stakeSDK = new WeakMap(), _EarnController_selectedNetworkClientId = new WeakMap(), _EarnController_stakingApiService = new WeakMap(), _EarnController_instances = new WeakSet(), _EarnController_initializeSDK = function _EarnController_initializeSDK(networkClientId) {
622
+ _EarnController_earnSDK = new WeakMap(), _EarnController_selectedNetworkClientId = new WeakMap(), _EarnController_earnApiService = new WeakMap(), _EarnController_addTransactionFn = new WeakMap(), _EarnController_supportedPooledStakingChains = new WeakMap(), _EarnController_instances = new WeakSet(), _EarnController_initializeSDK =
623
+ /**
624
+ * Initializes the Earn SDK.
625
+ *
626
+ * @param networkClientId - The network client id to initialize the Earn SDK for (optional).
627
+ */
628
+ async function _EarnController_initializeSDK(networkClientId) {
281
629
  const { selectedNetworkClientId } = networkClientId
282
630
  ? { selectedNetworkClientId: networkClientId }
283
631
  : this.messagingSystem.call('NetworkController:getState');
284
632
  const networkClient = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
285
633
  if (!networkClient?.provider) {
286
- __classPrivateFieldSet(this, _EarnController_stakeSDK, null, "f");
634
+ __classPrivateFieldSet(this, _EarnController_earnSDK, null, "f");
287
635
  return;
288
636
  }
289
637
  const provider = new providers_1.Web3Provider(networkClient.provider);
@@ -293,31 +641,23 @@ _EarnController_stakeSDK = new WeakMap(), _EarnController_selectedNetworkClientI
293
641
  chainId: (0, controller_utils_1.convertHexToDecimal)(chainId),
294
642
  };
295
643
  try {
296
- __classPrivateFieldSet(this, _EarnController_stakeSDK, stake_sdk_1.StakeSdk.create(config), "f");
297
- __classPrivateFieldGet(this, _EarnController_stakeSDK, "f").pooledStakingContract.connectSignerOrProvider(provider);
644
+ __classPrivateFieldSet(this, _EarnController_earnSDK, await stake_sdk_1.EarnSdk.create(provider, config), "f");
298
645
  }
299
646
  catch (error) {
300
- __classPrivateFieldSet(this, _EarnController_stakeSDK, null, "f");
647
+ __classPrivateFieldSet(this, _EarnController_earnSDK, null, "f");
301
648
  // Only log unexpected errors, not unsupported chain errors
302
649
  if (!(error instanceof Error &&
303
650
  error.message.includes('Unsupported chainId'))) {
304
- console.error('Stake SDK initialization failed:', error);
651
+ console.error('Earn SDK initialization failed:', error);
305
652
  }
306
653
  }
307
654
  }, _EarnController_getCurrentAccount = function _EarnController_getCurrentAccount() {
308
655
  return this.messagingSystem.call('AccountsController:getSelectedAccount');
309
- }, _EarnController_getCurrentChainId = function _EarnController_getCurrentChainId() {
310
- // const { selectedNetworkClientId } = this.messagingSystem.call(
311
- // 'NetworkController:getState',
312
- // );
313
- // const {
314
- // configuration: { chainId },
315
- // } = this.messagingSystem.call(
316
- // 'NetworkController:getNetworkClientById',
317
- // selectedNetworkClientId,
318
- // );
319
- // return convertHexToDecimal(chainId);
320
- // TEMP: Until we update our data-fetching and storage solution to not depend on single selected network.
321
- return stake_sdk_1.ChainId.ETHEREUM;
656
+ }, _EarnController_getCurrentChainId = function _EarnController_getCurrentChainId(networkClientId) {
657
+ const networkClientIdToUse = networkClientId ??
658
+ this.messagingSystem.call('NetworkController:getState')
659
+ .selectedNetworkClientId;
660
+ const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientIdToUse);
661
+ return (0, controller_utils_1.convertHexToDecimal)(chainId);
322
662
  };
323
663
  //# sourceMappingURL=EarnController.cjs.map