refactor ethers-abi to be part of ethers-types

This commit is contained in:
Georgios Konstantopoulos 2020-05-28 12:04:12 +03:00
parent a78979a3ac
commit bc523b6d60
No known key found for this signature in database
GPG Key ID: FA607837CD26EDBC
32 changed files with 99 additions and 112 deletions

20
Cargo.lock generated
View File

@ -229,7 +229,6 @@ name = "ethers"
version = "0.1.0"
dependencies = [
"anyhow 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)",
"ethers-abi 0.1.0",
"ethers-contract 0.1.0",
"ethers-providers 0.1.0",
"ethers-signers 0.1.0",
@ -241,23 +240,10 @@ dependencies = [
"tokio 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ethers-abi"
version = "0.1.0"
dependencies = [
"arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ethabi 12.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethers-types 0.1.0",
"ethers-utils 0.1.0",
"serde_json 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
"thiserror 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ethers-contract"
version = "0.1.0"
dependencies = [
"ethers-abi 0.1.0",
"ethers-contract-abigen 0.1.0",
"ethers-contract-derive 0.1.0",
"ethers-providers 0.1.0",
@ -276,7 +262,6 @@ dependencies = [
"Inflector 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)",
"anyhow 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)",
"curl 0.4.29 (registry+https://github.com/rust-lang/crates.io-index)",
"ethers-abi 0.1.0",
"ethers-types 0.1.0",
"once_cell 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
@ -291,8 +276,8 @@ dependencies = [
name = "ethers-contract-derive"
version = "0.1.0"
dependencies = [
"ethers-abi 0.1.0",
"ethers-contract-abigen 0.1.0",
"ethers-types 0.1.0",
"proc-macro2 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
@ -304,7 +289,6 @@ name = "ethers-providers"
version = "0.1.0"
dependencies = [
"async-trait 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
"ethers-abi 0.1.0",
"ethers-types 0.1.0",
"ethers-utils 0.1.0",
"reqwest 0.10.4 (registry+https://github.com/rust-lang/crates.io-index)",
@ -329,7 +313,9 @@ dependencies = [
name = "ethers-types"
version = "0.1.0"
dependencies = [
"arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"bincode 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ethabi 12.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethereum-types 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
"ethers-utils 0.1.0",
"rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -2,7 +2,6 @@
members = [
"./crates/ethers",
"./crates/ethers-abi",
"./crates/ethers-contract",
"./crates/ethers-providers",
"./crates/ethers-signers",

View File

@ -1,16 +0,0 @@
[package]
name = "ethers-abi"
version = "0.1.0"
authors = ["Georgios Konstantopoulos <me@gakonst.com>"]
edition = "2018"
[dependencies]
ethers-types = { version = "0.1.0", path = "../ethers-types" }
ethers-utils = { version = "0.1.0", path = "../ethers-utils" }
ethabi = "12.0.0"
arrayvec = "0.5.1"
thiserror = { version = "1.0.19", default-features = false }
[dev-dependencies]
serde_json = { version = "1.0.53", default-features = false }

View File

@ -8,7 +8,6 @@ edition = "2018"
ethers-contract-abigen = { version = "0.1.0", path = "ethers-contract-abigen", optional = true }
ethers-contract-derive = { version = "0.1.0", path = "ethers-contract-derive", optional = true }
ethers-abi = { version = "0.1.0", path = "../ethers-abi" }
ethers-providers = { version = "0.1.0", path = "../ethers-providers" }
ethers-signers = { version = "0.1.0", path = "../ethers-signers" }
ethers-types = { version = "0.1.0", path = "../ethers-types" }

View File

@ -7,7 +7,6 @@ license = "MIT OR Apache-2.0"
description = "Code generation for type-safe bindings to Ethereum smart contracts"
[dependencies]
ethers-abi = { path = "../../ethers-abi" }
ethers-types = { path = "../../ethers-types" }
anyhow = "1.0"

View File

@ -12,8 +12,7 @@ mod types;
use super::util;
use super::Args;
use anyhow::{anyhow, Context as _, Result};
use ethers_abi::Abi;
use ethers_types::Address;
use ethers_types::{abi::Abi, Address};
use inflector::Inflector;
use proc_macro2::{Ident, Literal, TokenStream};
use quote::quote;

View File

@ -1,5 +1,5 @@
use super::{types, util, Context};
use ethers_abi::{Event, EventExt, EventParam, Hash, ParamType};
use ethers_types::abi::{Event, EventExt, EventParam, Hash, ParamType};
use anyhow::Result;
use inflector::Inflector;
@ -286,7 +286,7 @@ fn expand_hash(hash: Hash) -> TokenStream {
#[cfg(test)]
mod tests {
use super::*;
use ethers_abi::{EventParam, ParamType};
use ethers_types::abi::{EventParam, ParamType};
#[test]
fn expand_transfer_filter() {

View File

@ -1,7 +1,8 @@
use super::{types, util, Context};
use ethers_abi::{Function, FunctionExt, Param};
use ethers_types::Selector;
use ethers_types::{
abi::{Function, FunctionExt, Param},
Selector,
};
use anyhow::{anyhow, Context as _, Result};
use inflector::Inflector;
@ -106,7 +107,7 @@ fn expand_selector(selector: Selector) -> TokenStream {
#[cfg(test)]
mod tests {
use super::*;
use ethers_abi::ParamType;
use ethers_types::abi::ParamType;
#[test]
fn expand_inputs_empty() {

View File

@ -1,5 +1,5 @@
use anyhow::{anyhow, Result};
use ethers_abi::ParamType;
use ethers_types::abi::ParamType;
use proc_macro2::{Literal, TokenStream};
use quote::quote;

View File

@ -10,7 +10,7 @@ description = "Proc macro for type-safe bindings generation to Ethereum smart co
proc-macro = true
[dependencies]
ethers-abi = { path = "../../ethers-abi" }
ethers-types = { path = "../../ethers-types" }
ethers-contract-abigen = { path = "../ethers-contract-abigen" }
serde_json = "1.0.53"

View File

@ -2,8 +2,8 @@
//! ethereum smart contract.
use crate::spanned::{ParseInner, Spanned};
use ethers_abi::{Function, FunctionExt, Param};
use ethers_contract_abigen::Builder;
use ethers_types::abi::{Function, FunctionExt, Param};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens as _};

View File

@ -1,7 +1,9 @@
use ethers_abi::{Detokenize, Function};
use ethers_providers::{networks::Network, JsonRpcClient};
use ethers_signers::{Client, Signer};
use ethers_types::{Address, BlockNumber, TransactionRequest, H256, U256};
use ethers_types::{
abi::{Detokenize, Error as AbiError, Function, InvalidOutputType},
Address, BlockNumber, TransactionRequest, H256, U256,
};
use std::{fmt::Debug, marker::PhantomData};
@ -48,9 +50,9 @@ where
P::Error: 'static,
{
#[error(transparent)]
DecodingError(#[from] ethers_abi::Error),
DecodingError(#[from] AbiError),
#[error(transparent)]
DetokenizationError(#[from] ethers_abi::InvalidOutputType),
DetokenizationError(#[from] InvalidOutputType),
#[error(transparent)]
CallError(P::Error),
}

View File

@ -1,9 +1,11 @@
use crate::{ContractCall, Event};
use ethers_abi::{Abi, Detokenize, Error, EventExt, Function, FunctionExt, Tokenize};
use ethers_providers::{networks::Network, JsonRpcClient};
use ethers_signers::{Client, Signer};
use ethers_types::{Address, Filter, NameOrAddress, Selector, TransactionRequest};
use ethers_types::{
abi::{Abi, Detokenize, Error, EventExt, Function, FunctionExt, Tokenize},
Address, Filter, NameOrAddress, Selector, TransactionRequest,
};
use rustc_hex::ToHex;
use std::{collections::HashMap, fmt::Debug, hash::Hash, marker::PhantomData};

View File

@ -1,9 +1,11 @@
use crate::ContractError;
use ethers_abi::{Detokenize, Event as AbiEvent, RawLog};
use ethers_providers::{networks::Network, JsonRpcClient, Provider};
use ethers_types::{BlockNumber, Filter, ValueOrArray, H256};
use ethers_types::{
abi::{Detokenize, Event as AbiEvent, RawLog},
BlockNumber, Filter, ValueOrArray, H256,
};
use std::marker::PhantomData;

View File

@ -14,8 +14,8 @@ pub use ethers_contract_abigen::Builder;
pub use ethers_contract_derive::abigen;
// re-export for convenience
pub use ethers_abi as abi;
pub use ethers_providers as providers;
pub use ethers_signers as signers;
pub use ethers_types as types;
pub use ethers_types::abi;
pub use once_cell::sync::Lazy;

View File

@ -7,7 +7,6 @@ edition = "2018"
[dependencies]
ethers-types = { version = "0.1.0", path = "../ethers-types" }
ethers-utils = { version = "0.1.0", path = "../ethers-utils" }
ethers-abi = { version = "0.1.0", path = "../ethers-abi" }
async-trait = { version = "0.1.31", default-features = false }
reqwest = { version = "0.10.4", default-features = false, features = ["json", "rustls-tls"] }

View File

@ -1,7 +1,7 @@
use crate::{ens, http::Provider as HttpProvider, networks::Network, JsonRpcClient};
use ethers_abi::{Detokenize, ParamType};
use ethers_types::{
abi::{self, Detokenize, ParamType},
Address, Block, BlockId, BlockNumber, Bytes, Filter, Log, NameOrAddress, Selector, Transaction,
TransactionReceipt, TransactionRequest, TxHash, U256,
};
@ -256,8 +256,8 @@ impl<P: JsonRpcClient, N: Network> Provider<P, N> {
///
/// If the provided bytes were not an interpretation of an address
fn decode_bytes<T: Detokenize>(param: ParamType, bytes: Bytes) -> T {
let tokens = ethers_abi::decode(&[param], &bytes.0)
.expect("could not abi-decode bytes to address tokens");
let tokens =
abi::decode(&[param], &bytes.0).expect("could not abi-decode bytes to address tokens");
T::from_tokens(tokens).expect("could not parse tokens as address")
}

View File

@ -7,18 +7,27 @@ edition = "2018"
[dependencies]
ethers-utils = { version = "0.1.0", path = "../ethers-utils" }
# ethereum related
ethereum-types = { version = "0.9.2", default-features = false, features = ["serialize"] }
serde = { version = "1.0.110", default-features = false, features = ["derive"] }
rlp = { version = "0.4.5", default-features = false }
rustc-hex = { version = "2.1.0", default-features = false }
thiserror = { version = "1.0.19", default-features = false }
ethabi = { version = "12.0.0", default-features = false, optional = true }
# crypto
secp256k1 = { version = "0.17.2", default-features = false, features = ["std", "recovery", "rand"] }
rand = { version = "0.5.1", default-features = false } # this should be the same rand crate version as the one in secp
zeroize = { version = "1.1.0", default-features = false }
# misc
serde = { version = "1.0.110", default-features = false, features = ["derive"] }
rustc-hex = { version = "2.1.0", default-features = false }
thiserror = { version = "1.0.19", default-features = false }
arrayvec = { version = "0.5.1", default-features = false, optional = true }
[dev-dependencies]
serde_json = { version = "1.0.53", default-features = false }
rand = { version = "0.5.1" }
bincode = "1.2.1"
[features]
default = ["abi"]
abi = ["ethabi", "arrayvec"]

View File

@ -1,6 +1,6 @@
//! This module implements extensions to the `ethabi` API.
//! Taken from: https://github.com/gnosis/ethcontract-rs/blob/master/common/src/abiext.rs
use ethers_types::Selector;
use crate::Selector;
use ethers_utils::id;
pub use ethabi::Contract as Abi;

View File

@ -1,10 +1,9 @@
//! Contract Functions Output types.
//! Adapted from: https://github.com/tomusdrw/rust-web3/blob/master/src/contract/tokens.rs
#![allow(clippy::all)]
use crate::Token;
use crate::{abi::Token, Address, Bytes, H256, U128, U256};
use arrayvec::ArrayVec;
use ethers_types::{Address, Bytes, H256, U128, U256};
use thiserror::Error;
#[derive(Clone, Debug, Error)]
@ -470,8 +469,8 @@ impl_fixed_types!(1024);
#[cfg(test)]
mod tests {
use super::{Detokenize, Tokenizable};
use crate::{Address, U256};
use ethabi::Token;
use ethers_types::{Address, U256};
fn output<R: Detokenize>() -> R {
unimplemented!()

View File

@ -1,3 +1,4 @@
// Taken from https://github.com/tomusdrw/rust-web3/blob/master/src/types/block.rs
use crate::{Address, Bloom, Bytes, H256, U256, U64};
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
@ -63,8 +64,13 @@ pub struct Block<TX> {
}
#[derive(Copy, Clone, Debug, PartialEq)]
/// A Block Hash or Block Number
pub enum BlockId {
// TODO: May want to expand this to include the requireCanonical field
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1898.md
/// A block hash
Hash(H256),
/// A block number
Number(BlockNumber),
}
@ -102,7 +108,7 @@ impl Serialize for BlockId {
}
}
/// Block Number
/// A block Number (or tag - "latest", "earliest", "pending")
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum BlockNumber {
/// Latest block

View File

@ -0,0 +1,27 @@
pub type Selector = [u8; 4];
// Re-export common ethereum datatypes with more specific names
pub use ethereum_types::H256 as TxHash;
pub use ethereum_types::{Address, Bloom, H160, H256, U128, U256, U64};
mod transaction;
pub use transaction::{Overrides, Transaction, TransactionReceipt, TransactionRequest};
mod bytes;
pub use bytes::Bytes;
mod block;
pub use block::{Block, BlockId, BlockNumber};
mod log;
pub use log::{Filter, Log, ValueOrArray};
mod ens;
pub use ens::NameOrAddress;
// re-export the non-standard rand version so that other crates don't use the
// wrong one by accident
pub use rand;
// re-export libsecp
pub use secp256k1;

View File

@ -74,20 +74,21 @@ impl PrivateKey {
tx: TransactionRequest,
chain_id: Option<U64>,
) -> Result<Transaction, TxError> {
// Calling `
// The nonce, gas and gasprice fields must already be populated
let nonce = tx.nonce.ok_or(TxError::NonceMissing)?;
let gas_price = tx.gas_price.ok_or(TxError::NonceMissing)?;
let gas = tx.gas.ok_or(TxError::NonceMissing)?;
let gas_price = tx.gas_price.ok_or(TxError::GasPriceMissing)?;
let gas = tx.gas.ok_or(TxError::GasMissing)?;
// Hash the transaction's RLP encoding
let hash = tx.hash(chain_id);
let message = Message::from_slice(hash.as_bytes()).expect("hash is non-zero 32-bytes; qed");
// Sign it (with replay protection if applicable)
let signature = self.sign_with_eip155(&message, chain_id);
let rlp = tx.rlp_signed(&signature);
let hash = keccak256(&rlp.0);
// This function should not be called with ENS names
let to = tx.to.map(|to| match to {
NameOrAddress::Address(inner) => inner,
NameOrAddress::Name(_) => {
@ -129,6 +130,7 @@ impl PrivateKey {
}
}
/// Applies [EIP155](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md)
fn to_eip155_v(recovery_id: RecoveryId, chain_id: Option<U64>) -> u64 {
let standard_v = recovery_id.to_i32() as u64;
if let Some(chain_id) = chain_id {
@ -156,7 +158,7 @@ impl Deref for PrivateKey {
impl DefaultIsZeroes for PrivateKey {}
/// A public key
/// A secp256k1 Public Key
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PublicKey(pub(super) PubKey);

View File

@ -0,0 +1,5 @@
mod keys;
pub use keys::{PrivateKey, PublicKey, TxError};
mod signature;
pub use signature::Signature;

View File

@ -1,35 +1,10 @@
//! Various Ethereum Related Datatypes
pub type Selector = [u8; 4];
mod crypto;
pub use crypto::*;
// Re-export common ethereum datatypes with more specific names
pub use ethereum_types::H256 as TxHash;
pub use ethereum_types::{Address, Bloom, H160, H256, U128, U256, U64};
mod chainstate;
pub use chainstate::*;
mod transaction;
pub use transaction::{Overrides, Transaction, TransactionReceipt, TransactionRequest};
mod keys;
pub use keys::{PrivateKey, PublicKey, TxError};
mod signature;
pub use signature::Signature;
mod bytes;
pub use bytes::Bytes;
mod block;
pub use block::{Block, BlockId, BlockNumber};
mod log;
pub use log::{Filter, Log, ValueOrArray};
mod ens;
pub use ens::NameOrAddress;
// re-export the non-standard rand version so that other crates don't use the
// wrong one by accident
pub use rand;
// re-export libsecp
pub use secp256k1;
#[cfg(feature = "abi")]
pub mod abi;

View File

@ -7,7 +7,6 @@ edition = "2018"
[features]
default = ["full"]
full = [
"abi",
"contract",
"providers",
"signers",
@ -15,7 +14,6 @@ full = [
"utils",
]
abi = ["ethers-abi"]
contract = ["ethers-contract"]
providers = ["ethers-providers"]
signers = ["ethers-signers"]
@ -23,7 +21,6 @@ types = ["ethers-types"]
utils = ["ethers-utils"]
[dependencies]
ethers-abi = { version = "0.1.0", path = "../ethers-abi", optional = true }
ethers-contract = { version = "0.1.0", path = "../ethers-contract", features = ["abigen"], optional = true }
ethers-providers = { version = "0.1.0", path = "../ethers-providers", optional = true }
ethers-signers = { version = "0.1.0", path = "../ethers-signers", optional = true }

View File

@ -15,11 +15,6 @@
//! More examples can be found in the [`examples` directory of the
//! repositry](https://github.com/gakonst/ethers-rs)
#[cfg(feature = "abi")]
pub mod abi {
pub use ethers_abi::*;
}
#[cfg(feature = "contract")]
pub mod contract {
pub use ethers_contract::*;