From 3242710f3d18420cc159d1f3689944faff4433ea Mon Sep 17 00:00:00 2001 From: ryanml Date: Wed, 26 May 2021 09:17:18 -0700 Subject: [PATCH] Supporting existing default networks in switchEthereumChain (#11184) --- .../handlers/switch-ethereum-chain.js | 20 +++++++++++++++++-- .../templates/switch-ethereum-chain.js | 15 ++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js b/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js index 97b5d0f3c4cd..bba11d26cb87 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js @@ -1,6 +1,11 @@ import { ethErrors } from 'eth-rpc-errors'; import { omit } from 'lodash'; import { MESSAGE_TYPE } from '../../../../../shared/constants/app'; +import { + ETH_SYMBOL, + CHAIN_ID_TO_TYPE_MAP, + NETWORK_TO_NAME_MAP, +} from '../../../../../shared/constants/network'; import { isPrefixedFormattedHexString, isSafeChainId, @@ -12,6 +17,18 @@ const switchEthereumChain = { }; export default switchEthereumChain; +function findExistingNetwork(chainId, findCustomRpcBy) { + if (chainId in CHAIN_ID_TO_TYPE_MAP) { + return { + chainId, + nickname: NETWORK_TO_NAME_MAP[chainId], + ticker: ETH_SYMBOL, + }; + } + + return findCustomRpcBy({ chainId }); +} + async function switchEthereumChainHandler( req, res, @@ -61,7 +78,7 @@ async function switchEthereumChainHandler( ); } - const existingNetwork = findCustomRpcBy({ chainId: _chainId }); + const existingNetwork = findExistingNetwork(_chainId, findCustomRpcBy); if (existingNetwork) { const currentChainId = getCurrentChainId(); @@ -75,7 +92,6 @@ async function switchEthereumChainHandler( origin, type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN, requestData: { - rpcUrl: existingNetwork.rpcUrl, chainId: existingNetwork.chainId, nickname: existingNetwork.nickname, ticker: existingNetwork.ticker, diff --git a/ui/pages/confirmation/templates/switch-ethereum-chain.js b/ui/pages/confirmation/templates/switch-ethereum-chain.js index 23f255bc6840..08affd769281 100644 --- a/ui/pages/confirmation/templates/switch-ethereum-chain.js +++ b/ui/pages/confirmation/templates/switch-ethereum-chain.js @@ -1,5 +1,8 @@ import { ethErrors } from 'eth-rpc-errors'; -import { NETWORK_TYPE_RPC } from '../../../../shared/constants/network'; +import { + CHAIN_ID_TO_TYPE_MAP, + NETWORK_TYPE_RPC, +} from '../../../../shared/constants/network'; import { JUSTIFY_CONTENT, SEVERITIES, @@ -24,6 +27,14 @@ 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: [ @@ -65,7 +76,7 @@ function getValues(pendingApproval, t, actions) { colored: false, outline: true, targetNetwork: { - type: NETWORK_TYPE_RPC, + type: getNetworkType(pendingApproval.requestData.chainId), nickname: pendingApproval.requestData.nickname, }, },