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
pub const DANCELIGHT_GENESIS_HASH: [u8; 32] =
22
    hex_literal::hex!["983a1a72503d6cc3636776747ec627172b51272bf45e50a355348facb67a820a"];
23

            
24
/// Money matters.
25
pub mod currency {
26
    use primitives::Balance;
27

            
28
    /// The existential deposit.
29
    pub const EXISTENTIAL_DEPOSIT: Balance = 1 * CENTS;
30

            
31
    // Provide a common factor between runtimes based on a supply of 10_000_000 tokens.
32
    pub const SUPPLY_FACTOR: Balance = 1;
33

            
34
    pub const UNITS: Balance = 1_000_000_000_000;
35
    pub const DECIMALS: u32 = 12;
36
    pub const CENTS: Balance = UNITS / 30_000;
37
    pub const GRAND: Balance = CENTS * 100_000;
38
    pub const MILLICENTS: Balance = CENTS / 1_000;
39
    pub const MICROUNITS: Balance = 1_000_000;
40
    pub const MILLIUNITS: Balance = 1_000_000_000;
41

            
42
    pub const STORAGE_BYTE_FEE: Balance = 100 * MICROUNITS * SUPPLY_FACTOR;
43
    pub const STORAGE_ITEM_FEE: Balance = 100 * MILLIUNITS * SUPPLY_FACTOR;
44

            
45
44
    pub const fn deposit(items: u32, bytes: u32) -> Balance {
46
44
        items as Balance * STORAGE_ITEM_FEE + (bytes as Balance) * STORAGE_BYTE_FEE
47
44
    }
48
}
49

            
50
/// Time and blocks.
51
pub mod time {
52
    use primitives::{BlockNumber, Moment};
53
    pub const MILLISECS_PER_BLOCK: Moment = 6000;
54
    pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK;
55

            
56
    tp_traits::prod_or_fast_parameter_types! {
57
        pub const EpochDurationInBlocks: BlockNumber = { prod: 1 * HOURS, fast: 1 * MINUTES };
58
    }
59

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

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

            
73
pub mod snowbridge {
74
    use xcm::prelude::InteriorLocation;
75
    use {
76
        frame_support::parameter_types,
77
        xcm::prelude::{Location, NetworkId},
78
    };
79

            
80
    parameter_types! {
81
            /// Network and location for the Ethereum chain. On Stagelight, the Ethereum chain bridged
82
            /// to is the Sepolia Ethereum testnet, with chain ID 11155111.
83
            /// <https://chainlist.org/chain/11155111>
84
            /// <https://ethereum.org/en/developers/docs/apis/json-rpc/#net_version>
85
            pub EthereumNetwork: NetworkId = NetworkId::Ethereum { chain_id: 11155111 };
86
            pub EthereumUniversalLocation: InteriorLocation = EthereumNetwork::get().into();
87
            pub EthereumLocation: Location = EthereumUniversalLocation::get().into_exterior(1);
88

            
89
    }
90

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

            
97
    }
98
}
99

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

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

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

            
142
/// System Parachains.
143
pub mod system_parachain {
144
    use {primitives::Id, xcm_builder::IsChildSystemParachain};
145

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

            
159
    /// All system parachains of Dancelight.
160
    pub type SystemParachains = IsChildSystemParachain<Id>;
161
}
162

            
163
/// Dancelight Treasury pallet instance.
164
pub const TREASURY_PALLET_ID: u8 = 40;
165

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

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

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