fix: support display for bytes (#1148)

This commit is contained in:
Matthias Seitz 2022-04-16 22:08:31 +02:00 committed by GitHub
parent ae090517fc
commit a99dd1328b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 17 deletions

View File

@ -42,7 +42,7 @@ pub(crate) fn derive_eth_display_impl(input: DeriveInput) -> Result<TokenStream,
} }
ParamType::Bytes => { ParamType::Bytes => {
quote! { quote! {
write!(f, "0x{}", #hex_encode(self.#ident))?; write!(f, "0x{}", #hex_encode(&self.#ident))?;
} }
} }
ParamType::Bool | ParamType::String => { ParamType::Bool | ParamType::String => {

View File

@ -111,21 +111,6 @@ pub fn find_parameter_type(ty: &Type) -> Result<ParamType, Error> {
Err(Error::new(ty.span(), "Failed to derive proper ABI from array field")) Err(Error::new(ty.span(), "Failed to derive proper ABI from array field"))
} }
Type::Path(ty) => { 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` // check for `Vec`
if ty.path.segments.len() == 1 && ty.path.segments[0].ident == "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 let PathArguments::AngleBracketed(ref args) = ty.path.segments[0].arguments {
@ -137,7 +122,26 @@ pub fn find_parameter_type(ty: &Type) -> Result<ParamType, Error> {
} }
} }
} }
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")) Err(Error::new(ty.span(), "Failed to derive proper ABI from fields"))
} }
Type::Tuple(ty) => { Type::Tuple(ty) => {

View File

@ -583,3 +583,16 @@ fn can_derive_array_tuples() {
pub calldata: Bytes, 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");
}