Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to query delivery fees #3

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
Loading