baffle/
deploy.rs

1extern crate web3;
2
3use std::{fs, time};
4use std::path::Path;
5use web3::contract::{Contract, Options};
6use web3::futures::Future;
7use web3::types::Address;
8use web3::Web3;
9
10pub struct ContractArtifact {
11    abi: Vec<u8>,
12    bin: String
13}
14
15fn get_artifact_for_files(abi_file: &str, bin_file: &str) -> ContractArtifact {
16    ContractArtifact {
17        abi: fs::read(abi_file).unwrap(),
18        bin: fs::read_to_string(bin_file).unwrap()
19    }
20}
21
22pub fn get_artifact(build_path: &Path, contract_name: &str) -> ContractArtifact {
23    let abi_path = build_path.join(format!("{}.abi", contract_name));
24    let bin_path = build_path.join(format!("{}.bin", contract_name));
25    get_artifact_for_files(abi_path.to_str().unwrap(), bin_path.to_str().unwrap())
26}
27
28pub fn deploy_contract<T: web3::Transport>(artifact: &ContractArtifact, web3: &Web3<T>, from_address: Address) -> Contract<T> {
29    return Contract::deploy(web3.eth(), &artifact.abi)
30        .unwrap()
31        .confirmations(0)
32        .poll_interval(time::Duration::from_secs(10))
33        .options(Options::with(|opt| opt.gas = Some(3_000_000.into())))
34        .execute(&artifact.bin, (), from_address)
35        .unwrap()
36        .wait()
37        .unwrap()
38}
39
40pub fn make_web3(rpc_url: &str) -> (web3::transports::EventLoopHandle, Web3<web3::transports::Http>) {
41    let (_eloop, transport) = web3::transports::Http::new(rpc_url).unwrap();
42    (_eloop, web3::Web3::new(transport))
43}
44
45pub fn make_web3_ganache() -> (web3::transports::EventLoopHandle, Web3<web3::transports::Http>) {
46    make_web3("http://localhost:8545")
47}