plugins-network.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { defineProperties } from "../utils/properties.js";
  2. import { assertArgument } from "../utils/index.js";
  3. const EnsAddress = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
  4. /**
  5. * A **NetworkPlugin** provides additional functionality on a [[Network]].
  6. */
  7. export class NetworkPlugin {
  8. /**
  9. * The name of the plugin.
  10. *
  11. * It is recommended to use reverse-domain-notation, which permits
  12. * unique names with a known authority as well as hierarchal entries.
  13. */
  14. name;
  15. /**
  16. * Creates a new **NetworkPlugin**.
  17. */
  18. constructor(name) {
  19. defineProperties(this, { name });
  20. }
  21. /**
  22. * Creates a copy of this plugin.
  23. */
  24. clone() {
  25. return new NetworkPlugin(this.name);
  26. }
  27. }
  28. /**
  29. * A **GasCostPlugin** allows a network to provide alternative values when
  30. * computing the intrinsic gas required for a transaction.
  31. */
  32. export class GasCostPlugin extends NetworkPlugin {
  33. /**
  34. * The block number to treat these values as valid from.
  35. *
  36. * This allows a hardfork to have updated values included as well as
  37. * mulutiple hardforks to be supported.
  38. */
  39. effectiveBlock;
  40. /**
  41. * The transactions base fee.
  42. */
  43. txBase;
  44. /**
  45. * The fee for creating a new account.
  46. */
  47. txCreate;
  48. /**
  49. * The fee per zero-byte in the data.
  50. */
  51. txDataZero;
  52. /**
  53. * The fee per non-zero-byte in the data.
  54. */
  55. txDataNonzero;
  56. /**
  57. * The fee per storage key in the [[link-eip-2930]] access list.
  58. */
  59. txAccessListStorageKey;
  60. /**
  61. * The fee per address in the [[link-eip-2930]] access list.
  62. */
  63. txAccessListAddress;
  64. /**
  65. * Creates a new GasCostPlugin from %%effectiveBlock%% until the
  66. * latest block or another GasCostPlugin supercedes that block number,
  67. * with the associated %%costs%%.
  68. */
  69. constructor(effectiveBlock, costs) {
  70. if (effectiveBlock == null) {
  71. effectiveBlock = 0;
  72. }
  73. super(`org.ethers.network.plugins.GasCost#${(effectiveBlock || 0)}`);
  74. const props = { effectiveBlock };
  75. function set(name, nullish) {
  76. let value = (costs || {})[name];
  77. if (value == null) {
  78. value = nullish;
  79. }
  80. assertArgument(typeof (value) === "number", `invalud value for ${name}`, "costs", costs);
  81. props[name] = value;
  82. }
  83. set("txBase", 21000);
  84. set("txCreate", 32000);
  85. set("txDataZero", 4);
  86. set("txDataNonzero", 16);
  87. set("txAccessListStorageKey", 1900);
  88. set("txAccessListAddress", 2400);
  89. defineProperties(this, props);
  90. }
  91. clone() {
  92. return new GasCostPlugin(this.effectiveBlock, this);
  93. }
  94. }
  95. /**
  96. * An **EnsPlugin** allows a [[Network]] to specify the ENS Registry
  97. * Contract address and the target network to use when using that
  98. * contract.
  99. *
  100. * Various testnets have their own instance of the contract to use, but
  101. * in general, the mainnet instance supports multi-chain addresses and
  102. * should be used.
  103. */
  104. export class EnsPlugin extends NetworkPlugin {
  105. /**
  106. * The ENS Registrty Contract address.
  107. */
  108. address;
  109. /**
  110. * The chain ID that the ENS contract lives on.
  111. */
  112. targetNetwork;
  113. /**
  114. * Creates a new **EnsPlugin** connected to %%address%% on the
  115. * %%targetNetwork%%. The default ENS address and mainnet is used
  116. * if unspecified.
  117. */
  118. constructor(address, targetNetwork) {
  119. super("org.ethers.plugins.network.Ens");
  120. defineProperties(this, {
  121. address: (address || EnsAddress),
  122. targetNetwork: ((targetNetwork == null) ? 1 : targetNetwork)
  123. });
  124. }
  125. clone() {
  126. return new EnsPlugin(this.address, this.targetNetwork);
  127. }
  128. }
  129. /**
  130. * A **FeeDataNetworkPlugin** allows a network to provide and alternate
  131. * means to specify its fee data.
  132. *
  133. * For example, a network which does not support [[link-eip-1559]] may
  134. * choose to use a Gas Station site to approximate the gas price.
  135. */
  136. export class FeeDataNetworkPlugin extends NetworkPlugin {
  137. #feeDataFunc;
  138. /**
  139. * The fee data function provided to the constructor.
  140. */
  141. get feeDataFunc() {
  142. return this.#feeDataFunc;
  143. }
  144. /**
  145. * Creates a new **FeeDataNetworkPlugin**.
  146. */
  147. constructor(feeDataFunc) {
  148. super("org.ethers.plugins.network.FeeData");
  149. this.#feeDataFunc = feeDataFunc;
  150. }
  151. /**
  152. * Resolves to the fee data.
  153. */
  154. async getFeeData(provider) {
  155. return await this.#feeDataFunc(provider);
  156. }
  157. clone() {
  158. return new FeeDataNetworkPlugin(this.#feeDataFunc);
  159. }
  160. }
  161. export class FetchUrlFeeDataNetworkPlugin extends NetworkPlugin {
  162. #url;
  163. #processFunc;
  164. /**
  165. * The URL to initialize the FetchRequest with in %%processFunc%%.
  166. */
  167. get url() { return this.#url; }
  168. /**
  169. * The callback to use when computing the FeeData.
  170. */
  171. get processFunc() { return this.#processFunc; }
  172. /**
  173. * Creates a new **FetchUrlFeeDataNetworkPlugin** which will
  174. * be used when computing the fee data for the network.
  175. */
  176. constructor(url, processFunc) {
  177. super("org.ethers.plugins.network.FetchUrlFeeDataPlugin");
  178. this.#url = url;
  179. this.#processFunc = processFunc;
  180. }
  181. // We are immutable, so we can serve as our own clone
  182. clone() { return this; }
  183. }
  184. /*
  185. export class CustomBlockNetworkPlugin extends NetworkPlugin {
  186. readonly #blockFunc: (provider: Provider, block: BlockParams<string>) => Block<string>;
  187. readonly #blockWithTxsFunc: (provider: Provider, block: BlockParams<TransactionResponseParams>) => Block<TransactionResponse>;
  188. constructor(blockFunc: (provider: Provider, block: BlockParams<string>) => Block<string>, blockWithTxsFunc: (provider: Provider, block: BlockParams<TransactionResponseParams>) => Block<TransactionResponse>) {
  189. super("org.ethers.network-plugins.custom-block");
  190. this.#blockFunc = blockFunc;
  191. this.#blockWithTxsFunc = blockWithTxsFunc;
  192. }
  193. async getBlock(provider: Provider, block: BlockParams<string>): Promise<Block<string>> {
  194. return await this.#blockFunc(provider, block);
  195. }
  196. async getBlockions(provider: Provider, block: BlockParams<TransactionResponseParams>): Promise<Block<TransactionResponse>> {
  197. return await this.#blockWithTxsFunc(provider, block);
  198. }
  199. clone(): CustomBlockNetworkPlugin {
  200. return new CustomBlockNetworkPlugin(this.#blockFunc, this.#blockWithTxsFunc);
  201. }
  202. }
  203. */
  204. //# sourceMappingURL=plugins-network.js.map