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
		maintenance_mode: MaintenanceModeConfig {
165
2
			start_in_maintenance_mode: false,
166
2
			..Default::default()
167
2
		},
168
2
		// This should initialize it to whatever we have set in the pallet
169
2
		polkadot_xcm: PolkadotXcmConfig::default(),
170
2
		transaction_payment: TransactionPaymentConfig {
171
2
			multiplier: Multiplier::from(8u128),
172
2
			..Default::default()
173
2
		},
174
2
		moonbeam_orbiters: MoonbeamOrbitersConfig {
175
2
			min_orbiter_deposit: One::one(),
176
2
		},
177
2
		xcm_transactor: XcmTransactorConfig {
178
2
			relay_indices: moonbeam_relay_encoder::westend::WESTEND_RELAY_INDICES,
179
2
			..Default::default()
180
2
		},
181
2
	};
182
2

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

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

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

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