Skip to content

Commit

Permalink
More flexible static network checking (ethers-io#3834).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo authored and Woodpile37 committed Jan 14, 2024
1 parent b53ca00 commit d8eeec3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
77 changes: 75 additions & 2 deletions src.ts/providers/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,75 @@ export class Network {
}

toJSON(): any {
return { name: this.name, chainId: this.chainId };
return { name: this.name, chainId: String(this.chainId) };
}

/**
* The network common name.
*
* This is the canonical name, as networks migh have multiple
* names.
*/
get name(): string { return this.#name; }
set name(value: string) { this.#name = value; }

/**
* The network chain ID.
*/
get chainId(): bigint { return this.#chainId; }
set chainId(value: BigNumberish) { this.#chainId = getBigInt(value, "chainId"); }

/**
* Returns true if %%other%% matches this network. Any chain ID
* must match, and if no chain ID is present, the name must match.
*
* This method does not currently check for additional properties,
* such as ENS address or plug-in compatibility.
*/
matches(other: Networkish): boolean {
if (other == null) { return false; }

if (typeof(other) === "string") {
try {
return (this.chainId === getBigInt(other));
} catch (error) { }
return (this.name === other);
}

if (typeof(other) === "number" || typeof(other) === "bigint") {
try {
return (this.chainId === getBigInt(other));
} catch (error) { }
return false;
}

if (typeof(other) === "object") {
if (other.chainId != null) {
try {
return (this.chainId === getBigInt(other.chainId));
} catch (error) { }
return false;
}
if (other.name != null) {
return (this.name === other.name);
}
return false;
}

return false;
}

/**
* Returns the list of plugins currently attached to this Network.
*/
get plugins(): Array<NetworkPlugin> {
return Array.from(this.#plugins.values());
}

/**
* Attach a new %%plugin%% to this Network. The network name
* must be unique, excluding any fragment.
*/
attachPlugin(plugin: NetworkPlugin): this {
if (this.#plugins.get(plugin.name)) {
throw new Error(`cannot replace existing plugin: ${ plugin.name } `);
Expand All @@ -125,15 +181,26 @@ export class Network {
return this;
}

/**
* Return the plugin, if any, matching %%name%% exactly. Plugins
* with fragments will not be returned unless %%name%% includes
* a fragment.
*/
getPlugin<T extends NetworkPlugin = NetworkPlugin>(name: string): null | T {
return <T>(this.#plugins.get(name)) || null;
}

// Gets a list of Plugins which match basename, ignoring any fragment
/**
* Gets a list of all plugins that match %%name%%, with otr without
* a fragment.
*/
getPlugins<T extends NetworkPlugin = NetworkPlugin>(basename: string): Array<T> {
return <Array<T>>(this.plugins.filter((p) => (p.name.split("#")[0] === basename)));
}

/**
* Create a copy of this Network.
*/
clone(): Network {
const clone = new Network(this.name, this.chainId);
this.plugins.forEach((plugin) => {
Expand All @@ -142,6 +209,12 @@ export class Network {
return clone;
}

/**
* Compute the intrinsic gas required for a transaction.
*
* A GasCostPlugin can be attached to override the default
* values.
*/
computeIntrinsicGas(tx: TransactionLike): number {
const costs = this.getPlugin<GasCostPlugin>("org.ethers.plugins.network.GasCost") || (new GasCostPlugin());

Expand Down
4 changes: 2 additions & 2 deletions src.ts/providers/provider-jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,10 @@ export abstract class JsonRpcApiProvider extends AbstractProvider {
this.#notReady = { promise, resolve };
}

// This could be relaxed in the future to just check equivalent networks
// Make sure any static network is compatbile with the provided netwrok
const staticNetwork = this._getOption("staticNetwork");
if (staticNetwork) {
assertArgument(staticNetwork === network,
assertArgument(network == null || staticNetwork.matches(network),
"staticNetwork MUST match network object", "options", options);
this.#network = staticNetwork;
}
Expand Down

0 comments on commit d8eeec3

Please sign in to comment.