Skip to content

Commit

Permalink
fix: examples (#2153)
Browse files Browse the repository at this point in the history
* fix: abigen example

* fix: gas oracle example

* fix: call override example
  • Loading branch information
DaniPopes committed Feb 14, 2023
1 parent 5f08a2d commit adefb6c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
10 changes: 6 additions & 4 deletions examples/contracts/examples/abigen.rs
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
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
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)
}

0 comments on commit adefb6c

Please sign in to comment.