Commit 691b9dc
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
- packages/earn-controller
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
929 | 929 | | |
930 | 930 | | |
931 | 931 | | |
932 | | - | |
| 932 | + | |
933 | 933 | | |
934 | | - | |
935 | | - | |
936 | | - | |
937 | 934 | | |
938 | 935 | | |
939 | 936 | | |
940 | 937 | | |
941 | | - | |
942 | | - | |
943 | | - | |
944 | | - | |
| 938 | + | |
945 | 939 | | |
946 | 940 | | |
947 | 941 | | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
948 | 945 | | |
949 | 946 | | |
950 | 947 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
| |||
Lines changed: 0 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | 119 | | |
133 | 120 | | |
134 | 121 | | |
| |||
302 | 289 | | |
303 | 290 | | |
304 | 291 | | |
305 | | - | |
306 | 292 | | |
307 | 293 | | |
308 | 294 | | |
| |||
0 commit comments