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 frame_support::PalletId;
31
use nimbus_primitives::NimbusId;
32
use pallet_transaction_payment::Multiplier;
33
use sp_genesis_builder::PresetId;
34
use sp_keyring::Sr25519Keyring;
35
use sp_runtime::{traits::One, Perbill, Percent};
36

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

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

            
70
2
pub fn testnet_genesis(
71
2
	root_key: AccountId,
72
2
	treasury_council_members: Vec<AccountId>,
73
2
	open_tech_committee_members: Vec<AccountId>,
74
2
	candidates: Vec<(AccountId, NimbusId, Balance)>,
75
2
	delegations: Vec<(AccountId, AccountId, Balance, Percent)>,
76
2
	endowed_accounts: Vec<AccountId>,
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
	// Fund the crowdloan pallet account with enough balance for rewards
87
2
	let crowdloan_pallet_account: AccountId =
88
2
		sp_runtime::traits::AccountIdConversion::into_account_truncating(&PalletId(*b"Crowdloa"));
89
2

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

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

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

            
208
2
	serde_json::to_value(&config).expect("Could not build genesis config.")
209
2
}
210

            
211
pub fn development() -> serde_json::Value {
212
	testnet_genesis(
213
		// Alith is Sudo
214
		AccountId::from(sp_core::hex2array!(
215
			"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
216
		)),
217
		// Treasury Council members: Baltathar, Charleth and Dorothy
218
		vec![
219
			AccountId::from(sp_core::hex2array!(
220
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
221
			)),
222
			AccountId::from(sp_core::hex2array!(
223
				"798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc"
224
			)),
225
			AccountId::from(sp_core::hex2array!(
226
				"773539d4Ac0e786233D90A233654ccEE26a613D9"
227
			)),
228
		],
229
		// Open Tech committee members: Alith and Baltathar
230
		vec![
231
			AccountId::from(sp_core::hex2array!(
232
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
233
			)),
234
			AccountId::from(sp_core::hex2array!(
235
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
236
			)),
237
		],
238
		// Collator Candidates
239
		vec![(
240
			// Alice -> Alith
241
			AccountId::from(sp_core::hex2array!(
242
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
243
			)),
244
			NimbusId::from(Sr25519Keyring::Alice.public()),
245
			1_000 * UNIT,
246
		)],
247
		// Delegations
248
		vec![],
249
		// Endowed: Alith, Baltathar, Charleth and Dorothy
250
		vec![
251
			AccountId::from(sp_core::hex2array!(
252
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
253
			)),
254
			AccountId::from(sp_core::hex2array!(
255
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
256
			)),
257
			AccountId::from(sp_core::hex2array!(
258
				"798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc"
259
			)),
260
			AccountId::from(sp_core::hex2array!(
261
				"773539d4Ac0e786233D90A233654ccEE26a613D9"
262
			)),
263
		],
264
		Default::default(), // para_id
265
		1280,               //ChainId
266
	)
267
}
268

            
269
/// Provides the JSON representation of predefined genesis config for given `id`.
270
pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
271
	let patch = match id.as_str() {
272
		sp_genesis_builder::DEV_RUNTIME_PRESET => development(),
273
		_ => return None,
274
	};
275
	Some(
276
		serde_json::to_string(&patch)
277
			.expect("serialization to json is expected to work. qed.")
278
			.into_bytes(),
279
	)
280
}
281

            
282
/// List of supported presets.
283
pub fn preset_names() -> Vec<PresetId> {
284
	vec![PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET)]
285
}