Skip to content

Commit

Permalink
add networkType in switchEthereumChainHandler to differ 0x1 for starc…
Browse files Browse the repository at this point in the history
…oin or aptos
  • Loading branch information
wk3368 committed Oct 31, 2022
1 parent 88a989f commit 4400f16
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};
}

Expand All @@ -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({
Expand All @@ -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(
Expand All @@ -80,7 +83,6 @@ async function switchEthereumChainHandler(
}

const _chainId = typeof chainId === 'string' && chainId.toLowerCase();
log.debug({ _chainId })

if (!isPrefixedFormattedHexString(_chainId)) {
return end(
Expand All @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions shared/constants/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
};

0 comments on commit 4400f16

Please sign in to comment.