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 TANSSI_GENESIS_HASH: [u8; 32] =
22
    hex_literal::hex!["dd6d086f75ec041b66e20c4186d327b23c8af244c534a2418de6574e8c041a60"];
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: 6 * 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 {
75
        frame_support::parameter_types,
76
        xcm::prelude::{Location, NetworkId},
77
    };
78

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

            
87
    }
88

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

            
95
    }
96
}
97

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

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

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

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

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

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

            
161
/// Starlight Treasury pallet instance.
162
pub const TREASURY_PALLET_ID: u8 = 40;
163

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

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

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