referrerpolicy=no-referrer-when-downgrade

asset_hub_westend_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//! # Asset Hub Westend Runtime genesis config presets
17
18use crate::{
19	xcm_config::{bridging::to_rococo::RococoNetwork, UniversalLocation},
20	*,
21};
22use alloc::{vec, vec::Vec};
23use cumulus_primitives_core::ParaId;
24use frame_support::build_struct_json_patch;
25use hex_literal::hex;
26use parachains_common::{AccountId, AuraId};
27use sp_core::crypto::UncheckedInto;
28use sp_genesis_builder::PresetId;
29use sp_keyring::Sr25519Keyring;
30use testnet_parachains_constants::westend::{
31	currency::UNITS as WND, xcm_version::SAFE_XCM_VERSION,
32};
33use xcm::latest::prelude::*;
34use xcm_builder::GlobalConsensusConvertsFor;
35use xcm_executor::traits::ConvertLocation;
36
37const ASSET_HUB_WESTEND_ED: Balance = ExistentialDeposit::get();
38
39fn asset_hub_westend_genesis(
40	invulnerables: Vec<(AccountId, AuraId)>,
41	endowed_accounts: Vec<AccountId>,
42	endowment: Balance,
43	dev_stakers: Option<(u32, u32)>,
44	id: ParaId,
45	foreign_assets: Vec<(Location, AccountId, Balance)>,
46	foreign_assets_endowed_accounts: Vec<(Location, AccountId, Balance)>,
47) -> serde_json::Value {
48	build_struct_json_patch!(RuntimeGenesisConfig {
49		balances: BalancesConfig {
50			balances: endowed_accounts.iter().cloned().map(|k| (k, endowment)).collect(),
51		},
52		parachain_info: ParachainInfoConfig { parachain_id: id },
53		collator_selection: CollatorSelectionConfig {
54			invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
55			candidacy_bond: ASSET_HUB_WESTEND_ED * 16,
56		},
57		session: SessionConfig {
58			keys: invulnerables
59				.into_iter()
60				.map(|(acc, aura)| {
61					(
62						acc.clone(),          // account id
63						acc,                  // validator id
64						SessionKeys { aura }, // session keys
65					)
66				})
67				.collect(),
68		},
69		polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
70		staking: StakingConfig {
71			stakers: vec![
72				(
73					Sr25519Keyring::AliceStash.to_account_id(),
74					1000 * ASSET_HUB_WESTEND_ED,
75					pallet_staking_async::StakerStatus::Validator,
76				),
77				(
78					Sr25519Keyring::BobStash.to_account_id(),
79					1000 * ASSET_HUB_WESTEND_ED,
80					pallet_staking_async::StakerStatus::Validator,
81				),
82			],
83			dev_stakers,
84			..Default::default()
85		},
86		foreign_assets: ForeignAssetsConfig {
87			assets: foreign_assets
88				.into_iter()
89				.map(|asset| (asset.0.try_into().unwrap(), asset.1, false, asset.2))
90				.collect(),
91			accounts: foreign_assets_endowed_accounts
92				.into_iter()
93				.map(|asset| (asset.0.try_into().unwrap(), asset.1, asset.2))
94				.collect(),
95			..Default::default()
96		}
97	})
98}
99
100/// Encapsulates names of predefined presets.
101mod preset_names {
102	pub const PRESET_GENESIS: &str = "genesis";
103}
104
105/// Provides the JSON representation of predefined genesis config for given `id`.
106pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
107	use preset_names::*;
108	let dev_stakers = Some((0, 25_000));
109	let patch = match id.as_ref() {
110		PRESET_GENESIS => asset_hub_westend_genesis(
111			// initial collators.
112			vec![
113				(
114					hex!("9cfd429fa002114f33c1d3e211501d62830c9868228eb3b4b8ae15a83de04325").into(),
115					hex!("9cfd429fa002114f33c1d3e211501d62830c9868228eb3b4b8ae15a83de04325")
116						.unchecked_into(),
117				),
118				(
119					hex!("12a03fb4e7bda6c9a07ec0a11d03c24746943e054ff0bb04938970104c783876").into(),
120					hex!("12a03fb4e7bda6c9a07ec0a11d03c24746943e054ff0bb04938970104c783876")
121						.unchecked_into(),
122				),
123				(
124					hex!("1256436307dfde969324e95b8c62cb9101f520a39435e6af0f7ac07b34e1931f").into(),
125					hex!("1256436307dfde969324e95b8c62cb9101f520a39435e6af0f7ac07b34e1931f")
126						.unchecked_into(),
127				),
128				(
129					hex!("98102b7bca3f070f9aa19f58feed2c0a4e107d203396028ec17a47e1ed80e322").into(),
130					hex!("98102b7bca3f070f9aa19f58feed2c0a4e107d203396028ec17a47e1ed80e322")
131						.unchecked_into(),
132				),
133			],
134			Vec::new(),
135			ASSET_HUB_WESTEND_ED * 4096,
136			None,
137			1000.into(),
138			vec![],
139			vec![],
140		),
141		sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET =>
142			asset_hub_westend_genesis(
143				// initial collators.
144				vec![
145					(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
146					(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
147				],
148				Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
149				WND * 1_000_000,
150				dev_stakers,
151				1000.into(),
152				vec![
153			// bridged ROC
154			(
155				Location::new(2, [GlobalConsensus(RococoNetwork::get())]),
156				GlobalConsensusConvertsFor::<UniversalLocation, AccountId>::convert_location(
157					&Location { parents: 2, interior: [GlobalConsensus(RococoNetwork::get())].into() },
158				)
159				.unwrap(),
160				10_000_000,
161			),
162			],
163				vec![
164					// bridged ROC to Bob
165					(
166						Location::new(2, [GlobalConsensus(RococoNetwork::get())]),
167						Sr25519Keyring::Bob.to_account_id(),
168						10_000_000 * 4096 * 4096,
169					),
170				],
171			),
172		sp_genesis_builder::DEV_RUNTIME_PRESET => asset_hub_westend_genesis(
173			// initial collators.
174			vec![(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into())],
175			vec![
176				Sr25519Keyring::Alice.to_account_id(),
177				Sr25519Keyring::Bob.to_account_id(),
178				Sr25519Keyring::AliceStash.to_account_id(),
179				Sr25519Keyring::BobStash.to_account_id(),
180			],
181			WND * 1_000_000,
182			dev_stakers,
183			1000.into(),
184			vec![],
185			vec![],
186		),
187		_ => return None,
188	};
189
190	Some(
191		serde_json::to_string(&patch)
192			.expect("serialization to json is expected to work. qed.")
193			.into_bytes(),
194	)
195}
196
197/// List of supported presets.
198pub fn preset_names() -> Vec<PresetId> {
199	use preset_names::*;
200	vec![
201		PresetId::from(PRESET_GENESIS),
202		PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET),
203		PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET),
204	]
205}