Skip to content

Commit

Permalink
Fix to edition 2018 (#206)
Browse files Browse the repository at this point in the history
* Fix fmt

* Remove error_chain in favor of simple errors

* Fix to edition 2018

* Update travis configuration
  • Loading branch information
zzau13 authored and tomusdrw committed Mar 27, 2019
1 parent 56ad08e commit 5503acb
Show file tree
Hide file tree
Showing 46 changed files with 833 additions and 1,465 deletions.
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ matrix:
- rust: nightly

before_script:
- rustup toolchain install stable
- rustup component add rustfmt-preview --toolchain=stable
- if [[ "${TRAVIS_RUST_VERSION}" == stable ]]; then
rustup component add rustfmt;
fi

script:
# - cargo +stable fmt --all -- --write-mode=diff
- if [[ "${TRAVIS_RUST_VERSION}" == "stable" ]]; then
cargo fmt -- --check;
fi
- cargo check
- cargo build
- cargo test

Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ license = "MIT"
keywords = ["jsonrpc", "web3", "ethereum", "rpc", "client"]
authors = ["Tomasz Drwięga <[email protected]>"]
readme = "README.md"
edition = "2018"

[dependencies]
arrayvec = "0.4"
error-chain = "0.12"
ethabi = "6.0"
ethereum-types = "0.4"
futures = "0.1"
Expand All @@ -26,6 +26,7 @@ serde_json = "1.0"
tokio-timer = "0.1"
url = "1.7"
base64 = "0.10"
derive_more = "0.14"
# Optional deps
hyper = { version = "0.12", optional = true }
hyper-tls = { version = "0.3", optional = true }
Expand Down
6 changes: 1 addition & 5 deletions examples/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ fn main() {
let mut event_loop = tokio_core::reactor::Core::new().unwrap();
let remote = event_loop.remote();

let http = web3::transports::Http::with_event_loop(
"http://localhost:8545",
&event_loop.handle(),
MAX_PARALLEL_REQUESTS,
).unwrap();
let http = web3::transports::Http::with_event_loop("http://localhost:8545", &event_loop.handle(), MAX_PARALLEL_REQUESTS).unwrap();

let web3 = web3::Web3::new(web3::transports::Batch::new(http));
let _ = web3.eth().accounts();
Expand Down
9 changes: 3 additions & 6 deletions examples/bench.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate parking_lot;
extern crate web3;

use std::{thread, time};
use std::sync::{atomic, Arc};
use parking_lot::Mutex;
use std::sync::{atomic, Arc};
use std::{thread, time};
use web3::futures::Future;

fn as_millis(dur: time::Duration) -> u64 {
Expand Down Expand Up @@ -45,10 +45,7 @@ impl Ticker {
let elapsed = as_millis(time.elapsed());
let result = reqs * 1_000 / elapsed;

println!(
"[{}] {} reqs/s ({} reqs in {} ms)",
self.id, result, reqs, elapsed
);
println!("[{}] {} reqs/s ({} reqs in {} ms)", self.id, result, reqs, elapsed);

self.reqs.store(0, atomic::Ordering::Release);
*time = time::Instant::now();
Expand Down
25 changes: 5 additions & 20 deletions examples/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ extern crate env_logger;
extern crate rustc_hex;
extern crate web3;

use web3::futures::Future;
use rustc_hex::FromHex;
use web3::contract::{Contract, Options};
use web3::futures::Future;
use web3::types::{Address, U256};
use rustc_hex::FromHex;

fn main() {
env_logger::init();
let (_eloop, http) = web3::transports::Http::new("http://localhost:8545").unwrap();
let web3 = web3::Web3::new(http);

let my_account: Address = "d028d24f16a8893bd078259d413372ac01580769"
.parse()
.unwrap();
let my_account: Address = "d028d24f16a8893bd078259d413372ac01580769".parse().unwrap();
// Get the contract bytecode for instance from Solidity compiler
let bytecode: Vec<u8> = include_str!("./contract_token.code").from_hex().unwrap();
// Deploying a contract
Expand All @@ -26,16 +24,7 @@ fn main() {
opt.gas_price = Some(5.into());
opt.gas = Some(1_000_000.into());
}))
.execute(
bytecode,
(
U256::from(1_000_000),
"My Token".to_owned(),
3u64,
"MT".to_owned(),
),
my_account,
)
.execute(bytecode, (U256::from(1_000_000), "My Token".to_owned(), 3u64, "MT".to_owned()), my_account)
.expect("Correct parameters are passed to the constructor.")
.wait()
.unwrap();
Expand All @@ -46,11 +35,7 @@ fn main() {

// Accessing existing contract
let contract_address = contract.address();
let contract = Contract::from_json(
web3.eth(),
contract_address,
include_bytes!("../src/contract/res/token.json"),
).unwrap();
let contract = Contract::from_json(web3.eth(), contract_address, include_bytes!("../src/contract/res/token.json")).unwrap();

let result = contract.query("balanceOf", (my_account,), None, Options::default(), None);
let balance_of: U256 = result.wait().unwrap();
Expand Down
8 changes: 1 addition & 7 deletions examples/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ const MAX_PARALLEL_REQUESTS: usize = 64;
fn main() {
let mut event_loop = tokio_core::reactor::Core::new().unwrap();

let web3 = web3::Web3::new(
web3::transports::Http::with_event_loop(
"http://localhost:8545",
&event_loop.handle(),
MAX_PARALLEL_REQUESTS,
).unwrap(),
);
let web3 = web3::Web3::new(web3::transports::Http::with_event_loop("http://localhost:8545", &event_loop.handle(), MAX_PARALLEL_REQUESTS).unwrap());
let accounts = web3.eth().accounts().map(|accounts| {
println!("Accounts: {:?}", accounts);
});
Expand Down
65 changes: 21 additions & 44 deletions examples/simple_log_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ extern crate rustc_hex;
extern crate tokio_core;
extern crate web3;

use std::time;
use rustc_hex::FromHex;
use std::time;
use web3::contract::{Contract, Options};
use web3::futures::{Future, Stream};
use web3::types::FilterBuilder;
Expand All @@ -20,54 +20,31 @@ fn main() {
let accounts = accounts.unwrap();
println!("accounts: {:?}", &accounts);

Contract::deploy(web3.eth(), include_bytes!("./build/SimpleEvent.abi"))
.unwrap()
.confirmations(1)
.poll_interval(time::Duration::from_secs(10))
.options(Options::with(|opt| {
opt.gas = Some(3_000_000.into())
}))
.execute(bytecode, (), accounts[0])
.unwrap()
.then(move |contract| {
let contract = contract.unwrap();
println!("contract deployed at: {}", contract.address());
Contract::deploy(web3.eth(), include_bytes!("./build/SimpleEvent.abi")).unwrap().confirmations(1).poll_interval(time::Duration::from_secs(10)).options(Options::with(|opt| opt.gas = Some(3_000_000.into()))).execute(bytecode, (), accounts[0]).unwrap().then(move |contract| {
let contract = contract.unwrap();
println!("contract deployed at: {}", contract.address());

// Filter for Hello event in our contract
let filter = FilterBuilder::default()
.address(vec![contract.address()])
.topics(
Some(vec![
"0xd282f389399565f3671145f5916e51652b60eee8e5c759293a2f5771b8ddfd2e".into(),
]),
None,
None,
None,
)
.build();
// Filter for Hello event in our contract
let filter = FilterBuilder::default().address(vec![contract.address()]).topics(Some(vec!["0xd282f389399565f3671145f5916e51652b60eee8e5c759293a2f5771b8ddfd2e".into()]), None, None, None).build();

let event_future = web3.eth_filter()
.create_logs_filter(filter)
.then(|filter| {
filter
.unwrap()
.stream(time::Duration::from_secs(0))
.for_each(|log| {
println!("got log: {:?}", log);
Ok(())
})
let event_future = web3
.eth_filter()
.create_logs_filter(filter)
.then(|filter| {
filter.unwrap().stream(time::Duration::from_secs(0)).for_each(|log| {
println!("got log: {:?}", log);
Ok(())
})
.map_err(|_| ());
})
.map_err(|_| ());

let call_future = contract
.call("hello", (), accounts[0], Options::default())
.then(|tx| {
println!("got tx: {:?}", tx);
Ok(())
});
let call_future = contract.call("hello", (), accounts[0], Options::default()).then(|tx| {
println!("got tx: {:?}", tx);
Ok(())
});

event_future.join(call_future)
})
event_future.join(call_future)
})
}))
.unwrap();
}
62 changes: 21 additions & 41 deletions examples/simple_log_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ extern crate rustc_hex;
extern crate tokio_core;
extern crate web3;

use std::time;
use rustc_hex::FromHex;
use std::time;
use web3::contract::{Contract, Options};
use web3::futures::{Future, Stream};
use web3::types::FilterBuilder;
Expand All @@ -20,51 +20,31 @@ fn main() {
let accounts = accounts.unwrap();
println!("accounts: {:?}", &accounts);

Contract::deploy(web3.eth(), include_bytes!("./build/SimpleEvent.abi"))
.unwrap()
.confirmations(1)
.poll_interval(time::Duration::from_secs(10))
.options(Options::with(|opt| {
opt.gas = Some(3_000_000.into())
}))
.execute(bytecode, (), accounts[0])
.unwrap()
.then(move |contract| {
let contract = contract.unwrap();
println!("contract deployed at: {}", contract.address());
Contract::deploy(web3.eth(), include_bytes!("./build/SimpleEvent.abi")).unwrap().confirmations(1).poll_interval(time::Duration::from_secs(10)).options(Options::with(|opt| opt.gas = Some(3_000_000.into()))).execute(bytecode, (), accounts[0]).unwrap().then(move |contract| {
let contract = contract.unwrap();
println!("contract deployed at: {}", contract.address());

// Filter for Hello event in our contract
let filter = FilterBuilder::default()
.address(vec![contract.address()])
.topics(
Some(vec![
"0xd282f389399565f3671145f5916e51652b60eee8e5c759293a2f5771b8ddfd2e".into(),
]),
None,
None,
None,
)
.build();
// Filter for Hello event in our contract
let filter = FilterBuilder::default().address(vec![contract.address()]).topics(Some(vec!["0xd282f389399565f3671145f5916e51652b60eee8e5c759293a2f5771b8ddfd2e".into()]), None, None, None).build();

let event_future = web3.eth_subscribe()
.subscribe_logs(filter)
.then(|sub| {
sub.unwrap().for_each(|log| {
println!("got log: {:?}", log);
Ok(())
})
let event_future = web3
.eth_subscribe()
.subscribe_logs(filter)
.then(|sub| {
sub.unwrap().for_each(|log| {
println!("got log: {:?}", log);
Ok(())
})
.map_err(|_| ());
})
.map_err(|_| ());

let call_future = contract
.call("hello", (), accounts[0], Options::default())
.then(|tx| {
println!("got tx: {:?}", tx);
Ok(())
});
let call_future = contract.call("hello", (), accounts[0], Options::default()).then(|tx| {
println!("got tx: {:?}", tx);
Ok(())
});

event_future.join(call_future)
})
event_future.join(call_future)
})
}))
.unwrap();
}
22 changes: 5 additions & 17 deletions examples/simple_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
extern crate rustc_hex;
extern crate web3;

use web3::futures::Future;
use web3::contract::{Contract, Options};
use web3::types::U256;
use rustc_hex::FromHex;
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();
Expand All @@ -19,21 +19,9 @@ fn main() {
println!("Balance: {}", balance);

// Get the contract bytecode for instance from Solidity compiler
let bytecode: Vec<u8> = include_str!("./build/SimpleStorage.bin")
.from_hex()
.unwrap();
let bytecode: Vec<u8> = include_str!("./build/SimpleStorage.bin").from_hex().unwrap();
// 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();
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());

Expand Down
Loading

0 comments on commit 5503acb

Please sign in to comment.