Getting started with OP Stack
Viem provides first-class support for chains implemented on the OP Stack.
The OP Stack is a set of modular open-source software that enable developers to build fast, secure, and scalable Layer 2 Ethereum protocols & applications. Read more.
Quick Start
1. Set up your Client & Transport
Firstly, set up your Client with a desired Transport & OP Stack Chain.
ts
import { createPublicClient, http } from 'viem'
import { base } from 'viem/chains'
const client = createPublicClient({
chain: base,
transport: http(),
})
import { createPublicClient, http } from 'viem'
import { base } from 'viem/chains'
const client = createPublicClient({
chain: base,
transport: http(),
})
INFO
In a production app, it is highly recommended to pass through your authenticated RPC provider URL (Alchemy, Infura, Ankr, etc). If no URL is provided, viem will default to a public RPC provider. Read more.
2. Extend Client with the OP Stack
Now that you have a Client set up, you can extend it with OP Stack Actions! Read more.
ts
import { createPublicClient, http } from 'viem'
import { base } from 'viem/chains'
import { publicActionsL2 } from 'viem/op-stack'
const client = createPublicClient({
chain: base,
transport: http(),
}).extend(publicActionsL2())
import { createPublicClient, http } from 'viem'
import { base } from 'viem/chains'
import { publicActionsL2 } from 'viem/op-stack'
const client = createPublicClient({
chain: base,
transport: http(),
}).extend(publicActionsL2())
3. Consume OP Stack Actions
Now that you have an OP Stack Client set up, you can now interact with the OP Stack and consume Actions!
tsx
import { createPublicClient, http, parseEther } from 'viem'
import { mainnet } from 'viem/chains'
const client = createPublicClient({
chain: mainnet,
transport: http(),
}).extend(publicActionsL2())
const l1Gas = await client.estimateL1Gas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
import { createPublicClient, http, parseEther } from 'viem'
import { mainnet } from 'viem/chains'
const client = createPublicClient({
chain: mainnet,
transport: http(),
}).extend(publicActionsL2())
const l1Gas = await client.estimateL1Gas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})