1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
#![cfg_attr(not(feature = "std"), no_std)]
18

            
19
pub mod weights;
20

            
21
/// Money matters.
22
pub mod currency {
23
    use primitives::Balance;
24

            
25
    /// The existential deposit.
26
    pub const EXISTENTIAL_DEPOSIT: Balance = 1 * CENTS;
27

            
28
    // Provide a common factor between runtimes based on a supply of 10_000_000 tokens.
29
    pub const SUPPLY_FACTOR: Balance = 100;
30

            
31
    pub const UNITS: Balance = 1_000_000_000_000;
32
    pub const CENTS: Balance = UNITS / 30_000;
33
    pub const GRAND: Balance = CENTS * 100_000;
34
    pub const MILLICENTS: Balance = CENTS / 1_000;
35
    pub const MICROUNITS: Balance = 1_000_000;
36
    pub const MILLIUNITS: Balance = 1_000_000_000;
37

            
38
    pub const STORAGE_BYTE_FEE: Balance = 100 * MICROUNITS * SUPPLY_FACTOR;
39
    pub const STORAGE_ITEM_FEE: Balance = 100 * MILLIUNITS * SUPPLY_FACTOR;
40

            
41
    pub const fn deposit(items: u32, bytes: u32) -> Balance {
42
        items as Balance * STORAGE_ITEM_FEE + (bytes as Balance) * STORAGE_BYTE_FEE
43
    }
44
}
45

            
46
/// Time and blocks.
47
pub mod time {
48
    use runtime_common::prod_or_fast;
49

            
50
    use primitives::{BlockNumber, Moment};
51
    pub const MILLISECS_PER_BLOCK: Moment = 6000;
52
    pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK;
53

            
54
    frame_support::parameter_types! {
55
        pub const EpochDurationInBlocks: BlockNumber = prod_or_fast!(1 * HOURS, 1 * MINUTES);
56
    }
57

            
58
    // These time units are defined in number of blocks.
59
    pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
60
    pub const HOURS: BlockNumber = MINUTES * 60;
61
    pub const DAYS: BlockNumber = HOURS * 24;
62
    pub const WEEKS: BlockNumber = DAYS * 7;
63

            
64
    // 1 in 4 blocks (on average, not counting collisions) will be primary babe blocks.
65
    // The choice of is done in accordance to the slot duration and expected target
66
    // block time, for safely resisting network delays of maximum two seconds.
67
    // <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
68
    pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
69
}
70

            
71
/// Fee-related.
72
pub mod fee {
73
    pub use sp_runtime::Perbill;
74
    use {
75
        crate::weights::ExtrinsicBaseWeight,
76
        frame_support::weights::{
77
            WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
78
        },
79
        primitives::Balance,
80
        smallvec::smallvec,
81
    };
82

            
83
    /// The block saturation level. Fees will be updates based on this value.
84
    pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
85

            
86
    /// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
87
    /// node's balance type.
88
    ///
89
    /// This should typically create a mapping between the following ranges:
90
    ///   - [0, `frame_system::MaximumBlockWeight`]
91
    ///   - [Balance::min, Balance::max]
92
    ///
93
    /// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
94
    ///   - Setting it to `0` will essentially disable the weight fee.
95
    ///   - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
96
    pub struct WeightToFee;
97
    impl WeightToFeePolynomial for WeightToFee {
98
        type Balance = Balance;
99
2
        fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
100
2
            // in Dancelight, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
101
2
            let p = super::currency::CENTS;
102
2
            let q = 10 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
103
2
            smallvec![WeightToFeeCoefficient {
104
                degree: 1,
105
                negative: false,
106
                coeff_frac: Perbill::from_rational(p % q, q),
107
                coeff_integer: p / q,
108
            }]
109
2
        }
110
    }
111
}
112

            
113
/// System Parachains.
114
pub mod system_parachain {
115
    use {primitives::Id, xcm_builder::IsChildSystemParachain};
116

            
117
    /// Network's Asset Hub parachain ID.
118
    pub const ASSET_HUB_ID: u32 = 1000;
119
    /// Contracts parachain ID.
120
    pub const CONTRACTS_ID: u32 = 1002;
121
    /// Encointer parachain ID.
122
    pub const ENCOINTER_ID: u32 = 1003;
123
    /// People parachain ID.
124
    pub const PEOPLE_ID: u32 = 1004;
125
    /// BridgeHub parachain ID.
126
    pub const BRIDGE_HUB_ID: u32 = 1013;
127
    /// Brokerage parachain ID.
128
    pub const BROKER_ID: u32 = 1005;
129

            
130
    /// All system parachains of Dancelight.
131
    pub type SystemParachains = IsChildSystemParachain<Id>;
132
}
133

            
134
/// Dancelight Treasury pallet instance.
135
pub const TREASURY_PALLET_ID: u8 = 40;
136

            
137
#[cfg(test)]
138
mod tests {
139
    use {
140
        super::{
141
            currency::{CENTS, MILLICENTS},
142
            fee::WeightToFee,
143
        },
144
        crate::weights::ExtrinsicBaseWeight,
145
        frame_support::weights::WeightToFee as WeightToFeeT,
146
        runtime_common::MAXIMUM_BLOCK_WEIGHT,
147
    };
148

            
149
    #[test]
150
    // Test that the fee for `MAXIMUM_BLOCK_WEIGHT` of weight has sane bounds.
151
1
    fn full_block_fee_is_correct() {
152
1
        // A full block should cost between 1,000 and 10,000 CENTS.
153
1
        let full_block = WeightToFee::weight_to_fee(&MAXIMUM_BLOCK_WEIGHT);
154
1
        assert!(full_block >= 1_000 * CENTS);
155
1
        assert!(full_block <= 10_000 * CENTS);
156
1
    }
157

            
158
    #[test]
159
    // This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
160
1
    fn extrinsic_base_fee_is_correct() {
161
1
        // `ExtrinsicBaseWeight` should cost 1/10 of a CENT
162
1
        println!("Base: {}", ExtrinsicBaseWeight::get());
163
1
        let x = WeightToFee::weight_to_fee(&ExtrinsicBaseWeight::get());
164
1
        let y = CENTS / 10;
165
1
        assert!(x.max(y) - x.min(y) < MILLICENTS);
166
1
    }
167
}