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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::{
    interfaces::{LinearFeeDef, ValueDef},
    time::SystemTime,
};
use chain_addr::Discrimination;
use chain_impl_mockchain::{
    block::Epoch,
    fee::LinearFee,
    rewards::{CompoundingType, Limit, Parameters, Ratio, TaxType},
    value::Value,
};
use serde::{Deserialize, Serialize};
use std::num::{NonZeroU32, NonZeroU64};

#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct SettingsDto {
    pub block0_hash: String,
    pub block0_time: SystemTime,
    pub curr_slot_start_time: Option<SystemTime>,
    pub consensus_version: String,
    #[serde(with = "LinearFeeDef")]
    pub fees: LinearFee,
    pub block_content_max_size: u32,
    pub epoch_stability_depth: u32,
    pub slot_duration: u64,
    pub slots_per_epoch: u32,
    #[serde(with = "TaxTypeDef")]
    pub treasury_tax: TaxType,
    #[serde(with = "ParametersDef")]
    pub reward_params: Parameters,
    #[serde(with = "DiscriminationDef")]
    pub discrimination: Discrimination,
    pub tx_max_expiry_epochs: u8,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, remote = "TaxType")]
pub struct TaxTypeDef {
    #[serde(with = "ValueDef")]
    pub fixed: Value,

    #[serde(with = "RatioDef")]
    pub ratio: Ratio,

    #[serde(default, rename = "max", skip_serializing_if = "Option::is_none")]
    pub max_limit: Option<NonZeroU64>,
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[serde(transparent)]
pub struct TaxTypeSerde(#[serde(with = "TaxTypeDef")] pub TaxType);

#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[serde(remote = "Limit")]
pub enum LimitDef {
    None,
    ByStakeAbsolute(#[serde(with = "RatioDef")] Ratio),
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[serde(remote = "Ratio")]
pub struct RatioDef {
    pub numerator: u64,
    pub denominator: NonZeroU64,
}

#[derive(Deserialize, Serialize)]
#[serde(remote = "Parameters", rename_all = "camelCase")]
pub struct ParametersDef {
    pub initial_value: u64,
    #[serde(with = "RatioDef")]
    pub compounding_ratio: Ratio,
    #[serde(with = "CompoundingTypeDef")]
    pub compounding_type: CompoundingType,
    pub epoch_rate: NonZeroU32,
    pub epoch_start: Epoch,
    #[serde(with = "LimitDef")]
    pub reward_drawing_limit_max: Limit,
    pub pool_participation_capping: Option<(NonZeroU32, NonZeroU32)>,
}

#[derive(Deserialize, Serialize)]
#[serde(remote = "CompoundingType")]
pub enum CompoundingTypeDef {
    Linear,
    Halvening,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase", remote = "Discrimination")]
enum DiscriminationDef {
    Test,
    Production,
}

impl PartialEq<SettingsDto> for SettingsDto {
    fn eq(&self, other: &SettingsDto) -> bool {
        self.block0_hash == other.block0_hash
            && self.block0_time == other.block0_time
            && self.consensus_version == other.consensus_version
            && self.fees == other.fees
            && self.block_content_max_size == other.block_content_max_size
            && self.epoch_stability_depth == other.epoch_stability_depth
            && self.slot_duration == other.slot_duration
            && self.slots_per_epoch == other.slots_per_epoch
            && self.treasury_tax == other.treasury_tax
            && self.reward_params == other.reward_params
    }
}