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

Integrate PollingController into TokenRatesController #2015

Merged
merged 70 commits into from
Nov 17, 2023

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Nov 8, 2023

Explanation

Integrates PollingController into TokensRatesController, keeping as much of the current method interfaces intact. Needed for eventual multichain support.

References

Fixes: https://github.com/MetaMask/MetaMask-planning/issues/1122

Changelog

@metamask/assets-controllers

  • BREAKING: TokenRatesController constructor params now requires getNetworkClientById
  • ADDED: TokenRatesController state now has contractExchangeRatesByChainId property which an object keyed by chainId and nativeCurrency
    • tokenRatesController.state.contractExchangeRatesByChainId[chainId][nativeCurrency]
  • ADDED: TokenRatesController constructor params now accepts optional interval and threshold
  • ADDED: Add updateExchangeRatesByChainId, _executePoll, startPollingByNetworkClientId, stopAllPolling, and stopPollingByPollingToken methods to TokenRatesController
  • CHANGED: TokenRatesController.fetchExchangeRate() now accepts an optional tokenAddresses as the last parameter
  • CHANGED: TokenRatesController.getChainSlug() now accepts an optional tokenAddresses parameter
  • CHANGED: TokenRatesController.fetchAndMapExchangeRates() now accepts an optional tokenAddresses as the last parameter

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

…p-1025/multichain-v1-currency-rate-controller

# Conflicts:
#	packages/network-controller/src/NetworkController.ts
#	packages/network-controller/tests/NetworkController.test.ts
…p-1025/multichain-v1-currency-rate-controller
* should be used to query token exchange rates.
* @param tokenAddresses - The addresses for the token contracts.
* @returns An object with conversion rates for each token
* related to the network's native currency.
*/
async fetchAndMapExchangeRates(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be made private? I'm not seeing it used by either of the clients or other controllers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

typescript wouldn't be happy with that since these methods are tested directly in spec

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not seeing this one in the test file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

describe('fetchAndMapExchangeRates', () => {

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok lets make this private in a subsequent PR per @mcmire 's suggestion

* @param vsCurrency - Query according to tokens in tokenList and native currency.
* @param chainSlug - The chain string identifier.
* @param vsCurrency - The currency used to generate pairs against the tokens.
* @param tokenAddresses - The addresses for the token contracts.
* @returns The exchange rates for the given pairs.
*/
async fetchExchangeRate(
Copy link
Contributor

Choose a reason for hiding this comment

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

Also can this be made private? Helps to keep track of what changes should be considered and breaking and how to think about the method signatures

Copy link
Contributor

Choose a reason for hiding this comment

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

This one is stubbed out a few times but we should be able to test the public methods without referring to it. I know its kindof annoying but it will make our tests better and this controller better if we make it private.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

had to do this because i couldn't get nock/fetch to play nicely with fake timers, I can take another look at this again though

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok lets make this private in a subsequent PR per @mcmire 's suggestion

*/
async getChainSlug(): Promise<string | null> {
const { threshold, chainId } = this.config;
async getChainSlug(
Copy link
Contributor

Choose a reason for hiding this comment

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

Another one we can make private.

Copy link
Contributor

Choose a reason for hiding this comment

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

I also don't see this one referenced in the test file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

describe('getChainSlug', () => {

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh you're adding them in your PR... I was looking on main... so yea I'm sorry cuz you put in the effort to create tests for these but really they are essentially private methods that just weren't marked as such and I feel like we should mark them as private now. Lets see what @mcmire and @Gudahtt think?

Copy link
Member

@Gudahtt Gudahtt Nov 16, 2023

Choose a reason for hiding this comment

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

Less API surface area generally sounds good to me!

Alternatively, if we did want proper test coverage of an internal method we could pull it out into a separate module, and test it that way. Then it can still be private (in that it's not exported by the package), but will follow our convention of reducing API surface area and not testing private things.

The guideline of not testing private methods is really a nudge to organize code into more cohesive chunks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Lets make this private in a subsequent PR per @mcmire 's suggestion

Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

Looks great. Just a few small changes requested.

Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

LGTM!

@jiexi
Copy link
Contributor Author

jiexi commented Nov 17, 2023

Tracking remaining cleanup for marking methods private and updating specs in this issue
#2060

@jiexi jiexi merged commit 5a8639f into main Nov 17, 2023
128 checks passed
@jiexi jiexi deleted the jl/mmp-1122/mutlichain-v1-token-rates-controller branch November 17, 2023 21:12
@mcmire
Copy link
Contributor

mcmire commented Nov 17, 2023

FYI I've added this PR to the next release: #2063.

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.

6 participants