From 38377a91aa8cc6c25f650ef9d975294579eae437 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 28 May 2024 09:14:07 +0200 Subject: [PATCH] restore set_balance as it was --- pallets/restricted-tokens/src/benchmarking.rs | 10 ++++- pallets/restricted-tokens/src/lib.rs | 45 +++++++++++++++++-- pallets/restricted-tokens/src/tests.rs | 15 +++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/pallets/restricted-tokens/src/benchmarking.rs b/pallets/restricted-tokens/src/benchmarking.rs index 09f61dd32f..1cc44527fd 100644 --- a/pallets/restricted-tokens/src/benchmarking.rs +++ b/pallets/restricted-tokens/src/benchmarking.rs @@ -332,13 +332,16 @@ 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); - }:set_balance(RawOrigin::Root, recv_lookup, currency, free) + reserve_balance::(currency, &recv, reserved + reserved); + }:set_balance(RawOrigin::Root, recv_lookup, currency, free, reserved) 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)); } @@ -349,13 +352,16 @@ 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); - }:set_balance(RawOrigin::Root, recv_loopup, currency.clone(), free) + reserve_balance::(currency, &recv, reserved + reserved); + }:set_balance(RawOrigin::Root, recv_loopup, currency.clone(), free, reserved) verify { + assert!( as fungible::InspectHold>::total_balance_on_hold(&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 96b54ef089..c224d22018 100644 --- a/pallets/restricted-tokens/src/lib.rs +++ b/pallets/restricted-tokens/src/lib.rs @@ -67,7 +67,7 @@ pub mod pallet { use frame_support::{ pallet_prelude::TypeInfo, sp_runtime::{ - traits::{AtLeast32BitUnsigned, StaticLookup}, + traits::{AtLeast32BitUnsigned, EnsureAdd, StaticLookup}, FixedPointOperand, }, traits::tokens::{Fortitude, Precision, Preservation}, @@ -238,6 +238,7 @@ pub mod pallet { currency_id: T::CurrencyId, who: T::AccountId, free: T::Balance, + reserved: T::Balance, }, } @@ -501,11 +502,24 @@ 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.ensure_add(new_reserved)?; let token = if T::NativeToken::get() == currency_id { + let old_reserved = >::balance_on_hold( + &HoldReason::NativeIndex.into(), + &who, + ); + + >::release( + &HoldReason::NativeIndex.into(), + &who, + old_reserved, + Precision::Exact, + )?; let to_burn = >::balance(&who); >::burn_from( &who, @@ -513,10 +527,28 @@ pub mod pallet { Precision::Exact, Fortitude::Force, )?; - >::mint_into(&who, new_free)?; + >::mint_into(&who, new_total)?; + >::hold( + &HoldReason::NativeIndex.into(), + &who, + new_reserved, + )?; TokenType::Native } else { + let old_reserved = + >::balance_on_hold( + currency_id, + &(), + &who, + ); + >::release( + currency_id, + &(), + &who, + old_reserved, + Precision::Exact, + )?; let to_burn = >::balance(currency_id, &who); >::burn_from( @@ -529,7 +561,13 @@ pub mod pallet { >::mint_into( currency_id, &who, - new_free, + new_total, + )?; + >::hold( + currency_id, + &(), + &who, + new_reserved, )?; TokenType::Other @@ -539,6 +577,7 @@ 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 97f8aa6323..8c6d5a50e8 100644 --- a/pallets/restricted-tokens/src/tests.rs +++ b/pallets/restricted-tokens/src/tests.rs @@ -252,6 +252,7 @@ fn set_balance_native_works() { 1, CurrencyId::Cfg, 200, + 100, )); assert_eq!(System::account(1).data.free, 200); }) @@ -267,37 +268,51 @@ 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 + ); }) }