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(assets-controllers): Drop support for updating arbitary token rates #3639

Merged
merged 1 commit into from
Dec 8, 2023

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Dec 7, 2023

Explanation

Updating and polling for token rates for arbitrary tokens is no longer supported. Instead the user's token list (from the TokensController) always dictates which tokens will be tracked by the TokenRatesController. See #3637 for more details.

This simplifies the API, dramatically simplifies the complexity of testing this controller thoroughly, and it resolves a recently introduced bug (#3638).

Rates from non-tracked tokens can still be retrieved by calling the price service directly, using the token-prices-service module.

References

Resolves #3637
Fixes #3638

Changelog

@metamask/assets-controllers

  • BREAKING: The TokenRatesController now only supports updating and polling rates for tokens tracked by the TokensController
    • The tokenAddresses option has been removed from startPollingByNetworkClientId
    • The tokenContractAddresses option has been removed from updateExchangeRatesByChainId

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

Updating and polling for token rates for arbitrary tokens is no longer
supported. Instead the user's token list (from the `TokensController`)
always dictates which tokens will be tracked by the
`TokenRatesController`. See #3637 for more details.�

This simplifies the API, dramatically simplifies the complexity of
testing this controller thoroughly, and it resolves a recently
introduced bug (#3638).

Rates from non-tracked tokens can still be retrieved by calling the
price service directly, using the `token-prices-service` module.

Resolves #3637
Fixes #3638
@Gudahtt Gudahtt marked this pull request as ready for review December 7, 2023 23:47
@Gudahtt Gudahtt requested a review from a team as a code owner December 7, 2023 23:47
@@ -127,7 +128,12 @@ describe('TokenRatesController', () => {
allTokens: {
'0x1': {
[defaultSelectedAddress]: [
{ address: 'bar', decimals: 0, symbol: '', aggregators: [] },
{
address: mockTokenAddress,
Copy link
Member Author

Choose a reason for hiding this comment

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

I had to update a bunch of test fixtures to use hex addresses rather than non-hex strings, as it threw an error when they were passed to toHex

* @param chainId - The chain ID.
* @returns The list of tokens addresses for the current chain
*/
#getTokenAddresses(chainId: Hex): Hex[] {
Copy link
Member Author

Choose a reason for hiding this comment

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

I decided to drop the token list in favor of this function. This gives us exactly what we need (including the token => address mapping), doesn't require careful updating to keep in-sync, and doesn't take up additional memory (which might have been a concern if we tracked lists for all chains).

cc @mcmire , wanted to make sure you see this because for the "prevent redundant updates" issue, the solution made in the mobile patch used the updateTokenList function to determine whether the tokens changed. I think we can still do something similar with this pattern though, by calling this function before and after to see if it changed.

this.tokenList = [...tokens, ...detectedTokens];
allDetectedTokens[chainId]?.[this.config.selectedAddress] || [];

return [...tokens, ...detectedTokens].map((token) => toHex(token.address));
Copy link
Member Author

Choose a reason for hiding this comment

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

Here is the toHex call that caused unit test issues

const { chainId, nativeCurrency } = this.config;
const tokenContractAddresses = this.tokenList.map(
(token) => token.address,
) as Hex[];
Copy link
Member Author

Choose a reason for hiding this comment

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

Here is the as Hex call that was the reason we didn't need toHex prior to now

Copy link
Contributor

@jiexi jiexi left a comment

Choose a reason for hiding this comment

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

beautiful. thank you for cleaning this up 💪

@Gudahtt Gudahtt merged commit 420237e into main Dec 8, 2023
136 checks passed
@Gudahtt Gudahtt deleted the drop-support-for-updating-arbitrary-token-rates branch December 8, 2023 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants