Skip to content

Commit

Permalink
fix: fix zero pending amount
Browse files Browse the repository at this point in the history
  • Loading branch information
Akagi201 committed Jul 4, 2022
1 parent c6e3d3f commit d47369d
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 34 deletions.
40 changes: 26 additions & 14 deletions pallets/system-staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,46 @@

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

use frame_benchmarking::{benchmarks, whitelisted_caller};
use frame_benchmarking::{benchmarks, vec, whitelisted_caller};
use frame_support::sp_runtime::{Perbill, Permill};
use frame_system::RawOrigin;
use node_primitives::{CurrencyId, TokenSymbol};
use sp_arithmetic::per_things::Permill;
use node_primitives::{CurrencyId, PoolId, TokenSymbol};

use frame_support::{sp_runtime::traits::UniqueSaturatedFrom, traits::OnInitialize};

use crate::{Pallet as SystemStaking, *};

benchmarks! {
on_initialize {}:{SystemStaking::<T>::on_initialize(T::BlockNumber::from(101u32));}
on_initialize {}:{SystemStaking::<T>::on_initialize(T::BlockNumber::from(101u32));}

token_config {
token_config {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
}: _(RawOrigin::Root, KSM, Some(1), Some(Permill::from_percent(80)),Some(false),Some(token_amount),None,None)
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
let pool_id = PoolId::from(1u32);
}: _(RawOrigin::Root, KSM, Some(1), Some(Permill::from_percent(80)),Some(false),Some(token_amount),Some(vec![pool_id]),Some(vec![Perbill::from_percent(100)]))

refresh_token_info {
refresh_token_info {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
}: _(RawOrigin::Root,KSM)

payout {
payout {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
}: _(RawOrigin::Root,KSM)

on_redeem_success {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
let caller: T::AccountId = whitelisted_caller();
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
}:{SystemStaking::<T>::on_redeem_success(KSM,caller,token_amount);}
on_redeem_success {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
let caller: T::AccountId = whitelisted_caller();
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
}:{SystemStaking::<T>::on_redeem_success(KSM,caller,token_amount);}

on_redeemed {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
let caller: T::AccountId = whitelisted_caller();
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
let fee_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
}:{SystemStaking::<T>::on_redeemed(caller,KSM,token_amount,token_amount,fee_amount);}

delete_token {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
}: _(RawOrigin::Root,KSM)
}
55 changes: 35 additions & 20 deletions pallets/system-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ pub type BalanceOf<T: Config> =
pub mod pallet {
use super::*;
use crate::{RoundInfo, TokenInfo};
use frame_support::pallet_prelude::*;
use frame_support::{
pallet_prelude::*,
sp_runtime::{Perbill, Permill},
};
use frame_system::pallet_prelude::*;
use sp_arithmetic::{Perbill, Permill};

pub type RoundIndex = u32;

Expand Down Expand Up @@ -180,14 +182,6 @@ pub mod pallet {
system_shadow_amount: BalanceOf<T>,
pending_redeem_amount: BalanceOf<T>,
},
RedeemSuccess {
token: CurrencyIdOf<T>,
amount: BalanceOf<T>,
farming_staking_amount: BalanceOf<T>,
system_stakable_amount: BalanceOf<T>,
system_shadow_amount: BalanceOf<T>,
pending_redeem_amount: BalanceOf<T>,
},
Redeemed {
token: CurrencyIdOf<T>,
amount: BalanceOf<T>,
Expand Down Expand Up @@ -366,6 +360,23 @@ pub mod pallet {
Ok(().into())
}

/// token config,take effect when next round begins
#[pallet::weight(<T as Config>::WeightInfo::delete_token())]
pub fn delete_token(
origin: OriginFor<T>,
token: CurrencyIdOf<T>,
) -> DispatchResultWithPostInfo {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?; // Motion

<TokenStatus<T>>::remove(&token);

let mut token_list = Self::token_list();
token_list.retain(|&x| x != token);
<TokenList<T>>::put(token_list);

Ok(().into())
}

/// refresh token info,query farming pallet, and update TokenInfo, change to new
/// config,ignore exec_delay, execute immediately
#[pallet::weight(<T as Config>::WeightInfo::refresh_token_info())]
Expand Down Expand Up @@ -468,6 +479,10 @@ impl<T: Config> Pallet<T> {
.saturating_sub(token_info.current_config.system_stakable_base)
};
token_info.system_stakable_amount = stakable_amount;

// write state before other calls
<TokenStatus<T>>::insert(&token_id, token_info.clone());

if stakable_amount >
token_info.system_shadow_amount.saturating_sub(token_info.pending_redeem_amount)
{
Expand All @@ -478,6 +493,8 @@ impl<T: Config> Pallet<T> {
Ok(_) =>
match T::VtokenMintingInterface::mint(account.clone(), token_id, mint_amount) {
Ok(_) => {
token_info.system_shadow_amount =
token_info.system_shadow_amount.saturating_add(mint_amount);
Self::deposit_event(Event::MintSuccess {
token: token_id,
amount: mint_amount,
Expand All @@ -486,8 +503,6 @@ impl<T: Config> Pallet<T> {
system_shadow_amount: token_info.system_shadow_amount,
pending_redeem_amount: token_info.pending_redeem_amount,
});
token_info.system_shadow_amount =
token_info.system_shadow_amount.saturating_add(mint_amount);
},
Err(error) => {
Self::deposit_event(Event::MintFailed {
Expand Down Expand Up @@ -556,14 +571,14 @@ impl<T: Config> Pallet<T> {
match T::VtokenMintingInterface::redeem(account, vtoken_id, vredeem_amount)
{
Ok(_) => {
Self::deposit_event(Event::RedeemSuccess {
token: token_id,
amount: vredeem_amount,
farming_staking_amount: token_info.farming_staking_amount,
system_stakable_amount: token_info.system_stakable_amount,
system_shadow_amount: token_info.system_shadow_amount,
pending_redeem_amount: token_info.pending_redeem_amount,
});
let new_token_info =
if let Some(state) = <TokenStatus<T>>::get(&token_id) {
state
} else {
<TokenInfo<BalanceOf<T>>>::default()
};
token_info.pending_redeem_amount =
new_token_info.pending_redeem_amount;
},
Err(error) => {
Self::deposit_event(Event::RedeemFailed {
Expand Down
110 changes: 110 additions & 0 deletions pallets/system-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use frame_support::{
assert_ok,
sp_runtime::{Perbill, Permill},
};
use node_primitives::{TimeUnit, VtokenMintingOperator};
use sp_std::{collections::btree_map::BTreeMap, prelude::*};

#[test]
fn token_config_should_work() {
Expand All @@ -46,6 +48,53 @@ fn token_config_should_work() {
});
}

#[test]
fn delete_token_should_work() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
assert_ok!(SystemStaking::token_config(
Origin::root(),
KSM,
Some(1),
Some(Permill::from_percent(80)),
Some(false),
Some(100),
None,
None,
));

assert_ok!(SystemStaking::token_config(
Origin::root(),
MOVR,
Some(2),
Some(Permill::from_percent(80)),
Some(false),
Some(100),
None,
None,
));

assert_ok!(SystemStaking::token_config(
Origin::root(),
MOVR,
Some(2),
Some(Permill::from_percent(80)),
Some(false),
Some(100),
None,
None,
));

assert_ok!(SystemStaking::delete_token(Origin::root(), MOVR,));

assert!(<TokenStatus<Runtime>>::get(MOVR).is_none());
assert!(<TokenStatus<Runtime>>::get(KSM).is_some());
let token_list = <TokenList<Runtime>>::get();
assert_eq!(token_list.len(), 1);
assert!(!token_list.clone().into_inner().contains(&MOVR));
assert!(token_list.into_inner().contains(&KSM));
});
}

#[test]
fn round_info_should_correct() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
Expand All @@ -66,3 +115,64 @@ fn round_info_should_correct() {
assert_eq!(SystemStaking::round().unwrap().first, 1001);
});
}

#[test]
fn refresh_token_info_should_work() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
let (pid, _tokens) = init_farming_no_gauge();

assert_ok!(VtokenMinting::set_minimum_mint(Origin::signed(ALICE), KSM, 10));
pub const FEE: Permill = Permill::from_percent(5);
assert_ok!(VtokenMinting::set_fees(Origin::root(), FEE, FEE));
assert_ok!(VtokenMinting::set_unlock_duration(
Origin::signed(ALICE),
KSM,
TimeUnit::Era(1)
));
assert_ok!(VtokenMinting::increase_token_pool(KSM, 1000));
assert_ok!(VtokenMinting::update_ongoing_time_unit(KSM, TimeUnit::Era(1)));
assert_ok!(VtokenMinting::set_minimum_redeem(Origin::signed(ALICE), vKSM, 10));

assert_ok!(SystemStaking::token_config(
Origin::root(),
KSM,
Some(1),
Some(Permill::from_percent(80)),
Some(false),
Some(100),
Some(vec![pid]),
Some(vec![Perbill::from_percent(100)]),
));

assert_ok!(SystemStaking::refresh_token_info(Origin::root(), KSM));
let token_info = <TokenStatus<Runtime>>::get(KSM).unwrap();
assert_eq!(token_info.new_config, token_info.current_config);
});
}

fn init_farming_no_gauge() -> (PoolId, BalanceOf<Runtime>) {
let mut tokens_proportion_map = BTreeMap::<CurrencyIdOf<Runtime>, Perbill>::new();
tokens_proportion_map.entry(KSM).or_insert(Perbill::from_percent(100));
let tokens_proportion = vec![(KSM, Perbill::from_percent(100))];
let tokens = 1000;
let basic_rewards = vec![(KSM, 1000)];
let gauge_basic_rewards = vec![(KSM, 1000)];

assert_ok!(Farming::create_farming_pool(
Origin::signed(ALICE),
tokens_proportion.clone(),
basic_rewards.clone(),
Some((KSM, 1000, gauge_basic_rewards)),
0,
0,
10,
0,
1
));

let pid = 0;
let charge_rewards = vec![(KSM, 100000)];
assert_ok!(Farming::charge(Origin::signed(BOB), pid, charge_rewards));
assert_ok!(Farming::deposit(Origin::signed(ALICE), pid, tokens.clone(), None));
(pid, tokens)
}
5 changes: 5 additions & 0 deletions pallets/system-staking/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn on_initialize(x: u32) -> Weight;
fn token_config() -> Weight;
fn delete_token() -> Weight;
fn refresh_token_info() -> Weight;
fn payout() -> Weight;
fn on_redeem_success() -> Weight;
Expand All @@ -46,6 +47,10 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
(50_000_000 as Weight)
}

fn delete_token() -> Weight {
(50_000_000 as Weight)
}

fn refresh_token_info() -> Weight {
(100_000_000 as Weight)
}
Expand Down

0 comments on commit d47369d

Please sign in to comment.