Skip to content

Commit d69f226

Browse files
syan095Roy Yang
andauthored
Fix/repatriate emit event (#647)
* Added the Repatriated event to Tokens module Updated unit test to test the event is correctly emited * Changed the event name to be more clear * Improved the comments Co-authored-by: Roy Yang <roy@laminar.one>
1 parent 5ba42f5 commit d69f226

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

tokens/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ pub mod module {
249249
/// Some balance was unreserved (moved from reserved to free).
250250
/// \[currency_id, who, value\]
251251
Unreserved(T::CurrencyId, T::AccountId, T::Balance),
252+
/// Some reserved balance was repatriated (moved from reserved to
253+
/// another account).
254+
/// \[currency_id, from, to, amount_actually_moved, status\]
255+
RepatriatedReserve(T::CurrencyId, T::AccountId, T::AccountId, T::Balance, BalanceStatus),
252256
/// A balance was set by root. \[who, free, reserved\]
253257
BalanceSet(T::CurrencyId, T::AccountId, T::Balance, T::Balance),
254258
}
@@ -1196,6 +1200,13 @@ impl<T: Config> MultiReservableCurrency<T::AccountId> for Pallet<T> {
11961200
}
11971201
}
11981202
Self::set_reserved_balance(currency_id, slashed, from_account.reserved - actual);
1203+
Self::deposit_event(Event::<T>::RepatriatedReserve(
1204+
currency_id,
1205+
slashed.clone(),
1206+
beneficiary.clone(),
1207+
actual,
1208+
status,
1209+
));
11991210
Ok(value - actual)
12001211
}
12011212
}

tokens/src/tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,9 @@ fn multi_reservable_currency_repatriate_reserved_work() {
13211321
Tokens::repatriate_reserved(DOT, &ALICE, &ALICE, 50, BalanceStatus::Free),
13221322
Ok(50)
13231323
);
1324+
// Repatriating from and to the same account, fund is `unreserved`.
1325+
System::assert_last_event(Event::Tokens(crate::Event::Unreserved(DOT, ALICE, 0)));
1326+
13241327
assert_eq!(Tokens::free_balance(DOT, &ALICE), 100);
13251328
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 0);
13261329

@@ -1333,13 +1336,22 @@ fn multi_reservable_currency_repatriate_reserved_work() {
13331336
Tokens::repatriate_reserved(DOT, &BOB, &BOB, 60, BalanceStatus::Reserved),
13341337
Ok(10)
13351338
);
1339+
13361340
assert_eq!(Tokens::free_balance(DOT, &BOB), 50);
13371341
assert_eq!(Tokens::reserved_balance(DOT, &BOB), 50);
13381342

13391343
assert_eq!(
13401344
Tokens::repatriate_reserved(DOT, &BOB, &ALICE, 30, BalanceStatus::Reserved),
13411345
Ok(0)
13421346
);
1347+
System::assert_last_event(Event::Tokens(crate::Event::RepatriatedReserve(
1348+
DOT,
1349+
BOB,
1350+
ALICE,
1351+
30,
1352+
BalanceStatus::Reserved,
1353+
)));
1354+
13431355
assert_eq!(Tokens::free_balance(DOT, &ALICE), 100);
13441356
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 30);
13451357
assert_eq!(Tokens::free_balance(DOT, &BOB), 50);
@@ -1349,6 +1361,16 @@ fn multi_reservable_currency_repatriate_reserved_work() {
13491361
Tokens::repatriate_reserved(DOT, &BOB, &ALICE, 30, BalanceStatus::Free),
13501362
Ok(10)
13511363
);
1364+
1365+
// Actual amount repatriated is 20.
1366+
System::assert_last_event(Event::Tokens(crate::Event::RepatriatedReserve(
1367+
DOT,
1368+
BOB,
1369+
ALICE,
1370+
20,
1371+
BalanceStatus::Free,
1372+
)));
1373+
13521374
assert_eq!(Tokens::free_balance(DOT, &ALICE), 120);
13531375
assert_eq!(Tokens::reserved_balance(DOT, &ALICE), 30);
13541376
assert_eq!(Tokens::free_balance(DOT, &BOB), 50);

0 commit comments

Comments
 (0)