2020-05-26 11:00:56 +00:00
|
|
|
use anyhow::Result;
|
2020-05-31 14:50:00 +00:00
|
|
|
use ethers::{
|
2020-05-31 16:20:08 +00:00
|
|
|
prelude::*,
|
2020-06-11 06:45:14 +00:00
|
|
|
utils::{Ganache, Solc},
|
2020-05-31 14:50:00 +00:00
|
|
|
};
|
2020-06-22 08:44:08 +00:00
|
|
|
use std::{convert::TryFrom, sync::Arc, time::Duration};
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2020-06-02 10:58:48 +00:00
|
|
|
// Generate the type-safe contract bindings by providing the ABI
|
2020-05-26 18:57:59 +00:00
|
|
|
abigen!(
|
|
|
|
SimpleContract,
|
2020-06-16 12:08:42 +00:00
|
|
|
r#"[{"inputs":[{"internalType":"string","name":"value","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"author","type":"address"},{"indexed":false,"internalType":"string","name":"oldValue","type":"string"},{"indexed":false,"internalType":"string","name":"newValue","type":"string"}],"name":"ValueChanged","type":"event"},{"inputs":[],"name":"getValue","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"value","type":"string"}],"name":"setValue","outputs":[],"stateMutability":"nonpayable","type":"function"}]"#,
|
2020-05-26 18:57:59 +00:00
|
|
|
event_derives(serde::Deserialize, serde::Serialize)
|
|
|
|
);
|
2020-05-26 14:43:51 +00:00
|
|
|
|
2020-05-25 10:05:00 +00:00
|
|
|
#[tokio::main]
|
2020-05-26 11:00:56 +00:00
|
|
|
async fn main() -> Result<()> {
|
2020-05-31 14:50:00 +00:00
|
|
|
// 1. compile the contract (note this requires that you are inside the `ethers/examples` directory)
|
|
|
|
let compiled = Solc::new("./contract.sol").build()?;
|
|
|
|
let contract = compiled
|
|
|
|
.get("SimpleStorage")
|
|
|
|
.expect("could not find contract");
|
|
|
|
|
|
|
|
// 2. launch ganache
|
2020-06-22 13:42:34 +00:00
|
|
|
let ganache = Ganache::new().spawn();
|
2020-05-31 14:50:00 +00:00
|
|
|
|
|
|
|
// 3. instantiate our wallet
|
2020-06-22 13:42:34 +00:00
|
|
|
let wallet: Wallet = ganache.keys()[0].clone().into();
|
2020-05-31 14:50:00 +00:00
|
|
|
|
|
|
|
// 4. connect to the network
|
2020-06-22 13:42:34 +00:00
|
|
|
let provider =
|
|
|
|
Provider::<Http>::try_from(ganache.endpoint())?.interval(Duration::from_millis(10u64));
|
2020-05-31 14:50:00 +00:00
|
|
|
|
|
|
|
// 5. instantiate the client with the wallet
|
2020-09-24 21:33:09 +00:00
|
|
|
let client = Client::new(provider, wallet);
|
2020-06-22 08:44:08 +00:00
|
|
|
let client = Arc::new(client);
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2020-05-31 14:50:00 +00:00
|
|
|
// 6. create a factory which will be used to deploy instances of the contract
|
2020-06-22 08:44:08 +00:00
|
|
|
let factory = ContractFactory::new(
|
|
|
|
contract.abi.clone(),
|
|
|
|
contract.bytecode.clone(),
|
|
|
|
client.clone(),
|
|
|
|
);
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2020-05-31 14:50:00 +00:00
|
|
|
// 7. deploy it with the constructor arguments
|
|
|
|
let contract = factory.deploy("initial value".to_string())?.send().await?;
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2020-05-31 14:50:00 +00:00
|
|
|
// 8. get the contract's address
|
|
|
|
let addr = contract.address();
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2020-05-31 14:50:00 +00:00
|
|
|
// 9. instantiate the contract
|
2020-06-22 08:44:08 +00:00
|
|
|
let contract = SimpleContract::new(addr, client.clone());
|
2020-05-25 15:35:38 +00:00
|
|
|
|
2020-05-31 14:50:00 +00:00
|
|
|
// 10. call the `setValue` method
|
2020-06-22 08:44:08 +00:00
|
|
|
let tx_hash = contract.set_value("hi".to_owned()).send().await?;
|
|
|
|
let _receipt = client.pending_transaction(tx_hash).await?;
|
2020-05-26 14:43:51 +00:00
|
|
|
|
2020-05-31 14:50:00 +00:00
|
|
|
// 11. get all events
|
2020-05-31 21:17:50 +00:00
|
|
|
let logs = contract
|
|
|
|
.value_changed_filter()
|
|
|
|
.from_block(0u64)
|
|
|
|
.query()
|
|
|
|
.await?;
|
2020-05-26 14:43:51 +00:00
|
|
|
|
2020-05-31 14:50:00 +00:00
|
|
|
// 12. get the new value
|
2020-05-26 14:43:51 +00:00
|
|
|
let value = contract.get_value().call().await?;
|
2020-05-25 15:35:38 +00:00
|
|
|
|
2020-05-26 14:43:51 +00:00
|
|
|
println!("Value: {}. Logs: {}", value, serde_json::to_string(&logs)?);
|
2020-05-25 15:35:38 +00:00
|
|
|
|
2020-05-25 10:05:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|