Skip to content

Commit

Permalink
simplify set_balance removing the holding part
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed May 13, 2024
1 parent 5940897 commit 7d3335f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 34 deletions.
10 changes: 2 additions & 8 deletions pallets/restricted-tokens/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,13 @@ benchmarks! {
// We let the other die to have clean-up logic in weight
set_balance_native {
let free = as_balance::<T>(300);
let reserved = as_balance::<T>(200);
let currency: <T as Config>::CurrencyId = CurrencyId::Native.into();
let recv = get_account::<T>("receiver", false);
let recv_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(recv.clone());

make_free_balance::<T>(currency, &recv, free + free);
reserve_balance::<T>(currency, &recv, reserved + reserved);
}:set_balance(RawOrigin::Root, recv_lookup, currency, free, reserved)
}:set_balance(RawOrigin::Root, recv_lookup, currency, free)
verify {
assert!(<pallet_balances::Pallet<T> as fungible::InspectHold<T::AccountId>>::total_balance_on_hold(&recv) == reserved);
assert!(<pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::reducible_balance(&recv, Preservation::Protect, Fortitude::Polite) == free - <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::minimum_balance());
assert!(<pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::balance(&recv) == (free));
}
Expand All @@ -343,16 +340,13 @@ benchmarks! {
// We let the other die to have clean-up logic in weight
set_balance_other {
let free = as_balance::<T>(300);
let reserved = as_balance::<T>(200);
let currency: <T as Config>::CurrencyId = get_non_native_currency::<T>();
let recv = get_account_maybe_permission::<T>("receiver", currency.clone());
let recv_loopup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(recv.clone());

make_free_balance::<T>(currency, &recv, free + free);
reserve_balance::<T>(currency, &recv, reserved + reserved);
}:set_balance(RawOrigin::Root, recv_loopup, currency.clone(), free, reserved)
}:set_balance(RawOrigin::Root, recv_loopup, currency.clone(), free)
verify {
assert!(<orml_tokens::Pallet<T> as fungibles::InspectHold<T::AccountId>>::total_balance_on_hold(currency, &recv) == reserved);
assert!(<orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::reducible_balance(currency, &recv, Preservation::Protect, Fortitude::Polite) == free - <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::minimum_balance(currency));
assert!(<orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::balance(currency, &recv) == (free));
}
Expand Down
14 changes: 4 additions & 10 deletions pallets/restricted-tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ pub mod pallet {
use frame_support::{
pallet_prelude::TypeInfo,
sp_runtime::{
traits::{AtLeast32BitUnsigned, CheckedAdd, StaticLookup},
ArithmeticError, FixedPointOperand,
traits::{AtLeast32BitUnsigned, StaticLookup},
FixedPointOperand,
},
traits::tokens::{Fortitude, Precision, Preservation},
};
Expand Down Expand Up @@ -229,7 +229,6 @@ pub mod pallet {
currency_id: T::CurrencyId,
who: T::AccountId,
free: T::Balance,
reserved: T::Balance,
},
}

Expand Down Expand Up @@ -493,13 +492,9 @@ pub mod pallet {
who: <T::Lookup as StaticLookup>::Source,
currency_id: T::CurrencyId,
#[pallet::compact] new_free: T::Balance,
#[pallet::compact] new_reserved: T::Balance,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
let who = T::Lookup::lookup(who)?;
let new_total = new_free
.checked_add(&new_reserved)
.ok_or(ArithmeticError::Overflow)?;

let token = if T::NativeToken::get() == currency_id {
let to_burn = <Self as fungible::Inspect<T::AccountId>>::balance(&who);
Expand All @@ -509,7 +504,7 @@ pub mod pallet {
Precision::Exact,
Fortitude::Force,
)?;
<Self as fungible::Mutate<T::AccountId>>::mint_into(&who, new_total)?;
<Self as fungible::Mutate<T::AccountId>>::mint_into(&who, new_free)?;

TokenType::Native
} else {
Expand All @@ -525,7 +520,7 @@ pub mod pallet {
<T::Fungibles as fungibles::Mutate<T::AccountId>>::mint_into(
currency_id,
&who,
new_total,
new_free,
)?;

TokenType::Other
Expand All @@ -535,7 +530,6 @@ pub mod pallet {
currency_id,
who,
free: new_free,
reserved: new_reserved,
});

match token {
Expand Down
16 changes: 0 additions & 16 deletions pallets/restricted-tokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,8 @@ fn set_balance_native_works() {
1,
CurrencyId::Cfg,
200,
100
));
assert_eq!(System::account(1).data.free, 200);
assert_eq!(System::account(1).data.reserved, 100);
})
}

Expand All @@ -269,51 +267,37 @@ fn set_balance_foreign_works() {
1,
CurrencyId::AUSD,
200,
100
));
assert!(orml_tokens::Pallet::<Runtime>::accounts(1, CurrencyId::AUSD).free == 200);
assert!(orml_tokens::Pallet::<Runtime>::accounts(1, CurrencyId::AUSD).reserved == 100);

assert_ok!(pallet_restricted_tokens::Pallet::<Runtime>::set_balance(
RuntimeOrigin::root(),
1,
CurrencyId::AUSD,
400,
200
));
assert!(orml_tokens::Pallet::<Runtime>::accounts(1, CurrencyId::AUSD).free == 400);
assert!(orml_tokens::Pallet::<Runtime>::accounts(1, CurrencyId::AUSD).reserved == 200);

assert_ok!(pallet_restricted_tokens::Pallet::<Runtime>::set_balance(
RuntimeOrigin::root(),
1111,
CurrencyId::RestrictedCoin,
999,
80
));
assert!(
orml_tokens::Pallet::<Runtime>::accounts(1111, CurrencyId::RestrictedCoin).free
== 999
);
assert!(
orml_tokens::Pallet::<Runtime>::accounts(1111, CurrencyId::RestrictedCoin).reserved
== 80
);

assert_ok!(pallet_restricted_tokens::Pallet::<Runtime>::set_balance(
RuntimeOrigin::root(),
101,
CurrencyId::RestrictedCoin,
0,
100
));
assert!(
orml_tokens::Pallet::<Runtime>::accounts(101, CurrencyId::RestrictedCoin).free == 0
);
assert!(
orml_tokens::Pallet::<Runtime>::accounts(101, CurrencyId::RestrictedCoin).reserved
== 100
);
})
}

Expand Down

0 comments on commit 7d3335f

Please sign in to comment.