Commit fc6641a
authored
fix: use currency rate fallback for native token fiat when market data missing (#7636)
## Explanation
The `selectAssetsBySelectedAccountGroup` selector was returning
`undefined` for the `fiat` field on native tokens for certain chains
(like Ink chain `0xdef1`) even though the currency conversion rate was
available.
**Problem:**
The `getFiatBalanceForEvmToken` function requires
`marketData[chainId][tokenAddress]` to exist in order to calculate fiat
values. For native tokens, this market data entry should contain `price:
1` (since 1 ETH = 1 ETH). However, some chains don't have this native
token entry in `marketData`, causing the function to return `undefined`
even when `currencyRates` has the conversion rate for the native
currency symbol (e.g., ETH).
**Solution:**
Added a fallback mechanism specifically for native tokens: when
`marketData` doesn't have an entry for the native token but
`currencyRates` has the conversion rate for the native currency symbol,
the function now uses `price = 1` and multiplies directly by the
currency rate. This ensures native token fiat values are calculated
correctly for chains like Ink (`0xdef1`) that use ETH as their native
currency but don't have explicit market data entries.
UI extension: MetaMask/metamask-extension#39269
## References
- Fixes native token fiat calculation for Ink chain and similar L2s that
share the same native currency as mainnet but lack market data entries
## Checklist
- [x] 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/processes/updating-changelogs.md)
- [ ] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Ensures native token fiat is calculated even when market data is
absent by leveraging the native currency conversion rate.
>
> - Update `getFiatBalanceForEvmToken` to accept `nativeCurrencySymbol`
and, when no market data exists for a native token, compute fiat using
`currencyRates` (price=1 in native units); return `undefined` if rate
also missing
> - Pass native currency from `selectAssetsBySelectedAccountGroup` into
`getFiatBalanceForEvmToken`
> - Add unit tests covering fallback calculation and missing-rate
behavior
> - Update `CHANGELOG.md` under Unreleased -> Fixed
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
7f46dc3. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 5972fe0 commit fc6641a
File tree
3 files changed
+113
-0
lines changed- packages/assets-controllers
- src/selectors
3 files changed
+113
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
14 | 18 | | |
15 | 19 | | |
16 | 20 | | |
| |||
Lines changed: 87 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1024 | 1024 | | |
1025 | 1025 | | |
1026 | 1026 | | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
1027 | 1114 | | |
1028 | 1115 | | |
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| 211 | + | |
211 | 212 | | |
212 | 213 | | |
213 | 214 | | |
| |||
564 | 565 | | |
565 | 566 | | |
566 | 567 | | |
| 568 | + | |
567 | 569 | | |
568 | 570 | | |
569 | 571 | | |
| |||
573 | 575 | | |
574 | 576 | | |
575 | 577 | | |
| 578 | + | |
576 | 579 | | |
577 | 580 | | |
578 | 581 | | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
579 | 601 | | |
580 | 602 | | |
581 | 603 | | |
| |||
0 commit comments