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 CENTS: Balance = UNITS / 30_000;
36
    pub const GRAND: Balance = CENTS * 100_000;
37
    pub const MILLICENTS: Balance = CENTS / 1_000;
38
    pub const MICROUNITS: Balance = 1_000_000;
39
    pub const MILLIUNITS: Balance = 1_000_000_000;
40

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

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

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

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

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

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

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

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

            
86
    }
87

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

            
94
    }
95
}
96

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

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

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

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

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

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

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

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

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

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