Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use Happened trait for lifecycle callbacks instead
  • Loading branch information
apopiak committed May 30, 2022
commit 5554b66224d0a35ec239bc73417d34dff8d35ed0
10 changes: 5 additions & 5 deletions tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use orml_traits::{
arithmetic::{self, Signed},
currency::TransferAll,
BalanceStatus, GetByKey, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency,
MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewTokenAccount, OnKilledTokenAccount,
MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, Happened,
};

mod imbalances;
Expand Down Expand Up @@ -217,10 +217,10 @@ pub mod module {
type OnDust: OnDust<Self::AccountId, Self::CurrencyId, Self::Balance>;

/// Handler for when an account was created
type OnNewTokenAccount: OnNewTokenAccount<Self::AccountId, Self::CurrencyId>;
type OnNewTokenAccount: Happened<(Self::AccountId, Self::CurrencyId)>;

/// Handler for when an account was created
type OnKilledTokenAccount: OnKilledTokenAccount<Self::AccountId, Self::CurrencyId>;
type OnKilledTokenAccount: Happened<(Self::AccountId, Self::CurrencyId)>;

#[pallet::constant]
type MaxLocks: Get<u32>;
Expand Down Expand Up @@ -753,11 +753,11 @@ impl<T: Config> Pallet<T> {
// Ignore the result, because if it failed then there are remaining consumers,
// and the account storage in frame_system shouldn't be reaped.
let _ = frame_system::Pallet::<T>::dec_providers(who);
T::OnKilledTokenAccount::on_killed_account_for(&who, currency_id);
T::OnKilledTokenAccount::happened(&(who.clone(), currency_id));
} else if !existed && exists {
// if new, increase account provider
frame_system::Pallet::<T>::inc_providers(who);
T::OnNewTokenAccount::on_new_account_for(&who, currency_id);
T::OnNewTokenAccount::happened(&(who.clone(), currency_id));
}

if let Some(endowed) = maybe_endowed {
Expand Down
12 changes: 6 additions & 6 deletions tokens/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ impl TrackCreatedAccounts {
CREATED.with(|accounts| { accounts.replace(vec![]); });
}
}
impl OnNewTokenAccount<AccountId, CurrencyId> for TrackCreatedAccounts {
fn on_new_account_for(who: &AccountId, currency: CurrencyId) {
CREATED.with(|accounts| { accounts.borrow_mut().push((who.clone(), currency)); });
impl Happened<(AccountId, CurrencyId)> for TrackCreatedAccounts {
fn happened((who, currency): &(AccountId, CurrencyId)) {
CREATED.with(|accounts| { accounts.borrow_mut().push((who.clone(), *currency)); });
}
}

Expand All @@ -251,9 +251,9 @@ impl TrackKilledAccounts {
KILLED.with(|accounts| { accounts.replace(vec![]); });
}
}
impl OnKilledTokenAccount<AccountId, CurrencyId> for TrackKilledAccounts {
fn on_killed_account_for(who: &AccountId, currency: CurrencyId) {
KILLED.with(|accounts| { accounts.borrow_mut().push((who.clone(), currency)); });
impl Happened<(AccountId, CurrencyId)> for TrackKilledAccounts {
fn happened((who, currency): &(AccountId, CurrencyId)) {
KILLED.with(|accounts| { accounts.borrow_mut().push((who.clone(), *currency)); });
}
}

Expand Down
18 changes: 0 additions & 18 deletions traits/src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,24 +643,6 @@ impl<AccountId, CurrencyId, Balance> OnDust<AccountId, CurrencyId, Balance> for
fn on_dust(_: &AccountId, _: CurrencyId, _: Balance) {}
}

/// Handler for a newly created account
pub trait OnNewTokenAccount<AccountId, CurrencyId> {
fn on_new_account_for(who: &AccountId, currency_id: CurrencyId);
}

impl<AccountId, CurrencyId> OnNewTokenAccount<AccountId, CurrencyId> for () {
fn on_new_account_for(_: &AccountId, _: CurrencyId) {}
}

/// Handler for an account that was removed
pub trait OnKilledTokenAccount<AccountId, CurrencyId> {
fn on_killed_account_for(who: &AccountId, currency_id: CurrencyId);
}

impl<AccountId, CurrencyId> OnKilledTokenAccount<AccountId, CurrencyId> for () {
fn on_killed_account_for(_: &AccountId, _: CurrencyId) {}
}

pub trait TransferAll<AccountId> {
fn transfer_all(source: &AccountId, dest: &AccountId) -> DispatchResult;
}
Expand Down
2 changes: 1 addition & 1 deletion traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use auction::{Auction, AuctionHandler, AuctionInfo, OnNewBidResult};
pub use currency::{
BalanceStatus, BasicCurrency, BasicCurrencyExtended, BasicLockableCurrency, BasicReservableCurrency,
LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, MultiReservableCurrency,
NamedBasicReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewTokenAccount, OnKilledTokenAccount,
NamedBasicReservableCurrency, NamedMultiReservableCurrency, OnDust,
};
pub use data_provider::{DataFeeder, DataProvider, DataProviderExtended};
pub use get_by_key::GetByKey;
Expand Down