Skip to content

Commit

Permalink
Merge pull request #297 from justinphamnz/improvement/boosting-accumu…
Browse files Browse the repository at this point in the history
…late

Improvement/boosting accumulate
  • Loading branch information
justinphamnz authored Dec 21, 2023
2 parents 8a3324e + ecc95cf commit 114b332
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 28 deletions.
29 changes: 20 additions & 9 deletions pallets/spp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ pub mod pallet {
unlock_duration: Option<(FungibleTokenId, StakingRound)>,
iteration_limit: Option<u32>,
network_fee: Option<(FungibleTokenId, BalanceOf<T>)>,
reward_per_era: Option<BalanceOf<T>>,
current_staking_round: Option<(FungibleTokenId, StakingRound)>,
) -> DispatchResult {
T::GovernanceOrigin::ensure_origin(origin)?;

Expand All @@ -711,11 +713,11 @@ pub mod pallet {
let current_relay_chain_block = <frame_system::Pallet<T>>::block_number();
// let current_relay_chain_block = T::RelayChainBlockNumber::current_block_number();
if !update_era_frequency.is_zero() {
// ensure!(
// change > current_relay_chain_block.saturating_sub(update_era_frequency)
// && change <= current_relay_chain_block,
// Error::<T>::InvalidLastEraUpdatedBlock
// );
ensure!(
change > current_relay_chain_block.saturating_sub(update_era_frequency)
&& change <= current_relay_chain_block,
Error::<T>::InvalidLastEraUpdatedBlock
);

LastEraUpdatedBlock::<T>::put(change);
LastStakingRound::<T>::insert(FungibleTokenId::NativeToken(1), last_staking_round);
Expand Down Expand Up @@ -749,6 +751,14 @@ pub mod pallet {
Self::deposit_event(Event::<T>::NetworkFeeUpdated { currency_id, new_fee });
}

if let Some(reward_p_era) = reward_per_era {
RewardEraFrequency::<T>::put(reward_p_era);
}

if let Some((currency, current_staking_round)) = current_staking_round {
CurrentStakingRound::<T>::insert(currency, current_staking_round);
}

Ok(())
}

Expand Down Expand Up @@ -789,9 +799,7 @@ pub mod pallet {
.1
.add(total_balance.clone())
.ok_or(Error::<T>::ArithmeticOverflow)?;
voting
.prior
.accumulate(unlock_at, votes[i].1.balance.saturating_add(total_balance))
voting.prior.accumulate(unlock_at, total_balance)
}
Err(i) => {
votes.insert(i, (pool_id, vote.clone()));
Expand Down Expand Up @@ -1258,6 +1266,9 @@ impl<T: Config> Pallet<T> {
RelayChainCurrentEra::<T>::put(new_era);
// LastEraUpdatedBlock::<T>::put(T::RelayChainBlockNumber::current_block_number());
LastEraUpdatedBlock::<T>::put(<frame_system::Pallet<T>>::block_number());

CurrentStakingRound::<T>::insert(FungibleTokenId::NativeToken(1), StakingRound::Era(new_era));

Self::handle_redeem_requests(new_era)?;
Self::handle_reward_distribution_to_network_pool()?;
Self::handle_reward_distribution_to_pool_treasury(previous_era, new_era)?;
Expand All @@ -1282,7 +1293,7 @@ impl<T: Config> Pallet<T> {
}

fn do_claim_rewards(who: T::AccountId, pool_id: PoolId) -> DispatchResult {
if pool_id.is_zero() {
if !pool_id.is_zero() {
<orml_rewards::Pallet<T>>::claim_rewards(&who, &pool_id);

PendingRewards::<T>::mutate_exists(pool_id, &who, |maybe_pending_multi_rewards| {
Expand Down
96 changes: 80 additions & 16 deletions pallets/spp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ fn create_ksm_pool_works() {
.ksm_setup_for_alice_and_bob()
.build()
.execute_with(|| {

// Create the first pool
assert_ok!(SppModule::create_pool(
RuntimeOrigin::signed(ALICE),
Expand Down Expand Up @@ -77,7 +76,7 @@ fn create_ksm_pool_works() {
RuntimeOrigin::signed(BOB),
FungibleTokenId::NativeToken(1),
10,
Permill::from_percent(1)
Rate::saturating_from_rational(1, 100),
));

// Check Id will increment
Expand All @@ -88,7 +87,7 @@ fn create_ksm_pool_works() {
Pool::<Runtime>::get(next_pool_id - 1).unwrap(),
PoolInfo::<AccountId> {
creator: BOB,
commission: Permill::from_percent(1),
commission: Rate::saturating_from_rational(1, 100),
currency_id: FungibleTokenId::NativeToken(1),
max: 10
}
Expand Down Expand Up @@ -236,7 +235,8 @@ fn current_era_update_works() {
assert_eq!(SppModule::update_era_frequency(), 0);
assert_eq!(MockRelayBlockNumberProvider::current_block_number(), 0);
// Current relaychain block is 102.
MockRelayBlockNumberProvider::set(102);
// MockRelayBlockNumberProvider::set(102);
run_to_block(102);
RelayChainCurrentEra::<Runtime>::put(1);
IterationLimit::<Runtime>::put(50);
// The correct set up era config is the last era block records is 101 with duration is 100 blocks
Expand All @@ -245,7 +245,12 @@ fn current_era_update_works() {
Some(101),
Some(100),
StakingRound::Era(1),
Some(Rate::saturating_from_rational(35, 100000))
Some(Rate::saturating_from_rational(35, 100000)),
Some((FungibleTokenId::NativeToken(1), StakingRound::Era(1))),
Some(50),
Some((FungibleTokenId::NativeToken(1), 0)),
Some(100),
Some((FungibleTokenId::NativeToken(1), StakingRound::Era(1)))
));

assert_ok!(SppModule::create_pool(
Expand Down Expand Up @@ -440,12 +445,12 @@ fn boosting_works() {
assert_eq!(NetworkLedger::<Runtime>::get(FungibleTokenId::NativeToken(1)), 20000);

// Boosting works
let bob_free_balance = Balances::free_balance(BOB);
let bob_boost_balance = 1000;
assert_ok!(SppModule::boost(
RuntimeOrigin::signed(BOB),
1,
BoostInfo {
balance: bob_free_balance,
balance: bob_boost_balance,
conviction: BoostingConviction::None
}
));
Expand All @@ -454,18 +459,65 @@ fn boosting_works() {
votes: vec![(
1,
BoostInfo {
balance: bob_free_balance,
balance: bob_boost_balance,
conviction: BoostingConviction::None,
},
)],
prior: PriorLock(1, bob_free_balance),
prior: PriorLock(1, bob_boost_balance),
};
assert_eq!(boosting_of, some_record);
assert_eq!(Balances::usable_balance(&BOB), 0);
assert_eq!(Balances::usable_balance(&BOB), 99000);
let pool_1_shared_rewards = RewardsModule::shares_and_withdrawn_rewards(1, BOB);
let network_shared_rewards = RewardsModule::shares_and_withdrawn_rewards(0, BOB);
assert_eq!(pool_1_shared_rewards, (bob_free_balance, Default::default()));
assert_eq!(network_shared_rewards, (bob_free_balance, Default::default()));
assert_eq!(pool_1_shared_rewards, (bob_boost_balance, Default::default()));
assert_eq!(network_shared_rewards, (bob_boost_balance, Default::default()));

// Second boost that will make total lock 11000
assert_ok!(SppModule::boost(
RuntimeOrigin::signed(BOB),
1,
BoostInfo {
balance: 10000,
conviction: BoostingConviction::None
}
));
let second_boosting_of = BoostingOf::<Runtime>::get(BOB);
let second_boosting_record = BoostingRecord {
votes: vec![(
1,
BoostInfo {
balance: 11000,
conviction: BoostingConviction::None,
},
)],
prior: PriorLock(1, 11000),
};
let view_votes = &second_boosting_of.votes;
let debug_votes = &second_boosting_of.votes[0];
assert_eq!(second_boosting_of, second_boosting_record);

// Third boosting with lower balance than previous boost
assert_ok!(SppModule::boost(
RuntimeOrigin::signed(BOB),
1,
BoostInfo {
balance: 500,
conviction: BoostingConviction::None
}
));
let third_boosting_of = BoostingOf::<Runtime>::get(BOB);
let third_boosting_record = BoostingRecord {
votes: vec![(
1,
BoostInfo {
balance: 11500,
conviction: BoostingConviction::None,
},
)],
prior: PriorLock(1, 11500),
};

assert_eq!(third_boosting_of, third_boosting_record);
});
}

Expand All @@ -477,7 +529,8 @@ fn boosting_and_claim_reward_works() {
.execute_with(|| {
// Era config set up
// Current relaychain block is 102.
MockRelayBlockNumberProvider::set(102);
// MockRelayBlockNumberProvider::set(102);
run_to_block(102);
RelayChainCurrentEra::<Runtime>::put(1);
IterationLimit::<Runtime>::put(50);
// The correct set up era config is the last era block records is 101 with duration is 100 blocks
Expand All @@ -486,7 +539,12 @@ fn boosting_and_claim_reward_works() {
Some(101),
Some(100),
StakingRound::Era(1),
Some(Rate::saturating_from_rational(35, 100000))
Some(Rate::saturating_from_rational(35, 100000)),
Some((FungibleTokenId::NativeToken(1), StakingRound::Era(1))),
Some(50),
Some((FungibleTokenId::NativeToken(1), 0)),
Some(100),
Some((FungibleTokenId::NativeToken(1), StakingRound::Era(1)))
));

assert_ok!(SppModule::create_pool(
Expand Down Expand Up @@ -679,7 +737,8 @@ fn reward_distribution_works() {
.execute_with(|| {
// Era config set up
// Current relaychain block is 102.
MockRelayBlockNumberProvider::set(102);
// MockRelayBlockNumberProvider::set(102);
run_to_block(102);
RelayChainCurrentEra::<Runtime>::put(1);
IterationLimit::<Runtime>::put(50);
UnlockDuration::<Runtime>::insert(FungibleTokenId::NativeToken(1), StakingRound::Era(1)); // Bump current staking round to 1
Expand All @@ -690,7 +749,12 @@ fn reward_distribution_works() {
Some(101),
Some(100),
StakingRound::Era(1),
Some(Rate::saturating_from_rational(20, 100)) // Set reward rate per era is 20%.
Some(Rate::saturating_from_rational(20, 100)), // Set reward rate per era is 20%.
Some((FungibleTokenId::NativeToken(1), StakingRound::Era(1))),
Some(50),
Some((FungibleTokenId::NativeToken(1), 0)),
Some(100),
Some((FungibleTokenId::NativeToken(1), StakingRound::Era(1)))
));

assert_ok!(SppModule::create_pool(
Expand Down
4 changes: 2 additions & 2 deletions pallets/spp/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<Balance: Saturating + Copy> BoostInfo<Balance> {
}

pub fn add(&mut self, balance: Balance) -> Option<()> {
self.balance.saturating_add(balance.into());
self.balance = self.balance.saturating_add(balance.into());
Some(())
}
}
Expand All @@ -169,7 +169,7 @@ impl<BlockNumber: Ord + Copy + Zero, Balance: Ord + Copy + Zero> PriorLock<Block
/// Accumulates an additional lock.
pub fn accumulate(&mut self, until: BlockNumber, amount: Balance) {
self.0 = self.0.max(until);
self.1 = self.1.max(amount);
self.1 = self.1.add(amount);
}

pub fn locked(&self) -> Balance {
Expand Down
2 changes: 1 addition & 1 deletion runtime/metaverse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 98,
spec_version: 99,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down

0 comments on commit 114b332

Please sign in to comment.