-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
Conversation
@@ -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 |
There was a problem hiding this comment.
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 = { |
There was a problem hiding this comment.
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)
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
…/core into brian/token-balances-controller
Major release of the assets controllers to include #4782
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
, andTokensController
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 fieldtokenBalances
, as a mapping from account address -> chain id -> token address -> balance.BREAKING: The
TokenBalancesController
now extendsStaticIntervalPollingController
, and the new polling APIstartPolling
must be used to initiate polling (startPolling
,stopPollingByPollingToken
).BREAKING:
TokenBalancesController
now requires subscriptions to thePreferencesController:stateChange
andNetworkController:stateChange
events. And access to theNetworkController:getNetworkClientById
,NetworkController:getState
,TokensController:getState
, andPreferencesController:getState
actions.BREAKING:
TokensController
requires a subscription to theNetworkController: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