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: Integrate new TokenListController polling pattern [Core] #4878

Merged
merged 14 commits into from
Nov 5, 2024

Conversation

gambinish
Copy link
Contributor

@gambinish gambinish commented Oct 31, 2024

Explanation

TokenListController is currently responsible for maintaining a list of all tokens per chain. This dataset is accessible via tokenList state variable in metamask state. After this task is complete, this token list will migrate it's polling pattern to leverage the base class, to execute a single poll per chain, rather than on an interval for all chains


Move TokenListController away from being scoped to a single polling loop, to executing individual polling loops per chain, allowing it to be more UI based in its controls.

The controller will accept PollingInputs from the extension from various UI elements, and will be responsible for starting, stopping, and deduping polls as needed. Deduping is baked into the inherited base class StaticIntervalPollingController

Here is the corresponding PR in extension that consumes and integrates with this controller. There will also be an additional mobile PR at some point to implement something similar.

References

https://github.com/MetaMask/MetaMask-planning/issues/3429

Changelog

@metamask/package-a

  • : Your change here
  • : Your change here

@metamask/package-b

  • : Your change here
  • : Your change here

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

@gambinish gambinish changed the title feat: Init polling controller updates feat: TokenListController polling updates Oct 31, 2024
@gambinish gambinish changed the title feat: TokenListController polling updates feat: Integrate new TokenListController polling pattern [Core] Nov 1, 2024
@gambinish
Copy link
Contributor Author

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

@gambinish gambinish marked this pull request as ready for review November 5, 2024 01:46
@gambinish gambinish requested a review from a team as a code owner November 5, 2024 01:46
@@ -204,104 +204,110 @@ export class TokenListController extends StaticIntervalPollingController<TokenLi
tokenList: this.state.tokensChainsCache[this.chainId]?.data || {},
};
});
Copy link
Contributor

Choose a reason for hiding this comment

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

We discussed it's probably a good idea to remove entries when their network is deleted. But I don't think it's necessary to do that now.

Untested example:

    const chainIdsToRemove = [] as Hex[];
    for (const chainId in Object.keys(this.state.tokensChainsCache)) {
      if (!networkControllerState.networkConfigurationsByChainId[chainId as Hex]) {
        chainIdsToRemove.push(chainId as Hex)
      }
    }
    if (chainIdsToRemove.length > 0) {
      this.update(() => {
        return {
          ...this.state,
          tokensChainsCache: chainIdsToRemove.reduce((acc, chainId) => {
            delete acc[chainId]
            return acc
          }, this.state.tokensChainsCache)
        }
      })
    }

*/
async fetchTokenList(networkClientId?: NetworkClientId): Promise<void> {
async fetchTokenList(chainId: Hex): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

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

What are your thoughts on continuing to take a network client ID until we remove the global network completely? My thought is that clients might find it easier to pass this instead of a chain ID. The selected network client ID can be easily obtained from NetworkController state as selectedNetworkClientId and accessed within a Redux selector, while the client must obtain the chain ID by either doing the same thing that's happening here (use the messenger to get the network client by its ID, then get the chain ID that corresponds to it), or if within a Redux selector, loop through the networkConfigurationsByChainId to find the network configuration that matches the selectedNetworkClientId and then get the chainId off of that.

Ultimately I guess it depends on what the clients are currently doing. If we already have a way of accessing the current chain ID in a more or les type-safe way, then no worries. So, maybe this is change is just fine, but I wanted to double-check?

Copy link
Contributor Author

@gambinish gambinish Nov 5, 2024

Choose a reason for hiding this comment

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

I think that the move towards passing in the chain ID, rather than an networkClientId was an intentional one. You're right, it's one of the steps towards removing the global network selector entirely. Yes, the clients are currently being refactored to now execute a single poll per ChainId.

I'll defer to Brian here for more on these design decisions, but the pattern with the other AssetsControllers are also moving towards this same way of passing chainID instead of networkClientId.

Copy link
Contributor

Choose a reason for hiding this comment

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

We're going to fetch token lists across all chains rather than just the current chain, so from the perspective of token lists we already no longer care about the current network. It'll just do Object.keys(networkConfigurationsByChainId) and fetch for each chain id. So the chain id input is already more convenient.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh okay! Sounds good then. Thanks for the reply.

@gambinish gambinish merged commit d664234 into main Nov 5, 2024
120 checks passed
@gambinish gambinish deleted the 3429--tokenlist-polling-controller branch November 5, 2024 19:32
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