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, xcm_config::Transactors, AccountId, AuthorFilterConfig, AuthorMappingConfig,
21
	Balance, BalancesConfig, CrowdloanRewardsConfig, EVMConfig, EligibilityValue,
22
	EthereumChainIdConfig, EthereumConfig, InflationInfo, MaintenanceModeConfig,
23
	MoonbeamOrbitersConfig, OpenTechCommitteeCollectiveConfig, ParachainInfoConfig,
24
	ParachainStakingConfig, PolkadotXcmConfig, Precompiles, Range, RuntimeGenesisConfig,
25
	SudoConfig, TransactionPaymentConfig, TreasuryCouncilCollectiveConfig, XcmTransactorConfig,
26
	HOURS,
27
};
28
use alloc::{vec, vec::Vec};
29
use cumulus_primitives_core::ParaId;
30
use fp_evm::GenesisAccount;
31
use frame_support::PalletId;
32
use nimbus_primitives::NimbusId;
33
use pallet_transaction_payment::Multiplier;
34
use sp_genesis_builder::PresetId;
35
use sp_keyring::Sr25519Keyring;
36
use sp_runtime::{traits::One, Perbill, Percent};
37

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

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

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

            
87
2
	// Fund the crowdloan pallet account with enough balance for rewards
88
2
	let crowdloan_pallet_account: AccountId =
89
2
		sp_runtime::traits::AccountIdConversion::into_account_truncating(&PalletId(*b"Crowdloa"));
90
2

            
91
2
	let mut balances: Vec<(AccountId, Balance)> = endowed_accounts
92
2
		.iter()
93
2
		.cloned()
94
22
		.map(|k| (k, 1 << 80))
95
2
		.collect();
96
2

            
97
2
	// Add crowdloan pallet account with sufficient funds for all rewards
98
2
	balances.push((crowdloan_pallet_account, 100_000_000 * UNIT));
99
2

            
100
2
	let config = RuntimeGenesisConfig {
101
2
		system: Default::default(),
102
2
		balances: BalancesConfig {
103
2
			balances,
104
2
			dev_accounts: Default::default(),
105
2
		},
106
2
		sudo: SudoConfig {
107
2
			key: Some(root_key),
108
2
		},
109
2
		parachain_info: ParachainInfoConfig {
110
2
			parachain_id: para_id,
111
2
			..Default::default()
112
2
		},
113
2
		ethereum_chain_id: EthereumChainIdConfig {
114
2
			chain_id,
115
2
			..Default::default()
116
2
		},
117
2
		evm: EVMConfig {
118
2
			// We need _some_ code inserted at the precompile address so that
119
2
			// the evm will actually call the address.
120
2
			accounts: Precompiles::used_addresses()
121
98
				.map(|addr| {
122
98
					(
123
98
						addr.into(),
124
98
						GenesisAccount {
125
98
							nonce: Default::default(),
126
98
							balance: Default::default(),
127
98
							storage: Default::default(),
128
98
							code: revert_bytecode.clone(),
129
98
						},
130
98
					)
131
98
				})
132
2
				.collect(),
133
2
			..Default::default()
134
2
		},
135
2
		ethereum: EthereumConfig {
136
2
			..Default::default()
137
2
		},
138
2
		parachain_staking: ParachainStakingConfig {
139
2
			candidates: candidates
140
2
				.iter()
141
2
				.cloned()
142
2
				.map(|(account, _, bond)| (account, bond))
143
2
				.collect(),
144
2
			delegations,
145
2
			inflation_config: moonbase_inflation_config(),
146
2
			collator_commission: COLLATOR_COMMISSION,
147
2
			parachain_bond_reserve_percent: PARACHAIN_BOND_RESERVE_PERCENT,
148
2
			blocks_per_round: BLOCKS_PER_ROUND,
149
2
			num_selected_candidates: NUM_SELECTED_CANDIDATES,
150
2
		},
151
2
		treasury_council_collective: TreasuryCouncilCollectiveConfig {
152
2
			phantom: Default::default(),
153
2
			members: treasury_council_members,
154
2
		},
155
2
		open_tech_committee_collective: OpenTechCommitteeCollectiveConfig {
156
2
			phantom: Default::default(),
157
2
			members: open_tech_committee_members,
158
2
		},
159
2
		author_filter: AuthorFilterConfig {
160
2
			eligible_count: EligibilityValue::new_unchecked(50),
161
2
			..Default::default()
162
2
		},
163
2
		author_mapping: AuthorMappingConfig {
164
2
			mappings: candidates
165
2
				.iter()
166
2
				.cloned()
167
2
				.map(|(account_id, author_id, _)| (author_id, account_id))
168
2
				.collect(),
169
2
		},
170
2
		proxy_genesis_companion: Default::default(),
171
2
		treasury: Default::default(),
172
2
		maintenance_mode: MaintenanceModeConfig {
173
2
			start_in_maintenance_mode: false,
174
2
			..Default::default()
175
2
		},
176
2
		// This should initialize it to whatever we have set in the pallet
177
2
		polkadot_xcm: PolkadotXcmConfig::default(),
178
2
		transaction_payment: TransactionPaymentConfig {
179
2
			multiplier: Multiplier::from(8u128),
180
2
			..Default::default()
181
2
		},
182
2
		moonbeam_orbiters: MoonbeamOrbitersConfig {
183
2
			min_orbiter_deposit: One::one(),
184
2
		},
185
2
		xcm_transactor: XcmTransactorConfig {
186
2
			chain_indices_map: vec![
187
2
				(
188
2
					Transactors::Relay,
189
2
					pallet_xcm_transactor::chain_indices::ChainIndices::Relay(
190
2
						moonbeam_relay_encoder::westend::WESTEND_RELAY_INDICES,
191
2
					),
192
2
				),
193
2
				(
194
2
					Transactors::AssetHub,
195
2
					pallet_xcm_transactor::chain_indices::ChainIndices::AssetHub(
196
2
						moonbeam_assethub_encoder::westend::WESTEND_ASSETHUB_INDICES,
197
2
					),
198
2
				),
199
2
			],
200
2
			..Default::default()
201
2
		},
202
2
		crowdloan_rewards: CrowdloanRewardsConfig {
203
2
			funded_accounts: vec![
204
2
				// Dorothy account with test rewards
205
2
				(
206
2
					[
207
2
						0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
208
2
						0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
209
2
						0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
210
2
					],
211
2
					Some(AccountId::from(sp_core::hex2array!(
212
2
						"773539d4Ac0e786233D90A233654ccEE26a613D9"
213
2
					))),
214
2
					3_000_000 * UNIT,
215
2
				),
216
2
			],
217
2
			init_vesting_block: 0u32,
218
2
			end_vesting_block: 201600u32,
219
2
		},
220
2
	};
221
2

            
222
2
	serde_json::to_value(&config).expect("Could not build genesis config.")
223
2
}
224

            
225
pub fn development() -> serde_json::Value {
226
	testnet_genesis(
227
		// Alith is Sudo
228
		AccountId::from(sp_core::hex2array!(
229
			"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
230
		)),
231
		// Treasury Council members: Baltathar, Charleth and Dorothy
232
		vec![
233
			AccountId::from(sp_core::hex2array!(
234
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
235
			)),
236
			AccountId::from(sp_core::hex2array!(
237
				"798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc"
238
			)),
239
			AccountId::from(sp_core::hex2array!(
240
				"773539d4Ac0e786233D90A233654ccEE26a613D9"
241
			)),
242
		],
243
		// Open Tech committee members: Alith and Baltathar
244
		vec![
245
			AccountId::from(sp_core::hex2array!(
246
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
247
			)),
248
			AccountId::from(sp_core::hex2array!(
249
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
250
			)),
251
		],
252
		// Collator Candidates
253
		vec![(
254
			// Alice -> Alith
255
			AccountId::from(sp_core::hex2array!(
256
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
257
			)),
258
			NimbusId::from(Sr25519Keyring::Alice.public()),
259
			1_000 * UNIT,
260
		)],
261
		// Delegations
262
		vec![],
263
		// Endowed: Alith, Baltathar, Charleth and Dorothy
264
		vec![
265
			AccountId::from(sp_core::hex2array!(
266
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
267
			)),
268
			AccountId::from(sp_core::hex2array!(
269
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
270
			)),
271
			AccountId::from(sp_core::hex2array!(
272
				"798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc"
273
			)),
274
			AccountId::from(sp_core::hex2array!(
275
				"773539d4Ac0e786233D90A233654ccEE26a613D9"
276
			)),
277
		],
278
		Default::default(), // para_id
279
		1280,               //ChainId
280
	)
281
}
282

            
283
/// Provides the JSON representation of predefined genesis config for given `id`.
284
pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
285
	let patch = match id.as_str() {
286
		sp_genesis_builder::DEV_RUNTIME_PRESET => development(),
287
		_ => return None,
288
	};
289
	Some(
290
		serde_json::to_string(&patch)
291
			.expect("serialization to json is expected to work. qed.")
292
			.into_bytes(),
293
	)
294
}
295

            
296
/// List of supported presets.
297
pub fn preset_names() -> Vec<PresetId> {
298
	vec![PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET)]
299
}