referrerpolicy=no-referrer-when-downgrade

yet_another_parachain_runtime/
genesis_config_presets.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Yet-Another-Parachain Runtime genesis config presets
17
18use crate::*;
19use alloc::{vec, vec::Vec};
20use cumulus_primitives_core::ParaId;
21use frame_support::build_struct_json_patch;
22use parachains_common::{AccountId, AuraId};
23use sp_genesis_builder::PresetId;
24use sp_keyring::Sr25519Keyring;
25
26const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
27
28const DEFAULT_PARA_ID: ParaId = ParaId::new(1000);
29const ENDOWMENT: u128 = 1 << 60;
30
31fn yap_parachain_genesis(
32	root_key: AccountId,
33	initial_authorities: Vec<AuraId>,
34	endowed_accounts: Vec<AccountId>,
35	endowment: Balance,
36	id: ParaId,
37) -> serde_json::Value {
38	build_struct_json_patch!(RuntimeGenesisConfig {
39		aura: AuraConfig { authorities: initial_authorities },
40		balances: BalancesConfig {
41			balances: endowed_accounts.iter().cloned().map(|k| (k, endowment)).collect(),
42		},
43		parachain_info: ParachainInfoConfig { parachain_id: id },
44		polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
45		sudo: SudoConfig { key: Some(root_key) }
46	})
47}
48
49/// Provides the JSON representation of predefined genesis config for given `id`.
50pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
51	let genesis_fn = |authorities| {
52		yap_parachain_genesis(
53			Sr25519Keyring::Alice.to_account_id(),
54			authorities,
55			Sr25519Keyring::well_known().map(|x| x.to_account_id()).collect(),
56			ENDOWMENT,
57			DEFAULT_PARA_ID,
58		)
59	};
60
61	let patch = match id.as_ref() {
62		sp_genesis_builder::DEV_RUNTIME_PRESET =>
63			genesis_fn(vec![Sr25519Keyring::Alice.public().into()]),
64		sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET => genesis_fn(vec![
65			Sr25519Keyring::Alice.public().into(),
66			Sr25519Keyring::Bob.public().into(),
67		]),
68		_ => return None,
69	};
70
71	Some(
72		serde_json::to_string(&patch)
73			.expect("serialization to json is expected to work. qed.")
74			.into_bytes(),
75	)
76}
77
78/// List of supported presets.
79pub fn preset_names() -> Vec<PresetId> {
80	vec![
81		PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET),
82		PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET),
83	]
84}