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
pub mod snowbridge {
72
    use {
73
        frame_support::parameter_types,
74
        xcm::prelude::{Location, NetworkId},
75
    };
76

            
77
    parameter_types! {
78
            /// Network and location for the Ethereum chain. On Stagelight, the Ethereum chain bridged
79
            /// to is the Holesky Ethereum testnet, with chain ID 17000.
80
            /// <https://chainlist.org/chain/17000>
81
            /// <https://ethereum.org/en/developers/docs/apis/json-rpc/#net_version>
82
            pub EthereumNetwork: NetworkId = NetworkId::Ethereum { chain_id: 17000 };
83
            pub EthereumLocation: Location = Location::new(1, EthereumNetwork::get());
84

            
85
    }
86

            
87
    #[cfg(feature = "runtime-benchmarks")]
88
    parameter_types! {
89
            // We need a different ethereum location for benchmarks as the ethereum system pallet
90
            // is written for benchmarks from para
91
            pub EthereumLocationForParaIdBenchmarks: Location = Location::new(2, EthereumNetwork::get());
92

            
93
    }
94
}
95

            
96
/// Fee-related.
97
pub mod fee {
98
    pub use sp_runtime::Perbill;
99
    use {
100
        crate::weights::ExtrinsicBaseWeight,
101
        frame_support::weights::{
102
            WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
103
        },
104
        primitives::Balance,
105
        smallvec::smallvec,
106
    };
107

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

            
111
    /// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
112
    /// node's balance type.
113
    ///
114
    /// This should typically create a mapping between the following ranges:
115
    ///   - [0, `frame_system::MaximumBlockWeight`]
116
    ///   - [Balance::min, Balance::max]
117
    ///
118
    /// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
119
    ///   - Setting it to `0` will essentially disable the weight fee.
120
    ///   - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
121
    pub struct WeightToFee;
122
    impl WeightToFeePolynomial for WeightToFee {
123
        type Balance = Balance;
124
93
        fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
125
93
            // in Dancelight, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
126
93
            let p = super::currency::CENTS;
127
93
            let q = 10 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
128
93
            smallvec![WeightToFeeCoefficient {
129
                degree: 1,
130
                negative: false,
131
                coeff_frac: Perbill::from_rational(p % q, q),
132
                coeff_integer: p / q,
133
            }]
134
93
        }
135
    }
136
}
137

            
138
/// System Parachains.
139
pub mod system_parachain {
140
    use {primitives::Id, xcm_builder::IsChildSystemParachain};
141

            
142
    /// Network's Asset Hub parachain ID.
143
    pub const ASSET_HUB_ID: u32 = 1000;
144
    /// Contracts parachain ID.
145
    pub const CONTRACTS_ID: u32 = 1002;
146
    /// Encointer parachain ID.
147
    pub const ENCOINTER_ID: u32 = 1003;
148
    /// People parachain ID.
149
    pub const PEOPLE_ID: u32 = 1004;
150
    /// BridgeHub parachain ID.
151
    pub const BRIDGE_HUB_ID: u32 = 1013;
152
    /// Brokerage parachain ID.
153
    pub const BROKER_ID: u32 = 1005;
154

            
155
    /// All system parachains of Dancelight.
156
    pub type SystemParachains = IsChildSystemParachain<Id>;
157
}
158

            
159
/// Dancelight Treasury pallet instance.
160
pub const TREASURY_PALLET_ID: u8 = 40;
161

            
162
#[cfg(test)]
163
mod tests {
164
    use {
165
        super::{
166
            currency::{CENTS, MILLICENTS},
167
            fee::WeightToFee,
168
        },
169
        crate::weights::ExtrinsicBaseWeight,
170
        frame_support::weights::WeightToFee as WeightToFeeT,
171
        runtime_common::MAXIMUM_BLOCK_WEIGHT,
172
    };
173

            
174
    #[test]
175
    // Test that the fee for `MAXIMUM_BLOCK_WEIGHT` of weight has sane bounds.
176
1
    fn full_block_fee_is_correct() {
177
1
        // A full block should cost between 1,000 and 10,000 CENTS.
178
1
        let full_block = WeightToFee::weight_to_fee(&MAXIMUM_BLOCK_WEIGHT);
179
1
        assert!(full_block >= 1_000 * CENTS);
180
1
        assert!(full_block <= 10_000 * CENTS);
181
1
    }
182

            
183
    #[test]
184
    // This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
185
1
    fn extrinsic_base_fee_is_correct() {
186
1
        // `ExtrinsicBaseWeight` should cost 1/10 of a CENT
187
1
        println!("Base: {}", ExtrinsicBaseWeight::get());
188
1
        let x = WeightToFee::weight_to_fee(&ExtrinsicBaseWeight::get());
189
1
        let y = CENTS / 10;
190
1
        assert!(x.max(y) - x.min(y) < MILLICENTS);
191
1
    }
192
}