diff --git a/Cargo.lock b/Cargo.lock index 1a40e0139b..35507b6a43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1428,6 +1428,21 @@ dependencies = [ "tokio", ] +[[package]] +name = "example-uniswap-v2-usdc-swap" +version = "0.0.0" +dependencies = [ + "alloy-eips", + "alloy-provider", + "alloy-sol-types", + "alloy-transport-http", + "anyhow", + "reqwest", + "revm", + "revm-database", + "tokio", +] + [[package]] name = "eyre" version = "0.6.12" diff --git a/Cargo.toml b/Cargo.toml index a869b56368..d3adb3e832 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,11 +22,11 @@ members = [ # examples "examples/block_traces", "examples/contract_deployment", - #"examples/custom_opcodes", "examples/database_components", "examples/database_ref", "examples/uniswap_get_reserves", - #"examples/uniswap_v2_usdc_swap", + "examples/uniswap_v2_usdc_swap", + #"examples/custom_opcodes", ] [workspace.dependencies] diff --git a/bins/revme/Cargo.toml b/bins/revme/Cargo.toml index 0bda112fd6..4032a8ae09 100644 --- a/bins/revme/Cargo.toml +++ b/bins/revme/Cargo.toml @@ -12,7 +12,7 @@ repository.workspace = true # revm database.workspace = true revm = { workspace = true, features = ["std", "hashbrown", "c-kzg", "blst"] } -inspector = { workspace = true, features = ["std", "serde", "serde-json"] } +inspector = { workspace = true, features = ["std", "serde-json"] } hash-db = "0.15" hex = "0.4" diff --git a/examples/uniswap_get_reserves/Cargo.toml b/examples/uniswap_get_reserves/Cargo.toml index 9a20f21f74..e6335b2d12 100644 --- a/examples/uniswap_get_reserves/Cargo.toml +++ b/examples/uniswap_get_reserves/Cargo.toml @@ -36,9 +36,5 @@ alloy-sol-types = { version = "0.8.2", default-features = false, features = [ alloy-eips = "0.3.6" alloy-provider = "0.3" -# criterion = "0.5" -# indicatif = "0.17" -# reqwest = { version = "0.12" } -# rstest = "0.22.0" - +# mics anyhow = "1.0.89" diff --git a/examples/uniswap_v2_usdc_swap/Cargo.toml b/examples/uniswap_v2_usdc_swap/Cargo.toml index ad9f88e0b5..3aba647e5c 100644 --- a/examples/uniswap_v2_usdc_swap/Cargo.toml +++ b/examples/uniswap_v2_usdc_swap/Cargo.toml @@ -35,7 +35,5 @@ alloy-sol-types = { version = "0.8.2", default-features = false, features = [ alloy-eips = "0.3.6" alloy-transport-http = "0.3" alloy-provider = "0.3" -indicatif = "0.17" reqwest = { version = "0.12" } -rstest = "0.22.0" anyhow = "1.0.89" diff --git a/examples/uniswap_v2_usdc_swap/src/main.rs b/examples/uniswap_v2_usdc_swap/src/main.rs index cd86dbdc99..012899b9bb 100644 --- a/examples/uniswap_v2_usdc_swap/src/main.rs +++ b/examples/uniswap_v2_usdc_swap/src/main.rs @@ -1,36 +1,36 @@ //! Example of uniswap getReserves() call emulation. #![cfg_attr(not(test), warn(unused_crate_dependencies))] -use alloy_provider::{network::Ethereum, ProviderBuilder, RootProvider}; use alloy_eips::BlockId; +use alloy_provider::{network::Ethereum, ProviderBuilder, RootProvider}; use alloy_sol_types::{sol, SolCall, SolValue}; use alloy_transport_http::Http; use anyhow::{anyhow, Result}; -use reqwest::Client; use database::{AlloyDB, CacheDB}; +use reqwest::Client; use revm::{ - primitives::{ - address, keccak256, Address, Bytes, TxKind, U256, + primitives::{address, keccak256, Address, Bytes, TxKind, U256}, + state::AccountInfo, + wiring::{ + result::{ExecutionResult, Output}, + EthereumWiring, }, - state::{AccountInfo}, - wiring::{result::{ExecutionResult,Output}}, - database_interface::Database, Evm, }; use std::ops::Div; -use std::sync::Arc; -type AlloyCacheDB = CacheDB, Ethereum, Arc>>>>; +type AlloyCacheDB = CacheDB, Ethereum, RootProvider>>>; #[tokio::main] async fn main() -> Result<()> { - let client = ProviderBuilder::new().on_http( - "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27" - .parse() - .unwrap(), - ); - let client = Arc::new(client); - let mut cache_db = CacheDB::new(AlloyDB::new(client, BlockId::default())); + // Set up the HTTP transport which is consumed by the RPC client. + let rpc_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27".parse()?; + + // create ethers client and wrap it in Arc + let client = ProviderBuilder::new().on_http(rpc_url); + + let alloy = AlloyDB::new(client, BlockId::latest()).unwrap(); + let mut cache_db = CacheDB::new(alloy); // Random empty account let account = address!("18B06aaF27d44B756FCF16Ca20C1f183EB49111f"); @@ -86,18 +86,20 @@ async fn main() -> Result<()> { let acc_usdc_balance_after = balance_of(usdc, account, &mut cache_db)?; println!("USDC balance after swap: {}", acc_usdc_balance_after); + println!("OK"); Ok(()) } -fn balance_of(token: Address, address: Address, cache_db: &mut AlloyCacheDB) -> Result { +fn balance_of(token: Address, address: Address, alloy_db: &mut AlloyCacheDB) -> Result { sol! { function balanceOf(address account) public returns (uint256); } let encoded = balanceOfCall { account: address }.abi_encode(); - let mut evm = Evm::builder() - .with_db(cache_db) + let mut evm = Evm::>::builder() + .with_db(alloy_db) + .with_default_ext_ctx() .modify_tx_env(|tx| { // 0x1 because calling USDC proxy from zero address fails tx.caller = address!("0000000000000000000000000000000000000001"); @@ -141,8 +143,9 @@ async fn get_amount_out( } .abi_encode(); - let mut evm = Evm::builder() + let mut evm = Evm::>::builder() .with_db(cache_db) + .with_default_ext_ctx() .modify_tx_env(|tx| { tx.caller = address!("0000000000000000000000000000000000000000"); tx.transact_to = TxKind::Call(uniswap_v2_router); @@ -174,8 +177,9 @@ fn get_reserves(pair_address: Address, cache_db: &mut AlloyCacheDB) -> Result<(U let encoded = getReservesCall {}.abi_encode(); - let mut evm = Evm::builder() + let mut evm = Evm::>::builder() .with_db(cache_db) + .with_default_ext_ctx() .modify_tx_env(|tx| { tx.caller = address!("0000000000000000000000000000000000000000"); tx.transact_to = TxKind::Call(pair_address); @@ -223,13 +227,15 @@ fn swap( } .abi_encode(); - let mut evm = Evm::builder() + let mut evm = Evm::>::builder() .with_db(cache_db) + .with_default_ext_ctx() .modify_tx_env(|tx| { tx.caller = from; tx.transact_to = TxKind::Call(pool_address); tx.data = encoded.into(); tx.value = U256::from(0); + tx.nonce = 1; }) .build(); @@ -256,8 +262,9 @@ fn transfer( let encoded = transferCall { to, amount }.abi_encode(); - let mut evm = Evm::builder() + let mut evm = Evm::>::builder() .with_db(cache_db) + .with_default_ext_ctx() .modify_tx_env(|tx| { tx.caller = from; tx.transact_to = TxKind::Call(token);