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

fix: support display for bytes #1148

Merged
merged 1 commit into from
Apr 16, 2022
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
2 changes: 1 addition & 1 deletion ethers-contract/ethers-contract-derive/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) fn derive_eth_display_impl(input: DeriveInput) -> Result<TokenStream,
}
ParamType::Bytes => {
quote! {
write!(f, "0x{}", #hex_encode(self.#ident))?;
write!(f, "0x{}", #hex_encode(&self.#ident))?;
}
}
ParamType::Bool | ParamType::String => {
Expand Down
30 changes: 17 additions & 13 deletions ethers-contract/ethers-contract-derive/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,26 @@ pub fn find_parameter_type(ty: &Type) -> Result<ParamType, Error> {
Err(Error::new(ty.span(), "Failed to derive proper ABI from array field"))
}
Type::Path(ty) => {
if let Some(ident) = ty.path.get_ident() {
// check for `Vec`
if ty.path.segments.len() == 1 && ty.path.segments[0].ident == "Vec" {
if let PathArguments::AngleBracketed(ref args) = ty.path.segments[0].arguments {
if args.args.len() == 1 {
if let GenericArgument::Type(ref ty) = args.args.iter().next().unwrap() {
let kind = find_parameter_type(ty)?;
return Ok(ParamType::Array(Box::new(kind)))
}
}
}
}
let mut ident = ty.path.get_ident();
if ident.is_none() {
ident = ty.path.segments.last().map(|s| &s.ident);
}
if let Some(ident) = ident {
let ident = ident.to_string().to_lowercase();
return match ident.as_str() {
"address" => Ok(ParamType::Address),
"bytes" => Ok(ParamType::Bytes),
"string" => Ok(ParamType::String),
"bool" => Ok(ParamType::Bool),
"int" | "uint" => Ok(ParamType::Uint(256)),
Expand All @@ -126,18 +142,6 @@ pub fn find_parameter_type(ty: &Type) -> Result<ParamType, Error> {
}),
}
}
// check for `Vec`
if ty.path.segments.len() == 1 && ty.path.segments[0].ident == "Vec" {
if let PathArguments::AngleBracketed(ref args) = ty.path.segments[0].arguments {
if args.args.len() == 1 {
if let GenericArgument::Type(ref ty) = args.args.iter().next().unwrap() {
let kind = find_parameter_type(ty)?;
return Ok(ParamType::Array(Box::new(kind)))
}
}
}
}

Err(Error::new(ty.span(), "Failed to derive proper ABI from fields"))
}
Type::Tuple(ty) => {
Expand Down
13 changes: 13 additions & 0 deletions ethers-contract/tests/common/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,16 @@ fn can_derive_array_tuples() {
pub calldata: Bytes,
}
}

#[test]
fn eth_display_works_on_ethers_bytes() {
#[derive(Clone, Debug, Default, Eq, PartialEq, EthCall, EthDisplay)]
#[ethcall(name = "logBytes", abi = "logBytes(bytes)")]
pub struct LogBytesCall {
pub p_0: ethers_core::types::Bytes,
}
let call = LogBytesCall { p_0: hex::decode(b"aaaaaa").unwrap().into() };

let s = format!("{}", call);
assert_eq!(s, "0xaaaaaa");
}