From a24436d8b73494bc3db29c24b16326175ab4251d Mon Sep 17 00:00:00 2001 From: Ari Gibson Date: Sat, 14 May 2022 00:56:22 -0600 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=8E=89=20Init=20tests=20and=20sample?= =?UTF-8?q?=20functionality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/providers/BaseProvider.ts | 35 ++++++++++++++----- .../json-rpc-provider/get-network.test.ts | 30 +++++++++++++--- src/types/Network.types.ts | 2 ++ 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/providers/BaseProvider.ts b/src/providers/BaseProvider.ts index d456688d..fa8fb3e9 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,34 @@ 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 { + 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; From 6b25ff4982865b85a8dc745b9344fededed49ce2 Mon Sep 17 00:00:00 2001 From: Ari Gibson Date: Sat, 14 May 2022 01:08:54 -0600 Subject: [PATCH 2/2] Add case for passing in full Network object --- src/providers/BaseProvider.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/providers/BaseProvider.ts b/src/providers/BaseProvider.ts index fa8fb3e9..7684c0c5 100644 --- a/src/providers/BaseProvider.ts +++ b/src/providers/BaseProvider.ts @@ -58,6 +58,8 @@ export abstract class BaseProvider { (key) => (chainsInfo as any)[key][0] === network, ); info = (chainsInfo as any)[Number(chainId)]; + } else if (typeof network === 'object') { + } } else { const hexChainId = (await this.post(