use revm_trace::{types::BlockEnv, utils::balance_utils::query_balance};
use alloy::primitives::{address, utils::format_units};
use anyhow::Result;
use colored::*;
#[cfg(not(feature = "foundry-fork"))]
use revm_trace::create_evm;
#[cfg(feature = "foundry-fork")]
use revm_trace::create_shared_evm;
const ETH_RPC_URL: &str = "https://eth.llamarpc.com";
async fn show_balance() -> Result<()> {
#[cfg(not(feature = "foundry-fork"))]
println!("{}", "đ§ Testing AlloyDB backend".yellow().bold());
#[cfg(feature = "foundry-fork")]
println!("{}", "đ§ Testing Foundry fork backend".blue().bold());
let some_address = address!("0x892D4Cc1c2C1cee9091b1F30F64423693F333333");
println!("Testing address: {} \n", some_address);
let test_blocks = vec![
("Before Transfer", 22770150), ("After Transfer", 22770152), ];
#[cfg(not(feature = "foundry-fork"))]
let mut evm = create_evm(ETH_RPC_URL).await?;
#[cfg(feature = "foundry-fork")]
let mut evm = create_shared_evm(ETH_RPC_URL).await?;
for (era_name, block_number) in test_blocks {
let block_env = BlockEnv {
number: block_number,
timestamp: 1700000000, ..Default::default()
};
println!("{} - setting block {}...", era_name, block_number);
match evm.set_db_block(block_env) {
#[cfg(not(feature = "foundry-fork"))]
Ok(()) => println!("â
AlloyDB block reset successful"),
#[cfg(feature = "foundry-fork")]
Ok(()) => println!("â
SharedBackend block reset successful"),
Err(e) => {
println!("â AlloyDB block reset failed: {}", e);
continue;
}
}
match query_balance(&mut evm, some_address) {
Ok(balance) => {
println!("{} query: {} wei", era_name, balance);
let eth_balance =
format_units(balance, "ether").unwrap_or_else(|_| "N/A".to_string());
println!("{} query: {} ETH", era_name, eth_balance);
}
Err(e) => {
println!("{} query error: {}", era_name, e);
}
}
println!(); }
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
println!(
"{}",
"đ Testing balance queries at different block heights with both backends\n"
.cyan()
.bold()
);
show_balance().await?;
println!("{}", "â".repeat(60));
println!();
#[cfg(feature = "foundry-fork")]
println!(
"{}",
"â
Balance query test completed for both backends!"
.green()
.bold()
);
#[cfg(feature = "foundry-fork")]
println!(
"{}",
"⨠Success! Both AlloyDB and SharedBackend support ResetBlock trait!".green()
);
#[cfg(not(feature = "foundry-fork"))]
println!(
"{}",
"â
Balance query test completed for AlloyDB backend!"
.green()
.bold()
);
#[cfg(not(feature = "foundry-fork"))]
println!(
"{}",
"âšī¸ SharedBackend test skipped (foundry-fork feature not enabled)".yellow()
);
println!("{}", "đĄ The balance difference includes both the transfer amount (0.09445674 ETH) and gas fees (~0.00115 ETH)".cyan());
println!(
"{}",
"đ ResetBlock trait with associated Error type works correctly!".magenta()
);
Ok(())
}