From e751affc30325e1df95faf23ee129ae551217670 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Thu, 27 Oct 2022 14:09:27 +0800 Subject: [PATCH 1/6] Fix fungible unbalanced trait --- frame/balances/src/lib.rs | 12 ++-- frame/balances/src/tests.rs | 115 +++++++++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 5 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 4014efdd26e4e..67976cbaf06ac 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -1149,15 +1149,19 @@ impl, I: 'static> fungible::Transfer for Pallet impl, I: 'static> fungible::Unbalanced for Pallet { fn set_balance(who: &T::AccountId, amount: Self::Balance) -> DispatchResult { - Self::mutate_account(who, |account| { - account.free = amount; + Self::mutate_account(who, |account| -> DispatchResult { + // fungibles::Unbalanced::decrease_balance didn't check account.reserved + // free = new_balance - reserved + account.free = + amount.checked_sub(&account.reserved).ok_or(ArithmeticError::Underflow)?; Self::deposit_event(Event::BalanceSet { who: who.clone(), free: account.free, reserved: account.reserved, }); - })?; - Ok(()) + + Ok(()) + })? } fn set_total_issuance(amount: Self::Balance) { diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index faf53e16cb028..3ba46ca54a0c1 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -24,7 +24,7 @@ macro_rules! decl_tests { ($test:ty, $ext_builder:ty, $existential_deposit:expr) => { use crate::*; - use sp_runtime::{ArithmeticError, FixedPointNumber, traits::{SignedExtension, BadOrigin}}; + use sp_runtime::{ArithmeticError, TokenError, FixedPointNumber, traits::{SignedExtension, BadOrigin}}; use frame_support::{ assert_noop, assert_storage_noop, assert_ok, assert_err, traits::{ @@ -1298,5 +1298,118 @@ macro_rules! decl_tests { assert_eq!(Balances::reserved_balance(&1337), 42); }); } + + #[test] + fn fungible_unbalanced_trait_set_balance_works() { + <$ext_builder>::default().build().execute_with(|| { + assert_eq!(>::balance(&1337), 0); + assert_ok!(>::set_balance(&1337, 10)); + assert_eq!(>::balance(&1337), 10); + }); + } + + #[test] + fn fungible_unbalanced_trait_set_total_issuance_works() { + <$ext_builder>::default().build().execute_with(|| { + assert_eq!(>::total_issuance(), 0); + >::set_total_issuance(10); + assert_eq!(>::total_issuance(), 10); + }); + } + + #[test] + fn fungible_unbalanced_trait_decrease_balance_works() { + <$ext_builder>::default().build().execute_with(|| { + assert_ok!(>::set_balance(&1337, 10)); + assert_eq!(>::balance(&1337), 10); + + assert_noop!( + >::decrease_balance(&1337, 20), + TokenError::NoFunds + ); + assert_eq!( + >::decrease_balance(&1337, 5), + Ok(5) + ); + assert_eq!(>::balance(&1337), 5); + // reserve + assert_ok!(Balances::reserve(&1337, 4)); + assert_noop!( + >::decrease_balance(&1337, 2), + ArithmeticError::Underflow + ); + assert_eq!( + >::decrease_balance(&1337, 1), + Ok(1) + ); + }); + } + + #[test] + fn fungible_unbalanced_trait_decrease_balance_at_most_works() { + <$ext_builder>::default().build().execute_with(|| { + assert_ok!(>::set_balance(&1337, 10)); + assert_eq!(>::balance(&1337), 10); + + assert_eq!( + >::decrease_balance_at_most(&1337, 20), + 10 + ); + assert_eq!(>::balance(&1337), 0); + + assert_ok!(>::set_balance(&1337, 10)); + assert_eq!( + >::decrease_balance_at_most(&1337, 5), + 5 + ); + assert_eq!(>::balance(&1337), 5); + // reserve + assert_ok!(Balances::reserve(&1337, 4)); + assert_eq!( + >::decrease_balance_at_most(&1337, 2), + 0 + ); + assert_eq!( + >::decrease_balance_at_most(&1337, 1), + 1 + ); + }); + } + + #[test] + fn fungible_unbalanced_trait_increase_balance_works() { + <$ext_builder>::default().build().execute_with(|| { + assert_noop!( + >::increase_balance(&1337, 0), + TokenError::BelowMinimum + ); + assert_eq!( + >::increase_balance(&1337, 1), + Ok(1) + ); + assert_noop!( + >::increase_balance(&1337, u64::MAX), + ArithmeticError::Overflow + ); + }); + } + + #[test] + fn fungible_unbalanced_trait_increase_balance_at_most_works() { + <$ext_builder>::default().build().execute_with(|| { + assert_eq!( + >::increase_balance_at_most(&1337, 0), + 0 + ); + assert_eq!( + >::increase_balance_at_most(&1337, 1), + 1 + ); + assert_eq!( + >::increase_balance_at_most(&1337, u64::MAX), + u64::MAX - 1 + ); + }); + } } } From 76915227b36dd0e9cc9db5b799f5ebe20eb7d419 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Sun, 30 Oct 2022 00:35:44 +0200 Subject: [PATCH 2/6] Add simple decrease_balance test Signed-off-by: Oliver Tale-Yazdi --- frame/balances/src/tests.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index 3ba46ca54a0c1..5d22f16db50ff 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -1317,6 +1317,20 @@ macro_rules! decl_tests { }); } + #[test] + fn fungible_unbalanced_trait_decrease_balance_simple_works() { + <$ext_builder>::default().build().execute_with(|| { + // An Account that starts at 100 + assert_ok!(>::set_balance(&1337, 100)); + // and reserves 50 + assert_ok!(Balances::reserve(&1337, 50)); + // and is decreased by 20 + assert_ok!(>::decrease_balance(&1337, 20)); + // should end up at 80. + assert_eq!(>::balance(&1337), 80); + }); + } + #[test] fn fungible_unbalanced_trait_decrease_balance_works() { <$ext_builder>::default().build().execute_with(|| { From 0accb2f83385c25c3fb4b112be2080af8f4e85da Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Sun, 30 Oct 2022 12:00:33 +0800 Subject: [PATCH 3/6] Fix decrease_balance_at_most --- frame/balances/src/lib.rs | 3 + frame/balances/src/tests.rs | 89 +++++++++++++------ frame/support/src/traits/tokens/fungible.rs | 6 ++ .../src/traits/tokens/fungible/balanced.rs | 7 +- frame/support/src/traits/tokens/fungibles.rs | 3 + .../src/traits/tokens/fungibles/balanced.rs | 7 +- 6 files changed, 80 insertions(+), 35 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 67976cbaf06ac..a4d3b665855bd 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -1073,6 +1073,9 @@ impl, I: 'static> fungible::Inspect for Pallet fn balance(who: &T::AccountId) -> Self::Balance { Self::account(who).total() } + fn free_balance(who: &T::AccountId) -> Self::Balance { + Self::account(who).free + } fn reducible_balance(who: &T::AccountId, keep_alive: bool) -> Self::Balance { let a = Self::account(who); // Liquid balance is what is neither reserved nor locked/frozen. diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index 3ba46ca54a0c1..b20d38e9597cc 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -1303,8 +1303,18 @@ macro_rules! decl_tests { fn fungible_unbalanced_trait_set_balance_works() { <$ext_builder>::default().build().execute_with(|| { assert_eq!(>::balance(&1337), 0); - assert_ok!(>::set_balance(&1337, 10)); - assert_eq!(>::balance(&1337), 10); + assert_ok!(>::set_balance(&1337, 100)); + assert_eq!(>::balance(&1337), 100); + + assert_ok!(Balances::reserve(&1337, 60)); + assert_eq!(Balances::free_balance(1337) , 40); + assert_eq!(Balances::reserved_balance(1337), 60); + + assert_noop!(>::set_balance(&1337, 0), ArithmeticError::Underflow); + + assert_ok!(>::set_balance(&1337, 60)); + assert_eq!(Balances::free_balance(1337) , 0); + assert_eq!(Balances::reserved_balance(1337), 60); }); } @@ -1312,67 +1322,88 @@ macro_rules! decl_tests { fn fungible_unbalanced_trait_set_total_issuance_works() { <$ext_builder>::default().build().execute_with(|| { assert_eq!(>::total_issuance(), 0); - >::set_total_issuance(10); - assert_eq!(>::total_issuance(), 10); + >::set_total_issuance(100); + assert_eq!(>::total_issuance(), 100); }); } #[test] fn fungible_unbalanced_trait_decrease_balance_works() { <$ext_builder>::default().build().execute_with(|| { - assert_ok!(>::set_balance(&1337, 10)); - assert_eq!(>::balance(&1337), 10); + assert_ok!(>::set_balance(&1337, 100)); + assert_eq!(>::balance(&1337), 100); assert_noop!( - >::decrease_balance(&1337, 20), + >::decrease_balance(&1337, 101), TokenError::NoFunds ); assert_eq!( - >::decrease_balance(&1337, 5), - Ok(5) + >::decrease_balance(&1337, 100), + Ok(100) ); - assert_eq!(>::balance(&1337), 5); - // reserve - assert_ok!(Balances::reserve(&1337, 4)); + assert_eq!(>::balance(&1337), 0); + + // free: 40, reserved: 60 + assert_ok!(>::set_balance(&1337, 100)); + assert_ok!(Balances::reserve(&1337, 60)); + assert_eq!(Balances::free_balance(1337) , 40); + assert_eq!(Balances::reserved_balance(1337), 60); assert_noop!( - >::decrease_balance(&1337, 2), - ArithmeticError::Underflow + >::decrease_balance(&1337, 41), + TokenError::NoFunds ); assert_eq!( - >::decrease_balance(&1337, 1), - Ok(1) + >::decrease_balance(&1337, 40), + Ok(40) ); + assert_eq!(>::balance(&1337), 60); + assert_eq!(Balances::free_balance(1337), 0); + assert_eq!(Balances::reserved_balance(1337), 60); }); } #[test] fn fungible_unbalanced_trait_decrease_balance_at_most_works() { <$ext_builder>::default().build().execute_with(|| { - assert_ok!(>::set_balance(&1337, 10)); - assert_eq!(>::balance(&1337), 10); + assert_ok!(>::set_balance(&1337, 100)); + assert_eq!(>::balance(&1337), 100); assert_eq!( - >::decrease_balance_at_most(&1337, 20), - 10 + >::decrease_balance_at_most(&1337, 101), + 100 ); assert_eq!(>::balance(&1337), 0); - assert_ok!(>::set_balance(&1337, 10)); + assert_ok!(>::set_balance(&1337, 100)); assert_eq!( - >::decrease_balance_at_most(&1337, 5), - 5 + >::decrease_balance_at_most(&1337, 100), + 100 ); - assert_eq!(>::balance(&1337), 5); - // reserve - assert_ok!(Balances::reserve(&1337, 4)); + assert_eq!(>::balance(&1337), 0); + + // free: 40, reserved: 60 + assert_ok!(>::set_balance(&1337, 100)); + assert_ok!(Balances::reserve(&1337, 60)); + assert_eq!(Balances::free_balance(1337) , 40); + assert_eq!(Balances::reserved_balance(1337), 60); assert_eq!( - >::decrease_balance_at_most(&1337, 2), + >::decrease_balance_at_most(&1337, 0), 0 ); + assert_eq!(Balances::free_balance(1337) , 40); + assert_eq!(Balances::reserved_balance(1337), 60); assert_eq!( - >::decrease_balance_at_most(&1337, 1), - 1 + >::decrease_balance_at_most(&1337, 10), + 10 + ); + assert_eq!(Balances::free_balance(1337), 30); + assert_eq!( + >::decrease_balance_at_most(&1337, 200), + 30 ); + assert_eq!(>::balance(&1337), 60); + assert_eq!(Balances::free_balance(1337), 0); + assert_eq!(Balances::reserved_balance(1337), 60); }); } diff --git a/frame/support/src/traits/tokens/fungible.rs b/frame/support/src/traits/tokens/fungible.rs index 90aadb6d8daa6..ffa86449797fa 100644 --- a/frame/support/src/traits/tokens/fungible.rs +++ b/frame/support/src/traits/tokens/fungible.rs @@ -46,6 +46,9 @@ pub trait Inspect { /// Get the balance of `who`. fn balance(who: &AccountId) -> Self::Balance; + /// Get the free balance of `who`. + fn free_balance(who: &AccountId) -> Self::Balance; + /// Get the maximum amount that `who` can withdraw/transfer successfully. fn reducible_balance(who: &AccountId, keep_alive: bool) -> Self::Balance; @@ -219,6 +222,9 @@ impl< fn balance(who: &AccountId) -> Self::Balance { >::balance(A::get(), who) } + fn free_balance(who: &AccountId) -> Self::Balance { + >::free_balance(A::get(), who) + } fn reducible_balance(who: &AccountId, keep_alive: bool) -> Self::Balance { >::reducible_balance(A::get(), who, keep_alive) } diff --git a/frame/support/src/traits/tokens/fungible/balanced.rs b/frame/support/src/traits/tokens/fungible/balanced.rs index ed9c3a1afa480..d9c58198dc1c9 100644 --- a/frame/support/src/traits/tokens/fungible/balanced.rs +++ b/frame/support/src/traits/tokens/fungible/balanced.rs @@ -164,7 +164,7 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, ) -> Result { let old_balance = Self::balance(who); - let (mut new_balance, mut amount) = if old_balance < amount { + let (mut new_balance, mut amount) = if Self::free_balance(who) < amount { return Err(TokenError::NoFunds.into()) } else { (old_balance - amount, amount) @@ -186,8 +186,9 @@ pub trait Unbalanced: Inspect { /// Return the imbalance by which the account was reduced. fn decrease_balance_at_most(who: &AccountId, amount: Self::Balance) -> Self::Balance { let old_balance = Self::balance(who); - let (mut new_balance, mut amount) = if old_balance < amount { - (Zero::zero(), old_balance) + let old_free_balance = Self::free_balance(who); + let (mut new_balance, mut amount) = if old_free_balance < amount { + (old_balance.saturating_sub(old_free_balance), old_free_balance) } else { (old_balance - amount, amount) }; diff --git a/frame/support/src/traits/tokens/fungibles.rs b/frame/support/src/traits/tokens/fungibles.rs index e4108b7f80a98..38d767337b40d 100644 --- a/frame/support/src/traits/tokens/fungibles.rs +++ b/frame/support/src/traits/tokens/fungibles.rs @@ -50,6 +50,9 @@ pub trait Inspect { /// Get the `asset` balance of `who`. fn balance(asset: Self::AssetId, who: &AccountId) -> Self::Balance; + /// Get the `asset` free balance of `who`. + fn free_balance(asset: Self::AssetId, who: &AccountId) -> Self::Balance; + /// Get the maximum amount of `asset` that `who` can withdraw/transfer successfully. fn reducible_balance(asset: Self::AssetId, who: &AccountId, keep_alive: bool) -> Self::Balance; diff --git a/frame/support/src/traits/tokens/fungibles/balanced.rs b/frame/support/src/traits/tokens/fungibles/balanced.rs index a870168e4db91..0de0c39112faf 100644 --- a/frame/support/src/traits/tokens/fungibles/balanced.rs +++ b/frame/support/src/traits/tokens/fungibles/balanced.rs @@ -185,7 +185,7 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, ) -> Result { let old_balance = Self::balance(asset, who); - let (mut new_balance, mut amount) = if old_balance < amount { + let (mut new_balance, mut amount) = if Self::free_balance(asset, who) < amount { return Err(TokenError::NoFunds.into()) } else { (old_balance - amount, amount) @@ -211,8 +211,9 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, ) -> Self::Balance { let old_balance = Self::balance(asset, who); - let (mut new_balance, mut amount) = if old_balance < amount { - (Zero::zero(), old_balance) + let old_free_balance = Self::free_balance(asset, who); + let (mut new_balance, mut amount) = if old_free_balance < amount { + (Zero::zero(), old_free_balance) } else { (old_balance - amount, amount) }; From 45b48de9b39560a65b8bdd2e6de9447001a3979a Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Sun, 30 Oct 2022 12:04:31 +0800 Subject: [PATCH 4/6] Fix decrease_balance_at_most in fungibles --- frame/support/src/traits/tokens/fungibles/balanced.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/src/traits/tokens/fungibles/balanced.rs b/frame/support/src/traits/tokens/fungibles/balanced.rs index 0de0c39112faf..575a60f0a7feb 100644 --- a/frame/support/src/traits/tokens/fungibles/balanced.rs +++ b/frame/support/src/traits/tokens/fungibles/balanced.rs @@ -213,7 +213,7 @@ pub trait Unbalanced: Inspect { let old_balance = Self::balance(asset, who); let old_free_balance = Self::free_balance(asset, who); let (mut new_balance, mut amount) = if old_free_balance < amount { - (Zero::zero(), old_free_balance) + (old_balance.saturating_sub(old_free_balance), old_free_balance) } else { (old_balance - amount, amount) }; From e959b425727da44a4b422a4dc63788489c193570 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Sun, 30 Oct 2022 14:23:39 +0800 Subject: [PATCH 5/6] Rename free_balanceto balance_on_free --- frame/assets/src/impl_fungibles.rs | 7 +++++++ frame/balances/src/lib.rs | 2 +- frame/support/src/traits/tokens/fungible.rs | 6 +++--- frame/support/src/traits/tokens/fungible/balanced.rs | 4 ++-- frame/support/src/traits/tokens/fungibles.rs | 2 +- frame/support/src/traits/tokens/fungibles/balanced.rs | 4 ++-- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/frame/assets/src/impl_fungibles.rs b/frame/assets/src/impl_fungibles.rs index 842ee5c102c1d..60f6e811bd44b 100644 --- a/frame/assets/src/impl_fungibles.rs +++ b/frame/assets/src/impl_fungibles.rs @@ -35,6 +35,13 @@ impl, I: 'static> fungibles::Inspect<::AccountId Pallet::::balance(asset, who) } + fn balance_on_free( + _asset: Self::AssetId, + _who: &::AccountId, + ) -> Self::Balance { + unreachable!() + } + fn reducible_balance( asset: Self::AssetId, who: &::AccountId, diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index a4d3b665855bd..64d32c3751773 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -1073,7 +1073,7 @@ impl, I: 'static> fungible::Inspect for Pallet fn balance(who: &T::AccountId) -> Self::Balance { Self::account(who).total() } - fn free_balance(who: &T::AccountId) -> Self::Balance { + fn balance_on_free(who: &T::AccountId) -> Self::Balance { Self::account(who).free } fn reducible_balance(who: &T::AccountId, keep_alive: bool) -> Self::Balance { diff --git a/frame/support/src/traits/tokens/fungible.rs b/frame/support/src/traits/tokens/fungible.rs index ffa86449797fa..243eaff1955c7 100644 --- a/frame/support/src/traits/tokens/fungible.rs +++ b/frame/support/src/traits/tokens/fungible.rs @@ -47,7 +47,7 @@ pub trait Inspect { fn balance(who: &AccountId) -> Self::Balance; /// Get the free balance of `who`. - fn free_balance(who: &AccountId) -> Self::Balance; + fn balance_on_free(who: &AccountId) -> Self::Balance; /// Get the maximum amount that `who` can withdraw/transfer successfully. fn reducible_balance(who: &AccountId, keep_alive: bool) -> Self::Balance; @@ -222,8 +222,8 @@ impl< fn balance(who: &AccountId) -> Self::Balance { >::balance(A::get(), who) } - fn free_balance(who: &AccountId) -> Self::Balance { - >::free_balance(A::get(), who) + fn balance_on_free(who: &AccountId) -> Self::Balance { + >::balance_on_free(A::get(), who) } fn reducible_balance(who: &AccountId, keep_alive: bool) -> Self::Balance { >::reducible_balance(A::get(), who, keep_alive) diff --git a/frame/support/src/traits/tokens/fungible/balanced.rs b/frame/support/src/traits/tokens/fungible/balanced.rs index d9c58198dc1c9..6d41471f5c377 100644 --- a/frame/support/src/traits/tokens/fungible/balanced.rs +++ b/frame/support/src/traits/tokens/fungible/balanced.rs @@ -164,7 +164,7 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, ) -> Result { let old_balance = Self::balance(who); - let (mut new_balance, mut amount) = if Self::free_balance(who) < amount { + let (mut new_balance, mut amount) = if Self::balance_on_free(who) < amount { return Err(TokenError::NoFunds.into()) } else { (old_balance - amount, amount) @@ -186,7 +186,7 @@ pub trait Unbalanced: Inspect { /// Return the imbalance by which the account was reduced. fn decrease_balance_at_most(who: &AccountId, amount: Self::Balance) -> Self::Balance { let old_balance = Self::balance(who); - let old_free_balance = Self::free_balance(who); + let old_free_balance = Self::balance_on_free(who); let (mut new_balance, mut amount) = if old_free_balance < amount { (old_balance.saturating_sub(old_free_balance), old_free_balance) } else { diff --git a/frame/support/src/traits/tokens/fungibles.rs b/frame/support/src/traits/tokens/fungibles.rs index 38d767337b40d..3dea6eb1c36ad 100644 --- a/frame/support/src/traits/tokens/fungibles.rs +++ b/frame/support/src/traits/tokens/fungibles.rs @@ -51,7 +51,7 @@ pub trait Inspect { fn balance(asset: Self::AssetId, who: &AccountId) -> Self::Balance; /// Get the `asset` free balance of `who`. - fn free_balance(asset: Self::AssetId, who: &AccountId) -> Self::Balance; + fn balance_on_free(asset: Self::AssetId, who: &AccountId) -> Self::Balance; /// Get the maximum amount of `asset` that `who` can withdraw/transfer successfully. fn reducible_balance(asset: Self::AssetId, who: &AccountId, keep_alive: bool) -> Self::Balance; diff --git a/frame/support/src/traits/tokens/fungibles/balanced.rs b/frame/support/src/traits/tokens/fungibles/balanced.rs index 575a60f0a7feb..dbf0bb0e16c40 100644 --- a/frame/support/src/traits/tokens/fungibles/balanced.rs +++ b/frame/support/src/traits/tokens/fungibles/balanced.rs @@ -185,7 +185,7 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, ) -> Result { let old_balance = Self::balance(asset, who); - let (mut new_balance, mut amount) = if Self::free_balance(asset, who) < amount { + let (mut new_balance, mut amount) = if Self::balance_on_free(asset, who) < amount { return Err(TokenError::NoFunds.into()) } else { (old_balance - amount, amount) @@ -211,7 +211,7 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, ) -> Self::Balance { let old_balance = Self::balance(asset, who); - let old_free_balance = Self::free_balance(asset, who); + let old_free_balance = Self::balance_on_free(asset, who); let (mut new_balance, mut amount) = if old_free_balance < amount { (old_balance.saturating_sub(old_free_balance), old_free_balance) } else { From 3550a847e2f2da4b2836534bf87ec79f80432f21 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Sun, 30 Oct 2022 17:44:10 +0800 Subject: [PATCH 6/6] Use reducible_balance instead of balance_on_free --- frame/assets/src/impl_fungibles.rs | 7 ------- frame/balances/src/lib.rs | 3 --- frame/support/src/traits/tokens/fungible.rs | 6 ------ frame/support/src/traits/tokens/fungible/balanced.rs | 4 ++-- frame/support/src/traits/tokens/fungibles.rs | 3 --- frame/support/src/traits/tokens/fungibles/balanced.rs | 4 ++-- 6 files changed, 4 insertions(+), 23 deletions(-) diff --git a/frame/assets/src/impl_fungibles.rs b/frame/assets/src/impl_fungibles.rs index 60f6e811bd44b..842ee5c102c1d 100644 --- a/frame/assets/src/impl_fungibles.rs +++ b/frame/assets/src/impl_fungibles.rs @@ -35,13 +35,6 @@ impl, I: 'static> fungibles::Inspect<::AccountId Pallet::::balance(asset, who) } - fn balance_on_free( - _asset: Self::AssetId, - _who: &::AccountId, - ) -> Self::Balance { - unreachable!() - } - fn reducible_balance( asset: Self::AssetId, who: &::AccountId, diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 64d32c3751773..67976cbaf06ac 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -1073,9 +1073,6 @@ impl, I: 'static> fungible::Inspect for Pallet fn balance(who: &T::AccountId) -> Self::Balance { Self::account(who).total() } - fn balance_on_free(who: &T::AccountId) -> Self::Balance { - Self::account(who).free - } fn reducible_balance(who: &T::AccountId, keep_alive: bool) -> Self::Balance { let a = Self::account(who); // Liquid balance is what is neither reserved nor locked/frozen. diff --git a/frame/support/src/traits/tokens/fungible.rs b/frame/support/src/traits/tokens/fungible.rs index 243eaff1955c7..90aadb6d8daa6 100644 --- a/frame/support/src/traits/tokens/fungible.rs +++ b/frame/support/src/traits/tokens/fungible.rs @@ -46,9 +46,6 @@ pub trait Inspect { /// Get the balance of `who`. fn balance(who: &AccountId) -> Self::Balance; - /// Get the free balance of `who`. - fn balance_on_free(who: &AccountId) -> Self::Balance; - /// Get the maximum amount that `who` can withdraw/transfer successfully. fn reducible_balance(who: &AccountId, keep_alive: bool) -> Self::Balance; @@ -222,9 +219,6 @@ impl< fn balance(who: &AccountId) -> Self::Balance { >::balance(A::get(), who) } - fn balance_on_free(who: &AccountId) -> Self::Balance { - >::balance_on_free(A::get(), who) - } fn reducible_balance(who: &AccountId, keep_alive: bool) -> Self::Balance { >::reducible_balance(A::get(), who, keep_alive) } diff --git a/frame/support/src/traits/tokens/fungible/balanced.rs b/frame/support/src/traits/tokens/fungible/balanced.rs index 6d41471f5c377..0e75ccc22d050 100644 --- a/frame/support/src/traits/tokens/fungible/balanced.rs +++ b/frame/support/src/traits/tokens/fungible/balanced.rs @@ -164,7 +164,7 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, ) -> Result { let old_balance = Self::balance(who); - let (mut new_balance, mut amount) = if Self::balance_on_free(who) < amount { + let (mut new_balance, mut amount) = if Self::reducible_balance(who, false) < amount { return Err(TokenError::NoFunds.into()) } else { (old_balance - amount, amount) @@ -186,7 +186,7 @@ pub trait Unbalanced: Inspect { /// Return the imbalance by which the account was reduced. fn decrease_balance_at_most(who: &AccountId, amount: Self::Balance) -> Self::Balance { let old_balance = Self::balance(who); - let old_free_balance = Self::balance_on_free(who); + let old_free_balance = Self::reducible_balance(who, false); let (mut new_balance, mut amount) = if old_free_balance < amount { (old_balance.saturating_sub(old_free_balance), old_free_balance) } else { diff --git a/frame/support/src/traits/tokens/fungibles.rs b/frame/support/src/traits/tokens/fungibles.rs index 3dea6eb1c36ad..e4108b7f80a98 100644 --- a/frame/support/src/traits/tokens/fungibles.rs +++ b/frame/support/src/traits/tokens/fungibles.rs @@ -50,9 +50,6 @@ pub trait Inspect { /// Get the `asset` balance of `who`. fn balance(asset: Self::AssetId, who: &AccountId) -> Self::Balance; - /// Get the `asset` free balance of `who`. - fn balance_on_free(asset: Self::AssetId, who: &AccountId) -> Self::Balance; - /// Get the maximum amount of `asset` that `who` can withdraw/transfer successfully. fn reducible_balance(asset: Self::AssetId, who: &AccountId, keep_alive: bool) -> Self::Balance; diff --git a/frame/support/src/traits/tokens/fungibles/balanced.rs b/frame/support/src/traits/tokens/fungibles/balanced.rs index dbf0bb0e16c40..9e50ff834a874 100644 --- a/frame/support/src/traits/tokens/fungibles/balanced.rs +++ b/frame/support/src/traits/tokens/fungibles/balanced.rs @@ -185,7 +185,7 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, ) -> Result { let old_balance = Self::balance(asset, who); - let (mut new_balance, mut amount) = if Self::balance_on_free(asset, who) < amount { + let (mut new_balance, mut amount) = if Self::reducible_balance(asset, who, false) < amount { return Err(TokenError::NoFunds.into()) } else { (old_balance - amount, amount) @@ -211,7 +211,7 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, ) -> Self::Balance { let old_balance = Self::balance(asset, who); - let old_free_balance = Self::balance_on_free(asset, who); + let old_free_balance = Self::reducible_balance(asset, who, false); let (mut new_balance, mut amount) = if old_free_balance < amount { (old_balance.saturating_sub(old_free_balance), old_free_balance) } else {