From 6f2d59e46ee0d01c4380ca3416673dd2afc70e55 Mon Sep 17 00:00:00 2001 From: J M Rossy Date: Mon, 30 May 2022 16:53:44 -0400 Subject: [PATCH 1/2] Add network detection to CeloProvider --- src/lib/CeloProvider.ts | 18 +++++++++ src/lib/networks.ts | 82 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/lib/networks.ts diff --git a/src/lib/CeloProvider.ts b/src/lib/CeloProvider.ts index 41ba7b8..e15f5dc 100644 --- a/src/lib/CeloProvider.ts +++ b/src/lib/CeloProvider.ts @@ -1,6 +1,9 @@ import { BigNumber, providers, utils } from "ethers"; +import { getNetwork } from "./networks"; import { parseCeloTransaction } from "./transactions"; +const logger = new utils.Logger("CeloProvider"); + export class CeloProvider extends providers.JsonRpcProvider { constructor( url?: utils.ConnectionInfo | string, @@ -69,4 +72,19 @@ export class CeloProvider extends providers.JsonRpcProvider { return super.prepareRequest(method, params); } + + static getNetwork(networkish: providers.Networkish): providers.Network { + const network = getNetwork(networkish == null ? 'celo' : networkish); + if (network == null) { + return logger.throwError( + `unknown network: ${JSON.stringify(network)}`, + utils.Logger.errors.UNSUPPORTED_OPERATION, + { + operation: 'getNetwork', + value: networkish, + }, + ); + } + return network; + } } diff --git a/src/lib/networks.ts b/src/lib/networks.ts new file mode 100644 index 0000000..831fccd --- /dev/null +++ b/src/lib/networks.ts @@ -0,0 +1,82 @@ +import { providers, utils } from 'ethers'; + +const logger = new utils.Logger("CeloNetworks"); + +const networks = [ + { + name: 'celo', + chainId: 42220, + }, + { + name: 'alfajores', + chainId: 44787, + }, + { + name: 'baklava', + chainId: 62320, + }, +]; + +export function getNetwork( + network?: providers.Networkish, +): null | providers.Network { + { + if (network == null) { + return null; + } + + // Chain ID + if (typeof network === 'number') { + const matches = networks.filter((n) => n.chainId === network); + if (matches.length) { + return { name: matches[0].name, chainId: matches[0].chainId }; + } + + return { + name: 'unknown', + chainId: network, + }; + } + + // Chain name + if (typeof network === 'string') { + const matches = networks.filter((n) => n.name === network); + if (matches.length) { + return { name: matches[0].name, chainId: matches[0].chainId }; + } + return null; + } + + if ( + typeof network.name === 'string' && + typeof network.chainId === 'number' + ) { + const byName = getNetwork(network.name); + const byChainId = getNetwork(network.chainId); + + // Nothing standard; valid custom network + if (byName == null && byChainId == null) { + return { + name: network.name, + chainId: network.chainId, + }; + } + + // Make sure if it is a standard chain the parameters match + if ( + byName && + byChainId && + byName.name === byChainId.name && + byName.chainId === byChainId.chainId + ) { + return byName; + } + } + + return logger.throwArgumentError( + 'network chainId mismatch', + 'network', + network, + ); + } +} \ No newline at end of file From af08f081e543d6dc97a50a51d68d49a6dadc8766 Mon Sep 17 00:00:00 2001 From: J M Rossy Date: Mon, 30 May 2022 16:53:59 -0400 Subject: [PATCH 2/2] Bump version to 0.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9ec977c..8c9bfe1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@celo-tools/celo-ethers-wrapper", - "version": "0.1.1", + "version": "0.2.0", "description": "A minimal wrapper to make Ethers.JS compatible with the Celo network.", "main": "build/main/index.js", "typings": "build/main/index.d.ts",