eventify_primitives/networks/base/
block.rs

1use std::{fmt::Debug, hash::Hash};
2
3use alloy_primitives::{B256, U256};
4use eyre::Result;
5use redis::AsyncCommands;
6use sqlx::FromRow;
7use utoipa::ToSchema;
8
9use crate::{
10    networks::{core::CoreBlock, NetworkKind, ResourceKind},
11    traits::{Block, Emit, Insert, Stream},
12    PropagateError,
13};
14
15#[derive(
16    Clone,
17    Debug,
18    Default,
19    serde::Deserialize,
20    serde::Serialize,
21    PartialEq,
22    Eq,
23    Hash,
24    FromRow,
25    ToSchema,
26)]
27pub struct BaseBlock {
28    #[serde(flatten)]
29    core: CoreBlock,
30
31    #[serde(rename = "baseFeePerGas")]
32    pub base_fee: Option<U256>,
33    #[serde(rename = "totalDifficulty")]
34    pub total_difficulty: Option<U256>,
35    #[serde(rename = "withdrawalsRoot")]
36    pub withdrawals_hash: Option<B256>,
37}
38
39impl Block for BaseBlock {
40    fn core(&self) -> &CoreBlock {
41        &self.core
42    }
43}
44
45impl Insert for BaseBlock {
46    async fn insert(
47        &self,
48        pool: &sqlx::PgPool,
49        _: &Option<alloy_primitives::B256>,
50    ) -> Result<(), sqlx::Error> {
51        self.core.insert(pool, NetworkKind::Base).await
52    }
53}
54
55impl Emit for BaseBlock {
56    async fn emit(
57        &self,
58        queue: &redis::Client,
59        network: &NetworkKind,
60    ) -> Result<(), PropagateError> {
61        let mut con = queue.get_async_connection().await?;
62
63        let channel = format!("{}:{}", network, ResourceKind::Block);
64        con.lpush(channel, serde_json::to_string(self)?).await?;
65
66        Ok(())
67    }
68}
69
70impl Stream for BaseBlock {
71    async fn stream(
72        &self,
73        queue: &redis::Client,
74        network: &NetworkKind,
75    ) -> Result<(), PropagateError> {
76        self.core().stream(queue, network).await
77    }
78}