From e99fa834149bf4313dd32a1d2c66092e8049eee2 Mon Sep 17 00:00:00 2001
From: Greg Hill <gregorydhill@outlook.com>
Date: Sat, 12 Jun 2021 06:11:22 +0100
Subject: [PATCH] harmonize tokens events with pallet_balances (#511)

Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
---
 tokens/src/lib.rs   | 47 ++++++++++++++++++++++++++++++---------------
 tokens/src/tests.rs | 19 +++++++++++++++---
 2 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs
index d19116e..ad5ae6b 100644
--- a/tokens/src/lib.rs
+++ b/tokens/src/lib.rs
@@ -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.
@@ -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())
 		}
 
@@ -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())
 		}
 	}
@@ -361,7 +370,8 @@ 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
@@ -369,30 +379,34 @@ impl<T: Config> Pallet<T> {
 					// 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
@@ -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(())
 	}
 
@@ -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
 	}
 
diff --git a/tokens/src/tests.rs b/tokens/src/tests.rs
index 0e02706..297da4d 100644
--- a/tokens/src/tests.rs
+++ b/tokens/src/tests.rs
@@ -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)));
 	});
 }
 
@@ -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);
@@ -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);
 		});
@@ -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),
@@ -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)));
 		});
 }
 
-- 
GitLab