diff --git a/ethers-contract/ethers-contract-derive/src/display.rs b/ethers-contract/ethers-contract-derive/src/display.rs index 459182074..8fc613f1a 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 066cbbfaf..abef0999b 100644 --- a/ethers-contract/ethers-contract-derive/src/utils.rs +++ b/ethers-contract/ethers-contract-derive/src/utils.rs @@ -111,10 +111,26 @@ 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() { + // 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)), @@ -126,18 +142,6 @@ pub fn find_parameter_type(ty: &Type) -> Result { }), } } - // 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) => { diff --git a/ethers-contract/tests/common/derive.rs b/ethers-contract/tests/common/derive.rs index 65f07ca7d..25dd7aaf8 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"); +}