Skip to content

0xKurt/eip-4844-ethers-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EIP-4844 Ethers Example

A simple TypeScript example demonstrating how to send EIP-4844 blob transactions using ethers.js v6 and KZG commitments.

Table of Contents


Prerequisites

  • Node.js >= 20
  • npm or yarn
  • A funded Sepolia wallet (for testing)
  • Basic knowledge of TypeScript/JavaScript

Quick Start

npm install
cp .env.example .env
# Edit .env with your PRIVATE_KEY
npm run blob
# Or with a contract call
npm run blob-contract

What is This?

EIP-4844 blob transactions (Type 3) allow attaching large data blobs (up to 131,072 bytes) to transactions at lower cost than calldata. Blobs are stored separately and pruned after ~18 days.

This repository contains two examples:

  1. send-blob.ts - Simple blob transaction without contract interaction
  2. send-blob-contract.ts - Blob transaction with contract function call

Configuration

Create .env file:

PRIVATE_KEY=0x1234567890abcdef...
RPC_URL=https://1rpc.io/sepolia
  • PRIVATE_KEY: Required - your wallet's private key
  • RPC_URL: Optional - defaults to Sepolia testnet if not provided

Usage

Simple Blob Transaction

npm run blob

Sends a blob transaction without contract interaction.

Blob Transaction with Contract Call

npm run blob-contract

Sends a blob transaction and calls the shout(string) function on the contract at 0x7bef193Df28Cd0B164A7227a7904Cdac09f9A1ef with the text "ethers-example".

Example Output

Simple blob transaction:

🚀 Sending blob transaction...
Blob data: "Blob vibes only 🌈"
Transaction sent: 0xabc123...
Waiting for confirmation...

✅ Transaction confirmed!
Block: 123456
Status: Success
Blob Versioned Hashes: 0xdef456...
Blobscan: https://sepolia.blobscan.com/blob/0xdef456...

Blob transaction with contract call:

🚀 Sending blob transaction with contract call...
Blob data: "Blob vibes only 🌈"
Contract: 0x7bef193Df28Cd0B164A7227a7904Cdac09f9A1ef
Function: shout("ethers-example")

Transaction sent: 0xabc123...
Waiting for confirmation...

✅ Transaction confirmed!
Block: 123456
Status: Success
Blob Versioned Hashes: 0xdef456...
Blobscan: https://sepolia.blobscan.com/blob/0xdef456...
Etherscan: https://sepolia.etherscan.io/tx/0xabc123...

RPC Endpoints

Working public RPC endpoints for Sepolia (blob transaction support):

  • https://1rpc.io/sepolia
  • https://ethereum-sepolia-rpc.publicnode.com
  • https://eth-sepolia.api.onfinality.io/public
  • https://0xrpc.io/sep
  • https://sepolia.infura.io/v3/YOUR_API_KEY (requires account)

Technical Details

EIP-4844 Overview

EIP-4844 introduces Type 3 transactions that attach "blobs" of data:

  • Stored separately in blob space (not in transaction calldata)
  • Cheaper for large data (up to 131,072 bytes per blob)
  • Pruned after ~18 days (Epoch 4096)
  • Verifiable using KZG polynomial commitments

KZG Commitments

KZG (Kate-Zaverucha-Goldberg) commitments provide:

  • Commitment to blob data without revealing it
  • Proof that blob matches commitment
  • Efficient verification (constant time, 48-byte proofs)

In this script:

  • kzg.blobToKZGCommitment(blobHex) - Creates commitment
  • kzg.computeBlobKZGProof(blobHex, commitmentHex) - Generates proof
  • Versioned hash: 0x01 + first 31 bytes of SHA-256(commitment)

Blob Versioned Hash

The blob versioned hash is derived from the KZG commitment and is used to reference blobs on-chain. It allows explorers like BlobScan to locate and verify blob data attached to a transaction. Each blob attached to a transaction has a unique versioned hash.

Blob Transaction Structure

{
  type: 3,
  to: address,
  data: '0x...',
  maxFeePerGas: bigint,
  maxPriorityFeePerGas: bigint,
  maxFeePerBlobGas: bigint,
  blobs: [{
    data: Uint8Array,        // 131,072 bytes
    commitment: Uint8Array,   // 48 bytes
    proof: Uint8Array         // 48 bytes
  }],
  kzg: KZG
}

Limits:

  • Up to 6 blobs per transaction
  • Each blob exactly 131,072 bytes (padded with zeros if smaller)

Implementation

Simple blob transaction (send-blob.ts):

  1. Blob Preparation: Creates 131,072-byte buffer, fills with UTF-8 message
  2. KZG Operations: Computes commitment and proof using kzg-wasm
  3. Transaction: Uses ethers v6 sendTransaction() with blob support
  4. Versioned Hashes: Automatically computed by ethers from commitments

Blob transaction with contract call (send-blob-contract.ts):

  1. Blob Preparation: Same as above
  2. KZG Operations: Same as above
  3. Contract Call Encoding: Encodes function call using Interface.encodeFunctionData()
  4. Transaction: Sends blob transaction with data field containing encoded function call
  5. Output: Includes both Blobscan and Etherscan links for viewing blob and contract interaction

Dependencies

  • ethers@^6.16.0 - Ethereum library with blob transaction support
  • kzg-wasm@^1.0.0 - WebAssembly KZG commitment library
  • dotenv@^17.2.3 - Environment variable loader
  • tsx@^4.21.0 - TypeScript executor

References


License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors