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-29 12:29:35 +00:00
|
|
|
use ethers_core::{
|
|
|
|
abi::Abi,
|
|
|
|
types::Bytes,
|
|
|
|
utils::{GanacheInstance, Solc},
|
|
|
|
};
|
2020-09-24 21:33:09 +00:00
|
|
|
use ethers_providers::{Http, Middleware, Provider};
|
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-29 12:29:35 +00:00
|
|
|
let compiled = Solc::new(&format!("./tests/solidity-contracts/{}", filename)).build().unwrap();
|
2020-07-03 16:52:09 +00:00
|
|
|
let contract = compiled.get(name).expect("could not find contract");
|
2020-06-15 08:46:07 +00:00
|
|
|
(contract.abi.clone(), contract.bytecode.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-10-29 12:29:35 +00:00
|
|
|
factory.deploy("initial value".to_string()).unwrap().legacy().send().await.unwrap()
|
2020-06-15 08:46:07 +00:00
|
|
|
}
|