use crate::{Result, Word, abi::AbiDecoderConfig};
use alloy_primitives::Log;
pub trait SolEventInterface: Sized {
const NAME: &'static str;
const COUNT: usize;
fn decode_raw_log(topics: &[Word], data: &[u8]) -> Result<Self>;
#[inline]
fn decode_raw_log_with_config(
topics: &[Word],
data: &[u8],
config: AbiDecoderConfig,
) -> Result<Self> {
if config.get_strict() {
Err(crate::Error::custom(
"strict decoding is unsupported by this SolEventInterface implementation",
))
} else {
Self::decode_raw_log(topics, data)
}
}
fn decode_log(log: &Log) -> Result<Log<Self>> {
Self::decode_raw_log(log.topics(), &log.data.data)
.map(|data| Log { address: log.address, data })
}
fn decode_log_with_config(log: &Log, config: AbiDecoderConfig) -> Result<Log<Self>> {
Self::decode_raw_log_with_config(log.topics(), &log.data.data, config)
.map(|data| Log { address: log.address, data })
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Legacy;
impl SolEventInterface for Legacy {
const NAME: &'static str = "Legacy";
const COUNT: usize = 0;
fn decode_raw_log(_topics: &[Word], _data: &[u8]) -> Result<Self> {
Ok(Self)
}
}
#[test]
fn legacy_interfaces_reject_strict_configs() {
assert!(Legacy::decode_raw_log_with_config(&[], &[], AbiDecoderConfig::new()).is_ok());
assert!(
Legacy::decode_raw_log_with_config(&[], &[], AbiDecoderConfig::new().validate(true))
.is_ok()
);
assert!(
Legacy::decode_raw_log_with_config(&[], &[], AbiDecoderConfig::new().strict(true))
.is_err()
);
}
}