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 unique_runtime_common::types::*;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;5657pub enum RuntimeId {58 #[cfg(feature = "unique-runtime")]59 Unique,6061 #[cfg(feature = "quartz-runtime")]62 Quartz,6364 Opal,65 Unknown(String),66}6768pub trait RuntimeIdentification {69 fn runtime_id(&self) -> RuntimeId;70}7172impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {73 fn runtime_id(&self) -> RuntimeId {74 #[cfg(feature = "unique-runtime")]75 if self.id().starts_with("unique") || self.id().starts_with("unq") {76 return RuntimeId::Unique;77 }7879 #[cfg(feature = "quartz-runtime")]80 if self.id().starts_with("quartz") || self.id().starts_with("qtz") {81 return RuntimeId::Quartz;82 }8384 if self.id().starts_with("opal") || self.id() == "dev" || self.id() == "local_testnet" {85 return RuntimeId::Opal;86 }8788 RuntimeId::Unknown(self.id().into())89 }90}9192pub enum ServiceId {93 Prod,94 Dev,95}9697pub trait ServiceIdentification {98 fn service_id(&self) -> ServiceId;99}100101impl ServiceIdentification for Box<dyn sc_service::ChainSpec> {102 fn service_id(&self) -> ServiceId {103 if self.id().ends_with("dev") {104 ServiceId::Dev105 } else {106 ServiceId::Prod107 }108 }109}110111112pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {113 TPublic::Pair::from_string(&format!("//{}", seed), None)114 .expect("static values are valid; qed")115 .public()116}117118119#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]120#[serde(deny_unknown_fields)]121pub struct Extensions {122 123 pub relay_chain: String,124 125 pub para_id: u32,126}127128impl Extensions {129 130 pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {131 sc_chain_spec::get_extension(chain_spec.extensions())132 }133}134135type AccountPublic = <Signature as Verify>::Signer;136137138pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId139where140 AccountPublic: From<<TPublic::Pair as Pair>::Public>,141{142 AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()143}144145macro_rules! testnet_genesis {146 (147 $runtime:path,148 $root_key:expr,149 $initial_authorities:expr,150 $endowed_accounts:expr,151 $id:expr152 ) => {{153 use $runtime::*;154155 GenesisConfig {156 system: SystemConfig {157 code: WASM_BINARY158 .expect("WASM binary was not build, please build it!")159 .to_vec(),160 },161 balances: BalancesConfig {162 balances: $endowed_accounts163 .iter()164 .cloned()165 166 .map(|k| (k, 1 << 100))167 .collect(),168 },169 treasury: Default::default(),170 sudo: SudoConfig {171 key: Some($root_key),172 },173 vesting: VestingConfig { vesting: vec![] },174 parachain_info: ParachainInfoConfig {175 parachain_id: $id.into(),176 },177 parachain_system: Default::default(),178 aura: AuraConfig {179 authorities: $initial_authorities,180 },181 aura_ext: Default::default(),182 evm: EVMConfig {183 accounts: BTreeMap::new(),184 },185 ethereum: EthereumConfig {},186 }187 }};188}189190pub fn development_config() -> DefaultChainSpec {191 let mut properties = Map::new();192 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());193 properties.insert("tokenDecimals".into(), 18.into());194 properties.insert(195 "ss58Format".into(),196 default_runtime::SS58Prefix::get().into(),197 );198199 DefaultChainSpec::from_genesis(200 201 format!(202 "{}{}",203 default_runtime::RUNTIME_NAME.to_uppercase(),204 if cfg!(feature = "unique-runtime") {205 ""206 } else {207 " by UNIQUE"208 }209 )210 .as_str(),211 212 format!("{}_dev", default_runtime::RUNTIME_NAME).as_str(),213 ChainType::Local,214 move || {215 testnet_genesis!(216 default_runtime,217 218 get_account_id_from_seed::<sr25519::Public>("Alice"),219 vec![220 get_from_seed::<AuraId>("Alice"),221 get_from_seed::<AuraId>("Bob"),222 ],223 224 vec![225 get_account_id_from_seed::<sr25519::Public>("Alice"),226 get_account_id_from_seed::<sr25519::Public>("Bob"),227 get_account_id_from_seed::<sr25519::Public>("Charlie"),228 get_account_id_from_seed::<sr25519::Public>("Dave"),229 get_account_id_from_seed::<sr25519::Public>("Eve"),230 get_account_id_from_seed::<sr25519::Public>("Ferdie"),231 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),232 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),233 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),234 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),235 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),236 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),237 ],238 1000239 )240 },241 242 vec![],243 244 None,245 246 None,247 None,248 249 Some(properties),250 251 Extensions {252 relay_chain: "rococo-dev".into(),253 para_id: 1000,254 },255 )256}257258pub fn local_testnet_config() -> DefaultChainSpec {259 let mut properties = Map::new();260 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());261 properties.insert("tokenDecimals".into(), 18.into());262 properties.insert(263 "ss58Format".into(),264 default_runtime::SS58Prefix::get().into(),265 );266267 DefaultChainSpec::from_genesis(268 269 format!(270 "{}{}",271 default_runtime::RUNTIME_NAME.to_uppercase(),272 if cfg!(feature = "unique-runtime") {273 ""274 } else {275 " by UNIQUE"276 }277 )278 .as_str(),279 280 format!("{}_local", default_runtime::RUNTIME_NAME).as_str(),281 ChainType::Local,282 move || {283 testnet_genesis!(284 default_runtime,285 286 get_account_id_from_seed::<sr25519::Public>("Alice"),287 vec![288 get_from_seed::<AuraId>("Alice"),289 get_from_seed::<AuraId>("Bob"),290 ],291 292 vec![293 get_account_id_from_seed::<sr25519::Public>("Alice"),294 get_account_id_from_seed::<sr25519::Public>("Bob"),295 get_account_id_from_seed::<sr25519::Public>("Charlie"),296 get_account_id_from_seed::<sr25519::Public>("Dave"),297 get_account_id_from_seed::<sr25519::Public>("Eve"),298 get_account_id_from_seed::<sr25519::Public>("Ferdie"),299 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),300 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),301 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),302 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),303 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),304 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),305 ],306 1000307 )308 },309 310 vec![],311 312 None,313 314 None,315 None,316 317 Some(properties),318 319 Extensions {320 relay_chain: "westend-local".into(),321 para_id: 1000,322 },323 )324}