1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![cfg(feature = "std")]

use chrono::{DateTime, NaiveDateTime, Utc};
use fuel_core_client::client::schema::block::{Block as ClientBlock, Header as ClientHeader};
use fuel_tx::Bytes32;

#[derive(Debug)]
pub struct Header {
    pub id: Bytes32,
    pub da_height: u64,
    pub transactions_count: u64,
    pub message_receipt_count: u64,
    pub transactions_root: Bytes32,
    pub message_receipt_root: Bytes32,
    pub height: u32,
    pub prev_root: Bytes32,
    pub time: Option<DateTime<Utc>>,
    pub application_hash: Bytes32,
}

impl From<ClientHeader> for Header {
    fn from(client_header: ClientHeader) -> Self {
        let naive = NaiveDateTime::from_timestamp_opt(client_header.time.0.to_unix(), 0);
        let time = naive.map(|time| DateTime::<Utc>::from_utc(time, Utc));

        Self {
            id: client_header.id.0 .0,
            da_height: client_header.da_height.0,
            transactions_count: client_header.transactions_count.0,
            message_receipt_count: client_header.message_receipt_count.0,
            transactions_root: client_header.transactions_root.0 .0,
            message_receipt_root: client_header.message_receipt_root.0 .0,
            height: client_header.height.0,
            prev_root: client_header.prev_root.0 .0,
            time,
            application_hash: client_header.application_hash.0 .0,
        }
    }
}

#[derive(Debug)]
pub struct Block {
    pub id: Bytes32,
    pub header: Header,
    pub transactions: Vec<Bytes32>,
}

impl From<ClientBlock> for Block {
    fn from(client_block: ClientBlock) -> Self {
        let transactions = client_block
            .transactions
            .iter()
            .map(|tx| tx.id.0 .0)
            .collect();

        Self {
            id: client_block.id.0 .0,
            header: client_block.header.into(),
            transactions,
        }
    }
}