Skip to content

[token-detection-controller] Consolidate with DetectTokensController from extension - #3775

Merged
MajorLift merged 34 commits into
mainfrom
230113-consolidate-TokenDetectionController-DetectTokensController
Feb 8, 2024
Merged

[token-detection-controller] Consolidate with DetectTokensController from extension#3775
MajorLift merged 34 commits into
mainfrom
230113-consolidate-TokenDetectionController-DetectTokensController

Conversation

@MajorLift

@MajorLiftMajorLift commented Jan 11, 2024

Copy link
Copy Markdown
Contributor

Explanation

Merges the core TokenDetectionController with DetectTokensController from the extension and patches for this controller from mobile.

  • Aims to replace the extension and mobile versions of this controller and implement a unified interface that consolidates the functionality of all three iterations.
  • Generally assumes DetectTokensController to be more up-to-date, and prioritizes preserving extension and mobile behavior to minimize disruption.

Significant API changes: see Changelog for details.

References

Changelog

@metamask/assets-controllers

Added

  • BREAKING: Adds @metamask/accounts-controller ^8.0.0 and @metamask/keyring-controller ^12.0.0 as dependencies and peer dependencies. (#3775).
  • BREAKING:TokenDetectionController newly subscribes to the PreferencesController:stateChange, AccountsController:selectedAccountChange, KeyringController:lock, KeyringController:unlock events, and allows the PreferencesController:getState messenger action. (#3775)

Changed

  • BREAKING:TokenDetectionController is merged with DetectTokensController from the metamask-extension repo. (#3775)
    • BREAKING:TokenDetectionController now resets its polling interval to the default value of 3 minutes when token detection is triggered by external controller events KeyringController:unlock, TokenListController:stateChange, PreferencesController:stateChange, AccountsController:selectedAccountChange.
    • BREAKING:TokenDetectionController now refetches tokens on NetworkController:networkDidChange if the networkClientId is changed instead of chainId.
    • BREAKING:TokenDetectionController cannot initiate polling or token detection if KeyringController state is locked.
    • BREAKING: The detectTokens method now excludes tokens that are already included in the TokensController's detectedTokens list from the batch of incoming tokens it sends to the TokensControlleraddDetectedTokens method.
    • BREAKING: The constructor for TokenDetectionController expects a new required proprerty trackMetaMetricsEvent, which defines the callback that is called in the detectTokens method.
    • BREAKING: In Mainnet, even if the PreferenceController's useTokenDetection option is set to false, automatic token detection is performed on the legacy token list (token data from the contract-metadata repo).

Removed

  • BREAKING:TokenDetectionController constructor no longer accepts options onPreferencesStateChange, getPreferencesState. (#3775)
  • BREAKING:TokenDetectionController no longer allows the NetworkController:stateChange event. The NetworkController:networkDidChange event can be used instead. (#3775)

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 highlighted breaking changes using the "BREAKING" category above as appropriate

@MajorLiftMajorLift self-assigned this Jan 12, 2024
@MajorLiftMajorLift added the team-wallet-framework Deprecated: Please use `team-core-platform` instead. label Jan 12, 2024
@MajorLift
MajorLiftforce-pushed the 230113-consolidate-TokenDetectionController-DetectTokensController branch 6 times, most recently from 2444b95 to db6974cCompareJanuary 20, 2024 02:40
@MajorLift
MajorLift marked this pull request as ready for review January 20, 2024 03:13
@MajorLift
MajorLift requested a review from a team as a code ownerJanuary 20, 2024 03:13
@MajorLift
MajorLiftforce-pushed the 230113-consolidate-TokenDetectionController-DetectTokensController branch from e8b624c to 209effdCompareJanuary 21, 2024 21:44
@MajorLift
MajorLiftforce-pushed the 230113-consolidate-TokenDetectionController-DetectTokensController branch from 209effd to e9e4a4eCompareJanuary 22, 2024 01:14
@socket-security

This comment was marked as outdated.

@socket-security

This comment was marked as resolved.

@MajorLift
MajorLiftforce-pushed the 230113-consolidate-TokenDetectionController-DetectTokensController branch from db13e83 to f8df3d5CompareJanuary 22, 2024 15:05
Comment threadpackages/assets-controllers/CHANGELOG.md Outdated
Comment threadpackages/assets-controllers/CHANGELOG.md Outdated
@MajorLift

Copy link
Copy Markdown
ContributorAuthor

@SocketSecurity ignore npm/@metamask/object-multiplex@1.3.0 (Internal tooling)

@MajorLiftMajorLift changed the title Consolidate core TokenDetectionController and extension DetectTokensController[token-detection-controller] Consolidate with DetectTokensController from extensionJan 22, 2024
@MajorLift
MajorLiftforce-pushed the 230113-consolidate-TokenDetectionController-DetectTokensController branch from 197e379 to 8231533CompareJanuary 24, 2024 05:39

@mcmiremcmire left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice job sorting all of these differences out. I did a first pass and made some notes. I noticed that some tests are missing for the added behavior. I know we have another ticket to ensure that there are comprehensive tests for this controller, but do we want to add tests for the changes added here? Then that other tickets would cover behavior that is out of scope of this consolidation work.

Comment threadpackages/assets-controllers/src/TokenDetectionController.ts
Comment threadpackages/assets-controllers/src/TokenDetectionController.ts Outdated
Comment threadpackages/assets-controllers/src/TokenDetectionController.ts Outdated
Comment threadpackages/assets-controllers/src/TokenDetectionController.ts Outdated
Comment threadpackages/assets-controllers/src/TokenDetectionController.ts
Comment threadpackages/assets-controllers/src/TokenDetectionController.ts
Comment threadpackages/assets-controllers/src/TokenDetectionController.ts
Comment threadpackages/assets-controllers/src/TokenDetectionController.ts
Comment threadpackages/assets-controllers/src/TokenDetectionController.ts
Comment threadpackages/assets-controllers/src/TokenDetectionController.ts
@MajorLift
MajorLiftforce-pushed the 230113-consolidate-TokenDetectionController-DetectTokensController branch 2 times, most recently from 0ae2ffd to fbc1a83CompareJanuary 26, 2024 16:25
@MajorLift

Copy link
Copy Markdown
ContributorAuthor

Hmm CI changelog:validate is not running on the current state of the changelog file:
https://github.com/MetaMask/core/actions/runs/7670677089/job/20907426326?pr=3775

@mcmire

mcmire commented Jan 26, 2024

Copy link
Copy Markdown
Collaborator

@MajorLift That's strange. yarn changelog:validate works for me locally. I also tried running it on Node 20 instead of 16 and it still works. I wonder if this is due to bumping create-release-branch a few days ago. Although a new version of auto-changelog was released a month ago. There must be some difference between the CI and local environments. I'll have to investigate this.

@mcmire

Copy link
Copy Markdown
Collaborator

@MajorLift Is this commit present in this branch? f4d4047

@MajorLift
MajorLiftforce-pushed the 230113-consolidate-TokenDetectionController-DetectTokensController branch from 1957a33 to ca19f99CompareJanuary 26, 2024 21:25
@MajorLift

Copy link
Copy Markdown
ContributorAuthor

@MajorLift Is this commit present in this branch? f4d4047

It looks like changelog:validate was correctly alerting us that my changelog wasn't conforming to prettier rules. Adding newlines after the unreleased category headings fixed the issue.

The current way changelog:validate shows diffs is a bit confusing. Would it make sense to reverse the diff source and target so that suggested additions show up as positive diffs and removals as negative diffs?

@mcmire

Copy link
Copy Markdown
Collaborator

The current way changelog:validate shows diffs is a bit confusing. Would it make sense to reverse the diff source and target so that suggested additions show up as positive diffs and removals as negative diffs?

Absolutely, yes. This is an issue that we've been meaning to fix for a while.

…s` versions used by `@metamask/accounts-controller`
- **BREAKING**: use `NetworkControllerNetworkDidChangeEvent` instead
…e yarn resolutions entry for `@metamask/providers`
Update packages/assets-controllers/CHANGELOG.md
…` is changed instead of `chainId`, and avoid resetting polling interval
@MajorLift
MajorLiftforce-pushed the 230113-consolidate-TokenDetectionController-DetectTokensController branch from 754b46f to 88eb7caCompareFebruary 6, 2024 21:46
MajorLiftand others added 2 commits February 7, 2024 13:44
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>

@mcmiremcmire left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me! 🎉

@MajorLift
MajorLift merged commit 00e4a9c into mainFeb 8, 2024
@MajorLift
MajorLift deleted the 230113-consolidate-TokenDetectionController-DetectTokensController branch February 8, 2024 20:59
MajorLift added a commit that referenced this pull request Feb 9, 2024
…roller-messenger pattern (#3690)
## Motivation
Remove `TokenDetectionController` constructor callbacks for
`TokensController`.
- `TokensController` features should be exposed as messenger
actions/events so that consumers don't need to import the entire class.
- `TokensController` should use messenger action/events to consume
external controller features instead of callbacks.
## Explanation
- Replaces constructor options callbacks `onPreferencesStateChange`,
`onNetworkDidChange`, `onTokenListStateChange`, `getNetworkClientById`
with messenger actions (`NetworkController:getNetworkClientById`) and
events (`PreferencesController:stateChange`,
`NetworkController:networkDidChange`,
`TokenListController:stateChange`).
- Replaces tokens-controller callbacks in token-detection-controller and
token-balances-controller with `TokensController:getState`,
`TokensController:addDetectedTokens` actions and
`TokensController:stateChange` event.
## References
- Extracted from #3775
- Contributes to #3626 ## Changelog
###
[`@metamask/assets-controllers`](https://github.com/MetaMask/core/pull/3690/files#diff-ee47d03d53776b8dd530799a8047f5e32e36e35765620aeb50b294adc3339fab)
### Added
- `TokensController` now exports `TokensControllerActions`,
`TokensControllerGetStateAction`,
`TokensControllerAddDetectedTokensAction`, `TokensControllerEvents`,
`TokensControllerStateChangeEvent`.
([#3690](#3690))
### Changed
- **BREAKING:** The `TokensState` type is now defined as a type alias
rather than an interface.
([#3690](#3690))
- This is breaking because it could affect how this type is used with
other types, such as `Json`, which does not support TypeScript
interfaces.
### Removed
- **BREAKING:** `TokenDetectionController` constructor no longer accepts
options `onPreferencesStateChange`, `getPreferencesState`,
`getTokensState`, `addDetectedTokens`.
([#3690](#3690),
[#3775](#3775))
- **BREAKING:** `TokensController` constructor no longer accepts options
`onPreferencesStateChange`, `onNetworkDidChange`,
`onTokenListStateChange`, `getNetworkClientById`.
([#3690](#3690))
- **BREAKING:** `TokenBalancesController` constructor no longer accepts
options `onTokensStateChange`, `getSelectedAddress`.
([#3690](#3690))
## 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 highlighted breaking changes using the "BREAKING" category
above as appropriate
---------
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
MajorLift added a commit that referenced this pull request Feb 9, 2024
…h `DetectTokensController` (#3867)
## Explanation
- This PR adds unit tests for the token-detection-controller API changes
implemented in #3775.
- The following changes are covered in the new tests:
- Don't detect if keyring-controller `isUnlocked` state is false.
- Subscribe to `KeyringController:unlock`, `KeyringController:lock`
events
- Subscribe to `AccountsController:selectedAccountChange` event
- Detect tokens using `@metamask/contract-metadata` static token list if
on mainnet and `useTokenDetection` is false.
- Call `trackMetaMetricsEvent` for every detected token.
- The aim of this PR isn't to achieve 100% test coverage for
token-detection-controller. That will be the goal of follow-up ticket
#1615
## References
- Closes#3626 - Follows from:
- #3775
- #3690
- Followed by #1615 ## Changelog
N/A
## 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 highlighted breaking changes using the "BREAKING" category
above as appropriate
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-wallet-frameworkDeprecated: Please use `team-core-platform` instead.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merge extension DetectTokensController into core TokenDetectionController

2 participants

@MajorLift@mcmire