Skip to content

Commit

Permalink
Add Freeze/Thaw events and tests (#13779)
Browse files Browse the repository at this point in the history
* Add Freeze/Thaw events and tests

* Remove duplicate docstring

* Correct spelling error

* Cargo fmt

* Use proper punctuation in docstring

Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com>

---------

Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com>
Co-authored-by: parity-processbot <>
  • Loading branch information
sea212 and jsidorenko authored Apr 21, 2023
1 parent 2059dca commit 471b49b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
15 changes: 15 additions & 0 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ pub mod pallet {
Locked { who: T::AccountId, amount: T::Balance },
/// Some balance was unlocked.
Unlocked { who: T::AccountId, amount: T::Balance },
/// Some balance was frozen.
Frozen { who: T::AccountId, amount: T::Balance },
/// Some balance was thawed.
Thawed { who: T::AccountId, amount: T::Balance },
}

#[pallet::error]
Expand Down Expand Up @@ -1084,21 +1088,32 @@ pub mod pallet {
who: &T::AccountId,
freezes: BoundedSlice<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
) -> DispatchResult {
let mut prev_frozen = Zero::zero();
let mut after_frozen = Zero::zero();
let (_, maybe_dust) = Self::mutate_account(who, |b| {
prev_frozen = b.frozen;
b.frozen = Zero::zero();
for l in Locks::<T, I>::get(who).iter() {
b.frozen = b.frozen.max(l.amount);
}
for l in freezes.iter() {
b.frozen = b.frozen.max(l.amount);
}
after_frozen = b.frozen;
})?;
debug_assert!(maybe_dust.is_none(), "Not altering main balance; qed");
if freezes.is_empty() {
Freezes::<T, I>::remove(who);
} else {
Freezes::<T, I>::insert(who, freezes);
}
if prev_frozen > after_frozen {
let amount = prev_frozen.saturating_sub(after_frozen);
Self::deposit_event(Event::Thawed { who: who.clone(), amount });
} else if after_frozen > prev_frozen {
let amount = after_frozen.saturating_sub(prev_frozen);
Self::deposit_event(Event::Frozen { who: who.clone(), amount });
}
Ok(())
}

Expand Down
46 changes: 46 additions & 0 deletions frame/balances/src/tests/fungible_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,49 @@ fn unholding_frees_hold_slot() {
assert_ok!(Balances::hold(&TestId::Baz, &1, 10));
});
}

#[test]
fn emit_events_with_changing_freezes() {
ExtBuilder::default().build_and_execute_with(|| {
let _ = Balances::set_balance(&1, 100);
System::reset_events();

// Freeze = [] --> [10]
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 10));
assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Frozen { who: 1, amount: 10 })]);

// Freeze = [10] --> [15]
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 15));
assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Frozen { who: 1, amount: 5 })]);

// Freeze = [15] --> [15, 20]
assert_ok!(Balances::set_freeze(&TestId::Bar, &1, 20));
assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Frozen { who: 1, amount: 5 })]);

// Freeze = [15, 20] --> [17, 20]
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 17));
for event in events() {
match event {
RuntimeEvent::Balances(crate::Event::Frozen { .. }) => {
assert!(false, "unexpected freeze event")
},
RuntimeEvent::Balances(crate::Event::Thawed { .. }) => {
assert!(false, "unexpected thaw event")
},
_ => continue,
}
}

// Freeze = [17, 20] --> [17, 15]
assert_ok!(Balances::set_freeze(&TestId::Bar, &1, 15));
assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 3 })]);

// Freeze = [17, 15] --> [15]
assert_ok!(Balances::thaw(&TestId::Foo, &1));
assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 2 })]);

// Freeze = [15] --> []
assert_ok!(Balances::thaw(&TestId::Bar, &1));
assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 15 })]);
});
}

0 comments on commit 471b49b

Please sign in to comment.