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

harmonize tokens events with pallet_balances #511

Merged
merged 1 commit into from
Jun 12, 2021
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
47 changes: 32 additions & 15 deletions tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,21 @@ pub mod module {
#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// Token transfer success. \[currency_id, from, to, amount\]
Transferred(T::CurrencyId, T::AccountId, T::AccountId, T::Balance),
/// An account was created with some free balance. \[currency_id,
/// account, free_balance\]
Endowed(T::CurrencyId, T::AccountId, T::Balance),
/// An account was removed whose balance was non-zero but below
/// ExistentialDeposit, resulting in an outright loss. \[account,
/// currency_id, amount\]
DustLost(T::AccountId, T::CurrencyId, T::Balance),
/// ExistentialDeposit, resulting in an outright loss. \[currency_id,
/// account, balance\]
DustLost(T::CurrencyId, T::AccountId, T::Balance),
/// Transfer succeeded. \[currency_id, from, to, value\]
Transfer(T::CurrencyId, T::AccountId, T::AccountId, T::Balance),
/// Some balance was reserved (moved from free to reserved).
/// \[currency_id, who, value\]
Reserved(T::CurrencyId, T::AccountId, T::Balance),
/// Some balance was unreserved (moved from reserved to free).
/// \[currency_id, who, value\]
Unreserved(T::CurrencyId, T::AccountId, T::Balance),
}

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

Self::deposit_event(Event::Transferred(currency_id, from, to, amount));
Self::deposit_event(Event::Transfer(currency_id, from, to, amount));
Ok(().into())
}

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

Self::deposit_event(Event::Transferred(currency_id, from, to, balance));
Self::deposit_event(Event::Transfer(currency_id, from, to, balance));
Ok(().into())
}
}
Expand All @@ -361,38 +370,43 @@ impl<T: Config> Pallet<T> {
let existed = maybe_account.is_some();
let mut account = maybe_account.take().unwrap_or_default();
f(&mut account, existed).map(move |result| {
let mut handle_dust: Option<T::Balance> = None;
let maybe_endowed = if !existed { Some(account.free) } else { None };
let mut maybe_dust: Option<T::Balance> = None;
let total = account.total();
*maybe_account = if total.is_zero() {
None
} else {
// if non_zero total is below existential deposit and the account is not a
// module account, should handle the dust.
if total < T::ExistentialDeposits::get(&currency_id) && !Self::is_module_account_id(who) {
handle_dust = Some(total);
maybe_dust = Some(total);
}
Some(account)
};

(existed, maybe_account.is_some(), handle_dust, result)
(maybe_endowed, existed, maybe_account.is_some(), maybe_dust, result)
})
})
.map(|(existed, exists, handle_dust, result)| {
.map(|(maybe_endowed, existed, exists, maybe_dust, result)| {
if existed && !exists {
// If existed before, decrease account provider.
// Ignore the result, because if it failed means that these’s remain consumers,
// and the account storage in frame_system shouldn't be repeaded.
// 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);
} else if !existed && exists {
// if new, increase account provider
frame_system::Pallet::<T>::inc_providers(who);
}

if let Some(dust_amount) = handle_dust {
if let Some(endowed) = maybe_endowed {
Self::deposit_event(Event::Endowed(currency_id, who.clone(), endowed));
}

if let Some(dust_amount) = maybe_dust {
// `OnDust` maybe get/set storage `Accounts` of `who`, trigger handler here
// to avoid some unexpected errors.
T::OnDust::on_dust(who, currency_id, dust_amount);
Self::deposit_event(Event::DustLost(who.clone(), currency_id, dust_amount));
Self::deposit_event(Event::DustLost(currency_id, who.clone(), dust_amount));
}

result
Expand Down Expand Up @@ -757,6 +771,8 @@ impl<T: Config> MultiReservableCurrency<T::AccountId> for Pallet<T> {
// Cannot overflow becuase total issuance is using the same balance type and
// this doesn't increase total issuance
Self::set_reserved_balance(currency_id, who, account.reserved + value);

Self::deposit_event(Event::Reserved(currency_id, who.clone(), value));
Ok(())
}

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

Self::deposit_event(Event::Unreserved(currency_id, who.clone(), actual));
value - actual
}

Expand Down
19 changes: 16 additions & 3 deletions tokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ fn remove_dust_work() {
assert_eq!(Tokens::free_balance(DOT, &DustAccount::get()), 1);
assert_eq!(System::providers(&DustAccount::get()), 1);

System::assert_last_event(Event::tokens(crate::Event::DustLost(ALICE, DOT, 1)));
System::assert_last_event(Event::tokens(crate::Event::DustLost(DOT, ALICE, 1)));
});
}

#[test]
fn set_free_balance_should_work() {
ExtBuilder::default().build().execute_with(|| {
Tokens::set_free_balance(DOT, &ALICE, 100);
System::assert_last_event(Event::tokens(crate::Event::Endowed(DOT, ALICE, 100)));
});
}

Expand Down Expand Up @@ -157,6 +165,7 @@ fn reserve_should_work() {
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 0);
assert_eq!(Tokens::total_balance(DOT, &ALICE), 100);
assert_ok!(Tokens::reserve(DOT, &ALICE, 50));
System::assert_last_event(Event::tokens(crate::Event::Reserved(DOT, ALICE, 50)));
assert_eq!(Tokens::free_balance(DOT, &ALICE), 50);
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 50);
assert_eq!(Tokens::total_balance(DOT, &ALICE), 100);
Expand All @@ -173,13 +182,17 @@ fn unreserve_should_work() {
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 0);
assert_eq!(Tokens::unreserve(DOT, &ALICE, 0), 0);
assert_eq!(Tokens::unreserve(DOT, &ALICE, 50), 50);
System::assert_last_event(Event::tokens(crate::Event::Unreserved(DOT, ALICE, 0)));
assert_ok!(Tokens::reserve(DOT, &ALICE, 30));
System::assert_last_event(Event::tokens(crate::Event::Reserved(DOT, ALICE, 30)));
assert_eq!(Tokens::free_balance(DOT, &ALICE), 70);
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 30);
assert_eq!(Tokens::unreserve(DOT, &ALICE, 15), 0);
System::assert_last_event(Event::tokens(crate::Event::Unreserved(DOT, ALICE, 15)));
assert_eq!(Tokens::free_balance(DOT, &ALICE), 85);
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 15);
assert_eq!(Tokens::unreserve(DOT, &ALICE, 30), 15);
System::assert_last_event(Event::tokens(crate::Event::Unreserved(DOT, ALICE, 15)));
assert_eq!(Tokens::free_balance(DOT, &ALICE), 100);
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 0);
});
Expand Down Expand Up @@ -305,7 +318,7 @@ fn transfer_should_work() {
assert_eq!(Tokens::free_balance(DOT, &BOB), 150);
assert_eq!(Tokens::total_issuance(DOT), 200);

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

assert_noop!(
Tokens::transfer(Some(ALICE).into(), BOB, DOT, 60),
Expand All @@ -326,7 +339,7 @@ fn transfer_all_should_work() {
assert_eq!(Tokens::free_balance(DOT, &ALICE), 0);
assert_eq!(Tokens::free_balance(DOT, &BOB), 200);

System::assert_last_event(Event::tokens(crate::Event::Transferred(DOT, ALICE, BOB, 100)));
System::assert_last_event(Event::tokens(crate::Event::Transfer(DOT, ALICE, BOB, 100)));
});
}

Expand Down