2020-05-26 11:00:56 +00:00
|
|
|
use anyhow::Result;
|
2020-05-26 18:57:59 +00:00
|
|
|
use ethers::{providers::HttpProvider, signers::MainnetWallet, types::Address};
|
2020-05-25 10:05:00 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2020-05-26 18:57:59 +00:00
|
|
|
use ethers::contract::abigen;
|
2020-05-26 14:43:51 +00:00
|
|
|
|
2020-05-26 18:57:59 +00:00
|
|
|
// Generate the contract code
|
|
|
|
abigen!(
|
|
|
|
SimpleContract,
|
|
|
|
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","constant": true, "type":"function"},{"inputs":[{"internalType":"string","name":"value","type":"string"}],"name":"setValue","outputs":[],"stateMutability":"nonpayable","type":"function"}]"#,
|
|
|
|
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-25 10:05:00 +00:00
|
|
|
// connect to the network
|
|
|
|
let provider = HttpProvider::try_from("http://localhost:8545")?;
|
|
|
|
|
|
|
|
// create a wallet and connect it to the provider
|
2020-05-26 14:43:51 +00:00
|
|
|
let client = "ea878d94d9b1ffc78b45fc7bfc72ec3d1ce6e51e80c8e376c3f7c9a861f7c214"
|
2020-05-25 10:05:00 +00:00
|
|
|
.parse::<MainnetWallet>()?
|
|
|
|
.connect(&provider);
|
|
|
|
|
|
|
|
// Contract should take both provider or a signer
|
|
|
|
|
2020-05-25 15:35:38 +00:00
|
|
|
// get the contract's address
|
2020-05-26 14:43:51 +00:00
|
|
|
let addr = "ebBe15d9C365fC8a04a82E06644d6B39aF20cC31".parse::<Address>()?;
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2020-05-25 15:35:38 +00:00
|
|
|
// instantiate it
|
2020-05-26 14:43:51 +00:00
|
|
|
let contract = SimpleContract::new(addr, &client);
|
2020-05-25 15:35:38 +00:00
|
|
|
|
|
|
|
// call the method
|
2020-05-26 18:57:59 +00:00
|
|
|
let _tx_hash = contract.set_value("hi".to_owned()).send().await?;
|
2020-05-26 14:43:51 +00:00
|
|
|
|
|
|
|
let logs = contract.value_changed().from_block(0u64).query().await?;
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|