2021-08-23 09:56:44 +00:00
|
|
|
#![cfg(not(target_arch = "wasm32"))]
|
2021-08-28 21:06:29 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "abigen")]
|
|
|
|
use ethers_core::types::Address;
|
|
|
|
|
|
|
|
#[cfg(feature = "abigen")]
|
|
|
|
use ethers_contract::EthEvent;
|
|
|
|
|
|
|
|
#[cfg(feature = "abigen")]
|
2021-03-15 11:59:52 +00:00
|
|
|
mod derive;
|
2020-06-15 08:46:07 +00:00
|
|
|
|
2021-08-28 21:06:29 +00:00
|
|
|
use ethers_contract::{Contract, ContractFactory};
|
2021-10-31 11:34:51 +00:00
|
|
|
use ethers_core::{abi::Abi, types::Bytes, utils::GanacheInstance};
|
2020-09-24 21:33:09 +00:00
|
|
|
use ethers_providers::{Http, Middleware, Provider};
|
2021-10-31 11:34:51 +00:00
|
|
|
use ethers_solc::Solc;
|
2020-06-22 08:44:08 +00:00
|
|
|
use std::{convert::TryFrom, sync::Arc, time::Duration};
|
2020-06-15 08:46:07 +00:00
|
|
|
|
2021-03-19 15:44:59 +00:00
|
|
|
// Note: The `EthEvent` derive macro implements the necessary conversion between `Tokens` and
|
2021-03-15 11:59:52 +00:00
|
|
|
// the struct
|
2021-08-28 21:06:29 +00:00
|
|
|
#[cfg(feature = "abigen")]
|
2021-03-19 15:44:59 +00:00
|
|
|
#[derive(Clone, Debug, EthEvent)]
|
2020-06-15 08:46:07 +00:00
|
|
|
pub struct ValueChanged {
|
2021-03-19 15:44:59 +00:00
|
|
|
#[ethevent(indexed)]
|
2020-06-15 08:46:07 +00:00
|
|
|
pub old_author: Address,
|
2021-03-19 15:44:59 +00:00
|
|
|
#[ethevent(indexed)]
|
2020-06-15 08:46:07 +00:00
|
|
|
pub new_author: Address,
|
|
|
|
pub old_value: String,
|
|
|
|
pub new_value: String,
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:52:09 +00:00
|
|
|
/// compiles the given contract and returns the ABI and Bytecode
|
|
|
|
pub fn compile_contract(name: &str, filename: &str) -> (Abi, Bytes) {
|
2021-10-31 11:34:51 +00:00
|
|
|
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");
|
2021-12-08 00:38:29 +00:00
|
|
|
let (abi, bin, _) = contract.into_parts_or_default();
|
|
|
|
(abi, bin)
|
2020-06-15 08:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// connects the private key to http://localhost:8545
|
2021-08-28 21:06:29 +00:00
|
|
|
pub fn connect(ganache: &GanacheInstance, idx: usize) -> Arc<Provider<Http>> {
|
|
|
|
let sender = ganache.addresses()[idx];
|
2020-09-24 21:33:09 +00:00
|
|
|
let provider = Provider::<Http>::try_from(ganache.endpoint())
|
|
|
|
.unwrap()
|
2021-08-28 21:06:29 +00:00
|
|
|
.interval(Duration::from_millis(10u64))
|
|
|
|
.with_sender(sender);
|
|
|
|
Arc::new(provider)
|
2020-06-15 08:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Launches a ganache instance and deploys the SimpleStorage contract
|
2020-09-24 21:33:09 +00:00
|
|
|
pub async fn deploy<M: Middleware>(client: Arc<M>, abi: Abi, bytecode: Bytes) -> Contract<M> {
|
2020-06-15 08:46:07 +00:00
|
|
|
let factory = ContractFactory::new(abi, bytecode, client);
|
2021-11-04 13:02:05 +00:00
|
|
|
let deployer = factory.deploy("initial value".to_string()).unwrap();
|
|
|
|
assert!(deployer.call().await.is_ok());
|
|
|
|
deployer.legacy().send().await.unwrap()
|
2020-06-15 08:46:07 +00:00
|
|
|
}
|