Skip to content

Commit

Permalink
Merge pull request #6 from celo-tools/0.2.0
Browse files Browse the repository at this point in the history
0.2.0 - Add network detection
  • Loading branch information
jmrossy authored May 30, 2022
2 parents d2dae69 + af08f08 commit 0916f26
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 18 additions & 0 deletions src/lib/CeloProvider.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
}
}
82 changes: 82 additions & 0 deletions src/lib/networks.ts
Original file line number Diff line number Diff line change
@@ -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,
);
}
}

0 comments on commit 0916f26

Please sign in to comment.