Skip to content

Commit 8e86618

Browse files
fix: provide better native token names when bridging and swapping removed networks (#32293) cp-12.17.0 (#32300)
CP: #32293 <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** This ensures we handle better native token display names when you bridge or swap tokens for networks you have removed or not added. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/32293?quickstart=1) ## **Related issues** Fixes: https://consensyssoftware.atlassian.net/browse/MMS-2293 ## **Manual testing steps** 1. Add BASE network (if you have native tokens in base, otherwise use a different network you have native tokens for) 2. Try to bridge to BASE - you should see the native token 3. Remove BASE 4. Try to bridge to BASE - you should see the correct native token still displayed ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** https://www.loom.com/share/812617be0e6e4101b777ff56c367ce0a ### **After** https://www.loom.com/share/1df5613c2b274314b2839bc143a3cbad?sid=a7776357-eab8-473d-b2a6-2da71f3c4107 ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/32300?quickstart=1) ## **Related issues** Fixes: ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --------- Co-authored-by: Prithpal Sooriya <prithpal.sooriya@gmail.com>
1 parent 95864ba commit 8e86618

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
113113
- Fix: Removed timeout for messages requiring user actions ([#32046](https://github.com/MetaMask/metamask-extension/pull/32046))
114114
- Fix: Unable to send transaction low / high nonce transaction on any network when STX is enabled ([#32244](https://github.com/MetaMask/metamask-extension/pull/32244))
115115
- Fix: Ensure no duplicate accounts are persisted ([#32287](https://github.com/MetaMask/metamask-extension/pull/32287))
116+
- Fix: provide better native token names when bridging and swapping removed networks([#32293](https://github.com/MetaMask/metamask-extension/pull/32293))
116117

117118
## [12.16.1]
118119
### Added

ui/selectors/selectors.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ import {
6363
MOONRIVER_DISPLAY_NAME,
6464
TEST_NETWORK_IDS,
6565
FEATURED_NETWORK_CHAIN_IDS,
66+
CHAIN_ID_TO_CURRENCY_SYMBOL_MAP,
67+
NETWORK_TO_NAME_MAP,
6668
} from '../../shared/constants/network';
6769
import {
6870
WebHIDConnectedStatuses,
@@ -792,11 +794,12 @@ export function getTokensAcrossChainsByAccountAddress(state, selectedAddress) {
792794
* @param {string} chainId - Chain ID
793795
* @returns {object} Native token information
794796
*/
795-
function getNativeTokenInfo(state, chainId) {
797+
export function getNativeTokenInfo(state, chainId) {
796798
const { networkConfigurationsByChainId } = state.metamask;
797799

798800
const networkConfig = networkConfigurationsByChainId?.[chainId];
799801

802+
// Fill native token info by network config (if a user has a network added)
800803
if (networkConfig) {
801804
const symbol = networkConfig.nativeCurrency || AssetType.native;
802805
const decimals = 18;
@@ -809,6 +812,7 @@ function getNativeTokenInfo(state, chainId) {
809812
};
810813
}
811814

815+
// Fill native token info by DApp provider
812816
const { provider } = state.metamask;
813817
if (provider?.chainId === chainId) {
814818
const symbol = provider.ticker || AssetType.native;
@@ -822,6 +826,14 @@ function getNativeTokenInfo(state, chainId) {
822826
};
823827
}
824828

829+
// Attempt to retried native token info from hardcoded known networks
830+
const hardcodedSymbol = CHAIN_ID_TO_CURRENCY_SYMBOL_MAP[chainId];
831+
const hardcodedName = NETWORK_TO_NAME_MAP[chainId];
832+
if (hardcodedSymbol && hardcodedName) {
833+
return { symbol: hardcodedSymbol, decimals: 18, name: hardcodedName };
834+
}
835+
836+
// Fallback to "NATIVE" symbol as this is an unknown native token
825837
return { symbol: AssetType.native, decimals: 18, name: 'Native Token' };
826838
}
827839

ui/selectors/selectors.test.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,3 +2386,101 @@ describe('#getConnectedSitesList', () => {
23862386
});
23872387
});
23882388
});
2389+
2390+
describe('getNativeTokenInfo', () => {
2391+
const arrange = () => {
2392+
const state = {
2393+
metamask: {
2394+
networkConfigurationsByChainId: {},
2395+
provider: {},
2396+
},
2397+
};
2398+
2399+
return { state };
2400+
};
2401+
2402+
it('provides native token info from a network a user has added', () => {
2403+
const mocks = arrange();
2404+
mocks.state.metamask.networkConfigurationsByChainId['0x1337'] = {
2405+
nativeCurrency: 'HELLO',
2406+
name: 'MyToken',
2407+
};
2408+
2409+
const result = selectors.getNativeTokenInfo(mocks.state, '0x1337');
2410+
expect(result).toStrictEqual({
2411+
symbol: 'HELLO',
2412+
decimals: 18,
2413+
name: 'MyToken',
2414+
});
2415+
});
2416+
2417+
it('provides native token info from a network added but with fallbacks for missing fields', () => {
2418+
const mocks = arrange();
2419+
mocks.state.metamask.networkConfigurationsByChainId['0x1337'] = {
2420+
nativeCurrency: undefined,
2421+
name: undefined,
2422+
};
2423+
2424+
const result = selectors.getNativeTokenInfo(mocks.state, '0x1337');
2425+
expect(result).toStrictEqual({
2426+
symbol: 'NATIVE',
2427+
decimals: 18,
2428+
name: 'Native Token',
2429+
});
2430+
});
2431+
2432+
it('provides native token from DApp provider', () => {
2433+
const mocks = arrange();
2434+
mocks.state.metamask.provider = {
2435+
chainId: '0x1337',
2436+
ticker: 'HELLO',
2437+
nativeCurrency: { decimals: 18 },
2438+
nickname: 'MyToken',
2439+
};
2440+
2441+
const result = selectors.getNativeTokenInfo(mocks.state, '0x1337');
2442+
expect(result).toStrictEqual({
2443+
symbol: 'HELLO',
2444+
decimals: 18,
2445+
name: 'MyToken',
2446+
});
2447+
});
2448+
2449+
it('provides native token from DApp provider but with fallbacks for missing fields', () => {
2450+
const mocks = arrange();
2451+
mocks.state.metamask.provider = {
2452+
chainId: '0x1337',
2453+
ticker: undefined,
2454+
nativeCurrency: undefined,
2455+
nickname: undefined,
2456+
};
2457+
2458+
const result = selectors.getNativeTokenInfo(mocks.state, '0x1337');
2459+
expect(result).toStrictEqual({
2460+
symbol: 'NATIVE',
2461+
decimals: 18,
2462+
name: 'Native Token',
2463+
});
2464+
});
2465+
2466+
it('provides native token from known list of hardcoded native tokens', () => {
2467+
const mocks = arrange();
2468+
2469+
const result = selectors.getNativeTokenInfo(mocks.state, '0x89');
2470+
expect(result).toStrictEqual({
2471+
symbol: 'POL',
2472+
decimals: 18,
2473+
name: 'Polygon',
2474+
});
2475+
});
2476+
2477+
it('fallbacks for unknown native token info', () => {
2478+
const mocks = arrange();
2479+
const result = selectors.getNativeTokenInfo(mocks.state, '0xFakeToken');
2480+
expect(result).toStrictEqual({
2481+
symbol: 'NATIVE',
2482+
decimals: 18,
2483+
name: 'Native Token',
2484+
});
2485+
});
2486+
});

0 commit comments

Comments
 (0)