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

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

51 lines
1.8 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 ethers_contract::{Contract, ContractFactory, EthEvent};
use ethers_core::{
abi::Abi,
types::{Address, Bytes},
utils::AnvilInstance,
};
use ethers_providers::{Http, Middleware, Provider};
use ethers_solc::Solc;
use std::{convert::TryFrom, sync::Arc, time::Duration};
// Note: The `EthEvent` derive macro implements the necessary conversion between `Tokens` and
// the struct
#[cfg(feature = "abigen")]
#[derive(Clone, Debug, EthEvent)]
pub struct ValueChanged {
#[ethevent(indexed)]
pub old_author: Address,
#[ethevent(indexed)]
pub new_author: Address,
pub old_value: String,
pub new_value: String,
}
/// compiles the given contract and returns the ABI and Bytecode
#[track_caller]
pub fn compile_contract(name: &str, filename: &str) -> (Abi, Bytes) {
let path = format!("./tests/solidity-contracts/{filename}");
let compiled = Solc::default().compile_source(&path).unwrap();
let contract = compiled.get(&path, name).expect("could not find contract");
let (abi, bin, _) = contract.into_parts_or_default();
(abi, bin)
}
/// connects the private key to http://localhost:8545
pub fn connect(anvil: &AnvilInstance, idx: usize) -> Arc<Provider<Http>> {
let sender = anvil.addresses()[idx];
let provider = Provider::<Http>::try_from(anvil.endpoint())
.unwrap()
.interval(Duration::from_millis(10u64))
.with_sender(sender);
Arc::new(provider)
}
/// Launches a Anvil instance and deploys the SimpleStorage contract
pub async fn deploy<M: Middleware>(client: Arc<M>, abi: Abi, bytecode: Bytes) -> Contract<M> {
let factory = ContractFactory::new(abi, bytecode, client);
let deployer = factory.deploy("initial value".to_string()).unwrap();
deployer.call().await.unwrap();
deployer.legacy().send().await.unwrap()
}