Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Refactor code to use execute_no_unlock
Browse files Browse the repository at this point in the history
Introduced in tomusdrw/rust-web3#252.
  • Loading branch information
rockbmb committed Sep 8, 2019
1 parent 0006f93 commit da0a419
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 64 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ web3 = "0.8.0"
pwasm-utils-cli = "0.10.0"

[patch.crates-io]
# We require the patch https://github.com/tomusdrw/rust-web3/pull/242.
# We require the patch https://github.com/tomusdrw/rust-web3/pull/252.
# Once a new version of web3 is released we can update it.
web3 = { git = "https://github.com/tomusdrw/rust-web3.git", rev = "b2aa7336bc9bf192f7959e79525a6d2667ff4c85" }
web3 = { git = "https://github.com/rockbmb/rust-web3.git", rev = "7cb9e684e9c454ca53c0447894ecce71a97e1294" }
# See https://github.com/paritytech/wasm-utils/pull/132
pwasm-utils-cli = { git = "https://github.com/oscoin/wasm-utils.git", branch = "pack-min-pages" }
# See https://github.com/oscoin/oscoin-parity-wasm-prototype/pull/45
Expand Down
2 changes: 1 addition & 1 deletion deploy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Thomas Scholtes <thomas@monadic.xyz>"]
edition = "2018"

[dependencies]
web3 = { git = "https://github.com/tomusdrw/rust-web3.git", rev = "b2aa7336bc9bf192f7959e79525a6d2667ff4c85" }
web3 = "0.8.0"
env_logger = "0.6.2"
hex = "0.3.1"
clap = "2.31"
Expand Down
62 changes: 6 additions & 56 deletions deploy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@
//! let contract = oscoin_deploy::deploy().unwrap();
//! oscoin_deploy::write_contract_address(&contract.address());
//! ```
use ethabi;
use rustc_hex::FromHex;
use std::{fs, time};
use std::collections::HashMap;
use web3::api::Namespace;
use web3::confirm;
use web3::contract::deploy::{Builder, PendingContract};
use std::fs;
use web3::contract::{Contract, Options};
use web3::futures::Future;
use web3::types::{Address, Bytes, TransactionRequest};
use web3::types::Address;
use web3::Web3;

/// Maximum gas used to deploy the contract
Expand Down Expand Up @@ -51,60 +45,16 @@ pub fn deploy() -> Result<Contract<web3::transports::Http>, String> {
let contract_code = fs::read(CONTRACT_CODE_PATH)
.map_err(|e| format!("Failed to read {}: {}", CONTRACT_CODE_PATH, e))?;

let opts = Options::with(|opt| {
opt.gas = Some(DEPLOY_GAS.into());
});

let eth_ = web3.eth();
let abi_ = ethabi::Contract::load(CONTRACT_ABI)
.expect("Contract is successfully loaded");
let builder : Builder<web3::transports::http::Http> = Contract::deploy(web3.eth(), CONTRACT_ABI)
let builder = Contract::deploy(web3.eth(), CONTRACT_ABI)
.expect("contract ABI is hardcoded and valid")
.confirmations(0)
.options(Options::with(|opt| {
opt.gas = Some(DEPLOY_GAS.into());
}));

let code_hex = hex::encode(contract_code);

// This is to fix truffle + serde_json redundant `"` and `0x`.
// Notice the intentional variable shadowing.
let code_hex = code_hex.replace("\"", "").replace("0x", "");

let code = code_hex.from_hex().map_err(ethabi::ErrorKind::Hex).unwrap();

let tx_request = TransactionRequest {
from: dev_account_address(),
to: None,
gas: opts.gas,
gas_price: opts.gas_price,
value: opts.value,
nonce: opts.nonce,
data: Some(Bytes(code)),
condition: opts.condition,
};

let poll_interval = time::Duration::from_secs(7);
let confirmations = 1;

let signed_tx = web3
.personal()
.sign_transaction(tx_request, "")
.wait()
.expect("Transaction signing failed");

let sent_raw_tx = confirm::send_raw_transaction_with_confirmation(
eth_.transport().clone(),
signed_tx.raw,
poll_interval,
confirmations,
);

let pending_contract = PendingContract {
Some(eth_),
Some(abi_),
sent_raw_tx,
};
let pending_contract = builder
.execute_no_unlock(hex::encode(contract_code), (), dev_account_address(), "", web3)
.expect("Correct parameters are passed to the constructor.");

let contract = pending_contract
.wait()
Expand Down

0 comments on commit da0a419

Please sign in to comment.