Blob Transactions
Blob Transactions are a new type of transaction in Ethereum (introduced in EIP-4844) that allows you to broadcast BLObs (Binary Large Objects) to the Ethereum network. Blob Transactions are like any other transaction, but with the added ability to carry a payload of Blobs. Blobs are extremely larger than regular calldata (~128kB), however unlike regular calldata, they are not accessible on the EVM. The EVM can only view the commitments of the blobs. Blobs are also transient, and only last for 4096 epochs (approx. 18 days).
To read more on Blob Transactions and EIP-4844, check out these resources:
In this guide, we will walk you through how to send your first Blob Transaction with Viem.
Set up Client
We will first set up our Viem Client.
Let's create a client.ts
file that holds our Client.
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
export const account = privateKeyToAccount('0x...')
export const client = createWalletClient({
account,
chain: mainnet,
transport: http()
})
Install KZG bindings
Next, we will need to install some KZG bindings. KZG will be used to compute the commitments of the blobs, and generate proofs from the blobs & commitments. The commitments and proofs are needed to serialize and sign the Blob Transaction before we send it off.
A couple of KZG implementations we recommend are:
npm i c-kzg
# or
npm i kzg-wasm
Set up KZG interface
After that, we will need to hook up the KZG bindings to Viem.
Let's create a kzg.ts
file that holds our KZG interface.
import * as cKzg from 'c-kzg'
import { setupKzg } from 'viem'
import { mainnetTrustedSetupPath } from 'viem/node'
export const kzg = setupKzg(cKzg, mainnetTrustedSetupPath)
Send Blob Transaction
Now that we have our Client and KZG interface set up, we can send our first Blob Transaction.
For demonstration purposes, we will construct a blob with a simple string: "hello world"
, and send it to the zero address.
import { parseGwei, stringToHex, toBlobs } from 'viem'
import { account, client } from './client'
import { kzg } from './kzg'
const blobs = toBlobs({ data: stringToHex('hello world') })
const hash = await client.sendTransaction({
blobs,
kzg,
maxFeePerBlobGas: parseGwei('30'),
to: '0x0000000000000000000000000000000000000000',
})
That's it!
You've just sent your first Blob Transaction with Viem.
With the hash
you received in Step 4, you can now track your Blob Transaction on a blob explorer like Blobscan.