diff --git a/src/providers/BaseProvider.ts b/src/providers/BaseProvider.ts index d456688d..7684c0c5 100644 --- a/src/providers/BaseProvider.ts +++ b/src/providers/BaseProvider.ts @@ -5,7 +5,7 @@ import { buildRPCPostBody, post } from '../classes/utils/fetchers'; import { hexToDecimal } from '../classes/utils/hex-to-decimal'; import { TinyBig, tinyBig } from '../shared/tiny-big/tiny-big'; import { BlockResponse, BlockTag, RPCBlock } from '../types/Block.types'; -import { Network } from '../types/Network.types'; +import { Network, Networkish } from '../types/Network.types'; import { RPCTransaction, RPCTransactionReceipt, @@ -39,15 +39,36 @@ export abstract class BaseProvider { } /** - * Returns the network this provider is connected to + * Returns the network this provider is connected to, or information about the network provided + * + * * Similar to [`ethers.provider.getNetwork`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getNetwork), accepts short names of networks instead of long names + * + * @param network the network, either chainId or short name, to get information about + * + * @returns information about the network either specified, or the network the user is currently connected to */ - public async getNetwork(): Promise { - const hexChainId = (await this.post( - buildRPCPostBody('eth_chainId', []), - )) as string; + public async getNetwork(network?: Networkish): Promise { + let chainId, info; + if (network) { + if (typeof network === 'number') { + chainId = network; + info = (chainsInfo as any)[network]; + } else if (typeof network === 'string') { + chainId = Object.keys(chainsInfo).find( + (key) => (chainsInfo as any)[key][0] === network, + ); + info = (chainsInfo as any)[Number(chainId)]; + } else if (typeof network === 'object') { + + } + } else { + const hexChainId = (await this.post( + buildRPCPostBody('eth_chainId', []), + )) as string; - const chainId = hexToDecimal(hexChainId); - const info = (chainsInfo as any)[chainId]; + chainId = hexToDecimal(hexChainId); + info = (chainsInfo as any)[chainId]; + } return { chainId: Number(chainId), name: info[0] || 'unknown', diff --git a/src/providers/test/json-rpc-provider/get-network.test.ts b/src/providers/test/json-rpc-provider/get-network.test.ts index 2a5ce3c8..a5c897c9 100644 --- a/src/providers/test/json-rpc-provider/get-network.test.ts +++ b/src/providers/test/json-rpc-provider/get-network.test.ts @@ -1,18 +1,27 @@ import { ethers } from 'ethers'; import { JsonRpcProvider } from '../../../index'; import { fakeUrls, rpcUrls } from '../rpc-urls'; +import { Networkish } from './../../../types/Network.types'; const xdaiRPCUrl = rpcUrls.gno; const bscRPCUrl = rpcUrls.bnb; describe('provider.getNetwork happy path', () => { - async function testNetwork(rpcUrl: string) { + async function testNetwork(rpcUrl: string, network?: Networkish) { const essentialEth = new JsonRpcProvider(rpcUrl); const ethersProvider = new ethers.providers.StaticJsonRpcProvider(rpcUrl); - const [eeNetwork, ethersNetwork] = await Promise.all([ - essentialEth.getNetwork(), - ethersProvider.getNetwork(), - ]); + let eeNetwork, ethersNetwork; + if (network) { + [eeNetwork, ethersNetwork] = await Promise.all([ + essentialEth.getNetwork(network), + ethers.providers.getNetwork(network), + ]); + } else { + [eeNetwork, ethersNetwork] = await Promise.all([ + essentialEth.getNetwork(), + ethersProvider.getNetwork(), + ]); + } expect(eeNetwork.chainId).toBe(ethersNetwork.chainId); expect(eeNetwork.ensAddress).toBe(ethersNetwork.ensAddress); @@ -27,6 +36,17 @@ describe('provider.getNetwork happy path', () => { it('bsc should match ethers', async () => { await testNetwork(bscRPCUrl); }); + it('should match ethers for a specified chain ID number', () => { + const chainIds = [1, 6, 137]; + chainIds.forEach((id) => { + }) + }); + it('should match ethers for a specified chain name', () => { + const chainNames = []; + }); + it('should match ethers for a specified network', () => { + const networkObjects = []; + }); }); describe('provider.getNetwork error handling', () => { diff --git a/src/types/Network.types.ts b/src/types/Network.types.ts index d544ac62..6002f610 100644 --- a/src/types/Network.types.ts +++ b/src/types/Network.types.ts @@ -6,3 +6,5 @@ export interface Network { ensAddress: string | null; // ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e'; name: string; } + +export type Networkish = Network | string | number;