MetaMask / core

This monorepo is a collection of packages used across multiple MetaMask clients
MIT License
291 stars 188 forks source link

Plan consolidated API for TokenDetectionController + DetectTokensController #1813

Closed mcmire closed 10 months ago

mcmire commented 1 year ago

Look into how TokenDetectionController (from this repo) is being used in the mobile app (including the patches for this controller in mobile) and how DetectTokensController is being used in the extension, and determine an API for a combined controller that satisfies the requirements that extension and mobile have.

MajorLift commented 11 months ago

Consolidated TokenDetectionController API

Constructor

Fields

Methods

polling-controller:

(remove start, stop)

core:

extension:

mobile:

const { tokens } = this.getTokensState();
...
if (tokens.length && !tokens[0].name) {
    this.updateTokensName(tokenList);
}

remove

mcmire commented 11 months ago

@MajorLift Is this essentially blocked by https://github.com/MetaMask/core/issues/3625? Would you need to revisit this once that's merged?

MajorLift commented 11 months ago

Any changes to the TokenDetectionController API that are made in #3625 will need to be reflected here as well, but it doesn't look like there will be any significant departures from what I have right now.

Mostly I need a sign-off on the plan in the comment above before I can get started on #3626. That can wait until after #3625 is merged, though.

mcmire commented 11 months ago

It is good that you've noted some of the ways that each implementation captures data internally, but the list is fairly large for what is a small controller. I wonder if it would be better to list the public API that we want to have rather than all of the private properties and methods that are present. How we end up storing data internally or refactoring the logic, we can decide when we create the consolidated version, but I don't know if that's important for now.

Here's what I see as the public API:

That said, I know I'm sort of changing the purpose of this ticket, but in addition to reviewing the public API, perhaps we should also take this time to audit the behavior between the three implementations as well, and capture a list of differences there. When creating a consolidated version of the controller I imagine we'll want to do some refactoring, so we'll want to make sure we preserve any nuances.

I see you've done this a bit already: you've alluded to the fact that DetectTokensController (extension implementation) keeps track of the locked/unlocked state of the wallet, and calls the method to detect tokens when the wallet is unlocked, and refuses to detect tokens when it's locked.

Besides this, it looks like there may be other slight differences in the constructor in TokenDetectionController vs. DetectTokensController. Specifically, in TokenDetectionController, when TokenListController state changes, the method to detect tokens is called only when the tokens collection is non-empty; but in DetectTokensController, it's always called.

Also, detectTokens in TokenDetectionController is slightly different from detectNewTokens in DetectTokensController:

mcmire commented 11 months ago

Between what you've noted and what I've noted, I imagine we probably have enough to go on, but if you find any other differences feel free to add them here.

I'll let you close this ticket when you feel you have enough information to proceed to #3626.

MajorLift commented 10 months ago

Notes: