diff --git a/node/primitives/src/lib.rs b/node/primitives/src/lib.rs index f8bd7b7fad..1c424e87c3 100644 --- a/node/primitives/src/lib.rs +++ b/node/primitives/src/lib.rs @@ -28,13 +28,15 @@ use sp_runtime::{ }; mod bridge; -mod currency; +pub mod currency; mod salp; -mod tests; pub mod traits; mod xcm; pub use salp::*; +#[cfg(test)] +mod tests; + pub use crate::{ bridge::*, currency::{CurrencyId, TokenSymbol}, diff --git a/node/service/src/chain_spec/asgard.rs b/node/service/src/chain_spec/asgard.rs index ebbae445a8..e8300ddaa1 100644 --- a/node/service/src/chain_spec/asgard.rs +++ b/node/service/src/chain_spec/asgard.rs @@ -17,12 +17,13 @@ // along with this program. If not, see . use asgard_runtime::{ - constants::{currency::DOLLARS, time::DAYS}, - AccountId, AuraId, Balance, BalancesConfig, BancorConfig, BlockNumber, CollatorSelectionConfig, - CouncilConfig, DemocracyConfig, GenesisConfig, IndicesConfig, MinterRewardConfig, - ParachainInfoConfig, PolkadotXcmConfig, SessionConfig, SudoConfig, SystemConfig, - TechnicalCommitteeConfig, TokensConfig, VestingConfig, VtokenMintConfig, WASM_BINARY, + constants::currency::DOLLARS, AccountId, AuraId, Balance, BalancesConfig, BancorConfig, + BlockNumber, CollatorSelectionConfig, CouncilConfig, DemocracyConfig, GenesisConfig, + IndicesConfig, MinterRewardConfig, ParachainInfoConfig, PolkadotXcmConfig, SessionConfig, + SudoConfig, SystemConfig, TechnicalCommitteeConfig, TokensConfig, VestingConfig, + VtokenMintConfig, WASM_BINARY, }; +use bifrost_runtime_common::constants::time::*; use cumulus_primitives_core::ParaId; use frame_benchmarking::{account, whitelisted_caller}; use hex_literal::hex; diff --git a/node/service/src/chain_spec/bifrost.rs b/node/service/src/chain_spec/bifrost.rs index a9f0044e6f..afe4d89847 100644 --- a/node/service/src/chain_spec/bifrost.rs +++ b/node/service/src/chain_spec/bifrost.rs @@ -22,12 +22,12 @@ use std::{ }; use bifrost_runtime::{ - constants::currency::DOLLARS, AccountId, AuraId, Balance, BalancesConfig, BlockNumber, - CollatorSelectionConfig, CouncilConfig, CouncilMembershipConfig, DemocracyConfig, - GenesisConfig, IndicesConfig, ParachainInfoConfig, PolkadotXcmConfig, SessionConfig, - SystemConfig, TechnicalCommitteeConfig, TechnicalMembershipConfig, TokensConfig, VestingConfig, - WASM_BINARY, + AccountId, AuraId, Balance, BalancesConfig, BlockNumber, CollatorSelectionConfig, + CouncilConfig, CouncilMembershipConfig, DemocracyConfig, GenesisConfig, IndicesConfig, + ParachainInfoConfig, PolkadotXcmConfig, SessionConfig, SystemConfig, TechnicalCommitteeConfig, + TechnicalMembershipConfig, TokensConfig, VestingConfig, WASM_BINARY, }; +use bifrost_runtime_common::dollar; use cumulus_primitives_core::ParaId; use frame_benchmarking::{account, whitelisted_caller}; use hex_literal::hex; @@ -47,7 +47,10 @@ const DEFAULT_PROTOCOL_ID: &str = "bifrost"; /// Specialized `ChainSpec` for the bifrost runtime. pub type ChainSpec = sc_service::GenericChainSpec; -const ENDOWMENT: u128 = 1_000_000 * DOLLARS; +#[allow(non_snake_case)] +pub fn ENDOWMENT() -> u128 { + 1_000_000 * dollar(CurrencyId::Token(TokenSymbol::BNC)) +} pub fn bifrost_genesis( invulnerables: Vec<(AccountId, AuraId)>, @@ -114,21 +117,21 @@ fn development_config_genesis(id: ParaId) -> GenesisConfig { get_account_id_from_seed::("Alice"), whitelisted_caller(), // Benchmarking whitelist_account ]; - let balances = endowed_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect(); + let balances = endowed_accounts.iter().cloned().map(|x| (x, ENDOWMENT())).collect(); let vestings = endowed_accounts .iter() .cloned() - .map(|x| (x.clone(), 0u32, 100u32, ENDOWMENT / 4)) + .map(|x| (x.clone(), 0u32, 100u32, ENDOWMENT() / 4)) .collect(); let tokens = endowed_accounts .iter() .flat_map(|x| { vec![ - (x.clone(), CurrencyId::Stable(TokenSymbol::KUSD), ENDOWMENT * 10_000), - (x.clone(), CurrencyId::Token(TokenSymbol::KAR), ENDOWMENT * 10_000), - (x.clone(), CurrencyId::Token(TokenSymbol::KSM), ENDOWMENT), - (x.clone(), CurrencyId::Token(TokenSymbol::DOT), ENDOWMENT), - (x.clone(), CurrencyId::VSToken(TokenSymbol::DOT), ENDOWMENT), + (x.clone(), CurrencyId::Stable(TokenSymbol::KUSD), ENDOWMENT() * 10_000), + (x.clone(), CurrencyId::Token(TokenSymbol::KAR), ENDOWMENT() * 10_000), + (x.clone(), CurrencyId::Token(TokenSymbol::KSM), ENDOWMENT()), + (x.clone(), CurrencyId::Token(TokenSymbol::DOT), ENDOWMENT()), + (x.clone(), CurrencyId::VSToken(TokenSymbol::DOT), ENDOWMENT()), ] }) .collect(); @@ -182,29 +185,29 @@ fn local_config_genesis(id: ParaId) -> GenesisConfig { account("bechmarking_account_1", 0, 0), /* Benchmarking account_1, used for interacting * with whitelistted_caller */ ]; - let balances = endowed_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect(); + let balances = endowed_accounts.iter().cloned().map(|x| (x, ENDOWMENT())).collect(); let vestings = endowed_accounts .iter() .cloned() - .map(|x| (x.clone(), 0u32, 100u32, ENDOWMENT / 4)) + .map(|x| (x.clone(), 0u32, 100u32, ENDOWMENT() / 4)) .collect(); let tokens = endowed_accounts .iter() .flat_map(|x| { vec![ - (x.clone(), CurrencyId::Stable(TokenSymbol::KUSD), ENDOWMENT * 10_000), - (x.clone(), CurrencyId::Token(TokenSymbol::KAR), ENDOWMENT * 10_000), - (x.clone(), CurrencyId::Token(TokenSymbol::KSM), ENDOWMENT * 4_000_000), - (x.clone(), CurrencyId::VSToken(TokenSymbol::KSM), ENDOWMENT * 4_000_000), + (x.clone(), CurrencyId::Stable(TokenSymbol::KUSD), ENDOWMENT() * 10_000), + (x.clone(), CurrencyId::Token(TokenSymbol::KAR), ENDOWMENT() * 10_000), + (x.clone(), CurrencyId::Token(TokenSymbol::KSM), ENDOWMENT() * 4_000_000), + (x.clone(), CurrencyId::VSToken(TokenSymbol::KSM), ENDOWMENT() * 4_000_000), ( x.clone(), CurrencyId::VSBond(TokenSymbol::KSM, 3000, 13, 20), - ENDOWMENT * 4_000_000, + ENDOWMENT() * 4_000_000, ), ( x.clone(), CurrencyId::VSBond(TokenSymbol::BNC, 2001, 13, 20), - ENDOWMENT * 4_000_000, + ENDOWMENT() * 4_000_000, ), ] }) @@ -321,7 +324,11 @@ fn bifrost_config_genesis(id: ParaId) -> GenesisConfig { .into_iter() .collect(); - assert_eq!(total_issuance, 32_000_000 * DOLLARS, "total issuance must be equal to 320 million"); + assert_eq!( + total_issuance, + 32_000_000 * dollar(CurrencyId::Token(TokenSymbol::BNC)), + "total issuance must be equal to 320 million" + ); let vesting_configs: Vec = config_from_json_files(exe_dir.join("res/genesis_config/vesting")).unwrap(); diff --git a/runtime/asgard/src/constants.rs b/runtime/asgard/src/constants.rs index 9aed24789d..d310833d7c 100644 --- a/runtime/asgard/src/constants.rs +++ b/runtime/asgard/src/constants.rs @@ -28,8 +28,8 @@ pub mod currency { use smallvec::smallvec; pub use sp_runtime::Perbill; - pub const BNCS: Balance = 1_000_000_000_000; - pub const DOLLARS: Balance = BNCS; + pub const ASGS: Balance = 1_000_000_000_000; + pub const DOLLARS: Balance = ASGS; pub const CENTS: Balance = DOLLARS / 100; // assume this is worth about a cent. pub const RELAY_CENTS: Balance = DOLLARS / 30_000; pub const MILLICENTS: Balance = CENTS / 1_000; @@ -69,49 +69,4 @@ pub mod currency { } /// Time. -pub mod time { - use node_primitives::{BlockNumber, Moment}; - - /// Since BABE is probabilistic this is the average expected block time that - /// we are targetting. Blocks will be produced at a minimum duration defined - /// by `SLOT_DURATION`, but some slots will not be allocated to any - /// authority and hence no block will be produced. We expect to have this - /// block time on average following the defined slot duration and the value - /// of `c` configured for BABE (where `1 - c` represents the probability of - /// a slot being empty). - /// This value is only used indirectly to define the unit constants below - /// that are expressed in blocks. The rest of the code should use - /// `SLOT_DURATION` instead (like the Timestamp pallet for calculating the - /// minimum period). - /// - /// If using BABE with secondary slots (default) then all of the slots will - /// always be assigned, in which case `MILLISECS_PER_BLOCK` and - /// `SLOT_DURATION` should have the same value. - /// - /// - pub const MILLISECS_PER_BLOCK: Moment = 12000; - pub const SECS_PER_BLOCK: Moment = MILLISECS_PER_BLOCK / 1000; - - pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK; - - // 1 in 4 blocks (on average, not counting collisions) will be primary BABE blocks. - pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4); - - pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 10 * MINUTES; - pub const EPOCH_DURATION_IN_SLOTS: u64 = { - const SLOT_FILL_RATE: f64 = MILLISECS_PER_BLOCK as f64 / SLOT_DURATION as f64; - - (EPOCH_DURATION_IN_BLOCKS as f64 * SLOT_FILL_RATE) as u64 - }; - - // These time units are defined in number of blocks. - pub const MINUTES: BlockNumber = 60 / (SECS_PER_BLOCK as BlockNumber); - pub const HOURS: BlockNumber = MINUTES * 60; - pub const DAYS: BlockNumber = HOURS * 24; - pub const WEEKS: BlockNumber = DAYS * 7; - - // The `LeasePeriod` defination from `polkadot`. - pub const KUSAMA_LEASE_PERIOD: BlockNumber = 6 * WEEKS; - pub const ROCOCO_LEASE_PERIOD: BlockNumber = 1 * DAYS; - pub const WESTEND_LEASE_PERIOD: BlockNumber = 28 * DAYS; -} +pub mod time {} diff --git a/runtime/asgard/src/lib.rs b/runtime/asgard/src/lib.rs index 369b46ec34..65dc50472f 100644 --- a/runtime/asgard/src/lib.rs +++ b/runtime/asgard/src/lib.rs @@ -76,8 +76,10 @@ use bifrost_flexible_fee::{ misc_fees::{ExtraFeeMatcher, MiscFeeHandler, NameGetter}, }; use bifrost_runtime_common::{ - constants::parachains, - xcm_impl::{ + cent, + constants::{parachains, time::*}, + dollar, micro, milli, millicent, + r#impl::{ BifrostAccountIdToMultiLocation, BifrostAssetMatcher, BifrostCurrencyIdConvert, BifrostFilteredAssets, }, @@ -85,7 +87,7 @@ use bifrost_runtime_common::{ SlowAdjustingFeeUpdate, TechnicalCollective, }; use codec::{Decode, Encode, MaxEncodedLen}; -use constants::{currency::*, time::*}; +use constants::currency::*; use cumulus_primitives_core::ParaId as CumulusParaId; use frame_support::{ sp_runtime::traits::Convert, @@ -1112,19 +1114,18 @@ impl orml_currencies::Config for Runtime { orml_traits::parameter_type_with_key! { pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance { match currency_id { - &CurrencyId::Native(TokenSymbol::ASG) => 10 * MILLIBNC, // 0.01 BNC - &CurrencyId::Stable(TokenSymbol::KUSD) => 10 * MILLICENTS, - &CurrencyId::Token(TokenSymbol::KSM) => 10 * MILLICENTS, // 0.0001 KSM - &CurrencyId::Token(TokenSymbol::KAR) => 10 * MILLICENTS, - &CurrencyId::Token(TokenSymbol::DOT) => 100_000_000, // DOT has a decimals of 10e10, 0.01 DOT - &CurrencyId::Token(TokenSymbol::ZLK) => 1_000_000_000_000, // ZLK has a decimals of 10e18 - &CurrencyId::VSToken(TokenSymbol::KSM) => 10 * MILLICENTS, - &CurrencyId::VSToken(TokenSymbol::DOT) => 100_000_000, - &CurrencyId::VSBond(TokenSymbol::ASG, ..) => 10 * MILLICENTS, - &CurrencyId::VSBond(TokenSymbol::KSM, ..) => 10 * MILLICENTS, - &CurrencyId::VSBond(TokenSymbol::BNC, ..) => 10 * MILLICENTS, - &CurrencyId::VSBond(TokenSymbol::DOT, ..) => 100_000_000, - &CurrencyId::LPToken(..) => 10 * MILLICENTS, + &CurrencyId::Native(TokenSymbol::BNC) => 10 * milli(NativeCurrencyId::get()), // 0.01 BNC + &CurrencyId::Stable(TokenSymbol::KUSD) => 10 * millicent(StableCurrencyId::get()), + &CurrencyId::Token(TokenSymbol::KSM) => 10 * millicent(RelayCurrencyId::get()), // 0.0001 KSM + &CurrencyId::Token(TokenSymbol::KAR) => 10 * millicent(CurrencyId::Token(TokenSymbol::KAR)), + &CurrencyId::Token(TokenSymbol::DOT) => 1 * cent(PolkadotCurrencyId::get()), // DOT has a decimals of 10e10, 0.01 DOT + &CurrencyId::Token(TokenSymbol::ZLK) => 1 * micro(CurrencyId::Token(TokenSymbol::ZLK)), // ZLK has a decimals of 10e18 + &CurrencyId::VSToken(TokenSymbol::KSM) => 10 * millicent(RelayCurrencyId::get()), + &CurrencyId::VSToken(TokenSymbol::DOT) => 1 * cent(PolkadotCurrencyId::get()), + &CurrencyId::VSBond(TokenSymbol::BNC, ..) => 10 * millicent(NativeCurrencyId::get()), + &CurrencyId::VSBond(TokenSymbol::KSM, ..) => 10 * millicent(RelayCurrencyId::get()), + &CurrencyId::VSBond(TokenSymbol::DOT, ..) => 1 * cent(PolkadotCurrencyId::get()), + &CurrencyId::LPToken(..) => 10 * millicent(NativeCurrencyId::get()), _ => Balance::max_value() // unsupported } }; @@ -1412,7 +1413,7 @@ impl bifrost_vsbond_auction::Config for Runtime { parameter_types! { pub const RelayChainTokenSymbol: TokenSymbol = TokenSymbol::KSM; pub const RelayChainTokenSymbolDOT: TokenSymbol = TokenSymbol::DOT; - pub const MaximumDepositInPool: Balance = 1_000_000_000 * DOLLARS; + pub MaximumDepositInPool: Balance = 1_000_000_000 * dollar(NativeCurrencyId::get()); pub const MinimumDepositOfUser: Balance = 1_000_000; pub const MinimumRewardPerBlock: Balance = 1_000; pub const MinimumDuration: BlockNumber = HOURS; diff --git a/runtime/bifrost/src/constants.rs b/runtime/bifrost/src/constants.rs index 1e83f803c5..fef3b95ce6 100644 --- a/runtime/bifrost/src/constants.rs +++ b/runtime/bifrost/src/constants.rs @@ -20,33 +20,42 @@ /// Money matters. pub mod currency { + use bifrost_runtime_common::{cent, dollar}; use frame_support::weights::{ constants::{ExtrinsicBaseWeight, WEIGHT_PER_SECOND}, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }; - use node_primitives::Balance; + use node_primitives::{Balance, CurrencyId, TokenSymbol}; use smallvec::smallvec; pub use sp_runtime::Perbill; - pub const BNCS: Balance = 1_000_000_000_000; - pub const DOLLARS: Balance = BNCS; - pub const CENTS: Balance = DOLLARS / 100; // assume this is worth about a cent. - pub const RELAY_CENTS: Balance = DOLLARS / 10_000; - pub const MILLICENTS: Balance = CENTS / 1_000; - pub const MILLIBNC: Balance = 1_000_000_000; - pub const MICROBNC: Balance = 1_000_000; - pub const XCM_WEIGHT: u64 = 1_000_000_000; + pub fn deposit(items: u32, bytes: u32) -> Balance { + items as Balance * 15 * cent(CurrencyId::Token(TokenSymbol::BNC)) + + (bytes as Balance) * 6 * cent(CurrencyId::Token(TokenSymbol::BNC)) + } - pub const fn deposit(items: u32, bytes: u32) -> Balance { - items as Balance * 15 * CENTS + (bytes as Balance) * 6 * CENTS + pub struct KsmWeightToFee; + impl WeightToFeePolynomial for KsmWeightToFee { + type Balance = Balance; + fn polynomial() -> WeightToFeeCoefficients { + let p = ksm_base_tx_fee(); + let q = 10 * Balance::from(ExtrinsicBaseWeight::get()); + smallvec![WeightToFeeCoefficient { + degree: 1, + negative: false, + coeff_frac: Perbill::from_rational(p % q, q), + coeff_integer: p / q, + }] + } } pub struct WeightToFee; impl WeightToFeePolynomial for WeightToFee { type Balance = Balance; fn polynomial() -> WeightToFeeCoefficients { - let p = super::currency::RELAY_CENTS; - let q = 10 * Balance::from(ExtrinsicBaseWeight::get()); + // extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT: + let p = base_tx_fee(); + let q = Balance::from(ExtrinsicBaseWeight::get()); smallvec![WeightToFeeCoefficient { degree: 1, negative: false, @@ -56,8 +65,12 @@ pub mod currency { } } + fn ksm_base_tx_fee() -> Balance { + dollar(CurrencyId::Token(TokenSymbol::KSM)) / 10_000 + } + fn base_tx_fee() -> Balance { - CENTS / 10 + cent(CurrencyId::Token(TokenSymbol::BNC)) / 5 } pub fn ksm_per_second() -> u128 { @@ -69,50 +82,4 @@ pub mod currency { } /// Time. -pub mod time { - use node_primitives::{BlockNumber, Moment}; - - /// Since BABE is probabilistic this is the average expected block time that - /// we are targetting. Blocks will be produced at a minimum duration defined - /// by `SLOT_DURATION`, but some slots will not be allocated to any - /// authority and hence no block will be produced. We expect to have this - /// block time on average following the defined slot duration and the value - /// of `c` configured for BABE (where `1 - c` represents the probability of - /// a slot being empty). - /// This value is only used indirectly to define the unit constants below - /// that are expressed in blocks. The rest of the code should use - /// `SLOT_DURATION` instead (like the Timestamp pallet for calculating the - /// minimum period). - /// - /// If using BABE with secondary slots (default) then all of the slots will - /// always be assigned, in which case `MILLISECS_PER_BLOCK` and - /// `SLOT_DURATION` should have the same value. - /// - /// - pub const MILLISECS_PER_BLOCK: Moment = 12000; - pub const SECS_PER_BLOCK: Moment = MILLISECS_PER_BLOCK / 1000; - - pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK; - - // 1 in 4 blocks (on average, not counting collisions) will be primary BABE blocks. - pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4); - - pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 6 * HOURS; - pub const EPOCH_DURATION_IN_SLOTS: u64 = { - const SLOT_FILL_RATE: f64 = MILLISECS_PER_BLOCK as f64 / SLOT_DURATION as f64; - - (EPOCH_DURATION_IN_BLOCKS as f64 * SLOT_FILL_RATE) as u64 - }; - - // These time units are defined in number of blocks. - pub const MINUTES: BlockNumber = 60 / (SECS_PER_BLOCK as BlockNumber); - pub const HOURS: BlockNumber = MINUTES * 60; - pub const DAYS: BlockNumber = HOURS * 24; - pub const WEEKS: BlockNumber = DAYS * 7; - - // The `LeasePeriod` defination from `polkadot`. - pub const POLKA_LEASE_PERIOD: BlockNumber = 12 * WEEKS; - pub const KUSAMA_LEASE_PERIOD: BlockNumber = 6 * WEEKS; - pub const ROCOCO_LEASE_PERIOD: BlockNumber = 1 * DAYS; - pub const WESTEND_LEASE_PERIOD: BlockNumber = 28 * DAYS; -} +pub mod time {} diff --git a/runtime/bifrost/src/lib.rs b/runtime/bifrost/src/lib.rs index a94f97320d..e665cff2f2 100644 --- a/runtime/bifrost/src/lib.rs +++ b/runtime/bifrost/src/lib.rs @@ -69,8 +69,10 @@ use bifrost_flexible_fee::{ misc_fees::{ExtraFeeMatcher, MiscFeeHandler, NameGetter}, }; use bifrost_runtime_common::{ - constants::parachains, - xcm_impl::{ + cent, + constants::{parachains, time::*}, + dollar, micro, milli, millicent, + r#impl::{ BifrostAccountIdToMultiLocation, BifrostAssetMatcher, BifrostCurrencyIdConvert, BifrostFilteredAssets, }, @@ -78,7 +80,7 @@ use bifrost_runtime_common::{ SlowAdjustingFeeUpdate, TechnicalCollective, }; use codec::{Decode, Encode, MaxEncodedLen}; -use constants::{currency::*, time::*}; +use constants::currency::*; use cumulus_primitives_core::ParaId as CumulusParaId; use frame_support::{ sp_runtime::traits::Convert, @@ -337,10 +339,10 @@ impl pallet_timestamp::Config for Runtime { } parameter_types! { - pub const ExistentialDeposit: Balance = 10 * MILLIBNC; - pub const TransferFee: Balance = 1 * MILLIBNC; - pub const CreationFee: Balance = 1 * MILLIBNC; - pub const TransactionByteFee: Balance = 1 * MICROBNC; + pub ExistentialDeposit: Balance = 10 * milli(NativeCurrencyId::get()); + pub TransferFee: Balance = 1 * milli(NativeCurrencyId::get()); + pub CreationFee: Balance = 1 * milli(NativeCurrencyId::get()); + pub TransactionByteFee: Balance = 16 * micro(NativeCurrencyId::get()); pub const OperationalFeeMultiplier: u8 = 5; pub const MaxLocks: u32 = 50; pub const MaxReserves: u32 = 50; @@ -354,12 +356,12 @@ impl pallet_utility::Config for Runtime { parameter_types! { // One storage item; key size 32, value size 8; . - pub const ProxyDepositBase: Balance = deposit(1, 8); + pub ProxyDepositBase: Balance = deposit(1, 8); // Additional storage item size of 33 bytes. - pub const ProxyDepositFactor: Balance = deposit(0, 33); + pub ProxyDepositFactor: Balance = deposit(0, 33); pub const MaxProxies: u16 = 32; - pub const AnnouncementDepositBase: Balance = deposit(1, 8); - pub const AnnouncementDepositFactor: Balance = deposit(0, 66); + pub AnnouncementDepositBase: Balance = deposit(1, 8); + pub AnnouncementDepositFactor: Balance = deposit(0, 66); pub const MaxPending: u16 = 32; } @@ -484,9 +486,9 @@ impl pallet_scheduler::Config for Runtime { parameter_types! { // One storage item; key size is 32; value is size 4+4+16+32 bytes = 56 bytes. - pub const DepositBase: Balance = deposit(1, 88); + pub DepositBase: Balance = deposit(1, 88); // Additional storage item size of 32 bytes. - pub const DepositFactor: Balance = deposit(0, 32); + pub DepositFactor: Balance = deposit(0, 32); pub const MaxSignatories: u16 = 100; } @@ -502,9 +504,9 @@ impl pallet_multisig::Config for Runtime { parameter_types! { // Minimum 4 CENTS/byte - pub const BasicDeposit: Balance = deposit(1, 258); - pub const FieldDeposit: Balance = deposit(0, 66); - pub const SubAccountDeposit: Balance = deposit(1, 53); + pub BasicDeposit: Balance = deposit(1, 258); + pub FieldDeposit: Balance = deposit(0, 66); + pub SubAccountDeposit: Balance = deposit(1, 53); pub const MaxSubAccounts: u32 = 100; pub const MaxAdditionalFields: u32 = 100; pub const MaxRegistrars: u32 = 20; @@ -526,7 +528,7 @@ impl pallet_identity::Config for Runtime { } parameter_types! { - pub const IndexDeposit: Balance = 1 * DOLLARS; + pub IndexDeposit: Balance = 1 * dollar(NativeCurrencyId::get()); } impl pallet_indices::Config for Runtime { @@ -612,11 +614,11 @@ impl pallet_membership::Config for Runtime { } parameter_types! { - pub const CandidacyBond: Balance = 100 * CENTS; + pub CandidacyBond: Balance = 100 * cent(NativeCurrencyId::get()); // 1 storage item created, key size is 32 bytes, value size is 16+16. - pub const VotingBondBase: Balance = deposit(1, 64); + pub VotingBondBase: Balance = deposit(1, 64); // additional data per vote is 32 bytes (account id). - pub const VotingBondFactor: Balance = deposit(0, 32); + pub VotingBondFactor: Balance = deposit(0, 32); /// Daily council elections pub const TermDuration: BlockNumber = 24 * HOURS; pub const DesiredMembers: u32 = 7; @@ -649,10 +651,10 @@ parameter_types! { pub const LaunchPeriod: BlockNumber = 7 * DAYS; pub const VotingPeriod: BlockNumber = 7 * DAYS; pub const FastTrackVotingPeriod: BlockNumber = 3 * HOURS; - pub const MinimumDeposit: Balance = 100 * DOLLARS; + pub MinimumDeposit: Balance = 100 * dollar(NativeCurrencyId::get()); pub const EnactmentPeriod: BlockNumber = 2 * DAYS; pub const CooloffPeriod: BlockNumber = 7 * DAYS; - pub const PreimageByteDeposit: Balance = 10 * MILLICENTS; + pub PreimageByteDeposit: Balance = 10 * millicent(NativeCurrencyId::get()); pub const InstantAllowed: bool = true; pub const MaxVotes: u32 = 100; pub const MaxProposals: u32 = 100; @@ -712,20 +714,20 @@ impl pallet_democracy::Config for Runtime { parameter_types! { pub const ProposalBond: Permill = Permill::from_percent(5); - pub const ProposalBondMinimum: Balance = 100 * DOLLARS; + pub ProposalBondMinimum: Balance = 100 * dollar(NativeCurrencyId::get()); pub const SpendPeriod: BlockNumber = 6 * DAYS; pub const Burn: Permill = Permill::from_perthousand(0); pub const TipCountdown: BlockNumber = 1 * DAYS; pub const TipFindersFee: Percent = Percent::from_percent(20); - pub const TipReportDepositBase: Balance = 1 * DOLLARS; - pub const DataDepositPerByte: Balance = 10 * CENTS; - pub const BountyDepositBase: Balance = 1 * DOLLARS; + pub TipReportDepositBase: Balance = 1 * dollar(NativeCurrencyId::get()); + pub DataDepositPerByte: Balance = 10 * cent(NativeCurrencyId::get()); + pub BountyDepositBase: Balance = 1 * dollar(NativeCurrencyId::get()); pub const BountyDepositPayoutDelay: BlockNumber = 4 * DAYS; pub const BountyUpdatePeriod: BlockNumber = 90 * DAYS; pub const MaximumReasonLength: u32 = 16384; pub const BountyCuratorDeposit: Permill = Permill::from_percent(50); - pub const BountyValueMinimum: Balance = 10 * DOLLARS; + pub BountyValueMinimum: Balance = 10 * dollar(NativeCurrencyId::get()); pub const MaxApprovals: u32 = 100; } @@ -780,7 +782,7 @@ impl pallet_transaction_payment::Config for Runtime { type OnChargeTransaction = FlexibleFee; type TransactionByteFee = TransactionByteFee; type OperationalFeeMultiplier = OperationalFeeMultiplier; - type WeightToFee = IdentityFee; + type WeightToFee = WeightToFee; } // culumus runtime start @@ -1100,18 +1102,18 @@ impl orml_currencies::Config for Runtime { orml_traits::parameter_type_with_key! { pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance { match currency_id { - &CurrencyId::Native(TokenSymbol::BNC) => 10 * MILLIBNC, // 0.01 BNC - &CurrencyId::Stable(TokenSymbol::KUSD) => 10 * MILLICENTS, - &CurrencyId::Token(TokenSymbol::KSM) => 10 * MILLICENTS, // 0.0001 KSM - &CurrencyId::Token(TokenSymbol::KAR) => 10 * MILLICENTS, - &CurrencyId::Token(TokenSymbol::DOT) => 100_000_000, // DOT has a decimals of 10e10, 0.01 DOT - &CurrencyId::Token(TokenSymbol::ZLK) => 1_000_000_000_000, // ZLK has a decimals of 10e18 - &CurrencyId::VSToken(TokenSymbol::KSM) => 10 * MILLICENTS, - &CurrencyId::VSToken(TokenSymbol::DOT) => 100_000_000, - &CurrencyId::VSBond(TokenSymbol::BNC, ..) => 10 * MILLICENTS, - &CurrencyId::VSBond(TokenSymbol::KSM, ..) => 10 * MILLICENTS, - &CurrencyId::VSBond(TokenSymbol::DOT, ..) => 100_000_000, - &CurrencyId::LPToken(..) => 10 * MILLICENTS, + &CurrencyId::Native(TokenSymbol::BNC) => 10 * milli(NativeCurrencyId::get()), // 0.01 BNC + &CurrencyId::Stable(TokenSymbol::KUSD) => 10 * millicent(StableCurrencyId::get()), + &CurrencyId::Token(TokenSymbol::KSM) => 10 * millicent(RelayCurrencyId::get()), // 0.0001 KSM + &CurrencyId::Token(TokenSymbol::KAR) => 10 * millicent(CurrencyId::Token(TokenSymbol::KAR)), + &CurrencyId::Token(TokenSymbol::DOT) => 1 * cent(PolkadotCurrencyId::get()), // DOT has a decimals of 10e10, 0.01 DOT + &CurrencyId::Token(TokenSymbol::ZLK) => 1 * micro(CurrencyId::Token(TokenSymbol::ZLK)), // ZLK has a decimals of 10e18 + &CurrencyId::VSToken(TokenSymbol::KSM) => 10 * millicent(RelayCurrencyId::get()), + &CurrencyId::VSToken(TokenSymbol::DOT) => 1 * cent(PolkadotCurrencyId::get()), + &CurrencyId::VSBond(TokenSymbol::BNC, ..) => 10 * millicent(NativeCurrencyId::get()), + &CurrencyId::VSBond(TokenSymbol::KSM, ..) => 10 * millicent(RelayCurrencyId::get()), + &CurrencyId::VSBond(TokenSymbol::DOT, ..) => 1 * cent(PolkadotCurrencyId::get()), + &CurrencyId::LPToken(..) => 10 * millicent(NativeCurrencyId::get()), _ => Balance::max_value() // unsupported } }; @@ -1147,6 +1149,7 @@ impl orml_tokens::Config for Runtime { parameter_types! { pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into()))); + pub RelayXcmBaseWeight: u64 = milli(RelayCurrencyId::get()) as u64; } impl orml_xtokens::Config for Runtime { @@ -1159,7 +1162,7 @@ impl orml_xtokens::Config for Runtime { type SelfLocation = SelfLocation; type XcmExecutor = XcmExecutor; type Weigher = FixedWeightBounds; - type BaseXcmWeight = XcmWeight; + type BaseXcmWeight = RelayXcmBaseWeight; } impl orml_unknown_tokens::Config for Runtime { @@ -1212,7 +1215,7 @@ impl Contains for ContributeFeeFilter { parameter_types! { pub const AltFeeCurrencyExchangeRate: (u32, u32) = (1, 100); - pub SalpWeightHolder: XcmBaseWeight = XcmBaseWeight::from(4 * XCM_WEIGHT) + ContributionWeight::get() + u64::pow(2, 24).into(); + pub SalpWeightHolder: XcmBaseWeight = XcmBaseWeight::from(4 * milli(RelayCurrencyId::get()) as u64) + ContributionWeight::get() + u64::pow(2, 24).into(); } impl bifrost_flexible_fee::Config for Runtime { @@ -1232,7 +1235,7 @@ impl bifrost_flexible_fee::Config for Runtime { type MiscFeeHandler = MiscFeeHandler< Runtime, RelayCurrencyId, - WeightToFee, + KsmWeightToFee, SalpWeightHolder, ContributeFeeFilter, >; @@ -1271,7 +1274,7 @@ pub fn create_x2_multilocation(index: u16) -> MultiLocation { } parameter_types! { - pub const MinContribution: Balance = DOLLARS / 10; + pub MinContribution: Balance = dollar(RelayCurrencyId::get()) / 10; pub const RemoveKeysLimit: u32 = 500; pub const VSBondValidPeriod: BlockNumber = 30 * DAYS; pub const ReleaseCycle: BlockNumber = 1 * DAYS; @@ -1279,9 +1282,9 @@ parameter_types! { pub const ReleaseRatio: Percent = Percent::from_percent(50); pub const SlotLength: BlockNumber = 8u32 as BlockNumber; pub const XcmTransferOrigin: TransferOriginType = TransferOriginType::FromRelayChain; - pub XcmWeight: XcmBaseWeight = XCM_WEIGHT.into(); - pub ContributionWeight:XcmBaseWeight = XCM_WEIGHT.into(); - pub AddProxyWeight:XcmBaseWeight = XCM_WEIGHT.into(); + pub XcmWeight: XcmBaseWeight = RelayXcmBaseWeight::get().into(); + pub ContributionWeight:XcmBaseWeight = RelayXcmBaseWeight::get().into(); + pub AddProxyWeight:XcmBaseWeight = RelayXcmBaseWeight::get().into(); pub ConfirmMuitiSigAccount: AccountId = hex!["e4da05f08e89bf6c43260d96f26fffcfc7deae5b465da08669a9d008e64c2c63"].into(); pub RelaychainSovereignSubAccount: MultiLocation = create_x2_multilocation(ParachainDerivedProxyAccountType::Salp as u16); pub SalpTransactType: ParachainTransactType = ParachainTransactType::Xcm; @@ -1290,7 +1293,7 @@ parameter_types! { impl bifrost_salp::Config for Runtime { type BancorPool = (); - type BifrostXcmExecutor = BifrostXcmAdaptor; + type BifrostXcmExecutor = BifrostXcmAdaptor; type Event = Event; type LeasePeriod = LeasePeriod; type MinContribution = MinContribution; @@ -1369,7 +1372,7 @@ impl bifrost_salp_lite::Config for Runtime { parameter_types! { pub const RelayChainTokenSymbolKSM: TokenSymbol = TokenSymbol::KSM; pub const RelayChainTokenSymbolDOT: TokenSymbol = TokenSymbol::DOT; - pub const MaximumDepositInPool: Balance = 1_000_000_000 * DOLLARS; + pub MaximumDepositInPool: Balance = 1_000_000_000 * dollar(NativeCurrencyId::get()); pub const MinimumDepositOfUser: Balance = 1_000_000; pub const MinimumRewardPerBlock: Balance = 1_000; pub const MinimumDuration: BlockNumber = HOURS; diff --git a/runtime/common/src/constants.rs b/runtime/common/src/constants.rs index a9bab70424..a7d90a7c0a 100644 --- a/runtime/common/src/constants.rs +++ b/runtime/common/src/constants.rs @@ -25,3 +25,52 @@ pub mod parachains { pub const KUSD_KEY: &[u8] = &[0, 129]; } } + +/// Time. +pub mod time { + use node_primitives::{BlockNumber, Moment}; + + /// Since BABE is probabilistic this is the average expected block time that + /// we are targetting. Blocks will be produced at a minimum duration defined + /// by `SLOT_DURATION`, but some slots will not be allocated to any + /// authority and hence no block will be produced. We expect to have this + /// block time on average following the defined slot duration and the value + /// of `c` configured for BABE (where `1 - c` represents the probability of + /// a slot being empty). + /// This value is only used indirectly to define the unit constants below + /// that are expressed in blocks. The rest of the code should use + /// `SLOT_DURATION` instead (like the Timestamp pallet for calculating the + /// minimum period). + /// + /// If using BABE with secondary slots (default) then all of the slots will + /// always be assigned, in which case `MILLISECS_PER_BLOCK` and + /// `SLOT_DURATION` should have the same value. + /// + /// + pub const MILLISECS_PER_BLOCK: Moment = 12000; + pub const SECS_PER_BLOCK: Moment = MILLISECS_PER_BLOCK / 1000; + + pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK; + + // 1 in 4 blocks (on average, not counting collisions) will be primary BABE blocks. + pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4); + + pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 6 * HOURS; + pub const EPOCH_DURATION_IN_SLOTS: u64 = { + const SLOT_FILL_RATE: f64 = MILLISECS_PER_BLOCK as f64 / SLOT_DURATION as f64; + + (EPOCH_DURATION_IN_BLOCKS as f64 * SLOT_FILL_RATE) as u64 + }; + + // These time units are defined in number of blocks. + pub const MINUTES: BlockNumber = 60 / (SECS_PER_BLOCK as BlockNumber); + pub const HOURS: BlockNumber = MINUTES * 60; + pub const DAYS: BlockNumber = HOURS * 24; + pub const WEEKS: BlockNumber = DAYS * 7; + + // The `LeasePeriod` defination from `polkadot`. + pub const POLKA_LEASE_PERIOD: BlockNumber = 12 * WEEKS; + pub const KUSAMA_LEASE_PERIOD: BlockNumber = 6 * WEEKS; + pub const ROCOCO_LEASE_PERIOD: BlockNumber = 1 * DAYS; + pub const WESTEND_LEASE_PERIOD: BlockNumber = 28 * DAYS; +} diff --git a/runtime/common/src/xcm_impl.rs b/runtime/common/src/impl.rs similarity index 100% rename from runtime/common/src/xcm_impl.rs rename to runtime/common/src/impl.rs diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 6109574230..e6985c3131 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -19,14 +19,17 @@ #![cfg_attr(not(feature = "std"), no_std)] use frame_support::{parameter_types, sp_runtime::traits::BlockNumberProvider}; use frame_system::{EnsureOneOf, EnsureRoot}; -use node_primitives::{AccountId, BlockNumber}; +use node_primitives::{AccountId, Balance, BlockNumber, CurrencyId, TokenInfo}; use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment}; pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_core::u32_trait::{_1, _2}; use sp_runtime::{FixedPointNumber, Perquintill}; pub mod constants; -pub mod xcm_impl; +pub mod r#impl; + +#[cfg(test)] +mod tests; pub struct RelaychainBlockNumberProvider(sp_std::marker::PhantomData); @@ -74,3 +77,41 @@ pub type EnsureRootOrAllTechnicalCommittee = EnsureOneOf< EnsureRoot, pallet_collective::EnsureProportionAtLeast<_1, _1, AccountId, TechnicalCollective>, >; + +pub fn dollar(currency_id: CurrencyId) -> Balance { + 10u128.saturating_pow(currency_id.decimals().into()) +} + +pub fn milli(currency_id: CurrencyId) -> Balance { + dollar(currency_id) / 1000 +} + +pub fn micro(currency_id: CurrencyId) -> Balance { + milli(currency_id) / 1000 +} + +pub fn cent(currency_id: CurrencyId) -> Balance { + dollar(currency_id) / 100 +} + +pub fn millicent(currency_id: CurrencyId) -> Balance { + cent(currency_id) / 1000 +} + +pub fn microcent(currency_id: CurrencyId) -> Balance { + millicent(currency_id) / 1000 +} + +pub struct RelayChainBlockNumberProvider(sp_std::marker::PhantomData); + +impl BlockNumberProvider + for RelayChainBlockNumberProvider +{ + type BlockNumber = BlockNumber; + + fn current_block_number() -> Self::BlockNumber { + cumulus_pallet_parachain_system::Pallet::::validation_data() + .map(|d| d.relay_parent_number) + .unwrap_or_default() + } +} diff --git a/runtime/common/src/tests.rs b/runtime/common/src/tests.rs new file mode 100644 index 0000000000..4f7cc1cbb0 --- /dev/null +++ b/runtime/common/src/tests.rs @@ -0,0 +1,27 @@ +// This file is part of Bifrost. + +// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use node_primitives::{CurrencyId, TokenSymbol}; + +use crate::{cent, micro}; + +#[test] +fn cal_currency_unit_by_decimal_should_work() { + assert_eq!(1 * cent(CurrencyId::Token(TokenSymbol::DOT)), 100_000_000); + assert_eq!(1 * micro(CurrencyId::Token(TokenSymbol::ZLK)), 1_000_000_000_000); +}