Skip to content

Commit

Permalink
Merge pull request #3 from paritytech/pr3607
Browse files Browse the repository at this point in the history
Add function to query delivery fees
  • Loading branch information
PraetorP authored Mar 20, 2024
2 parents f45f9dd + 18e54e7 commit 4caea72
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
7 changes: 5 additions & 2 deletions polkadot/node/service/src/fake_runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use sp_runtime::{
use sp_version::RuntimeVersion;
use sp_weights::Weight;
use std::collections::BTreeMap;
use xcm::{VersionedAssetId, VersionedXcm};
use xcm::{VersionedAssetId, VersionedXcm, VersionedLocation, VersionedAssets};
sp_api::decl_runtime_apis! {
/// This runtime API is only implemented for the test runtime!
pub trait GetLastTimestamp {
Expand Down Expand Up @@ -400,7 +400,6 @@ sp_api::impl_runtime_apis! {
}

impl xcm_payment_runtime_api::XcmPaymentApi<Block, RuntimeCall> for Runtime {

fn query_acceptable_payment_assets(_: xcm::Version) -> Result<Vec<VersionedAssetId>, xcm_payment_runtime_api::Error> {
unimplemented!()
}
Expand All @@ -412,5 +411,9 @@ sp_api::impl_runtime_apis! {
fn query_xcm_weight(_: VersionedXcm<RuntimeCall>) -> Result<Weight, xcm_payment_runtime_api::Error> {
unimplemented!()
}

fn query_delivery_fees(_: VersionedLocation, _: VersionedXcm<()>) -> Result<VersionedAssets, xcm_payment_runtime_api::Error> {
unimplemented!()
}
}
}
16 changes: 15 additions & 1 deletion polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ use sp_staking::SessionIndex;
#[cfg(any(feature = "std", test))]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use xcm::{latest::prelude::*, IntoVersion, VersionedAssetId, VersionedLocation, VersionedXcm};
use xcm::{latest::prelude::*, IntoVersion, VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm};
use xcm_builder::PayOverXcm;

pub use frame_system::Call as SystemCall;
Expand Down Expand Up @@ -1830,6 +1830,20 @@ sp_api::impl_runtime_apis! {
<xcm_config::XcmConfig as xcm_executor::Config>::Weigher::weight(&mut message)
.map_err(|_| XcmPaymentApiError::WeightNotComputable)
}

fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result<VersionedAssets, XcmPaymentApiError> {
let destination = destination
.try_into()
.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?;
let message = message
.try_into()
.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?;
let (_, fees) = xcm_config::XcmRouter::validate(&mut Some(destination), &mut Some(message)).map_err(|error| {
log::error!("Error when querying delivery fees: {:?}", error);
XcmPaymentApiError::Unroutable
})?;
Ok(VersionedAssets::from(fees))
}
}

impl sp_api::Metadata<Block> for Runtime {
Expand Down
14 changes: 14 additions & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,20 @@ sp_api::impl_runtime_apis! {
<xcm_config::XcmConfig as xcm_executor::Config>::Weigher::weight(&mut message)
.map_err(|_| XcmPaymentApiError::WeightNotComputable)
}

fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result<VersionedAssets, XcmPaymentApiError> {
let destination = destination
.try_into()
.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?;
let message = message
.try_into()
.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?;
let (_, fees) = xcm_config::XcmRouter::validate(&mut Some(destination), &mut Some(message)).map_err(|error| {
log::error!("Error when querying delivery fees: {:?}", error);
XcmPaymentApiError::Unroutable
})?;
Ok(VersionedAssets::from(fees))
}
}

impl pallet_nomination_pools_runtime_api::NominationPoolsApi<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sp-runtime = { path = "../../../../substrate/primitives/runtime", default-featur
sp-weights = { path = "../../../../substrate/primitives/weights", default-features = false }
xcm = { package = "staging-xcm", path = "../..", default-features = false }
frame-support = { path = "../../../../substrate/frame/support", default-features = false }

[features]
default = ["std"]
std = [
Expand Down
14 changes: 13 additions & 1 deletion polkadot/xcm/xcm-builder/xcm-payment-runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use codec::{Codec, Decode, Encode};
use frame_support::pallet_prelude::TypeInfo;
use sp_std::vec::Vec;
use sp_weights::Weight;
use xcm::{Version, VersionedAssetId, VersionedXcm};
use xcm::{Version, VersionedAssetId, VersionedXcm, VersionedLocation, VersionedAssets};

sp_api::decl_runtime_apis! {
/// A trait of XCM payment API.
Expand Down Expand Up @@ -60,6 +60,16 @@ sp_api::decl_runtime_apis! {
/// * `weight`: convertible `Weight`.
/// * `asset`: `VersionedAssetId`.
fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, Error>;

/// Get delivery fees for sending a specific `message` to a `destination`.
/// These always come in a specific asset, defined by the chain.
///
/// # Arguments
/// * `message`: The message that'll be sent, necessary because most delivery fees are based on the
/// size of the message.
/// * `destination`: The destination to send the message to. Different destinations may use
/// different senders that charge different fees.
fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result<VersionedAssets, Error>;
}
}

Expand All @@ -79,4 +89,6 @@ pub enum Error {
/// The given asset is not handled(as a fee payment).
#[codec(index = 4)]
AssetNotFound,
#[codec(index = 5)]
Unroutable,
}

0 comments on commit 4caea72

Please sign in to comment.