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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

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.

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.

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

1 participant