diff --git a/ethers-core/src/abi/codec.rs b/ethers-core/src/abi/codec.rs index a953eef0..45b0ad2d 100644 --- a/ethers-core/src/abi/codec.rs +++ b/ethers-core/src/abi/codec.rs @@ -77,6 +77,12 @@ impl_abi_codec!( i128 ); +impl<'a> AbiEncode for &'a str { + fn encode(self) -> Vec { + self.to_string().encode() + } +} + impl AbiEncode for [T; N] { fn encode(self) -> Vec { let token = self.into_token(); @@ -266,4 +272,11 @@ mod tests { let tuple: Vec<(Address, u8, Vec<[u8; 4]>)> = vec![(Address::random(), 0, nested)]; assert_codec(tuple); } + + #[test] + fn str_encoding() { + let value = "str value"; + let encoded = value.encode(); + assert_eq!(value, String::decode(encoded).unwrap()); + } } diff --git a/ethers-core/src/abi/mod.rs b/ethers-core/src/abi/mod.rs index da12946b..bf31b833 100644 --- a/ethers-core/src/abi/mod.rs +++ b/ethers-core/src/abi/mod.rs @@ -163,6 +163,7 @@ impl_abi_type!( Address => Address, bool => Bool, String => String, + str => String, H256 => FixedBytes(32), H512 => FixedBytes(64), U64 => Uint(64), @@ -180,6 +181,14 @@ impl_abi_type!( I256 => Int(256) ); +impl<'a> AbiType for &'a str { + fn param_type() -> ParamType { + ParamType::String + } +} + +impl<'a> AbiArrayType for &'a str {} + macro_rules! impl_abi_type_tuple { ($num: expr, $( $ty: ident),+) => { impl<$($ty, )+> AbiType for ($($ty,)+) where @@ -310,6 +319,9 @@ mod tests { ParamType::FixedArray(Box::new(ParamType::Uint(16)), 32), <[u16; 32]>::param_type() ); + + assert_eq!(ParamType::String, str::param_type()); + assert_eq!(ParamType::String, <&str>::param_type()); } #[test]