Skip to content

Commit

Permalink
feat: Update token balances controller for multichain (#4782)
Browse files Browse the repository at this point in the history
## 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

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

## 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`

- **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
  • Loading branch information
bergeron authored Nov 13, 2024
1 parent 6859854 commit 40f14dd
Show file tree
Hide file tree
Showing 12 changed files with 1,386 additions and 447 deletions.
1 change: 1 addition & 0 deletions packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
},
"dependencies": {
"@ethereumjs/util": "^8.1.0",
"@ethersproject/abi": "^5.7.0",
"@ethersproject/address": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
Expand Down
Loading

0 comments on commit 40f14dd

Please sign in to comment.