ethers.js/src.ts/providers/plugins-network.ts

282 lines
8.5 KiB
TypeScript
Raw Normal View History

2022-09-05 23:14:43 +03:00
import { defineProperties } from "../utils/properties.js";
import { assertArgument } from "../utils/index.js";
2022-09-05 23:14:43 +03:00
import type { FeeData, Provider } from "./provider.js";
import type { FetchRequest } from "../utils/fetch.js";
2022-09-05 23:14:43 +03:00
const EnsAddress = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
/**
* A **NetworkPlugin** provides additional functionality on a [[Network]].
*/
2022-09-05 23:14:43 +03:00
export class NetworkPlugin {
/**
* The name of the plugin.
*
* It is recommended to use reverse-domain-notation, which permits
* unique names with a known authority as well as hierarchal entries.
*/
2022-09-05 23:14:43 +03:00
readonly name!: string;
/**
* Creates a new **NetworkPlugin**.
*/
2022-09-05 23:14:43 +03:00
constructor(name: string) {
defineProperties<NetworkPlugin>(this, { name });
}
/**
* Creates a copy of this plugin.
*/
2022-09-05 23:14:43 +03:00
clone(): NetworkPlugin {
return new NetworkPlugin(this.name);
}
// validate(network: Network): NetworkPlugin {
// return this;
// }
2022-09-05 23:14:43 +03:00
}
/**
* The gas cost parameters for a [[GasCostPlugin]].
*/
2022-09-05 23:14:43 +03:00
export type GasCostParameters = {
/**
* The transactions base fee.
*/
2022-09-05 23:14:43 +03:00
txBase?: number;
/**
* The fee for creating a new account.
*/
2022-09-05 23:14:43 +03:00
txCreate?: number;
/**
* The fee per zero-byte in the data.
*/
2022-09-05 23:14:43 +03:00
txDataZero?: number;
/**
* The fee per non-zero-byte in the data.
*/
2022-09-05 23:14:43 +03:00
txDataNonzero?: number;
/**
* The fee per storage key in the [[link-eip-2930]] access list.
*/
2022-09-05 23:14:43 +03:00
txAccessListStorageKey?: number;
/**
* The fee per address in the [[link-eip-2930]] access list.
*/
2022-09-05 23:14:43 +03:00
txAccessListAddress?: number;
};
/**
* A **GasCostPlugin** allows a network to provide alternative values when
* computing the intrinsic gas required for a transaction.
*/
export class GasCostPlugin extends NetworkPlugin implements GasCostParameters {
/**
* The block number to treat these values as valid from.
*
* This allows a hardfork to have updated values included as well as
* mulutiple hardforks to be supported.
*/
2022-09-05 23:14:43 +03:00
readonly effectiveBlock!: number;
/**
* The transactions base fee.
*/
2022-09-05 23:14:43 +03:00
readonly txBase!: number;
/**
* The fee for creating a new account.
*/
2022-09-05 23:14:43 +03:00
readonly txCreate!: number;
/**
* The fee per zero-byte in the data.
*/
2022-09-05 23:14:43 +03:00
readonly txDataZero!: number;
/**
* The fee per non-zero-byte in the data.
*/
2022-09-05 23:14:43 +03:00
readonly txDataNonzero!: number;
/**
* The fee per storage key in the [[link-eip-2930]] access list.
*/
2022-09-05 23:14:43 +03:00
readonly txAccessListStorageKey!: number;
/**
* The fee per address in the [[link-eip-2930]] access list.
*/
2022-09-05 23:14:43 +03:00
readonly txAccessListAddress!: number;
/**
* Creates a new GasCostPlugin from %%effectiveBlock%% until the
* latest block or another GasCostPlugin supercedes that block number,
* with the associated %%costs%%.
*/
2022-11-28 05:58:07 +03:00
constructor(effectiveBlock?: number, costs?: GasCostParameters) {
if (effectiveBlock == null) { effectiveBlock = 0; }
super(`org.ethers.network.plugins.GasCost#${ (effectiveBlock || 0) }`);
2022-09-05 23:14:43 +03:00
const props: Record<string, number> = { effectiveBlock };
function set(name: keyof GasCostParameters, nullish: number): void {
let value = (costs || { })[name];
if (value == null) { value = nullish; }
assertArgument(typeof(value) === "number", `invalud value for ${ name }`, "costs", costs);
2022-09-05 23:14:43 +03:00
props[name] = value;
}
set("txBase", 21000);
set("txCreate", 32000);
set("txDataZero", 4);
set("txDataNonzero", 16);
set("txAccessListStorageKey", 1900);
set("txAccessListAddress", 2400);
defineProperties<GasCostPlugin>(this, props);
}
clone(): GasCostPlugin {
return new GasCostPlugin(this.effectiveBlock, this);
}
}
/**
* An **EnsPlugin** allows a [[Network]] to specify the ENS Registry
* Contract address and the target network to use when using that
* contract.
*
* Various testnets have their own instance of the contract to use, but
* in general, the mainnet instance supports multi-chain addresses and
* should be used.
*/
2022-09-05 23:14:43 +03:00
export class EnsPlugin extends NetworkPlugin {
/**
* The ENS Registrty Contract address.
*/
2022-09-05 23:14:43 +03:00
readonly address!: string;
/**
* The chain ID that the ENS contract lives on.
*/
2022-09-05 23:14:43 +03:00
readonly targetNetwork!: number;
/**
* Creates a new **EnsPlugin** connected to %%address%% on the
* %%targetNetwork%%. The default ENS address and mainnet is used
* if unspecified.
*/
2022-09-05 23:14:43 +03:00
constructor(address?: null | string, targetNetwork?: null | number) {
super("org.ethers.plugins.network.Ens");
2022-09-05 23:14:43 +03:00
defineProperties<EnsPlugin>(this, {
address: (address || EnsAddress),
targetNetwork: ((targetNetwork == null) ? 1: targetNetwork)
});
}
clone(): EnsPlugin {
return new EnsPlugin(this.address, this.targetNetwork);
}
}
/**
* A **FeeDataNetworkPlugin** allows a network to provide and alternate
* means to specify its fee data.
*
* For example, a network which does not support [[link-eip-1559]] may
* choose to use a Gas Station site to approximate the gas price.
*/
2022-09-05 23:14:43 +03:00
export class FeeDataNetworkPlugin extends NetworkPlugin {
readonly #feeDataFunc: (provider: Provider) => Promise<FeeData>;
/**
* The fee data function provided to the constructor.
*/
get feeDataFunc(): (provider: Provider) => Promise<FeeData> {
return this.#feeDataFunc;
}
2022-09-05 23:14:43 +03:00
/**
* Creates a new **FeeDataNetworkPlugin**.
*/
2022-09-05 23:14:43 +03:00
constructor(feeDataFunc: (provider: Provider) => Promise<FeeData>) {
super("org.ethers.plugins.network.FeeData");
2022-09-05 23:14:43 +03:00
this.#feeDataFunc = feeDataFunc;
}
/**
* Resolves to the fee data.
*/
2022-09-05 23:14:43 +03:00
async getFeeData(provider: Provider): Promise<FeeData> {
return await this.#feeDataFunc(provider);
}
clone(): FeeDataNetworkPlugin {
return new FeeDataNetworkPlugin(this.#feeDataFunc);
}
}
export class FetchUrlFeeDataNetworkPlugin extends NetworkPlugin {
readonly #url: string;
readonly #processFunc: (f: () => Promise<FeeData>, p: Provider, r: FetchRequest) => Promise<{ gasPrice?: null | bigint, maxFeePerGas?: null | bigint, maxPriorityFeePerGas?: null | bigint }>;
/**
* The URL to initialize the FetchRequest with in %%processFunc%%.
*/
get url(): string { return this.#url; }
/**
* The callback to use when computing the FeeData.
*/
get processFunc(): (f: () => Promise<FeeData>, p: Provider, r: FetchRequest) => Promise<{ gasPrice?: null | bigint, maxFeePerGas?: null | bigint, maxPriorityFeePerGas?: null | bigint }> { return this.#processFunc; }
/**
* Creates a new **FetchUrlFeeDataNetworkPlugin** which will
* be used when computing the fee data for the network.
*/
constructor(url: string, processFunc: (f: () => Promise<FeeData>, p: Provider, r: FetchRequest) => Promise<{ gasPrice?: null | bigint, maxFeePerGas?: null | bigint, maxPriorityFeePerGas?: null | bigint }>) {
super("org.ethers.plugins.network.FetchUrlFeeDataPlugin");
this.#url = url;
this.#processFunc = processFunc;
}
// We are immutable, so we can serve as our own clone
clone(): FetchUrlFeeDataNetworkPlugin { return this; }
}
/*
export class CustomBlockNetworkPlugin extends NetworkPlugin {
readonly #blockFunc: (provider: Provider, block: BlockParams<string>) => Block<string>;
readonly #blockWithTxsFunc: (provider: Provider, block: BlockParams<TransactionResponseParams>) => Block<TransactionResponse>;
constructor(blockFunc: (provider: Provider, block: BlockParams<string>) => Block<string>, blockWithTxsFunc: (provider: Provider, block: BlockParams<TransactionResponseParams>) => Block<TransactionResponse>) {
super("org.ethers.network-plugins.custom-block");
this.#blockFunc = blockFunc;
this.#blockWithTxsFunc = blockWithTxsFunc;
}
async getBlock(provider: Provider, block: BlockParams<string>): Promise<Block<string>> {
return await this.#blockFunc(provider, block);
}
async getBlockions(provider: Provider, block: BlockParams<TransactionResponseParams>): Promise<Block<TransactionResponse>> {
return await this.#blockWithTxsFunc(provider, block);
}
clone(): CustomBlockNetworkPlugin {
return new CustomBlockNetworkPlugin(this.#blockFunc, this.#blockWithTxsFunc);
}
}
*/