1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Add testnet name as prefix for native currency of preloaded testnets (#14454)

* add testnet name as prefix for native currency of preloaded testnets
This commit is contained in:
Alex Donesky 2022-04-21 14:09:41 -05:00 committed by GitHub
parent c25c0432ed
commit fbeae57247
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 21 deletions

View File

@ -18,6 +18,7 @@ import {
MAINNET_CHAIN_ID, MAINNET_CHAIN_ID,
RINKEBY_CHAIN_ID, RINKEBY_CHAIN_ID,
INFURA_BLOCKED_KEY, INFURA_BLOCKED_KEY,
TEST_NETWORK_TICKER_MAP,
} from '../../../../shared/constants/network'; } from '../../../../shared/constants/network';
import { SECOND } from '../../../../shared/constants/time'; import { SECOND } from '../../../../shared/constants/time';
import { import {
@ -41,7 +42,11 @@ if (process.env.IN_TEST) {
nickname: 'Localhost 8545', nickname: 'Localhost 8545',
}; };
} else if (process.env.METAMASK_DEBUG || env === 'test') { } else if (process.env.METAMASK_DEBUG || env === 'test') {
defaultProviderConfigOpts = { type: RINKEBY, chainId: RINKEBY_CHAIN_ID }; defaultProviderConfigOpts = {
type: RINKEBY,
chainId: RINKEBY_CHAIN_ID,
ticker: TEST_NETWORK_TICKER_MAP.rinkeby,
};
} else { } else {
defaultProviderConfigOpts = { type: MAINNET, chainId: MAINNET_CHAIN_ID }; defaultProviderConfigOpts = { type: MAINNET, chainId: MAINNET_CHAIN_ID };
} }
@ -296,12 +301,12 @@ export default class NetworkController extends EventEmitter {
INFURA_PROVIDER_TYPES.includes(type), INFURA_PROVIDER_TYPES.includes(type),
`Unknown Infura provider type "${type}".`, `Unknown Infura provider type "${type}".`,
); );
const { chainId } = NETWORK_TYPE_TO_ID_MAP[type]; const { chainId, ticker } = NETWORK_TYPE_TO_ID_MAP[type];
this.setProviderConfig({ this.setProviderConfig({
type, type,
rpcUrl: '', rpcUrl: '',
chainId, chainId,
ticker: 'ETH', ticker: ticker ?? 'ETH',
nickname: '', nickname: '',
}); });
} }

View File

@ -396,7 +396,10 @@ export default class MetamaskController extends EventEmitter {
this.currencyRateController = new CurrencyRateController({ this.currencyRateController = new CurrencyRateController({
includeUSDRate: true, includeUSDRate: true,
messenger: currencyRateMessenger, messenger: currencyRateMessenger,
state: initState.CurrencyController, state: {
...initState.CurrencyController,
nativeCurrency: this.networkController.providerStore.getState().ticker,
},
}); });
const tokenListMessenger = this.controllerMessenger.getRestricted({ const tokenListMessenger = this.controllerMessenger.getRestricted({

View File

@ -1,3 +1,5 @@
import { capitalize } from 'lodash';
export const ROPSTEN = 'ropsten'; export const ROPSTEN = 'ropsten';
export const RINKEBY = 'rinkeby'; export const RINKEBY = 'rinkeby';
export const KOVAN = 'kovan'; export const KOVAN = 'kovan';
@ -79,16 +81,45 @@ export const TEST_CHAINS = [
LOCALHOST_CHAIN_ID, LOCALHOST_CHAIN_ID,
]; ];
export const TEST_NETWORK_TICKER_MAP = {
[ROPSTEN]: `${capitalize(ROPSTEN)}${ETH_SYMBOL}`,
[RINKEBY]: `${capitalize(RINKEBY)}${ETH_SYMBOL}`,
[KOVAN]: `${capitalize(KOVAN)}${ETH_SYMBOL}`,
[GOERLI]: `${capitalize(GOERLI)}${ETH_SYMBOL}`,
};
/** /**
* Map of all build-in Infura networks to their network and chain IDs. * Map of all build-in Infura networks to their network, ticker and chain IDs.
*/ */
export const NETWORK_TYPE_TO_ID_MAP = { export const NETWORK_TYPE_TO_ID_MAP = {
[ROPSTEN]: { networkId: ROPSTEN_NETWORK_ID, chainId: ROPSTEN_CHAIN_ID }, [ROPSTEN]: {
[RINKEBY]: { networkId: RINKEBY_NETWORK_ID, chainId: RINKEBY_CHAIN_ID }, networkId: ROPSTEN_NETWORK_ID,
[KOVAN]: { networkId: KOVAN_NETWORK_ID, chainId: KOVAN_CHAIN_ID }, chainId: ROPSTEN_CHAIN_ID,
[GOERLI]: { networkId: GOERLI_NETWORK_ID, chainId: GOERLI_CHAIN_ID }, ticker: TEST_NETWORK_TICKER_MAP[ROPSTEN],
[MAINNET]: { networkId: MAINNET_NETWORK_ID, chainId: MAINNET_CHAIN_ID }, },
[LOCALHOST]: { networkId: LOCALHOST_NETWORK_ID, chainId: LOCALHOST_CHAIN_ID }, [RINKEBY]: {
networkId: RINKEBY_NETWORK_ID,
chainId: RINKEBY_CHAIN_ID,
ticker: TEST_NETWORK_TICKER_MAP[RINKEBY],
},
[KOVAN]: {
networkId: KOVAN_NETWORK_ID,
chainId: KOVAN_CHAIN_ID,
ticker: TEST_NETWORK_TICKER_MAP[KOVAN],
},
[GOERLI]: {
networkId: GOERLI_NETWORK_ID,
chainId: GOERLI_CHAIN_ID,
ticker: TEST_NETWORK_TICKER_MAP[GOERLI],
},
[MAINNET]: {
networkId: MAINNET_NETWORK_ID,
chainId: MAINNET_CHAIN_ID,
},
[LOCALHOST]: {
networkId: LOCALHOST_NETWORK_ID,
chainId: LOCALHOST_CHAIN_ID,
},
}; };
export const NETWORK_TO_NAME_MAP = { export const NETWORK_TO_NAME_MAP = {
@ -207,19 +238,19 @@ export const BUYABLE_CHAINS_MAP = {
}, },
}, },
[ROPSTEN_CHAIN_ID]: { [ROPSTEN_CHAIN_ID]: {
nativeCurrency: ETH_SYMBOL, nativeCurrency: TEST_NETWORK_TICKER_MAP[ROPSTEN],
network: BUYABLE_CHAIN_ETHEREUM_NETWORK_NAME, network: BUYABLE_CHAIN_ETHEREUM_NETWORK_NAME,
}, },
[RINKEBY_CHAIN_ID]: { [RINKEBY_CHAIN_ID]: {
nativeCurrency: ETH_SYMBOL, nativeCurrency: TEST_NETWORK_TICKER_MAP[RINKEBY],
network: BUYABLE_CHAIN_ETHEREUM_NETWORK_NAME, network: BUYABLE_CHAIN_ETHEREUM_NETWORK_NAME,
}, },
[GOERLI_CHAIN_ID]: { [GOERLI_CHAIN_ID]: {
nativeCurrency: ETH_SYMBOL, nativeCurrency: TEST_NETWORK_TICKER_MAP[GOERLI],
network: BUYABLE_CHAIN_ETHEREUM_NETWORK_NAME, network: BUYABLE_CHAIN_ETHEREUM_NETWORK_NAME,
}, },
[KOVAN_CHAIN_ID]: { [KOVAN_CHAIN_ID]: {
nativeCurrency: ETH_SYMBOL, nativeCurrency: TEST_NETWORK_TICKER_MAP[KOVAN],
network: BUYABLE_CHAIN_ETHEREUM_NETWORK_NAME, network: BUYABLE_CHAIN_ETHEREUM_NETWORK_NAME,
}, },
[BSC_CHAIN_ID]: { [BSC_CHAIN_ID]: {

View File

@ -1,6 +1,7 @@
.currency-display-component { .currency-display-component {
display: flex; display: flex;
align-items: center; align-items: center;
flex-wrap: wrap;
&__text { &__text {
/*rtl:ignore*/ /*rtl:ignore*/

View File

@ -11,6 +11,7 @@ import {
} from '../ducks/metamask/metamask'; } from '../ducks/metamask/metamask';
import { conversionUtil } from '../../shared/modules/conversion.utils'; import { conversionUtil } from '../../shared/modules/conversion.utils';
import { TEST_NETWORK_TICKER_MAP } from '../../shared/constants/network';
/** /**
* Defines the shape of the options parameter for useCurrencyDisplay * Defines the shape of the options parameter for useCurrencyDisplay
@ -95,7 +96,15 @@ export function useCurrencyDisplay(
let suffix; let suffix;
if (!opts.hideLabel) { if (!opts.hideLabel) {
suffix = opts.suffix || currency?.toUpperCase(); // if the currency we are displaying is the native currency of one of our preloaded test-nets (rinkeby, ropsten etc.)
// then we allow lowercase characters, otherwise we force to uppercase any suffix passed as a currency
const currencyTickerSymbol = Object.values(
TEST_NETWORK_TICKER_MAP,
).includes(currency)
? currency
: currency?.toUpperCase();
suffix = opts.suffix || currencyTickerSymbol;
} }
return [ return [

View File

@ -10,6 +10,8 @@ import {
ROPSTEN, ROPSTEN,
ROPSTEN_CHAIN_ID, ROPSTEN_CHAIN_ID,
getRpcUrl, getRpcUrl,
ETH_SYMBOL,
TEST_NETWORK_TICKER_MAP,
} from '../../../../shared/constants/network'; } from '../../../../shared/constants/network';
const defaultNetworksData = [ const defaultNetworksData = [
@ -19,7 +21,7 @@ const defaultNetworksData = [
providerType: MAINNET, providerType: MAINNET,
rpcUrl: getRpcUrl({ network: MAINNET, excludeProjectId: true }), rpcUrl: getRpcUrl({ network: MAINNET, excludeProjectId: true }),
chainId: MAINNET_CHAIN_ID, chainId: MAINNET_CHAIN_ID,
ticker: 'ETH', ticker: ETH_SYMBOL,
blockExplorerUrl: 'https://etherscan.io', blockExplorerUrl: 'https://etherscan.io',
}, },
{ {
@ -28,7 +30,7 @@ const defaultNetworksData = [
providerType: ROPSTEN, providerType: ROPSTEN,
rpcUrl: getRpcUrl({ network: ROPSTEN, excludeProjectId: true }), rpcUrl: getRpcUrl({ network: ROPSTEN, excludeProjectId: true }),
chainId: ROPSTEN_CHAIN_ID, chainId: ROPSTEN_CHAIN_ID,
ticker: 'ETH', ticker: TEST_NETWORK_TICKER_MAP[ROPSTEN],
blockExplorerUrl: 'https://ropsten.etherscan.io', blockExplorerUrl: 'https://ropsten.etherscan.io',
}, },
{ {
@ -37,7 +39,7 @@ const defaultNetworksData = [
providerType: RINKEBY, providerType: RINKEBY,
rpcUrl: getRpcUrl({ network: RINKEBY, excludeProjectId: true }), rpcUrl: getRpcUrl({ network: RINKEBY, excludeProjectId: true }),
chainId: RINKEBY_CHAIN_ID, chainId: RINKEBY_CHAIN_ID,
ticker: 'ETH', ticker: TEST_NETWORK_TICKER_MAP[RINKEBY],
blockExplorerUrl: 'https://rinkeby.etherscan.io', blockExplorerUrl: 'https://rinkeby.etherscan.io',
}, },
{ {
@ -46,7 +48,7 @@ const defaultNetworksData = [
providerType: GOERLI, providerType: GOERLI,
rpcUrl: getRpcUrl({ network: GOERLI, excludeProjectId: true }), rpcUrl: getRpcUrl({ network: GOERLI, excludeProjectId: true }),
chainId: GOERLI_CHAIN_ID, chainId: GOERLI_CHAIN_ID,
ticker: 'ETH', ticker: TEST_NETWORK_TICKER_MAP[GOERLI],
blockExplorerUrl: 'https://goerli.etherscan.io', blockExplorerUrl: 'https://goerli.etherscan.io',
}, },
{ {
@ -55,7 +57,7 @@ const defaultNetworksData = [
providerType: KOVAN, providerType: KOVAN,
rpcUrl: getRpcUrl({ network: KOVAN, excludeProjectId: true }), rpcUrl: getRpcUrl({ network: KOVAN, excludeProjectId: true }),
chainId: KOVAN_CHAIN_ID, chainId: KOVAN_CHAIN_ID,
ticker: 'ETH', ticker: TEST_NETWORK_TICKER_MAP[KOVAN],
blockExplorerUrl: 'https://kovan.etherscan.io', blockExplorerUrl: 'https://kovan.etherscan.io',
}, },
]; ];