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

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

100 lines
3.4 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
use crate::*;
use ethers_core::types::Chain;
use ethers_etherscan::contract::SourceCodeMetadata;
use serial_test::serial;
/// Abi of [0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413](https://api.etherscan.io/api?module=contract&action=getsourcecode&address=0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413).
const DAO_ABI: &str = include!("../../../tests/testdata/the_dao_abi.expr");
#[tokio::test]
#[serial]
async fn can_fetch_ftm_contract_abi() {
run_with_client(Chain::Fantom, |client| async move {
let _abi = client
.contract_abi("0x80AA7cb0006d5DDD91cce684229Ac6e398864606".parse().unwrap())
.await
.unwrap();
})
.await;
}
#[tokio::test]
#[serial]
async fn can_fetch_contract_abi() {
run_with_client(Chain::Mainnet, |client| async move {
let abi = client
.contract_abi("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".parse().unwrap())
.await
.unwrap();
assert_eq!(abi, serde_json::from_str(DAO_ABI).unwrap());
})
.await;
}
#[tokio::test]
#[serial]
async fn can_fetch_contract_source_code() {
run_with_client(Chain::Mainnet, |client| async move {
let meta = client
.contract_source_code("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".parse().unwrap())
.await
.unwrap();
assert_eq!(meta.items.len(), 1);
let item = &meta.items[0];
assert!(matches!(item.source_code, SourceCodeMetadata::SourceCode(_)));
assert_eq!(item.source_code.sources().len(), 1);
assert_eq!(item.abi().unwrap(), serde_json::from_str(DAO_ABI).unwrap());
})
.await
}
#[tokio::test]
#[serial]
async fn can_get_error_on_unverified_contract() {
init_tracing();
run_with_client(Chain::Mainnet, |client| async move {
let addr = "0xb5c31a0e22cae98ac08233e512bd627885aa24e5".parse().unwrap();
let err = client.contract_source_code(addr).await.unwrap_err();
assert!(matches!(err, EtherscanError::ContractCodeNotVerified(_)));
})
.await
}
/// Query a contract that has a single string source entry instead of underlying JSON metadata.
#[tokio::test]
#[serial]
async fn can_fetch_contract_source_tree_for_singleton_contract() {
run_with_client(Chain::Mainnet, |client| async move {
let meta = client
.contract_source_code("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".parse().unwrap())
.await
.unwrap();
assert_eq!(meta.items.len(), 1);
let item = &meta.items[0];
assert!(matches!(item.source_code, SourceCodeMetadata::SourceCode(_)));
assert_eq!(item.source_code.sources().len(), 1);
assert_eq!(item.abi().unwrap(), serde_json::from_str(DAO_ABI).unwrap());
})
.await
}
/// Query a contract that has many source entries as JSON metadata and ensure they are reflected.
#[tokio::test]
#[serial]
async fn can_fetch_contract_source_tree_for_multi_entry_contract() {
run_with_client(Chain::Mainnet, |client| async move {
let meta = client
.contract_source_code("0x8d04a8c79cEB0889Bdd12acdF3Fa9D207eD3Ff63".parse().unwrap())
.await
.unwrap();
assert_eq!(meta.items.len(), 1);
assert!(matches!(meta.items[0].source_code, SourceCodeMetadata::Metadata { .. }));
let source_tree = meta.source_tree();
assert_eq!(source_tree.entries.len(), 15);
})
.await
}