Skip to content
Snippets Groups Projects
Unverified Commit 8500c498 authored by zjb0807's avatar zjb0807 Committed by GitHub
Browse files

Feature/emit events (#97)


* emit events by transfer, deposit, withdraw

* Use Event<T> instead of RawEvent outside decl_module.

* add unit test

Co-authored-by: default avatarShaopeng Wang <spxwang@gmail.com>
parent 2a88acd6
No related branches found
No related tags found
No related merge requests found
......@@ -48,6 +48,10 @@ decl_event!(
Transferred(CurrencyId, AccountId, AccountId, Balance),
/// Update balance success (currency_id, who, amount)
BalanceUpdated(CurrencyId, AccountId, Amount),
/// Deposit success (currency_id, who, amount)
Deposited(CurrencyId, AccountId, Balance),
/// Withdraw success (currency_id, who, amount)
Withdrawn(CurrencyId, AccountId, Balance),
}
);
......@@ -77,8 +81,6 @@ decl_module! {
let from = ensure_signed(origin)?;
let to = T::Lookup::lookup(dest)?;
<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, &from, &to, amount)?;
Self::deposit_event(RawEvent::Transferred(currency_id, from, to, amount));
}
/// Transfer native currency balance from one account to another.
......@@ -147,26 +149,32 @@ impl<T: Trait> MultiCurrency<T::AccountId> for Module<T> {
amount: Self::Balance,
) -> DispatchResult {
if currency_id == T::GetNativeCurrencyId::get() {
T::NativeCurrency::transfer(from, to, amount)
T::NativeCurrency::transfer(from, to, amount)?;
} else {
T::MultiCurrency::transfer(currency_id, from, to, amount)
T::MultiCurrency::transfer(currency_id, from, to, amount)?;
}
Self::deposit_event(RawEvent::Transferred(currency_id, from.clone(), to.clone(), amount));
Ok(())
}
fn deposit(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
if currency_id == T::GetNativeCurrencyId::get() {
T::NativeCurrency::deposit(who, amount)
T::NativeCurrency::deposit(who, amount)?;
} else {
T::MultiCurrency::deposit(currency_id, who, amount)
T::MultiCurrency::deposit(currency_id, who, amount)?;
}
Self::deposit_event(RawEvent::Deposited(currency_id, who.clone(), amount));
Ok(())
}
fn withdraw(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
if currency_id == T::GetNativeCurrencyId::get() {
T::NativeCurrency::withdraw(who, amount)
T::NativeCurrency::withdraw(who, amount)?;
} else {
T::MultiCurrency::withdraw(currency_id, who, amount)
T::MultiCurrency::withdraw(currency_id, who, amount)?;
}
Self::deposit_event(RawEvent::Withdrawn(currency_id, who.clone(), amount));
Ok(())
}
fn slash(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> Self::Balance {
......
......@@ -2,7 +2,7 @@
#![cfg(test)]
use frame_support::{impl_outer_origin, parameter_types};
use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
use pallet_balances;
use primitives::H256;
use sp_runtime::{testing::Header, traits::IdentityLookup, Perbill};
......@@ -11,6 +11,19 @@ use tokens;
use super::*;
mod currencies {
pub use crate::Event;
}
impl_outer_event! {
pub enum TestEvent for Runtime {
frame_system<T>,
currencies<T>,
tokens<T>,
pallet_balances<T>,
}
}
impl_outer_origin! {
pub enum Origin for Runtime where system = frame_system {}
}
......@@ -36,7 +49,7 @@ impl frame_system::Trait for Runtime {
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type Event = TestEvent;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
......@@ -47,6 +60,7 @@ impl frame_system::Trait for Runtime {
type OnNewAccount = ();
type OnReapAccount = ();
}
pub type System = system::Module<Runtime>;
type CurrencyId = u32;
type Balance = u64;
......@@ -58,7 +72,7 @@ parameter_types! {
impl pallet_balances::Trait for Runtime {
type Balance = Balance;
type DustRemoval = ();
type Event = ();
type Event = TestEvent;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = frame_system::Module<Runtime>;
}
......@@ -66,7 +80,7 @@ impl pallet_balances::Trait for Runtime {
pub type PalletBalances = pallet_balances::Module<Runtime>;
impl tokens::Trait for Runtime {
type Event = ();
type Event = TestEvent;
type Balance = Balance;
type Amount = i64;
type CurrencyId = CurrencyId;
......@@ -82,7 +96,7 @@ parameter_types! {
}
impl Trait for Runtime {
type Event = ();
type Event = TestEvent;
type MultiCurrency = tokens::Module<Runtime>;
type NativeCurrency = AdaptedBasicCurrency;
type GetNativeCurrencyId = GetNativeCurrencyId;
......
......@@ -5,8 +5,8 @@
use super::*;
use frame_support::{assert_noop, assert_ok};
use mock::{
AccountId, AdaptedBasicCurrency, Currencies, ExtBuilder, NativeCurrency, Origin, PalletBalances, ALICE, BOB, EVA,
NATIVE_CURRENCY_ID, X_TOKEN_ID,
AccountId, AdaptedBasicCurrency, Currencies, ExtBuilder, NativeCurrency, Origin, PalletBalances, System, TestEvent,
ALICE, BOB, EVA, NATIVE_CURRENCY_ID, X_TOKEN_ID,
};
use sp_runtime::traits::BadOrigin;
......@@ -169,3 +169,43 @@ fn update_balance_call_fails_if_not_root_origin() {
);
});
}
#[test]
fn call_event_should_work() {
ExtBuilder::default()
.one_hundred_for_alice_n_bob()
.build()
.execute_with(|| {
assert_ok!(Currencies::transfer(Some(ALICE).into(), BOB, X_TOKEN_ID, 50));
assert_eq!(Currencies::balance(X_TOKEN_ID, &ALICE), 50);
assert_eq!(Currencies::balance(X_TOKEN_ID, &BOB), 150);
let transferred_event = TestEvent::currencies(RawEvent::Transferred(X_TOKEN_ID, ALICE, BOB, 50));
assert!(System::events().iter().any(|record| record.event == transferred_event));
assert_ok!(<Currencies as MultiCurrency<AccountId>>::transfer(
X_TOKEN_ID, &ALICE, &BOB, 10
));
assert_eq!(Currencies::balance(X_TOKEN_ID, &ALICE), 40);
assert_eq!(Currencies::balance(X_TOKEN_ID, &BOB), 160);
let transferred_event = TestEvent::currencies(RawEvent::Transferred(X_TOKEN_ID, ALICE, BOB, 10));
assert!(System::events().iter().any(|record| record.event == transferred_event));
assert_ok!(<Currencies as MultiCurrency<AccountId>>::deposit(
X_TOKEN_ID, &ALICE, 100
));
assert_eq!(Currencies::balance(X_TOKEN_ID, &ALICE), 140);
let transferred_event = TestEvent::currencies(RawEvent::Deposited(X_TOKEN_ID, ALICE, 100));
assert!(System::events().iter().any(|record| record.event == transferred_event));
assert_ok!(<Currencies as MultiCurrency<AccountId>>::withdraw(
X_TOKEN_ID, &ALICE, 20
));
assert_eq!(Currencies::balance(X_TOKEN_ID, &ALICE), 120);
let transferred_event = TestEvent::currencies(RawEvent::Withdrawn(X_TOKEN_ID, ALICE, 20));
assert!(System::events().iter().any(|record| record.event == transferred_event));
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment