eventify_primitives/networks/zksync/
block.rs

1use std::{fmt::Debug, hash::Hash};
2
3use alloy_primitives::B256;
4use eyre::Result;
5
6use sqlx::{Error as SqlError, FromRow};
7use utoipa::ToSchema;
8
9use crate::{
10    networks::{core::CoreBlock, NetworkKind},
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 ZksyncBlock {
28    #[serde(flatten)]
29    core: CoreBlock,
30}
31
32impl Block for ZksyncBlock {
33    fn core(&self) -> &CoreBlock {
34        &self.core
35    }
36}
37
38impl Insert for ZksyncBlock {
39    async fn insert(&self, pool: &sqlx::PgPool, _: &Option<B256>) -> Result<(), SqlError> {
40        self.core().insert(pool, NetworkKind::Zksync).await
41    }
42}
43
44impl Emit for ZksyncBlock {
45    // TODO: do we really need the network here?
46    async fn emit(
47        &self,
48        queue: &redis::Client,
49        network: &NetworkKind,
50    ) -> Result<(), PropagateError> {
51        self.core().emit(queue, network).await
52    }
53}
54
55impl Stream for ZksyncBlock {
56    async fn stream(
57        &self,
58        queue: &redis::Client,
59        network: &NetworkKind,
60    ) -> Result<(), PropagateError> {
61        self.core().stream(queue, network).await
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::ZksyncBlock;
68
69    #[test]
70    fn deserialize_zksync_block() {
71        let json = serde_json::json!(
72            {
73                "hash": "0x73c37ce80f617edf559dbc824d968de772e9a894c6faea4e0e1c0fe7267ed1f5",
74                "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
75                "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
76                "miner": "0x0000000000000000000000000000000000000000",
77                "stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
78                "transactionsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
79                "receiptsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
80                "number": "0x1b82257",
81                "gasUsed": "0x0",
82                "gasLimit": "0x0",
83                "extraData": "0x",
84                "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
85                "timestamp": "0x65f1682a",
86                "difficulty": "0x0",
87                "mixHash": null,
88                "nonce": null
89              }
90        );
91
92        assert!(serde_json::from_value::<ZksyncBlock>(json).is_ok());
93    }
94
95    #[test]
96    fn deserialize_empty_zksync_block() {
97        let json = serde_json::json!({});
98
99        assert!(serde_json::from_value::<ZksyncBlock>(json).is_err());
100    }
101}