feat: migrate price api to v3 #7033
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Explanation
What is the current state of things and why does it need to change?
The
TokenRatesControllerwas using the single-chain/v1/spot-pricesendpoint from the Codefi Price API, which required making separate API calls for each chain when fetching token prices across multiple networks. This approach was inefficient and created unnecessary network overhead, especially as MetaMask supports an increasing number of chains.What is the solution your changes offer and how does it work?
This PR migrates the
TokenRatesControllerto use the multichain/v3/spot-pricesendpoint, which allows fetching token prices for multiple chains in a single API request. The key improvements include:eip155:{chainId}/slip44:{coinType}(e.g.,eip155:1/slip44:60for ETH) instead of using zero addressesAre there any changes whose purpose might not obvious to those unfamiliar with the domain?
SLIP44 integration: Added
@metamask/slip44dependency to properly generate CAIP asset IDs for native tokens. SLIP44 defines standardized coin type numbers (e.g., 60 for ETH, 966 for MATIC) that the v3 API requires for native token identification.CAIP asset ID parsing: Added helper functions to handle both ERC20 token format (
eip155:1:0xA0b86a33E6441B6F3A82F1a2b1F1D7a8F2C8A3E8) and native token SLIP44 format (eip155:1/slip44:60) when processing API responses.Interface extension: Made
fetchMultichainTokenPricesa required method inAbstractTokenPricesServiceto ensure consistent multichain support across implementations.If your primary goal was to update one package but you found you had to update another one along the way, why did you do so?
The primary changes were in
assets-controllers, but I had to update multiple test files (TokenRatesController.test.ts,CurrencyRateController.test.ts,TokenSearchDiscoveryDataController.test.ts,assetsUtil.test.ts) to add mock implementations of the newfetchMultichainTokenPricesmethod, ensuring all tests continue to pass with the updated interface.If you had to upgrade a dependency, why did you do so?
Added
@metamask/slip44: ^4.3.0as a local dependency toassets-controllersto access standardized SLIP44 coin type constants needed for generating proper CAIP asset IDs for native tokens as required by the v3 Price API.References
Checklist