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

feat: Update token balances controller for multichain #4782

Merged
merged 19 commits into from
Nov 13, 2024

Conversation

bergeron
Copy link
Contributor

@bergeron bergeron commented Oct 10, 2024

Explanation

Updates the TokenBalancesController to be used across chains.

  • It now extends StaticIntervalPollingController so the new token based polling patterns can be used.

  • The state now stores erc20 balances across all chains and accounts. It used to only store them for the current chain and current account.

  • erc20 balances are now fetched with a single RPC multicall across all accounts + tokens (on the ~250 chains that support the multicall3 contract). Instead of doing a balanceOf request for each token like before.

  • The TokenBalancesController, TokenRatesController, and TokensController now remove state from networks when they're removed, to avoid leaving stale data.

References

Changelog

@metamask/assets-controllers

  • BREAKING: The TokenBalancesController state is now across all chains and accounts under the field tokenBalances, as a mapping from account address -> chain id -> token address -> balance.

  • BREAKING: The TokenBalancesController now extends StaticIntervalPollingController, and the new polling API startPolling must be used to initiate polling (startPolling, stopPollingByPollingToken).

  • BREAKING: TokenBalancesController now requires subscriptions to the PreferencesController:stateChange and NetworkController:stateChange events. And access to the NetworkController:getNetworkClientById, NetworkController:getState, TokensController:getState, and PreferencesController:getState actions.

  • BREAKING: TokensController requires a subscription to the NetworkController:stateChange event. It now now removes state for chain IDs when their network is removed.

  • CHANGED: TokenRatesController now removes state for chain IDs when their network is removed.

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

@bergeron bergeron marked this pull request as ready for review October 22, 2024 23:25
@bergeron bergeron requested a review from a team as a code owner October 22, 2024 23:25
@@ -319,7 +319,7 @@ export function divideIntoBatches<Value>(
}

/**
* Constructs an object from processing batches of the given values
* Constructs a result from processing batches of the given values
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I allowed this to reduce to something other than an object. In my case an array was useful. Saw no reason it couldn't be a non-object, same as a regular javascript reduce. Added tests.

import { reduceInBatchesSerially } from './assetsUtil';

// https://github.com/mds1/multicall/blob/main/deployments.json
const MULTICALL_CONTRACT_BY_CHAINID = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The multicall3 smart contract is introduced.

Simple but useful utility here. It accepts an array of calls, and executes them via a single RPC multicall if the chain supports it. And falls back to N requests otherwise.

I was initially using eth-balance-checker, but it was worse in 2 areas.

  • It requires the same list of tokens to be checked across all accounts. Which can blow up into a lot of unnecessary checks. e.g. you're watching vitalik.eth and now you have to check 1000 tokens on every account.

  • multicall3 is deployed on a lot more networks (~250)

@bergeron bergeron changed the title feat: Update token balances controller to new polling pattern + multicall feat: Update token balances controller for multichain Oct 22, 2024
@bergeron
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

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": "18.2.2-preview-5c534570",
  "@metamask-previews/address-book-controller": "6.0.1-preview-5c534570",
  "@metamask-previews/announcement-controller": "7.0.1-preview-5c534570",
  "@metamask-previews/approval-controller": "7.1.0-preview-5c534570",
  "@metamask-previews/assets-controllers": "39.0.0-preview-5c534570",
  "@metamask-previews/base-controller": "7.0.1-preview-5c534570",
  "@metamask-previews/build-utils": "3.0.1-preview-5c534570",
  "@metamask-previews/chain-controller": "0.1.3-preview-5c534570",
  "@metamask-previews/composable-controller": "9.0.1-preview-5c534570",
  "@metamask-previews/controller-utils": "11.4.0-preview-5c534570",
  "@metamask-previews/ens-controller": "14.0.1-preview-5c534570",
  "@metamask-previews/eth-json-rpc-provider": "4.1.5-preview-5c534570",
  "@metamask-previews/gas-fee-controller": "21.0.0-preview-5c534570",
  "@metamask-previews/json-rpc-engine": "10.0.0-preview-5c534570",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.4-preview-5c534570",
  "@metamask-previews/keyring-controller": "17.3.0-preview-5c534570",
  "@metamask-previews/logging-controller": "6.0.1-preview-5c534570",
  "@metamask-previews/message-manager": "11.0.0-preview-5c534570",
  "@metamask-previews/multichain": "0.0.0-preview-5c534570",
  "@metamask-previews/name-controller": "8.0.1-preview-5c534570",
  "@metamask-previews/network-controller": "22.0.0-preview-5c534570",
  "@metamask-previews/notification-controller": "7.0.0-preview-5c534570",
  "@metamask-previews/notification-services-controller": "0.12.0-preview-5c534570",
  "@metamask-previews/permission-controller": "11.0.2-preview-5c534570",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-5c534570",
  "@metamask-previews/phishing-controller": "12.0.3-preview-5c534570",
  "@metamask-previews/polling-controller": "11.0.0-preview-5c534570",
  "@metamask-previews/preferences-controller": "13.1.0-preview-5c534570",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-5c534570",
  "@metamask-previews/queued-request-controller": "5.1.0-preview-5c534570",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-5c534570",
  "@metamask-previews/selected-network-controller": "18.0.2-preview-5c534570",
  "@metamask-previews/signature-controller": "20.1.0-preview-5c534570",
  "@metamask-previews/transaction-controller": "37.3.0-preview-5c534570",
  "@metamask-previews/user-operation-controller": "16.0.0-preview-5c534570"
}

@bergeron bergeron marked this pull request as draft October 25, 2024 15:57
@bergeron
Copy link
Contributor Author

bergeron commented Nov 4, 2024

@metamaskbot publish-preview

Copy link
Contributor

github-actions bot commented Nov 4, 2024

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": "18.2.3-preview-651fe366",
  "@metamask-previews/address-book-controller": "6.0.1-preview-651fe366",
  "@metamask-previews/announcement-controller": "7.0.1-preview-651fe366",
  "@metamask-previews/approval-controller": "7.1.1-preview-651fe366",
  "@metamask-previews/assets-controllers": "42.0.0-preview-651fe366",
  "@metamask-previews/base-controller": "7.0.2-preview-651fe366",
  "@metamask-previews/build-utils": "3.0.1-preview-651fe366",
  "@metamask-previews/chain-controller": "0.1.3-preview-651fe366",
  "@metamask-previews/composable-controller": "9.0.1-preview-651fe366",
  "@metamask-previews/controller-utils": "11.4.2-preview-651fe366",
  "@metamask-previews/ens-controller": "15.0.0-preview-651fe366",
  "@metamask-previews/eth-json-rpc-provider": "4.1.6-preview-651fe366",
  "@metamask-previews/gas-fee-controller": "22.0.0-preview-651fe366",
  "@metamask-previews/json-rpc-engine": "10.0.1-preview-651fe366",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.5-preview-651fe366",
  "@metamask-previews/keyring-controller": "17.3.1-preview-651fe366",
  "@metamask-previews/logging-controller": "6.0.1-preview-651fe366",
  "@metamask-previews/message-manager": "11.0.1-preview-651fe366",
  "@metamask-previews/multichain": "0.0.0-preview-651fe366",
  "@metamask-previews/name-controller": "8.0.1-preview-651fe366",
  "@metamask-previews/network-controller": "22.0.1-preview-651fe366",
  "@metamask-previews/notification-controller": "7.0.0-preview-651fe366",
  "@metamask-previews/notification-services-controller": "0.12.1-preview-651fe366",
  "@metamask-previews/permission-controller": "11.0.3-preview-651fe366",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-651fe366",
  "@metamask-previews/phishing-controller": "12.3.0-preview-651fe366",
  "@metamask-previews/polling-controller": "12.0.1-preview-651fe366",
  "@metamask-previews/preferences-controller": "13.2.0-preview-651fe366",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-651fe366",
  "@metamask-previews/queued-request-controller": "7.0.0-preview-651fe366",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-651fe366",
  "@metamask-previews/selected-network-controller": "19.0.0-preview-651fe366",
  "@metamask-previews/signature-controller": "21.0.0-preview-651fe366",
  "@metamask-previews/transaction-controller": "38.2.0-preview-651fe366",
  "@metamask-previews/user-operation-controller": "17.0.0-preview-651fe366"
}

@bergeron bergeron merged commit 40f14dd into main Nov 13, 2024
120 checks passed
@bergeron bergeron deleted the brian/token-balances-controller branch November 13, 2024 19:12
@bergeron bergeron mentioned this pull request Nov 13, 2024
bergeron added a commit that referenced this pull request Nov 13, 2024
Major release of the assets controllers to include
#4782
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.

2 participants