From ec55d035b0c0fd53a230bd32d5ffcc06b74f6ef5 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Sun, 28 Sep 2025 11:36:44 +0800 Subject: [PATCH 1/2] add XCM fee API --- Cargo.lock | 1 + Cargo.toml | 1 + runtime/acala/Cargo.toml | 2 ++ runtime/acala/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++ runtime/karura/Cargo.toml | 2 ++ runtime/karura/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++ runtime/mandala/Cargo.toml | 2 ++ runtime/mandala/src/lib.rs | 38 +++++++++++++++++++++++++++++++++++++ 8 files changed, 124 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 21fd20b27..c747651a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4428,6 +4428,7 @@ dependencies = [ "staging-xcm-builder", "staging-xcm-executor", "substrate-wasm-builder", + "xcm-runtime-apis", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index dc8363714..0d2cfad99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -299,6 +299,7 @@ sp-weights = { version = "32.0.0", default-features = false } xcm = { version = "17.0.0", package = "staging-xcm", default-features = false } xcm-builder = { version = "21.0.0", package = "staging-xcm-builder", default-features = false } xcm-executor = { version = "20.0.0", package = "staging-xcm-executor", default-features = false } +xcm-runtime-apis = { version = "0.8.0", default-features = false } # polkadot-sdk (client) substrate-wasm-builder = { version = "27.0.0", features = ["metadata-hash"] } diff --git a/runtime/acala/Cargo.toml b/runtime/acala/Cargo.toml index 8a9e2b66a..dd6ba416e 100644 --- a/runtime/acala/Cargo.toml +++ b/runtime/acala/Cargo.toml @@ -71,6 +71,7 @@ polkadot-parachain-primitives = { workspace = true } xcm = { workspace = true } xcm-executor = { workspace = true } xcm-builder = { workspace = true } +xcm-runtime-apis = { workspace = true } pallet-xcm = { workspace = true } pallet-xcm-benchmarks = { workspace = true, optional = true } polkadot-runtime-common = { workspace = true } @@ -213,6 +214,7 @@ std = [ "polkadot-parachain-primitives/std", "xcm-builder/std", "xcm-executor/std", + "xcm-runtime-apis/std", "xcm/std", "polkadot-runtime-common/std", "parachains-common/std", diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index 19654e338..fb6756aa9 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -48,6 +48,14 @@ use sp_runtime::{ RuntimeDebug, }; use sp_std::prelude::*; +use xcm::{ + prelude::AssetId, Version as XcmVersion, VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm, +}; +use xcm_runtime_apis::{ + dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects}, + fees::Error as XcmPaymentApiError, +}; + #[cfg(feature = "std")] use sp_version::NativeVersion; use sp_version::RuntimeVersion; @@ -2334,6 +2342,37 @@ sp_api::impl_runtime_apis! { } } + impl xcm_runtime_apis::fees::XcmPaymentApi for Runtime { + fn query_acceptable_payment_assets(xcm_version: XcmVersion) -> Result, XcmPaymentApiError> { + let acceptable_assets = vec![AssetId(xcm_config::SelfLocation::get())]; + PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets) + } + + fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result { + use crate::xcm_config::XcmConfig; + type Trader = ::Trader; + PolkadotXcm::query_weight_to_asset_fee::(weight, asset) + } + + fn query_xcm_weight(message: VersionedXcm<()>) -> Result { + PolkadotXcm::query_xcm_weight(message) + } + + fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result { + PolkadotXcm::query_delivery_fees(destination, message) + } + } + + impl xcm_runtime_apis::dry_run::DryRunApi for Runtime { + fn dry_run_call(origin: OriginCaller, call: RuntimeCall, result_xcms_version: XcmVersion) -> Result, XcmDryRunApiError> { + PolkadotXcm::dry_run_call::(origin, call, result_xcms_version) + } + + fn dry_run_xcm(origin_location: VersionedLocation, xcm: VersionedXcm) -> Result, XcmDryRunApiError> { + PolkadotXcm::dry_run_xcm::(origin_location, xcm) + } + } + #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) { diff --git a/runtime/karura/Cargo.toml b/runtime/karura/Cargo.toml index ab9865017..e3cdd34a2 100644 --- a/runtime/karura/Cargo.toml +++ b/runtime/karura/Cargo.toml @@ -71,6 +71,7 @@ polkadot-parachain-primitives = { workspace = true } xcm = { workspace = true } xcm-executor = { workspace = true } xcm-builder = { workspace = true } +xcm-runtime-apis = { workspace = true } pallet-xcm = { workspace = true } pallet-xcm-benchmarks = { workspace = true, optional = true } polkadot-runtime-common = { workspace = true } @@ -215,6 +216,7 @@ std = [ "polkadot-parachain-primitives/std", "xcm-builder/std", "xcm-executor/std", + "xcm-runtime-apis/std", "xcm/std", "polkadot-runtime-common/std", "parachains-common/std", diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index 6bcbfef54..14def5ffd 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -49,6 +49,14 @@ use sp_runtime::{ RuntimeDebug, }; use sp_std::prelude::*; +use xcm::{ + prelude::AssetId, Version as XcmVersion, VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm, +}; +use xcm_runtime_apis::{ + dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects}, + fees::Error as XcmPaymentApiError, +}; + #[cfg(feature = "std")] use sp_version::NativeVersion; use sp_version::RuntimeVersion; @@ -2339,6 +2347,37 @@ impl_runtime_apis! { } } + impl xcm_runtime_apis::fees::XcmPaymentApi for Runtime { + fn query_acceptable_payment_assets(xcm_version: XcmVersion) -> Result, XcmPaymentApiError> { + let acceptable_assets = vec![AssetId(xcm_config::SelfLocation::get())]; + PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets) + } + + fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result { + use crate::xcm_config::XcmConfig; + type Trader = ::Trader; + PolkadotXcm::query_weight_to_asset_fee::(weight, asset) + } + + fn query_xcm_weight(message: VersionedXcm<()>) -> Result { + PolkadotXcm::query_xcm_weight(message) + } + + fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result { + PolkadotXcm::query_delivery_fees(destination, message) + } + } + + impl xcm_runtime_apis::dry_run::DryRunApi for Runtime { + fn dry_run_call(origin: OriginCaller, call: RuntimeCall, result_xcms_version: XcmVersion) -> Result, XcmDryRunApiError> { + PolkadotXcm::dry_run_call::(origin, call, result_xcms_version) + } + + fn dry_run_xcm(origin_location: VersionedLocation, xcm: VersionedXcm) -> Result, XcmDryRunApiError> { + PolkadotXcm::dry_run_xcm::(origin_location, xcm) + } + } + #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) { diff --git a/runtime/mandala/Cargo.toml b/runtime/mandala/Cargo.toml index d490249da..494d12e70 100644 --- a/runtime/mandala/Cargo.toml +++ b/runtime/mandala/Cargo.toml @@ -74,6 +74,7 @@ polkadot-parachain-primitives = { workspace = true } xcm = { workspace = true } xcm-executor = { workspace = true } xcm-builder = { workspace = true } +xcm-runtime-apis = { workspace = true } pallet-xcm = { workspace = true } pallet-xcm-benchmarks = { workspace = true, optional = true } polkadot-runtime-common = { workspace = true } @@ -226,6 +227,7 @@ std = [ "polkadot-parachain-primitives/std", "xcm-builder/std", "xcm-executor/std", + "xcm-runtime-apis/std", "xcm/std", "polkadot-runtime-common/std", "parachains-common/std", diff --git a/runtime/mandala/src/lib.rs b/runtime/mandala/src/lib.rs index 2d7b01d9e..e416086f4 100644 --- a/runtime/mandala/src/lib.rs +++ b/runtime/mandala/src/lib.rs @@ -86,6 +86,13 @@ use sp_runtime::{ ApplyExtrinsicResult, ArithmeticError, DispatchResult, FixedPointNumber, RuntimeDebug, }; use sp_std::prelude::*; +use xcm::{ + prelude::AssetId, Version as XcmVersion, VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm, +}; +use xcm_runtime_apis::{ + dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects}, + fees::Error as XcmPaymentApiError, +}; #[cfg(feature = "std")] use sp_version::NativeVersion; @@ -2514,6 +2521,37 @@ impl_runtime_apis! { } } + impl xcm_runtime_apis::fees::XcmPaymentApi for Runtime { + fn query_acceptable_payment_assets(xcm_version: XcmVersion) -> Result, XcmPaymentApiError> { + let acceptable_assets = vec![AssetId(xcm_config::SelfLocation::get())]; + PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets) + } + + fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result { + use crate::xcm_config::XcmConfig; + type Trader = ::Trader; + PolkadotXcm::query_weight_to_asset_fee::(weight, asset) + } + + fn query_xcm_weight(message: VersionedXcm<()>) -> Result { + PolkadotXcm::query_xcm_weight(message) + } + + fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result { + PolkadotXcm::query_delivery_fees(destination, message) + } + } + + impl xcm_runtime_apis::dry_run::DryRunApi for Runtime { + fn dry_run_call(origin: OriginCaller, call: RuntimeCall, result_xcms_version: XcmVersion) -> Result, XcmDryRunApiError> { + PolkadotXcm::dry_run_call::(origin, call, result_xcms_version) + } + + fn dry_run_xcm(origin_location: VersionedLocation, xcm: VersionedXcm) -> Result, XcmDryRunApiError> { + PolkadotXcm::dry_run_xcm::(origin_location, xcm) + } + } + #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) { From 82d963124a6d86e52adc67ffe9bb420d3e722518 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Sun, 28 Sep 2025 12:59:28 +0800 Subject: [PATCH 2/2] fix --- Cargo.lock | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c747651a9..bb0ae1f9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -160,6 +160,7 @@ dependencies = [ "staging-xcm-builder", "staging-xcm-executor", "substrate-wasm-builder", + "xcm-runtime-apis", ] [[package]] @@ -3961,6 +3962,7 @@ dependencies = [ "staging-xcm-builder", "staging-xcm-executor", "substrate-wasm-builder", + "xcm-runtime-apis", ] [[package]]