This library provides a simple API for interacting with an embedded Ethereum Virtual Machine (EVM).
[]
= "0.2.5"
Supports
- Both an in-memory database and the ability to fork state from a remote node
- Snapshot of EVM state for later use
- Both raw encoding/decoding of function calls as well as alloy SolTypes
- Lightweight creation of ABI from human-readable syntax
Examples
Create and interact with the EVM
use ;
use ;
// Generate some random addresses
let addresses = generate_random_addresses;
let bob = addresses;
let alice = addresses;
// create the EVM with in-memory database (default)
let mut evm = default;
// create 2 accounts. Bob w/ 2 ether, alice w/ none
evm.create_account.unwrap;
evm.create_account.unwrap;
// check the balances
assert!;
assert!;
Fork a remote contract
Interacting with a remote contract pulls the state of the remote contract into the local in-memory database for use.
use ;
use ;
// create ABI inline
let abi = from_human_readable;
// create a fork using the latest block
let fork_info = CreateFork.latest_block;
let mut evm = new;
// remote contract address.
// using DAI: 0x6B175474E89094C44Da98b954EedeAC495271d0F
let contract_address = address!;
// encode the function call
let =
abi.encode_function.unwrap;
// call the function on the remote contract
let output = evm.transact_call.unwrap;
// decode the result
let value = decoder.unwrap.abi_decode
println!;
See uniswap for an example of using a fork and snapshot to trade a pair on Uniswap.
To run the example:
> cargo
Standing on the shoulders of giants...
Thanks to the following projects for making this work possible!
- revm
- alloy-rs
- foundry-rs for much of the design influence