cat_gateway/service/common/objects/cardano/
slot_info.rs

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Defines API schemas of Cardano Slot info types.

use poem_openapi::{types::Example, Object};

use crate::service::{
    api::cardano::types::{DateTime, SlotNumber},
    common::objects::cardano::hash::Hash,
};

/// Cardano block's slot data.
#[derive(Object)]
#[oai(example = true)]
#[allow(clippy::struct_field_names)]
pub(crate) struct Slot {
    /// Slot number.
    // TODO(bkioshn): https://github.com/input-output-hk/catalyst-voices/issues/239
    #[oai(validator(minimum(value = "0"), maximum(value = "9223372036854775807")))]
    pub(crate) slot_number: SlotNumber,

    /// Block hash.
    pub(crate) block_hash: Hash,

    /// Block time.
    pub(crate) block_time: DateTime,
}

impl Example for Slot {
    #[allow(clippy::expect_used)]
    fn example() -> Self {
        Self {
            slot_number: 121_099_410,
            block_hash: hex::decode(
                "aa34657bf91e04eb5b506d76a66f688dbfbc509dbf70bc38124d4e8832fdd68a",
            )
            .expect("Invalid hex")
            .into(),
            block_time: chrono::DateTime::from_timestamp(1_712_676_501, 0)
                .expect("Invalid timestamp"),
        }
    }
}

/// Cardano follower's slot info.
#[derive(Object)]
#[oai(example = true)]
pub(crate) struct SlotInfo {
    /// Previous slot info.
    #[oai(skip_serializing_if_is_none)]
    pub(crate) previous: Option<Slot>,

    /// Current slot info.
    #[oai(skip_serializing_if_is_none)]
    pub(crate) current: Option<Slot>,

    /// Next slot info.
    #[oai(skip_serializing_if_is_none)]
    pub(crate) next: Option<Slot>,
}

impl Example for SlotInfo {
    #[allow(clippy::expect_used)]
    fn example() -> Self {
        Self {
            previous: Some(Slot {
                slot_number: 121_099_406,
                block_hash: hex::decode(
                    "162ae0e2d08dd238233308eef328bf39ba529b82bc0b87c4eeea3c1dae4fc877",
                )
                .expect("Invalid hex")
                .into(),
                block_time: chrono::DateTime::from_timestamp(1_712_676_497, 0)
                    .expect("Invalid timestamp"),
            }),
            current: Some(Slot {
                slot_number: 121_099_409,
                block_hash: hex::decode(
                    "aa34657bf91e04eb5b506d76a66f688dbfbc509dbf70bc38124d4e8832fdd68a",
                )
                .expect("Invalid hex")
                .into(),
                block_time: chrono::DateTime::from_timestamp(1_712_676_501, 0)
                    .expect("Invalid timestamp"),
            }),
            next: Some(Slot {
                slot_number: 121_099_422,
                block_hash: hex::decode(
                    "83ad63288ae14e75de1a1f794bda5d317fa59cbdbf1cc4dc83471d76555a5e89",
                )
                .expect("Invalid hex")
                .into(),
                block_time: chrono::DateTime::from_timestamp(1_712_676_513, 0)
                    .expect("Invalid timestamp"),
            }),
        }
    }
}