2021-03-15 11:59:52 +00:00
|
|
|
mod derive;
|
2020-06-15 08:46:07 +00:00
|
|
|
use ethers_core::{
|
2021-03-15 11:59:52 +00:00
|
|
|
abi::Abi,
|
2020-06-15 08:46:07 +00:00
|
|
|
types::{Address, Bytes},
|
|
|
|
};
|
|
|
|
|
2021-03-19 15:44:59 +00:00
|
|
|
use ethers_contract::{Contract, ContractFactory, EthEvent};
|
2020-06-22 13:42:34 +00:00
|
|
|
use ethers_core::utils::{GanacheInstance, Solc};
|
2020-10-08 15:56:36 +00:00
|
|
|
use ethers_middleware::signer::SignerMiddleware;
|
2020-09-24 21:33:09 +00:00
|
|
|
use ethers_providers::{Http, Middleware, Provider};
|
2020-10-02 08:41:16 +00:00
|
|
|
use ethers_signers::LocalWallet;
|
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-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) {
|
|
|
|
let compiled = Solc::new(&format!("./tests/solidity-contracts/{}", filename))
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let contract = compiled.get(name).expect("could not find contract");
|
2020-06-15 08:46:07 +00:00
|
|
|
(contract.abi.clone(), contract.bytecode.clone())
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:56:36 +00:00
|
|
|
type HttpWallet = SignerMiddleware<Provider<Http>, LocalWallet>;
|
2020-09-24 21:33:09 +00:00
|
|
|
|
2020-06-15 08:46:07 +00:00
|
|
|
/// connects the private key to http://localhost:8545
|
2020-09-24 21:33:09 +00:00
|
|
|
pub fn connect(ganache: &GanacheInstance, idx: usize) -> Arc<HttpWallet> {
|
|
|
|
let provider = Provider::<Http>::try_from(ganache.endpoint())
|
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(10u64));
|
2020-10-02 08:41:16 +00:00
|
|
|
let wallet: LocalWallet = ganache.keys()[idx].clone().into();
|
2020-10-08 15:56:36 +00:00
|
|
|
Arc::new(SignerMiddleware::new(provider, wallet))
|
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);
|
2020-06-22 13:42:34 +00:00
|
|
|
factory
|
2020-06-15 08:46:07 +00:00
|
|
|
.deploy("initial value".to_string())
|
|
|
|
.unwrap()
|
|
|
|
.send()
|
|
|
|
.await
|
2020-06-22 13:42:34 +00:00
|
|
|
.unwrap()
|
2020-06-15 08:46:07 +00:00
|
|
|
}
|