From a99dd1328bbe6c9912ddac9efd2be8406e1d81e9 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 16 Apr 2022 22:08:31 +0200 Subject: [PATCH] fix: support display for bytes (#1148) --- .../ethers-contract-derive/src/display.rs | 2 +- .../ethers-contract-derive/src/utils.rs | 36 ++++++++++--------- ethers-contract/tests/common/derive.rs | 13 +++++++ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/ethers-contract/ethers-contract-derive/src/display.rs b/ethers-contract/ethers-contract-derive/src/display.rs index 45918207..8fc613f1 100644 --- a/ethers-contract/ethers-contract-derive/src/display.rs +++ b/ethers-contract/ethers-contract-derive/src/display.rs @@ -42,7 +42,7 @@ pub(crate) fn derive_eth_display_impl(input: DeriveInput) -> Result { quote! { - write!(f, "0x{}", #hex_encode(self.#ident))?; + write!(f, "0x{}", #hex_encode(&self.#ident))?; } } ParamType::Bool | ParamType::String => { diff --git a/ethers-contract/ethers-contract-derive/src/utils.rs b/ethers-contract/ethers-contract-derive/src/utils.rs index 066cbbfa..abef0999 100644 --- a/ethers-contract/ethers-contract-derive/src/utils.rs +++ b/ethers-contract/ethers-contract-derive/src/utils.rs @@ -111,21 +111,6 @@ pub fn find_parameter_type(ty: &Type) -> Result { Err(Error::new(ty.span(), "Failed to derive proper ABI from array field")) } Type::Path(ty) => { - if let Some(ident) = ty.path.get_ident() { - let ident = ident.to_string().to_lowercase(); - return match ident.as_str() { - "address" => Ok(ParamType::Address), - "string" => Ok(ParamType::String), - "bool" => Ok(ParamType::Bool), - "int" | "uint" => Ok(ParamType::Uint(256)), - "h160" => Ok(ParamType::FixedBytes(20)), - "h256" | "secret" | "hash" => Ok(ParamType::FixedBytes(32)), - "h512" | "public" => Ok(ParamType::FixedBytes(64)), - s => parse_int_param_type(s).ok_or_else(|| { - Error::new(ty.span(), "Failed to derive proper ABI from fields") - }), - } - } // 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 { @@ -137,7 +122,26 @@ pub fn find_parameter_type(ty: &Type) -> Result { } } } - + 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)), + "h160" => Ok(ParamType::FixedBytes(20)), + "h256" | "secret" | "hash" => Ok(ParamType::FixedBytes(32)), + "h512" | "public" => Ok(ParamType::FixedBytes(64)), + s => parse_int_param_type(s).ok_or_else(|| { + Error::new(ty.span(), "Failed to derive proper ABI from fields") + }), + } + } Err(Error::new(ty.span(), "Failed to derive proper ABI from fields")) } Type::Tuple(ty) => { diff --git a/ethers-contract/tests/common/derive.rs b/ethers-contract/tests/common/derive.rs index 65f07ca7..25dd7aaf 100644 --- a/ethers-contract/tests/common/derive.rs +++ b/ethers-contract/tests/common/derive.rs @@ -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"); +}