Skip to content

Using with Ethers.js v6

This guide demonstrates how to use Tevm Node with Ethers.js to build powerful Ethereum applications with a familiar, developer-friendly API.

Setup

Install Dependencies

First, install the required packages:

npm install tevm ethers
Using Yarn or pnpm?
# Yarn
yarn add tevm ethers
 
# pnpm
pnpm add tevm ethers

Create the Tevm Node

Set up a Tevm node with EIP-1193 compatibility:

import { createTevmNode } from 'tevm'
import { requestEip1193 } from 'tevm/decorators'
 
// Create a Tevm Node with optional configuration
const node = createTevmNode({
  // Configure mining behavior (auto or interval)
  miningConfig: {
    type: 'interval',
    interval: 2000 // Mine blocks every 2 seconds
  }
})
 
// Add EIP-1193 compatibility layer for Ethers.js
const nodeWithEip1193 = node.extend(requestEip1193())
 
// Wait for the node to be ready
await node.ready()

Create an Ethers Provider

Connect Ethers to your Tevm node:

import { BrowserProvider } from 'ethers'
 
// Create a provider using the EIP-1193 compatible node
const provider = new BrowserProvider(nodeWithEip1193)
 
// Test the connection
const blockNumber = await provider.getBlockNumber()
console.log(`Connected to block: ${blockNumber}`)
import { JsonRpcProvider } from 'ethers'
 
// For legacy code bases that require JsonRpcProvider
const legacyProvider = new JsonRpcProvider(
  // Pass the node as an endpoint
  nodeWithEip1193
)
 
// Test the connection
const network = await legacyProvider.getNetwork()
console.log(`Connected to network: ${network.name} (${network.chainId})`)

Set Up a Wallet

Create a wallet for transactions:

import { Wallet } from 'ethers'
 
// Generate a random wallet
const wallet = Wallet.createRandom()
console.log(`Generated wallet address: ${wallet.address}`)
 
// Connect the wallet to your provider
const signer = wallet.connect(provider)
 
// The default balance will be zero
const balance = await provider.getBalance(signer.address)
console.log(`Initial wallet balance: ${balance} wei (${balance === 0n ? 'empty' : balance})`)

Fund the Account

Use Tevm's state manipulation to fund your testing wallet:

import { parseEther, formatEther } from 'ethers'
 
// Manipulate blockchain state directly with Tevm
await node.setAccount({
  address: signer.address,
  balance: parseEther('100') // Add 100 ETH
})
 
// Verify the new balance
const newBalance = await provider.getBalance(signer.address)
console.log(`New wallet balance: ${formatEther(newBalance)} ETH`)

Core Functionality

Reading Contracts

Query contract state using Ethers.js Contract objects

Writing to Contracts

Execute transactions and modify blockchain state

Event Handling

Listen for and query contract events

Contract Deployment

Deploy new contracts to your local Tevm environment

Reading from Contracts

import { Contract } from 'ethers'
import { parseAbi } from 'tevm'
import { formatUnits } from 'ethers'
 
// Define contract ABI
const abi = parseAbi([
  'function balanceOf(address owner) view returns (uint256)',
  'function decimals() view returns (uint8)',
  'function symbol() view returns (string)',
  'function name() view returns (string)'
])
 
// Create a contract instance (using USDC on mainnet for this example)
const usdcAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
const usdc = new Contract(usdcAddress, abi, provider)
 
// Read multiple values concurrently for efficiency
const [
  balance,
  decimals,
  symbol,
  name
] = await Promise.all([
  usdc.balanceOf('0x6B175474E89094C44Da98b954EedeAC495271d0F'), // DAI address
  usdc.decimals(),
  usdc.symbol(),
  usdc.name()
])
 
// Format the results
console.log(`${name} (${symbol})`)
console.log(`Balance: ${formatUnits(balance, decimals)} ${symbol}`)