Skip to content

Commit fc6c318

Browse files
Implement XcmPaymentApi and DryRunApi on all system parachains (#4634)
Depends on #4621. Implemented the [`XcmPaymentApi`](#3607) and [`DryRunApi`](#3872) on all system parachains. More scenarios can be tested on both rococo and westend if all system parachains implement this APIs. The objective is for all XCM-enabled runtimes to implement them. After demonstrating fee estimation in a UI on the testnets, come the fellowship runtimes. Step towards #690.
1 parent 8d8c0e1 commit fc6c318

File tree

24 files changed

+481
-74
lines changed

24 files changed

+481
-74
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ use xcm::latest::prelude::{
9898
};
9999
use xcm::{
100100
latest::prelude::{AssetId, BodyId},
101-
IntoVersion, VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm,
101+
VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm,
102102
};
103103
use xcm_fee_payment_runtime_api::{
104104
dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects},
@@ -1295,15 +1295,8 @@ impl_runtime_apis! {
12951295

12961296
impl xcm_fee_payment_runtime_api::fees::XcmPaymentApi<Block> for Runtime {
12971297
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
1298-
let acceptable = vec![
1299-
// native token
1300-
VersionedAssetId::from(AssetId(xcm_config::TokenLocation::get()))
1301-
];
1302-
1303-
Ok(acceptable
1304-
.into_iter()
1305-
.filter_map(|asset| asset.into_version(xcm_version).ok())
1306-
.collect())
1298+
let acceptable_assets = vec![AssetId(xcm_config::TokenLocation::get())];
1299+
PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets)
13071300
}
13081301

13091302
fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {

cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ pub use sp_runtime::BuildStorage;
8585

8686
use assets_common::{foreign_creators::ForeignCreators, matching::FromSiblingParachain};
8787
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
88-
use xcm::{
89-
prelude::{VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm},
90-
IntoVersion,
91-
};
88+
use xcm::prelude::{VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm};
9289

9390
// We exclude `Assets` since it's the name of a pallet
9491
use xcm::latest::prelude::AssetId;
@@ -1331,15 +1328,8 @@ impl_runtime_apis! {
13311328

13321329
impl xcm_fee_payment_runtime_api::fees::XcmPaymentApi<Block> for Runtime {
13331330
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
1334-
let acceptable = vec![
1335-
// native token
1336-
VersionedAssetId::from(AssetId(xcm_config::WestendLocation::get()))
1337-
];
1338-
1339-
Ok(acceptable
1340-
.into_iter()
1341-
.filter_map(|asset| asset.into_version(xcm_version).ok())
1342-
.collect())
1331+
let acceptable_assets = vec![AssetId(xcm_config::WestendLocation::get())];
1332+
PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets)
13431333
}
13441334

13451335
fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {

cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ polkadot-runtime-common = { path = "../../../../../polkadot/runtime/common", def
6666
xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false }
6767
xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false }
6868
xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false }
69+
xcm-fee-payment-runtime-api = { path = "../../../../../polkadot/xcm/xcm-fee-payment-runtime-api", default-features = false }
6970

7071
# Cumulus
7172
cumulus-pallet-aura-ext = { path = "../../../../pallets/aura-ext", default-features = false }
@@ -220,6 +221,7 @@ std = [
220221
"tuplex/std",
221222
"xcm-builder/std",
222223
"xcm-executor/std",
224+
"xcm-fee-payment-runtime-api/std",
223225
"xcm/std",
224226
]
225227

@@ -262,6 +264,7 @@ runtime-benchmarks = [
262264
"sp-runtime/runtime-benchmarks",
263265
"xcm-builder/runtime-benchmarks",
264266
"xcm-executor/runtime-benchmarks",
267+
"xcm-fee-payment-runtime-api/runtime-benchmarks",
265268
]
266269

267270
try-runtime = [

cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use frame_support::{
7070
genesis_builder_helper::{build_state, get_preset},
7171
parameter_types,
7272
traits::{ConstBool, ConstU32, ConstU64, ConstU8, Get, TransformOrigin},
73-
weights::{ConstantMultiplier, Weight},
73+
weights::{ConstantMultiplier, Weight, WeightToFee as _},
7474
PalletId,
7575
};
7676
use frame_system::{
@@ -97,7 +97,11 @@ pub use sp_runtime::BuildStorage;
9797

9898
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
9999
use rococo_runtime_constants::system_parachain::{ASSET_HUB_ID, BRIDGE_HUB_ID};
100-
use xcm::latest::prelude::*;
100+
use xcm::prelude::*;
101+
use xcm_fee_payment_runtime_api::{
102+
dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects},
103+
fees::Error as XcmPaymentApiError,
104+
};
101105

102106
use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};
103107

@@ -962,6 +966,48 @@ impl_runtime_apis! {
962966
}
963967
}
964968

969+
impl xcm_fee_payment_runtime_api::fees::XcmPaymentApi<Block> for Runtime {
970+
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
971+
let acceptable_assets = vec![AssetId(xcm_config::TokenLocation::get())];
972+
PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets)
973+
}
974+
975+
fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
976+
match asset.try_as::<AssetId>() {
977+
Ok(asset_id) if asset_id.0 == xcm_config::TokenLocation::get() => {
978+
// for native token
979+
Ok(WeightToFee::weight_to_fee(&weight))
980+
},
981+
Ok(asset_id) => {
982+
log::trace!(target: "xcm::xcm_fee_payment_runtime_api", "query_weight_to_asset_fee - unhandled asset_id: {asset_id:?}!");
983+
Err(XcmPaymentApiError::AssetNotFound)
984+
},
985+
Err(_) => {
986+
log::trace!(target: "xcm::xcm_fee_payment_runtime_api", "query_weight_to_asset_fee - failed to convert asset: {asset:?}!");
987+
Err(XcmPaymentApiError::VersionedConversionFailed)
988+
}
989+
}
990+
}
991+
992+
fn query_xcm_weight(message: VersionedXcm<()>) -> Result<Weight, XcmPaymentApiError> {
993+
PolkadotXcm::query_xcm_weight(message)
994+
}
995+
996+
fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result<VersionedAssets, XcmPaymentApiError> {
997+
PolkadotXcm::query_delivery_fees(destination, message)
998+
}
999+
}
1000+
1001+
impl xcm_fee_payment_runtime_api::dry_run::DryRunApi<Block, RuntimeCall, RuntimeEvent, OriginCaller> for Runtime {
1002+
fn dry_run_call(origin: OriginCaller, call: RuntimeCall) -> Result<CallDryRunEffects<RuntimeEvent>, XcmDryRunApiError> {
1003+
PolkadotXcm::dry_run_call::<Runtime, xcm_config::XcmRouter, OriginCaller, RuntimeCall>(origin, call)
1004+
}
1005+
1006+
fn dry_run_xcm(origin_location: VersionedLocation, xcm: VersionedXcm<RuntimeCall>) -> Result<XcmDryRunEffects<RuntimeEvent>, XcmDryRunApiError> {
1007+
PolkadotXcm::dry_run_xcm::<Runtime, xcm_config::XcmRouter, RuntimeCall, xcm_config::XcmConfig>(origin_location, xcm)
1008+
}
1009+
}
1010+
9651011
impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
9661012
fn collect_collation_info(header: &<Block as BlockT>::Header) -> cumulus_primitives_core::CollationInfo {
9671013
ParachainSystem::collect_collation_info(header)

cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ polkadot-runtime-common = { path = "../../../../../polkadot/runtime/common", def
6262
xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false }
6363
xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false }
6464
xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false }
65+
xcm-fee-payment-runtime-api = { path = "../../../../../polkadot/xcm/xcm-fee-payment-runtime-api", default-features = false }
6566

6667
# Cumulus
6768
cumulus-pallet-aura-ext = { path = "../../../../pallets/aura-ext", default-features = false }
@@ -185,6 +186,7 @@ std = [
185186
"westend-runtime-constants/std",
186187
"xcm-builder/std",
187188
"xcm-executor/std",
189+
"xcm-fee-payment-runtime-api/std",
188190
"xcm/std",
189191
]
190192

@@ -219,6 +221,7 @@ runtime-benchmarks = [
219221
"sp-runtime/runtime-benchmarks",
220222
"xcm-builder/runtime-benchmarks",
221223
"xcm-executor/runtime-benchmarks",
224+
"xcm-fee-payment-runtime-api/runtime-benchmarks",
222225
]
223226

224227
try-runtime = [

cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use frame_support::{
6464
genesis_builder_helper::{build_state, get_preset},
6565
parameter_types,
6666
traits::{ConstBool, ConstU32, ConstU64, ConstU8, Get, TransformOrigin},
67-
weights::{ConstantMultiplier, Weight},
67+
weights::{ConstantMultiplier, Weight, WeightToFee as _},
6868
PalletId,
6969
};
7070
use frame_system::{
@@ -75,13 +75,18 @@ pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
7575
pub use sp_runtime::{MultiAddress, Perbill, Permill};
7676
use xcm_config::{XcmOriginToTransactDispatchOrigin, XcmRouter};
7777

78+
use xcm_fee_payment_runtime_api::{
79+
dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects},
80+
fees::Error as XcmPaymentApiError,
81+
};
82+
7883
use bp_runtime::HeaderId;
7984

8085
#[cfg(any(feature = "std", test))]
8186
pub use sp_runtime::BuildStorage;
8287

8388
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
84-
use xcm::latest::prelude::*;
89+
use xcm::prelude::*;
8590

8691
use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};
8792

@@ -711,6 +716,48 @@ impl_runtime_apis! {
711716
}
712717
}
713718

719+
impl xcm_fee_payment_runtime_api::fees::XcmPaymentApi<Block> for Runtime {
720+
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
721+
let acceptable_assets = vec![AssetId(xcm_config::WestendLocation::get())];
722+
PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets)
723+
}
724+
725+
fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
726+
match asset.try_as::<AssetId>() {
727+
Ok(asset_id) if asset_id.0 == xcm_config::WestendLocation::get() => {
728+
// for native token
729+
Ok(WeightToFee::weight_to_fee(&weight))
730+
},
731+
Ok(asset_id) => {
732+
log::trace!(target: "xcm::xcm_fee_payment_runtime_api", "query_weight_to_asset_fee - unhandled asset_id: {asset_id:?}!");
733+
Err(XcmPaymentApiError::AssetNotFound)
734+
},
735+
Err(_) => {
736+
log::trace!(target: "xcm::xcm_fee_payment_runtime_api", "query_weight_to_asset_fee - failed to convert asset: {asset:?}!");
737+
Err(XcmPaymentApiError::VersionedConversionFailed)
738+
}
739+
}
740+
}
741+
742+
fn query_xcm_weight(message: VersionedXcm<()>) -> Result<Weight, XcmPaymentApiError> {
743+
PolkadotXcm::query_xcm_weight(message)
744+
}
745+
746+
fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result<VersionedAssets, XcmPaymentApiError> {
747+
PolkadotXcm::query_delivery_fees(destination, message)
748+
}
749+
}
750+
751+
impl xcm_fee_payment_runtime_api::dry_run::DryRunApi<Block, RuntimeCall, RuntimeEvent, OriginCaller> for Runtime {
752+
fn dry_run_call(origin: OriginCaller, call: RuntimeCall) -> Result<CallDryRunEffects<RuntimeEvent>, XcmDryRunApiError> {
753+
PolkadotXcm::dry_run_call::<Runtime, xcm_config::XcmRouter, OriginCaller, RuntimeCall>(origin, call)
754+
}
755+
756+
fn dry_run_xcm(origin_location: VersionedLocation, xcm: VersionedXcm<RuntimeCall>) -> Result<XcmDryRunEffects<RuntimeEvent>, XcmDryRunApiError> {
757+
PolkadotXcm::dry_run_xcm::<Runtime, xcm_config::XcmRouter, RuntimeCall, xcm_config::XcmConfig>(origin_location, xcm)
758+
}
759+
}
760+
714761
impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
715762
fn collect_collation_info(header: &<Block as BlockT>::Header) -> cumulus_primitives_core::CollationInfo {
716763
ParachainSystem::collect_collation_info(header)

cumulus/parachains/runtimes/collectives/collectives-westend/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ polkadot-runtime-common = { path = "../../../../../polkadot/runtime/common", def
6666
xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false }
6767
xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false }
6868
xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false }
69+
xcm-fee-payment-runtime-api = { path = "../../../../../polkadot/xcm/xcm-fee-payment-runtime-api", default-features = false }
6970
westend-runtime-constants = { path = "../../../../../polkadot/runtime/westend/constants", default-features = false }
7071

7172
# Cumulus
@@ -130,6 +131,7 @@ runtime-benchmarks = [
130131
"sp-runtime/runtime-benchmarks",
131132
"xcm-builder/runtime-benchmarks",
132133
"xcm-executor/runtime-benchmarks",
134+
"xcm-fee-payment-runtime-api/runtime-benchmarks",
133135
]
134136
try-runtime = [
135137
"cumulus-pallet-aura-ext/try-runtime",
@@ -236,6 +238,7 @@ std = [
236238
"westend-runtime-constants/std",
237239
"xcm-builder/std",
238240
"xcm-executor/std",
241+
"xcm-fee-payment-runtime-api/std",
239242
"xcm/std",
240243
]
241244

0 commit comments

Comments
 (0)