Skip to content

Commit 7e18ff3

Browse files
fix: provide better native token names when bridging and swapping on networks removed
1 parent c41a715 commit 7e18ff3

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

ui/selectors/selectors.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ import {
6767
MOONRIVER_DISPLAY_NAME,
6868
TEST_NETWORK_IDS,
6969
FEATURED_NETWORK_CHAIN_IDS,
70+
CHAIN_ID_TO_CURRENCY_SYMBOL_MAP,
71+
NETWORK_TO_NAME_MAP,
7072
} from '../../shared/constants/network';
7173
import {
7274
WebHIDConnectedStatuses,
@@ -797,11 +799,12 @@ export function getTokensAcrossChainsByAccountAddress(state, selectedAddress) {
797799
* @param {string} chainId - Chain ID
798800
* @returns {object} Native token information
799801
*/
800-
function getNativeTokenInfo(state, chainId) {
802+
export function getNativeTokenInfo(state, chainId) {
801803
const { networkConfigurationsByChainId } = state.metamask;
802804

803805
const networkConfig = networkConfigurationsByChainId?.[chainId];
804806

807+
// Fill native token info by network config (if a user has a network added)
805808
if (networkConfig) {
806809
const symbol = networkConfig.nativeCurrency || AssetType.native;
807810
const decimals = 18;
@@ -814,6 +817,7 @@ function getNativeTokenInfo(state, chainId) {
814817
};
815818
}
816819

820+
// Fill native token info by DApp provider
817821
const { provider } = state.metamask;
818822
if (provider?.chainId === chainId) {
819823
const symbol = provider.ticker || AssetType.native;
@@ -827,6 +831,14 @@ function getNativeTokenInfo(state, chainId) {
827831
};
828832
}
829833

834+
// Attempt to retried native token info from hardcoded known networks
835+
const hardcodedSymbol = CHAIN_ID_TO_CURRENCY_SYMBOL_MAP[chainId];
836+
const hardcodedName = NETWORK_TO_NAME_MAP[chainId];
837+
if (hardcodedSymbol && hardcodedName) {
838+
return { symbol: hardcodedSymbol, decimals: 18, name: hardcodedName };
839+
}
840+
841+
// Fallback to "NATIVE" symbol as this is an unknown native token
830842
return { symbol: AssetType.native, decimals: 18, name: 'Native Token' };
831843
}
832844

ui/selectors/selectors.test.js

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

0 commit comments

Comments
 (0)