Using with Ethers.js v6
Info: Tevm integrates seamlessly with Ethers.js v6 allowing you to use your favorite Ethereum library alongside Tevm's powerful in-memory EVM capabilities.
This guide demonstrates how to use Tevm Node with Ethers.js to build powerful Ethereum applications with a familiar, developer-friendly API.
Setup
1. Install Dependencies
First, install the required packages:
npm install tevm ethers
# Yarn
yarn add tevm ethers
# pnpm
pnpm add tevm ethers
2. 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()
Tip: The
requestEip1193()
decorator is essential - it adds the standard Ethereum provider interface that Ethers.js requires.
3. Create an Ethers Provider
Connect Ethers to your Tevm node:
BrowserProvider (Recommended)
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}`)
Success: BrowserProvider is recommended for most applications - it's the modern Ethers.js provider and handles all async operations correctly.
JsonRpcProvider (Legacy)
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})`)
4. 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})`)
5. 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`)
Tip: This direct state manipulation is one of Tevm's powerful features - it allows you to set up any testing scenario without complex transactions.
Core Functionality
- Reading Contracts -