Skip to content

Commit

Permalink
Fix built-in networks switch-ethereum-chain, Including RPC url in swi…
Browse files Browse the repository at this point in the history
…tchEthereumChain requestData (#11268)

* Moving RPC Urls to network constants

* Including RPC url in switchEthereumChain requestData

* Setting project id to var

* Fix built-in networks switch-ethereum-chain

`switch-ethereum-chain` did not work correctly with built-in networks.
It was treating them as custom networks, rather than as built-in
networks. This affected how they were displayed in the network
dropdown, and resulted in slight differences to the network stack used
as well.

The problem was that `updateRpcTarget` was used, which was meant for
custom networks only. Now that `setProviderType` is used in the case of
a built-in network, the behaviour should match the network switcher
exactly.

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
  • Loading branch information
ryanml and Gudahtt committed Jun 9, 2021
1 parent 003b636 commit a750927
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 34 deletions.
Expand Up @@ -5,6 +5,7 @@ import {
ETH_SYMBOL,
CHAIN_ID_TO_TYPE_MAP,
NETWORK_TO_NAME_MAP,
CHAIN_ID_TO_RPC_URL_MAP,
} from '../../../../../shared/constants/network';
import {
isPrefixedFormattedHexString,
Expand All @@ -21,8 +22,10 @@ function findExistingNetwork(chainId, findCustomRpcBy) {
if (chainId in CHAIN_ID_TO_TYPE_MAP) {
return {
chainId,
nickname: NETWORK_TO_NAME_MAP[chainId],
ticker: ETH_SYMBOL,
nickname: NETWORK_TO_NAME_MAP[chainId],
rpcUrl: CHAIN_ID_TO_RPC_URL_MAP[chainId],
type: CHAIN_ID_TO_TYPE_MAP[chainId],
};
}

Expand All @@ -34,7 +37,13 @@ async function switchEthereumChainHandler(
res,
_next,
end,
{ getCurrentChainId, findCustomRpcBy, updateRpcTarget, requestUserApproval },
{
getCurrentChainId,
findCustomRpcBy,
setProviderType,
updateRpcTarget,
requestUserApproval,
},
) {
if (!req.params?.[0] || typeof req.params[0] !== 'object') {
return end(
Expand Down Expand Up @@ -78,26 +87,24 @@ async function switchEthereumChainHandler(
);
}

const existingNetwork = findExistingNetwork(_chainId, findCustomRpcBy);

if (existingNetwork) {
const requestData = findExistingNetwork(_chainId, findCustomRpcBy);
if (requestData) {
const currentChainId = getCurrentChainId();
if (currentChainId === _chainId) {
res.result = null;
return end();
}
try {
await updateRpcTarget(
await requestUserApproval({
origin,
type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN,
requestData: {
chainId: existingNetwork.chainId,
nickname: existingNetwork.nickname,
ticker: existingNetwork.ticker,
},
}),
);
const approvedRequestData = await requestUserApproval({
origin,
type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN,
requestData,
});
if (chainId in CHAIN_ID_TO_TYPE_MAP) {
setProviderType(approvedRequestData.type);
} else {
await updateRpcTarget(approvedRequestData);
}
res.result = null;
} catch (error) {
return end(error);
Expand Down
3 changes: 3 additions & 0 deletions app/scripts/metamask-controller.js
Expand Up @@ -2217,6 +2217,9 @@ export default class MetamaskController extends EventEmitter {
nickname,
);
},
setProviderType: this.networkController.setProviderType.bind(
this.networkController,
),
addCustomRpc: async ({
chainId,
blockExplorerUrl,
Expand Down
21 changes: 21 additions & 0 deletions shared/constants/network.js
Expand Up @@ -32,6 +32,16 @@ export const KOVAN_DISPLAY_NAME = 'Kovan';
export const MAINNET_DISPLAY_NAME = 'Ethereum Mainnet';
export const GOERLI_DISPLAY_NAME = 'Goerli';

const infuraProjectId = process.env.INFURA_PROJECT_ID;
const getRpcUrl = (network) =>
`https://${network}.infura.io/v3/${infuraProjectId}`;

export const ROPSTEN_RPC_URL = getRpcUrl('ropsten');
export const RINKEBY_RPC_URL = getRpcUrl('rinkeby');
export const KOVAN_RPC_URL = getRpcUrl('kovan');
export const MAINNET_RPC_URL = getRpcUrl('mainnet');
export const GOERLI_RPC_URL = getRpcUrl('goerli');

export const ETH_SYMBOL = 'ETH';
export const WETH_SYMBOL = 'WETH';
export const TEST_ETH_SYMBOL = 'TESTETH';
Expand All @@ -50,6 +60,9 @@ export const TEST_CHAINS = [
KOVAN_CHAIN_ID,
];

/**
* Map of all build-in Infura networks to their network and chain IDs.
*/
export const NETWORK_TYPE_TO_ID_MAP = {
[ROPSTEN]: { networkId: ROPSTEN_NETWORK_ID, chainId: ROPSTEN_CHAIN_ID },
[RINKEBY]: { networkId: RINKEBY_NETWORK_ID, chainId: RINKEBY_CHAIN_ID },
Expand Down Expand Up @@ -85,6 +98,14 @@ export const CHAIN_ID_TO_TYPE_MAP = Object.entries(
return chainIdToTypeMap;
}, {});

export const CHAIN_ID_TO_RPC_URL_MAP = {
[ROPSTEN_CHAIN_ID]: ROPSTEN_RPC_URL,
[RINKEBY_CHAIN_ID]: RINKEBY_RPC_URL,
[KOVAN_CHAIN_ID]: KOVAN_RPC_URL,
[GOERLI_CHAIN_ID]: GOERLI_RPC_URL,
[MAINNET_CHAIN_ID]: MAINNET_RPC_URL,
};

export const CHAIN_ID_TO_NETWORK_ID_MAP = Object.values(
NETWORK_TYPE_TO_ID_MAP,
).reduce((chainIdToNetworkIdMap, { chainId, networkId }) => {
Expand Down
15 changes: 2 additions & 13 deletions ui/pages/confirmation/templates/switch-ethereum-chain.js
@@ -1,8 +1,5 @@
import { ethErrors } from 'eth-rpc-errors';
import {
CHAIN_ID_TO_TYPE_MAP,
NETWORK_TYPE_RPC,
} from '../../../../shared/constants/network';
import { NETWORK_TYPE_RPC } from '../../../../shared/constants/network';
import {
JUSTIFY_CONTENT,
SEVERITIES,
Expand All @@ -27,14 +24,6 @@ async function getAlerts() {
return [PENDING_TX_DROP_NOTICE];
}

function getNetworkType(chainId) {
if (chainId in CHAIN_ID_TO_TYPE_MAP) {
return CHAIN_ID_TO_TYPE_MAP[chainId];
}

return NETWORK_TYPE_RPC;
}

function getValues(pendingApproval, t, actions) {
return {
content: [
Expand Down Expand Up @@ -76,7 +65,7 @@ function getValues(pendingApproval, t, actions) {
colored: false,
outline: true,
targetNetwork: {
type: getNetworkType(pendingApproval.requestData.chainId),
type: pendingApproval.requestData.type || NETWORK_TYPE_RPC,
nickname: pendingApproval.requestData.nickname,
},
},
Expand Down
15 changes: 10 additions & 5 deletions ui/pages/settings/networks-tab/networks-tab.constants.js
@@ -1,22 +1,27 @@
import {
GOERLI,
GOERLI_CHAIN_ID,
GOERLI_RPC_URL,
KOVAN,
KOVAN_CHAIN_ID,
KOVAN_RPC_URL,
MAINNET,
MAINNET_CHAIN_ID,
MAINNET_RPC_URL,
RINKEBY,
RINKEBY_CHAIN_ID,
RINKEBY_RPC_URL,
ROPSTEN,
ROPSTEN_CHAIN_ID,
ROPSTEN_RPC_URL,
} from '../../../../shared/constants/network';

const defaultNetworksData = [
{
labelKey: MAINNET,
iconColor: '#29B6AF',
providerType: MAINNET,
rpcUrl: `https://mainnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
rpcUrl: MAINNET_RPC_URL,
chainId: MAINNET_CHAIN_ID,
ticker: 'ETH',
blockExplorerUrl: 'https://etherscan.io',
Expand All @@ -25,7 +30,7 @@ const defaultNetworksData = [
labelKey: ROPSTEN,
iconColor: '#FF4A8D',
providerType: ROPSTEN,
rpcUrl: `https://ropsten.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
rpcUrl: ROPSTEN_RPC_URL,
chainId: ROPSTEN_CHAIN_ID,
ticker: 'ETH',
blockExplorerUrl: 'https://ropsten.etherscan.io',
Expand All @@ -34,7 +39,7 @@ const defaultNetworksData = [
labelKey: RINKEBY,
iconColor: '#F6C343',
providerType: RINKEBY,
rpcUrl: `https://rinkeby.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
rpcUrl: RINKEBY_RPC_URL,
chainId: RINKEBY_CHAIN_ID,
ticker: 'ETH',
blockExplorerUrl: 'https://rinkeby.etherscan.io',
Expand All @@ -43,7 +48,7 @@ const defaultNetworksData = [
labelKey: GOERLI,
iconColor: '#3099f2',
providerType: GOERLI,
rpcUrl: `https://goerli.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
rpcUrl: GOERLI_RPC_URL,
chainId: GOERLI_CHAIN_ID,
ticker: 'ETH',
blockExplorerUrl: 'https://goerli.etherscan.io',
Expand All @@ -52,7 +57,7 @@ const defaultNetworksData = [
labelKey: KOVAN,
iconColor: '#9064FF',
providerType: KOVAN,
rpcUrl: `https://kovan.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
rpcUrl: KOVAN_RPC_URL,
chainId: KOVAN_CHAIN_ID,
ticker: 'ETH',
blockExplorerUrl: 'https://kovan.etherscan.io',
Expand Down

0 comments on commit a750927

Please sign in to comment.