ethers.js/lib.commonjs/providers/plugins-network.js

216 lines
7.0 KiB
JavaScript
Raw Permalink Normal View History

2022-09-05 16:57:11 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FetchUrlFeeDataNetworkPlugin = exports.FeeDataNetworkPlugin = exports.EnsPlugin = exports.GasCostPlugin = exports.NetworkPlugin = void 0;
2022-09-05 16:57:11 -04:00
const properties_js_1 = require("../utils/properties.js");
2022-09-15 22:58:45 -04:00
const index_js_1 = require("../utils/index.js");
2022-09-05 16:57:11 -04:00
const EnsAddress = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
2023-06-01 17:52:58 -04:00
/**
* A **NetworkPlugin** provides additional functionality on a [[Network]].
*/
2022-09-05 16:57:11 -04:00
class NetworkPlugin {
2023-06-01 17:52:58 -04:00
/**
* 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 16:57:11 -04:00
name;
2023-06-01 17:52:58 -04:00
/**
* Creates a new **NetworkPlugin**.
*/
2022-09-05 16:57:11 -04:00
constructor(name) {
(0, properties_js_1.defineProperties)(this, { name });
}
2023-06-01 17:52:58 -04:00
/**
* Creates a copy of this plugin.
*/
2022-09-05 16:57:11 -04:00
clone() {
return new NetworkPlugin(this.name);
}
}
exports.NetworkPlugin = NetworkPlugin;
2023-06-01 17:52:58 -04:00
/**
* A **GasCostPlugin** allows a network to provide alternative values when
* computing the intrinsic gas required for a transaction.
*/
2022-09-05 16:57:11 -04:00
class GasCostPlugin extends NetworkPlugin {
2023-06-01 17:52:58 -04:00
/**
* 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 16:57:11 -04:00
effectiveBlock;
2023-06-01 17:52:58 -04:00
/**
* The transactions base fee.
*/
2022-09-05 16:57:11 -04:00
txBase;
2023-06-01 17:52:58 -04:00
/**
* The fee for creating a new account.
*/
2022-09-05 16:57:11 -04:00
txCreate;
2023-06-01 17:52:58 -04:00
/**
* The fee per zero-byte in the data.
*/
2022-09-05 16:57:11 -04:00
txDataZero;
2023-06-01 17:52:58 -04:00
/**
* The fee per non-zero-byte in the data.
*/
2022-09-05 16:57:11 -04:00
txDataNonzero;
2023-06-01 17:52:58 -04:00
/**
* The fee per storage key in the [[link-eip-2930]] access list.
*/
2022-09-05 16:57:11 -04:00
txAccessListStorageKey;
2023-06-01 17:52:58 -04:00
/**
* The fee per address in the [[link-eip-2930]] access list.
*/
2022-09-05 16:57:11 -04:00
txAccessListAddress;
2023-06-01 17:52:58 -04:00
/**
* Creates a new GasCostPlugin from %%effectiveBlock%% until the
* latest block or another GasCostPlugin supercedes that block number,
* with the associated %%costs%%.
*/
2022-11-30 15:44:23 -05:00
constructor(effectiveBlock, costs) {
if (effectiveBlock == null) {
effectiveBlock = 0;
}
2023-02-02 04:05:47 -05:00
super(`org.ethers.network.plugins.GasCost#${(effectiveBlock || 0)}`);
2022-09-05 16:57:11 -04:00
const props = { effectiveBlock };
function set(name, nullish) {
let value = (costs || {})[name];
if (value == null) {
value = nullish;
}
2022-11-09 02:57:02 -05:00
(0, index_js_1.assertArgument)(typeof (value) === "number", `invalud value for ${name}`, "costs", costs);
2022-09-05 16:57:11 -04:00
props[name] = value;
}
set("txBase", 21000);
set("txCreate", 32000);
set("txDataZero", 4);
set("txDataNonzero", 16);
set("txAccessListStorageKey", 1900);
set("txAccessListAddress", 2400);
(0, properties_js_1.defineProperties)(this, props);
}
clone() {
return new GasCostPlugin(this.effectiveBlock, this);
}
}
exports.GasCostPlugin = GasCostPlugin;
2023-06-01 17:52:58 -04:00
/**
* 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 16:57:11 -04:00
class EnsPlugin extends NetworkPlugin {
2023-06-01 17:52:58 -04:00
/**
* The ENS Registrty Contract address.
*/
2022-09-05 16:57:11 -04:00
address;
2023-06-01 17:52:58 -04:00
/**
* The chain ID that the ENS contract lives on.
*/
2022-09-05 16:57:11 -04:00
targetNetwork;
2023-06-01 17:52:58 -04:00
/**
* Creates a new **EnsPlugin** connected to %%address%% on the
* %%targetNetwork%%. The default ENS address and mainnet is used
* if unspecified.
*/
2022-09-05 16:57:11 -04:00
constructor(address, targetNetwork) {
2023-02-02 04:05:47 -05:00
super("org.ethers.plugins.network.Ens");
2022-09-05 16:57:11 -04:00
(0, properties_js_1.defineProperties)(this, {
address: (address || EnsAddress),
targetNetwork: ((targetNetwork == null) ? 1 : targetNetwork)
});
}
clone() {
return new EnsPlugin(this.address, this.targetNetwork);
}
}
exports.EnsPlugin = EnsPlugin;
2023-06-01 17:52:58 -04:00
/**
* 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 16:57:11 -04:00
class FeeDataNetworkPlugin extends NetworkPlugin {
#feeDataFunc;
2023-06-01 17:52:58 -04:00
/**
* The fee data function provided to the constructor.
*/
2023-02-02 04:05:47 -05:00
get feeDataFunc() {
return this.#feeDataFunc;
}
2023-06-01 17:52:58 -04:00
/**
* Creates a new **FeeDataNetworkPlugin**.
*/
2022-09-05 16:57:11 -04:00
constructor(feeDataFunc) {
2023-02-02 04:05:47 -05:00
super("org.ethers.plugins.network.FeeData");
2022-09-05 16:57:11 -04:00
this.#feeDataFunc = feeDataFunc;
}
2023-06-01 17:52:58 -04:00
/**
* Resolves to the fee data.
*/
2022-09-05 16:57:11 -04:00
async getFeeData(provider) {
return await this.#feeDataFunc(provider);
}
clone() {
return new FeeDataNetworkPlugin(this.#feeDataFunc);
}
}
exports.FeeDataNetworkPlugin = FeeDataNetworkPlugin;
class FetchUrlFeeDataNetworkPlugin extends NetworkPlugin {
#url;
#processFunc;
2023-08-02 23:52:01 -04:00
/**
* The URL to initialize the FetchRequest with in %%processFunc%%.
*/
get url() { return this.#url; }
2023-08-02 23:52:01 -04:00
/**
* The callback to use when computing the FeeData.
*/
get processFunc() { return this.#processFunc; }
2023-08-02 23:52:01 -04:00
/**
* Creates a new **FetchUrlFeeDataNetworkPlugin** which will
* be used when computing the fee data for the network.
*/
constructor(url, processFunc) {
super("org.ethers.plugins.network.FetchUrlFeeDataPlugin");
this.#url = url;
this.#processFunc = processFunc;
}
// We are immutable, so we can serve as our own clone
clone() { return this; }
}
exports.FetchUrlFeeDataNetworkPlugin = FetchUrlFeeDataNetworkPlugin;
2022-11-30 15:44:23 -05:00
/*
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>) {
2022-09-27 03:45:27 -04:00
super("org.ethers.network-plugins.custom-block");
this.#blockFunc = blockFunc;
this.#blockWithTxsFunc = blockWithTxsFunc;
}
2022-11-30 15:44:23 -05:00
async getBlock(provider: Provider, block: BlockParams<string>): Promise<Block<string>> {
2022-09-27 03:45:27 -04:00
return await this.#blockFunc(provider, block);
}
2022-11-30 15:44:23 -05:00
async getBlockions(provider: Provider, block: BlockParams<TransactionResponseParams>): Promise<Block<TransactionResponse>> {
2022-09-27 03:45:27 -04:00
return await this.#blockWithTxsFunc(provider, block);
}
2022-11-30 15:44:23 -05:00
clone(): CustomBlockNetworkPlugin {
2022-09-27 03:45:27 -04:00
return new CustomBlockNetworkPlugin(this.#blockFunc, this.#blockWithTxsFunc);
}
}
2022-11-30 15:44:23 -05:00
*/
2022-09-05 16:57:11 -04:00
//# sourceMappingURL=plugins-network.js.map