use crate::helpers::core_org;
use governance_os_pallet_coin_voting::VoteCountingStrategy;
use governance_os_pallet_organizations::OrganizationDetails;
use governance_os_pallet_tokens::CurrencyDetails;
use governance_os_primitives::{AccountId, CurrencyId, Role, Signature};
use governance_os_runtime::{
AuraConfig, AuraId, BylawsConfig, CoinVotingParameters, GenesisConfig, GrandpaConfig,
GrandpaId, NativeCurrencyId, OrganizationsConfig, RuntimeVotingParameters,
RuntimeVotingSystemId, SystemConfig, TokensConfig, WASM_BINARY,
};
use sc_service::ChainType;
use sp_core::{sr25519, Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify};
pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>;
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}
type AccountPublic = <Signature as Verify>::Signer;
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}
pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
(get_from_seed::<AuraId>(s), get_from_seed::<GrandpaId>(s))
}
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
endowed_accounts: Vec<AccountId>,
currencies: Option<Vec<(CurrencyId, CurrencyDetails<AccountId>)>>,
roles: Option<Vec<(Role, Option<AccountId>)>>,
organizations: Option<
Vec<OrganizationDetails<AccountId, (RuntimeVotingSystemId, RuntimeVotingParameters)>>,
>,
) -> GenesisConfig {
let chain_currencies = currencies.unwrap_or(vec![(
NativeCurrencyId::get(),
CurrencyDetails {
owner: core_org(),
transferable: true,
},
)]);
let chain_roles = roles.unwrap_or(vec![
(Role::Root, Some(core_org())),
(Role::CreateCurrencies, None),
(Role::CreateOrganizations, None),
]);
let chain_orgs = organizations.unwrap_or(vec![OrganizationDetails {
executors: vec![get_account_id_from_seed::<sr25519::Public>("Alice")],
voting: (
RuntimeVotingSystemId::CoinVoting,
RuntimeVotingParameters::CoinVoting(CoinVotingParameters {
ttl: 10,
voting_currency: NativeCurrencyId::get(),
min_quorum: 50,
min_participation: 33,
vote_counting_strategy: VoteCountingStrategy::Simple,
}),
),
}]);
GenesisConfig {
frame_system: Some(SystemConfig {
code: wasm_binary.to_vec(),
changes_trie_config: Default::default(),
}),
pallet_aura: Some(AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
}),
pallet_grandpa: Some(GrandpaConfig {
authorities: initial_authorities
.iter()
.map(|x| (x.1.clone(), 1))
.collect(),
}),
governance_os_pallet_tokens: Some(TokensConfig {
endowed_accounts: endowed_accounts
.iter()
.cloned()
.map(|account_id| (NativeCurrencyId::get(), account_id, 1 << 60))
.collect::<Vec<_>>(),
currency_details: chain_currencies,
}),
governance_os_pallet_bylaws: Some(BylawsConfig { roles: chain_roles }),
governance_os_pallet_organizations: Some(OrganizationsConfig {
organizations: chain_orgs,
}),
}
}
pub fn development_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or("Development wasm binary not available".to_string())?;
Ok(ChainSpec::from_genesis(
"Development",
"dev",
ChainType::Development,
move || {
testnet_genesis(
wasm_binary,
vec![authority_keys_from_seed("Alice")],
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
],
None,
None,
None,
)
},
vec![],
None,
None,
None,
None,
))
}
pub fn local_testnet_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or("Development wasm binary not available".to_string())?;
Ok(ChainSpec::from_genesis(
"Local Testnet",
"local_testnet",
ChainType::Local,
move || {
testnet_genesis(
wasm_binary,
vec![
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
],
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
],
None,
None,
None,
)
},
vec![],
None,
None,
None,
None,
))
}
pub fn dummy_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or("Development wasm binary not available".to_string())?;
Ok(ChainSpec::from_genesis(
"Dummy",
"dummy",
ChainType::Custom("dummy".to_string()),
move || {
testnet_genesis(
wasm_binary,
vec![authority_keys_from_seed("Alice")],
vec![],
Some(vec![]),
None,
Some(vec![]),
)
},
vec![],
None,
None,
None,
None,
))
}