mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Supporting existing default networks in switchEthereumChain (#11184)
This commit is contained in:
parent
8e3bffc825
commit
3242710f3d
@ -1,6 +1,11 @@
|
|||||||
import { ethErrors } from 'eth-rpc-errors';
|
import { ethErrors } from 'eth-rpc-errors';
|
||||||
import { omit } from 'lodash';
|
import { omit } from 'lodash';
|
||||||
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
|
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
|
||||||
|
import {
|
||||||
|
ETH_SYMBOL,
|
||||||
|
CHAIN_ID_TO_TYPE_MAP,
|
||||||
|
NETWORK_TO_NAME_MAP,
|
||||||
|
} from '../../../../../shared/constants/network';
|
||||||
import {
|
import {
|
||||||
isPrefixedFormattedHexString,
|
isPrefixedFormattedHexString,
|
||||||
isSafeChainId,
|
isSafeChainId,
|
||||||
@ -12,6 +17,18 @@ const switchEthereumChain = {
|
|||||||
};
|
};
|
||||||
export default 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(
|
async function switchEthereumChainHandler(
|
||||||
req,
|
req,
|
||||||
res,
|
res,
|
||||||
@ -61,7 +78,7 @@ async function switchEthereumChainHandler(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingNetwork = findCustomRpcBy({ chainId: _chainId });
|
const existingNetwork = findExistingNetwork(_chainId, findCustomRpcBy);
|
||||||
|
|
||||||
if (existingNetwork) {
|
if (existingNetwork) {
|
||||||
const currentChainId = getCurrentChainId();
|
const currentChainId = getCurrentChainId();
|
||||||
@ -75,7 +92,6 @@ async function switchEthereumChainHandler(
|
|||||||
origin,
|
origin,
|
||||||
type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN,
|
type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN,
|
||||||
requestData: {
|
requestData: {
|
||||||
rpcUrl: existingNetwork.rpcUrl,
|
|
||||||
chainId: existingNetwork.chainId,
|
chainId: existingNetwork.chainId,
|
||||||
nickname: existingNetwork.nickname,
|
nickname: existingNetwork.nickname,
|
||||||
ticker: existingNetwork.ticker,
|
ticker: existingNetwork.ticker,
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import { ethErrors } from 'eth-rpc-errors';
|
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 {
|
import {
|
||||||
JUSTIFY_CONTENT,
|
JUSTIFY_CONTENT,
|
||||||
SEVERITIES,
|
SEVERITIES,
|
||||||
@ -24,6 +27,14 @@ async function getAlerts() {
|
|||||||
return [PENDING_TX_DROP_NOTICE];
|
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) {
|
function getValues(pendingApproval, t, actions) {
|
||||||
return {
|
return {
|
||||||
content: [
|
content: [
|
||||||
@ -65,7 +76,7 @@ function getValues(pendingApproval, t, actions) {
|
|||||||
colored: false,
|
colored: false,
|
||||||
outline: true,
|
outline: true,
|
||||||
targetNetwork: {
|
targetNetwork: {
|
||||||
type: NETWORK_TYPE_RPC,
|
type: getNetworkType(pendingApproval.requestData.chainId),
|
||||||
nickname: pendingApproval.requestData.nickname,
|
nickname: pendingApproval.requestData.nickname,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user