diff --git a/app/scripts/lib/rpc-method-middleware/handlers/switch-starcoin-chain.js b/app/scripts/lib/rpc-method-middleware/handlers/switch-starcoin-chain.js index 9512baad..69753e24 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/switch-starcoin-chain.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/switch-starcoin-chain.js @@ -3,9 +3,11 @@ import { omit } from 'lodash'; import { MESSAGE_TYPE } from '../../../../../shared/constants/app'; import { STC_SYMBOL, + APTOS_SYMBOL, CHAIN_ID_TO_TYPE_MAP, NETWORK_TO_NAME_MAP, CHAIN_ID_TO_RPC_URL_MAP, + NETWORK_CHAIN_ID_TO_NAME_MAP, } from '../../../../../shared/constants/network'; import { isPrefixedFormattedHexString, @@ -26,14 +28,15 @@ const switchStarcoinChain = { }; export default switchStarcoinChain; -function findExistingNetwork(chainId, findCustomRpcBy) { +function findExistingNetwork(chainId, findCustomRpcBy, networkType) { if (chainId in CHAIN_ID_TO_TYPE_MAP) { + const name = NETWORK_CHAIN_ID_TO_NAME_MAP[networkType][chainId] return { chainId, - ticker: STC_SYMBOL, - nickname: NETWORK_TO_NAME_MAP[chainId], - rpcUrl: CHAIN_ID_TO_RPC_URL_MAP[chainId], - type: CHAIN_ID_TO_TYPE_MAP[chainId], + ticker: networkType === 'STARCOIN' ? STC_SYMBOL : APTOS_SYMBOL, + nickname: NETWORK_TO_NAME_MAP[name], + rpcUrl: CHAIN_ID_TO_RPC_URL_MAP[name], + type: name, }; } @@ -53,7 +56,6 @@ async function switchEthereumChainHandler( requestUserApproval, }, ) { - log.debug('switchEthereumChainHandler') if (!req.params?.[0] || typeof req.params[0] !== 'object') { return end( ethErrors.rpc.invalidParams({ @@ -67,9 +69,10 @@ async function switchEthereumChainHandler( const { origin } = req; const { chainId } = req.params[0]; - log.debug({ chainId }) - const otherKeys = Object.keys(omit(req.params[0], ['chainId'])); + const { networkType = 'STARCOIN' } = req.params[0]; + + const otherKeys = Object.keys(omit(req.params[0], ['chainId', 'networkType'])); if (otherKeys.length > 0) { return end( @@ -80,7 +83,6 @@ async function switchEthereumChainHandler( } const _chainId = typeof chainId === 'string' && chainId.toLowerCase(); - log.debug({ _chainId }) if (!isPrefixedFormattedHexString(_chainId)) { return end( @@ -98,11 +100,9 @@ async function switchEthereumChainHandler( ); } - const requestData = findExistingNetwork(_chainId, findCustomRpcBy); - log.debug({ requestData }) + const requestData = findExistingNetwork(_chainId, findCustomRpcBy, networkType); if (requestData) { const currentChainId = getCurrentChainId(); - log.debug({ currentChainId }) if (currentChainId === _chainId) { res.result = null; diff --git a/shared/constants/network.js b/shared/constants/network.js index 820e9e63..b12d5dd4 100644 --- a/shared/constants/network.js +++ b/shared/constants/network.js @@ -78,6 +78,21 @@ export const NETWORK_TYPE_TO_ID_MAP = { [APTOS_MAINNET]: { networkId: APTOS_MAINNET_NETWORK_ID, chainId: APTOS_MAINNET_CHAIN_ID }, }; +// 0x1 is both starcoin mainnet and aptos mainnet +export const NETWORK_CHAIN_ID_TO_NAME_MAP = { + "STARCOIN": { + [BARNARD_CHAIN_ID]: BARNARD, + [PROXIMA_CHAIN_ID]: PROXIMA, + [HALLEY_CHAIN_ID]: HALLEY, + [MAINNET_CHAIN_ID]: MAINNET, + }, + "APTOS": { + [APTOS_DEVNET_CHAIN_ID]: APTOS_DEVNET, + [APTOS_TESTNET_CHAIN_ID]: APTOS_TESTNET, + [APTOS_MAINNET_CHAIN_ID]: APTOS_MAINNET, + }, +}; + export const NETWORK_TO_NAME_MAP = { [MAINNET]: MAINNET_DISPLAY_NAME, [BARNARD]: BARNARD_DISPLAY_NAME, @@ -127,4 +142,11 @@ export const CHAIN_ID_TO_RPC_URL_MAP = { [APTOS_DEVNET_CHAIN_ID]: APTOS_DEVNET_RPC_URL, [APTOS_TESTNET_CHAIN_ID]: APTOS_TESTNET_RPC_URL, [APTOS_MAINNET_CHAIN_ID]: APTOS_MAINNET_RPC_URL, + [MAINNET]: MAINNET_RPC_URL, + [BARNARD]: BARNARD_RPC_URL, + [PROXIMA]: PROXIMA_RPC_URL, + [HALLEY]: HALLEY_RPC_URL, + [APTOS_DEVNET]: APTOS_DEVNET_RPC_URL, + [APTOS_TESTNET]: APTOS_TESTNET_RPC_URL, + [APTOS_MAINNET]: APTOS_MAINNET_RPC_URL, };