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

Use account API in TokenDetectionController #4743

Open
bergeron opened this issue Sep 30, 2024 · 0 comments
Open

Use account API in TokenDetectionController #4743

bergeron opened this issue Sep 30, 2024 · 0 comments
Assignees
Labels
area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. team-assets team-notifications Notification Team changes. https://github.com/orgs/MetaMask/teams/notifications

Comments

@bergeron
Copy link
Contributor

bergeron commented Sep 30, 2024

Account API docs:
https://docs.cx.metamask.io/docs/multichainAccounts/api-reference/

Use the account API as a means of detecting tokens in TokenDetectionController. This API uses off chain indexing to determine which tokens the wallet interacted with, and will be more efficient than the current approach of bulk calling balanceOf on the entire token list.

The account API can only be used on chains that it supports. If the account API does not support the current chain, we fall back to the old mechanism. Also if the request to the API fails, we can fallback to the old mechanism.

In the future, the account API may be used to replace additional functionality like the balance updates in TokenBalancesController. But we're starting with detection since its the highest perf impact.

@Prithpal-Sooriya Prithpal-Sooriya self-assigned this Sep 30, 2024
@gauthierpetetin gauthierpetetin added area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. and removed area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. labels Oct 3, 2024
@tommasini tommasini added the area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. label Oct 3, 2024
@desi desi added team-assets team-notifications Notification Team changes. https://github.com/orgs/MetaMask/teams/notifications labels Oct 3, 2024
Prithpal-Sooriya added a commit that referenced this issue Oct 18, 2024
## Explanation

This integrates the Accounts API (Multi-chain Balances Endpoint) to help
alleviate expensive RPC calls made by Token Detection.

The aim is to attempt to use the Accounts API when making balance calls
for expensive functionality (e.g. Token Detection)

<details><summary>Code Walkthrough</summary>


https://www.loom.com/share/e540cae3967746b0aca343d4c59d0af6?sid=69c2556c-96d3-451e-bd67-7d03f32fff03
</details> 

## References

#4743

https://consensyssoftware.atlassian.net/browse/NOTIFY-1179

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/assets-controllers`

- **ADDED**: MultiChain Accounts Service
- **ADDED**: `fetchSupportedNetworks()` function to dynamically fetch
supported networks by the Accounts API
- **ADDED**: `fetchMultiChainBalances()` function to get balances for a
given address
- **ADDED**: `useAccountsAPI` to the `TokenDetectionController`
constructor to enable/disable the accounts API feature.
- **ADDED**: `#addDetectedTokensViaAPI()` private method in
`TokenDetectionController` to get detected tokens via the Accounts API.
- **CHANGED**: `detectTokens()` method in `TokenDetectionController` to
try AccountsAPI first before using RPC flow to detect tokens.

## 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
- [x] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. team-assets team-notifications Notification Team changes. https://github.com/orgs/MetaMask/teams/notifications
Projects
None yet
Development

No branches or pull requests

5 participants