export type INetwork = { name: string; url: string; chainId: number; symbol?: string; explorer?: string; }; export const defaultNetworks = { mainnet: { name: 'Ethereum Mainnet', url: 'https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161', chainId: 1, symbol: 'ETH', explorer: 'https://etherscan.io', } as INetwork, ropsten: { name: 'Ropsten Test Network', url: 'https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161', chainId: 3, symbol: 'ETH', explorer: 'https://ropsten.etherscan.io', } as INetwork, rinkeby: { name: 'Rinkeby Test Network', url: 'https://rinkeby.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161', chainId: 4, symbol: 'ETH', explorer: 'https://rinkeby.etherscan.io', } as INetwork, goerli: { name: 'Goerli Test Network', url: 'https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161', chainId: 5, symbol: 'ETH', explorer: 'https://goerli.etherscan.io', } as INetwork, kovan: { name: 'Kovan Test Network', url: 'https://kovan.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161', chainId: 42, symbol: 'ETH', explorer: 'https://kovan.etherscan.io', } as INetwork, localhost: { name: 'Localhost 8545', url: 'http://localhost:8545', chainId: 1337, symbol: 'ETH', } as INetwork, }; type KnownNetworks = keyof typeof defaultNetworks; const aliases: Partial> = { mainnet: ['main', 'production'], }; export type Network = INetwork | KnownNetworks | string; const mappings: Map = Object.entries(defaultNetworks).reduce( (map, [key, spec]: [string, INetwork]) => { map.set(key, spec); map.set(spec.name, spec); map.set(spec.chainId.toString(), spec); aliases[key as KnownNetworks]?.forEach((alias) => map.set(alias.toString(), spec)); return map; }, new Map(), ); export let network: INetwork; export function setNetwork(newNetwork: INetwork | string | number) { // Resolve the network to plain INetwork interface if (typeof newNetwork === 'string' || typeof newNetwork === 'number') { if (!mappings.has(newNetwork.toString())) { throw new Error(`Alias "${newNetwork}" does not refer to any known network`); } network = mappings.get(newNetwork.toString())!; } else { network = newNetwork; } }