Skip to content

Commit

Permalink
Migrate runtime constants (#1652)
Browse files Browse the repository at this point in the history
* refactor: 💡 mv const currency

* refactor: 💡 rm time const
  • Loading branch information
yooml authored Feb 11, 2025
1 parent c83570b commit 0276ea9
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 132 deletions.
6 changes: 3 additions & 3 deletions node/service/src/chain_spec/bifrost_kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

use crate::chain_spec::{get_account_id_from_seed, get_from_seed, RelayExtensions};
use bifrost_kusama_runtime::{
constants::currency::DOLLARS, AccountId, Balance, BalancesConfig, BlockNumber, InflationInfo,
Range, SS58Prefix, VestingConfig,
AccountId, Balance, BalancesConfig, BlockNumber, InflationInfo, Range, SS58Prefix,
VestingConfig,
};
use bifrost_primitives::{
BifrostKusamaChainId, CurrencyId, CurrencyId::*, TokenInfo, TokenSymbol::*,
};
use bifrost_runtime_common::{constants::time::HOURS, AuraId};
use bifrost_runtime_common::{constants::currency::DOLLARS, constants::time::HOURS, AuraId};
use cumulus_primitives_core::ParaId;
use frame_benchmarking::{account, whitelisted_caller};
use hex_literal::hex;
Expand Down
9 changes: 5 additions & 4 deletions node/service/src/chain_spec/bifrost_polkadot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@

use crate::chain_spec::{get_account_id_from_seed, get_from_seed, RelayExtensions};
use bifrost_parachain_staking::{InflationInfo, Range};
use bifrost_polkadot_runtime::{
constants::currency::DOLLARS, AccountId, Balance, BlockNumber, SS58Prefix,
};
use bifrost_polkadot_runtime::{AccountId, Balance, BlockNumber, SS58Prefix};
use bifrost_primitives::{
currency::{BNCS, DED, IBTC, INTR, PEN, PINK, USDC, WETH},
BifrostPolkadotChainId, CurrencyId,
CurrencyId::*,
TokenInfo, TokenSymbol, ASTR, BNC, DOT, DOT_TOKEN_ID, DOT_U, FIL, GLMR, MANTA,
};
use bifrost_runtime_common::{constants::time::HOURS, AuraId};
use bifrost_runtime_common::{
constants::{currency::DOLLARS, time::HOURS},
AuraId,
};
use cumulus_primitives_core::ParaId;
use fp_evm::GenesisAccount;
use frame_benchmarking::{account, whitelisted_caller};
Expand Down
5 changes: 0 additions & 5 deletions runtime/bifrost-kusama/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ 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 MILLIBNC: Balance = 1_000_000_000;
pub const MICROBNC: Balance = 1_000_000;

pub fn deposit<Runtime: Config>(items: u32, bytes: u32) -> Balance {
items as Balance * 15 * cent::<Runtime>(CurrencyId::Native(TokenSymbol::BNC))
+ (bytes as Balance) * 6 * cent::<Runtime>(CurrencyId::Native(TokenSymbol::BNC))
Expand Down
1 change: 1 addition & 0 deletions runtime/bifrost-kusama/src/governance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod referenda;

use super::*;
pub use bifrost_runtime_common::dollar;
pub use bifrost_runtime_common::{cent, constants::currency::*, milli};
pub mod fellowship;
mod origins;
pub use origins::{
Expand Down
3 changes: 2 additions & 1 deletion runtime/bifrost-kusama/src/governance/origins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub use custom_origins::*;

#[frame_support::pallet]
pub mod custom_origins {
use crate::{Balance, BNCS};
use crate::Balance;
use bifrost_runtime_common::constants::currency::BNCS;
use frame_support::pallet_prelude::*;
use strum_macros::EnumString;

Expand Down
4 changes: 3 additions & 1 deletion runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ pub use bifrost_primitives::{
Shortfall, TimeUnit, TokenSymbol,
};
pub use bifrost_runtime_common::{
cent, constants::time::*, dollar, micro, milli, millicent, AuraId, SlowAdjustingFeeUpdate,
cent,
constants::{currency::*, time::*},
dollar, micro, milli, millicent, AuraId, SlowAdjustingFeeUpdate,
};
use bifrost_slp::QueryId;
use constants::currency::*;
Expand Down
59 changes: 1 addition & 58 deletions runtime/bifrost-paseo/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,14 @@
pub mod currency {
use crate::Runtime;
use bifrost_primitives::{Balance, CurrencyId, TokenSymbol};
use bifrost_runtime_common::{cent, milli};
use bifrost_runtime_common::{cent, constants::currency::*, milli};
use frame_support::weights::{
constants::{ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_SECOND},
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
};
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 const fn deposit(items: u32, bytes: u32) -> Balance {
items as Balance * 15 * CENTS + (bytes as Balance) * 6 * CENTS
}
Expand Down Expand Up @@ -76,51 +67,3 @@ pub mod currency {
fee_per_second / 100 * 10 / 100
}
}

/// Time.
pub mod time {
use bifrost_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.
///
/// <https://research.web3.foundation/en/latest/polkadot/block-production/Babe.html#-6.-practical-results>
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 KUSAMA_LEASE_PERIOD: BlockNumber = 6 * WEEKS;
pub const ROCOCO_LEASE_PERIOD: BlockNumber = 1 * DAYS;
pub const WESTEND_LEASE_PERIOD: BlockNumber = 28 * DAYS;
}
3 changes: 2 additions & 1 deletion runtime/bifrost-paseo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ pub use bifrost_primitives::{
RpcContributionStatus, Shortfall, TimeUnit, TokenSymbol, DOT_TOKEN_ID, GLMR_TOKEN_ID,
};
use bifrost_runtime_common::{
constants::time::*, dollar, micro, milli, AuraId, SlowAdjustingFeeUpdate,
constants::{currency::*, time::*},
dollar, micro, milli, AuraId, SlowAdjustingFeeUpdate,
};
use bifrost_slp::QueryId;
use constants::currency::*;
Expand Down
59 changes: 1 addition & 58 deletions runtime/bifrost-polkadot/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,14 @@
pub mod currency {
use crate::Runtime;
use bifrost_primitives::{Balance, CurrencyId, TokenSymbol};
use bifrost_runtime_common::{cent, milli};
use bifrost_runtime_common::{cent, constants::currency::*, milli};
use frame_support::weights::{
constants::{ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_SECOND},
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
};
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 const fn deposit(items: u32, bytes: u32) -> Balance {
items as Balance * 15 * CENTS + (bytes as Balance) * 6 * CENTS
}
Expand Down Expand Up @@ -76,51 +67,3 @@ pub mod currency {
fee_per_second / 100 * 10 / 100
}
}

/// Time.
pub mod time {
use bifrost_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.
///
/// <https://research.web3.foundation/en/latest/polkadot/block-production/Babe.html#-6.-practical-results>
pub const MILLISECS_PER_BLOCK: Moment = 6000;
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 KUSAMA_LEASE_PERIOD: BlockNumber = 6 * WEEKS;
pub const ROCOCO_LEASE_PERIOD: BlockNumber = 1 * DAYS;
pub const WESTEND_LEASE_PERIOD: BlockNumber = 28 * DAYS;
}
3 changes: 2 additions & 1 deletion runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ pub use bifrost_primitives::{
RpcContributionStatus, Shortfall, TimeUnit, TokenSymbol, DOT_TOKEN_ID, GLMR_TOKEN_ID,
};
use bifrost_runtime_common::{
constants::time::*, dollar, micro, milli, AuraId, SlowAdjustingFeeUpdate,
constants::{currency::*, time::*},
dollar, micro, milli, AuraId, SlowAdjustingFeeUpdate,
};
use bifrost_slp::QueryId;
use constants::currency::*;
Expand Down
13 changes: 13 additions & 0 deletions runtime/common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@

//! A set of constant values used for all runtimes in common.
pub mod currency {
use bifrost_primitives::Balance;

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;
}

/// Time.
pub mod time {
use bifrost_primitives::{BlockNumber, Moment};
Expand Down

0 comments on commit 0276ea9

Please sign in to comment.