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::*;2728#[cfg(feature = "unique-runtime")]29pub use unique_runtime as default_runtime;3031#[cfg(all(not(feature = "unique-runtime"), feature = "quartz-runtime"))]32pub use quartz_runtime as default_runtime;3334#[cfg(all(not(feature = "unique-runtime"), not(feature = "quartz-runtime")))]35pub use opal_runtime as default_runtime;363738#[cfg(feature = "unique-runtime")]39pub type UniqueChainSpec = sc_service::GenericChainSpec<unique_runtime::GenesisConfig, Extensions>;404142#[cfg(feature = "quartz-runtime")]43pub type QuartzChainSpec = sc_service::GenericChainSpec<quartz_runtime::GenesisConfig, Extensions>;444546pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;4748#[cfg(feature = "unique-runtime")]49pub type DefaultChainSpec = UniqueChainSpec;5051#[cfg(all(not(feature = "unique-runtime"), feature = "quartz-runtime"))]52pub type DefaultChainSpec = QuartzChainSpec;5354#[cfg(all(not(feature = "unique-runtime"), not(feature = "quartz-runtime")))]55pub type DefaultChainSpec = OpalChainSpec;5657#[cfg(not(feature = "unique-runtime"))]5859const PARA_ID: u32 = 2095;6061#[cfg(feature = "unique-runtime")]6263const PARA_ID: u32 = 2037;6465pub trait RuntimeIdentification {66 fn runtime_id(&self) -> RuntimeId;67}6869impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {70 fn runtime_id(&self) -> RuntimeId {71 #[cfg(feature = "unique-runtime")]72 if self.id().starts_with("unique") || self.id().starts_with("unq") {73 return RuntimeId::Unique;74 }7576 #[cfg(feature = "quartz-runtime")]77 if self.id().starts_with("quartz") || self.id().starts_with("qtz") {78 return RuntimeId::Quartz;79 }8081 if self.id().starts_with("opal")82 || self.id().starts_with("sapphire")83 || self.id() == "dev"84 || self.id() == "local_testnet"85 {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_authorities: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 tokens: TokensConfig { balances: vec![] },172 sudo: SudoConfig {173 key: Some($root_key),174 },175 vesting: VestingConfig { vesting: vec![] },176 parachain_info: ParachainInfoConfig {177 parachain_id: $id.into(),178 },179 parachain_system: Default::default(),180 aura: AuraConfig {181 authorities: $initial_authorities,182 },183 aura_ext: Default::default(),184 evm: EVMConfig {185 accounts: BTreeMap::new(),186 },187 ethereum: EthereumConfig {},188 }189 }};190}191192pub fn development_config() -> DefaultChainSpec {193 let mut properties = Map::new();194 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());195 properties.insert("tokenDecimals".into(), 18.into());196 properties.insert(197 "ss58Format".into(),198 default_runtime::SS58Prefix::get().into(),199 );200201 DefaultChainSpec::from_genesis(202 203 format!(204 "{}{}",205 default_runtime::RUNTIME_NAME.to_uppercase(),206 if cfg!(feature = "unique-runtime") {207 ""208 } else {209 " by UNIQUE"210 }211 )212 .as_str(),213 214 format!("{}_dev", default_runtime::RUNTIME_NAME).as_str(),215 ChainType::Local,216 move || {217 testnet_genesis!(218 default_runtime,219 220 get_account_id_from_seed::<sr25519::Public>("Alice"),221 vec![222 get_from_seed::<AuraId>("Alice"),223 get_from_seed::<AuraId>("Bob"),224 ],225 226 vec![227 get_account_id_from_seed::<sr25519::Public>("Alice"),228 get_account_id_from_seed::<sr25519::Public>("Bob"),229 get_account_id_from_seed::<sr25519::Public>("Charlie"),230 get_account_id_from_seed::<sr25519::Public>("Dave"),231 get_account_id_from_seed::<sr25519::Public>("Eve"),232 get_account_id_from_seed::<sr25519::Public>("Ferdie"),233 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),234 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),235 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),236 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),237 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),238 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),239 ],240 PARA_ID241 )242 },243 244 vec![],245 246 None,247 248 None,249 None,250 251 Some(properties),252 253 Extensions {254 relay_chain: "rococo-dev".into(),255 para_id: PARA_ID,256 },257 )258}259260pub fn local_testnet_config() -> DefaultChainSpec {261 let mut properties = Map::new();262 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());263 properties.insert("tokenDecimals".into(), 18.into());264 properties.insert(265 "ss58Format".into(),266 default_runtime::SS58Prefix::get().into(),267 );268269 DefaultChainSpec::from_genesis(270 271 format!(272 "{}{}",273 default_runtime::RUNTIME_NAME.to_uppercase(),274 if cfg!(feature = "unique-runtime") {275 ""276 } else {277 " by UNIQUE"278 }279 )280 .as_str(),281 282 format!("{}_local", default_runtime::RUNTIME_NAME).as_str(),283 ChainType::Local,284 move || {285 testnet_genesis!(286 default_runtime,287 288 get_account_id_from_seed::<sr25519::Public>("Alice"),289 vec![290 get_from_seed::<AuraId>("Alice"),291 get_from_seed::<AuraId>("Bob"),292 ],293 294 vec![295 get_account_id_from_seed::<sr25519::Public>("Alice"),296 get_account_id_from_seed::<sr25519::Public>("Bob"),297 get_account_id_from_seed::<sr25519::Public>("Charlie"),298 get_account_id_from_seed::<sr25519::Public>("Dave"),299 get_account_id_from_seed::<sr25519::Public>("Eve"),300 get_account_id_from_seed::<sr25519::Public>("Ferdie"),301 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),302 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),303 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),304 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),305 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),306 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),307 ],308 PARA_ID309 )310 },311 312 vec![],313 314 None,315 316 None,317 None,318 319 Some(properties),320 321 Extensions {322 relay_chain: "westend-local".into(),323 para_id: PARA_ID,324 },325 )326}