From 7d3335f9f2ce565fe877a778e82171ac91d91ed7 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Mon, 13 May 2024 13:36:38 +0200 Subject: [PATCH] simplify set_balance removing the holding part --- pallets/restricted-tokens/src/benchmarking.rs | 10 ++-------- pallets/restricted-tokens/src/lib.rs | 14 ++++---------- pallets/restricted-tokens/src/tests.rs | 16 ---------------- 3 files changed, 6 insertions(+), 34 deletions(-) diff --git a/pallets/restricted-tokens/src/benchmarking.rs b/pallets/restricted-tokens/src/benchmarking.rs index 05d9b9a17a..47ae2372f4 100644 --- a/pallets/restricted-tokens/src/benchmarking.rs +++ b/pallets/restricted-tokens/src/benchmarking.rs @@ -323,16 +323,13 @@ benchmarks! { // We let the other die to have clean-up logic in weight set_balance_native { let free = as_balance::(300); - let reserved = as_balance::(200); let currency: ::CurrencyId = CurrencyId::Native.into(); let recv = get_account::("receiver", false); let recv_lookup: ::Source = T::Lookup::unlookup(recv.clone()); make_free_balance::(currency, &recv, free + free); - reserve_balance::(currency, &recv, reserved + reserved); - }:set_balance(RawOrigin::Root, recv_lookup, currency, free, reserved) + }:set_balance(RawOrigin::Root, recv_lookup, currency, free) verify { - assert!( as fungible::InspectHold>::total_balance_on_hold(&recv) == reserved); assert!( as fungible::Inspect>::reducible_balance(&recv, Preservation::Protect, Fortitude::Polite) == free - as fungible::Inspect>::minimum_balance()); assert!( as fungible::Inspect>::balance(&recv) == (free)); } @@ -343,16 +340,13 @@ benchmarks! { // We let the other die to have clean-up logic in weight set_balance_other { let free = as_balance::(300); - let reserved = as_balance::(200); let currency: ::CurrencyId = get_non_native_currency::(); let recv = get_account_maybe_permission::("receiver", currency.clone()); let recv_loopup: ::Source = T::Lookup::unlookup(recv.clone()); make_free_balance::(currency, &recv, free + free); - reserve_balance::(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!( as fungibles::InspectHold>::total_balance_on_hold(currency, &recv) == reserved); assert!( as fungibles::Inspect>::reducible_balance(currency, &recv, Preservation::Protect, Fortitude::Polite) == free - as fungibles::Inspect>::minimum_balance(currency)); assert!( as fungibles::Inspect>::balance(currency, &recv) == (free)); } diff --git a/pallets/restricted-tokens/src/lib.rs b/pallets/restricted-tokens/src/lib.rs index 15bb7153ad..8c28c04da0 100644 --- a/pallets/restricted-tokens/src/lib.rs +++ b/pallets/restricted-tokens/src/lib.rs @@ -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}, }; @@ -229,7 +229,6 @@ pub mod pallet { currency_id: T::CurrencyId, who: T::AccountId, free: T::Balance, - reserved: T::Balance, }, } @@ -493,13 +492,9 @@ pub mod pallet { who: ::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 = >::balance(&who); @@ -509,7 +504,7 @@ pub mod pallet { Precision::Exact, Fortitude::Force, )?; - >::mint_into(&who, new_total)?; + >::mint_into(&who, new_free)?; TokenType::Native } else { @@ -525,7 +520,7 @@ pub mod pallet { >::mint_into( currency_id, &who, - new_total, + new_free, )?; TokenType::Other @@ -535,7 +530,6 @@ pub mod pallet { currency_id, who, free: new_free, - reserved: new_reserved, }); match token { diff --git a/pallets/restricted-tokens/src/tests.rs b/pallets/restricted-tokens/src/tests.rs index 6828356488..8fd11b294b 100644 --- a/pallets/restricted-tokens/src/tests.rs +++ b/pallets/restricted-tokens/src/tests.rs @@ -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); }) } @@ -269,51 +267,37 @@ fn set_balance_foreign_works() { 1, CurrencyId::AUSD, 200, - 100 )); assert!(orml_tokens::Pallet::::accounts(1, CurrencyId::AUSD).free == 200); - assert!(orml_tokens::Pallet::::accounts(1, CurrencyId::AUSD).reserved == 100); assert_ok!(pallet_restricted_tokens::Pallet::::set_balance( RuntimeOrigin::root(), 1, CurrencyId::AUSD, 400, - 200 )); assert!(orml_tokens::Pallet::::accounts(1, CurrencyId::AUSD).free == 400); - assert!(orml_tokens::Pallet::::accounts(1, CurrencyId::AUSD).reserved == 200); assert_ok!(pallet_restricted_tokens::Pallet::::set_balance( RuntimeOrigin::root(), 1111, CurrencyId::RestrictedCoin, 999, - 80 )); assert!( orml_tokens::Pallet::::accounts(1111, CurrencyId::RestrictedCoin).free == 999 ); - assert!( - orml_tokens::Pallet::::accounts(1111, CurrencyId::RestrictedCoin).reserved - == 80 - ); assert_ok!(pallet_restricted_tokens::Pallet::::set_balance( RuntimeOrigin::root(), 101, CurrencyId::RestrictedCoin, 0, - 100 )); assert!( orml_tokens::Pallet::::accounts(101, CurrencyId::RestrictedCoin).free == 0 ); - assert!( - orml_tokens::Pallet::::accounts(101, CurrencyId::RestrictedCoin).reserved - == 100 - ); }) }