ethers-rs/ethers-etherscan/tests/it/main.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.1 KiB
Rust
Raw Normal View History

ci/test: improve CI jobs and tests (#2189) * ci: move to scripts directory * nits * ci: improve main CI jobs * fix: install script * fix * fix: use curl for windows installation * fix: wasm typo * tests: move to single binary * chore: clippy * chore: clippy * chore: clippy * fix: test command * fix: quote tests * update script * fix: action exclude * fix: dev deps * fix: only run wasm in own job * ci: add aarch64 targets * test: rm useless test * ci: update security audit * ci: add deny CI * chore: rm unused audit.toml * chore: update geth.rs * ci: remove unusable targets * fix: install script path * fix: wasm * improve script * fix: failing ci * fix: contract tests * ci: improve install script * update middleware tests * move integration etherscan tests to tests/ dir * fix: eip2930 access_list field name * add pendingtransaction must_use * add random anvil comment * ci: add miri job * ci: simplify * fixci * Revert "add pendingtransaction must_use" This reverts commit 770b21b4a3c6ef8900a6aa1cd46aa9638317a60d. * fix: macos script * fix: use curl in script * unused ci * update script * fix wasm * rm_miri * fix: signer test * fix: wasm ci * fix: ipc test * fix: live celo tests * fix: abi online source test * fix: windows paths in test * chore: update serial_test * ci: run live tests separately * fix: provider tests * fix: unused var * fix: feature * fix merge * fix: etherscan key tests * ci: rm duplicate audit * fix: split etherscan test ci * fix: etherscan test * fix: generate multiple unused ports * fix: source test * fix: udeps * rm unused
2023-03-01 00:26:27 +00:00
//! Etherscan integration tests
#![cfg(not(target_arch = "wasm32"))]
use ethers_core::types::Chain;
use ethers_etherscan::{errors::EtherscanError, Client};
use std::{
future::Future,
time::{Duration, Instant},
};
mod account;
mod contract;
mod gas;
mod transaction;
mod verify;
mod version;
#[tokio::test]
async fn check_wrong_etherscan_api_key() {
let client = Client::new(Chain::Mainnet, "ABCDEFG").unwrap();
let resp = client
.contract_source_code("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".parse().unwrap())
.await
.unwrap_err();
assert!(matches!(resp, EtherscanError::InvalidApiKey));
}
/// Calls the function with a new Etherscan Client.
pub async fn run_with_client<F, Fut, T>(chain: Chain, f: F) -> T
where
F: FnOnce(Client) -> Fut,
Fut: Future<Output = T>,
{
init_tracing();
let (client, duration) = match Client::new_from_env(chain) {
Ok(c) => (c, rate_limit(chain, true)),
Err(_) => {
(Client::builder().chain(chain).unwrap().build().unwrap(), rate_limit(chain, false))
}
};
run_at_least_duration(duration, f(client)).await
}
#[track_caller]
fn rate_limit(chain: Chain, key: bool) -> Duration {
match (chain, key) {
// Rate limit with an API key is 5 call per second.
(_, true) => Duration::from_millis(250),
// Rate limit without an API key is 1 call every 5 seconds.
// (Chain::Mainnet, false) => Duration::from_millis(5100),
(Chain::Mainnet, false) => panic!("ETHERSCAN_API_KEY is not set"),
// Ignore other chains since we don't have more than 1 test with each.
(_, false) => Duration::ZERO,
}
}
async fn run_at_least_duration<T>(duration: Duration, block: impl Future<Output = T>) -> T {
let start = Instant::now();
let output = block.await;
let elapsed = start.elapsed();
if elapsed < duration {
tokio::time::sleep(duration - elapsed).await;
}
output
}
#[track_caller]
fn init_tracing() {
let _ = tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.try_init();
}