Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TokenDetectionController.detectTokens() used tokens states when passed networkClientId #3914

Merged

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Feb 9, 2024

Explanation

Currently, detectTokens() always uses the state for the globally selected network from the TokenListController and TokensController regardless of if/what networkClientId is passed in.

This PR updates detectTokens() to use the chainId keyed states of the TokenListController and TokensController instead.

References

Fixes: #3905

Changelog

@metamask/assets-controllers

  • FIXED: TokenDetectionController.detectTokens() now reads the chainId keyed state properties from TokenListController and TokensController rather than incorrectly using the globally selected state properties when networkClientId is passed

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

@jiexi jiexi requested a review from a team as a code owner February 9, 2024 23:55
@jiexi jiexi changed the title Jl/mmp 3905/fix detect tokens controller token list polling Fix TokenDetectionController.detectTokens() used tokens states when passed networkClientId Feb 9, 2024
MajorLift
MajorLift previously approved these changes Feb 15, 2024
Copy link
Contributor

@MajorLift MajorLift left a comment

Choose a reason for hiding this comment

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

LGTM! Definitely an improvement over the previous pattern.

mcmire
mcmire previously approved these changes Feb 15, 2024
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Makes sense!

bergeron
bergeron previously approved these changes Feb 15, 2024
MajorLift added a commit that referenced this pull request Feb 17, 2024
…ed to wrong network, and detection using `chainId` keyed states

- See MetaMask/metamask-extension#22814
- See #3914
@jiexi jiexi dismissed stale reviews from MajorLift, bergeron, and mcmire via d8395af February 20, 2024 17:46
jiexi and others added 3 commits February 20, 2024 09:46
Co-authored-by: Jongsun Suh <jongsun.suh@icloud.com>
Co-authored-by: Jongsun Suh <jongsun.suh@icloud.com>
@jiexi jiexi merged commit 66c15b1 into main Feb 20, 2024
136 checks passed
@jiexi jiexi deleted the jl/mmp-3905/fix-detect-tokens-controller-token-list-polling branch February 20, 2024 17:55
MajorLift added a commit that referenced this pull request Feb 20, 2024
…ed to wrong network, and detection using `chainId` keyed states

- See MetaMask/metamask-extension#22814
- See #3914
MajorLift added a commit that referenced this pull request Feb 20, 2024
…ed to wrong network, and detection using `chainId` keyed states

- See MetaMask/metamask-extension#22814
- See #3914
MajorLift added a commit that referenced this pull request Feb 23, 2024
…ed to wrong network, and detection using `chainId` keyed states

- See MetaMask/metamask-extension#22814
- See #3914
MajorLift added a commit that referenced this pull request Feb 27, 2024
…dates (#3923)

## Explanation

As a preparatory step for fully replacing the extension
`DetectTokensController` with the consolidated core repo
`TokenDetectionController`, `TokenDetectionController` needs to be
updated with changes made to the extension `DetectTokensController`
since #1813 was closed.

#### Diff of `DetectTokensController` state
-
[MetaMask/metamask-extension@`5d285f7be5f7be981995dfa725aad97d81cc990a..85cd1c89039e900b452edb704ec37e9ccbd3e76a`#diff-323d0cf464](https://github.com/MetaMask/metamask-extension/compare/5d285f7be5f7be981995dfa725aad97d81cc990a..85cd1c89039e900b452edb704ec37e9ccbd3e76a#diff-323d0cf46498be3850b971474905354ea5ccf7fa13745ad1e6eba59c5b586830)

### Differences from extension `DetectTokensController`

- Refactors logic for retrieving `chainId`, `networkClientId` into
`this.#getCorrectChainIdAndNetworkClientId`
- Uses `getNetworkConfigurationByNetworkClientId` action instead of
`getNetworkClientById` to retrieve `chainId`.
- If `networkClientId` is not supplied to the method, or it's supplied
but `getNetworkConfigurationByNetworkClientId` returns `undefined`,
finds `chainId` from `providerConfig`.

- `detectTokens` replaces `detectNewTokens`
- `detectTokens` accepts options object `{ selectedAddress,
networkClientId }` instead of `{ selectedAddress, chainId,
networkClientId }`.
- Does not throw error if `getBalancesInSingleCall` fails. Also does not
exit early -- continues looping.
- Passes lists of full `Token` types to
`TokensController:addDetectedTokens` instead of objects containing only
`{ address, decimals, symbol }`.
  
- `#trackMetaMetricsEvents` is a private method instead of protected.
- Passes string literals instead of extension shared constants into
`_trackMetaMetricsEvent`.

## References

- Partially implements #3916
- Blocking #3918
- Changes adopted from:
  - MetaMask/metamask-extension#22898
  - MetaMask/metamask-extension#22814
  - #3914
  - MetaMask/metamask-extension#21437
- Blocking (Followed by) #3938

## Changelog

###
[`@metamask/assets-controllers`](https://github.com/MetaMask/core/pull/3923/files#diff-ee47d03d53776b8dd530799a8047f5e32e36e35765620aeb50b294adc3339fab)

## 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: jiexi <jiexiluan@gmail.com>
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.

[assets-controllers] TokenDetectionController uses the wrong token list
5 participants