refactor: change decode_log to take a reference

This commit is contained in:
Matthias Seitz 2021-03-18 10:10:36 +01:00
parent 2513e1f3ff
commit 3e9159d834
2 changed files with 16 additions and 8 deletions

View File

@ -203,6 +203,8 @@ pub fn derive_abi_event(input: TokenStream) -> TokenStream {
signature(hash.as_bytes()) signature(hash.as_bytes())
}; };
let anon = attributes.anonymous.map(|(b,_)|b).unwrap_or_default();
let ethevent_impl = quote! { let ethevent_impl = quote! {
impl ethers_contract::EthEvent for #name { impl ethers_contract::EthEvent for #name {
@ -218,10 +220,13 @@ pub fn derive_abi_event(input: TokenStream) -> TokenStream {
#abi.into() #abi.into()
} }
fn decode_log(log: ethers::abi::RawLog) -> Result<Self, ethers::abi::Error> where Self: Sized { fn decode_log(log: &ethers::abi::RawLog) -> Result<Self, ethers::abi::Error> where Self: Sized {
#decode_log_impl #decode_log_impl
} }
fn is_anonymous() -> bool {
#anon
}
} }
}; };
@ -415,10 +420,10 @@ fn derive_decode_from_log_impl(
( (
quote! {}, quote! {},
quote! { quote! {
let flat_topics = topics.into_iter().flat_map(|t| t.as_ref().to_vec()).collect::<Vec<u8>>(); let flat_topics = topics.iter().flat_map(|t| t.as_ref().to_vec()).collect::<Vec<u8>>();
}, },
quote! { quote! {
if topic_tokens.len() != topics_len { if topic_tokens.len() != topics.len() {
return Err(ethers::abi::Error::InvalidData); return Err(ethers::abi::Error::InvalidData);
} }
}, },
@ -432,10 +437,10 @@ fn derive_decode_from_log_impl(
} }
}, },
quote! { quote! {
let flat_topics = topics.into_iter().skip(1).flat_map(|t| t.as_ref().to_vec()).collect::<Vec<u8>>(); let flat_topics = topics.iter().skip(1).flat_map(|t| t.as_ref().to_vec()).collect::<Vec<u8>>();
}, },
quote! { quote! {
if topic_tokens.is_empty() || topic_tokens.len() != topics_len - 1 { if topic_tokens.is_empty() || topic_tokens.len() != topics.len() - 1 {
return Err(ethers::abi::Error::InvalidData); return Err(ethers::abi::Error::InvalidData);
} }
}, },
@ -468,7 +473,7 @@ fn derive_decode_from_log_impl(
let mut topic_tokens = ethers::abi::decode(&topic_types, &flat_topics)?; let mut topic_tokens = ethers::abi::decode(&topic_types, &flat_topics)?;
#topic_tokens_len_check #topic_tokens_len_check
let mut data_tokens = ethers::abi::decode(&data_types, &data)?; let mut data_tokens = ethers::abi::decode(&data_types, &data)?;
let mut tokens = Vec::with_capacity(topics_len + data_tokens.len()); let mut tokens = Vec::with_capacity(topics.len() + data_tokens.len());
#( tokens.push(#swap_tokens); )* #( tokens.push(#swap_tokens); )*
} }
}; };
@ -476,7 +481,6 @@ fn derive_decode_from_log_impl(
Ok(quote! { Ok(quote! {
let ethers::abi::RawLog {data, topics} = log; let ethers::abi::RawLog {data, topics} = log;
let topics_len = topics.len();
#signature_check #signature_check

View File

@ -23,9 +23,13 @@ pub trait EthEvent: Detokenize {
fn abi_signature() -> Cow<'static, str>; fn abi_signature() -> Cow<'static, str>;
/// Decodes an Ethereum `RawLog` into an instance of the type. /// Decodes an Ethereum `RawLog` into an instance of the type.
fn decode_log(log: RawLog) -> Result<Self, ethers_core::abi::Error> fn decode_log(log: &RawLog) -> Result<Self, ethers_core::abi::Error>
where where
Self: Sized; Self: Sized;
/// Returns true if this is an anonymous event
fn is_anonymous() -> bool;
} }
/// Helper for managing the event filter before querying or streaming its logs /// Helper for managing the event filter before querying or streaming its logs