2020-06-15 08:46:07 +00:00
|
|
|
use ethers_core::{
|
|
|
|
abi::{Abi, Detokenize, InvalidOutputType, Token},
|
|
|
|
types::{Address, Bytes},
|
|
|
|
};
|
|
|
|
|
|
|
|
use ethers_contract::{Contract, ContractFactory};
|
2020-06-22 13:42:34 +00:00
|
|
|
use ethers_core::utils::{GanacheInstance, Solc};
|
2020-06-15 08:46:07 +00:00
|
|
|
use ethers_providers::{Http, Provider};
|
|
|
|
use ethers_signers::{Client, Wallet};
|
2020-06-22 08:44:08 +00:00
|
|
|
use std::{convert::TryFrom, sync::Arc, time::Duration};
|
2020-06-15 08:46:07 +00:00
|
|
|
|
|
|
|
// Note: We also provide the `abigen` macro for generating these bindings automatically
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ValueChanged {
|
|
|
|
pub old_author: Address,
|
|
|
|
pub new_author: Address,
|
|
|
|
pub old_value: String,
|
|
|
|
pub new_value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Detokenize for ValueChanged {
|
|
|
|
fn from_tokens(tokens: Vec<Token>) -> Result<ValueChanged, InvalidOutputType> {
|
|
|
|
let old_author: Address = tokens[1].clone().to_address().unwrap();
|
|
|
|
let new_author: Address = tokens[1].clone().to_address().unwrap();
|
|
|
|
let old_value = tokens[2].clone().to_string().unwrap();
|
|
|
|
let new_value = tokens[3].clone().to_string().unwrap();
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
old_author,
|
|
|
|
new_author,
|
|
|
|
old_value,
|
|
|
|
new_value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// connects the private key to http://localhost:8545
|
2020-06-22 13:42:34 +00:00
|
|
|
pub fn connect(ganache: &GanacheInstance, idx: usize) -> Arc<Client<Http, Wallet>> {
|
|
|
|
let provider = Provider::<Http>::try_from(ganache.endpoint()).unwrap();
|
|
|
|
let wallet: Wallet = ganache.keys()[idx].clone().into();
|
|
|
|
Arc::new(
|
|
|
|
wallet
|
|
|
|
.connect(provider)
|
|
|
|
.interval(Duration::from_millis(10u64)),
|
|
|
|
)
|
2020-06-15 08:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Launches a ganache instance and deploys the SimpleStorage contract
|
2020-06-22 08:44:08 +00:00
|
|
|
pub async fn deploy(
|
|
|
|
client: Arc<Client<Http, Wallet>>,
|
2020-06-15 08:46:07 +00:00
|
|
|
abi: Abi,
|
|
|
|
bytecode: Bytes,
|
2020-06-22 13:42:34 +00:00
|
|
|
) -> Contract<Http, Wallet> {
|
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
|
|
|
}
|