forked from tomusdrw/rust-web3
-
Notifications
You must be signed in to change notification settings - Fork 27
/
simple_storage.rs
47 lines (38 loc) · 1.52 KB
/
simple_storage.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//based on examples/contract.rs
extern crate rustc_hex;
extern crate web3;
use std::time;
use web3::contract::{Contract, Options};
use web3::futures::Future;
use web3::types::U256;
fn main() {
let (_eloop, transport) = web3::transports::Http::new("http://localhost:8545").unwrap();
let web3 = web3::Web3::new(transport);
let accounts = web3.eth().accounts().wait().unwrap();
//Get current balance
let balance = web3.eth().balance(accounts[0], None).wait().unwrap();
println!("Balance: {}", balance);
// Get the contract bytecode for instance from Solidity compiler
let bytecode = include_str!("./build/SimpleStorage.bin");
// Deploying a contract
let contract = Contract::deploy(web3.eth(), include_bytes!("./build/SimpleStorage.abi"))
.unwrap()
.confirmations(0)
.poll_interval(time::Duration::from_secs(10))
.options(Options::with(|opt| opt.gas = Some(3_000_000.into())))
.execute(bytecode, (), accounts[0])
.unwrap()
.wait()
.unwrap();
println!("{}", contract.address());
//interact with the contract
let result = contract.query("get", (), None, Options::default(), None);
let storage: U256 = result.wait().unwrap();
println!("{}", storage);
//Change state of the contract
contract.call("set", (42,), accounts[0], Options::default());
//View changes made
let result = contract.query("get", (), None, Options::default(), None);
let storage: U256 = result.wait().unwrap();
println!("{}", storage);
}