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

fix: multichainToken rate for non evm #5175

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

salimtb
Copy link
Contributor

@salimtb salimtb commented Jan 20, 2025


Explanation

This pull request introduces the MultiChainTokensRatesController, a new controller that manages multi‑chain token conversion rates within MetaMask. Its primary goal is to periodically poll for updated conversion rates of tokens associated with non‑EVM accounts (those using Snap metadata), ensuring that the conversion data remains up‑to‑date across supported chains.

Key changes and features include:

  • Polling Mechanism:
    The controller extends StaticIntervalPollingController to run periodic polls (default interval of 3 min). During each poll, it triggers an update of token conversion rates for the currently selected account if it is active (i.e. if the keyring is unlocked).

  • State Management & Mutex Locking:
    The controller stores conversion rates in its state (conversionRates), which is updated atomically using a mutex (from async-mutex) to prevent race conditions during concurrent updates.

  • Event Subscriptions:

    • Keyring Events: Listens to KeyringController:lock and KeyringController:unlock events to track if the keyring is active.
    • Accounts Events: Subscribes to AccountsController:selectedAccountChange to update the current account and trigger a conversion rate update, and to AccountsController:accountRemoved to clean up any state associated with a removed account.
  • Account Filtering:
    The controller retrieves all multichain accounts from the AccountsController and filters out non‑EVM accounts that have Snap metadata. Currently, only accounts of type 'solana:data-account' are supported for conversion updates, with a TODO note indicating future Snap support enhancements.

  • Integration with Snap:
    Conversion updates involve sending a Snap request using a helper method (#handleSnapRequest) to the SnapController. It builds a list of asset conversion pairs (from asset to USD via the Swift ISO4217 code) and then updates its state with the returned conversion rates.

  • Keyring Client Support:
    A dedicated helper (#getClient) instantiates a KeyringClient to route keyring-related JSON-RPC requests through the SnapController, ensuring a consistent integration with MetaMask’s underlying snap infrastructure.

  • Type Safety & Messaging:
    Comprehensive TypeScript types are provided for controller state, actions, events, and the messenger interface. This guarantees that interactions with other controllers (such as the AccountsController, KeyringController, and Snaps-related modules) are type‑safe and well‑documented.

Overall, this controller enhances the MetaMask architecture by adding robust support for multi‑chain tokens, ensuring that users with non‑EVM accounts receive updated conversion rate information in a timely and thread‑safe manner.


References

  • Fixes:

Changelog

@metamask/multi-chain-tokens-rates-controller

  • ADDED: New MultiChainTokensRatesController to manage and update token conversion rates for non‑EVM accounts with Snap metadata.

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

Feel free to adjust any section to better match the specifics of your implementation and project requirements.

@salimtb salimtb force-pushed the salim/solana-tokens-rate-controller-non-evm branch from 51c3893 to ac5d0e4 Compare January 20, 2025 13:01
@salimtb salimtb force-pushed the salim/solana-tokens-rate-controller-non-evm branch 4 times, most recently from 659ace0 to b000312 Compare February 3, 2025 09:35
}

// Retrieve assets from the assets controller.
const assets = await this.#getAssetsList(
Copy link
Contributor Author

@salimtb salimtb Feb 3, 2025

Choose a reason for hiding this comment

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

TODO: Once merged, replace this function with a call to MultichainAssetsController.getState to retrieve the list of assets by account.

@salimtb salimtb force-pushed the salim/solana-tokens-rate-controller-non-evm branch from b000312 to da502e6 Compare February 3, 2025 09:38
@salimtb salimtb self-assigned this Feb 3, 2025
@salimtb salimtb force-pushed the salim/solana-tokens-rate-controller-non-evm branch from da502e6 to 46d8b64 Compare February 3, 2025 10:22
@salimtb salimtb marked this pull request as ready for review February 3, 2025 10:32
@salimtb salimtb requested a review from a team as a code owner February 3, 2025 10:32
@salimtb salimtb force-pushed the salim/solana-tokens-rate-controller-non-evm branch from 9747ebd to 4165b5e Compare February 5, 2025 21:37
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.

5 participants