diff --git a/examples/contracts/examples/abigen.rs b/examples/contracts/examples/abigen.rs index b29fa2b2..f475dc17 100644 --- a/examples/contracts/examples/abigen.rs +++ b/examples/contracts/examples/abigen.rs @@ -32,10 +32,12 @@ async fn main() -> Result<()> { } fn rust_file_generation() -> Result<()> { - let base_dir = "./examples/contracts/examples/abi"; - Abigen::new("IERC20", format!("{base_dir}/IERC20.json"))? - .generate()? - .write_to_file(format!("{base_dir}/ierc20.rs"))?; + let abi_source = "./examples/contracts/examples/abi/IERC20.json"; + let out_file = std::env::temp_dir().join("ierc20.rs"); + if out_file.exists() { + std::fs::remove_file(&out_file)?; + } + Abigen::new("IERC20", abi_source)?.generate()?.write_to_file(out_file)?; Ok(()) } diff --git a/examples/middleware/examples/gas_oracle.rs b/examples/middleware/examples/gas_oracle.rs index 95105aeb..ea6795aa 100644 --- a/examples/middleware/examples/gas_oracle.rs +++ b/examples/middleware/examples/gas_oracle.rs @@ -39,13 +39,18 @@ async fn blocknative() { async fn etherscan() { let chain = Chain::Mainnet; - let api_key: String = std::env::var("ETHERSCAN_API_KEY_ETHEREUM").expect("Provide an API key"); - if let Ok(client) = Client::new(chain, api_key) { - let oracle = Etherscan::new(client).category(GasCategory::Fast); - match oracle.fetch().await { - Ok(gas_price) => println!("[Etherscan]: Gas price is {gas_price:?}"), - Err(e) => panic!("[Etherscan]: Cannot estimate gas: {e:?}"), - } + let client = match Client::new_from_env(chain) { + Ok(client) => client, + Err(_) => Client::builder() + .chain(chain) + .expect("Mainnet is valid") + .build() + .expect("Mainnet is valid"), + }; + let oracle = Etherscan::new(client).category(GasCategory::Fast); + match oracle.fetch().await { + Ok(gas_price) => println!("[Etherscan]: Gas price is {gas_price:?}"), + Err(e) => panic!("[Etherscan]: Cannot estimate gas: {e:?}"), } } diff --git a/examples/transactions/examples/call_override.rs b/examples/transactions/examples/call_override.rs index b80191e8..4a124fce 100644 --- a/examples/transactions/examples/call_override.rs +++ b/examples/transactions/examples/call_override.rs @@ -10,12 +10,27 @@ use ethers::{ }, }; use eyre::Result; -use std::sync::Arc; +use std::{ + process::{Command, Stdio}, + sync::Arc, +}; abigen!(Greeter, "ethers-contract/tests/solidity-contracts/greeter.json",); #[tokio::main] async fn main() -> Result<()> { + match geth_version() { + e @ Ok(false) | e @ Err(_) => { + eprint!("Error spawning geth, skipping example"); + if let Err(e) = e { + eprint!(": {e}"); + } + eprintln!(); + return Ok(()) + } + Ok(true) => {} + } + let geth = Geth::new().spawn(); let provider = Provider::::try_from(geth.endpoint()).unwrap(); let client = Arc::new(provider); @@ -75,3 +90,13 @@ fn encode_string_for_storage(s: &str) -> H256 { bytes.push(len as u8 * 2); H256::from_slice(&bytes) } + +fn geth_version() -> Result { + Command::new("geth") + .arg("version") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .map(|s| s.success()) + .map_err(Into::into) +}