ethers-rs/ethers-contract/src/log.rs

53 lines
1.6 KiB
Rust
Raw Permalink Normal View History

//! Mod of types for ethereum logs
use ethers_core::abi::Error;
use ethers_core::abi::RawLog;
2021-07-30 11:01:38 +00:00
use ethers_core::types::{Address, Log, TxHash, H256, U256, U64};
/// A trait for types (events) that can be decoded from a `RawLog`
pub trait EthLogDecode: Send + Sync {
/// decode from a `RawLog`
fn decode_log(log: &RawLog) -> Result<Self, Error>
where
Self: Sized;
}
/// Decodes a series of logs into a vector
pub fn decode_logs<T: EthLogDecode>(logs: &[RawLog]) -> Result<Vec<T>, Error> {
logs.iter().map(T::decode_log).collect()
}
2021-07-30 11:01:38 +00:00
/// Metadata inside a log
#[derive(Clone, Debug, PartialEq)]
pub struct LogMeta {
/// Address from which this log originated
pub address: Address,
/// The block in which the log was emitted
pub block_number: U64,
/// The block hash in which the log was emitted
pub block_hash: H256,
/// The transaction hash in which the log was emitted
pub transaction_hash: TxHash,
/// Transactions index position log was created from
pub transaction_index: U64,
/// Log index position in the block
pub log_index: U256,
}
impl From<&Log> for LogMeta {
fn from(src: &Log) -> Self {
LogMeta {
address: src.address,
block_number: src.block_number.expect("should have a block number"),
block_hash: src.block_hash.expect("should have a block hash"),
transaction_hash: src.transaction_hash.expect("should have a tx hash"),
transaction_index: src.transaction_index.expect("should have a tx index"),
log_index: src.log_index.expect("should have a log index"),
}
}
}