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: Token detection on accounts other than current #1848

Merged
merged 2 commits into from
Oct 25, 2023

Conversation

bergeron
Copy link
Contributor

Explanation

TokensController.addDetectedTokens supports detecting tokens on accounts other than the one currently selected. But a bug occurs in this case. Tokens (both added and detected) from the current account get incorrectly copied to the detecting account, even when the detecting account has no balance of those tokens.

This happened because state.tokens and state.detectedTokens were used to check for existing tokens, but these are scoped to the current account rather than the detecting account.

Fix: Check state.allTokens, state.allDetectedTokens, and state.allIgnoredTokens instead, since they contain tokens for all chains + addresses. Some perf improvements are also made to combine find and indexOf into a single findIndex.

Added a unit test that reproduced the problem on the old code, and passes with the new code. The existing test did not catch this because of its operation order (it detected the other account first, then added to the current account).

Changelog

@metamask/assets-controllers

  • FIXED: Token detection on accounts other than current

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

@bergeron bergeron requested a review from a team as a code owner October 16, 2023 21:01
@bergeron bergeron force-pushed the brian/token-detection-different-account branch from 2b536a6 to 265c2cd Compare October 17, 2023 14:24
Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

LGTM

@bergeron bergeron merged commit a6fb84d into main Oct 25, 2023
115 checks passed
@bergeron bergeron deleted the brian/token-detection-different-account branch October 25, 2023 20:13
mcmire pushed a commit that referenced this pull request Nov 2, 2023
## Explanation

`TokensController.addDetectedTokens` supports detecting tokens on
accounts other than the one currently selected. But a bug occurs in this
case. Tokens (both added and detected) from the current account get
incorrectly copied to the detecting account, even when the detecting
account has no balance of those tokens.

This happened because `state.tokens` and `state.detectedTokens` were
used to check for existing tokens, but these are scoped to the current
account rather than the detecting account.

Fix: Check `state.allTokens`, `state.allDetectedTokens`, and
`state.allIgnoredTokens` instead, since they contain tokens for all
chains + addresses. Some perf improvements are also made to combine
`find` and `indexOf` into a single `findIndex`.

Added a unit test that reproduced the problem on the old code, and
passes with the new code. The existing test did not catch this because
of its operation order (it detected the other account first, then added
to the current account).


## Changelog

### `@metamask/assets-controllers`

- **FIXED**: Token detection on accounts other than current

## 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 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.

4 participants