Skip to content

Commit

Permalink
refactor: inconsistent BalanceConversion fn (paritytech#13610)
Browse files Browse the repository at this point in the history
* refactor: inconsistent BalanceConversion fn

* Revert "refactor: inconsistent BalanceConversion fn"

This reverts commit 1177877.

* refactor: rename BalanceConversion trait

* feat: add ConversionFromAssetBalance
  • Loading branch information
wischli authored and nathanwhit committed Jul 19, 2023
1 parent 2b00d4b commit 5f26e2b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions frame/assets/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -228,7 +228,7 @@ type BalanceOf<F, T> = <F as fungible::Inspect<AccountIdOf<T>>>::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<F, T, CON, I = ()>(PhantomData<(F, T, CON, I)>);
impl<F, T, CON, I> BalanceConversion<BalanceOf<F, T>, AssetIdOf<T, I>, AssetBalanceOf<T, I>>
impl<F, T, CON, I> ConversionToAssetBalance<BalanceOf<F, T>, AssetIdOf<T, I>, AssetBalanceOf<T, I>>
for BalanceToAssetBalance<F, T, CON, I>
where
F: fungible::Inspect<AccountIdOf<T>>,
Expand Down
6 changes: 3 additions & 3 deletions frame/support/src/traits/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
14 changes: 12 additions & 2 deletions frame/support/src/traits/tokens/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,19 @@ impl<
}

/// Converts a balance value into an asset balance.
pub trait BalanceConversion<InBalance, AssetId, OutBalance> {
pub trait ConversionToAssetBalance<InBalance, AssetId, AssetBalance> {
type Error;
fn to_asset_balance(balance: InBalance, asset_id: AssetId) -> Result<OutBalance, Self::Error>;
fn to_asset_balance(balance: InBalance, asset_id: AssetId)
-> Result<AssetBalance, Self::Error>;
}

/// Converts an asset balance value into balance.
pub trait ConversionFromAssetBalance<AssetBalance, AssetId, OutBalance> {
type Error;
fn from_asset_balance(
balance: AssetBalance,
asset_id: AssetId,
) -> Result<OutBalance, Self::Error>;
}

/// Trait to handle asset locking mechanism to ensure interactions with the asset can be implemented
Expand Down
7 changes: 4 additions & 3 deletions frame/transaction-payment/asset-tx-payment/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -87,7 +88,7 @@ impl<A, B: Balanced<A>> HandleCredit<A, B> 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<CON, HC>(PhantomData<(CON, HC)>);
Expand All @@ -97,7 +98,7 @@ pub struct FungiblesAdapter<CON, HC>(PhantomData<(CON, HC)>);
impl<T, CON, HC> OnChargeAssetTransaction<T> for FungiblesAdapter<CON, HC>
where
T: Config,
CON: BalanceConversion<BalanceOf<T>, AssetIdOf<T>, AssetBalanceOf<T>>,
CON: ConversionToAssetBalance<BalanceOf<T>, AssetIdOf<T>, AssetBalanceOf<T>>,
HC: HandleCredit<T::AccountId, T::Fungibles>,
AssetIdOf<T>: FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default + Eq + TypeInfo,
{
Expand Down

0 comments on commit 5f26e2b

Please sign in to comment.