UNPKG

ox

Version:

Ethereum Standard Library

141 lines 4.2 kB
import * as Hex from './Hex.js'; /** * Converts a {@link ox#Fee.FeeHistoryRpc} to a {@link ox#Fee.FeeHistory}. * * @example * ```ts twoslash * import { Fee } from 'ox' * * const history = Fee.fromHistoryRpc({ * baseFeePerGas: ['0x01', '0x02'], * gasUsedRatio: [0.5, 0.6], * oldestBlock: '0x10', * reward: [['0x01']] * }) * // @log: { baseFeePerGas: [1n, 2n], gasUsedRatio: [0.5, 0.6], oldestBlock: 16n, reward: [[1n]] } * ``` * * @param history - The RPC fee history to convert. * @returns An instantiated {@link ox#Fee.FeeHistory}. */ export function fromHistoryRpc(history) { return { baseFeePerGas: history.baseFeePerGas.map((value) => BigInt(value)), gasUsedRatio: history.gasUsedRatio, oldestBlock: BigInt(history.oldestBlock), ...(history.reward ? { reward: history.reward.map((row) => row.map((value) => BigInt(value))), } : {}), }; } /** * Converts a {@link ox#Fee.FeeHistory} to a {@link ox#Fee.FeeHistoryRpc}. * * @example * ```ts twoslash * import { Fee } from 'ox' * * const rpc = Fee.toHistoryRpc({ * baseFeePerGas: [1n, 2n], * gasUsedRatio: [0.5, 0.6], * oldestBlock: 16n, * reward: [[1n]] * }) * ``` * * @param history - The fee history to convert. * @returns An RPC fee history. */ export function toHistoryRpc(history) { return { baseFeePerGas: history.baseFeePerGas.map((value) => Hex.fromNumber(value)), gasUsedRatio: history.gasUsedRatio, oldestBlock: Hex.fromNumber(history.oldestBlock), ...(history.reward ? { reward: history.reward.map((row) => row.map((value) => Hex.fromNumber(value))), } : {}), }; } /** * Estimates a `maxFeePerGas` from a base fee, a priority tip, and a multiplier * applied to the base fee: * * ``` * maxFeePerGas = baseFeePerGas * multiplier + maxPriorityFeePerGas * ``` * * The multiplier is supplied as `multiplierNumerator / multiplierDenominator` * to keep the math in `bigint`. The default is `2 / 1` (i.e. 2x), matching the * common wallet/relay heuristic for headroom against base-fee bumps. * * @example * ```ts twoslash * import { Fee } from 'ox' * * Fee.estimateMaxFeePerGas({ * baseFeePerGas: 100n, * maxPriorityFeePerGas: 5n * }) * // @log: 205n * * Fee.estimateMaxFeePerGas({ * baseFeePerGas: 100n, * maxPriorityFeePerGas: 5n, * multiplierNumerator: 3n, * multiplierDenominator: 2n * }) * // @log: 155n (= 100n * 3n / 2n + 5n) * ``` * * @param args.baseFeePerGas - Block base fee per gas. * @param args.maxPriorityFeePerGas - Tip per gas. * @param args.multiplierNumerator - Numerator of the base-fee multiplier (default `2n`). * @param args.multiplierDenominator - Denominator of the base-fee multiplier (default `1n`). * @returns Suggested `maxFeePerGas`. */ export function estimateMaxFeePerGas(args) { const { baseFeePerGas, maxPriorityFeePerGas, multiplierNumerator = 2n, multiplierDenominator = 1n, } = args; return ((baseFeePerGas * multiplierNumerator) / multiplierDenominator + maxPriorityFeePerGas); } /** * Computes the effective gas price an EIP-1559 transaction will pay: * * ``` * effective = min(maxFeePerGas, baseFeePerGas + maxPriorityFeePerGas) * ``` * * @example * ```ts twoslash * import { Fee } from 'ox' * * Fee.effectiveGasPrice({ * baseFeePerGas: 100n, * maxFeePerGas: 200n, * maxPriorityFeePerGas: 50n * }) * // @log: 150n (= 100n + 50n) * * Fee.effectiveGasPrice({ * baseFeePerGas: 100n, * maxFeePerGas: 120n, * maxPriorityFeePerGas: 50n * }) * // @log: 120n (capped at maxFeePerGas) * ``` * * @param args.baseFeePerGas - Block base fee per gas. * @param args.maxFeePerGas - Sender-supplied `maxFeePerGas`. * @param args.maxPriorityFeePerGas - Sender-supplied tip. * @returns Effective gas price (in wei). */ export function effectiveGasPrice(args) { const { baseFeePerGas, maxFeePerGas, maxPriorityFeePerGas } = args; const ceiling = baseFeePerGas + maxPriorityFeePerGas; return ceiling < maxFeePerGas ? ceiling : maxFeePerGas; } //# sourceMappingURL=Fee.js.map