diff --git a/Cargo.lock b/Cargo.lock index 930482cc2f..01bdaedad3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2442,6 +2442,7 @@ dependencies = [ "astar-primitives", "pallet-dapp-staking-v3", "sp-api", + "sp-std", ] [[package]] diff --git a/pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml b/pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml index 559d5f299b..63ef539a0c 100644 --- a/pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml +++ b/pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml @@ -12,6 +12,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] sp-api = { workspace = true } +sp-std = { workspace = true } astar-primitives = { workspace = true } pallet-dapp-staking-v3 = { workspace = true } @@ -20,6 +21,7 @@ pallet-dapp-staking-v3 = { workspace = true } default = ["std"] std = [ "sp-api/std", + "sp-std/std", "pallet-dapp-staking-v3/std", "astar-primitives/std", ] diff --git a/pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs b/pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs index dde32bf695..d829e2b275 100644 --- a/pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs +++ b/pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs @@ -19,7 +19,8 @@ #![cfg_attr(not(feature = "std"), no_std)] use astar_primitives::BlockNumber; -use pallet_dapp_staking_v3::EraNumber; +use pallet_dapp_staking_v3::{DAppId, EraNumber, PeriodNumber, TierId}; +pub use sp_std::collections::btree_map::BTreeMap; sp_api::decl_runtime_apis! { @@ -28,6 +29,9 @@ sp_api::decl_runtime_apis! { /// Used to provide information otherwise not available via RPC. pub trait DappStakingApi { + /// How many periods are there in one cycle. + fn periods_per_cycle() -> PeriodNumber; + /// For how many standard era lengths does the voting subperiod last. fn eras_per_voting_subperiod() -> EraNumber; @@ -36,5 +40,8 @@ sp_api::decl_runtime_apis! { /// How many blocks are there per standard era. fn blocks_per_era() -> BlockNumber; + + /// Get dApp tier assignment for the given dApp. + fn get_dapp_tier_assignment() -> BTreeMap; } } diff --git a/pallets/dapp-staking-v3/src/benchmarking/mod.rs b/pallets/dapp-staking-v3/src/benchmarking/mod.rs index 11663401d9..abb37ee071 100644 --- a/pallets/dapp-staking-v3/src/benchmarking/mod.rs +++ b/pallets/dapp-staking-v3/src/benchmarking/mod.rs @@ -946,8 +946,11 @@ mod benchmarks { #[block] { - let (dapp_tiers, _) = - Pallet::::get_dapp_tier_assignment(reward_era, reward_period, reward_pool); + let (dapp_tiers, _) = Pallet::::get_dapp_tier_assignment_and_rewards( + reward_era, + reward_period, + reward_pool, + ); assert_eq!(dapp_tiers.dapps.len(), x as usize); } } diff --git a/pallets/dapp-staking-v3/src/lib.rs b/pallets/dapp-staking-v3/src/lib.rs index f45abf32ec..81f50d8776 100644 --- a/pallets/dapp-staking-v3/src/lib.rs +++ b/pallets/dapp-staking-v3/src/lib.rs @@ -1637,6 +1637,19 @@ pub mod pallet { T::CycleConfiguration::blocks_per_era().saturating_mul(T::UnlockingPeriod::get().into()) } + /// Returns the dApp tier assignment for the current era, based on the current stake amounts. + pub fn get_dapp_tier_assignment() -> BTreeMap { + let protocol_state = ActiveProtocolState::::get(); + + let (dapp_tiers, _count) = Self::get_dapp_tier_assignment_and_rewards( + protocol_state.era, + protocol_state.period_number(), + Balance::zero(), + ); + + dapp_tiers.dapps.into_inner() + } + /// Assign eligible dApps into appropriate tiers, and calculate reward for each tier. /// /// ### Algorithm @@ -1666,7 +1679,7 @@ pub mod pallet { /// /// The returned object contains information about each dApp that made it into a tier. /// Alongside tier assignment info, number of read DB contract stake entries is returned. - pub(crate) fn get_dapp_tier_assignment( + pub(crate) fn get_dapp_tier_assignment_and_rewards( era: EraNumber, period: PeriodNumber, dapp_reward_pool: Balance, @@ -1835,7 +1848,7 @@ pub mod pallet { // To help with benchmarking, it's possible to omit real tier calculation using the `Dummy` approach. // This must never be used in production code, obviously. let (dapp_tier_rewards, counter) = match tier_assignment { - TierAssignment::Real => Self::get_dapp_tier_assignment( + TierAssignment::Real => Self::get_dapp_tier_assignment_and_rewards( current_era, protocol_state.period_number(), dapp_reward_pool, diff --git a/pallets/dapp-staking-v3/src/test/tests.rs b/pallets/dapp-staking-v3/src/test/tests.rs index 8839753b04..a6f8a9700f 100644 --- a/pallets/dapp-staking-v3/src/test/tests.rs +++ b/pallets/dapp-staking-v3/src/test/tests.rs @@ -2262,7 +2262,7 @@ fn force_with_incorrect_origin_fails() { } #[test] -fn get_dapp_tier_assignment_basic_example_works() { +fn get_dapp_tier_assignment_and_rewards_basic_example_works() { ExtBuilder::build().execute_with(|| { // This test will rely on the configuration inside the mock file. // If that changes, this test will have to be updated as well. @@ -2339,7 +2339,7 @@ fn get_dapp_tier_assignment_basic_example_works() { // Finally, the actual test let protocol_state = ActiveProtocolState::::get(); let dapp_reward_pool = 1000000; - let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment( + let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment_and_rewards( protocol_state.era + 1, protocol_state.period_number(), dapp_reward_pool, @@ -2393,7 +2393,7 @@ fn get_dapp_tier_assignment_basic_example_works() { } #[test] -fn get_dapp_tier_assignment_zero_slots_per_tier_works() { +fn get_dapp_tier_assignment_and_rewards_zero_slots_per_tier_works() { ExtBuilder::build().execute_with(|| { // This test will rely on the configuration inside the mock file. // If that changes, this test might have to be updated as well. @@ -2408,7 +2408,7 @@ fn get_dapp_tier_assignment_zero_slots_per_tier_works() { // Calculate tier assignment (we don't need dApps for this test) let protocol_state = ActiveProtocolState::::get(); let dapp_reward_pool = 1000000; - let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment( + let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment_and_rewards( protocol_state.era, protocol_state.period_number(), dapp_reward_pool, diff --git a/runtime/local/src/lib.rs b/runtime/local/src/lib.rs index 7ce5ad0470..da78c900fb 100644 --- a/runtime/local/src/lib.rs +++ b/runtime/local/src/lib.rs @@ -58,7 +58,7 @@ use sp_runtime::{ transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError}, ApplyExtrinsicResult, FixedPointNumber, Perbill, Permill, Perquintill, RuntimeDebug, }; -use sp_std::prelude::*; +use sp_std::{collections::btree_map::BTreeMap, prelude::*}; use astar_primitives::{ dapp_staking::{CycleConfiguration, SmartContract}, @@ -1728,6 +1728,10 @@ impl_runtime_apis! { } impl dapp_staking_v3_runtime_api::DappStakingApi for Runtime { + fn periods_per_cycle() -> pallet_dapp_staking_v3::PeriodNumber { + InflationCycleConfig::periods_per_cycle() + } + fn eras_per_voting_subperiod() -> pallet_dapp_staking_v3::EraNumber { InflationCycleConfig::eras_per_voting_subperiod() } @@ -1739,6 +1743,10 @@ impl_runtime_apis! { fn blocks_per_era() -> BlockNumber { InflationCycleConfig::blocks_per_era() } + + fn get_dapp_tier_assignment() -> BTreeMap { + DappStaking::get_dapp_tier_assignment() + } } #[cfg(feature = "runtime-benchmarks")] diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index ed341d10af..0955c63642 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -65,7 +65,7 @@ use sp_runtime::{ transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError}, ApplyExtrinsicResult, FixedPointNumber, Perbill, Permill, Perquintill, RuntimeDebug, }; -use sp_std::prelude::*; +use sp_std::{collections::btree_map::BTreeMap, prelude::*}; use astar_primitives::{ dapp_staking::{CycleConfiguration, SmartContract}, @@ -1932,6 +1932,10 @@ impl_runtime_apis! { } impl dapp_staking_v3_runtime_api::DappStakingApi for Runtime { + fn periods_per_cycle() -> pallet_dapp_staking_v3::PeriodNumber { + InflationCycleConfig::periods_per_cycle() + } + fn eras_per_voting_subperiod() -> pallet_dapp_staking_v3::EraNumber { InflationCycleConfig::eras_per_voting_subperiod() } @@ -1943,6 +1947,10 @@ impl_runtime_apis! { fn blocks_per_era() -> BlockNumber { InflationCycleConfig::blocks_per_era() } + + fn get_dapp_tier_assignment() -> BTreeMap { + DappStaking::get_dapp_tier_assignment() + } } #[cfg(feature = "runtime-benchmarks")]