Skip to content

Commit 21acad9

Browse files
authored
Expanded DappStakingApi (#1126)
* Expanded DappStakingApi * Modifications
1 parent 8fc8deb commit 21acad9

File tree

8 files changed

+53
-11
lines changed

8 files changed

+53
-11
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1212

1313
[dependencies]
1414
sp-api = { workspace = true }
15+
sp-std = { workspace = true }
1516

1617
astar-primitives = { workspace = true }
1718
pallet-dapp-staking-v3 = { workspace = true }
@@ -20,6 +21,7 @@ pallet-dapp-staking-v3 = { workspace = true }
2021
default = ["std"]
2122
std = [
2223
"sp-api/std",
24+
"sp-std/std",
2325
"pallet-dapp-staking-v3/std",
2426
"astar-primitives/std",
2527
]

pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#![cfg_attr(not(feature = "std"), no_std)]
2020

2121
use astar_primitives::BlockNumber;
22-
use pallet_dapp_staking_v3::EraNumber;
22+
use pallet_dapp_staking_v3::{DAppId, EraNumber, PeriodNumber, TierId};
23+
pub use sp_std::collections::btree_map::BTreeMap;
2324

2425
sp_api::decl_runtime_apis! {
2526

@@ -28,6 +29,9 @@ sp_api::decl_runtime_apis! {
2829
/// Used to provide information otherwise not available via RPC.
2930
pub trait DappStakingApi {
3031

32+
/// How many periods are there in one cycle.
33+
fn periods_per_cycle() -> PeriodNumber;
34+
3135
/// For how many standard era lengths does the voting subperiod last.
3236
fn eras_per_voting_subperiod() -> EraNumber;
3337

@@ -36,5 +40,8 @@ sp_api::decl_runtime_apis! {
3640

3741
/// How many blocks are there per standard era.
3842
fn blocks_per_era() -> BlockNumber;
43+
44+
/// Get dApp tier assignment for the given dApp.
45+
fn get_dapp_tier_assignment() -> BTreeMap<DAppId, TierId>;
3946
}
4047
}

pallets/dapp-staking-v3/src/benchmarking/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,11 @@ mod benchmarks {
946946

947947
#[block]
948948
{
949-
let (dapp_tiers, _) =
950-
Pallet::<T>::get_dapp_tier_assignment(reward_era, reward_period, reward_pool);
949+
let (dapp_tiers, _) = Pallet::<T>::get_dapp_tier_assignment_and_rewards(
950+
reward_era,
951+
reward_period,
952+
reward_pool,
953+
);
951954
assert_eq!(dapp_tiers.dapps.len(), x as usize);
952955
}
953956
}

pallets/dapp-staking-v3/src/lib.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,19 @@ pub mod pallet {
16371637
T::CycleConfiguration::blocks_per_era().saturating_mul(T::UnlockingPeriod::get().into())
16381638
}
16391639

1640+
/// Returns the dApp tier assignment for the current era, based on the current stake amounts.
1641+
pub fn get_dapp_tier_assignment() -> BTreeMap<DAppId, TierId> {
1642+
let protocol_state = ActiveProtocolState::<T>::get();
1643+
1644+
let (dapp_tiers, _count) = Self::get_dapp_tier_assignment_and_rewards(
1645+
protocol_state.era,
1646+
protocol_state.period_number(),
1647+
Balance::zero(),
1648+
);
1649+
1650+
dapp_tiers.dapps.into_inner()
1651+
}
1652+
16401653
/// Assign eligible dApps into appropriate tiers, and calculate reward for each tier.
16411654
///
16421655
/// ### Algorithm
@@ -1666,7 +1679,7 @@ pub mod pallet {
16661679
///
16671680
/// The returned object contains information about each dApp that made it into a tier.
16681681
/// Alongside tier assignment info, number of read DB contract stake entries is returned.
1669-
pub(crate) fn get_dapp_tier_assignment(
1682+
pub(crate) fn get_dapp_tier_assignment_and_rewards(
16701683
era: EraNumber,
16711684
period: PeriodNumber,
16721685
dapp_reward_pool: Balance,
@@ -1835,7 +1848,7 @@ pub mod pallet {
18351848
// To help with benchmarking, it's possible to omit real tier calculation using the `Dummy` approach.
18361849
// This must never be used in production code, obviously.
18371850
let (dapp_tier_rewards, counter) = match tier_assignment {
1838-
TierAssignment::Real => Self::get_dapp_tier_assignment(
1851+
TierAssignment::Real => Self::get_dapp_tier_assignment_and_rewards(
18391852
current_era,
18401853
protocol_state.period_number(),
18411854
dapp_reward_pool,

pallets/dapp-staking-v3/src/test/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,7 @@ fn force_with_incorrect_origin_fails() {
22622262
}
22632263

22642264
#[test]
2265-
fn get_dapp_tier_assignment_basic_example_works() {
2265+
fn get_dapp_tier_assignment_and_rewards_basic_example_works() {
22662266
ExtBuilder::build().execute_with(|| {
22672267
// This test will rely on the configuration inside the mock file.
22682268
// If that changes, this test will have to be updated as well.
@@ -2339,7 +2339,7 @@ fn get_dapp_tier_assignment_basic_example_works() {
23392339
// Finally, the actual test
23402340
let protocol_state = ActiveProtocolState::<Test>::get();
23412341
let dapp_reward_pool = 1000000;
2342-
let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment(
2342+
let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment_and_rewards(
23432343
protocol_state.era + 1,
23442344
protocol_state.period_number(),
23452345
dapp_reward_pool,
@@ -2393,7 +2393,7 @@ fn get_dapp_tier_assignment_basic_example_works() {
23932393
}
23942394

23952395
#[test]
2396-
fn get_dapp_tier_assignment_zero_slots_per_tier_works() {
2396+
fn get_dapp_tier_assignment_and_rewards_zero_slots_per_tier_works() {
23972397
ExtBuilder::build().execute_with(|| {
23982398
// This test will rely on the configuration inside the mock file.
23992399
// 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() {
24082408
// Calculate tier assignment (we don't need dApps for this test)
24092409
let protocol_state = ActiveProtocolState::<Test>::get();
24102410
let dapp_reward_pool = 1000000;
2411-
let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment(
2411+
let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment_and_rewards(
24122412
protocol_state.era,
24132413
protocol_state.period_number(),
24142414
dapp_reward_pool,

runtime/local/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use sp_runtime::{
5858
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
5959
ApplyExtrinsicResult, FixedPointNumber, Perbill, Permill, Perquintill, RuntimeDebug,
6060
};
61-
use sp_std::prelude::*;
61+
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
6262

6363
use astar_primitives::{
6464
dapp_staking::{CycleConfiguration, SmartContract},
@@ -1728,6 +1728,10 @@ impl_runtime_apis! {
17281728
}
17291729

17301730
impl dapp_staking_v3_runtime_api::DappStakingApi<Block> for Runtime {
1731+
fn periods_per_cycle() -> pallet_dapp_staking_v3::PeriodNumber {
1732+
InflationCycleConfig::periods_per_cycle()
1733+
}
1734+
17311735
fn eras_per_voting_subperiod() -> pallet_dapp_staking_v3::EraNumber {
17321736
InflationCycleConfig::eras_per_voting_subperiod()
17331737
}
@@ -1739,6 +1743,10 @@ impl_runtime_apis! {
17391743
fn blocks_per_era() -> BlockNumber {
17401744
InflationCycleConfig::blocks_per_era()
17411745
}
1746+
1747+
fn get_dapp_tier_assignment() -> BTreeMap<pallet_dapp_staking_v3::DAppId, pallet_dapp_staking_v3::TierId> {
1748+
DappStaking::get_dapp_tier_assignment()
1749+
}
17421750
}
17431751

17441752
#[cfg(feature = "runtime-benchmarks")]

runtime/shibuya/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use sp_runtime::{
6565
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
6666
ApplyExtrinsicResult, FixedPointNumber, Perbill, Permill, Perquintill, RuntimeDebug,
6767
};
68-
use sp_std::prelude::*;
68+
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
6969

7070
use astar_primitives::{
7171
dapp_staking::{CycleConfiguration, SmartContract},
@@ -1932,6 +1932,10 @@ impl_runtime_apis! {
19321932
}
19331933

19341934
impl dapp_staking_v3_runtime_api::DappStakingApi<Block> for Runtime {
1935+
fn periods_per_cycle() -> pallet_dapp_staking_v3::PeriodNumber {
1936+
InflationCycleConfig::periods_per_cycle()
1937+
}
1938+
19351939
fn eras_per_voting_subperiod() -> pallet_dapp_staking_v3::EraNumber {
19361940
InflationCycleConfig::eras_per_voting_subperiod()
19371941
}
@@ -1943,6 +1947,10 @@ impl_runtime_apis! {
19431947
fn blocks_per_era() -> BlockNumber {
19441948
InflationCycleConfig::blocks_per_era()
19451949
}
1950+
1951+
fn get_dapp_tier_assignment() -> BTreeMap<pallet_dapp_staking_v3::DAppId, pallet_dapp_staking_v3::TierId> {
1952+
DappStaking::get_dapp_tier_assignment()
1953+
}
19461954
}
19471955

19481956
#[cfg(feature = "runtime-benchmarks")]

0 commit comments

Comments
 (0)