Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 notify_rewards #1496

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 10 additions & 40 deletions pallets/bb-bnc/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ mod benchmarks {
BalanceOf::<T>::unique_saturated_from(100_000_000_000_000u128),
)?;

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

assert_ok!(BbBNC::<T>::notify_rewards(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
Expand Down Expand Up @@ -110,10 +107,7 @@ mod benchmarks {
BalanceOf::<T>::unique_saturated_from(100_000_000_000_000u128),
)?;

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

assert_ok!(BbBNC::<T>::notify_rewards(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
Expand Down Expand Up @@ -160,10 +154,7 @@ mod benchmarks {
BalanceOf::<T>::unique_saturated_from(100_000_000_000_000u128),
)?;

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

assert_ok!(BbBNC::<T>::notify_rewards(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
Expand Down Expand Up @@ -206,10 +197,7 @@ mod benchmarks {
BalanceOf::<T>::unique_saturated_from(100_000_000_000_000u128),
)?;

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

assert_ok!(BbBNC::<T>::notify_rewards(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
Expand Down Expand Up @@ -254,10 +242,7 @@ mod benchmarks {
BalanceOf::<T>::unique_saturated_from(100_000_000_000_000u128),
)?;

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

assert_ok!(BbBNC::<T>::notify_rewards(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
Expand Down Expand Up @@ -287,10 +272,7 @@ mod benchmarks {
Some((7 * 86400 / 12u32).into())
));

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

T::MultiCurrency::deposit(
CurrencyId::Native(TokenSymbol::BNC),
Expand Down Expand Up @@ -351,10 +333,7 @@ mod benchmarks {
BalanceOf::<T>::unique_saturated_from(100_000_000_000_000u128),
)?;

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

assert_ok!(BbBNC::<T>::notify_rewards(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
Expand Down Expand Up @@ -409,10 +388,7 @@ mod benchmarks {
BalanceOf::<T>::unique_saturated_from(100_000_000_000_000u128),
)?;

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

assert_ok!(BbBNC::<T>::notify_rewards(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
Expand Down Expand Up @@ -470,10 +446,7 @@ mod benchmarks {
BalanceOf::<T>::unique_saturated_from(100_000_000_000_000u128),
)?;

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

assert_ok!(BbBNC::<T>::notify_rewards(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
Expand Down Expand Up @@ -531,10 +504,7 @@ mod benchmarks {
BalanceOf::<T>::unique_saturated_from(100_000_000_000_000u128),
)?;

let rewards = vec![(
CurrencyId::Native(TokenSymbol::BNC),
BalanceOf::<T>::unique_saturated_from(10_000_000_000_000u128),
)];
let rewards = vec![CurrencyId::Native(TokenSymbol::BNC)];

assert_ok!(BbBNC::<T>::notify_rewards(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
Expand Down
4 changes: 2 additions & 2 deletions pallets/bb-bnc/src/incentive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct IncentiveConfig<CurrencyId, Balance, BlockNumber, AccountId> {
/// the system account.
pub incentive_controller: Option<AccountId>,
/// When a round is started, the value to be transferred will be obtained from this field.
pub last_reward: Vec<(CurrencyId, Balance)>,
pub last_reward: Vec<CurrencyId>,
}

impl<CurrencyId, Balance, BlockNumber, AccountId> Default
Expand Down Expand Up @@ -254,7 +254,7 @@ impl<T: Config> Pallet<T> {
pub fn notify_reward_amount(
pool_id: PoolId,
who: &Option<AccountIdOf<T>>,
rewards: Vec<(CurrencyIdOf<T>, BalanceOf<T>)>,
rewards: Vec<CurrencyIdOf<T>>,
) -> DispatchResult {
let account = match who {
Some(who) => who,
Expand Down
6 changes: 3 additions & 3 deletions pallets/bb-bnc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ pub mod pallet {
IncentiveConfig<CurrencyIdOf<T>, BalanceOf<T>, BlockNumberFor<T>, AccountIdOf<T>>,
},
/// The rewards for this round have been added to the system account.
RewardAdded { rewards: Vec<(CurrencyIdOf<T>, BalanceOf<T>)> },
RewardAdded { rewards: Vec<CurrencyIdOf<T>> },
/// The user has received the reward.
Rewarded { who: AccountIdOf<T>, rewards: Vec<(CurrencyIdOf<T>, BalanceOf<T>)> },
/// This currency_id has been refreshed.
AllRefreshed { currency_id: CurrencyIdOf<T> },
/// This currency_id has been partially refreshed.
PartiallyRefreshed { currency_id: CurrencyIdOf<T> },
/// Notify reward failed.
NotifyRewardFailed { rewards: Vec<(CurrencyIdOf<T>, BalanceOf<T>)> },
NotifyRewardFailed { rewards: Vec<CurrencyIdOf<T>> },
/// Markup has been deposited.
MarkupDeposited {
/// The user who deposited
Expand Down Expand Up @@ -524,7 +524,7 @@ pub mod pallet {
origin: OriginFor<T>,
incentive_from: AccountIdOf<T>,
rewards_duration: Option<BlockNumberFor<T>>,
rewards: Vec<(CurrencyIdOf<T>, BalanceOf<T>)>,
rewards: Vec<CurrencyIdOf<T>>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;
Self::set_incentive(
Expand Down
2 changes: 1 addition & 1 deletion pallets/bb-bnc/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl ExtBuilder {
self.balances(vec![
(ALICE, BNC, 1_000_000_000_000),
(ALICE, VBNC, 1_000_000_000_000_000),
(ALICE, KSM, 1_000_000_000_000),
(ALICE, KSM, 1_000_000_000),
(BOB, BNC, 1_000_000_000_000),
(BOB, VBNC, 1_000_000_000_000_000),
(BOB, VKSM, 1_000_000_000_000),
Expand Down
5 changes: 3 additions & 2 deletions pallets/bb-bnc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn notify_reward_amount() {
assert_ok!(BbBNC::increase_amount(RuntimeOrigin::signed(BOB), 0, 80_000_000_000));
assert_eq!(BbBNC::balance_of(&BOB, None), Ok(99715627680));

let rewards = vec![(KSM, 1_000_000_000)];
let rewards = vec![KSM];
assert_ok!(BbBNC::notify_rewards(
RuntimeOrigin::root(),
ALICE,
Expand All @@ -255,6 +255,7 @@ fn notify_reward_amount() {
System::set_block_number(System::block_number() + 7 * 86400 / 12);
assert_ok!(BbBNC::get_rewards_inner(BB_BNC_SYSTEM_POOL_ID, &BOB, None));
assert_eq!(Tokens::free_balance(KSM, &BOB), 999986398);
assert_ok!(Tokens::deposit(KSM, &ALICE, 1_000_000_000));
assert_ok!(BbBNC::notify_rewards(
RuntimeOrigin::root(),
ALICE,
Expand Down Expand Up @@ -289,7 +290,7 @@ fn create_lock_to_withdraw() {
Some(4 * 365 * 86400 / 12),
Some(14 * 86400 / 12)
));
let rewards = vec![(KSM, 1000)];
let rewards = vec![KSM];
assert_ok!(BbBNC::notify_rewards(
RuntimeOrigin::root(),
ALICE,
Expand Down
34 changes: 20 additions & 14 deletions pallets/bb-bnc/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub trait BbBNCInterface<AccountId, CurrencyId, Balance, BlockNumber> {
fn auto_notify_reward(
pool_id: PoolId,
n: BlockNumber,
rewards: Vec<(CurrencyId, Balance)>,
rewards: Vec<CurrencyId>,
) -> DispatchResult;
fn update_reward(
pool_id: PoolId,
Expand All @@ -65,13 +65,13 @@ pub trait BbBNCInterface<AccountId, CurrencyId, Balance, BlockNumber> {
fn add_reward(
who: &AccountId,
conf: &mut IncentiveConfig<CurrencyId, Balance, BlockNumber, AccountId>,
rewards: &Vec<(CurrencyId, Balance)>,
rewards: &Vec<CurrencyId>,
remaining: Balance,
) -> DispatchResult;
fn notify_reward(
pool_id: PoolId,
who: &Option<AccountId>,
rewards: Vec<(CurrencyId, Balance)>,
rewards: Vec<CurrencyId>,
) -> DispatchResult;
}

Expand Down Expand Up @@ -285,10 +285,11 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
fn auto_notify_reward(
pool_id: PoolId,
n: BlockNumberFor<T>,
rewards: Vec<(CurrencyIdOf<T>, BalanceOf<T>)>,
rewards: Vec<CurrencyIdOf<T>>,
) -> DispatchResult {
let conf = IncentiveConfigs::<T>::get(pool_id);
if n == conf.period_finish {
// If the period is reached or not set, the reward will be notified.
if n == conf.period_finish || conf.period_finish == Default::default() {
Self::notify_reward_amount(pool_id, &conf.incentive_controller, rewards)?;
}
Ok(())
Expand Down Expand Up @@ -335,11 +336,12 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
BlockNumberFor<T>,
AccountIdOf<T>,
>,
rewards: &Vec<(CurrencyIdOf<T>, BalanceOf<T>)>,
rewards: &Vec<CurrencyIdOf<T>>,
remaining: BalanceOf<T>,
) -> DispatchResult {
rewards.iter().try_for_each(|(currency, reward)| -> DispatchResult {
let mut total_reward: BalanceOf<T> = *reward;
rewards.iter().try_for_each(|currency| -> DispatchResult {
let reward = T::MultiCurrency::free_balance(*currency, who);
let mut total_reward: BalanceOf<T> = reward;
if remaining != BalanceOf::<T>::zero() {
let leftover: BalanceOf<T> = conf
.reward_rate
Expand All @@ -356,7 +358,7 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
// Make sure the new reward is less than or equal to the reward owned by the
// IncentivePalletId
ensure!(
total_reward <= currency_amount.saturating_add(*reward),
total_reward <= currency_amount.saturating_add(reward),
Error::<T>::NotEnoughBalance
);
let new_reward = total_reward
Expand All @@ -368,19 +370,23 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
*total_reward = new_reward;
})
.or_insert(new_reward);
// If the reward in this round is 0, it will only be recorded without transfer.
if reward == BalanceOf::<T>::zero() {
return Ok(());
}
T::MultiCurrency::transfer(
*currency,
who,
&T::IncentivePalletId::get().into_account_truncating(),
*reward,
reward,
)
})
}

fn notify_reward(
pool_id: PoolId,
who: &Option<AccountIdOf<T>>,
rewards: Vec<(CurrencyIdOf<T>, BalanceOf<T>)>,
rewards: Vec<CurrencyIdOf<T>>,
) -> DispatchResult {
Self::notify_reward_amount(pool_id, who, rewards)
}
Expand Down Expand Up @@ -441,7 +447,7 @@ where
fn auto_notify_reward(
_pool_id: PoolId,
_n: BlockNumber,
_rewards: Vec<(CurrencyId, Balance)>,
_rewards: Vec<CurrencyId>,
) -> DispatchResult {
Ok(())
}
Expand Down Expand Up @@ -471,15 +477,15 @@ where
fn add_reward(
_who: &AccountId,
_conf: &mut IncentiveConfig<CurrencyId, Balance, BlockNumber, AccountId>,
_rewards: &Vec<(CurrencyId, Balance)>,
_rewards: &Vec<CurrencyId>,
_remaining: Balance,
) -> DispatchResult {
Ok(())
}
fn notify_reward(
_pool_id: PoolId,
_who: &Option<AccountId>,
_rewards: Vec<(CurrencyId, Balance)>,
_rewards: Vec<CurrencyId>,
) -> DispatchResult {
Ok(())
}
Expand Down
3 changes: 1 addition & 2 deletions pallets/buy-back/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,10 @@ pub mod pallet {
let destruction_amount = ratio * bnc_balance_before_burn;
T::MultiCurrency::withdraw(BNC, &buyback_address, destruction_amount)?;
}
let bnc_balance = T::MultiCurrency::free_balance(BNC, &buyback_address);
T::BbBNC::notify_reward(
BB_BNC_SYSTEM_POOL_ID,
&Some(buyback_address.clone()),
vec![(BNC, bnc_balance)],
vec![BNC],
)
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/farming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ pub mod pallet {
GaugePoolInfos::<T>::iter().for_each(|(gid, gauge_pool_info)| {
match gauge_pool_info.gauge_state {
GaugeState::Bonded => {
let rewards = gauge_pool_info.gauge_basic_rewards.into_iter().collect();
let rewards = gauge_pool_info.gauge_basic_rewards.into_keys().collect();
T::BbBNC::auto_notify_reward(gid, n, rewards).unwrap_or_default();
},
_ => (),
Expand Down
1 change: 1 addition & 0 deletions pallets/farming/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ impl ExtBuilder {
}

pub fn build(self) -> sp_io::TestExternalities {
env_logger::try_init().unwrap_or(());
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();

pallet_balances::GenesisConfig::<Runtime> {
Expand Down
Loading