Skip to content

Commit 691b9dc

Browse files
authored
feat: MUSD-626 update earn controller to follow init pattern (#8421)
## Explanation `EarnController` previously performed async work (SDK initialization, data refreshing) directly in its constructor and required `selectedNetworkClientId` to be passed in as a constructor argument — storing derived state that was already available from `NetworkController`. This PR refactors `EarnController` to follow the standard init pattern used across the codebase: - Removes `selectedNetworkClientId` from the constructor options; the controller now reads it on-demand from `NetworkController:getState`. - Adds an `init()` method that performs the async startup work (SDK initialization, pooled staking data refresh, lending data refresh, eligibility refresh) that was previously triggered from the constructor. - Adds a `deferAccountDependentRefreshes()` method to handle refreshes that depend on account state being ready, subscribed to `AccountTreeController:stateChanged`. - Removes the now-redundant stored `#selectedNetworkClientId` field. - Deduplicates eligibility refresh calls that were previously triggered from multiple code paths. ## Breaking Changes - Removed `selectedNetworkClientId` from the `EarnController` constructor. - Added `NetworkController:getState` action to `EarnController` - Added `AccountTreeController:stateChange` event `EarnController` ## References - Fixes [MUSD-626: Update EarnController to follow .init() pattern](https://consensyssoftware.atlassian.net/browse/MUSD-626) ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by updating changelogs for packages I've changed - [ ] I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Introduces breaking API/behavior changes to `EarnController` initialization and refresh flow; consumers must now call `init()` and update messenger policies, and incorrect sequencing could lead to missing initial data refreshes. > > **Overview** > **Refactors `EarnController` to follow an explicit `init()` pattern.** Async SDK initialization and initial data refreshes are removed from the constructor; consumers must call `init()`, which deduplicates concurrent calls and supports retry on failure. > > **Changes how network/account state is sourced and when refreshes run.** `selectedNetworkClientId` is no longer stored/passed in; it’s read via `NetworkController:getState`, and when no EVM account is available at init time the controller defers portfolio refresh until a one-time `AccountTreeController:stateChange` provides a usable selected account group. > > **Eligibility/refresh API adjustments (breaking).** `refreshPooledStakingData`/`refreshLendingData` no longer perform eligibility checks internally, and the `EarnController:refreshLendingEligibility` action/type (and related options type) are removed in favor of `refreshEarnEligibility`; tests and changelog are updated accordingly. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit b3fcdef. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 086b7f0 commit 691b9dc

7 files changed

Lines changed: 452 additions & 142 deletions

File tree

eslint-suppressions.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -929,22 +929,19 @@
929929
}
930930
},
931931
"packages/earn-controller/src/EarnController.test.ts": {
932-
"@typescript-eslint/explicit-function-return-type": {
932+
"no-restricted-syntax": {
933933
"count": 1
934-
},
935-
"jest/unbound-method": {
936-
"count": 36
937934
}
938935
},
939936
"packages/earn-controller/src/EarnController.ts": {
940937
"@typescript-eslint/explicit-function-return-type": {
941-
"count": 9
942-
},
943-
"id-length": {
944-
"count": 2
938+
"count": 4
945939
},
946940
"no-negated-condition": {
947941
"count": 3
942+
},
943+
"no-restricted-syntax": {
944+
"count": 1
948945
}
949946
},
950947
"packages/earn-controller/src/selectors.ts": {

packages/earn-controller/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111

12+
- **BREAKING:** `EarnController` constructor no longer accepts `selectedNetworkClientId` and no longer performs async work during construction. Consumers must call `init()` after construction. The messenger must now allow `AccountTreeController:stateChange` and `NetworkController:getState` ([#8421](https://github.com/MetaMask/core/pull/8421))
13+
- **BREAKING:** `refreshPooledStakingData` and `refreshLendingData` no longer call eligibility checks internally. Eligibility is fetched once during `init()`. Consumers that relied on these methods to keep eligibility state current must call `refreshEarnEligibility` separately ([#8421](https://github.com/MetaMask/core/pull/8421))
1214
- Bump `@metamask/controller-utils` from `^11.19.0` to `^11.20.0` ([#8344](https://github.com/MetaMask/core/pull/8344))
1315
- Bump `@metamask/messenger` from `^1.0.0` to `^1.1.1` ([#8364](https://github.com/MetaMask/core/pull/8364), [#8373](https://github.com/MetaMask/core/pull/8373))
1416
- Bump `@metamask/transaction-controller` from `^64.0.0` to `^64.1.0` ([#8432](https://github.com/MetaMask/core/pull/8432))
1517

18+
### Removed
19+
20+
- **BREAKING:** Removed `EarnController:refreshLendingEligibility` messenger action and `EarnControllerRefreshLendingEligibilityAction` type. Use `EarnController:refreshEarnEligibility` instead ([#8421](https://github.com/MetaMask/core/pull/8421))
21+
1622
## [11.2.1]
1723

1824
### Changed

packages/earn-controller/src/EarnController-method-action-types.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,6 @@ export type EarnControllerRefreshLendingPositionsAction = {
116116
handler: EarnController['refreshLendingPositions'];
117117
};
118118

119-
/**
120-
* Refreshes the lending eligibility status for the current account.
121-
* Updates the eligibility status in the controller state based on the location and address blocklist for compliance.
122-
*
123-
* @param options - Optional arguments
124-
* @param [options.address] - The address to refresh lending eligibility for (optional).
125-
* @returns A promise that resolves when the eligibility status has been updated
126-
*/
127-
export type EarnControllerRefreshLendingEligibilityAction = {
128-
type: `EarnController:refreshLendingEligibility`;
129-
handler: EarnController['refreshLendingEligibility'];
130-
};
131-
132119
/**
133120
* Refreshes all lending related data including markets, positions, and eligibility.
134121
* This method allows partial success, meaning some data may update while other requests fail.
@@ -302,7 +289,6 @@ export type EarnControllerMethodActions =
302289
| EarnControllerRefreshPooledStakingDataAction
303290
| EarnControllerRefreshLendingMarketsAction
304291
| EarnControllerRefreshLendingPositionsAction
305-
| EarnControllerRefreshLendingEligibilityAction
306292
| EarnControllerRefreshLendingDataAction
307293
| EarnControllerRefreshTronStakingApyAction
308294
| EarnControllerGetTronStakingApyAction

0 commit comments

Comments
 (0)