Skip to content

Commit

Permalink
refactor: inconsistent BalanceConversion fn
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Mar 15, 2023
1 parent ae83a67 commit 1177877
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
9 changes: 3 additions & 6 deletions frame/assets/src/tests.rs
Expand Up @@ -1164,19 +1164,16 @@ fn balance_conversion_should_work() {
assert_ok!(Assets::force_create(RuntimeOrigin::root(), not_sufficient, 1, false, 10));
assert_eq!(asset_ids(), vec![23, 42, 999]);
assert_eq!(
BalanceToAssetBalance::<Balances, Test, ConvertInto>::to_asset_balance(100, 1234),
BalanceToAssetBalance::<Balances, Test, ConvertInto>::convert(100, 1234),
Err(ConversionError::AssetMissing)
);
assert_eq!(
BalanceToAssetBalance::<Balances, Test, ConvertInto>::to_asset_balance(
100,
not_sufficient
),
BalanceToAssetBalance::<Balances, Test, ConvertInto>::convert(100, not_sufficient),
Err(ConversionError::AssetNotSufficient)
);
// 10 / 1 == 10 -> the conversion should 10x the value
assert_eq!(
BalanceToAssetBalance::<Balances, Test, ConvertInto>::to_asset_balance(100, id),
BalanceToAssetBalance::<Balances, Test, ConvertInto>::convert(100, id),
Ok(100 * 10)
);
});
Expand Down
2 changes: 1 addition & 1 deletion frame/assets/src/types.rs
Expand Up @@ -245,7 +245,7 @@ where
///
/// Will return `Err` if the asset is not found, not sufficient or the fungible's minimum
/// balance is zero.
fn to_asset_balance(
fn convert(
balance: BalanceOf<F, T>,
asset_id: AssetIdOf<T, I>,
) -> Result<AssetBalanceOf<T, I>, ConversionError> {
Expand Down
5 changes: 3 additions & 2 deletions frame/support/src/traits/tokens/misc.rs
Expand Up @@ -189,10 +189,11 @@ impl<
{
}

/// Converts a balance value into an asset balance.
/// Provides a conversion mechanism between two balances.
/// The most natural conversion would be native to asset balance.
pub trait BalanceConversion<InBalance, AssetId, OutBalance> {
type Error;
fn to_asset_balance(balance: InBalance, asset_id: AssetId) -> Result<OutBalance, Self::Error>;
fn convert(balance: InBalance, asset_id: AssetId) -> Result<OutBalance, Self::Error>;
}

/// Trait to handle asset locking mechanism to ensure interactions with the asset can be implemented
Expand Down
6 changes: 3 additions & 3 deletions frame/transaction-payment/asset-tx-payment/src/payment.rs
Expand Up @@ -118,7 +118,7 @@ where
// less than one (e.g. 0.5) but gets rounded down by integer division we introduce a minimum
// fee.
let min_converted_fee = if fee.is_zero() { Zero::zero() } else { One::one() };
let converted_fee = CON::to_asset_balance(fee, asset_id)
let converted_fee = CON::convert(fee, asset_id)
.map_err(|_| TransactionValidityError::from(InvalidTransaction::Payment))?
.max(min_converted_fee);
let can_withdraw =
Expand Down Expand Up @@ -146,10 +146,10 @@ where
) -> Result<(AssetBalanceOf<T>, AssetBalanceOf<T>), TransactionValidityError> {
let min_converted_fee = if corrected_fee.is_zero() { Zero::zero() } else { One::one() };
// Convert the corrected fee and tip into the asset used for payment.
let converted_fee = CON::to_asset_balance(corrected_fee, paid.asset())
let converted_fee = CON::convert(corrected_fee, paid.asset())
.map_err(|_| -> TransactionValidityError { InvalidTransaction::Payment.into() })?
.max(min_converted_fee);
let converted_tip = CON::to_asset_balance(tip, paid.asset())
let converted_tip = CON::convert(tip, paid.asset())
.map_err(|_| -> TransactionValidityError { InvalidTransaction::Payment.into() })?;

// Calculate how much refund we should return.
Expand Down

0 comments on commit 1177877

Please sign in to comment.