Skip to content

Commit

Permalink
Merge pull request #155 from Synthetixio/fix/handleSwitchChain
Browse files Browse the repository at this point in the history
casting networkId to number for handleSwitchChain
  • Loading branch information
fritzschoff authored Jan 27, 2022
2 parents d74de5c + 1306a8c commit 3e5a177
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
24 changes: 12 additions & 12 deletions packages/contracts-interface/src/synthetix.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,32 +95,32 @@ declare module 'synthetix' {
: TargetsRecord;

export const chainIdMapping = {
'1': {
1: {
useOvm: false,
fork: false,
network: 'mainnet',
},
'5': {
5: {
useOvm: false,
fork: false,
network: 'goerli',
},
'10': {
10: {
useOvm: true,
fork: false,
network: 'mainnet',
},
'42': {
42: {
useOvm: false,
fork: false,
network: 'kovan',
},
'69': {
69: {
useOvm: true,
fork: false,
network: 'kovan',
},
'31337': {
31337: {
useOvm: false,
fork: true,
network: 'mainnet',
Expand All @@ -132,12 +132,12 @@ declare module 'synthetix' {
},
} as const;
export const networkToChainId = {
mainnet: '1',
goerli: '5',
'mainnet-ovm': '10',
kovan: '42',
'kovan-ovm': '69',
'mainnet-fork': '31337',
mainnet: 1,
goerli: 5,
'mainnet-ovm': 10,
kovan: 42,
'kovan-ovm': 69,
'mainnet-fork': 31337,
'goerli-ovm': '-1',
} as const;
// eslint-disable-next-line
Expand Down
24 changes: 14 additions & 10 deletions packages/contracts-interface/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ import {
import { Synths } from '../generated/mainnet';

export const NetworkIdByName = {
mainnet: '1',
goerli: '5',
kovan: '42',
'mainnet-ovm': '10',
'kovan-ovm': '69',
mainnet: 1,
goerli: 5,
'mainnet-ovm': 10,
kovan: 42,
'kovan-ovm': 69,
'mainnet-fork': 31337,
'goerli-ovm': '-1',
} as const;

export const NetworkNameById = {
'1': 'mainnet',
'5': 'goerli',
'42': 'kovan',
'10': 'mainnet-ovm',
'69': 'kovan-ovm',
1: 'mainnet',
5: 'goerli',
42: 'kovan',
10: 'mainnet-ovm',
69: 'kovan-ovm',
31337: 'mainnet-fork',
'-1': 'goerli-ovm',
} as const;

export type NetworkIdByNameType = typeof NetworkIdByName;
Expand Down
10 changes: 4 additions & 6 deletions packages/providers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,20 @@ const getOptimismProvider = ({
/**
* @dev if the new network is not added already, for instance in meta mask, this will throw an error. Call `wallet_addEthereumChain`
* @param web3Provider
* @param network
* @param isOVM
* @returns null or undefined
*/
const handleSwitchChain = async (
web3Provider: ethersProviders.Web3Provider,
isOVM: boolean
): Promise<null | undefined> => {
if (!web3Provider.provider?.request) return;
): Promise<boolean> => {
if (!web3Provider.provider?.request) return false;
const newNetworkId = getCorrespondingNetwork(isOVM);
const formattedChainId = utils.hexStripZeros(BigNumber.from(newNetworkId).toHexString());
// If request was successful, null is returned
return web3Provider.provider.request({
const success = await web3Provider.provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: formattedChainId }],
});
return success === null ? true : false;
};

const getCorrespondingNetwork = (isOVM: boolean) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const useGetBridgeDataQuery = (
// @ts-ignore
layerTwoProvider: l2provider,
layerTwoNetworkId: isFromL2
? parseInt(ctx.networkId)
? Number(ctx.networkId)
: L1_TO_L2_NETWORK_MAPPER[ctx.networkId],
});

Expand Down

0 comments on commit 3e5a177

Please sign in to comment.