1234567891011121314151617use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};18use sc_service::ChainType;19use sp_core::{sr25519, Pair, Public};20use sp_runtime::traits::{IdentifyAccount, Verify};21use std::collections::BTreeMap;2223use serde::{Deserialize, Serialize};24use serde_json::map::Map;2526use up_common::types::opaque::*;27use up_common::constants::EXISTENTIAL_DEPOSIT;2829#[cfg(feature = "unique-runtime")]30pub use unique_runtime as default_runtime;3132#[cfg(all(not(feature = "unique-runtime"), feature = "quartz-runtime"))]33pub use quartz_runtime as default_runtime;3435#[cfg(all(not(feature = "unique-runtime"), not(feature = "quartz-runtime")))]36pub use opal_runtime as default_runtime;373839#[cfg(feature = "unique-runtime")]40pub type UniqueChainSpec = sc_service::GenericChainSpec<unique_runtime::GenesisConfig, Extensions>;414243#[cfg(feature = "quartz-runtime")]44pub type QuartzChainSpec = sc_service::GenericChainSpec<quartz_runtime::GenesisConfig, Extensions>;454647pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;4849#[cfg(feature = "unique-runtime")]50pub type DefaultChainSpec = UniqueChainSpec;5152#[cfg(all(not(feature = "unique-runtime"), feature = "quartz-runtime"))]53pub type DefaultChainSpec = QuartzChainSpec;5455#[cfg(all(not(feature = "unique-runtime"), not(feature = "quartz-runtime")))]56pub type DefaultChainSpec = OpalChainSpec;5758pub enum RuntimeId {59 #[cfg(feature = "unique-runtime")]60 Unique,6162 #[cfg(feature = "quartz-runtime")]63 Quartz,6465 Opal,66 Unknown(String),67}6869pub trait RuntimeIdentification {70 fn runtime_id(&self) -> RuntimeId;71}7273impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {74 fn runtime_id(&self) -> RuntimeId {75 #[cfg(feature = "unique-runtime")]76 if self.id().starts_with("unique") || self.id().starts_with("unq") {77 return RuntimeId::Unique;78 }7980 #[cfg(feature = "quartz-runtime")]81 if self.id().starts_with("quartz") || self.id().starts_with("qtz") {82 return RuntimeId::Quartz;83 }8485 if self.id().starts_with("opal") || self.id() == "dev" || self.id() == "local_testnet" {86 return RuntimeId::Opal;87 }8889 RuntimeId::Unknown(self.id().into())90 }91}9293pub enum ServiceId {94 Prod,95 Dev,96}9798pub trait ServiceIdentification {99 fn service_id(&self) -> ServiceId;100}101102impl ServiceIdentification for Box<dyn sc_service::ChainSpec> {103 fn service_id(&self) -> ServiceId {104 if self.id().ends_with("dev") {105 ServiceId::Dev106 } else {107 ServiceId::Prod108 }109 }110}111112113pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {114 TPublic::Pair::from_string(&format!("//{}", seed), None)115 .expect("static values are valid; qed")116 .public()117}118119120#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]121#[serde(deny_unknown_fields)]122pub struct Extensions {123 124 pub relay_chain: String,125 126 pub para_id: u32,127}128129impl Extensions {130 131 pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {132 sc_chain_spec::get_extension(chain_spec.extensions())133 }134}135136type AccountPublic = <Signature as Verify>::Signer;137138139pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId140where141 AccountPublic: From<<TPublic::Pair as Pair>::Public>,142{143 AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()144}145146macro_rules! testnet_genesis {147 (148 $runtime:path,149 $root_key:expr,150 $initial_invulnerables:expr,151 $endowed_accounts:expr,152 $id:expr153 ) => {{154 use $runtime::*;155156 GenesisConfig {157 system: SystemConfig {158 code: WASM_BINARY159 .expect("WASM binary was not build, please build it!")160 .to_vec(),161 },162 balances: BalancesConfig {163 balances: $endowed_accounts164 .iter()165 .cloned()166 167 .map(|k| (k, 1 << 100))168 .collect(),169 },170 treasury: Default::default(),171 sudo: SudoConfig {172 key: Some($root_key),173 },174 vesting: VestingConfig { vesting: vec![] },175 parachain_info: ParachainInfoConfig {176 parachain_id: $id.into(),177 },178 parachain_system: Default::default(),179 collator_selection: CollatorSelectionConfig {180 invulnerables: $initial_invulnerables181 .iter()182 .cloned()183 .map(|(acc, _)| acc)184 .collect(),185 candidacy_bond: EXISTENTIAL_DEPOSIT * 16,186 ..Default::default()187 },188 session: SessionConfig {189 keys: $initial_invulnerables190 .into_iter()191 .map(|(acc, aura)| {192 (193 acc.clone(), 194 acc, 195 SessionKeys { aura }, 196 )197 })198 .collect(),199 },200 aura: Default::default(),201 202203204 aura_ext: Default::default(),205 evm: EVMConfig {206 accounts: BTreeMap::new(),207 },208 ethereum: EthereumConfig {},209 }210 }};211}212213pub fn development_config() -> DefaultChainSpec {214 let mut properties = Map::new();215 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());216 properties.insert("tokenDecimals".into(), 18.into());217 properties.insert(218 "ss58Format".into(),219 default_runtime::SS58Prefix::get().into(),220 );221222 DefaultChainSpec::from_genesis(223 224 format!(225 "{}{}",226 default_runtime::RUNTIME_NAME.to_uppercase(),227 if cfg!(feature = "unique-runtime") {228 ""229 } else {230 " by UNIQUE"231 }232 )233 .as_str(),234 235 format!("{}_dev", default_runtime::RUNTIME_NAME).as_str(),236 ChainType::Local,237 move || {238 testnet_genesis!(239 default_runtime,240 241 get_account_id_from_seed::<sr25519::Public>("Alice"),242 vec![243 (244 get_account_id_from_seed::<sr25519::Public>("Alice"),245 get_from_seed::<AuraId>("Alice"),246 ),247 (248 get_account_id_from_seed::<sr25519::Public>("Bob"),249 get_from_seed::<AuraId>("Bob"),250 ),251 ],252 253 vec![254 get_account_id_from_seed::<sr25519::Public>("Alice"),255 get_account_id_from_seed::<sr25519::Public>("Bob"),256 get_account_id_from_seed::<sr25519::Public>("Charlie"),257 get_account_id_from_seed::<sr25519::Public>("Dave"),258 get_account_id_from_seed::<sr25519::Public>("Eve"),259 get_account_id_from_seed::<sr25519::Public>("Ferdie"),260 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),261 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),262 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),263 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),264 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),265 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),266 ],267 1000268 )269 },270 271 vec![],272 273 None,274 275 None,276 None,277 278 Some(properties),279 280 Extensions {281 relay_chain: "rococo-dev".into(),282 para_id: 1000,283 },284 )285}286287pub fn local_testnet_config() -> DefaultChainSpec {288 let mut properties = Map::new();289 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());290 properties.insert("tokenDecimals".into(), 18.into());291 properties.insert(292 "ss58Format".into(),293 default_runtime::SS58Prefix::get().into(),294 );295296 DefaultChainSpec::from_genesis(297 298 format!(299 "{}{}",300 default_runtime::RUNTIME_NAME.to_uppercase(),301 if cfg!(feature = "unique-runtime") {302 ""303 } else {304 " by UNIQUE"305 }306 )307 .as_str(),308 309 format!("{}_local", default_runtime::RUNTIME_NAME).as_str(),310 ChainType::Local,311 move || {312 testnet_genesis!(313 default_runtime,314 315 get_account_id_from_seed::<sr25519::Public>("Alice"),316 vec![317 (318 get_account_id_from_seed::<sr25519::Public>("Alice"),319 get_from_seed::<AuraId>("Alice"),320 ),321 (322 get_account_id_from_seed::<sr25519::Public>("Bob"),323 get_from_seed::<AuraId>("Bob"),324 ),325 ],326 327 vec![328 get_account_id_from_seed::<sr25519::Public>("Alice"),329 get_account_id_from_seed::<sr25519::Public>("Bob"),330 get_account_id_from_seed::<sr25519::Public>("Charlie"),331 get_account_id_from_seed::<sr25519::Public>("Dave"),332 get_account_id_from_seed::<sr25519::Public>("Eve"),333 get_account_id_from_seed::<sr25519::Public>("Ferdie"),334 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),335 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),336 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),337 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),338 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),339 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),340 ],341 1000342 )343 },344 345 vec![],346 347 None,348 349 None,350 None,351 352 Some(properties),353 354 Extensions {355 relay_chain: "westend-local".into(),356 para_id: 1000,357 },358 )359}