Skip to content
On this page

OP Viem

Overview

op-viem is a TypeScript extension for Viem that provides actions for working with OP Stack L2 chains such as Optimism and Base.

Features

  • Simplifies cross L1 & L2 interactions
  • Seamless extension to Viem
  • TypeScript ready
  • Test suite running against forked Ethereum network

Installation

bash
npm i op-viem
bash
pnpm i op-viem
bash
bun i op-viem

Example

ts
// import modules
import { createWalletClient, createPublicClient, custom, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet, base } from 'viem/chains'
import { baseAddresses } from 'op-viem/chains'
import { walletL1OpStackActions, publicL1OpStackActions, publicL2OpStackActions } from 'op-viem'

// create clients
export const opStackL1WalletClient = createWalletClient({
  chain: mainnet,
  transport: custom(window.ethereum)
}).extend(walletL1OpStackActions)

export const opStackL1PublicClient = createPublicClient({
  chain: mainnet,
  transport: http()
}).extend(publicL1OpStackActions)

export const opStackL2PublicClient = createPublicClient({
  chain: base,
  transport: http()
}).extend(publicL2OpStackActions)

// perform an action
opStackL1PublicClient.getOutputForL2Block(blockNumber: 2725977n, ...baseAddresses)