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

Update implementation of Tokenizable for Vec<u8> #377

Merged
merged 4 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 32 additions & 4 deletions src/contract/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Contract Functions Output types.

use crate::contract::error::Error;
use crate::types::{Address, Bytes, H256, U128, U256};
use crate::types::{Address, Bytes, BytesArray, H256, U128, U256};
use arrayvec::ArrayVec;
use ethabi::Token;

Expand Down Expand Up @@ -291,10 +291,29 @@ macro_rules! tokenizable_item {
}

tokenizable_item! {
Token, String, Address, H256, U256, U128, bool, Vec<u8>,
Token, String, Address, H256, U256, U128, bool, BytesArray, Vec<u8>,
i8, i16, i32, i64, i128, u16, u32, u64, u128,
}

impl Tokenizable for BytesArray {
fn from_token(token: Token) -> Result<Self, Error> {
match token {
Token::FixedArray(tokens) | Token::Array(tokens) => {
let bytes = tokens
.into_iter()
.map(Tokenizable::from_token)
.collect::<Result<Vec<u8>, Error>>()?;
Ok(Self(bytes))
}
other => Err(Error::InvalidOutputType(format!("Expected `Array`, got {:?}", other))),
}
}

fn into_token(self) -> Token {
Token::Array(self.0.into_iter().map(Tokenizable::into_token).collect())
}
}

impl Tokenizable for Vec<u8> {
fn from_token(token: Token) -> Result<Self, Error> {
match token {
Expand Down Expand Up @@ -420,8 +439,8 @@ impl_fixed_types!(1024);
#[cfg(test)]
mod tests {
use super::{Detokenize, Tokenizable};
use crate::types::{Address, U256};
use ethabi::Token;
use crate::types::{Address, BytesArray, U256};
use ethabi::{Token, Uint};

fn output<R: Detokenize>() -> R {
unimplemented!()
Expand All @@ -436,6 +455,7 @@ mod tests {
let _string: String = output();
let _bool: bool = output();
let _bytes: Vec<u8> = output();
let _bytes_array: BytesArray = output();

let _pair: (U256, bool) = output();
let _vec: Vec<U256> = output();
Expand Down Expand Up @@ -468,6 +488,14 @@ mod tests {
assert_eq!(data[7][0], 8);
}

#[test]
fn should_decode_array_of_bytes() {
let token = Token::Array(vec![Token::Uint(Uint::from(0)), Token::Uint(Uint::from(1))]);
let data: BytesArray = Tokenizable::from_token(token).unwrap();
assert_eq!(data.0[0], 0);
assert_eq!(data.0[1], 1);
}

#[test]
fn should_sign_extend_negative_integers() {
assert_eq!((-1i8).into_token(), Token::Int(U256::MAX));
Expand Down
7 changes: 7 additions & 0 deletions src/types/bytes_array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};

/// A wrapper type for array of bytes.
///
/// Implements `Tokenizable` so can be used to retrieve data from `Solidity` contracts returning `byte8[]`.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Hash, Serialize)]
pub struct BytesArray(pub Vec<u8>);
2 changes: 2 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod block;
mod bytes;
mod bytes_array;
mod log;
mod parity_peers;
mod recovery;
Expand All @@ -17,6 +18,7 @@ mod work;

pub use self::block::{Block, BlockHeader, BlockId, BlockNumber};
pub use self::bytes::Bytes;
pub use self::bytes_array::BytesArray;
pub use self::log::{Filter, FilterBuilder, Log};
pub use self::parity_peers::{
EthProtocolInfo, ParityPeerInfo, ParityPeerType, PeerNetworkInfo, PeerProtocolsInfo, PipProtocolInfo,
Expand Down