Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Commit

Permalink
add xcm fee limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jiguantong committed Oct 18, 2022
1 parent e83111c commit bfac62d
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions runtime/common/src/message_router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use xcm::prelude::*;
use xcm_executor::traits::WeightBounds;

pub type AssetUnitsPerSecond = u128;
/// Fee type of xcm message fee, paid on this chain.
pub type XcmFee = u128;

/// router target
#[derive(Clone, Encode, Decode, TypeInfo, PartialEq, Debug)]
Expand Down Expand Up @@ -74,7 +76,7 @@ pub mod pallet {
target_location: MultiLocation,
local_asset_units_per_second: AssetUnitsPerSecond,
},
ForwardTo(MultiLocation, Target, Xcm<()>, Weight, u128),
ForwardTo(MultiLocation, Target, Xcm<()>, Weight, XcmFee),
}

#[pallet::error]
Expand All @@ -83,14 +85,17 @@ pub mod pallet {
TargetXcmExecNotConfig,
/// The message's weight could not be determined.
UnweighableMessage,
/// XCM execution failed. https://github.com/paritytech/substrate/pull/10242
XcmExecutionFailed,
/// Failed to transfer xcm fee.
FailedPayXcmFee,
BadVersion,
/// MultiLocation value too large to descend further.
MultiLocationFull,
/// Failed to send xcm.
XcmSendFailed,
/// Failed to convert account id to [u8; 32].
AccountIdConversionFailed,
/// When the `xcm_fee_limit` is less than required.
TooExpensive,
}

/// Stores the units per second executed by the target chain for local asset(e.g. CRAB).
Expand Down Expand Up @@ -136,6 +141,7 @@ pub mod pallet {
origin: OriginFor<T>,
target: Target,
message: Box<VersionedXcm<<T as frame_system::Config>::Call>>,
xcm_fee_limit: Option<XcmFee>,
) -> DispatchResultWithPostInfo {
// MultiLocation origin used to execute xcm
let origin_location = T::ExecuteXcmOrigin::ensure_origin(origin.clone())?;
Expand Down Expand Up @@ -164,8 +170,12 @@ pub mod pallet {
.map_err(|()| Error::<T>::UnweighableMessage)?;
},
}
let amount = local_asset_units_per_second.saturating_mul(remote_weight as u128)
/ (WEIGHT_PER_SECOND as u128);
let amount: XcmFee = local_asset_units_per_second
.saturating_mul(remote_weight as XcmFee)
/ (WEIGHT_PER_SECOND as XcmFee);
if let Some(fee_limit) = xcm_fee_limit {
ensure!(amount <= fee_limit, Error::<T>::TooExpensive);
}
let remote_xcm_fee =
MultiAsset { id: AssetId::from(T::LocalAssetId::get()), fun: Fungible(amount) };

Expand All @@ -189,8 +199,8 @@ pub mod pallet {
)
.ensure_complete()
.map_err(|error| {
log::error!("Failed execute route message with {:?}", error);
Error::<T>::XcmExecutionFailed
log::error!("Failed transfer xcm fee with {:?}", error);
Error::<T>::FailedPayXcmFee
})?;

// Toggle the xcm_fee relative to a target context
Expand Down

0 comments on commit bfac62d

Please sign in to comment.