Skip to content

Commit 5f4bacb

Browse files
committed
harmonize tokens events with pallet_balances
Signed-off-by: Gregory Hill <[email protected]>
1 parent 7f7c623 commit 5f4bacb

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

tokens/src/lib.rs

+37-19
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,21 @@ pub mod module {
206206
#[pallet::event]
207207
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
208208
pub enum Event<T: Config> {
209-
/// Token transfer success. \[currency_id, from, to, amount\]
210-
Transferred(T::CurrencyId, T::AccountId, T::AccountId, T::Balance),
209+
/// An account was created with some free balance. \[currency_id,
210+
/// account, free_balance\]
211+
Endowed(T::CurrencyId, T::AccountId, T::Balance),
211212
/// An account was removed whose balance was non-zero but below
212-
/// ExistentialDeposit, resulting in an outright loss. \[account,
213-
/// currency_id, amount\]
214-
DustLost(T::AccountId, T::CurrencyId, T::Balance),
213+
/// ExistentialDeposit, resulting in an outright loss. \[currency_id,
214+
/// account, balance\]
215+
DustLost(T::CurrencyId, T::AccountId, T::Balance),
216+
/// Transfer succeeded. \[currency_id, from, to, value\]
217+
Transfer(T::CurrencyId, T::AccountId, T::AccountId, T::Balance),
218+
/// Some balance was reserved (moved from free to reserved).
219+
/// \[currency_id, who, value\]
220+
Reserved(T::CurrencyId, T::AccountId, T::Balance),
221+
/// Some balance was unreserved (moved from reserved to free).
222+
/// \[currency_id, who, value\]
223+
Unreserved(T::CurrencyId, T::AccountId, T::Balance),
215224
}
216225

217226
/// The total issuance of a token type.
@@ -321,7 +330,7 @@ pub mod module {
321330
let to = T::Lookup::lookup(dest)?;
322331
<Self as MultiCurrency<_>>::transfer(currency_id, &from, &to, amount)?;
323332

324-
Self::deposit_event(Event::Transferred(currency_id, from, to, amount));
333+
Self::deposit_event(Event::Transfer(currency_id, from, to, amount));
325334
Ok(().into())
326335
}
327336

@@ -340,7 +349,7 @@ pub mod module {
340349
let balance = <Self as MultiCurrency<T::AccountId>>::free_balance(currency_id, &from);
341350
<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, &from, &to, balance)?;
342351

343-
Self::deposit_event(Event::Transferred(currency_id, from, to, balance));
352+
Self::deposit_event(Event::Transfer(currency_id, from, to, balance));
344353
Ok(().into())
345354
}
346355
}
@@ -358,41 +367,47 @@ impl<T: Config> Pallet<T> {
358367
f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> sp_std::result::Result<R, E>,
359368
) -> sp_std::result::Result<R, E> {
360369
Accounts::<T>::try_mutate_exists(who, currency_id, |maybe_account| {
361-
let existed = maybe_account.is_some();
370+
let is_new = maybe_account.is_some();
362371
let mut account = maybe_account.take().unwrap_or_default();
363-
f(&mut account, existed).map(move |result| {
364-
let mut handle_dust: Option<T::Balance> = None;
372+
f(&mut account, is_new).map(move |result| {
373+
let maybe_endowed = if is_new { Some(account.free) } else { None };
374+
375+
let mut maybe_dust: Option<T::Balance> = None;
365376
let total = account.total();
366377
*maybe_account = if total.is_zero() {
367378
None
368379
} else {
369380
// if non_zero total is below existential deposit and the account is not a
370381
// module account, should handle the dust.
371382
if total < T::ExistentialDeposits::get(&currency_id) && !Self::is_module_account_id(who) {
372-
handle_dust = Some(total);
383+
maybe_dust = Some(total);
373384
}
374385
Some(account)
375386
};
376387

377-
(existed, maybe_account.is_some(), handle_dust, result)
388+
(maybe_endowed, maybe_account.is_some(), maybe_dust, result)
378389
})
379390
})
380-
.map(|(existed, exists, handle_dust, result)| {
381-
if existed && !exists {
391+
.map(|(maybe_endowed, exists, maybe_dust, result)| {
392+
if maybe_endowed.is_some() && !exists {
382393
// If existed before, decrease account provider.
383-
// Ignore the result, because if it failed means that these’s remain consumers,
384-
// and the account storage in frame_system shouldn't be repeaded.
394+
// Ignore the result, because if it failed then there are remaining consumers,
395+
// and the account storage in frame_system shouldn't be reaped.
385396
let _ = frame_system::Pallet::<T>::dec_providers(who);
386-
} else if !existed && exists {
397+
} else if maybe_endowed.is_none() && exists {
387398
// if new, increase account provider
388399
frame_system::Pallet::<T>::inc_providers(who);
389400
}
390401

391-
if let Some(dust_amount) = handle_dust {
402+
if let Some(endowed) = maybe_endowed {
403+
Self::deposit_event(Event::Endowed(currency_id, who.clone(), endowed));
404+
}
405+
406+
if let Some(dust_amount) = maybe_dust {
392407
// `OnDust` maybe get/set storage `Accounts` of `who`, trigger handler here
393408
// to avoid some unexpected errors.
394409
T::OnDust::on_dust(who, currency_id, dust_amount);
395-
Self::deposit_event(Event::DustLost(who.clone(), currency_id, dust_amount));
410+
Self::deposit_event(Event::DustLost(currency_id, who.clone(), dust_amount));
396411
}
397412

398413
result
@@ -757,6 +772,8 @@ impl<T: Config> MultiReservableCurrency<T::AccountId> for Pallet<T> {
757772
// Cannot overflow becuase total issuance is using the same balance type and
758773
// this doesn't increase total issuance
759774
Self::set_reserved_balance(currency_id, who, account.reserved + value);
775+
776+
Self::deposit_event(Event::Reserved(currency_id, who.clone(), value));
760777
Ok(())
761778
}
762779

@@ -774,6 +791,7 @@ impl<T: Config> MultiReservableCurrency<T::AccountId> for Pallet<T> {
774791
Self::set_reserved_balance(currency_id, who, account.reserved - actual);
775792
Self::set_free_balance(currency_id, who, account.free + actual);
776793

794+
Self::deposit_event(Event::Unreserved(currency_id, who.clone(), actual.clone()));
777795
value - actual
778796
}
779797

tokens/src/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn remove_dust_work() {
6262
assert_eq!(Tokens::free_balance(DOT, &DustAccount::get()), 1);
6363
assert_eq!(System::providers(&DustAccount::get()), 1);
6464

65-
System::assert_last_event(Event::tokens(crate::Event::DustLost(ALICE, DOT, 1)));
65+
System::assert_last_event(Event::tokens(crate::Event::DustLost(1, ALICE, DOT)));
6666
});
6767
}
6868

@@ -305,7 +305,7 @@ fn transfer_should_work() {
305305
assert_eq!(Tokens::free_balance(DOT, &BOB), 150);
306306
assert_eq!(Tokens::total_issuance(DOT), 200);
307307

308-
System::assert_last_event(Event::tokens(crate::Event::Transferred(DOT, ALICE, BOB, 50)));
308+
System::assert_last_event(Event::tokens(crate::Event::Transfer(DOT, ALICE, BOB, 50)));
309309

310310
assert_noop!(
311311
Tokens::transfer(Some(ALICE).into(), BOB, DOT, 60),

0 commit comments

Comments
 (0)