Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Jun 4, 2024
1 parent 0c8ba15 commit 59a6d7e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 15 deletions.
7 changes: 6 additions & 1 deletion pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#![cfg_attr(not(feature = "std"), no_std)]

use astar_primitives::dapp_staking::{DAppId, EraNumber, PeriodNumber, RankedTier};
use astar_primitives::dapp_staking::{DAppId, EraNumber, PeriodNumber, RankedTier, TierId};
use astar_primitives::BlockNumber;
pub use sp_std::collections::btree_map::BTreeMap;

Expand All @@ -27,6 +27,7 @@ sp_api::decl_runtime_apis! {
/// dApp Staking Api.
///
/// Used to provide information otherwise not available via RPC.
#[api_version(2)]
pub trait DappStakingApi {

/// How many periods are there in one cycle.
Expand All @@ -42,6 +43,10 @@ sp_api::decl_runtime_apis! {
fn blocks_per_era() -> BlockNumber;

/// Get dApp tier assignment for the given dApp.
#[changed_in(2)]
fn get_dapp_tier_assignment() -> BTreeMap<DAppId, TierId>;

/// Get dApp ranked tier assignment for the given dApp.
fn get_dapp_tier_assignment() -> BTreeMap<DAppId, RankedTier>;
}
}
6 changes: 4 additions & 2 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub use sp_std::vec::Vec;
use astar_primitives::{
dapp_staking::{
AccountCheck, CycleConfiguration, DAppId, EraNumber, Observer as DAppStakingObserver,
PeriodNumber, RankedTier, SmartContractHandle, StakingRewardHandler, TierId,
PeriodNumber, Rank, RankedTier, SmartContractHandle, StakingRewardHandler, TierId,
TierSlots as TierSlotFunc,
},
oracle::PriceProvider,
Expand Down Expand Up @@ -296,6 +296,7 @@ pub mod pallet {
beneficiary: T::AccountId,
smart_contract: T::SmartContract,
tier_id: TierId,
rank: Rank,
era: EraNumber,
amount: Balance,
},
Expand Down Expand Up @@ -1418,7 +1419,7 @@ pub mod pallet {
_ => Error::<T>::InternalClaimDAppError,
})?;

let (tier_id, _rank) = ranked_tier.deconstruct();
let (tier_id, rank) = ranked_tier.deconstruct();

// Get reward destination, and deposit the reward.
let beneficiary = dapp_info.reward_beneficiary();
Expand All @@ -1432,6 +1433,7 @@ pub mod pallet {
beneficiary: beneficiary.clone(),
smart_contract,
tier_id,
rank,
era,
amount,
});
Expand Down
8 changes: 7 additions & 1 deletion pallets/dapp-staking-v3/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ mod v7 {
_,
>(|_key, old_value| {
translated.saturating_inc();

// fill rank_rewards with zero
let mut rank_rewards = Vec::new();
rank_rewards.resize_with(old_value.rewards.len(), || Balance::zero());

Some(DAppTierRewards {
dapps: old_value.dapps,
rewards: old_value.rewards,
period: old_value.period,
rank_rewards: Default::default(),
rank_rewards: BoundedVec::<Balance, T::NumberOfTiers>::try_from(rank_rewards)
.unwrap_or_default(),
})
});

Expand Down
1 change: 1 addition & 0 deletions pallets/dapp-staking-v3/src/test/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ pub(crate) fn assert_claim_dapp_reward(
beneficiary: beneficiary.clone(),
smart_contract: smart_contract.clone(),
tier_id: expected_ranked_tier.tier(),
rank: expected_ranked_tier.rank(),
era,
amount: expected_reward,
}));
Expand Down
24 changes: 13 additions & 11 deletions pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2476,7 +2476,7 @@ fn get_dapp_tier_assignment_and_rewards_basic_example_works() {
);

// There's enough reward to satisfy 100% reward per rank.
// Slot reward is 60_000 therefor expected rank reward is 6_000
// Slot reward is 60_000 therefore expected rank reward is 6_000
assert_eq!(
tier_assignment.rank_rewards,
BoundedVec::<Balance, ConstU32<4>>::try_from(vec![0, 6_000, 0, 0]).unwrap()
Expand Down Expand Up @@ -3138,12 +3138,11 @@ fn ranking_will_calc_reward_correctly() {
ExtBuilder::build().execute_with(|| {
// Tier config is specially adapted for this test.
TierConfig::<Test>::mutate(|config| {
config.number_of_slots = 40;
config.slots_per_tier = BoundedVec::try_from(vec![2, 3, 3, 20]).unwrap();
config.slots_per_tier = BoundedVec::try_from(vec![2, 3, 2, 20]).unwrap();
});

// Register smart contracts
let smart_contracts: Vec<_> = (1..=7u32)
let smart_contracts: Vec<_> = (1..=8u32)
.map(|x| {
let smart_contract = MockSmartContract::Wasm(x.into());
assert_register(x.into(), &smart_contract);
Expand All @@ -3158,7 +3157,7 @@ fn ranking_will_calc_reward_correctly() {
assert_stake(account, smart_contract, amount);
}

for (idx, amount) in [101, 102, 100, 99, 15, 16, 14].into_iter().enumerate() {
for (idx, amount) in [101, 102, 100, 99, 15, 49, 35, 14].into_iter().enumerate() {
lock_and_stake(idx, &smart_contracts[idx], amount)
}

Expand All @@ -3178,22 +3177,24 @@ fn ranking_will_calc_reward_correctly() {
(1, RankedTier::new_saturated(0, 0)),
(2, RankedTier::new_saturated(1, 10)),
(3, RankedTier::new_saturated(1, 9)),
(5, RankedTier::new_saturated(2, 9)),
(6, RankedTier::new_saturated(2, 5)),
(4, RankedTier::new_saturated(3, 0)),
(5, RankedTier::new_saturated(3, 0)),
]))
.unwrap(),
rewards: BoundedVec::try_from(vec![200_000, 100_000, 66_666, 5_000]).unwrap(),
rewards: BoundedVec::try_from(vec![200_000, 100_000, 100_000, 5_000]).unwrap(),
period: 1,
// Tier 0 has no ranking therefor no rank reward.
// Tier 0 has no ranking therefore no rank reward.
// For tier 1 there's not enough reward to satisfy 100% reward per rank.
// Only one slot is empty. Slot reward is 100_000 therefor expected rank reward is 100_000 / 19 (ranks_sum).
// Tier 2..3 has no ranking therefor no rank reward.
// Only one slot is empty. Slot reward is 100_000 therefore expected rank reward is 100_000 / 19 (ranks_sum).
// Tier 2 has ranking but there's no empty slot therefore no rank reward.
// Tier 3 has no ranking therefore no rank reward.
rank_rewards: BoundedVec::try_from(vec![0, 5_263, 0, 0]).unwrap()
}
);

// one didn't make it
assert_eq!(counter, 7);
assert_eq!(counter, 8);
})
}

Expand Down Expand Up @@ -3235,6 +3236,7 @@ fn claim_dapp_reward_with_rank() {
beneficiary: 1,
smart_contract: smart_contract.clone(),
tier_id: 1,
rank: 9,
era,
amount: expected_total_reward,
}));
Expand Down
52 changes: 52 additions & 0 deletions pallets/dapp-staking-v3/src/test/tests_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2991,3 +2991,55 @@ fn cleanup_marker_works() {
"There are pending cleanups for era reward spans."
);
}

#[test]
fn dapp_tier_rewards_with_rank() {
get_u32_type!(NumberOfDApps, 8);
get_u32_type!(NumberOfTiers, 3);

// Example dApps & rewards
let dapps = BTreeMap::<DAppId, RankedTier>::from([
(1, RankedTier::new_saturated(0, 5)),
(2, RankedTier::new_saturated(0, 0)),
(3, RankedTier::new_saturated(1, 10)),
(5, RankedTier::new_saturated(1, 5)),
(6, RankedTier::new_saturated(2, 0)),
]);
let tier_rewards = vec![300, 20, 1];
let rank_rewards = vec![0, 2, 0];
let period = 2;

let mut dapp_tier_rewards = DAppTierRewards::<NumberOfDApps, NumberOfTiers>::new(
dapps.clone(),
tier_rewards.clone(),
period,
rank_rewards.clone(),
)
.expect("Bounds are respected.");

// has rank but no reward per rank
// receive only tier reward
let ranked_tier = dapps[&1];
assert_eq!(
dapp_tier_rewards.try_claim(1),
Ok((tier_rewards[ranked_tier.tier() as usize], ranked_tier))
);

// has no rank, receive only tier reward
let ranked_tier = dapps[&2];
assert_eq!(
dapp_tier_rewards.try_claim(2),
Ok((tier_rewards[ranked_tier.tier() as usize], ranked_tier))
);

// receives both tier and rank rewards
let ranked_tier = dapps[&3];
let (tier, rank) = ranked_tier.deconstruct();
assert_eq!(
dapp_tier_rewards.try_claim(3),
Ok((
tier_rewards[tier as usize] + rank_rewards[tier as usize] * rank as Balance,
ranked_tier
))
);
}

0 comments on commit 59a6d7e

Please sign in to comment.