@metamask/earn-controller 0.3.0 → 0.5.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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.5.0]
11
+
12
+ ### Added
13
+
14
+ - Add pooled staking vault daily apys and vault apy averages to earn controller ([#5368](https://github.com/MetaMask/core/pull/5368))
15
+
16
+ ## [0.4.0]
17
+
18
+ ### Added
19
+
20
+ - Add resetCache arg to `refreshPooledStakingData` and `refreshPooledStakes` in EarnController ([#5334](https://github.com/MetaMask/core/pull/5334))
21
+
10
22
  ## [0.3.0]
11
23
 
12
24
  ### Changed
@@ -32,7 +44,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
32
44
 
33
45
  - Initial release ([#5271](https://github.com/MetaMask/core/pull/5271))
34
46
 
35
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/earn-controller@0.3.0...HEAD
47
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/earn-controller@0.5.0...HEAD
48
+ [0.5.0]: https://github.com/MetaMask/core/compare/@metamask/earn-controller@0.4.0...@metamask/earn-controller@0.5.0
49
+ [0.4.0]: https://github.com/MetaMask/core/compare/@metamask/earn-controller@0.3.0...@metamask/earn-controller@0.4.0
36
50
  [0.3.0]: https://github.com/MetaMask/core/compare/@metamask/earn-controller@0.2.1...@metamask/earn-controller@0.3.0
37
51
  [0.2.1]: https://github.com/MetaMask/core/compare/@metamask/earn-controller@0.2.0...@metamask/earn-controller@0.2.1
38
52
  [0.2.0]: https://github.com/MetaMask/core/compare/@metamask/earn-controller@0.1.0...@metamask/earn-controller@0.2.0
@@ -46,6 +46,14 @@ const DEFAULT_STABLECOIN_VAULT = {
46
46
  supply: '0',
47
47
  liquidity: '0',
48
48
  };
49
+ const DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES = {
50
+ oneDay: '0',
51
+ oneWeek: '0',
52
+ oneMonth: '0',
53
+ threeMonths: '0',
54
+ sixMonths: '0',
55
+ oneYear: '0',
56
+ };
49
57
  /**
50
58
  * Gets the default state for the EarnController.
51
59
  *
@@ -61,13 +69,15 @@ function getDefaultEarnControllerState() {
61
69
  exitRequests: [],
62
70
  },
63
71
  exchangeRate: '1',
64
- vaultData: {
72
+ vaultMetadata: {
65
73
  apy: '0',
66
74
  capacity: '0',
67
75
  feePercent: 0,
68
76
  totalAssets: '0',
69
77
  vaultAddress: '0x0000000000000000000000000000000000000000',
70
78
  },
79
+ vaultDailyApys: [],
80
+ vaultApyAverages: DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES,
71
81
  isEligible: false,
72
82
  },
73
83
  stablecoin_lending: {
@@ -104,13 +114,17 @@ class EarnController extends base_controller_1.BaseController {
104
114
  if (networkControllerState.selectedNetworkClientId !==
105
115
  __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
106
116
  __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this, networkControllerState.selectedNetworkClientId);
107
- this.refreshPooledStakingData().catch(console.error);
117
+ this.refreshPooledStakingVaultMetadata().catch(console.error);
118
+ this.refreshPooledStakingVaultDailyApys().catch(console.error);
119
+ this.refreshPooledStakingVaultApyAverages().catch(console.error);
120
+ this.refreshPooledStakes().catch(console.error);
108
121
  }
109
122
  __classPrivateFieldSet(this, _EarnController_selectedNetworkClientId, networkControllerState.selectedNetworkClientId, "f");
110
123
  });
111
124
  // Listen for account changes
112
125
  this.messagingSystem.subscribe('AccountsController:selectedAccountChange', () => {
113
- this.refreshPooledStakingData().catch(console.error);
126
+ this.refreshStakingEligibility().catch(console.error);
127
+ this.refreshPooledStakes().catch(console.error);
114
128
  });
115
129
  }
116
130
  /**
@@ -118,15 +132,16 @@ class EarnController extends base_controller_1.BaseController {
118
132
  * Fetches updated stake information including lifetime rewards, assets, and exit requests
119
133
  * from the staking API service and updates the state.
120
134
  *
135
+ * @param resetCache - Control whether the BE cache should be invalidated.
121
136
  * @returns A promise that resolves when the stakes data has been updated
122
137
  */
123
- async refreshPooledStakes() {
138
+ async refreshPooledStakes(resetCache = false) {
124
139
  const currentAccount = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this);
125
140
  if (!currentAccount?.address) {
126
141
  return;
127
142
  }
128
143
  const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
129
- const { accounts, exchangeRate } = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getPooledStakes([currentAccount.address], chainId);
144
+ const { accounts, exchangeRate } = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getPooledStakes([currentAccount.address], chainId, resetCache);
130
145
  this.update((state) => {
131
146
  state.pooled_staking.pooledStakes = accounts[0];
132
147
  state.pooled_staking.exchangeRate = exchangeRate;
@@ -151,17 +166,45 @@ class EarnController extends base_controller_1.BaseController {
151
166
  });
152
167
  }
153
168
  /**
154
- * Refreshes vault data for the current chain.
155
- * Updates the vault data in the controller state including APY, capacity,
169
+ * Refreshes pooled staking vault metadata for the current chain.
170
+ * Updates the vault metadata in the controller state including APY, capacity,
156
171
  * fee percentage, total assets, and vault address.
157
172
  *
158
- * @returns A promise that resolves when the vault data has been updated
173
+ * @returns A promise that resolves when the vault metadata has been updated
174
+ */
175
+ async refreshPooledStakingVaultMetadata() {
176
+ const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
177
+ const vaultMetadata = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultData(chainId);
178
+ this.update((state) => {
179
+ state.pooled_staking.vaultMetadata = vaultMetadata;
180
+ });
181
+ }
182
+ /**
183
+ * Refreshes pooled staking vault daily apys for the current chain.
184
+ * Updates the pooled staking vault daily apys controller state.
185
+ *
186
+ * @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 30).
187
+ * @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').
188
+ * @returns A promise that resolves when the pooled staking vault daily apys have been updated.
159
189
  */
160
- async refreshVaultData() {
190
+ async refreshPooledStakingVaultDailyApys(days = 30, order = 'desc') {
161
191
  const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
162
- const vaultData = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultData(chainId);
192
+ const vaultDailyApys = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultDailyApys(chainId, days, order);
163
193
  this.update((state) => {
164
- state.pooled_staking.vaultData = vaultData;
194
+ state.pooled_staking.vaultDailyApys = vaultDailyApys;
195
+ });
196
+ }
197
+ /**
198
+ * Refreshes pooled staking vault apy averages for the current chain.
199
+ * Updates the pooled staking vault apy averages controller state.
200
+ *
201
+ * @returns A promise that resolves when the pooled staking vault apy averages have been updated.
202
+ */
203
+ async refreshPooledStakingVaultApyAverages() {
204
+ const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
205
+ const vaultApyAverages = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultApyAverages(chainId);
206
+ this.update((state) => {
207
+ state.pooled_staking.vaultApyAverages = vaultApyAverages;
165
208
  });
166
209
  }
167
210
  /**
@@ -169,19 +212,26 @@ class EarnController extends base_controller_1.BaseController {
169
212
  * This method allows partial success, meaning some data may update while other requests fail.
170
213
  * All errors are collected and thrown as a single error message.
171
214
  *
215
+ * @param resetCache - Control whether the BE cache should be invalidated.
172
216
  * @returns A promise that resolves when all possible data has been updated
173
217
  * @throws {Error} If any of the refresh operations fail, with concatenated error messages
174
218
  */
175
- async refreshPooledStakingData() {
219
+ async refreshPooledStakingData(resetCache = false) {
176
220
  const errors = [];
177
221
  await Promise.all([
178
- this.refreshPooledStakes().catch((error) => {
222
+ this.refreshPooledStakes(resetCache).catch((error) => {
179
223
  errors.push(error);
180
224
  }),
181
225
  this.refreshStakingEligibility().catch((error) => {
182
226
  errors.push(error);
183
227
  }),
184
- this.refreshVaultData().catch((error) => {
228
+ this.refreshPooledStakingVaultMetadata().catch((error) => {
229
+ errors.push(error);
230
+ }),
231
+ this.refreshPooledStakingVaultDailyApys().catch((error) => {
232
+ errors.push(error);
233
+ }),
234
+ this.refreshPooledStakingVaultApyAverages().catch((error) => {
185
235
  errors.push(error);
186
236
  }),
187
237
  ]);
@@ -1 +1 @@
1
- {"version":3,"file":"EarnController.cjs","sourceRoot":"","sources":["../src/EarnController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,wDAAwD;AAWxD,+DAA2D;AAC3D,iEAAiE;AAMjE,mDAM6B;AAEhB,QAAA,cAAc,GAAG,gBAAgB,CAAC;AAwB/C;;GAEG;AACH,MAAM,sBAAsB,GAAuC;IACjE,cAAc,EAAE;QACd,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;IACD,kBAAkB,EAAE;QAClB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;IACD,WAAW,EAAE;QACX,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,IAAI;KAChB;CACF,CAAC;AASF,wBAAwB;AACxB,MAAM,wBAAwB,GAAoB;IAChD,MAAM,EAAE,EAAE;IACV,IAAI,EAAE,EAAE;IACR,OAAO,EAAE,CAAC;IACV,YAAY,EAAE,EAAE;IAChB,YAAY,EAAE,EAAE;IAChB,UAAU,EAAE,GAAG;IACf,MAAM,EAAE,GAAG;IACX,SAAS,EAAE,GAAG;CACf,CAAC;AAEF;;;;GAIG;AACH,SAAgB,6BAA6B;IAC3C,OAAO;QACL,cAAc,EAAE;YACd,YAAY,EAAE;gBACZ,OAAO,EAAE,EAAE;gBACX,eAAe,EAAE,GAAG;gBACpB,MAAM,EAAE,GAAG;gBACX,YAAY,EAAE,EAAE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AAe,CAAC,IAAI,CAAC,uCAAuC,CAAC,CAAC;AAC5E,CAAC;IAGC,MAAM,EAAE,uBAAuB,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3D,4BAA4B,CAC7B,CAAC;IACF,MAAM,EACJ,aAAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3B,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IACF,OAAO,IAAA,sCAAmB,EAAC,OAAO,CAAC,CAAC;AACtC,CAAC","sourcesContent":["import { Web3Provider } from '@ethersproject/providers';\nimport type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport type { NetworkControllerStateChangeEvent } from '@metamask/network-controller';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n} from '@metamask/network-controller';\nimport {\n StakeSdk,\n StakingApiService,\n type PooledStake,\n type StakeSdkConfig,\n type VaultData,\n} from '@metamask/stake-sdk';\n\nexport const controllerName = 'EarnController';\n\nexport type PooledStakingState = {\n pooledStakes: PooledStake;\n exchangeRate: string;\n vaultData: VaultData;\n isEligible: boolean;\n};\n\nexport type StablecoinLendingState = {\n vaults: StablecoinVault[];\n};\n\nexport type StablecoinVault = {\n symbol: string;\n name: string;\n chainId: number;\n tokenAddress: string;\n vaultAddress: string;\n currentAPY: string;\n supply: string;\n liquidity: string;\n};\n\n/**\n * Metadata for the EarnController.\n */\nconst earnControllerMetadata: StateMetadata<EarnControllerState> = {\n pooled_staking: {\n persist: true,\n anonymous: false,\n },\n stablecoin_lending: {\n persist: true,\n anonymous: false,\n },\n lastUpdated: {\n persist: false,\n anonymous: true,\n },\n};\n\n// === State Types ===\nexport type EarnControllerState = {\n pooled_staking: PooledStakingState;\n stablecoin_lending?: StablecoinLendingState;\n lastUpdated: number;\n};\n\n// === Default State ===\nconst DEFAULT_STABLECOIN_VAULT: StablecoinVault = {\n symbol: '',\n name: '',\n chainId: 0,\n tokenAddress: '',\n vaultAddress: '',\n currentAPY: '0',\n supply: '0',\n liquidity: '0',\n};\n\n/**\n * Gets the default state for the EarnController.\n *\n * @returns The default EarnController state.\n */\nexport function getDefaultEarnControllerState(): EarnControllerState {\n return {\n pooled_staking: {\n pooledStakes: {\n account: '',\n lifetimeRewards: '0',\n assets: '0',\n exitRequests: [],\n },\n exchangeRate: '1',\n vaultData: {\n apy: '0',\n capacity: '0',\n feePercent: 0,\n totalAssets: '0',\n vaultAddress: '0x0000000000000000000000000000000000000000',\n },\n isEligible: false,\n },\n stablecoin_lending: {\n vaults: [DEFAULT_STABLECOIN_VAULT],\n },\n lastUpdated: 0,\n };\n}\n\n// === MESSENGER ===\n\n/**\n * The action which can be used to retrieve the state of the EarnController.\n */\nexport type EarnControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n EarnControllerState\n>;\n\n/**\n * All actions that EarnController registers, to be called externally.\n */\nexport type EarnControllerActions = EarnControllerGetStateAction;\n\n/**\n * All actions that EarnController calls internally.\n */\nexport type AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction;\n\n/**\n * The event that EarnController publishes when updating state.\n */\nexport type EarnControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n EarnControllerState\n>;\n\n/**\n * All events that EarnController publishes, to be subscribed to externally.\n */\nexport type EarnControllerEvents = EarnControllerStateChangeEvent;\n\n/**\n * All events that EarnController subscribes to internally.\n */\nexport type AllowedEvents =\n | AccountsControllerSelectedAccountChangeEvent\n | NetworkControllerStateChangeEvent;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * EarnController.\n */\nexport type EarnControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n EarnControllerActions | AllowedActions,\n EarnControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * EarnController manages DeFi earning opportunities across different protocols and chains.\n */\nexport class EarnController extends BaseController<\n typeof controllerName,\n EarnControllerState,\n EarnControllerMessenger\n> {\n #stakeSDK: StakeSdk | null = null;\n\n #selectedNetworkClientId?: string;\n\n readonly #stakingApiService: StakingApiService = new StakingApiService();\n\n constructor({\n messenger,\n state = {},\n }: {\n messenger: EarnControllerMessenger;\n state?: Partial<EarnControllerState>;\n }) {\n super({\n name: controllerName,\n metadata: earnControllerMetadata,\n messenger,\n state: {\n ...getDefaultEarnControllerState(),\n ...state,\n },\n });\n\n this.#initializeSDK();\n this.refreshPooledStakingData().catch(console.error);\n\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n this.#selectedNetworkClientId = selectedNetworkClientId;\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n (networkControllerState) => {\n if (\n networkControllerState.selectedNetworkClientId !==\n this.#selectedNetworkClientId\n ) {\n this.#initializeSDK(networkControllerState.selectedNetworkClientId);\n this.refreshPooledStakingData().catch(console.error);\n }\n this.#selectedNetworkClientId =\n networkControllerState.selectedNetworkClientId;\n },\n );\n\n // Listen for account changes\n this.messagingSystem.subscribe(\n 'AccountsController:selectedAccountChange',\n () => {\n this.refreshPooledStakingData().catch(console.error);\n },\n );\n }\n\n #initializeSDK(networkClientId?: string) {\n const { selectedNetworkClientId } = networkClientId\n ? { selectedNetworkClientId: networkClientId }\n : this.messagingSystem.call('NetworkController:getState');\n\n const networkClient = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (!networkClient?.provider) {\n this.#stakeSDK = null;\n return;\n }\n\n const provider = new Web3Provider(networkClient.provider);\n const { chainId } = networkClient.configuration;\n\n // Initialize appropriate contracts based on chainId\n const config: StakeSdkConfig = {\n chainId: convertHexToDecimal(chainId),\n };\n\n try {\n this.#stakeSDK = StakeSdk.create(config);\n this.#stakeSDK.pooledStakingContract.connectSignerOrProvider(provider);\n } catch (error) {\n this.#stakeSDK = null;\n // Only log unexpected errors, not unsupported chain errors\n if (\n !(\n error instanceof Error &&\n error.message.includes('Unsupported chainId')\n )\n ) {\n console.error('Stake SDK initialization failed:', error);\n }\n }\n }\n\n #getCurrentAccount() {\n return this.messagingSystem.call('AccountsController:getSelectedAccount');\n }\n\n #getCurrentChainId(): number {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return convertHexToDecimal(chainId);\n }\n\n /**\n * Refreshes the pooled stakes data for the current account.\n * Fetches updated stake information including lifetime rewards, assets, and exit requests\n * from the staking API service and updates the state.\n *\n * @returns A promise that resolves when the stakes data has been updated\n */\n async refreshPooledStakes(): Promise<void> {\n const currentAccount = this.#getCurrentAccount();\n if (!currentAccount?.address) {\n return;\n }\n\n const chainId = this.#getCurrentChainId();\n\n const { accounts, exchangeRate } =\n await this.#stakingApiService.getPooledStakes(\n [currentAccount.address],\n chainId,\n );\n\n this.update((state) => {\n state.pooled_staking.pooledStakes = accounts[0];\n state.pooled_staking.exchangeRate = exchangeRate;\n });\n }\n\n /**\n * Refreshes the staking eligibility status for the current account.\n * Updates the eligibility status in the controller state based on the location and address blocklist for compliance.\n *\n * @returns A promise that resolves when the eligibility status has been updated\n */\n async refreshStakingEligibility(): Promise<void> {\n const currentAccount = this.#getCurrentAccount();\n if (!currentAccount?.address) {\n return;\n }\n\n const { eligible: isEligible } =\n await this.#stakingApiService.getPooledStakingEligibility([\n currentAccount.address,\n ]);\n\n this.update((state) => {\n state.pooled_staking.isEligible = isEligible;\n });\n }\n\n /**\n * Refreshes vault data for the current chain.\n * Updates the vault data in the controller state including APY, capacity,\n * fee percentage, total assets, and vault address.\n *\n * @returns A promise that resolves when the vault data has been updated\n */\n async refreshVaultData(): Promise<void> {\n const chainId = this.#getCurrentChainId();\n const vaultData = await this.#stakingApiService.getVaultData(chainId);\n\n this.update((state) => {\n state.pooled_staking.vaultData = vaultData;\n });\n }\n\n /**\n * Refreshes all pooled staking related data including stakes, eligibility, and vault data.\n * This method allows partial success, meaning some data may update while other requests fail.\n * All errors are collected and thrown as a single error message.\n *\n * @returns A promise that resolves when all possible data has been updated\n * @throws {Error} If any of the refresh operations fail, with concatenated error messages\n */\n async refreshPooledStakingData(): Promise<void> {\n const errors: Error[] = [];\n\n await Promise.all([\n this.refreshPooledStakes().catch((error) => {\n errors.push(error);\n }),\n this.refreshStakingEligibility().catch((error) => {\n errors.push(error);\n }),\n this.refreshVaultData().catch((error) => {\n errors.push(error);\n }),\n ]);\n\n if (errors.length > 0) {\n throw new Error(\n `Failed to refresh some staking data: ${errors\n .map((e) => e.message)\n .join(', ')}`,\n );\n }\n }\n}\n"]}
1
+ {"version":3,"file":"EarnController.cjs","sourceRoot":"","sources":["../src/EarnController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,wDAAwD;AAWxD,+DAA2D;AAC3D,iEAAiE;AAMjE,mDAQ6B;AAEhB,QAAA,cAAc,GAAG,gBAAgB,CAAC;AA0B/C;;GAEG;AACH,MAAM,sBAAsB,GAAuC;IACjE,cAAc,EAAE;QACd,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;IACD,kBAAkB,EAAE;QAClB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;IACD,WAAW,EAAE;QACX,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,IAAI;KAChB;CACF,CAAC;AASF,wBAAwB;AACxB,MAAM,wBAAwB,GAAoB;IAChD,MAAM,EAAE,EAAE;IACV,IAAI,EAAE,EAAE;IACR,OAAO,EAAE,CAAC;IACV,YAAY,EAAE,EAAE;IAChB,YA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AAC;QACtB,OAAO;KACR;IAED,MAAM,QAAQ,GAAG,IAAI,wBAAY,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAC1D,MAAM,EAAE,OAAO,EAAE,GAAG,aAAa,CAAC,aAAa,CAAC;IAEhD,oDAAoD;IACpD,MAAM,MAAM,GAAmB;QAC7B,OAAO,EAAE,IAAA,sCAAmB,EAAC,OAAO,CAAC;KACtC,CAAC;IAEF,IAAI;QACF,uBAAA,IAAI,4BAAa,oBAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,MAAA,CAAC;QACzC,uBAAA,IAAI,gCAAU,CAAC,qBAAqB,CAAC,uBAAuB,CAAC,QAAQ,CAAC,CAAC;KACxE;IAAC,OAAO,KAAK,EAAE;QACd,uBAAA,IAAI,4BAAa,IAAI,MAAA,CAAC;QACtB,2DAA2D;QAC3D,IACE,CAAC,CACC,KAAK,YAAY,KAAK;YACtB,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAC9C,EACD;YACA,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,KAAK,CAAC,CAAC;SAC1D;KACF;AACH,CAAC;IAGC,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,uCAAuC,CAAC,CAAC;AAC5E,CAAC;IAGC,MAAM,EAAE,uBAAuB,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3D,4BAA4B,CAC7B,CAAC;IACF,MAAM,EACJ,aAAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3B,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IACF,OAAO,IAAA,sCAAmB,EAAC,OAAO,CAAC,CAAC;AACtC,CAAC","sourcesContent":["import { Web3Provider } from '@ethersproject/providers';\nimport type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport {\n StakeSdk,\n StakingApiService,\n type PooledStake,\n type StakeSdkConfig,\n type VaultData,\n type VaultDailyApy,\n type VaultApyAverages,\n} from '@metamask/stake-sdk';\n\nexport const controllerName = 'EarnController';\n\nexport type PooledStakingState = {\n pooledStakes: PooledStake;\n exchangeRate: string;\n vaultMetadata: VaultData;\n vaultDailyApys: VaultDailyApy[];\n vaultApyAverages: VaultApyAverages;\n isEligible: boolean;\n};\n\nexport type StablecoinLendingState = {\n vaults: StablecoinVault[];\n};\n\nexport type StablecoinVault = {\n symbol: string;\n name: string;\n chainId: number;\n tokenAddress: string;\n vaultAddress: string;\n currentAPY: string;\n supply: string;\n liquidity: string;\n};\n\n/**\n * Metadata for the EarnController.\n */\nconst earnControllerMetadata: StateMetadata<EarnControllerState> = {\n pooled_staking: {\n persist: true,\n anonymous: false,\n },\n stablecoin_lending: {\n persist: true,\n anonymous: false,\n },\n lastUpdated: {\n persist: false,\n anonymous: true,\n },\n};\n\n// === State Types ===\nexport type EarnControllerState = {\n pooled_staking: PooledStakingState;\n stablecoin_lending?: StablecoinLendingState;\n lastUpdated: number;\n};\n\n// === Default State ===\nconst DEFAULT_STABLECOIN_VAULT: StablecoinVault = {\n symbol: '',\n name: '',\n chainId: 0,\n tokenAddress: '',\n vaultAddress: '',\n currentAPY: '0',\n supply: '0',\n liquidity: '0',\n};\n\nconst DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES: VaultApyAverages = {\n oneDay: '0',\n oneWeek: '0',\n oneMonth: '0',\n threeMonths: '0',\n sixMonths: '0',\n oneYear: '0',\n};\n\n/**\n * Gets the default state for the EarnController.\n *\n * @returns The default EarnController state.\n */\nexport function getDefaultEarnControllerState(): EarnControllerState {\n return {\n pooled_staking: {\n pooledStakes: {\n account: '',\n lifetimeRewards: '0',\n assets: '0',\n exitRequests: [],\n },\n exchangeRate: '1',\n vaultMetadata: {\n apy: '0',\n capacity: '0',\n feePercent: 0,\n totalAssets: '0',\n vaultAddress: '0x0000000000000000000000000000000000000000',\n },\n vaultDailyApys: [],\n vaultApyAverages: DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES,\n isEligible: false,\n },\n stablecoin_lending: {\n vaults: [DEFAULT_STABLECOIN_VAULT],\n },\n lastUpdated: 0,\n };\n}\n\n// === MESSENGER ===\n\n/**\n * The action which can be used to retrieve the state of the EarnController.\n */\nexport type EarnControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n EarnControllerState\n>;\n\n/**\n * All actions that EarnController registers, to be called externally.\n */\nexport type EarnControllerActions = EarnControllerGetStateAction;\n\n/**\n * All actions that EarnController calls internally.\n */\nexport type AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction;\n\n/**\n * The event that EarnController publishes when updating state.\n */\nexport type EarnControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n EarnControllerState\n>;\n\n/**\n * All events that EarnController publishes, to be subscribed to externally.\n */\nexport type EarnControllerEvents = EarnControllerStateChangeEvent;\n\n/**\n * All events that EarnController subscribes to internally.\n */\nexport type AllowedEvents =\n | AccountsControllerSelectedAccountChangeEvent\n | NetworkControllerStateChangeEvent;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * EarnController.\n */\nexport type EarnControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n EarnControllerActions | AllowedActions,\n EarnControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * EarnController manages DeFi earning opportunities across different protocols and chains.\n */\nexport class EarnController extends BaseController<\n typeof controllerName,\n EarnControllerState,\n EarnControllerMessenger\n> {\n #stakeSDK: StakeSdk | null = null;\n\n #selectedNetworkClientId?: string;\n\n readonly #stakingApiService: StakingApiService = new StakingApiService();\n\n constructor({\n messenger,\n state = {},\n }: {\n messenger: EarnControllerMessenger;\n state?: Partial<EarnControllerState>;\n }) {\n super({\n name: controllerName,\n metadata: earnControllerMetadata,\n messenger,\n state: {\n ...getDefaultEarnControllerState(),\n ...state,\n },\n });\n\n this.#initializeSDK();\n this.refreshPooledStakingData().catch(console.error);\n\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n this.#selectedNetworkClientId = selectedNetworkClientId;\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n (networkControllerState) => {\n if (\n networkControllerState.selectedNetworkClientId !==\n this.#selectedNetworkClientId\n ) {\n this.#initializeSDK(networkControllerState.selectedNetworkClientId);\n this.refreshPooledStakingVaultMetadata().catch(console.error);\n this.refreshPooledStakingVaultDailyApys().catch(console.error);\n this.refreshPooledStakingVaultApyAverages().catch(console.error);\n this.refreshPooledStakes().catch(console.error);\n }\n this.#selectedNetworkClientId =\n networkControllerState.selectedNetworkClientId;\n },\n );\n\n // Listen for account changes\n this.messagingSystem.subscribe(\n 'AccountsController:selectedAccountChange',\n () => {\n this.refreshStakingEligibility().catch(console.error);\n this.refreshPooledStakes().catch(console.error);\n },\n );\n }\n\n #initializeSDK(networkClientId?: string) {\n const { selectedNetworkClientId } = networkClientId\n ? { selectedNetworkClientId: networkClientId }\n : this.messagingSystem.call('NetworkController:getState');\n\n const networkClient = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (!networkClient?.provider) {\n this.#stakeSDK = null;\n return;\n }\n\n const provider = new Web3Provider(networkClient.provider);\n const { chainId } = networkClient.configuration;\n\n // Initialize appropriate contracts based on chainId\n const config: StakeSdkConfig = {\n chainId: convertHexToDecimal(chainId),\n };\n\n try {\n this.#stakeSDK = StakeSdk.create(config);\n this.#stakeSDK.pooledStakingContract.connectSignerOrProvider(provider);\n } catch (error) {\n this.#stakeSDK = null;\n // Only log unexpected errors, not unsupported chain errors\n if (\n !(\n error instanceof Error &&\n error.message.includes('Unsupported chainId')\n )\n ) {\n console.error('Stake SDK initialization failed:', error);\n }\n }\n }\n\n #getCurrentAccount() {\n return this.messagingSystem.call('AccountsController:getSelectedAccount');\n }\n\n #getCurrentChainId(): number {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return convertHexToDecimal(chainId);\n }\n\n /**\n * Refreshes the pooled stakes data for the current account.\n * Fetches updated stake information including lifetime rewards, assets, and exit requests\n * from the staking API service and updates the state.\n *\n * @param resetCache - Control whether the BE cache should be invalidated.\n * @returns A promise that resolves when the stakes data has been updated\n */\n async refreshPooledStakes(resetCache = false): Promise<void> {\n const currentAccount = this.#getCurrentAccount();\n if (!currentAccount?.address) {\n return;\n }\n\n const chainId = this.#getCurrentChainId();\n\n const { accounts, exchangeRate } =\n await this.#stakingApiService.getPooledStakes(\n [currentAccount.address],\n chainId,\n resetCache,\n );\n\n this.update((state) => {\n state.pooled_staking.pooledStakes = accounts[0];\n state.pooled_staking.exchangeRate = exchangeRate;\n });\n }\n\n /**\n * Refreshes the staking eligibility status for the current account.\n * Updates the eligibility status in the controller state based on the location and address blocklist for compliance.\n *\n * @returns A promise that resolves when the eligibility status has been updated\n */\n async refreshStakingEligibility(): Promise<void> {\n const currentAccount = this.#getCurrentAccount();\n if (!currentAccount?.address) {\n return;\n }\n\n const { eligible: isEligible } =\n await this.#stakingApiService.getPooledStakingEligibility([\n currentAccount.address,\n ]);\n\n this.update((state) => {\n state.pooled_staking.isEligible = isEligible;\n });\n }\n\n /**\n * Refreshes pooled staking vault metadata for the current chain.\n * Updates the vault metadata in the controller state including APY, capacity,\n * fee percentage, total assets, and vault address.\n *\n * @returns A promise that resolves when the vault metadata has been updated\n */\n async refreshPooledStakingVaultMetadata(): Promise<void> {\n const chainId = this.#getCurrentChainId();\n const vaultMetadata = await this.#stakingApiService.getVaultData(chainId);\n\n this.update((state) => {\n state.pooled_staking.vaultMetadata = vaultMetadata;\n });\n }\n\n /**\n * Refreshes pooled staking vault daily apys for the current chain.\n * Updates the pooled staking vault daily apys controller state.\n *\n * @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 30).\n * @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').\n * @returns A promise that resolves when the pooled staking vault daily apys have been updated.\n */\n async refreshPooledStakingVaultDailyApys(\n days = 30,\n order: 'asc' | 'desc' = 'desc',\n ): Promise<void> {\n const chainId = this.#getCurrentChainId();\n const vaultDailyApys = await this.#stakingApiService.getVaultDailyApys(\n chainId,\n days,\n order,\n );\n\n this.update((state) => {\n state.pooled_staking.vaultDailyApys = vaultDailyApys;\n });\n }\n\n /**\n * Refreshes pooled staking vault apy averages for the current chain.\n * Updates the pooled staking vault apy averages controller state.\n *\n * @returns A promise that resolves when the pooled staking vault apy averages have been updated.\n */\n async refreshPooledStakingVaultApyAverages() {\n const chainId = this.#getCurrentChainId();\n const vaultApyAverages =\n await this.#stakingApiService.getVaultApyAverages(chainId);\n\n this.update((state) => {\n state.pooled_staking.vaultApyAverages = vaultApyAverages;\n });\n }\n\n /**\n * Refreshes all pooled staking related data including stakes, eligibility, and vault data.\n * This method allows partial success, meaning some data may update while other requests fail.\n * All errors are collected and thrown as a single error message.\n *\n * @param resetCache - Control whether the BE cache should be invalidated.\n * @returns A promise that resolves when all possible data has been updated\n * @throws {Error} If any of the refresh operations fail, with concatenated error messages\n */\n async refreshPooledStakingData(resetCache = false): Promise<void> {\n const errors: Error[] = [];\n\n await Promise.all([\n this.refreshPooledStakes(resetCache).catch((error) => {\n errors.push(error);\n }),\n this.refreshStakingEligibility().catch((error) => {\n errors.push(error);\n }),\n this.refreshPooledStakingVaultMetadata().catch((error) => {\n errors.push(error);\n }),\n this.refreshPooledStakingVaultDailyApys().catch((error) => {\n errors.push(error);\n }),\n this.refreshPooledStakingVaultApyAverages().catch((error) => {\n errors.push(error);\n }),\n ]);\n\n if (errors.length > 0) {\n throw new Error(\n `Failed to refresh some staking data: ${errors\n .map((e) => e.message)\n .join(', ')}`,\n );\n }\n }\n}\n"]}
@@ -1,14 +1,15 @@
1
1
  import type { AccountsControllerGetSelectedAccountAction, AccountsControllerSelectedAccountChangeEvent } from "@metamask/accounts-controller";
2
2
  import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
3
3
  import { BaseController } from "@metamask/base-controller";
4
- import type { NetworkControllerStateChangeEvent } from "@metamask/network-controller";
5
- import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction } from "@metamask/network-controller";
6
- import { type PooledStake, type VaultData } from "@metamask/stake-sdk";
4
+ import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
5
+ import { type PooledStake, type VaultData, type VaultDailyApy, type VaultApyAverages } from "@metamask/stake-sdk";
7
6
  export declare const controllerName = "EarnController";
8
7
  export type PooledStakingState = {
9
8
  pooledStakes: PooledStake;
10
9
  exchangeRate: string;
11
- vaultData: VaultData;
10
+ vaultMetadata: VaultData;
11
+ vaultDailyApys: VaultDailyApy[];
12
+ vaultApyAverages: VaultApyAverages;
12
13
  isEligible: boolean;
13
14
  };
14
15
  export type StablecoinLendingState = {
@@ -78,9 +79,10 @@ export declare class EarnController extends BaseController<typeof controllerName
78
79
  * Fetches updated stake information including lifetime rewards, assets, and exit requests
79
80
  * from the staking API service and updates the state.
80
81
  *
82
+ * @param resetCache - Control whether the BE cache should be invalidated.
81
83
  * @returns A promise that resolves when the stakes data has been updated
82
84
  */
83
- refreshPooledStakes(): Promise<void>;
85
+ refreshPooledStakes(resetCache?: boolean): Promise<void>;
84
86
  /**
85
87
  * Refreshes the staking eligibility status for the current account.
86
88
  * Updates the eligibility status in the controller state based on the location and address blocklist for compliance.
@@ -89,21 +91,38 @@ export declare class EarnController extends BaseController<typeof controllerName
89
91
  */
90
92
  refreshStakingEligibility(): Promise<void>;
91
93
  /**
92
- * Refreshes vault data for the current chain.
93
- * Updates the vault data in the controller state including APY, capacity,
94
+ * Refreshes pooled staking vault metadata for the current chain.
95
+ * Updates the vault metadata in the controller state including APY, capacity,
94
96
  * fee percentage, total assets, and vault address.
95
97
  *
96
- * @returns A promise that resolves when the vault data has been updated
98
+ * @returns A promise that resolves when the vault metadata has been updated
97
99
  */
98
- refreshVaultData(): Promise<void>;
100
+ refreshPooledStakingVaultMetadata(): Promise<void>;
101
+ /**
102
+ * Refreshes pooled staking vault daily apys for the current chain.
103
+ * Updates the pooled staking vault daily apys controller state.
104
+ *
105
+ * @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 30).
106
+ * @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').
107
+ * @returns A promise that resolves when the pooled staking vault daily apys have been updated.
108
+ */
109
+ refreshPooledStakingVaultDailyApys(days?: number, order?: 'asc' | 'desc'): Promise<void>;
110
+ /**
111
+ * Refreshes pooled staking vault apy averages for the current chain.
112
+ * Updates the pooled staking vault apy averages controller state.
113
+ *
114
+ * @returns A promise that resolves when the pooled staking vault apy averages have been updated.
115
+ */
116
+ refreshPooledStakingVaultApyAverages(): Promise<void>;
99
117
  /**
100
118
  * Refreshes all pooled staking related data including stakes, eligibility, and vault data.
101
119
  * This method allows partial success, meaning some data may update while other requests fail.
102
120
  * All errors are collected and thrown as a single error message.
103
121
  *
122
+ * @param resetCache - Control whether the BE cache should be invalidated.
104
123
  * @returns A promise that resolves when all possible data has been updated
105
124
  * @throws {Error} If any of the refresh operations fail, with concatenated error messages
106
125
  */
107
- refreshPooledStakingData(): Promise<void>;
126
+ refreshPooledStakingData(resetCache?: boolean): Promise<void>;
108
127
  }
109
128
  //# sourceMappingURL=EarnController.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"EarnController.d.cts","sourceRoot":"","sources":["../src/EarnController.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,0CAA0C,EAC1C,4CAA4C,EAC7C,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,KAAK,EAAE,iCAAiC,EAAE,qCAAqC;AACtF,OAAO,KAAK,EACV,2CAA2C,EAC3C,+BAA+B,EAChC,qCAAqC;AACtC,OAAO,EAGL,KAAK,WAAW,EAEhB,KAAK,SAAS,EACf,4BAA4B;AAE7B,eAAO,MAAM,cAAc,mBAAmB,CAAC;AAE/C,MAAM,MAAM,kBAAkB,GAAG;IAC/B,YAAY,EAAE,WAAW,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,SAAS,CAAC;IACrB,UAAU,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACnC,MAAM,EAAE,eAAe,EAAE,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAqBF,MAAM,MAAM,mBAAmB,GAAG;IAChC,cAAc,EAAE,kBAAkB,CAAC;IACnC,kBAAkB,CAAC,EAAE,sBAAsB,CAAC;IAC5C,WAAW,EAAE,MAAM,CAAC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,GAAE,KAAK,GAAG,MAAe,GAC7B,OAAO,CAAC,IAAI,CAAC;IAahB;;;;;OAKG;IACG,oCAAoC;IAU1C;;;;;;;;OAQG;IACG,wBAAwB,CAAC,UAAU,UAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;CA6BlE"}
@@ -1,14 +1,15 @@
1
1
  import type { AccountsControllerGetSelectedAccountAction, AccountsControllerSelectedAccountChangeEvent } from "@metamask/accounts-controller";
2
2
  import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
3
3
  import { BaseController } from "@metamask/base-controller";
4
- import type { NetworkControllerStateChangeEvent } from "@metamask/network-controller";
5
- import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction } from "@metamask/network-controller";
6
- import { type PooledStake, type VaultData } from "@metamask/stake-sdk";
4
+ import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
5
+ import { type PooledStake, type VaultData, type VaultDailyApy, type VaultApyAverages } from "@metamask/stake-sdk";
7
6
  export declare const controllerName = "EarnController";
8
7
  export type PooledStakingState = {
9
8
  pooledStakes: PooledStake;
10
9
  exchangeRate: string;
11
- vaultData: VaultData;
10
+ vaultMetadata: VaultData;
11
+ vaultDailyApys: VaultDailyApy[];
12
+ vaultApyAverages: VaultApyAverages;
12
13
  isEligible: boolean;
13
14
  };
14
15
  export type StablecoinLendingState = {
@@ -78,9 +79,10 @@ export declare class EarnController extends BaseController<typeof controllerName
78
79
  * Fetches updated stake information including lifetime rewards, assets, and exit requests
79
80
  * from the staking API service and updates the state.
80
81
  *
82
+ * @param resetCache - Control whether the BE cache should be invalidated.
81
83
  * @returns A promise that resolves when the stakes data has been updated
82
84
  */
83
- refreshPooledStakes(): Promise<void>;
85
+ refreshPooledStakes(resetCache?: boolean): Promise<void>;
84
86
  /**
85
87
  * Refreshes the staking eligibility status for the current account.
86
88
  * Updates the eligibility status in the controller state based on the location and address blocklist for compliance.
@@ -89,21 +91,38 @@ export declare class EarnController extends BaseController<typeof controllerName
89
91
  */
90
92
  refreshStakingEligibility(): Promise<void>;
91
93
  /**
92
- * Refreshes vault data for the current chain.
93
- * Updates the vault data in the controller state including APY, capacity,
94
+ * Refreshes pooled staking vault metadata for the current chain.
95
+ * Updates the vault metadata in the controller state including APY, capacity,
94
96
  * fee percentage, total assets, and vault address.
95
97
  *
96
- * @returns A promise that resolves when the vault data has been updated
98
+ * @returns A promise that resolves when the vault metadata has been updated
97
99
  */
98
- refreshVaultData(): Promise<void>;
100
+ refreshPooledStakingVaultMetadata(): Promise<void>;
101
+ /**
102
+ * Refreshes pooled staking vault daily apys for the current chain.
103
+ * Updates the pooled staking vault daily apys controller state.
104
+ *
105
+ * @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 30).
106
+ * @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').
107
+ * @returns A promise that resolves when the pooled staking vault daily apys have been updated.
108
+ */
109
+ refreshPooledStakingVaultDailyApys(days?: number, order?: 'asc' | 'desc'): Promise<void>;
110
+ /**
111
+ * Refreshes pooled staking vault apy averages for the current chain.
112
+ * Updates the pooled staking vault apy averages controller state.
113
+ *
114
+ * @returns A promise that resolves when the pooled staking vault apy averages have been updated.
115
+ */
116
+ refreshPooledStakingVaultApyAverages(): Promise<void>;
99
117
  /**
100
118
  * Refreshes all pooled staking related data including stakes, eligibility, and vault data.
101
119
  * This method allows partial success, meaning some data may update while other requests fail.
102
120
  * All errors are collected and thrown as a single error message.
103
121
  *
122
+ * @param resetCache - Control whether the BE cache should be invalidated.
104
123
  * @returns A promise that resolves when all possible data has been updated
105
124
  * @throws {Error} If any of the refresh operations fail, with concatenated error messages
106
125
  */
107
- refreshPooledStakingData(): Promise<void>;
126
+ refreshPooledStakingData(resetCache?: boolean): Promise<void>;
108
127
  }
109
128
  //# sourceMappingURL=EarnController.d.mts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ACH,MAAM,MAAM,4BAA4B,GAAG,wBAAwB,CACjE,OAAO,cAAc,EACrB,mBAAmB,CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,4BAA4B,CAAC;AAEjE;;GAEG;AACH,MAAM,MAAM,cAAc,GACtB,2CAA2C,GAC3C,+BAA+B,GAC/B,0CAA0C,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG,0BAA0B,CACrE,OAAO,cAAc,EACrB,mBAAmB,CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,8BAA8B,CAAC;AAElE;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB,4CAA4C,GAC5C,iCAAiC,CAAC;AAEtC;;;GAGG;AACH,MAAM,MAAM,uBAAuB,GAAG,mBAAmB,CACvD,OAAO,cAAc,EACrB,qBAAqB,GAAG,cAAc,EACtC,oBAAoB,GAAG,aAAa,EACpC,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAIF;;GAEG;AACH,qBAAa,cAAe,SAAQ,cAAc,CAChD,OAAO,cAAc,EACrB,mBAAmB,EACnB,uBAAuB,CACxB;;gBAOa,EACV,SAAS,EACT,KAAU,GACX,EAAE;QACD,SAAS,EAAE,uBAAuB,CAAC;QACnC,KAAK,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,CAAC;KACtC;IAoGD;;;;;;OAMG;IACG,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC;IAoB1C;;;;;OAKG;IACG,yBAAyB,IAAI,OAAO,CAAC,IAAI,CAAC;IAgBhD;;;;;;OAMG;IACG,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IASvC;;;;;;;OAOG;IACG,wBAAwB,IAAI,OAAO,CAAC,IAAI,CAAC;CAuBhD"}
1
+ {"version":3,"file":"EarnController.d.mts","sourceRoot":"","sources":["../src/EarnController.ts"],"names":[],"mappings":"AACA,O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IAU1C;;;;;;;;OAQG;IACG,wBAAwB,CAAC,UAAU,UAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;CA6BlE"}
@@ -43,6 +43,14 @@ const DEFAULT_STABLECOIN_VAULT = {
43
43
  supply: '0',
44
44
  liquidity: '0',
45
45
  };
46
+ const DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES = {
47
+ oneDay: '0',
48
+ oneWeek: '0',
49
+ oneMonth: '0',
50
+ threeMonths: '0',
51
+ sixMonths: '0',
52
+ oneYear: '0',
53
+ };
46
54
  /**
47
55
  * Gets the default state for the EarnController.
48
56
  *
@@ -58,13 +66,15 @@ export function getDefaultEarnControllerState() {
58
66
  exitRequests: [],
59
67
  },
60
68
  exchangeRate: '1',
61
- vaultData: {
69
+ vaultMetadata: {
62
70
  apy: '0',
63
71
  capacity: '0',
64
72
  feePercent: 0,
65
73
  totalAssets: '0',
66
74
  vaultAddress: '0x0000000000000000000000000000000000000000',
67
75
  },
76
+ vaultDailyApys: [],
77
+ vaultApyAverages: DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES,
68
78
  isEligible: false,
69
79
  },
70
80
  stablecoin_lending: {
@@ -100,13 +110,17 @@ export class EarnController extends BaseController {
100
110
  if (networkControllerState.selectedNetworkClientId !==
101
111
  __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
102
112
  __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this, networkControllerState.selectedNetworkClientId);
103
- this.refreshPooledStakingData().catch(console.error);
113
+ this.refreshPooledStakingVaultMetadata().catch(console.error);
114
+ this.refreshPooledStakingVaultDailyApys().catch(console.error);
115
+ this.refreshPooledStakingVaultApyAverages().catch(console.error);
116
+ this.refreshPooledStakes().catch(console.error);
104
117
  }
105
118
  __classPrivateFieldSet(this, _EarnController_selectedNetworkClientId, networkControllerState.selectedNetworkClientId, "f");
106
119
  });
107
120
  // Listen for account changes
108
121
  this.messagingSystem.subscribe('AccountsController:selectedAccountChange', () => {
109
- this.refreshPooledStakingData().catch(console.error);
122
+ this.refreshStakingEligibility().catch(console.error);
123
+ this.refreshPooledStakes().catch(console.error);
110
124
  });
111
125
  }
112
126
  /**
@@ -114,15 +128,16 @@ export class EarnController extends BaseController {
114
128
  * Fetches updated stake information including lifetime rewards, assets, and exit requests
115
129
  * from the staking API service and updates the state.
116
130
  *
131
+ * @param resetCache - Control whether the BE cache should be invalidated.
117
132
  * @returns A promise that resolves when the stakes data has been updated
118
133
  */
119
- async refreshPooledStakes() {
134
+ async refreshPooledStakes(resetCache = false) {
120
135
  const currentAccount = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this);
121
136
  if (!currentAccount?.address) {
122
137
  return;
123
138
  }
124
139
  const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
125
- const { accounts, exchangeRate } = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getPooledStakes([currentAccount.address], chainId);
140
+ const { accounts, exchangeRate } = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getPooledStakes([currentAccount.address], chainId, resetCache);
126
141
  this.update((state) => {
127
142
  state.pooled_staking.pooledStakes = accounts[0];
128
143
  state.pooled_staking.exchangeRate = exchangeRate;
@@ -147,17 +162,45 @@ export class EarnController extends BaseController {
147
162
  });
148
163
  }
149
164
  /**
150
- * Refreshes vault data for the current chain.
151
- * Updates the vault data in the controller state including APY, capacity,
165
+ * Refreshes pooled staking vault metadata for the current chain.
166
+ * Updates the vault metadata in the controller state including APY, capacity,
152
167
  * fee percentage, total assets, and vault address.
153
168
  *
154
- * @returns A promise that resolves when the vault data has been updated
169
+ * @returns A promise that resolves when the vault metadata has been updated
170
+ */
171
+ async refreshPooledStakingVaultMetadata() {
172
+ const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
173
+ const vaultMetadata = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultData(chainId);
174
+ this.update((state) => {
175
+ state.pooled_staking.vaultMetadata = vaultMetadata;
176
+ });
177
+ }
178
+ /**
179
+ * Refreshes pooled staking vault daily apys for the current chain.
180
+ * Updates the pooled staking vault daily apys controller state.
181
+ *
182
+ * @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 30).
183
+ * @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').
184
+ * @returns A promise that resolves when the pooled staking vault daily apys have been updated.
155
185
  */
156
- async refreshVaultData() {
186
+ async refreshPooledStakingVaultDailyApys(days = 30, order = 'desc') {
157
187
  const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
158
- const vaultData = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultData(chainId);
188
+ const vaultDailyApys = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultDailyApys(chainId, days, order);
159
189
  this.update((state) => {
160
- state.pooled_staking.vaultData = vaultData;
190
+ state.pooled_staking.vaultDailyApys = vaultDailyApys;
191
+ });
192
+ }
193
+ /**
194
+ * Refreshes pooled staking vault apy averages for the current chain.
195
+ * Updates the pooled staking vault apy averages controller state.
196
+ *
197
+ * @returns A promise that resolves when the pooled staking vault apy averages have been updated.
198
+ */
199
+ async refreshPooledStakingVaultApyAverages() {
200
+ const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
201
+ const vaultApyAverages = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultApyAverages(chainId);
202
+ this.update((state) => {
203
+ state.pooled_staking.vaultApyAverages = vaultApyAverages;
161
204
  });
162
205
  }
163
206
  /**
@@ -165,19 +208,26 @@ export class EarnController extends BaseController {
165
208
  * This method allows partial success, meaning some data may update while other requests fail.
166
209
  * All errors are collected and thrown as a single error message.
167
210
  *
211
+ * @param resetCache - Control whether the BE cache should be invalidated.
168
212
  * @returns A promise that resolves when all possible data has been updated
169
213
  * @throws {Error} If any of the refresh operations fail, with concatenated error messages
170
214
  */
171
- async refreshPooledStakingData() {
215
+ async refreshPooledStakingData(resetCache = false) {
172
216
  const errors = [];
173
217
  await Promise.all([
174
- this.refreshPooledStakes().catch((error) => {
218
+ this.refreshPooledStakes(resetCache).catch((error) => {
175
219
  errors.push(error);
176
220
  }),
177
221
  this.refreshStakingEligibility().catch((error) => {
178
222
  errors.push(error);
179
223
  }),
180
- this.refreshVaultData().catch((error) => {
224
+ this.refreshPooledStakingVaultMetadata().catch((error) => {
225
+ errors.push(error);
226
+ }),
227
+ this.refreshPooledStakingVaultDailyApys().catch((error) => {
228
+ errors.push(error);
229
+ }),
230
+ this.refreshPooledStakingVaultApyAverages().catch((error) => {
181
231
  errors.push(error);
182
232
  }),
183
233
  ]);
@@ -1 +1 @@
1
- {"version":3,"file":"EarnController.mjs","sourceRoot":"","sources":["../src/EarnController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,YAAY,EAAE,iCAAiC;AAWxD,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,mCAAmC;AAMjE,OAAO,EACL,QAAQ,EACR,iBAAiB,EAIlB,4BAA4B;AAE7B,MAAM,CAAC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAwB/C;;GAEG;AACH,MAAM,sBAAsB,GAAuC;IACjE,cAAc,EAAE;QACd,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;IACD,kBAAkB,EAAE;QAClB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;IACD,WAAW,EAAE;QACX,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,IAAI;KAChB;CACF,CAAC;AASF,wBAAwB;AACxB,MAAM,wBAAwB,GAAoB;IAChD,MAAM,EAAE,EAAE;IACV,IAAI,EAAE,EAAE;IACR,OAAO,EAAE,CAAC;IACV,YAAY,E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,IAAI,4BAAa,IAAI,MAAA,CAAC;QACtB,2DAA2D;QAC3D,IACE,CAAC,CACC,KAAK,YAAY,KAAK;YACtB,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAC9C,EACD;YACA,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,KAAK,CAAC,CAAC;SAC1D;KACF;AACH,CAAC;IAGC,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,uCAAuC,CAAC,CAAC;AAC5E,CAAC;IAGC,MAAM,EAAE,uBAAuB,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3D,4BAA4B,CAC7B,CAAC;IACF,MAAM,EACJ,aAAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3B,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IACF,OAAO,mBAAmB,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC","sourcesContent":["import { Web3Provider } from '@ethersproject/providers';\nimport type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport type { NetworkControllerStateChangeEvent } from '@metamask/network-controller';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n} from '@metamask/network-controller';\nimport {\n StakeSdk,\n StakingApiService,\n type PooledStake,\n type StakeSdkConfig,\n type VaultData,\n} from '@metamask/stake-sdk';\n\nexport const controllerName = 'EarnController';\n\nexport type PooledStakingState = {\n pooledStakes: PooledStake;\n exchangeRate: string;\n vaultData: VaultData;\n isEligible: boolean;\n};\n\nexport type StablecoinLendingState = {\n vaults: StablecoinVault[];\n};\n\nexport type StablecoinVault = {\n symbol: string;\n name: string;\n chainId: number;\n tokenAddress: string;\n vaultAddress: string;\n currentAPY: string;\n supply: string;\n liquidity: string;\n};\n\n/**\n * Metadata for the EarnController.\n */\nconst earnControllerMetadata: StateMetadata<EarnControllerState> = {\n pooled_staking: {\n persist: true,\n anonymous: false,\n },\n stablecoin_lending: {\n persist: true,\n anonymous: false,\n },\n lastUpdated: {\n persist: false,\n anonymous: true,\n },\n};\n\n// === State Types ===\nexport type EarnControllerState = {\n pooled_staking: PooledStakingState;\n stablecoin_lending?: StablecoinLendingState;\n lastUpdated: number;\n};\n\n// === Default State ===\nconst DEFAULT_STABLECOIN_VAULT: StablecoinVault = {\n symbol: '',\n name: '',\n chainId: 0,\n tokenAddress: '',\n vaultAddress: '',\n currentAPY: '0',\n supply: '0',\n liquidity: '0',\n};\n\n/**\n * Gets the default state for the EarnController.\n *\n * @returns The default EarnController state.\n */\nexport function getDefaultEarnControllerState(): EarnControllerState {\n return {\n pooled_staking: {\n pooledStakes: {\n account: '',\n lifetimeRewards: '0',\n assets: '0',\n exitRequests: [],\n },\n exchangeRate: '1',\n vaultData: {\n apy: '0',\n capacity: '0',\n feePercent: 0,\n totalAssets: '0',\n vaultAddress: '0x0000000000000000000000000000000000000000',\n },\n isEligible: false,\n },\n stablecoin_lending: {\n vaults: [DEFAULT_STABLECOIN_VAULT],\n },\n lastUpdated: 0,\n };\n}\n\n// === MESSENGER ===\n\n/**\n * The action which can be used to retrieve the state of the EarnController.\n */\nexport type EarnControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n EarnControllerState\n>;\n\n/**\n * All actions that EarnController registers, to be called externally.\n */\nexport type EarnControllerActions = EarnControllerGetStateAction;\n\n/**\n * All actions that EarnController calls internally.\n */\nexport type AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction;\n\n/**\n * The event that EarnController publishes when updating state.\n */\nexport type EarnControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n EarnControllerState\n>;\n\n/**\n * All events that EarnController publishes, to be subscribed to externally.\n */\nexport type EarnControllerEvents = EarnControllerStateChangeEvent;\n\n/**\n * All events that EarnController subscribes to internally.\n */\nexport type AllowedEvents =\n | AccountsControllerSelectedAccountChangeEvent\n | NetworkControllerStateChangeEvent;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * EarnController.\n */\nexport type EarnControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n EarnControllerActions | AllowedActions,\n EarnControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * EarnController manages DeFi earning opportunities across different protocols and chains.\n */\nexport class EarnController extends BaseController<\n typeof controllerName,\n EarnControllerState,\n EarnControllerMessenger\n> {\n #stakeSDK: StakeSdk | null = null;\n\n #selectedNetworkClientId?: string;\n\n readonly #stakingApiService: StakingApiService = new StakingApiService();\n\n constructor({\n messenger,\n state = {},\n }: {\n messenger: EarnControllerMessenger;\n state?: Partial<EarnControllerState>;\n }) {\n super({\n name: controllerName,\n metadata: earnControllerMetadata,\n messenger,\n state: {\n ...getDefaultEarnControllerState(),\n ...state,\n },\n });\n\n this.#initializeSDK();\n this.refreshPooledStakingData().catch(console.error);\n\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n this.#selectedNetworkClientId = selectedNetworkClientId;\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n (networkControllerState) => {\n if (\n networkControllerState.selectedNetworkClientId !==\n this.#selectedNetworkClientId\n ) {\n this.#initializeSDK(networkControllerState.selectedNetworkClientId);\n this.refreshPooledStakingData().catch(console.error);\n }\n this.#selectedNetworkClientId =\n networkControllerState.selectedNetworkClientId;\n },\n );\n\n // Listen for account changes\n this.messagingSystem.subscribe(\n 'AccountsController:selectedAccountChange',\n () => {\n this.refreshPooledStakingData().catch(console.error);\n },\n );\n }\n\n #initializeSDK(networkClientId?: string) {\n const { selectedNetworkClientId } = networkClientId\n ? { selectedNetworkClientId: networkClientId }\n : this.messagingSystem.call('NetworkController:getState');\n\n const networkClient = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (!networkClient?.provider) {\n this.#stakeSDK = null;\n return;\n }\n\n const provider = new Web3Provider(networkClient.provider);\n const { chainId } = networkClient.configuration;\n\n // Initialize appropriate contracts based on chainId\n const config: StakeSdkConfig = {\n chainId: convertHexToDecimal(chainId),\n };\n\n try {\n this.#stakeSDK = StakeSdk.create(config);\n this.#stakeSDK.pooledStakingContract.connectSignerOrProvider(provider);\n } catch (error) {\n this.#stakeSDK = null;\n // Only log unexpected errors, not unsupported chain errors\n if (\n !(\n error instanceof Error &&\n error.message.includes('Unsupported chainId')\n )\n ) {\n console.error('Stake SDK initialization failed:', error);\n }\n }\n }\n\n #getCurrentAccount() {\n return this.messagingSystem.call('AccountsController:getSelectedAccount');\n }\n\n #getCurrentChainId(): number {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return convertHexToDecimal(chainId);\n }\n\n /**\n * Refreshes the pooled stakes data for the current account.\n * Fetches updated stake information including lifetime rewards, assets, and exit requests\n * from the staking API service and updates the state.\n *\n * @returns A promise that resolves when the stakes data has been updated\n */\n async refreshPooledStakes(): Promise<void> {\n const currentAccount = this.#getCurrentAccount();\n if (!currentAccount?.address) {\n return;\n }\n\n const chainId = this.#getCurrentChainId();\n\n const { accounts, exchangeRate } =\n await this.#stakingApiService.getPooledStakes(\n [currentAccount.address],\n chainId,\n );\n\n this.update((state) => {\n state.pooled_staking.pooledStakes = accounts[0];\n state.pooled_staking.exchangeRate = exchangeRate;\n });\n }\n\n /**\n * Refreshes the staking eligibility status for the current account.\n * Updates the eligibility status in the controller state based on the location and address blocklist for compliance.\n *\n * @returns A promise that resolves when the eligibility status has been updated\n */\n async refreshStakingEligibility(): Promise<void> {\n const currentAccount = this.#getCurrentAccount();\n if (!currentAccount?.address) {\n return;\n }\n\n const { eligible: isEligible } =\n await this.#stakingApiService.getPooledStakingEligibility([\n currentAccount.address,\n ]);\n\n this.update((state) => {\n state.pooled_staking.isEligible = isEligible;\n });\n }\n\n /**\n * Refreshes vault data for the current chain.\n * Updates the vault data in the controller state including APY, capacity,\n * fee percentage, total assets, and vault address.\n *\n * @returns A promise that resolves when the vault data has been updated\n */\n async refreshVaultData(): Promise<void> {\n const chainId = this.#getCurrentChainId();\n const vaultData = await this.#stakingApiService.getVaultData(chainId);\n\n this.update((state) => {\n state.pooled_staking.vaultData = vaultData;\n });\n }\n\n /**\n * Refreshes all pooled staking related data including stakes, eligibility, and vault data.\n * This method allows partial success, meaning some data may update while other requests fail.\n * All errors are collected and thrown as a single error message.\n *\n * @returns A promise that resolves when all possible data has been updated\n * @throws {Error} If any of the refresh operations fail, with concatenated error messages\n */\n async refreshPooledStakingData(): Promise<void> {\n const errors: Error[] = [];\n\n await Promise.all([\n this.refreshPooledStakes().catch((error) => {\n errors.push(error);\n }),\n this.refreshStakingEligibility().catch((error) => {\n errors.push(error);\n }),\n this.refreshVaultData().catch((error) => {\n errors.push(error);\n }),\n ]);\n\n if (errors.length > 0) {\n throw new Error(\n `Failed to refresh some staking data: ${errors\n .map((e) => e.message)\n .join(', ')}`,\n );\n }\n }\n}\n"]}
1
+ {"version":3,"file":"EarnController.mjs","sourceRoot":"","sources":["../src/EarnController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,YAAY,EAAE,iCAAiC;AAWxD,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,mCAAmC;AAMjE,OAAO,EACL,QAAQ,EACR,iBAAiB,EAMlB,4BAA4B;AAE7B,MAAM,CAAC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AA0B/C;;GAEG;AACH,MAA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C7B,CAAC;IACF,MAAM,EACJ,aAAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3B,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IACF,OAAO,mBAAmB,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC","sourcesContent":["import { Web3Provider } from '@ethersproject/providers';\nimport type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport {\n StakeSdk,\n StakingApiService,\n type PooledStake,\n type StakeSdkConfig,\n type VaultData,\n type VaultDailyApy,\n type VaultApyAverages,\n} from '@metamask/stake-sdk';\n\nexport const controllerName = 'EarnController';\n\nexport type PooledStakingState = {\n pooledStakes: PooledStake;\n exchangeRate: string;\n vaultMetadata: VaultData;\n vaultDailyApys: VaultDailyApy[];\n vaultApyAverages: VaultApyAverages;\n isEligible: boolean;\n};\n\nexport type StablecoinLendingState = {\n vaults: StablecoinVault[];\n};\n\nexport type StablecoinVault = {\n symbol: string;\n name: string;\n chainId: number;\n tokenAddress: string;\n vaultAddress: string;\n currentAPY: string;\n supply: string;\n liquidity: string;\n};\n\n/**\n * Metadata for the EarnController.\n */\nconst earnControllerMetadata: StateMetadata<EarnControllerState> = {\n pooled_staking: {\n persist: true,\n anonymous: false,\n },\n stablecoin_lending: {\n persist: true,\n anonymous: false,\n },\n lastUpdated: {\n persist: false,\n anonymous: true,\n },\n};\n\n// === State Types ===\nexport type EarnControllerState = {\n pooled_staking: PooledStakingState;\n stablecoin_lending?: StablecoinLendingState;\n lastUpdated: number;\n};\n\n// === Default State ===\nconst DEFAULT_STABLECOIN_VAULT: StablecoinVault = {\n symbol: '',\n name: '',\n chainId: 0,\n tokenAddress: '',\n vaultAddress: '',\n currentAPY: '0',\n supply: '0',\n liquidity: '0',\n};\n\nconst DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES: VaultApyAverages = {\n oneDay: '0',\n oneWeek: '0',\n oneMonth: '0',\n threeMonths: '0',\n sixMonths: '0',\n oneYear: '0',\n};\n\n/**\n * Gets the default state for the EarnController.\n *\n * @returns The default EarnController state.\n */\nexport function getDefaultEarnControllerState(): EarnControllerState {\n return {\n pooled_staking: {\n pooledStakes: {\n account: '',\n lifetimeRewards: '0',\n assets: '0',\n exitRequests: [],\n },\n exchangeRate: '1',\n vaultMetadata: {\n apy: '0',\n capacity: '0',\n feePercent: 0,\n totalAssets: '0',\n vaultAddress: '0x0000000000000000000000000000000000000000',\n },\n vaultDailyApys: [],\n vaultApyAverages: DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES,\n isEligible: false,\n },\n stablecoin_lending: {\n vaults: [DEFAULT_STABLECOIN_VAULT],\n },\n lastUpdated: 0,\n };\n}\n\n// === MESSENGER ===\n\n/**\n * The action which can be used to retrieve the state of the EarnController.\n */\nexport type EarnControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n EarnControllerState\n>;\n\n/**\n * All actions that EarnController registers, to be called externally.\n */\nexport type EarnControllerActions = EarnControllerGetStateAction;\n\n/**\n * All actions that EarnController calls internally.\n */\nexport type AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction;\n\n/**\n * The event that EarnController publishes when updating state.\n */\nexport type EarnControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n EarnControllerState\n>;\n\n/**\n * All events that EarnController publishes, to be subscribed to externally.\n */\nexport type EarnControllerEvents = EarnControllerStateChangeEvent;\n\n/**\n * All events that EarnController subscribes to internally.\n */\nexport type AllowedEvents =\n | AccountsControllerSelectedAccountChangeEvent\n | NetworkControllerStateChangeEvent;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * EarnController.\n */\nexport type EarnControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n EarnControllerActions | AllowedActions,\n EarnControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * EarnController manages DeFi earning opportunities across different protocols and chains.\n */\nexport class EarnController extends BaseController<\n typeof controllerName,\n EarnControllerState,\n EarnControllerMessenger\n> {\n #stakeSDK: StakeSdk | null = null;\n\n #selectedNetworkClientId?: string;\n\n readonly #stakingApiService: StakingApiService = new StakingApiService();\n\n constructor({\n messenger,\n state = {},\n }: {\n messenger: EarnControllerMessenger;\n state?: Partial<EarnControllerState>;\n }) {\n super({\n name: controllerName,\n metadata: earnControllerMetadata,\n messenger,\n state: {\n ...getDefaultEarnControllerState(),\n ...state,\n },\n });\n\n this.#initializeSDK();\n this.refreshPooledStakingData().catch(console.error);\n\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n this.#selectedNetworkClientId = selectedNetworkClientId;\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n (networkControllerState) => {\n if (\n networkControllerState.selectedNetworkClientId !==\n this.#selectedNetworkClientId\n ) {\n this.#initializeSDK(networkControllerState.selectedNetworkClientId);\n this.refreshPooledStakingVaultMetadata().catch(console.error);\n this.refreshPooledStakingVaultDailyApys().catch(console.error);\n this.refreshPooledStakingVaultApyAverages().catch(console.error);\n this.refreshPooledStakes().catch(console.error);\n }\n this.#selectedNetworkClientId =\n networkControllerState.selectedNetworkClientId;\n },\n );\n\n // Listen for account changes\n this.messagingSystem.subscribe(\n 'AccountsController:selectedAccountChange',\n () => {\n this.refreshStakingEligibility().catch(console.error);\n this.refreshPooledStakes().catch(console.error);\n },\n );\n }\n\n #initializeSDK(networkClientId?: string) {\n const { selectedNetworkClientId } = networkClientId\n ? { selectedNetworkClientId: networkClientId }\n : this.messagingSystem.call('NetworkController:getState');\n\n const networkClient = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (!networkClient?.provider) {\n this.#stakeSDK = null;\n return;\n }\n\n const provider = new Web3Provider(networkClient.provider);\n const { chainId } = networkClient.configuration;\n\n // Initialize appropriate contracts based on chainId\n const config: StakeSdkConfig = {\n chainId: convertHexToDecimal(chainId),\n };\n\n try {\n this.#stakeSDK = StakeSdk.create(config);\n this.#stakeSDK.pooledStakingContract.connectSignerOrProvider(provider);\n } catch (error) {\n this.#stakeSDK = null;\n // Only log unexpected errors, not unsupported chain errors\n if (\n !(\n error instanceof Error &&\n error.message.includes('Unsupported chainId')\n )\n ) {\n console.error('Stake SDK initialization failed:', error);\n }\n }\n }\n\n #getCurrentAccount() {\n return this.messagingSystem.call('AccountsController:getSelectedAccount');\n }\n\n #getCurrentChainId(): number {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return convertHexToDecimal(chainId);\n }\n\n /**\n * Refreshes the pooled stakes data for the current account.\n * Fetches updated stake information including lifetime rewards, assets, and exit requests\n * from the staking API service and updates the state.\n *\n * @param resetCache - Control whether the BE cache should be invalidated.\n * @returns A promise that resolves when the stakes data has been updated\n */\n async refreshPooledStakes(resetCache = false): Promise<void> {\n const currentAccount = this.#getCurrentAccount();\n if (!currentAccount?.address) {\n return;\n }\n\n const chainId = this.#getCurrentChainId();\n\n const { accounts, exchangeRate } =\n await this.#stakingApiService.getPooledStakes(\n [currentAccount.address],\n chainId,\n resetCache,\n );\n\n this.update((state) => {\n state.pooled_staking.pooledStakes = accounts[0];\n state.pooled_staking.exchangeRate = exchangeRate;\n });\n }\n\n /**\n * Refreshes the staking eligibility status for the current account.\n * Updates the eligibility status in the controller state based on the location and address blocklist for compliance.\n *\n * @returns A promise that resolves when the eligibility status has been updated\n */\n async refreshStakingEligibility(): Promise<void> {\n const currentAccount = this.#getCurrentAccount();\n if (!currentAccount?.address) {\n return;\n }\n\n const { eligible: isEligible } =\n await this.#stakingApiService.getPooledStakingEligibility([\n currentAccount.address,\n ]);\n\n this.update((state) => {\n state.pooled_staking.isEligible = isEligible;\n });\n }\n\n /**\n * Refreshes pooled staking vault metadata for the current chain.\n * Updates the vault metadata in the controller state including APY, capacity,\n * fee percentage, total assets, and vault address.\n *\n * @returns A promise that resolves when the vault metadata has been updated\n */\n async refreshPooledStakingVaultMetadata(): Promise<void> {\n const chainId = this.#getCurrentChainId();\n const vaultMetadata = await this.#stakingApiService.getVaultData(chainId);\n\n this.update((state) => {\n state.pooled_staking.vaultMetadata = vaultMetadata;\n });\n }\n\n /**\n * Refreshes pooled staking vault daily apys for the current chain.\n * Updates the pooled staking vault daily apys controller state.\n *\n * @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 30).\n * @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').\n * @returns A promise that resolves when the pooled staking vault daily apys have been updated.\n */\n async refreshPooledStakingVaultDailyApys(\n days = 30,\n order: 'asc' | 'desc' = 'desc',\n ): Promise<void> {\n const chainId = this.#getCurrentChainId();\n const vaultDailyApys = await this.#stakingApiService.getVaultDailyApys(\n chainId,\n days,\n order,\n );\n\n this.update((state) => {\n state.pooled_staking.vaultDailyApys = vaultDailyApys;\n });\n }\n\n /**\n * Refreshes pooled staking vault apy averages for the current chain.\n * Updates the pooled staking vault apy averages controller state.\n *\n * @returns A promise that resolves when the pooled staking vault apy averages have been updated.\n */\n async refreshPooledStakingVaultApyAverages() {\n const chainId = this.#getCurrentChainId();\n const vaultApyAverages =\n await this.#stakingApiService.getVaultApyAverages(chainId);\n\n this.update((state) => {\n state.pooled_staking.vaultApyAverages = vaultApyAverages;\n });\n }\n\n /**\n * Refreshes all pooled staking related data including stakes, eligibility, and vault data.\n * This method allows partial success, meaning some data may update while other requests fail.\n * All errors are collected and thrown as a single error message.\n *\n * @param resetCache - Control whether the BE cache should be invalidated.\n * @returns A promise that resolves when all possible data has been updated\n * @throws {Error} If any of the refresh operations fail, with concatenated error messages\n */\n async refreshPooledStakingData(resetCache = false): Promise<void> {\n const errors: Error[] = [];\n\n await Promise.all([\n this.refreshPooledStakes(resetCache).catch((error) => {\n errors.push(error);\n }),\n this.refreshStakingEligibility().catch((error) => {\n errors.push(error);\n }),\n this.refreshPooledStakingVaultMetadata().catch((error) => {\n errors.push(error);\n }),\n this.refreshPooledStakingVaultDailyApys().catch((error) => {\n errors.push(error);\n }),\n this.refreshPooledStakingVaultApyAverages().catch((error) => {\n errors.push(error);\n }),\n ]);\n\n if (errors.length > 0) {\n throw new Error(\n `Failed to refresh some staking data: ${errors\n .map((e) => e.message)\n .join(', ')}`,\n );\n }\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/earn-controller",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Manages state for earning features and coordinates interactions between staking services, SDK integrations, and other controllers to enable users to participate in various earning opportunities",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -53,7 +53,7 @@
53
53
  "@metamask/stake-sdk": "^1.0.0"
54
54
  },
55
55
  "devDependencies": {
56
- "@metamask/accounts-controller": "^24.0.0",
56
+ "@metamask/accounts-controller": "^24.0.1",
57
57
  "@metamask/auto-changelog": "^3.4.4",
58
58
  "@metamask/network-controller": "^22.2.1",
59
59
  "@types/jest": "^27.4.1",