Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ethereum-types library for uints and hashes #76

Merged
merged 6 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "web3"
version = "0.1.0"
version = "0.2.0"
description = "Ethereum JSON-RPC client."
homepage = "https://github.com/tomusdrw/rust-web3"
repository = "https://github.com/tomusdrw/rust-web3"
Expand All @@ -11,17 +11,18 @@ authors = ["Tomasz Drwięga <tomasz@ethcore.io>"]

[dependencies]
arrayvec = "0.3"
error-chain = "0.11.0"
ethabi = "4.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please bump ethabi to 5.1

ethereum-types = "0.2.3"
futures = "0.1"
jsonrpc-core = "7.0"
jsonrpc-core = "7.1"
log = "0.3"
parking_lot = "0.4"
rustc-serialize = "0.3"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
tokio-timer = "0.1"
error-chain = "0.11.0-rc.2"
# Optional deps
hyper = { version = "0.11", optional = true }
tokio-core = { version = "0.1", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion examples/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
// Deploying a contract
let contract = Contract::deploy(web3.eth(), include_bytes!("../src/contract/res/token.json")).unwrap()
.confirmations(4)
.options(Options::with(|mut opt| opt.gas = Some(5_000_000.into())))
.options(Options::with(|opt| opt.value = Some(5.into())))
.execute(bytecode, (
U256::from(1_000_000),
"My Token".to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion src/contract/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod tests {

// when
builder
.options(Options::with(|mut opt| opt.value = Some(5.into())))
.options(Options::with(|opt| opt.value = Some(5.into())))
.confirmations(1)
.execute(vec![1, 2, 3, 4], (U256::from(1_000_000), "My Token".to_owned(), 3u64, "MT".to_owned()), 5.into())
.unwrap()
Expand Down
6 changes: 3 additions & 3 deletions src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
let token = contract(&transport);

// when
token.query("name", (), Address::from(5), Options::with(|mut options| {
token.query("name", (), Address::from(5), Options::with(|options| {
options.gas_price = Some(10_000_000.into());
}), BlockNumber::Latest).wait().unwrap()
};
Expand All @@ -214,7 +214,7 @@ mod tests {
fn should_call_a_contract_function() {
// given
let mut transport = TestTransport::default();
transport.set_response(rpc::Value::String(format!("{:?}", H256::from(5))));
transport.set_response(rpc::Value::String(format!("0x{:?}", H256::from(5))));

let result = {
let token = contract(&transport);
Expand All @@ -235,7 +235,7 @@ mod tests {
fn should_estimate_gas_usage() {
// given
let mut transport = TestTransport::default();
transport.set_response(rpc::Value::String(format!("{:?}", U256::from(5))));
transport.set_response(rpc::Value::String(format!("0x{:?}", U256::from(5))));

let result = {
let token = contract(&transport);
Expand Down
18 changes: 10 additions & 8 deletions src/contract/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use arrayvec::ArrayVec;
use ethabi::Token;
use contract::error::{Error, ErrorKind};
use types::{self, Address, H256, U256, U64};
use types::{Address, H256, U256, U128};

/// Output type possible to deserialize from Contract ABI
pub trait Detokenize {
Expand Down Expand Up @@ -132,7 +132,7 @@ impl Tokenizable for H256 {
for (idx, val) in s.drain(..).enumerate() {
data[idx] = val;
}
Ok(H256(data))
Ok(data.into())
},
other => Err(ErrorKind::InvalidOutputType(format!("Expected `H256`, got {:?}", other)).into()),
}
Expand All @@ -147,13 +147,13 @@ impl Tokenizable for H256 {
impl Tokenizable for Address {
fn from_token(token: Token) -> Result<Self, Error> {
match token {
Token::Address(data) => Ok(types::H160(data)),
Token::Address(data) => Ok(data.into()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in ethabi 5.1 data is already an Address, there is no need to call .into(), docs

other => Err(ErrorKind::InvalidOutputType(format!("Expected `Address`, got {:?}", other)).into()),
}
}

fn into_token(self) -> Token {
Token::Address(self.0)
Token::Address(self.into())
}
}

Expand All @@ -168,15 +168,17 @@ macro_rules! uint_tokenizable {
}

fn into_token(self) -> Token {
let u = U256::from(self.0.as_ref());
Token::Uint(u.0)
let mut arr = [0; 32];
self.to_big_endian(&mut arr);
let u = U256::from(&arr);
Token::Uint(u.into())
}
}
}
}

uint_tokenizable!(U256, "U256");
uint_tokenizable!(U64, "U64");
uint_tokenizable!(U128, "U128");

impl Tokenizable for u64 {
fn from_token(token: Token) -> Result<Self, Error> {
Expand All @@ -188,7 +190,7 @@ impl Tokenizable for u64 {

fn into_token(self) -> Token {
let u = U256::from(self);
Token::Uint(u.0)
Token::Uint(u.into())
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

extern crate arrayvec;
extern crate ethabi;
extern crate ethereum_types;
extern crate jsonrpc_core as rpc;
extern crate parking_lot;
extern crate rustc_serialize;
Expand Down
3 changes: 3 additions & 0 deletions src/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ impl Http {
Ok(Http {
id: Default::default(),
url: url.parse()?,
// TODO [ToDr] This may lock down the event loop
// if the queue is full and you send another request
// from the event loop.
write_sender: Mutex::new(write_sender.wait()),
})
}
Expand Down
4 changes: 3 additions & 1 deletion src/transports/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ impl Ipc {

Ok(Ipc {
id: atomic::AtomicUsize::new(1),
// TODO [ToDr] This may lock down the event loop
// if the queue is full and you send another request
// from the event loop.
write_sender: Mutex::new(write_sender.wait()),
pending,
})
Expand All @@ -102,7 +105,6 @@ impl Ipc {
{
let request = helpers::to_string(&request);
debug!("[{}] Calling: {}", id, request);

let (tx, rx) = futures::oneshot();
self.pending.lock().insert(id, tx);
let result = {
Expand Down
4 changes: 2 additions & 2 deletions src/types/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Serialize, Serializer};
use types::{Bytes, U64, U256, H256, H160, H2048};
use types::{Bytes, U128, U256, H256, H160, H2048};

/// The block type returned from RPC calls.
/// This is generic over a `TX` type.
Expand All @@ -26,7 +26,7 @@ pub struct Block<TX> {
#[serde(rename="receiptsRoot")]
pub receipts_root: H256,
/// Block number. None if pending.
pub number: Option<U64>,
pub number: Option<U128>,
/// Gas Used
#[serde(rename="gasUsed")]
pub gas_used: U256,
Expand Down
4 changes: 2 additions & 2 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub use self::log::{Log, Filter, FilterBuilder};
pub use self::transaction::{Transaction, Receipt as TransactionReceipt};
pub use self::transaction_id::TransactionId;
pub use self::transaction_request::{TransactionRequest, CallRequest, TransactionCondition};
pub use self::uint::{H64, H128, H160, H256, H512, H520, H2048, U64, U256};
pub use self::uint::{H64, H128, H160, H256, H512, H520, H2048, U128, U256};
pub use self::work::Work;

/// Address
pub type Address = H160;
/// Index in block
pub type Index = U64;
pub type Index = U128;
Loading