ethers.js/lib.esm/providers/network.js

188 lines
6.8 KiB
JavaScript
Raw Normal View History

2022-09-27 10:45:27 +03:00
import { accessListify } from "../transaction/index.js";
2022-09-16 05:58:45 +03:00
import { getStore, getBigInt, setStore, throwArgumentError } from "../utils/index.js";
2022-09-05 23:57:11 +03:00
import { EnsPlugin, GasCostPlugin } from "./plugins-network.js";
/* * * *
// Networks which operation against an L2 can use this plugin to
// specify how to access L1, for the purpose of resolving ENS,
// for example.
export class LayerOneConnectionPlugin extends NetworkPlugin {
readonly provider!: Provider;
// @TODO: Rename to ChainAccess and allow for connecting to any chain
constructor(provider: Provider) {
super("org.ethers.plugins.layer-one-connection");
defineProperties<LayerOneConnectionPlugin>(this, { provider });
}
clone(): LayerOneConnectionPlugin {
return new LayerOneConnectionPlugin(this.provider);
}
}
*/
/* * * *
export class PriceOraclePlugin extends NetworkPlugin {
readonly address!: string;
constructor(address: string) {
super("org.ethers.plugins.price-oracle");
defineProperties<PriceOraclePlugin>(this, { address });
}
clone(): PriceOraclePlugin {
return new PriceOraclePlugin(this.address);
}
}
*/
// Networks or clients with a higher need for security (such as clients
// that may automatically make CCIP requests without user interaction)
// can use this plugin to anonymize requests or intercept CCIP requests
// to notify and/or receive authorization from the user
/* * * *
export type FetchDataFunc = (req: Frozen<FetchRequest>) => Promise<FetchRequest>;
export class CcipPreflightPlugin extends NetworkPlugin {
readonly fetchData!: FetchDataFunc;
constructor(fetchData: FetchDataFunc) {
super("org.ethers.plugins.ccip-preflight");
defineProperties<CcipPreflightPlugin>(this, { fetchData });
}
clone(): CcipPreflightPlugin {
return new CcipPreflightPlugin(this.fetchData);
}
}
*/
const Networks = new Map();
2022-09-27 10:45:27 +03:00
// @TODO: Add a _ethersNetworkObj variable to better detect network ovjects
2022-09-05 23:57:11 +03:00
export class Network {
#props;
2022-09-27 10:45:27 +03:00
constructor(name, _chainId) {
2022-09-16 05:58:45 +03:00
const chainId = getBigInt(_chainId);
2022-09-05 23:57:11 +03:00
const plugins = new Map();
2022-09-27 10:45:27 +03:00
this.#props = { name, chainId, plugins };
2022-09-05 23:57:11 +03:00
}
toJSON() {
return { name: this.name, chainId: this.chainId };
}
get name() { return getStore(this.#props, "name"); }
set name(value) { setStore(this.#props, "name", value); }
get chainId() { return getStore(this.#props, "chainId"); }
2022-09-16 05:58:45 +03:00
set chainId(value) { setStore(this.#props, "chainId", getBigInt(value, "chainId")); }
2022-09-05 23:57:11 +03:00
get plugins() {
return Array.from(this.#props.plugins.values());
}
attachPlugin(plugin) {
if (this.#props.plugins.get(plugin.name)) {
throw new Error(`cannot replace existing plugin: ${plugin.name} `);
}
2022-09-27 10:45:27 +03:00
this.#props.plugins.set(plugin.name, plugin.clone());
2022-09-05 23:57:11 +03:00
return this;
}
getPlugin(name) {
return (this.#props.plugins.get(name)) || null;
}
// Gets a list of Plugins which match basename, ignoring any fragment
getPlugins(basename) {
return (this.plugins.filter((p) => (p.name.split("#")[0] === basename)));
}
clone() {
2022-09-27 10:45:27 +03:00
const clone = new Network(this.name, this.chainId);
2022-09-05 23:57:11 +03:00
this.plugins.forEach((plugin) => {
clone.attachPlugin(plugin.clone());
});
return clone;
}
2022-09-27 10:45:27 +03:00
/*
freeze(): Frozen<Network> {
Object.freeze(this.#props);
return this;
}
isFrozen(): boolean {
return Object.isFrozen(this.#props);
}
*/
2022-09-05 23:57:11 +03:00
computeIntrinsicGas(tx) {
const costs = this.getPlugin("org.ethers.gas-cost") || (new GasCostPlugin());
let gas = costs.txBase;
if (tx.to == null) {
gas += costs.txCreate;
}
if (tx.data) {
for (let i = 2; i < tx.data.length; i += 2) {
if (tx.data.substring(i, i + 2) === "00") {
gas += costs.txDataZero;
}
else {
gas += costs.txDataNonzero;
}
}
}
if (tx.accessList) {
2022-09-27 10:45:27 +03:00
const accessList = accessListify(tx.accessList);
2022-09-05 23:57:11 +03:00
for (const addr in accessList) {
gas += costs.txAccessListAddress + costs.txAccessListStorageKey * accessList[addr].storageKeys.length;
}
}
return gas;
}
2022-09-16 05:58:45 +03:00
/**
* Returns a new Network for the %%network%% name or chainId.
*/
2022-09-05 23:57:11 +03:00
static from(network) {
// Default network
if (network == null) {
2022-09-30 05:57:27 +03:00
return Network.from("mainnet");
2022-09-05 23:57:11 +03:00
}
// Canonical name or chain ID
if (typeof (network) === "number") {
network = BigInt(network);
}
if (typeof (network) === "string" || typeof (network) === "bigint") {
const networkFunc = Networks.get(network);
if (networkFunc) {
return networkFunc();
}
if (typeof (network) === "bigint") {
return new Network("unknown", network);
}
2022-09-16 05:58:45 +03:00
throwArgumentError("unknown network", "network", network);
2022-09-05 23:57:11 +03:00
}
// Clonable with network-like abilities
if (typeof (network.clone) === "function") {
const clone = network.clone();
//if (typeof(network.name) !== "string" || typeof(network.chainId) !== "number") {
//}
return clone;
}
// Networkish
if (typeof (network) === "object") {
if (typeof (network.name) !== "string" || typeof (network.chainId) !== "number") {
2022-09-16 05:58:45 +03:00
throwArgumentError("invalid network object name or chainId", "network", network);
2022-09-05 23:57:11 +03:00
}
const custom = new Network((network.name), (network.chainId));
if (network.ensAddress || network.ensNetwork != null) {
custom.attachPlugin(new EnsPlugin(network.ensAddress, network.ensNetwork));
}
//if ((<any>network).layerOneConnection) {
// custom.attachPlugin(new LayerOneConnectionPlugin((<any>network).layerOneConnection));
//}
return custom;
}
2022-09-16 05:58:45 +03:00
return throwArgumentError("invalid network", "network", network);
2022-09-05 23:57:11 +03:00
}
2022-09-16 05:58:45 +03:00
/**
* Register %%nameOrChainId%% with a function which returns
* an instance of a Network representing that chain.
*/
2022-09-05 23:57:11 +03:00
static register(nameOrChainId, networkFunc) {
if (typeof (nameOrChainId) === "number") {
nameOrChainId = BigInt(nameOrChainId);
}
const existing = Networks.get(nameOrChainId);
if (existing) {
2022-09-16 05:58:45 +03:00
throwArgumentError(`conflicting network for ${JSON.stringify(existing.name)}`, "nameOrChainId", nameOrChainId);
2022-09-05 23:57:11 +03:00
}
Networks.set(nameOrChainId, networkFunc);
}
}
//# sourceMappingURL=network.js.map