use crate::TxLocation;
use alloy::primitives::{BlockNumber, B256};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ConfirmationMeta {
block_number: BlockNumber,
block_hash: B256,
transaction_index: u64,
}
impl ConfirmationMeta {
pub const fn new(block_number: BlockNumber, block_hash: B256, transaction_index: u64) -> Self {
Self { block_number, block_hash, transaction_index }
}
pub const fn block_number(&self) -> BlockNumber {
self.block_number
}
pub const fn block_hash(&self) -> B256 {
self.block_hash
}
pub const fn transaction_index(&self) -> u64 {
self.transaction_index
}
}
impl From<(TxLocation, B256)> for ConfirmationMeta {
fn from((loc, block_hash): (TxLocation, B256)) -> Self {
Self::new(loc.block, block_hash, loc.index)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_confirmation_meta_new() {
let meta = ConfirmationMeta::new(100, B256::ZERO, 5);
assert_eq!(meta.block_number(), 100);
assert_eq!(meta.block_hash(), B256::ZERO);
assert_eq!(meta.transaction_index(), 5);
}
#[test]
fn test_from_tx_location() {
let loc = TxLocation { block: 42, index: 3 };
let hash = B256::repeat_byte(0xAB);
let meta = ConfirmationMeta::from((loc, hash));
assert_eq!(meta.block_number(), 42);
assert_eq!(meta.block_hash(), hash);
assert_eq!(meta.transaction_index(), 3);
}
}