Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
fix unreserve_all_named
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Jun 8, 2021
1 parent fa26ce6 commit d1b62c9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
71 changes: 71 additions & 0 deletions frame/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,5 +1082,76 @@ macro_rules! decl_tests {
assert_eq!(Balances::free_balance(&1), 52);
});
}

#[test]
fn ensure_reserved_named_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 111);

let id = [1u8; 8];

assert_ok!(Balances::ensure_reserved_named(&id, &1, 15));
assert_eq!(Balances::reserved_balance_named(&id, &1), 15);

assert_ok!(Balances::ensure_reserved_named(&id, &1, 10));
assert_eq!(Balances::reserved_balance_named(&id, &1), 10);

assert_ok!(Balances::ensure_reserved_named(&id, &1, 20));
assert_eq!(Balances::reserved_balance_named(&id, &1), 20);
});
}

#[test]
fn unreserve_all_named_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 111);

let id = [1u8; 8];

assert_ok!(Balances::reserve_named(&id, &1, 15));

assert_eq!(Balances::unreserve_all_named(&id, &1), 15);
assert_eq!(Balances::reserved_balance_named(&id, &1), 0);

assert_eq!(Balances::unreserve_all_named(&id, &1), 0);
});
}

#[test]
fn slash_all_reserved_named_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 111);

let id = [1u8; 8];

assert_ok!(Balances::reserve_named(&id, &1, 15));

assert_eq!(Balances::slash_all_reserved_named(&id, &1).peek(), 15);
assert_eq!(Balances::reserved_balance_named(&id, &1), 0);

assert_eq!(Balances::slash_all_reserved_named(&id, &1).peek(), 0);
});
}

#[test]
fn repatriate_all_reserved_named_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 111);
let _ = Balances::deposit_creating(&2, 10);
let _ = Balances::deposit_creating(&3, 10);

let id = [1u8; 8];

assert_ok!(Balances::reserve_named(&id, &1, 15));

assert_ok!(Balances::repatriate_all_reserved_named(&id, &1, &2, Status::Reserved));
assert_eq!(Balances::reserved_balance_named(&id, &1), 0);
assert_eq!(Balances::reserved_balance_named(&id, &2), 15);

assert_ok!(Balances::repatriate_all_reserved_named(&id, &2, &3, Status::Free));
assert_eq!(Balances::reserved_balance_named(&id, &2), 0);
assert_eq!(Balances::free_balance(&3), 25);
});
}
}
}
2 changes: 1 addition & 1 deletion frame/support/src/traits/tokens/currency/reservable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub trait NamedReservableCurrency<AccountId>: ReservableCurrency<AccountId> {
/// Is a no-op if the value to be unreserved is zero.
fn unreserve_all_named(id: &Self::ReserveIdentifier, who: &AccountId) -> Self::Balance {
let value = Self::reserved_balance_named(id, who);
Self::slash_reserved_named(id, who, value);
Self::unreserve_named(id, who, value);
value
}

Expand Down

0 comments on commit d1b62c9

Please sign in to comment.