UNPKG

@exodus/ethersproject-abstract-provider

Version:

An Abstract Class for describing an Ethereum Provider for ethers.

99 lines (98 loc) 3.42 kB
"use strict"; import { BigNumber } from "@exodus/ethersproject-bignumber"; import { isHexString } from "@exodus/ethersproject-bytes"; import { Description, defineReadOnly, resolveProperties } from "@exodus/ethersproject-properties"; import { Logger } from "@exodus/ethersproject-logger"; import { version } from "./_version.js"; const logger = new Logger(version); ; ; //export type CallTransactionable = { // call(transaction: TransactionRequest): Promise<TransactionResponse>; //}; export class ForkEvent extends Description { static isForkEvent(value) { return !!(value && value._isForkEvent); } } export class BlockForkEvent extends ForkEvent { constructor(blockHash, expiry) { if (!isHexString(blockHash, 32)) { logger.throwArgumentError("invalid blockHash", "blockHash", blockHash); } super({ _isForkEvent: true, _isBlockForkEvent: true, expiry: (expiry || 0), blockHash: blockHash }); } } export class TransactionForkEvent extends ForkEvent { constructor(hash, expiry) { if (!isHexString(hash, 32)) { logger.throwArgumentError("invalid transaction hash", "hash", hash); } super({ _isForkEvent: true, _isTransactionForkEvent: true, expiry: (expiry || 0), hash: hash }); } } export class TransactionOrderForkEvent extends ForkEvent { constructor(beforeHash, afterHash, expiry) { if (!isHexString(beforeHash, 32)) { logger.throwArgumentError("invalid transaction hash", "beforeHash", beforeHash); } if (!isHexString(afterHash, 32)) { logger.throwArgumentError("invalid transaction hash", "afterHash", afterHash); } super({ _isForkEvent: true, _isTransactionOrderForkEvent: true, expiry: (expiry || 0), beforeHash: beforeHash, afterHash: afterHash }); } } /////////////////////////////// // Exported Abstracts export class Provider { constructor() { logger.checkAbstract(new.target, Provider); defineReadOnly(this, "_isProvider", true); } async getFeeData() { const { block, gasPrice } = await resolveProperties({ block: this.getBlock("latest"), gasPrice: this.getGasPrice().catch((error) => { // @TODO: Why is this now failing on Calaveras? //console.log(error); return null; }) }); let maxFeePerGas = null, maxPriorityFeePerGas = null; if (block && block.baseFeePerGas) { // We may want to compute this more accurately in the future, // using the formula "check if the base fee is correct". // See: https://eips.ethereum.org/EIPS/eip-1559 maxPriorityFeePerGas = BigNumber.from("2500000000"); maxFeePerGas = block.baseFeePerGas.mul(2).add(maxPriorityFeePerGas); } return { maxFeePerGas, maxPriorityFeePerGas, gasPrice }; } // Alias for "on" addListener(eventName, listener) { return this.on(eventName, listener); } // Alias for "off" removeListener(eventName, listener) { return this.off(eventName, listener); } static isProvider(value) { return !!(value && value._isProvider); } }