Commit 579cb28
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- packages/assets-controllers
- src
3 files changed
+9
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1297 | 1297 | | |
1298 | 1298 | | |
1299 | 1299 | | |
1300 | | - | |
| 1300 | + | |
1301 | 1301 | | |
1302 | 1302 | | |
1303 | 1303 | | |
| |||
1518 | 1518 | | |
1519 | 1519 | | |
1520 | 1520 | | |
1521 | | - | |
| 1521 | + | |
1522 | 1522 | | |
1523 | 1523 | | |
1524 | 1524 | | |
| |||
1598 | 1598 | | |
1599 | 1599 | | |
1600 | 1600 | | |
1601 | | - | |
| 1601 | + | |
1602 | 1602 | | |
1603 | 1603 | | |
1604 | 1604 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
257 | 257 | | |
258 | 258 | | |
259 | 259 | | |
| 260 | + | |
260 | 261 | | |
261 | 262 | | |
262 | | - | |
| 263 | + | |
263 | 264 | | |
264 | 265 | | |
265 | 266 | | |
| |||
0 commit comments