Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat(codec): impl codec for Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Feb 2, 2022
1 parent dc1565c commit 1d8845d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 13 additions & 1 deletion ethers-contract/tests/common/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ethers_contract::{
};
use ethers_core::{
abi::{AbiDecode, AbiEncode, RawLog, Tokenizable},
types::{Address, H160, H256, I256, U128, U256},
types::{Address, Bytes, H160, H256, I256, U128, U256},
};

fn assert_tokenizeable<T: Tokenizable>() {}
Expand Down Expand Up @@ -560,3 +560,15 @@ fn can_derive_abi_codec_two_field() {

assert_eq!(decoded_wrapped, tuple);
}

#[test]
fn can_derive_ethcall_for_bytes() {
#[derive(Clone, Debug, Default, Eq, PartialEq, EthCall, EthDisplay)]
#[ethcall(name = "batch", abi = "batch(bytes[],bool)")]
pub struct BatchCall {
pub calls: Vec<Bytes>,
pub revert_on_fail: bool,
}

assert_ethcall::<BatchCall>();
}
10 changes: 9 additions & 1 deletion ethers-core/src/abi/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
abi::{
AbiArrayType, AbiError, AbiType, Detokenize, Token, Tokenizable, TokenizableItem, Tokenize,
},
types::{Address, H256, U128, U256},
types::{Address, Bytes, H256, U128, U256},
};

/// Trait for ABI encoding
Expand Down Expand Up @@ -40,6 +40,7 @@ macro_rules! impl_abi_codec {

impl_abi_codec!(
Vec<u8>,
Bytes,
Address,
bool,
String,
Expand Down Expand Up @@ -228,4 +229,11 @@ mod tests {
.collect::<String>()
};
}

#[test]
fn bytes_codec() {
let bytes: Bytes = std::iter::repeat_with(random::<u8>).take(10).collect::<Vec<_>>().into();
let v = vec![bytes];
assert_codec(v);
}
}

0 comments on commit 1d8845d

Please sign in to comment.