@metamask-previews/earn-controller 0.14.0-preview-e9f8bc88 → 0.14.0-preview-2723071f

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