Skip to main content

Try it Live

Run FeeMarket examples in the interactive playground

BrandedFeeMarket

Tree-shakable namespace functions for fee market calculations.

Overview

BrandedFeeMarket/ directory contains implementation functions organized as standalone modules. Each function operates on data passed as arguments (no instance methods). All functions exported through FeeMarket.js namespace.

Pattern

Tree-shakable: bundlers only include functions you use.

Functions

EIP-1559 Base Fee

BaseFee.js
Construct next block’s base fee using EIP-1559 formula. Implementation: Standard form per EIP-1559 spec. Formula:
See eip1559.mdx#basefee

EIP-4844 Blob Fees

BlobBaseFee.js
Construct blob base fee using exponential formula. Implementation: Uses fakeExponential helper for Taylor series approximation. See eip4844.mdx#blobbasefee fakeExponential.js
Internal exponential approximation via Taylor series. Formula: factor * e^(numerator / denominator) Implementation:
calculateExcessBlobGas.js
Calculate excess blob gas for next block. Implementation:
See eip4844.mdx#calculateexcessblobgas

Transaction Fees

calculateTxFee.js
Calculate EIP-1559 transaction fee breakdown. Implementation:
See calculations.mdx#calculatetxfee calculateBlobTxFee.js
Calculate blob transaction fee (combines regular + blob fees). Implementation:
See calculations.mdx#calculateblobtxfee canIncludeTx.js
Check if transaction meets minimum fees. Implementation:
See calculations.mdx#canincludetx

State Operations

nextState.js
Calculate next block’s complete state. Implementation:
See calculations.mdx#nextstate projectBaseFees.js
Project future base fees over multiple blocks. Implementation: Iteratively calls nextState with constant usage. See calculations.mdx#projectbasefees

Query Functions

getBlobBaseFee.js
Get current blob base fee from state. Implementation:
getGasTarget.js
Get gas target (50% of limit). Implementation:
isAboveGasTarget.js
Check if gas usage exceeds target. Implementation:
isAboveBlobGasTarget.js
Check if blob gas usage exceeds target. Implementation:

Validation

validateState.js
Validate block state parameters. Checks:
  • gasUsed >= 0
  • gasLimit > 0
  • gasUsed <= gasLimit
  • baseFee >= MIN_BASE_FEE
  • excessBlobGas >= 0
  • blobGasUsed >= 0
  • blobGasUsed <= MAX_BLOB_GAS_PER_BLOCK
See utilities.mdx#validatestate validateTxFeeParams.js
Validate transaction fee parameters. Checks:
  • All values non-negative
  • maxPriorityFeePerGas <= maxFeePerGas
  • 1 <= blobCount <= 6 (if blob tx)
See utilities.mdx#validatetxfeeparams

Conversions

weiToGwei.js
Convert wei to gwei (9 decimal places). Implementation:
gweiToWei.js
Convert gwei to wei (floors fractional). Implementation:
See utilities.mdx

File Organization

Testing

FeeMarket.test.ts - Comprehensive test suite covering:
  • EIP-1559 base fee calculations (all scenarios)
  • EIP-4844 blob fee calculations
  • Transaction fee calculations
  • State transitions
  • Validation functions
  • Edge cases and boundaries
Run tests:

Usage

Tree-Shaking

Bundlers include only what you use:

Direct Imports

Import specific functions for minimal bundle size:

Namespace Import

Import all via namespace (convenient but larger bundle):

Implementation Notes

Bigint Arithmetic

All calculations use bigint for precision:

Constant-Time Operations

Fee calculations are not constant-time (no security requirement):

Pure Functions

All functions pure (no side effects):

No WASM

Pure TypeScript optimal for these lightweight calculations. See wasm.mdx.

References