Skip to content

Commit a032a47

Browse files
committed
add OnNewAccount and OnKilledAccount to orml-tokens
1 parent 070457d commit a032a47

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

tokens/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use orml_traits::{
6868
arithmetic::{self, Signed},
6969
currency::TransferAll,
7070
BalanceStatus, GetByKey, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency,
71-
MultiReservableCurrency, NamedMultiReservableCurrency, OnDust,
71+
MultiReservableCurrency, NamedMultiReservableCurrency, OnDust, OnNewAccount, OnKilledAccount,
7272
};
7373

7474
mod imbalances;
@@ -216,6 +216,12 @@ pub mod module {
216216
/// Handler to burn or transfer account's dust
217217
type OnDust: OnDust<Self::AccountId, Self::CurrencyId, Self::Balance>;
218218

219+
/// Handler for when an account was created
220+
type OnNewAccount: OnNewAccount<Self::AccountId, Self::CurrencyId>;
221+
222+
/// Handler for when an account was created
223+
type OnKilledAccount: OnKilledAccount<Self::AccountId, Self::CurrencyId>;
224+
219225
#[pallet::constant]
220226
type MaxLocks: Get<u32>;
221227

@@ -747,9 +753,11 @@ impl<T: Config> Pallet<T> {
747753
// Ignore the result, because if it failed then there are remaining consumers,
748754
// and the account storage in frame_system shouldn't be reaped.
749755
let _ = frame_system::Pallet::<T>::dec_providers(who);
756+
T::OnKilledAccount::on_killed_account(&who, currency_id);
750757
} else if !existed && exists {
751758
// if new, increase account provider
752759
frame_system::Pallet::<T>::inc_providers(who);
760+
T::OnNewAccount::on_new_account(&who, currency_id);
753761
}
754762

755763
if let Some(endowed) = maybe_endowed {

tokens/src/mock.rs

+42
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,43 @@ parameter_type_with_key! {
220220
};
221221
}
222222

223+
thread_local! {
224+
pub static CREATED: RefCell<Vec<(AccountId, CurrencyId)>> = RefCell::new(vec![]);
225+
pub static KILLED: RefCell<Vec<(AccountId, CurrencyId)>> = RefCell::new(vec![]);
226+
}
227+
228+
pub struct TrackCreatedAccounts;
229+
impl TrackCreatedAccounts {
230+
pub fn accounts() -> Vec<(AccountId, CurrencyId)> {
231+
CREATED.clone()
232+
}
233+
234+
pub fn reset() {
235+
*CREATED = RefCell::new(vec![]);
236+
}
237+
}
238+
impl OnNewAccount<AccountId, CurrencyId> for TrackCreatedAccounts {
239+
fn on_new_account(who: &AccountId, currency: CurrencyId) {
240+
CREATED.push((who, CurrencyId));
241+
}
242+
}
243+
244+
pub struct TrackKilledAccounts;
245+
impl TrackKilledAccounts {
246+
pub fn accounts() -> Vec<(AccountId, CurrencyId)> {
247+
KILLED.clone()
248+
}
249+
250+
pub fn reset() {
251+
*KILLED = RefCell::new(vec![]);
252+
}
253+
}
254+
impl OnNewAccount<AccountId, CurrencyId> for TrackKilledAccounts {
255+
fn on_new_account(who: &AccountId, currency: CurrencyId) {
256+
KILLED.push((who, CurrencyId));
257+
}
258+
}
259+
223260
parameter_types! {
224261
pub DustReceiver: AccountId = PalletId(*b"orml/dst").into_account();
225262
}
@@ -232,6 +269,8 @@ impl Config for Runtime {
232269
type WeightInfo = ();
233270
type ExistentialDeposits = ExistentialDeposits;
234271
type OnDust = TransferDust<Runtime, DustReceiver>;
272+
type OnNewAccount = TrackCreatedAccounts;
273+
type OnKilledAccount = TrackKilledAccounts;
235274
type MaxLocks = ConstU32<2>;
236275
type MaxReserves = ConstU32<2>;
237276
type ReserveIdentifier = ReserveIdentifier;
@@ -296,6 +335,9 @@ impl ExtBuilder {
296335
.unwrap();
297336
}
298337

338+
TrackCreatedAccounts::reset();
339+
TrackKilledAccounts::reset();
340+
299341
let mut ext = sp_io::TestExternalities::new(t);
300342
ext.execute_with(|| System::set_block_number(1));
301343
ext

tokens/src/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ fn transfer_all_allow_death_should_work() {
139139
}));
140140
assert!(!Accounts::<Runtime>::contains_key(ALICE, DOT));
141141
assert_eq!(Tokens::free_balance(DOT, &ALICE), 0);
142+
assert_eq!(TrackKilledAccounts::accounts(), vec![(ALICE, DOT)]);
142143

143144
assert_ok!(Tokens::set_lock(ID_1, DOT, &BOB, 50));
144145
assert_eq!(Tokens::accounts(&BOB, DOT).frozen, 50);
@@ -513,6 +514,7 @@ fn set_free_balance_should_work() {
513514

514515
Tokens::set_free_balance(DOT, &ALICE, 2);
515516
assert!(Accounts::<Runtime>::contains_key(ALICE, DOT));
517+
assert_eq!(TrackCreatedAccounts::accounts(), vec![(ALICE, DOT)]);
516518
assert_eq!(Tokens::free_balance(DOT, &ALICE), 2);
517519
assert_eq!(Tokens::free_balance(DOT, &DustReceiver::get()), 1);
518520

traits/src/currency.rs

+18
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,24 @@ impl<AccountId, CurrencyId, Balance> OnDust<AccountId, CurrencyId, Balance> for
643643
fn on_dust(_: &AccountId, _: CurrencyId, _: Balance) {}
644644
}
645645

646+
/// Handler for a newly created account
647+
pub trait OnNewAccount<AccountId, CurrencyId> {
648+
fn on_new_account(who: &AccountId, currency_id: CurrencyId);
649+
}
650+
651+
impl<AccountId, CurrencyId, Balance> OnDust<AccountId, CurrencyId> for () {
652+
fn on_new_account(_: &AccountId, _: CurrencyId) {}
653+
}
654+
655+
/// Handler for an account that was removed
656+
pub trait OnKilledAccount<AccountId, CurrencyId> {
657+
fn on_killed_account(who: &AccountId, currency_id: CurrencyId);
658+
}
659+
660+
impl<AccountId, CurrencyId, Balance> OnDust<AccountId, CurrencyId> for () {
661+
fn on_killed_account(_: &AccountId, _: CurrencyId) {}
662+
}
663+
646664
pub trait TransferAll<AccountId> {
647665
fn transfer_all(source: &AccountId, dest: &AccountId) -> DispatchResult;
648666
}

0 commit comments

Comments
 (0)