feat(assets-controller): visibleAssets and full update - #9651
Conversation
53a6e84 to
be213dd
Compare
cf7e7db to
5ad5431
Compare
| // ); | ||
| // } catch { | ||
| // return false; | ||
| // } |
There was a problem hiding this comment.
Temp, revert before merge
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
b542aa8 to
b123e43
Compare
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
b123e43 to
b5b17a6
Compare
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
81a42c3 to
b280b24
Compare
b280b24 to
ff6e6f3
Compare
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
|
|
||
| ### Fixed | ||
|
|
||
| - On the Accounts API v6 RPC poll path, skip staking vault share tokens so a `full` snapshot cannot overwrite the converted staked amount with a raw `balanceOf` |
There was a problem hiding this comment.
Thoughts on shortening the changelog? 😅
There was a problem hiding this comment.
I've simplified it. WDYT?
fec10d2 to
64019d5
Compare
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 387e920. Configure here.
| chainId, | ||
| assetId, | ||
| }); | ||
| balances[assetId] = { ...previous }; |
|
|
||
| if (nativeAssetId) { | ||
| if (!shouldSkipNative) { | ||
| assetsBalance[accountId][nativeAssetId] = { amount: '0' }; |
Resolve conflicts by adopting main's implementation changes from #9651 and #10469 (constructor-injected state/visibility, removal of the unprocessedCustomAssets mechanism, full-chain RPC recovery on v6) while keeping the PR's integration test coverage: the v5 suites keep their existing structure, and the two new v6 suites are adapted to main's data-source/pipeline API. Co-Authored-By: Claude <noreply@anthropic.com>
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> Mobile preview: MetaMask/metamask-mobile#36604 Extension preview: MetaMask/metamask-extension#46451 Architecture walkthrough: [`packages/assets-controller/docs/architecture/v1/CHANGES.md`](packages/assets-controller/docs/architecture/v1/CHANGES.md) **Onboarding** - Current logic + v5: https://www.loom.com/share/c55eed16333749e29d99cbda87ca4d9e - After PR + v6: https://www.loom.com/share/6f443eda5f344d7aa1733c3b5bf7ea4e **Start / basic flows (change account, network, add/remove custom tokens, add non-supported network)** - Current logic + v5: https://www.loom.com/share/d5fdd69c1f384c0a97574729773d578f - After PR + v6: https://www.loom.com/share/4753a4fd68c54a58abed48e728836760 **Transactions** - Current logic + v5: https://www.loom.com/share/fbc0e58acc9e435694dc6f2f2dfb6d6f - After PR + v6: https://www.loom.com/share/5b3b1cf5f58f4e6fa31de34f30cc01a9 **Fallback** - Current logic + v5: https://www.loom.com/share/16f95d2b237e4b63ab0f68e5d2d18cb1 - After PR + v6: https://www.loom.com/share/d34beee512d041b19ccb1869cef5c327 - After PR + v6 + custom tokens: https://www.loom.com/share/11f342e1ac1e4644a695403a536b9780 ### Why Accounts API **v6** can take pinned custom tokens (`includeAssetIds`) and return them in the balance snapshot even at zero balance, and can drop hidden tokens (`excludeAssetIds`). Once the API owns that, the client does not need a parallel custom-token path on the v6 flow. v5 stays intact. Which path runs is decided only in `AssetsController.#isBalanceV6Enabled()` (`assetsAccountsApiV6 === true`). ### What v6 does - Subscribe and force refresh assign **accounts + chains only**. Pins and hides are **not** copied onto `DataRequest`. - `AccountsApiDataSource` reads controller state via a required `getAssetsState` getter: - visible pins on fetched chains → `includeAssetIds` - hidden assets on fetched chains → `excludeAssetIds` - a hide wins over a pin - `request.customAssets` is only a **scoped override** (`getAssets({ customAssets })`, `addCustomAsset`, RPC fallback). - Accounts API reports `updateMode: 'full'`. `mergeAccountBalancesV6` replaces the covered chain slice, keeps `unprocessedCustomAssets` and staked vault balances, and re-asserts default tracked assets (mUSD) when the snapshot omits a zero balance. - RPC fallback recovers errored chains and pins listed in `unprocessedCustomAssets`. - Account Activity ignores pins/hides. - Token-detection filtering is **not** applied; the v6 snapshot is kept in full. - `CustomAssetGraduationMiddleware` does **not** run on this path. There is **no** `customAssetsOnly` RPC supplement. ### What v5 still does (flag off) Unchanged from production: - Force update attaches every pin (including hidden) on `DataRequest.customAssets`. - Extra RPC poll (`customAssetsOnly`) for pins on chains another source already owns. - `updateMode: 'merge'` with `replaceCoveredChainBalances`. - `CustomAssetGraduationMiddleware` on Accounts API and Account Activity updates. ### Breaking - Remove `excludeAssetIds` from `DataRequest`. v6 builds the query param from `assetPreferences`. - `AccountsApiDataSource` now requires `getAssetsState`. `AssetsController` injects `() => this.state`. `CustomAssetGraduationMiddleware`, `customAssetsOnly`, and `replaceCoveredChainBalances` stay on the v5 path and are **not** removed from the public API. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> - [APIPLAT-2499](https://consensys.atlassian.net/browse/APIPLAT-2499) — v6 `includeAssetIds` for pinned / zero-balance custom tokens - Architecture: [`packages/assets-controller/docs/architecture/v1/CHANGES.md`](packages/assets-controller/docs/architecture/v1/CHANGES.md) - Clients: [metamask-mobile#36604](MetaMask/metamask-mobile#36604), [metamask-extension#46451](MetaMask/metamask-extension#46451) ## 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 - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md) - [x] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them [APIPLAT-2499]: https://consensyssoftware.atlassian.net/browse/APIPLAT-2499?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **High Risk** > This is a breaking, flag-gated rewrite of how wallet balances are fetched, merged, and pruned—core UI/send/swap state—with many edge cases around full replaces, hidden/pinned tokens, and RPC/API fallbacks. > > **Overview** > **Breaking:** `@metamask/assets-controller` now runs two separate balance pipelines behind `assetsAccountsApiV6`. With the flag on, the client declares the visible set (`includeAssetIds` / `excludeAssetIds` from shared `getAssetVisibility`), Accounts API / Snap / RPC stamp **`full`** snapshots, and state merges via `effectiveAccountBalancesV6` (pins, natives, default tracked assets, and staking survive omission; detected/hidden tokens can drop). With the flag off, the existing **v5** merge + `replaceCoveredChainBalances` path is unchanged, including custom-asset graduation and the supplemental `customAssetsOnly` RPC poll. > > **API / wiring changes:** `'update'` is removed from `AssetsUpdateMode`; `getAssets` no longer accepts `updateMode` (sources set it on responses). `getAssetsState` / `getAssetVisibility` are required on Accounts API, Snap, and RPC constructors (removed from pipeline `Context` / subscriptions). `unhideAsset` is **async** and force-fetches the chain like `addCustomAsset`; hide/unhide re-run subscriptions. New zero-balance helpers (`getZeroAssetBalance`, Stellar trustline metadata) replace plain `{ amount: '0' }` in several paths. The flag is treated as enabled when `assetsAccountsApiV6 === true` (not a nested `{ value }` object). > > Adds an ADR, changelog entries, broad unit/integration tests (v6 visibility, RPC fallback on failed chains, full vs merge behavior), and a BSC spam case where user-imported custom tokens are kept under v6. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit d273088. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->

Explanation
Mobile preview: MetaMask/metamask-mobile#36604
Extension preview: MetaMask/metamask-extension#46451
Architecture walkthrough:
packages/assets-controller/docs/architecture/v1/CHANGES.mdOnboarding
Start / basic flows (change account, network, add/remove custom tokens, add non-supported network)
Transactions
Fallback
Why
Accounts API v6 can take pinned custom tokens (
includeAssetIds) and return them in the balance snapshot even at zero balance, and can drop hidden tokens (excludeAssetIds). Once the API owns that, the client does not need a parallel custom-token path on the v6 flow.v5 stays intact. Which path runs is decided only in
AssetsController.#isBalanceV6Enabled()(assetsAccountsApiV6 === true).What v6 does
DataRequest.AccountsApiDataSourcereads controller state via a requiredgetAssetsStategetter:includeAssetIdsexcludeAssetIdsrequest.customAssetsis only a scoped override (getAssets({ customAssets }),addCustomAsset, RPC fallback).updateMode: 'full'.mergeAccountBalancesV6replaces the covered chain slice, keepsunprocessedCustomAssetsand staked vault balances, and re-asserts default tracked assets (mUSD) when the snapshot omits a zero balance.unprocessedCustomAssets.CustomAssetGraduationMiddlewaredoes not run on this path. There is nocustomAssetsOnlyRPC supplement.What v5 still does (flag off)
Unchanged from production:
DataRequest.customAssets.customAssetsOnly) for pins on chains another source already owns.updateMode: 'merge'withreplaceCoveredChainBalances.CustomAssetGraduationMiddlewareon Accounts API and Account Activity updates.Breaking
excludeAssetIdsfromDataRequest. v6 builds the query param fromassetPreferences.AccountsApiDataSourcenow requiresgetAssetsState.AssetsControllerinjects() => this.state.CustomAssetGraduationMiddleware,customAssetsOnly, andreplaceCoveredChainBalancesstay on the v5 path and are not removed from the public API.References
includeAssetIdsfor pinned / zero-balance custom tokenspackages/assets-controller/docs/architecture/v1/CHANGES.mdChecklist
Note
High Risk
This is a breaking, flag-gated rewrite of how wallet balances are fetched, merged, and pruned—core UI/send/swap state—with many edge cases around full replaces, hidden/pinned tokens, and RPC/API fallbacks.
Overview
Breaking:
@metamask/assets-controllernow runs two separate balance pipelines behindassetsAccountsApiV6. With the flag on, the client declares the visible set (includeAssetIds/excludeAssetIdsfrom sharedgetAssetVisibility), Accounts API / Snap / RPC stampfullsnapshots, and state merges viaeffectiveAccountBalancesV6(pins, natives, default tracked assets, and staking survive omission; detected/hidden tokens can drop). With the flag off, the existing v5 merge +replaceCoveredChainBalancespath is unchanged, including custom-asset graduation and the supplementalcustomAssetsOnlyRPC poll.API / wiring changes:
'update'is removed fromAssetsUpdateMode;getAssetsno longer acceptsupdateMode(sources set it on responses).getAssetsState/getAssetVisibilityare required on Accounts API, Snap, and RPC constructors (removed from pipelineContext/ subscriptions).unhideAssetis async and force-fetches the chain likeaddCustomAsset; hide/unhide re-run subscriptions. New zero-balance helpers (getZeroAssetBalance, Stellar trustline metadata) replace plain{ amount: '0' }in several paths. The flag is treated as enabled whenassetsAccountsApiV6 === true(not a nested{ value }object).Adds an ADR, changelog entries, broad unit/integration tests (v6 visibility, RPC fallback on failed chains, full vs merge behavior), and a BSC spam case where user-imported custom tokens are kept under v6.
Reviewed by Cursor Bugbot for commit d273088. Bugbot is set up for automated code reviews on this repo. Configure here.