Skip to content

Commit d42fdd6

Browse files
syan095Roy Yang
and
Roy Yang
authored
Refactor/tokens events (#729)
* Added new events and ensure events are deposited whenever balance and total issuances changes happen. Added unit tests to test these functions will deposit events correctly * Moved the event deposit of do_transfer to the outside of the mutate, so transfer event is emitted last (instead of endowed). Removed some redundant deposit_event for transfer, to avoid double transfer event emission * Removed all events from orml-currencies All events are deposited at implementation level instead * Minor tidying up. Addressed some PR review comments. * Fixed some line spacing with event deposits * Minor linebreak improvements Co-authored-by: Roy Yang <[email protected]>
1 parent 238828a commit d42fdd6

File tree

6 files changed

+495
-148
lines changed

6 files changed

+495
-148
lines changed

currencies/src/lib.rs

+11-79
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ pub mod module {
8181

8282
#[pallet::config]
8383
pub trait Config: frame_system::Config {
84-
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
85-
8684
type MultiCurrency: TransferAll<Self::AccountId>
8785
+ MultiCurrencyExtended<Self::AccountId>
8886
+ MultiLockableCurrency<Self::AccountId>
@@ -109,36 +107,6 @@ pub mod module {
109107
DepositFailed,
110108
}
111109

112-
#[pallet::event]
113-
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
114-
pub enum Event<T: Config> {
115-
/// Currency transfer success.
116-
Transferred {
117-
currency_id: CurrencyIdOf<T>,
118-
from: T::AccountId,
119-
to: T::AccountId,
120-
amount: BalanceOf<T>,
121-
},
122-
/// Update balance success.
123-
BalanceUpdated {
124-
currency_id: CurrencyIdOf<T>,
125-
who: T::AccountId,
126-
amount: AmountOf<T>,
127-
},
128-
/// Deposit success.
129-
Deposited {
130-
currency_id: CurrencyIdOf<T>,
131-
who: T::AccountId,
132-
amount: BalanceOf<T>,
133-
},
134-
/// Withdraw success.
135-
Withdrawn {
136-
currency_id: CurrencyIdOf<T>,
137-
who: T::AccountId,
138-
amount: BalanceOf<T>,
139-
},
140-
}
141-
142110
#[pallet::pallet]
143111
pub struct Pallet<T>(_);
144112

@@ -160,8 +128,7 @@ pub mod module {
160128
) -> DispatchResult {
161129
let from = ensure_signed(origin)?;
162130
let to = T::Lookup::lookup(dest)?;
163-
<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, &from, &to, amount)?;
164-
Ok(())
131+
<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, &from, &to, amount)
165132
}
166133

167134
/// Transfer some native currency to another account.
@@ -176,15 +143,7 @@ pub mod module {
176143
) -> DispatchResult {
177144
let from = ensure_signed(origin)?;
178145
let to = T::Lookup::lookup(dest)?;
179-
T::NativeCurrency::transfer(&from, &to, amount)?;
180-
181-
Self::deposit_event(Event::Transferred {
182-
currency_id: T::GetNativeCurrencyId::get(),
183-
from,
184-
to,
185-
amount,
186-
});
187-
Ok(())
146+
T::NativeCurrency::transfer(&from, &to, amount)
188147
}
189148

190149
/// update amount of account `who` under `currency_id`.
@@ -199,8 +158,7 @@ pub mod module {
199158
) -> DispatchResult {
200159
ensure_root(origin)?;
201160
let dest = T::Lookup::lookup(who)?;
202-
<Self as MultiCurrencyExtended<T::AccountId>>::update_balance(currency_id, &dest, amount)?;
203-
Ok(())
161+
<Self as MultiCurrencyExtended<T::AccountId>>::update_balance(currency_id, &dest, amount)
204162
}
205163
}
206164
}
@@ -259,51 +217,32 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
259217
return Ok(());
260218
}
261219
if currency_id == T::GetNativeCurrencyId::get() {
262-
T::NativeCurrency::transfer(from, to, amount)?;
220+
T::NativeCurrency::transfer(from, to, amount)
263221
} else {
264-
T::MultiCurrency::transfer(currency_id, from, to, amount)?;
222+
T::MultiCurrency::transfer(currency_id, from, to, amount)
265223
}
266-
Self::deposit_event(Event::Transferred {
267-
currency_id,
268-
from: from.clone(),
269-
to: to.clone(),
270-
amount,
271-
});
272-
Ok(())
273224
}
274225

275226
fn deposit(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
276227
if amount.is_zero() {
277228
return Ok(());
278229
}
279230
if currency_id == T::GetNativeCurrencyId::get() {
280-
T::NativeCurrency::deposit(who, amount)?;
231+
T::NativeCurrency::deposit(who, amount)
281232
} else {
282-
T::MultiCurrency::deposit(currency_id, who, amount)?;
233+
T::MultiCurrency::deposit(currency_id, who, amount)
283234
}
284-
Self::deposit_event(Event::Deposited {
285-
currency_id,
286-
who: who.clone(),
287-
amount,
288-
});
289-
Ok(())
290235
}
291236

292237
fn withdraw(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
293238
if amount.is_zero() {
294239
return Ok(());
295240
}
296241
if currency_id == T::GetNativeCurrencyId::get() {
297-
T::NativeCurrency::withdraw(who, amount)?;
242+
T::NativeCurrency::withdraw(who, amount)
298243
} else {
299-
T::MultiCurrency::withdraw(currency_id, who, amount)?;
244+
T::MultiCurrency::withdraw(currency_id, who, amount)
300245
}
301-
Self::deposit_event(Event::Withdrawn {
302-
currency_id,
303-
who: who.clone(),
304-
amount,
305-
});
306-
Ok(())
307246
}
308247

309248
fn can_slash(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> bool {
@@ -328,16 +267,10 @@ impl<T: Config> MultiCurrencyExtended<T::AccountId> for Pallet<T> {
328267

329268
fn update_balance(currency_id: Self::CurrencyId, who: &T::AccountId, by_amount: Self::Amount) -> DispatchResult {
330269
if currency_id == T::GetNativeCurrencyId::get() {
331-
T::NativeCurrency::update_balance(who, by_amount)?;
270+
T::NativeCurrency::update_balance(who, by_amount)
332271
} else {
333-
T::MultiCurrency::update_balance(currency_id, who, by_amount)?;
272+
T::MultiCurrency::update_balance(currency_id, who, by_amount)
334273
}
335-
Self::deposit_event(Event::BalanceUpdated {
336-
currency_id,
337-
who: who.clone(),
338-
amount: by_amount,
339-
});
340-
Ok(())
341274
}
342275
}
343276

@@ -608,7 +541,6 @@ where
608541
let actual_deposit = deposit_result.peek();
609542
ensure!(actual_deposit == amount, Error::<T>::DepositFailed);
610543
}
611-
612544
Ok(())
613545
}
614546

currencies/src/mock.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ parameter_types! {
9191
}
9292

9393
impl Config for Runtime {
94-
type Event = Event;
9594
type MultiCurrency = Tokens;
9695
type NativeCurrency = AdaptedBasicCurrency;
9796
type GetNativeCurrencyId = GetNativeCurrencyId;
@@ -110,7 +109,7 @@ construct_runtime!(
110109
UncheckedExtrinsic = UncheckedExtrinsic,
111110
{
112111
System: frame_system::{Pallet, Call, Storage, Config, Event<T>},
113-
Currencies: currencies::{Pallet, Call, Event<T>},
112+
Currencies: currencies::{Pallet, Call},
114113
Tokens: orml_tokens::{Pallet, Storage, Event<T>, Config<T>},
115114
PalletBalances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
116115
}

currencies/src/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn call_event_should_work() {
271271
assert_ok!(Currencies::transfer(Some(ALICE).into(), BOB, X_TOKEN_ID, 50));
272272
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 50);
273273
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &BOB), 150);
274-
System::assert_last_event(Event::Currencies(crate::Event::Transferred {
274+
System::assert_last_event(Event::Tokens(orml_tokens::Event::Transfer {
275275
currency_id: X_TOKEN_ID,
276276
from: ALICE,
277277
to: BOB,
@@ -283,7 +283,7 @@ fn call_event_should_work() {
283283
));
284284
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 40);
285285
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &BOB), 160);
286-
System::assert_last_event(Event::Currencies(crate::Event::Transferred {
286+
System::assert_last_event(Event::Tokens(orml_tokens::Event::Transfer {
287287
currency_id: X_TOKEN_ID,
288288
from: ALICE,
289289
to: BOB,
@@ -294,7 +294,7 @@ fn call_event_should_work() {
294294
X_TOKEN_ID, &ALICE, 100
295295
));
296296
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 140);
297-
System::assert_last_event(Event::Currencies(crate::Event::Deposited {
297+
System::assert_last_event(Event::Tokens(orml_tokens::Event::Deposited {
298298
currency_id: X_TOKEN_ID,
299299
who: ALICE,
300300
amount: 100,
@@ -304,7 +304,7 @@ fn call_event_should_work() {
304304
X_TOKEN_ID, &ALICE, 20
305305
));
306306
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 120);
307-
System::assert_last_event(Event::Currencies(crate::Event::Withdrawn {
307+
System::assert_last_event(Event::Tokens(orml_tokens::Event::Withdrawn {
308308
currency_id: X_TOKEN_ID,
309309
who: ALICE,
310310
amount: 20,

0 commit comments

Comments
 (0)