Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

fix: examples #2153

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 6 additions & 4 deletions examples/contracts/examples/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
19 changes: 12 additions & 7 deletions examples/middleware/examples/gas_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"),
}
}

Expand Down
27 changes: 26 additions & 1 deletion examples/transactions/examples/call_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Http>::try_from(geth.endpoint()).unwrap();
let client = Arc::new(provider);
Expand Down Expand Up @@ -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<bool> {
Command::new("geth")
.arg("version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.map(|s| s.success())
.map_err(Into::into)
}