Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
pallet-xcm: use right fees when estimating reserve_transfer_assets_re…
Browse files Browse the repository at this point in the history
…mote_weight
  • Loading branch information
acatangiu committed Jul 10, 2023
1 parent 6f7818e commit dd125cc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
39 changes: 25 additions & 14 deletions xcm/pallet-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,45 +1198,56 @@ impl<T: Config> Pallet<T> {
dest: MultiLocation,
beneficiary: MultiLocation,
assets: &[MultiAsset],
fees: &MultiAsset,
origin_fees: &MultiAsset,
destination_fees_setup: &DestinationFeesSetup,
) -> Result<WeightLimit, DispatchError> {
// TODO: estimate fees using local weigher,
// or add some `T::BuyExecutionSetupResolver::max_limit_for(&dest, &fees.id)`?
let max_assets = assets.len() as u32;
let assets: MultiAssets = assets.to_vec().into();
let context = T::UniversalLocation::get();
let fees = fees
.clone()
.reanchored(&dest, context)
.map_err(|_| Error::<T>::CannotReanchor)?;
let weight_limit = Limited(Weight::zero());

// estimate weight_limit by weighing message to be executed on destination
let mut remote_message = match destination_fees_setup {
DestinationFeesSetup::ByOrigin => Xcm(vec![
ReserveAssetDeposited(assets),
ClearOrigin,
BuyExecution { fees, weight_limit },
DepositAsset { assets: Wild(AllCounted(max_assets)), beneficiary },
]),
DestinationFeesSetup::ByOrigin => {
let fees = origin_fees
.clone()
.reanchored(&dest, context)
.map_err(|_| Error::<T>::CannotReanchor)?;
Xcm(vec![
ReserveAssetDeposited(assets),
ClearOrigin,
BuyExecution { fees, weight_limit },
DepositAsset { assets: Wild(AllCounted(max_assets)), beneficiary },
])
},
DestinationFeesSetup::ByUniversalLocation { .. } => {
let sovereign_account_on_destination = T::UniversalLocation::get()
.invert_target(&dest)
.map_err(|_| Error::<T>::DestinationNotInvertible)?;
let DestinationFees {
proportional_amount_to_withdraw: _,
proportional_amount_to_buy_execution,
} = T::DestinationFeesManager::estimate_fee_for(&dest, &origin_fees.id, &weight_limit)
.ok_or(Error::<T>::UnweighableMessage)?;
let remote_fees = proportional_amount_to_buy_execution
.clone()
.reanchored(&dest, context)
.map_err(|_| Error::<T>::CannotReanchor)?;
Xcm(vec![
ReserveAssetDeposited(assets.clone()),
ClearOrigin,
// Change origin to sovereign account of `T::UniversalLocation` on destination.
AliasOrigin(sovereign_account_on_destination),
// Withdraw fees (do not use those in `ReserveAssetDeposited`)
WithdrawAsset(MultiAssets::from(fees.clone())),
WithdrawAsset(MultiAssets::from(remote_fees.clone())),
ClearOrigin,
BuyExecution { fees: fees.clone(), weight_limit },
BuyExecution { fees: remote_fees.clone(), weight_limit },
RefundSurplus,
// deposit unspent `fees` back to `sovereign_account`
DepositAsset {
assets: MultiAssetFilter::from(MultiAssets::from(fees)),
assets: MultiAssetFilter::from(MultiAssets::from(remote_fees)),
beneficiary: sovereign_account_on_destination,
},
// deposit `assets` to beneficiary
Expand Down
2 changes: 1 addition & 1 deletion xcm/src/v3/multiasset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub struct MultiAsset {
/// The overall asset identity (aka *class*, in the case of a non-fungible).
pub id: AssetId,
/// The fungibility of the asset, which contains either the amount (in the case of a fungible
/// asset) or the *insance ID`, the secondary asset identifier.
/// asset) or the `instance ID`, the secondary asset identifier.
pub fun: Fungibility,
}

Expand Down

0 comments on commit dd125cc

Please sign in to comment.