Skip to content

feat: add getErc20Balances helper TokenBalancesControllers #5925

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

salimtb
Copy link
Contributor

@salimtb salimtb commented Jun 5, 2025

Explanation

This change introduces a new getErc20Balances service function within the TokenBalancesController. The goal is to provide a reusable and isolated utility to fetch ERC-20 token balances for a given address and token list.

The service is intended to be used internally by the controller or other consumers needing direct access to raw ERC-20 balances, while abstracting away Web3 calls and error handling.

References

Changelog

@metamask/assets-controllers

UPDATE: Created getErc20Balances function within TokenBalancesController to support fetching ERC-20 token balances for a given address and token list. This modular service simplifies balance retrieval logic and can be reused across different parts of the controller.

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, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@salimtb salimtb changed the title feat: add getErc20Balances helper with unit tests in TokenBalancesCon… feat: add getErc20Balances helper TokenBalancesControllers Jun 5, 2025
@salimtb salimtb force-pushed the feat/token-balances-controller-get-balance branch from 32372f2 to 69122ec Compare June 5, 2025 09:51
@salimtb
Copy link
Contributor Author

salimtb commented Jun 5, 2025

@metamaskbot publish-preview

Copy link
Contributor

github-actions bot commented Jun 5, 2025

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "30.0.0-preview-69122ec",
  "@metamask-previews/address-book-controller": "6.1.0-preview-69122ec",
  "@metamask-previews/announcement-controller": "7.0.3-preview-69122ec",
  "@metamask-previews/app-metadata-controller": "1.0.0-preview-69122ec",
  "@metamask-previews/approval-controller": "7.1.3-preview-69122ec",
  "@metamask-previews/assets-controllers": "68.0.0-preview-69122ec",
  "@metamask-previews/base-controller": "8.0.1-preview-69122ec",
  "@metamask-previews/bridge-controller": "32.1.1-preview-69122ec",
  "@metamask-previews/bridge-status-controller": "29.1.0-preview-69122ec",
  "@metamask-previews/build-utils": "3.0.3-preview-69122ec",
  "@metamask-previews/chain-agnostic-permission": "0.7.0-preview-69122ec",
  "@metamask-previews/composable-controller": "11.0.0-preview-69122ec",
  "@metamask-previews/controller-utils": "11.9.0-preview-69122ec",
  "@metamask-previews/delegation-controller": "0.4.0-preview-69122ec",
  "@metamask-previews/earn-controller": "1.0.0-preview-69122ec",
  "@metamask-previews/eip1193-permission-middleware": "0.1.0-preview-69122ec",
  "@metamask-previews/ens-controller": "16.0.0-preview-69122ec",
  "@metamask-previews/error-reporting-service": "1.0.0-preview-69122ec",
  "@metamask-previews/eth-json-rpc-provider": "4.1.8-preview-69122ec",
  "@metamask-previews/foundryup": "1.0.0-preview-69122ec",
  "@metamask-previews/gas-fee-controller": "23.0.0-preview-69122ec",
  "@metamask-previews/json-rpc-engine": "10.0.3-preview-69122ec",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.7-preview-69122ec",
  "@metamask-previews/keyring-controller": "22.0.1-preview-69122ec",
  "@metamask-previews/logging-controller": "6.0.4-preview-69122ec",
  "@metamask-previews/message-manager": "12.0.1-preview-69122ec",
  "@metamask-previews/multichain": "4.1.0-preview-69122ec",
  "@metamask-previews/multichain-api-middleware": "0.4.0-preview-69122ec",
  "@metamask-previews/multichain-network-controller": "0.8.0-preview-69122ec",
  "@metamask-previews/multichain-transactions-controller": "2.0.0-preview-69122ec",
  "@metamask-previews/name-controller": "8.0.3-preview-69122ec",
  "@metamask-previews/network-controller": "23.5.1-preview-69122ec",
  "@metamask-previews/notification-services-controller": "10.0.0-preview-69122ec",
  "@metamask-previews/permission-controller": "11.0.6-preview-69122ec",
  "@metamask-previews/permission-log-controller": "3.0.3-preview-69122ec",
  "@metamask-previews/phishing-controller": "12.5.0-preview-69122ec",
  "@metamask-previews/polling-controller": "13.0.0-preview-69122ec",
  "@metamask-previews/preferences-controller": "18.1.0-preview-69122ec",
  "@metamask-previews/profile-sync-controller": "17.0.0-preview-69122ec",
  "@metamask-previews/queued-request-controller": "10.0.0-preview-69122ec",
  "@metamask-previews/rate-limit-controller": "6.0.3-preview-69122ec",
  "@metamask-previews/remote-feature-flag-controller": "1.6.0-preview-69122ec",
  "@metamask-previews/sample-controllers": "0.1.0-preview-69122ec",
  "@metamask-previews/seedless-onboarding-controller": "1.0.0-preview-69122ec",
  "@metamask-previews/selected-network-controller": "22.1.0-preview-69122ec",
  "@metamask-previews/signature-controller": "30.0.0-preview-69122ec",
  "@metamask-previews/token-search-discovery-controller": "3.2.0-preview-69122ec",
  "@metamask-previews/transaction-controller": "57.1.0-preview-69122ec",
  "@metamask-previews/user-operation-controller": "36.0.0-preview-69122ec"
}

@salimtb salimtb force-pushed the feat/token-balances-controller-get-balance branch from c93d287 to 2a93861 Compare June 6, 2025 12:07
@salimtb salimtb marked this pull request as ready for review June 6, 2025 12:12
@salimtb salimtb requested review from a team as code owners June 6, 2025 12:12
* @returns The balances for the given token addresses.
*/
async #batchBalanceOf({
chainId,
Copy link
Contributor

@sahar-fehri sahar-fehri Jun 6, 2025

Choose a reason for hiding this comment

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

Nit: Do you think its better to pass the networkClientId instead of chainId?
We would also pass networkClientId in getErc20Balances instead of chainId

@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added Base Network for networks to track in `TokenDetectionController` ([#5902](https://github.com/MetaMask/core/pull/5902))
- Network changes were added in `@metamask/controller-utils`
- Add `getErc20Balances` function within `TokenBalancesController` to support fetching ERC-20 token balances for a given address and token list ([#5925](https://github.com/MetaMask/core/pull/5925))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can we add this Changelog before the "- Added Base Network for network..."

tokenAddresses: Hex[];
}): Promise<Record<Hex, Hex | null>> {
// Return early if no token addresses provided
if (tokenAddresses.length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense to return early here; wondering if there is a specific use case you had in mind for adding the condition?
I think this fct will be used on clients to fetch balances on the fly for 1 or more tokens in the import flow; i , for now, dnt expect it to be called with an empty array; but makes sense to me to keep the check!

Comment on lines +485 to +486
tokenAddresses.forEach((tokenAddress, i) => {
balances[tokenAddress] = results[i]?.success
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment - So this is a 1:1 mapping?

Reason I'm asking is below (in the updateBalancesByChainId) we loop through the results and then index to get the tokenAddress; over here we are doing it the other way round.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can leave as is, this was mostly a comment.

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