Skip to content

Commit 579cb28

Browse files
authored
fix: prevent duplicate native token address in token prices API request (#7181)
## Explanation **What is the current state of things and why does it need to change?** The `CurrencyRateController` has a fallback mechanism that uses spot prices from the token prices service when the primary exchange rates API fails. However, this fallback was sending duplicate native token addresses (the 0x0000...0000 address) to the API endpoint, causing requests to fail with the error: `"All tokenAddresses's elements must be unique"`. **What is the solution your changes offer and how does it work?** The issue occurred because: 1. The `CurrencyRateController` was explicitly passing the native token address in the `tokenAddresses` array: `tokenAddresses: [nativeTokenAddress]` 2. The `fetchTokenPrices` method in `CodefiTokenPricesServiceV2` automatically prepends the native token address to any token addresses passed to it (line 496 in `codefi-v2.ts`) 3. This resulted in the native token address appearing twice in the API URL The fix changes `tokenAddresses: [nativeTokenAddress]` to `tokenAddresses: []` since the service already handles including the native token address automatically. This ensures each address appears only once in the final API request. **Are there any changes whose purpose might not be obvious to those unfamiliar with the domain?** The empty array `[]` might seem counterintuitive, but it's the correct approach because the `fetchTokenPrices` method is designed to always include the native token address by default for any chain. ## References Fixes the duplicate token address validation error in the spot price fallback mechanism introduced after removing the CryptoCompare fallback in #7167. ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Stops sending the native token address explicitly to `fetchTokenPrices` (pass `tokenAddresses: []`) to avoid duplicates in spot price fallback requests. > > - **CurrencyRateController**: > - Update fallback spot price logic to call `fetchTokenPrices({ chainId, tokenAddresses: [], currency })`, relying on the service to include the native token automatically. > - **Tests**: > - Adjust expectations in `CurrencyRateController.test.ts` to assert `tokenAddresses: []` for ETH/BNB/POL scenarios and related calls. > - **Changelog**: > - Add Unreleased fix entry describing duplicate native token address issue in spot price fallback. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 64c9a6b. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 68aced1 commit 579cb28

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

packages/assets-controllers/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fix duplicate native token address in `CurrencyRateController` spot price fallback API requests ([#7181](https://github.com/MetaMask/core/pull/7181))
13+
1014
## [89.0.0]
1115

1216
### Changed

packages/assets-controllers/src/CurrencyRateController.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ describe('CurrencyRateController', () => {
12971297
expect(fetchTokenPricesSpy).toHaveBeenCalledTimes(1);
12981298
expect(fetchTokenPricesSpy).toHaveBeenCalledWith({
12991299
chainId: '0x1', // First chainId with ETH as native currency
1300-
tokenAddresses: ['0x0000000000000000000000000000000000000000'],
1300+
tokenAddresses: [],
13011301
currency: 'usd',
13021302
});
13031303

@@ -1518,7 +1518,7 @@ describe('CurrencyRateController', () => {
15181518
expect(fetchTokenPricesSpy).toHaveBeenCalledTimes(1);
15191519
expect(fetchTokenPricesSpy).toHaveBeenCalledWith({
15201520
chainId: '0x1',
1521-
tokenAddresses: ['0x0000000000000000000000000000000000000000'],
1521+
tokenAddresses: [],
15221522
currency: 'usd',
15231523
});
15241524

@@ -1598,7 +1598,7 @@ describe('CurrencyRateController', () => {
15981598
// Should use Polygon's native token address (line 269)
15991599
expect(fetchTokenPricesSpy).toHaveBeenCalledWith({
16001600
chainId: '0x89',
1601-
tokenAddresses: ['0x0000000000000000000000000000000000001010'],
1601+
tokenAddresses: [],
16021602
currency: 'usd',
16031603
});
16041604

packages/assets-controllers/src/CurrencyRateController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,10 @@ export class CurrencyRateController extends StaticIntervalPollingController<Curr
257257
const ratesResults = await Promise.allSettled(
258258
currencyToChainIdsEntries.map(async ([nativeCurrency, { chainId }]) => {
259259
const nativeTokenAddress = getNativeTokenAddress(chainId);
260+
// Pass empty array as fetchTokenPrices automatically includes the native token address
260261
const tokenPrices = await this.#tokenPricesService.fetchTokenPrices({
261262
chainId,
262-
tokenAddresses: [nativeTokenAddress],
263+
tokenAddresses: [],
263264
currency: currentCurrency,
264265
});
265266

0 commit comments

Comments
 (0)