Skip to content

Commit 7c89354

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

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

tokens/src/lib.rs

+32-15
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
}
@@ -361,38 +370,43 @@ impl<T: Config> Pallet<T> {
361370
let existed = maybe_account.is_some();
362371
let mut account = maybe_account.take().unwrap_or_default();
363372
f(&mut account, existed).map(move |result| {
364-
let mut handle_dust: Option<T::Balance> = None;
373+
let maybe_endowed = if !existed { Some(account.free) } else { None };
374+
let mut maybe_dust: Option<T::Balance> = None;
365375
let total = account.total();
366376
*maybe_account = if total.is_zero() {
367377
None
368378
} else {
369379
// if non_zero total is below existential deposit and the account is not a
370380
// module account, should handle the dust.
371381
if total < T::ExistentialDeposits::get(&currency_id) && !Self::is_module_account_id(who) {
372-
handle_dust = Some(total);
382+
maybe_dust = Some(total);
373383
}
374384
Some(account)
375385
};
376386

377-
(existed, maybe_account.is_some(), handle_dust, result)
387+
(maybe_endowed, existed, maybe_account.is_some(), maybe_dust, result)
378388
})
379389
})
380-
.map(|(existed, exists, handle_dust, result)| {
390+
.map(|(maybe_endowed, existed, exists, maybe_dust, result)| {
381391
if existed && !exists {
382392
// 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.
393+
// Ignore the result, because if it failed then there are remaining consumers,
394+
// and the account storage in frame_system shouldn't be reaped.
385395
let _ = frame_system::Pallet::<T>::dec_providers(who);
386396
} else if !existed && exists {
387397
// if new, increase account provider
388398
frame_system::Pallet::<T>::inc_providers(who);
389399
}
390400

391-
if let Some(dust_amount) = handle_dust {
401+
if let Some(endowed) = maybe_endowed {
402+
Self::deposit_event(Event::Endowed(currency_id, who.clone(), endowed));
403+
}
404+
405+
if let Some(dust_amount) = maybe_dust {
392406
// `OnDust` maybe get/set storage `Accounts` of `who`, trigger handler here
393407
// to avoid some unexpected errors.
394408
T::OnDust::on_dust(who, currency_id, dust_amount);
395-
Self::deposit_event(Event::DustLost(who.clone(), currency_id, dust_amount));
409+
Self::deposit_event(Event::DustLost(currency_id, who.clone(), dust_amount));
396410
}
397411

398412
result
@@ -757,6 +771,8 @@ impl<T: Config> MultiReservableCurrency<T::AccountId> for Pallet<T> {
757771
// Cannot overflow becuase total issuance is using the same balance type and
758772
// this doesn't increase total issuance
759773
Self::set_reserved_balance(currency_id, who, account.reserved + value);
774+
775+
Self::deposit_event(Event::Reserved(currency_id, who.clone(), value));
760776
Ok(())
761777
}
762778

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

793+
Self::deposit_event(Event::Unreserved(currency_id, who.clone(), actual));
777794
value - actual
778795
}
779796

tokens/src/tests.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ 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(DOT, ALICE, 1)));
66+
});
67+
}
68+
69+
#[test]
70+
fn set_free_balance_should_work() {
71+
ExtBuilder::default().build().execute_with(|| {
72+
Tokens::set_free_balance(DOT, &ALICE, 100);
73+
System::assert_last_event(Event::tokens(crate::Event::Endowed(DOT, ALICE, 100)));
6674
});
6775
}
6876

@@ -157,6 +165,7 @@ fn reserve_should_work() {
157165
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 0);
158166
assert_eq!(Tokens::total_balance(DOT, &ALICE), 100);
159167
assert_ok!(Tokens::reserve(DOT, &ALICE, 50));
168+
System::assert_last_event(Event::tokens(crate::Event::Reserved(DOT, ALICE, 50)));
160169
assert_eq!(Tokens::free_balance(DOT, &ALICE), 50);
161170
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 50);
162171
assert_eq!(Tokens::total_balance(DOT, &ALICE), 100);
@@ -173,13 +182,17 @@ fn unreserve_should_work() {
173182
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 0);
174183
assert_eq!(Tokens::unreserve(DOT, &ALICE, 0), 0);
175184
assert_eq!(Tokens::unreserve(DOT, &ALICE, 50), 50);
185+
System::assert_last_event(Event::tokens(crate::Event::Unreserved(DOT, ALICE, 0)));
176186
assert_ok!(Tokens::reserve(DOT, &ALICE, 30));
187+
System::assert_last_event(Event::tokens(crate::Event::Reserved(DOT, ALICE, 30)));
177188
assert_eq!(Tokens::free_balance(DOT, &ALICE), 70);
178189
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 30);
179190
assert_eq!(Tokens::unreserve(DOT, &ALICE, 15), 0);
191+
System::assert_last_event(Event::tokens(crate::Event::Unreserved(DOT, ALICE, 15)));
180192
assert_eq!(Tokens::free_balance(DOT, &ALICE), 85);
181193
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 15);
182194
assert_eq!(Tokens::unreserve(DOT, &ALICE, 30), 15);
195+
System::assert_last_event(Event::tokens(crate::Event::Unreserved(DOT, ALICE, 15)));
183196
assert_eq!(Tokens::free_balance(DOT, &ALICE), 100);
184197
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 0);
185198
});
@@ -305,7 +318,7 @@ fn transfer_should_work() {
305318
assert_eq!(Tokens::free_balance(DOT, &BOB), 150);
306319
assert_eq!(Tokens::total_issuance(DOT), 200);
307320

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

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

329-
System::assert_last_event(Event::tokens(crate::Event::Transferred(DOT, ALICE, BOB, 100)));
342+
System::assert_last_event(Event::tokens(crate::Event::Transfer(DOT, ALICE, BOB, 100)));
330343
});
331344
}
332345

0 commit comments

Comments
 (0)