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
//! Moonriver Chain Specifications and utilities for building them.
18
//!
19
//! Learn more about Substrate chain specifications at
20
//! https://substrate.dev/docs/en/knowledgebase/integrate/chain-spec
21
extern crate alloc;
22

            
23
use crate::{
24
	currency::MOVR, AccountId, AuthorFilterConfig, AuthorMappingConfig, Balance, Balances,
25
	BalancesConfig, BridgePolkadotGrandpaConfig, BridgePolkadotMessagesConfig,
26
	BridgePolkadotParachainsConfig, BridgeXcmOverMoonbeamConfig, CrowdloanRewardsConfig, EVMConfig,
27
	EligibilityValue, EthereumChainIdConfig, EthereumConfig, EvmForeignAssetsConfig, InflationInfo,
28
	MaintenanceModeConfig, OpenTechCommitteeCollectiveConfig, ParachainInfoConfig,
29
	ParachainStakingConfig, PolkadotXcmConfig, Precompiles, Range, RuntimeGenesisConfig,
30
	TransactionPaymentConfig, TreasuryCouncilCollectiveConfig, XcmWeightTraderConfig, HOURS,
31
};
32
use alloc::{vec, vec::Vec};
33
use bp_messages::MessagesOperatingMode;
34
use bp_runtime::BasicOperatingMode;
35
use cumulus_primitives_core::ParaId;
36
use fp_evm::GenesisAccount;
37
use frame_support::pallet_prelude::PalletInfoAccess;
38
use nimbus_primitives::NimbusId;
39
use pallet_moonbeam_foreign_assets::EvmForeignAssetInfo;
40
use pallet_transaction_payment::Multiplier;
41
use pallet_xcm_weight_trader::XcmWeightTraderAssetInfo;
42
use sp_genesis_builder::PresetId;
43
use sp_keyring::Sr25519Keyring;
44
use sp_runtime::{Perbill, Percent};
45
use xcm::latest::{Junctions, Location, NetworkId};
46
use xcm::prelude::{GlobalConsensus, PalletInstance, Parachain};
47

            
48
const COLLATOR_COMMISSION: Perbill = Perbill::from_percent(20);
49
const PARACHAIN_BOND_RESERVE_PERCENT: Percent = Percent::from_percent(30);
50
const BLOCKS_PER_ROUND: u32 = 2 * HOURS;
51
const BLOCKS_PER_YEAR: u32 = 31_557_600 / 12;
52
const NUM_SELECTED_CANDIDATES: u32 = 8;
53

            
54
pub fn moonriver_inflation_config() -> InflationInfo<Balance> {
55
	fn to_round_inflation(annual: Range<Perbill>) -> Range<Perbill> {
56
		use pallet_parachain_staking::inflation::perbill_annual_to_perbill_round;
57
		perbill_annual_to_perbill_round(
58
			annual,
59
			// rounds per year
60
			BLOCKS_PER_YEAR / BLOCKS_PER_ROUND,
61
		)
62
	}
63
	let annual = Range {
64
		min: Perbill::from_percent(4),
65
		ideal: Perbill::from_percent(5),
66
		max: Perbill::from_percent(5),
67
	};
68
	InflationInfo {
69
		// staking expectations
70
		expect: Range {
71
			min: 100_000 * MOVR,
72
			ideal: 200_000 * MOVR,
73
			max: 500_000 * MOVR,
74
		},
75
		// annual inflation
76
		annual,
77
		round: to_round_inflation(annual),
78
	}
79
}
80

            
81
pub fn testnet_genesis(
82
	treasury_council_members: Vec<AccountId>,
83
	open_tech_committee_members: Vec<AccountId>,
84
	candidates: Vec<(AccountId, NimbusId, Balance)>,
85
	delegations: Vec<(AccountId, AccountId, Balance, Percent)>,
86
	endowed_accounts: Vec<AccountId>,
87
	crowdloan_fund_pot: Balance,
88
	para_id: ParaId,
89
	chain_id: u64,
90
) -> serde_json::Value {
91
	// This is the simplest bytecode to revert without returning any data.
92
	// We will pre-deploy it under all of our precompiles to ensure they can be called from
93
	// within contracts.
94
	// (PUSH1 0x00 PUSH1 0x00 REVERT)
95
	let revert_bytecode = vec![0x60, 0x00, 0x60, 0x00, 0xFD];
96

            
97
	let config = RuntimeGenesisConfig {
98
		system: Default::default(),
99
		balances: BalancesConfig {
100
			balances: endowed_accounts
101
				.iter()
102
				.cloned()
103
				.map(|k| (k, 1 << 80))
104
				.collect(),
105
		},
106
		crowdloan_rewards: CrowdloanRewardsConfig {
107
			funded_amount: crowdloan_fund_pot,
108
		},
109
		parachain_info: ParachainInfoConfig {
110
			parachain_id: para_id,
111
			..Default::default()
112
		},
113
		ethereum_chain_id: EthereumChainIdConfig {
114
			chain_id,
115
			..Default::default()
116
		},
117
		evm: EVMConfig {
118
			// We need _some_ code inserted at the precompile address so that
119
			// the evm will actually call the address.
120
			accounts: Precompiles::used_addresses()
121
				.map(|addr| {
122
					(
123
						addr.into(),
124
						GenesisAccount {
125
							nonce: Default::default(),
126
							balance: Default::default(),
127
							storage: Default::default(),
128
							code: revert_bytecode.clone(),
129
						},
130
					)
131
				})
132
				.collect(),
133
			..Default::default()
134
		},
135
		ethereum: EthereumConfig {
136
			..Default::default()
137
		},
138
		parachain_staking: ParachainStakingConfig {
139
			candidates: candidates
140
				.iter()
141
				.cloned()
142
				.map(|(account, _, bond)| (account, bond))
143
				.collect(),
144
			delegations,
145
			inflation_config: moonriver_inflation_config(),
146
			collator_commission: COLLATOR_COMMISSION,
147
			parachain_bond_reserve_percent: PARACHAIN_BOND_RESERVE_PERCENT,
148
			blocks_per_round: BLOCKS_PER_ROUND,
149
			num_selected_candidates: NUM_SELECTED_CANDIDATES,
150
		},
151
		treasury_council_collective: TreasuryCouncilCollectiveConfig {
152
			phantom: Default::default(),
153
			members: treasury_council_members,
154
		},
155
		open_tech_committee_collective: OpenTechCommitteeCollectiveConfig {
156
			phantom: Default::default(),
157
			members: open_tech_committee_members,
158
		},
159
		author_filter: AuthorFilterConfig {
160
			eligible_count: EligibilityValue::new_unchecked(50),
161
			..Default::default()
162
		},
163
		author_mapping: AuthorMappingConfig {
164
			mappings: candidates
165
				.iter()
166
				.cloned()
167
				.map(|(account_id, author_id, _)| (author_id, account_id))
168
				.collect(),
169
		},
170
		proxy_genesis_companion: Default::default(),
171
		treasury: Default::default(),
172
		maintenance_mode: MaintenanceModeConfig {
173
			start_in_maintenance_mode: false,
174
			..Default::default()
175
		},
176
		polkadot_xcm: PolkadotXcmConfig {
177
			supported_version: vec![
178
				// Required for bridging Moonriver with Moonbeam
179
				(
180
					bp_moonbeam::GlobalConsensusLocation::get(),
181
					xcm::latest::VERSION,
182
				),
183
			],
184
			..Default::default()
185
		},
186
		transaction_payment: TransactionPaymentConfig {
187
			multiplier: Multiplier::from(10u128),
188
			..Default::default()
189
		},
190
		evm_foreign_assets: EvmForeignAssetsConfig {
191
			assets: vec![EvmForeignAssetInfo {
192
				asset_id: 1001,
193
				name: b"xcGLMR".to_vec().try_into().expect("Invalid asset name"),
194
				symbol: b"xcGLMR".to_vec().try_into().expect("Invalid asset symbol"),
195
				decimals: 18,
196
				xcm_location: Location::new(
197
					2,
198
					[
199
						GlobalConsensus(crate::bridge_config::PolkadotGlobalConsensusNetwork::get()),
200
						Parachain(<bp_moonbeam::Moonbeam as bp_runtime::Parachain>::PARACHAIN_ID),
201
						PalletInstance(<Balances as PalletInfoAccess>::index() as u8),
202
					],
203
				),
204
			}],
205
			_phantom: Default::default(),
206
		},
207
		xcm_weight_trader: XcmWeightTraderConfig {
208
			assets: vec![XcmWeightTraderAssetInfo {
209
				location: Location::new(
210
					2,
211
					[
212
						GlobalConsensus(crate::bridge_config::PolkadotGlobalConsensusNetwork::get()),
213
						Parachain(<bp_moonbeam::Moonbeam as bp_runtime::Parachain>::PARACHAIN_ID),
214
						PalletInstance(<Balances as PalletInfoAccess>::index() as u8),
215
					],
216
				),
217
				relative_price: MOVR,
218
			}],
219
			_phantom: Default::default(),
220
		},
221
		bridge_polkadot_grandpa: BridgePolkadotGrandpaConfig {
222
			owner: Some(endowed_accounts[0]),
223
			init_data: None,
224
		},
225
		bridge_polkadot_parachains: BridgePolkadotParachainsConfig {
226
			owner: Some(endowed_accounts[0]),
227
			operating_mode: BasicOperatingMode::Normal,
228
			_phantom: Default::default(),
229
		},
230
		bridge_polkadot_messages: BridgePolkadotMessagesConfig {
231
			owner: Some(endowed_accounts[0]),
232
			opened_lanes: vec![],
233
			operating_mode: MessagesOperatingMode::Basic(BasicOperatingMode::Normal),
234
			_phantom: Default::default(),
235
		},
236
		bridge_xcm_over_moonbeam: BridgeXcmOverMoonbeamConfig {
237
			opened_bridges: vec![(
238
				Location::new(
239
					1,
240
					[Parachain(
241
						<bp_moonriver::Moonriver as bp_runtime::Parachain>::PARACHAIN_ID,
242
					)],
243
				),
244
				Junctions::from([
245
					NetworkId::Polkadot.into(),
246
					Parachain(<bp_moonbeam::Moonbeam as bp_runtime::Parachain>::PARACHAIN_ID),
247
				]),
248
				Some(Default::default()),
249
			)],
250
			_phantom: Default::default(),
251
		},
252
	};
253

            
254
	serde_json::to_value(&config).expect("Could not build genesis config.")
255
}
256

            
257
/// Generate a chain spec for use with the development service.
258
pub fn development() -> serde_json::Value {
259
	testnet_genesis(
260
		// Treasury Council members: Baltathar, Charleth and Dorothy
261
		vec![
262
			AccountId::from(sp_core::hex2array!(
263
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
264
			)),
265
			AccountId::from(sp_core::hex2array!(
266
				"798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc"
267
			)),
268
			AccountId::from(sp_core::hex2array!(
269
				"773539d4Ac0e786233D90A233654ccEE26a613D9"
270
			)),
271
		],
272
		// Open Tech committee members: Alith and Baltathar
273
		vec![
274
			AccountId::from(sp_core::hex2array!(
275
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
276
			)),
277
			AccountId::from(sp_core::hex2array!(
278
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
279
			)),
280
		],
281
		// Collator Candidate: Alice -> Alith
282
		vec![(
283
			AccountId::from(sp_core::hex2array!(
284
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
285
			)),
286
			NimbusId::from(Sr25519Keyring::Alice.public()),
287
			100_000 * MOVR,
288
		)],
289
		// Delegations
290
		vec![],
291
		vec![
292
			AccountId::from(sp_core::hex2array!(
293
				"f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
294
			)),
295
			AccountId::from(sp_core::hex2array!(
296
				"3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0"
297
			)),
298
			AccountId::from(sp_core::hex2array!(
299
				"798d4Ba9baf0064Ec19eB4F0a1a45785ae9D6DFc"
300
			)),
301
			AccountId::from(sp_core::hex2array!(
302
				"773539d4Ac0e786233D90A233654ccEE26a613D9"
303
			)),
304
		],
305
		3_000_000 * MOVR,
306
		Default::default(), // para_id
307
		1281,               //ChainId
308
	)
309
}
310

            
311
/// Provides the JSON representation of predefined genesis config for given `id`.
312
pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
313
	let patch = match id.as_str() {
314
		sp_genesis_builder::DEV_RUNTIME_PRESET => development(),
315
		_ => return None,
316
	};
317
	Some(
318
		serde_json::to_string(&patch)
319
			.expect("serialization to json is expected to work. qed.")
320
			.into_bytes(),
321
	)
322
}
323

            
324
/// List of supported presets.
325
pub fn preset_names() -> Vec<PresetId> {
326
	vec![PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET)]
327
}