Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trevm"
version = "0.23.8"
version = "0.26.0"
rust-version = "1.83.0"
edition = "2021"
authors = ["init4"]
Expand Down Expand Up @@ -34,7 +34,7 @@ name = "fork_ref_transact"
required-features = ["alloy-db"]

[dependencies]
alloy = { version = "1.0.5", default-features = false, features = [
alloy = { version = "1.0.13", default-features = false, features = [
"consensus",
"rpc-types-mev",
"eips",
Expand All @@ -44,7 +44,7 @@ alloy = { version = "1.0.5", default-features = false, features = [
"sol-types",
] }

revm = { version = "23.1.0", default-features = false }
revm = { version = "26.0.1", default-features = false }

dashmap = { version = "6.1.0", optional = true }
tracing = { version = "0.1.41", optional = true }
Expand All @@ -53,10 +53,10 @@ thiserror = "2.0.11"
tokio = { version = "1.44", optional = true }

[dev-dependencies]
revm = { version = "23.1.0", features = ["serde-json", "std", "alloydb"] }
revm = { version = "26.0.1", features = ["serde-json", "std", "alloydb"] }
trevm = { path = ".", features = ["test-utils"] }

alloy = { version = "1.0.5", features = ["providers", "transports"] }
alloy = { version = "1.0.13", features = ["providers", "transports"] }

# misc
eyre = "0.6"
Expand Down Expand Up @@ -87,7 +87,6 @@ estimate_gas = ["optional_eip3607", "optional_no_base_fee", "dep:tracing"]
test-utils = ["revm/std", "revm/serde-json", "revm/alloydb"]

secp256k1 = ["revm/secp256k1"]
secp256r1 = ["revm/secp256r1"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this revm feature removed?

Copy link
Member Author

@Evalir Evalir Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was removed as a feature and it's now included by default—but only activated on the Osaka spec onwards. means we need to enable it/inject it ourselves wherever else we actually want to use the Opcode

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c-kzg = ["revm/c-kzg"]
blst = ["revm/blst"]

Expand Down
128 changes: 128 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Trevm Examples

This directory contains comprehensive examples demonstrating various aspects of the Trevm library.

## Examples Overview

### 1. `basic_transact.rs`
Basic contract interaction example showing:
- Contract deployment to memory database
- Simple contract call execution
- Basic transaction handling

**Run with:**
```bash
cargo run --example basic_transact
```

### 2. `fork_ref_transact.rs`
Mainnet forking example demonstrating:
- Querying real Ethereum state using AlloyDb
- Calling live contracts (Uniswap V2 pair)
- Reading storage slots from mainnet

**Run with:**
```bash
cargo run --example fork_ref_transact --features alloy-db
```

### 3. `trevm_basic_example.rs`
Simple Trevm usage showing:
- Type-safe transaction building
- Error handling patterns
- State management

**Run with:**
```bash
cargo run --example trevm_basic_example
```

### 4. `trevm_inspector_example.rs`
Advanced example with inspector integration:
- Contract deployment with tracing
- EIP-3155 tracer integration
- Multi-transaction workflow

**Run with:**
```bash
cargo run --example trevm_inspector_example
```

### 5. `trevm_block_driver_example.rs`
Block processing example showing:
- Custom BlockDriver implementation
- Multi-transaction batching
- Receipt generation

**Run with:**
```bash
cargo run --example trevm_block_driver_example
```

### 6. `tx_tracer_cli.rs` 🎯
**CLI tool for tracing any Ethereum transaction:**
- Fetches transaction from mainnet
- Replays with detailed EIP-3155 tracing
- Shows state changes and execution details

**Run with:**
```bash
cargo run --example tx_tracer_cli --features alloy-db -- <tx_hash> [rpc_url]
```

**Example usage:**
```bash
# Trace a specific transaction
cargo run --example tx_tracer_cli --features alloy-db -- 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef

# Use custom RPC endpoint
cargo run --example tx_tracer_cli --features alloy-db -- 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef https://mainnet.infura.io/v3/YOUR_KEY
```

## Key Concepts Demonstrated

### Typestate Pattern
All examples show how Trevm's typestate pattern prevents common EVM usage errors:
- Can't execute without configuration
- Can't apply state without successful execution
- Compile-time enforcement of correct state transitions

### Error Handling
Examples demonstrate proper error handling:
- `accept_state()` for successful transactions
- `discard_error()` for failed transactions
- Type-safe error recovery

### Database Integration
Various database backends:
- `InMemoryDB` for testing
- `AlloyDb` for mainnet forking
- `CacheDB` for performance optimization

### Inspector Integration
Examples show how to integrate revm inspectors:
- `NoOpInspector` for basic usage
- `TracerEip3155` for detailed tracing
- Custom inspectors for specialized needs

## Building and Running

All examples can be built with:
```bash
cargo build --examples
```

For examples requiring network access (alloy-db feature):
```bash
cargo build --examples --features alloy-db
```

## Usage in Your Projects

These examples serve as templates for common Trevm usage patterns:

1. **Testing contracts**: Use `trevm_basic_example.rs` pattern
2. **Mainnet simulation**: Use `fork_ref_transact.rs` pattern
3. **Transaction tracing**: Use `trevm_inspector_example.rs` pattern
4. **Block processing**: Use `trevm_block_driver_example.rs` pattern
5. **CLI tools**: Use `tx_tracer_cli.rs` pattern
55 changes: 55 additions & 0 deletions examples/trevm_basic_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Basic Trevm example demonstrating safe transaction execution

use revm::database::InMemoryDB;
use revm::{
context::TxEnv,
primitives::{hex, Address, TxKind},
};
use trevm::{NoopBlock, NoopCfg, TrevmBuilder, Tx};

struct MyTransaction {
caller: Address,
to: Address,
data: Vec<u8>,
}

impl Tx for MyTransaction {
fn fill_tx_env(&self, tx_env: &mut TxEnv) {
tx_env.caller = self.caller;
tx_env.kind = TxKind::Call(self.to);
tx_env.data = self.data.clone().into();
}
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Trevm provides safe, guided workflow
let tx = MyTransaction {
caller: Address::with_last_byte(1),
to: Address::with_last_byte(2),
data: hex::decode("deadbeef")?,
};

// Type-safe builder pattern
let result = TrevmBuilder::new()
.with_db(InMemoryDB::default())
.build_trevm()? // EvmNeedsCfg
.fill_cfg(&NoopCfg) // EvmNeedsBlock
.fill_block(&NoopBlock) // EvmNeedsTx
.run_tx(&tx); // Result<EvmTransacted, EvmErrored>

// Safe error handling with type guarantees
match result {
Ok(transacted) => {
println!("Transaction succeeded!");
let _final_evm = transacted.accept_state(); // EvmNeedsTx
// State automatically applied
}
Err(errored) => {
println!("Transaction failed: {:?}", errored.error());
let _recovered_evm = errored.discard_error(); // EvmNeedsTx
// Can continue with next transaction
}
}

Ok(())
}
Loading
Loading