Skip to content

Commit

Permalink
Use Handler insteal of Happend trait (#2763)
Browse files Browse the repository at this point in the history
* use Handler insteal of Happend trait

* fix
  • Loading branch information
zjb0807 authored May 22, 2024
1 parent 49679b7 commit bf6eee7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
14 changes: 7 additions & 7 deletions modules/incentives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
use frame_support::{pallet_prelude::*, transactional, PalletId};
use frame_system::pallet_prelude::*;
use module_support::{DEXIncentives, EmergencyShutdown, FractionalRate, IncentivesManager, PoolId, Rate};
use orml_traits::{Handler, Happened, MultiCurrency, RewardHandler};
use orml_traits::{Handler, MultiCurrency, RewardHandler};
use primitives::{Amount, Balance, CurrencyId};
use sp_runtime::{
traits::{AccountIdConversion, UniqueSaturatedInto, Zero},
Expand Down Expand Up @@ -619,15 +619,15 @@ impl<T: Config> Handler<(T::AccountId, Balance)> for OnEarningUnbonded<T> {
}

pub struct OnNomineesElectionBonded<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> Happened<(T::AccountId, Balance)> for OnNomineesElectionBonded<T> {
fn happened((who, amount): &(T::AccountId, Balance)) {
let _ = <orml_rewards::Pallet<T>>::add_share(who, &PoolId::NomineesElection, *amount);
impl<T: Config> Handler<(T::AccountId, Balance)> for OnNomineesElectionBonded<T> {
fn handle((who, amount): &(T::AccountId, Balance)) -> DispatchResult {
<orml_rewards::Pallet<T>>::add_share(who, &PoolId::NomineesElection, *amount)
}
}

pub struct OnNomineesElectionUnbonded<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> Happened<(T::AccountId, Balance)> for OnNomineesElectionUnbonded<T> {
fn happened((who, amount): &(T::AccountId, Balance)) {
let _ = <orml_rewards::Pallet<T>>::remove_share(who, &PoolId::NomineesElection, *amount);
impl<T: Config> Handler<(T::AccountId, Balance)> for OnNomineesElectionUnbonded<T> {
fn handle((who, amount): &(T::AccountId, Balance)) -> DispatchResult {
<orml_rewards::Pallet<T>>::remove_share(who, &PoolId::NomineesElection, *amount)
}
}
6 changes: 3 additions & 3 deletions modules/incentives/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ fn claim_reward_deduction_currency_works() {
#[test]
fn nominees_election_should_work() {
ExtBuilder::default().build().execute_with(|| {
OnNomineesElectionBonded::<Runtime>::happened(&(ALICE::get(), 80));
OnNomineesElectionBonded::<Runtime>::handle(&(ALICE::get(), 80));
assert_eq!(
RewardsModule::pool_infos(PoolId::NomineesElection),
PoolInfo {
Expand All @@ -1355,7 +1355,7 @@ fn nominees_election_should_work() {
(80, Default::default())
);

OnNomineesElectionUnbonded::<Runtime>::happened(&(ALICE::get(), 20));
OnNomineesElectionUnbonded::<Runtime>::handle(&(ALICE::get(), 20));
assert_eq!(
RewardsModule::pool_infos(PoolId::NomineesElection),
PoolInfo {
Expand All @@ -1368,7 +1368,7 @@ fn nominees_election_should_work() {
(60, Default::default())
);

OnNomineesElectionUnbonded::<Runtime>::happened(&(ALICE::get(), 60));
OnNomineesElectionUnbonded::<Runtime>::handle(&(ALICE::get(), 60));
assert_eq!(
RewardsModule::pool_infos(PoolId::NomineesElection),
PoolInfo { ..Default::default() }
Expand Down
12 changes: 6 additions & 6 deletions modules/nominees-election/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use frame_support::{
};
use frame_system::pallet_prelude::*;
use module_support::NomineesProvider;
use orml_traits::{BasicCurrency, BasicLockableCurrency, Happened};
use orml_traits::{BasicCurrency, BasicLockableCurrency, Handler};
use primitives::{
bonding::{self, BondingController},
Balance, EraIndex,
Expand Down Expand Up @@ -84,10 +84,10 @@ pub mod module {
type GovernanceOrigin: EnsureOrigin<Self::RuntimeOrigin>;

/// Callback when an account bonded.
type OnBonded: Happened<(Self::AccountId, Balance)>;
type OnBonded: Handler<(Self::AccountId, Balance)>;

/// Callback when an account unbonded.
type OnUnbonded: Happened<(Self::AccountId, Balance)>;
type OnUnbonded: Handler<(Self::AccountId, Balance)>;

/// Current era.
type CurrentEra: Get<EraIndex>;
Expand Down Expand Up @@ -199,7 +199,7 @@ pub mod module {

Self::update_votes(change.old, &old_nominations, change.new, &old_nominations);

T::OnBonded::happened(&(who.clone(), change.change));
T::OnBonded::handle(&(who.clone(), change.change))?;

Self::deposit_event(Event::Bond {
who,
Expand All @@ -222,7 +222,7 @@ pub mod module {

Self::update_votes(change.old, &old_nominations, change.new, &old_nominations);

T::OnUnbonded::happened(&(who.clone(), change.change));
T::OnUnbonded::handle(&(who.clone(), change.change))?;

Self::deposit_event(Event::Unbond {
who,
Expand All @@ -245,7 +245,7 @@ pub mod module {

Self::update_votes(change.old, &old_nominations, change.new, &old_nominations);

T::OnBonded::happened(&(who.clone(), change.change));
T::OnBonded::handle(&(who.clone(), change.change))?;

Self::deposit_event(Event::Rebond {
who,
Expand Down
10 changes: 6 additions & 4 deletions modules/nominees-election/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ parameter_types! {
}

pub struct MockOnBonded;
impl Happened<(AccountId, Balance)> for MockOnBonded {
fn happened(info: &(AccountId, Balance)) {
impl Handler<(AccountId, Balance)> for MockOnBonded {
fn handle(info: &(AccountId, Balance)) -> DispatchResult {
let (account_id, amount) = info;
Shares::mutate(|v| {
let mut old_map = v.clone();
Expand All @@ -143,12 +143,13 @@ impl Happened<(AccountId, Balance)> for MockOnBonded {

*v = old_map;
});
Ok(())
}
}

pub struct MockOnUnbonded;
impl Happened<(AccountId, Balance)> for MockOnUnbonded {
fn happened(info: &(AccountId, Balance)) {
impl Handler<(AccountId, Balance)> for MockOnUnbonded {
fn handle(info: &(AccountId, Balance)) -> DispatchResult {
let (account_id, amount) = info;
Shares::mutate(|v| {
let mut old_map = v.clone();
Expand All @@ -160,6 +161,7 @@ impl Happened<(AccountId, Balance)> for MockOnUnbonded {

*v = old_map;
});
Ok(())
}
}

Expand Down

0 comments on commit bf6eee7

Please sign in to comment.