fix: examples (#2153)

* fix: abigen example

* fix: gas oracle example

* fix: call override example
This commit is contained in:
DaniPopes 2023-02-14 02:08:08 +01:00 committed by GitHub
parent 5f08a2d931
commit adefb6c2f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 12 deletions

View File

@ -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(())
}

View File

@ -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:?}"),
}
}

View File

@ -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);
@ -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)
}