Skip to content

Commit

Permalink
pass asset id as ref for ConversionToAssetBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
muharem committed Sep 14, 2023
1 parent e64fba7 commit b84b809
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cumulus/parachains/common/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
let amount = WeightToFee::weight_to_fee(&weight);
// If the amount gotten is not at least the ED, then make it be the ED of the asset
// This is to avoid burning assets and decreasing the supply
let asset_amount = BalanceConverter::to_asset_balance(amount, asset_id)
let asset_amount = BalanceConverter::to_asset_balance(amount, &asset_id)
.map_err(|_| XcmError::TooExpensive)?;
Ok(asset_amount)
}
Expand Down
6 changes: 3 additions & 3 deletions substrate/frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,19 +1560,19 @@ 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>::to_asset_balance(100, &1234),
Err(ConversionError::AssetMissing)
);
assert_eq!(
BalanceToAssetBalance::<Balances, Test, ConvertInto>::to_asset_balance(
100,
not_sufficient
&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>::to_asset_balance(100, &id),
Ok(100 * 10)
);
});
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/assets/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ where
/// balance is zero.
fn to_asset_balance(
balance: BalanceOf<F, T>,
asset_id: AssetIdOf<T, I>,
asset_id: &AssetIdOf<T, I>,
) -> Result<AssetBalanceOf<T, I>, ConversionError> {
let asset = Asset::<T, I>::get(asset_id).ok_or(ConversionError::AssetMissing)?;
// only sufficient assets have a min balance with reliable value
Expand Down
6 changes: 4 additions & 2 deletions substrate/frame/support/src/traits/tokens/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@ impl<
/// Converts a balance value into an asset balance.
pub trait ConversionToAssetBalance<InBalance, AssetId, AssetBalance> {
type Error;
fn to_asset_balance(balance: InBalance, asset_id: AssetId)
-> Result<AssetBalance, Self::Error>;
fn to_asset_balance(
balance: InBalance,
asset_id: &AssetId,
) -> Result<AssetBalance, Self::Error>;
}

/// Converts an asset balance value into balance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,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::to_asset_balance(fee, &asset_id)
.map_err(|_| TransactionValidityError::from(InvalidTransaction::Payment))?
.max(min_converted_fee);
let can_withdraw =
Expand Down Expand Up @@ -156,10 +156,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::to_asset_balance(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::to_asset_balance(tip, &paid.asset())
.map_err(|_| -> TransactionValidityError { InvalidTransaction::Payment.into() })?;

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

0 comments on commit b84b809

Please sign in to comment.