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
20
    pub const fn deposit(items: u32, bytes: u32) -> Balance {
42
20
        items as Balance * STORAGE_ITEM_FEE + (bytes as Balance) * STORAGE_BYTE_FEE
43
20
    }
44
}
45

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

            
52
    tp_traits::prod_or_fast_parameter_types! {
53
        pub const EpochDurationInBlocks: BlockNumber = { prod: 1 * HOURS, fast: 1 * MINUTES };
54
    }
55

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

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

            
69
pub mod snowbridge {
70
    use {
71
        frame_support::parameter_types,
72
        xcm::prelude::{Location, NetworkId},
73
    };
74

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

            
83
    }
84

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

            
91
    }
92
}
93

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

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

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

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

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

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

            
157
/// Dancelight Treasury pallet instance.
158
pub const TREASURY_PALLET_ID: u8 = 40;
159

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

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

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