1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam 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
// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
extern crate alloc;
18

            
19
use crate::{
20
	currency::UNIT, AccountId, AuthorFilterConfig, AuthorMappingConfig, Balance, BalancesConfig,
21
	CrowdloanRewardsConfig, EVMConfig, EligibilityValue, EthereumChainIdConfig, EthereumConfig,
22
	InflationInfo, MaintenanceModeConfig, MoonbeamOrbitersConfig,
23
	OpenTechCommitteeCollectiveConfig, ParachainInfoConfig, ParachainStakingConfig,
24
	PolkadotXcmConfig, Precompiles, Range, RuntimeGenesisConfig, SudoConfig,
25
	TransactionPaymentConfig, TreasuryCouncilCollectiveConfig, XcmTransactorConfig, HOURS,
26
};
27
use alloc::{vec, vec::Vec};
28
use cumulus_primitives_core::ParaId;
29
use fp_evm::GenesisAccount;
30
use nimbus_primitives::NimbusId;
31
use pallet_transaction_payment::Multiplier;
32
use sp_genesis_builder::PresetId;
33
use sp_keyring::Sr25519Keyring;
34
use sp_runtime::{traits::One, Perbill, Percent};
35

            
36
const COLLATOR_COMMISSION: Perbill = Perbill::from_percent(20);
37
const PARACHAIN_BOND_RESERVE_PERCENT: Percent = Percent::from_percent(30);
38
const BLOCKS_PER_ROUND: u32 = 2 * HOURS;
39
const BLOCKS_PER_YEAR: u32 = 31_557_600 / 6;
40
const NUM_SELECTED_CANDIDATES: u32 = 8;
41

            
42
2
pub fn moonbase_inflation_config() -> InflationInfo<Balance> {
43
2
	fn to_round_inflation(annual: Range<Perbill>) -> Range<Perbill> {
44
		use pallet_parachain_staking::inflation::perbill_annual_to_perbill_round;
45
2
		perbill_annual_to_perbill_round(
46
2
			annual,
47
2
			// rounds per year
48
2
			BLOCKS_PER_YEAR / BLOCKS_PER_ROUND,
49
2
		)
50
2
	}
51
2
	let annual = Range {
52
2
		min: Perbill::from_percent(4),
53
2
		ideal: Perbill::from_percent(5),
54
2
		max: Perbill::from_percent(5),
55
2
	};
56
2
	InflationInfo {
57
2
		// staking expectations
58
2
		expect: Range {
59
2
			min: 100_000 * UNIT,
60
2
			ideal: 200_000 * UNIT,
61
2
			max: 500_000 * UNIT,
62
2
		},
63
2
		// annual inflation
64
2
		annual,
65
2
		round: to_round_inflation(annual),
66
2
	}
67
2
}
68

            
69
2
pub fn testnet_genesis(
70
2
	root_key: AccountId,
71
2
	treasury_council_members: Vec<AccountId>,
72
2
	open_tech_committee_members: Vec<AccountId>,
73
2
	candidates: Vec<(AccountId, NimbusId, Balance)>,
74
2
	delegations: Vec<(AccountId, AccountId, Balance, Percent)>,
75
2
	endowed_accounts: Vec<AccountId>,
76
2
	crowdloan_fund_pot: Balance,
77
2
	para_id: ParaId,
78
2
	chain_id: u64,
79
2
) -> serde_json::Value {
80
2
	// This is the simplest bytecode to revert without returning any data.
81
2
	// We will pre-deploy it under all of our precompiles to ensure they can be called from
82
2
	// within contracts.
83
2
	// (PUSH1 0x00 PUSH1 0x00 REVERT)
84
2
	let revert_bytecode = vec![0x60, 0x00, 0x60, 0x00, 0xFD];
85
2

            
86
2
	let config = RuntimeGenesisConfig {
87
2
		system: Default::default(),
88
2
		balances: BalancesConfig {
89
2
			balances: endowed_accounts
90
2
				.iter()
91
2
				.cloned()
92
22
				.map(|k| (k, 1 << 80))
93
2
				.collect(),
94
2
		},
95
2
		crowdloan_rewards: CrowdloanRewardsConfig {
96
2
			funded_amount: crowdloan_fund_pot,
97
2
		},
98
2
		sudo: SudoConfig {
99
2
			key: Some(root_key),
100
2
		},
101
2
		parachain_info: ParachainInfoConfig {
102
2
			parachain_id: para_id,
103
2
			..Default::default()
104
2
		},
105
2
		ethereum_chain_id: EthereumChainIdConfig {
106
2
			chain_id,
107
2
			..Default::default()
108
2
		},
109
2
		evm: EVMConfig {
110
2
			// We need _some_ code inserted at the precompile address so that
111
2
			// the evm will actually call the address.
112
2
			accounts: Precompiles::used_addresses()
113
112
				.map(|addr| {
114
112
					(
115
112
						addr.into(),
116
112
						GenesisAccount {
117
112
							nonce: Default::default(),
118
112
							balance: Default::default(),
119
112
							storage: Default::default(),
120
112
							code: revert_bytecode.clone(),
121
112
						},
122
112
					)
123
112
				})
124
2
				.collect(),
125
2
			..Default::default()
126
2
		},
127
2
		ethereum: EthereumConfig {
128
2
			..Default::default()
129
2
		},
130
2
		parachain_staking: ParachainStakingConfig {
131
2
			candidates: candidates
132
2
				.iter()
133
2
				.cloned()
134
2
				.map(|(account, _, bond)| (account, bond))
135
2
				.collect(),
136
2
			delegations,
137
2
			inflation_config: moonbase_inflation_config(),
138
2
			collator_commission: COLLATOR_COMMISSION,
139
2
			parachain_bond_reserve_percent: PARACHAIN_BOND_RESERVE_PERCENT,
140
2
			blocks_per_round: BLOCKS_PER_ROUND,
141
2
			num_selected_candidates: NUM_SELECTED_CANDIDATES,
142
2
		},
143
2
		treasury_council_collective: TreasuryCouncilCollectiveConfig {
144
2
			phantom: Default::default(),
145
2
			members: treasury_council_members,
146
2
		},
147
2
		open_tech_committee_collective: OpenTechCommitteeCollectiveConfig {
148
2
			phantom: Default::default(),
149
2
			members: open_tech_committee_members,
150
2
		},
151
2
		author_filter: AuthorFilterConfig {
152
2
			eligible_count: EligibilityValue::new_unchecked(50),
153
2
			..Default::default()
154
2
		},
155
2
		author_mapping: AuthorMappingConfig {
156
2
			mappings: candidates
157
2
				.iter()
158
2
				.cloned()
159
2
				.map(|(account_id, author_id, _)| (author_id, account_id))
160
2
				.collect(),
161
2
		},
162
2
		proxy_genesis_companion: Default::default(),
163
2
		treasury: Default::default(),
164
2
		migrations: Default::default(),
165
2
		maintenance_mode: MaintenanceModeConfig {
166
2
			start_in_maintenance_mode: false,
167
2
			..Default::default()
168
2
		},
169
2
		// This should initialize it to whatever we have set in the pallet
170
2
		polkadot_xcm: PolkadotXcmConfig::default(),
171
2
		transaction_payment: TransactionPaymentConfig {
172
2
			multiplier: Multiplier::from(8u128),
173
2
			..Default::default()
174
2
		},
175
2
		moonbeam_orbiters: MoonbeamOrbitersConfig {
176
2
			min_orbiter_deposit: One::one(),
177
2
		},
178
2
		xcm_transactor: XcmTransactorConfig {
179
2
			relay_indices: moonbeam_relay_encoder::westend::WESTEND_RELAY_INDICES,
180
2
			..Default::default()
181
2
		},
182
2
	};
183
2

            
184
2
	serde_json::to_value(&config).expect("Could not build genesis config.")
185
2
}
186

            
187
pub fn development() -> serde_json::Value {
188
	testnet_genesis(
189
		// Alith is Sudo
190
		AccountId::from(sp_core::hex2array!(
191
			"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
192
		)),
193
		// Treasury Council members: Baltathar, Charleth and Dorothy
194
		vec![
195
			AccountId::from(sp_core::hex2array!(
196
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
197
			)),
198
			AccountId::from(sp_core::hex2array!(
199
				"798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc"
200
			)),
201
			AccountId::from(sp_core::hex2array!(
202
				"773539d4Ac0e786233D90A233654ccEE26a613D9"
203
			)),
204
		],
205
		// Open Tech committee members: Alith and Baltathar
206
		vec![
207
			AccountId::from(sp_core::hex2array!(
208
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
209
			)),
210
			AccountId::from(sp_core::hex2array!(
211
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
212
			)),
213
		],
214
		// Collator Candidates
215
		vec![(
216
			// Alice -> Alith
217
			AccountId::from(sp_core::hex2array!(
218
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
219
			)),
220
			NimbusId::from(Sr25519Keyring::Alice.public()),
221
			1_000 * UNIT,
222
		)],
223
		// Delegations
224
		vec![],
225
		// Endowed: Alith, Baltathar, Charleth and Dorothy
226
		vec![
227
			AccountId::from(sp_core::hex2array!(
228
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
229
			)),
230
			AccountId::from(sp_core::hex2array!(
231
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
232
			)),
233
			AccountId::from(sp_core::hex2array!(
234
				"798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc"
235
			)),
236
			AccountId::from(sp_core::hex2array!(
237
				"773539d4Ac0e786233D90A233654ccEE26a613D9"
238
			)),
239
		],
240
		3_000_000 * UNIT,
241
		Default::default(), // para_id
242
		1280,               //ChainId
243
	)
244
}
245

            
246
/// Provides the JSON representation of predefined genesis config for given `id`.
247
pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
248
	let patch = match id.as_str() {
249
		sp_genesis_builder::DEV_RUNTIME_PRESET => development(),
250
		_ => return None,
251
	};
252
	Some(
253
		serde_json::to_string(&patch)
254
			.expect("serialization to json is expected to work. qed.")
255
			.into_bytes(),
256
	)
257
}
258

            
259
/// List of supported presets.
260
pub fn preset_names() -> Vec<PresetId> {
261
	vec![PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET)]
262
}