Skip to content

Feat: assets controller token detection preference - #9835

Draft
salimtb wants to merge 6 commits into
mainfrom
feat/assets-controller-token-detection-preference
Draft

Feat: assets controller token detection preference#9835
salimtb wants to merge 6 commits into
mainfrom
feat/assets-controller-token-detection-preference

Conversation

@salimtb

@salimtb salimtb commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Explanation

UI PR: MetaMask/metamask-extension#45447

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • 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

Note

Medium Risk
Changes how assets enter persisted state and adds a breaking messenger requirement for PreferencesController:getState. Incorrect gating could hide legitimate tokens or let unwanted ones through.

Overview
Respects the user's "Autodetect tokens" preference across the assets pipeline so new fungible tokens are not detected or persisted when it is off.

DetectionMiddleware now accepts an isTokenDetectionEnabled callback. When disabled, it strips new-to-state erc20 / token balances and stub metadata from responses (including websocket updates), while leaving natives, staking contracts, custom imports, and already-tracked holdings alone. AssetsController reads useTokenDetection via PreferencesController:getState (fail-open if unregistered), skips occurrence filtering when detection is off, and force-refreshes balances/metadata/prices when the preference is turned back on.

BREAKING: clients must allow PreferencesController:getState on the AssetsController messenger.

Reviewed by Cursor Bugbot for commit fbd4ef2. Bugbot is set up for automated code reviews on this repo. Configure here.

@salimtb salimtb changed the title Feat/assets controller token detection preference Feat: assets controller token detection preference Aug 12, 2026
When autodetection is off, strip new-to-state fungible tokens from the
pipeline while keeping natives, staking, custom, and already-tracked
holdings. Requires PreferencesController:getState on the messenger.
Subscribe to PreferencesController:stateChange and force-run getAssets
when useTokenDetection flips back on so tokens skipped while the
preference was off are detected without waiting for the next poll.
@salimtb
salimtb force-pushed the feat/assets-controller-token-detection-preference branch from 0a82d19 to e25f46d Compare August 12, 2026 08:33
@salimtb

salimtb commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

@cursor pls fix the build CI on this PR , update also changelog and add the PR number

@cursor

cursor Bot commented Aug 12, 2026

Copy link
Copy Markdown

I've successfully fixed the CI build issues on this PR. Here's what I did:

Fixed Issues

  1. Fixed formatting violations - Ran yarn lint:fix to automatically format the two files that had formatting issues:

    • packages/assets-controller/src/middlewares/DetectionMiddleware.test.ts
    • packages/assets-controller/src/middlewares/DetectionMiddleware.ts
  2. Updated changelog with PR number - Added PR Feat: assets controller token detection preference #9835 to both changelog entries in packages/assets-controller/CHANGELOG.md:

    • Added to the "Added" section entry about DetectionMiddlewareOptions
    • Added to the "Changed" section entry about the breaking change requiring PreferencesController:getState
  3. Validated all changes - Confirmed that:

    • Formatting checks now pass (yarn lint:misc:check)
    • Changelog validation passes (yarn changelog:validate)
    • All tests pass for the assets-controller package

Changes Committed

All fixes have been committed and pushed to the branch feat/assets-controller-token-detection-preference.

Note: The git SSL certificate verification failures shown in the CI are GitHub Actions infrastructure issues unrelated to the code changes and cannot be fixed from this end. The actual code quality checks (formatting, changelog validation, and tests) all pass successfully.

Open in Web Open in Cursor 

@salimtb
salimtb marked this pull request as ready for review August 12, 2026 09:09
@salimtb
salimtb requested review from a team as code owners August 12, 2026 09:09
@salimtb
salimtb deployed to default-branch August 12, 2026 09:09 — with GitHub Actions Active
@salimtb

salimtb commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

@metamaskbot publish-preview

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/account-tree-controller@7.6.1-preview-823dcff
@metamask-previews/accounts-controller@39.1.0-preview-823dcff
@metamask-previews/address-book-controller@7.1.2-preview-823dcff
@metamask-previews/ai-controllers@0.8.0-preview-823dcff
@metamask-previews/analytics-controller@2.0.0-preview-823dcff
@metamask-previews/analytics-data-regulation-controller@0.0.0-preview-823dcff
@metamask-previews/announcement-controller@8.1.0-preview-823dcff
@metamask-previews/app-metadata-controller@2.0.1-preview-823dcff
@metamask-previews/approval-controller@9.0.2-preview-823dcff
@metamask-previews/assets-controller@13.1.2-preview-823dcff
@metamask-previews/assets-controllers@111.1.0-preview-823dcff
@metamask-previews/authenticated-user-storage@3.0.1-preview-823dcff
@metamask-previews/base-controller@9.1.0-preview-823dcff
@metamask-previews/base-data-service@0.1.3-preview-823dcff
@metamask-previews/bitcoin-regtest-up@1.0.0-preview-823dcff
@metamask-previews/bridge-controller@79.1.0-preview-823dcff
@metamask-previews/bridge-status-controller@75.0.0-preview-823dcff
@metamask-previews/build-utils@3.0.4-preview-823dcff
@metamask-previews/chain-agnostic-permission@1.7.0-preview-823dcff
@metamask-previews/chomp-api-service@4.0.0-preview-823dcff
@metamask-previews/claims-controller@0.6.0-preview-823dcff
@metamask-previews/client-controller@1.0.1-preview-823dcff
@metamask-previews/client-utils@2.0.1-preview-823dcff
@metamask-previews/compliance-controller@2.1.0-preview-823dcff
@metamask-previews/composable-controller@12.0.1-preview-823dcff
@metamask-previews/config-registry-controller@2.0.1-preview-823dcff
@metamask-previews/connectivity-controller@0.3.0-preview-823dcff
@metamask-previews/controller-utils@12.3.0-preview-823dcff
@metamask-previews/core-backend@8.1.1-preview-823dcff
@metamask-previews/delegation-controller@3.0.2-preview-823dcff
@metamask-previews/earn-controller@12.2.4-preview-823dcff
@metamask-previews/eip-5792-middleware@3.0.5-preview-823dcff
@metamask-previews/eip-7702-internal-rpc-middleware@0.1.1-preview-823dcff
@metamask-previews/eip1193-permission-middleware@2.0.1-preview-823dcff
@metamask-previews/ens-controller@19.1.6-preview-823dcff
@metamask-previews/eth-block-tracker@15.0.1-preview-823dcff
@metamask-previews/eth-json-rpc-middleware@24.0.0-preview-823dcff
@metamask-previews/eth-json-rpc-provider@6.0.1-preview-823dcff
@metamask-previews/foundryup@1.0.1-preview-823dcff
@metamask-previews/gas-fee-controller@26.3.1-preview-823dcff
@metamask-previews/gator-permissions-controller@5.0.1-preview-823dcff
@metamask-previews/geolocation-controller@1.0.0-preview-823dcff
@metamask-previews/java-tron-up@1.0.0-preview-823dcff
@metamask-previews/json-rpc-engine@10.5.0-preview-823dcff
@metamask-previews/json-rpc-middleware-stream@8.0.8-preview-823dcff
@metamask-previews/keyring-controller@27.1.1-preview-823dcff
@metamask-previews/kyc-controller@0.0.0-preview-823dcff
@metamask-previews/local-node-utils@1.0.0-preview-823dcff
@metamask-previews/logging-controller@8.0.2-preview-823dcff
@metamask-previews/message-manager@14.1.2-preview-823dcff
@metamask-previews/messenger@2.0.0-preview-823dcff
@metamask-previews/messenger-cli@0.2.0-preview-823dcff
@metamask-previews/money-account-api-data-service@0.4.0-preview-823dcff
@metamask-previews/money-account-balance-service@2.4.1-preview-823dcff
@metamask-previews/money-account-controller@1.0.0-preview-823dcff
@metamask-previews/money-account-upgrade-controller@3.0.1-preview-823dcff
@metamask-previews/money-account-utils@1.1.0-preview-823dcff
@metamask-previews/multichain-account-service@13.0.1-preview-823dcff
@metamask-previews/multichain-api-middleware@4.0.2-preview-823dcff
@metamask-previews/multichain-network-controller@3.2.3-preview-823dcff
@metamask-previews/multichain-transactions-controller@7.1.2-preview-823dcff
@metamask-previews/name-controller@9.1.2-preview-823dcff
@metamask-previews/network-connection-banner-controller@0.1.2-preview-823dcff
@metamask-previews/network-controller@35.0.1-preview-823dcff
@metamask-previews/network-enablement-controller@6.0.3-preview-823dcff
@metamask-previews/notification-services-controller@26.0.1-preview-823dcff
@metamask-previews/passkey-controller@3.0.0-preview-823dcff
@metamask-previews/permission-controller@13.1.1-preview-823dcff
@metamask-previews/permission-log-controller@5.1.0-preview-823dcff
@metamask-previews/perps-controller@11.0.0-preview-823dcff
@metamask-previews/phishing-controller@17.3.1-preview-823dcff
@metamask-previews/platform-api-docs@0.0.0-preview-823dcff
@metamask-previews/polling-controller@16.0.9-preview-823dcff
@metamask-previews/preferences-controller@23.1.0-preview-823dcff
@metamask-previews/profile-metrics-controller@4.0.3-preview-823dcff
@metamask-previews/profile-sync-controller@29.0.0-preview-823dcff
@metamask-previews/ramps-controller@20.0.0-preview-823dcff
@metamask-previews/rate-limit-controller@7.0.1-preview-823dcff
@metamask-previews/react-data-query@0.2.2-preview-823dcff
@metamask-previews/remote-feature-flag-controller@5.0.0-preview-823dcff
@metamask-previews/sample-controllers@5.0.4-preview-823dcff
@metamask-previews/seedless-onboarding-controller@10.1.1-preview-823dcff
@metamask-previews/selected-network-controller@26.1.6-preview-823dcff
@metamask-previews/sentinel-api-service@1.0.0-preview-823dcff
@metamask-previews/shield-controller@6.0.0-preview-823dcff
@metamask-previews/signature-controller@39.2.9-preview-823dcff
@metamask-previews/smart-transactions-controller@25.1.0-preview-823dcff
@metamask-previews/snap-account-service@2.1.2-preview-823dcff
@metamask-previews/social-controllers@2.7.1-preview-823dcff
@metamask-previews/solana-test-validator-up@1.0.0-preview-823dcff
@metamask-previews/stellar-quickstart-up@0.0.0-preview-823dcff
@metamask-previews/storage-service@1.0.2-preview-823dcff
@metamask-previews/subscription-controller@7.0.0-preview-823dcff
@metamask-previews/transaction-controller@69.5.2-preview-823dcff
@metamask-previews/transaction-pay-controller@26.3.0-preview-823dcff
@metamask-previews/user-operation-controller@41.2.8-preview-823dcff
@metamask-previews/wallet@10.0.0-preview-823dcff
@metamask-previews/wallet-cli@0.0.0-preview-823dcff

Co-authored-by: Salim TOUBAL <salimtb@users.noreply.github.com>
@salimtb
salimtb force-pushed the feat/assets-controller-token-detection-preference branch from 963227f to 174861e Compare August 12, 2026 09:32
cursoragent and others added 3 commits August 12, 2026 09:38
Co-authored-by: Salim TOUBAL <salimtb@users.noreply.github.com>
Co-authored-by: Salim TOUBAL <salimtb@users.noreply.github.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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 fbd4ef2. Configure here.

assetIds: [...strippedAssetIds],
});
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale detectedAssets bypass preference

High Severity

When token detection is off and every new gated token is stripped, DetectionMiddleware builds an empty detectedAssets map but only assigns response.detectedAssets when that map is non-empty. Upstream RpcDataSource TokenDetector updates already set detectedAssets, so those IDs remain on the response. Later TokenDataSource still fetches and persists metadata for them, which makes the tokens look known and lets later balance updates bypass the preference gate.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit fbd4ef2. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RPC have it's own logic and have the toggle working , we can ignore

@Prithpal-Sooriya
Prithpal-Sooriya marked this pull request as draft August 12, 2026 15:11

@Prithpal-Sooriya Prithpal-Sooriya left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed look good. Lets pause on this until we get Prod decision on if we want this detection switch (might be removed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants