--- a/crates/evm-coder/src/abi/impls.rs +++ b/crates/evm-coder/src/abi/impls.rs @@ -258,3 +258,39 @@ impl_tuples! {A B C D E F G H} impl_tuples! {A B C D E F G H I} impl_tuples! {A B C D E F G H I J} + +//----- impls for Option ----- +impl AbiType for Option { + const SIGNATURE: SignatureUnit = <(bool, T)>::SIGNATURE; + + fn is_dynamic() -> bool { + <(bool, T)>::is_dynamic() + } + + fn size() -> usize { + <(bool, T)>::size() + } +} + +impl AbiWrite for Option { + fn abi_write(&self, writer: &mut AbiWriter) { + match self { + Some(value) => (true, value).abi_write(writer), + None => (true, T::default()).abi_write(writer), + } + } +} + +impl AbiRead for Option +where + Self: AbiType, + T: AbiRead + AbiType, +{ + fn abi_read(reader: &mut AbiReader) -> Result + where + Self: Sized, + { + let (status, value) = <(bool, T)>::abi_read(reader)?; + Ok(if status { Some(value) } else { None }) + } +}