From 5f26e2bb62433dca8c3997de90d240b7aa020fdf Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Tue, 4 Apr 2023 09:40:30 +0200 Subject: [PATCH] refactor: inconsistent BalanceConversion fn (#13610) * refactor: inconsistent BalanceConversion fn * Revert "refactor: inconsistent BalanceConversion fn" This reverts commit 1177877631a11b64df6f019b5390a8a7018e3a3f. * refactor: rename BalanceConversion trait * feat: add ConversionFromAssetBalance --- frame/assets/src/tests.rs | 2 +- frame/assets/src/types.rs | 4 ++-- frame/support/src/traits/tokens.rs | 6 +++--- frame/support/src/traits/tokens/misc.rs | 14 ++++++++++++-- .../asset-tx-payment/src/payment.rs | 7 ++++--- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/frame/assets/src/tests.rs b/frame/assets/src/tests.rs index d4a49ac5a4651..afd224ad66642 100644 --- a/frame/assets/src/tests.rs +++ b/frame/assets/src/tests.rs @@ -1162,7 +1162,7 @@ fn set_min_balance_should_work() { #[test] fn balance_conversion_should_work() { new_test_ext().execute_with(|| { - use frame_support::traits::tokens::BalanceConversion; + use frame_support::traits::tokens::ConversionToAssetBalance; let id = 42; assert_ok!(Assets::force_create(RuntimeOrigin::root(), id, 1, true, 10)); diff --git a/frame/assets/src/types.rs b/frame/assets/src/types.rs index d50461ba59898..c83e764f4a68d 100644 --- a/frame/assets/src/types.rs +++ b/frame/assets/src/types.rs @@ -20,7 +20,7 @@ use super::*; use frame_support::{ pallet_prelude::*, - traits::{fungible, tokens::BalanceConversion}, + traits::{fungible, tokens::ConversionToAssetBalance}, }; use sp_runtime::{traits::Convert, FixedPointNumber, FixedPointOperand, FixedU128}; @@ -228,7 +228,7 @@ type BalanceOf = >>::Balance; /// Converts a balance value into an asset balance based on the ratio between the fungible's /// minimum balance and the minimum asset balance. pub struct BalanceToAssetBalance(PhantomData<(F, T, CON, I)>); -impl BalanceConversion, AssetIdOf, AssetBalanceOf> +impl ConversionToAssetBalance, AssetIdOf, AssetBalanceOf> for BalanceToAssetBalance where F: fungible::Inspect>, diff --git a/frame/support/src/traits/tokens.rs b/frame/support/src/traits/tokens.rs index d2753caa6a79d..253b49c6671f8 100644 --- a/frame/support/src/traits/tokens.rs +++ b/frame/support/src/traits/tokens.rs @@ -29,8 +29,8 @@ pub mod nonfungibles_v2; pub use imbalance::Imbalance; pub mod pay; pub use misc::{ - AssetId, Balance, BalanceConversion, BalanceStatus, ConvertRank, DepositConsequence, - ExistenceRequirement, Fortitude, GetSalary, Locker, Precision, Preservation, Provenance, - Restriction, WithdrawConsequence, WithdrawReasons, + AssetId, Balance, BalanceStatus, ConversionFromAssetBalance, ConversionToAssetBalance, + ConvertRank, DepositConsequence, ExistenceRequirement, Fortitude, GetSalary, Locker, Precision, + Preservation, Provenance, Restriction, WithdrawConsequence, WithdrawReasons, }; pub use pay::{Pay, PayFromAccount, PaymentStatus}; diff --git a/frame/support/src/traits/tokens/misc.rs b/frame/support/src/traits/tokens/misc.rs index 8ad3a9535fd0d..75aef0e04ea65 100644 --- a/frame/support/src/traits/tokens/misc.rs +++ b/frame/support/src/traits/tokens/misc.rs @@ -244,9 +244,19 @@ impl< } /// Converts a balance value into an asset balance. -pub trait BalanceConversion { +pub trait ConversionToAssetBalance { type Error; - fn to_asset_balance(balance: InBalance, asset_id: AssetId) -> Result; + fn to_asset_balance(balance: InBalance, asset_id: AssetId) + -> Result; +} + +/// Converts an asset balance value into balance. +pub trait ConversionFromAssetBalance { + type Error; + fn from_asset_balance( + balance: AssetBalance, + asset_id: AssetId, + ) -> Result; } /// Trait to handle asset locking mechanism to ensure interactions with the asset can be implemented diff --git a/frame/transaction-payment/asset-tx-payment/src/payment.rs b/frame/transaction-payment/asset-tx-payment/src/payment.rs index f8d6888b9905c..49e78fb8bce01 100644 --- a/frame/transaction-payment/asset-tx-payment/src/payment.rs +++ b/frame/transaction-payment/asset-tx-payment/src/payment.rs @@ -22,7 +22,8 @@ use frame_support::{ traits::{ fungibles::{Balanced, Credit, Inspect}, tokens::{ - Balance, BalanceConversion, Fortitude::Polite, Precision::Exact, Preservation::Protect, + Balance, ConversionToAssetBalance, Fortitude::Polite, Precision::Exact, + Preservation::Protect, }, }, unsigned::TransactionValidityError, @@ -87,7 +88,7 @@ impl> HandleCredit for () { } /// Implements the asset transaction for a balance to asset converter (implementing -/// [`BalanceConversion`]) and a credit handler (implementing [`HandleCredit`]). +/// [`ConversionToAssetBalance`]) and a credit handler (implementing [`HandleCredit`]). /// /// The credit handler is given the complete fee in terms of the asset used for the transaction. pub struct FungiblesAdapter(PhantomData<(CON, HC)>); @@ -97,7 +98,7 @@ pub struct FungiblesAdapter(PhantomData<(CON, HC)>); impl OnChargeAssetTransaction for FungiblesAdapter where T: Config, - CON: BalanceConversion, AssetIdOf, AssetBalanceOf>, + CON: ConversionToAssetBalance, AssetIdOf, AssetBalanceOf>, HC: HandleCredit, AssetIdOf: FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default + Eq + TypeInfo, {