Skip to content

Commit

Permalink
feat(mainnet): add XCM runtime APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwht committed Jan 12, 2025
1 parent b04efa2 commit 4d18a39
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ rococo-runtime-constants = { git = "https://github.com/r0gue-io/polkadot-sdk", b
xcm = { git = "https://github.com/r0gue-io/polkadot-sdk", branch = "stable2412", package = "staging-xcm", default-features = false }
xcm-builder = { git = "https://github.com/r0gue-io/polkadot-sdk", branch = "stable2412", package = "staging-xcm-builder", default-features = false }
xcm-executor = { git = "https://github.com/r0gue-io/polkadot-sdk", branch = "stable2412", package = "staging-xcm-executor", default-features = false }
xcm-runtime-apis = { git = "https://github.com/r0gue-io/polkadot-sdk", branch = "stable2412", default-features = false }

# Cumulus
asset-test-utils = { git = "https://github.com/r0gue-io/polkadot-sdk", branch = "stable2412", default-features = false }
Expand Down
4 changes: 4 additions & 0 deletions runtime/mainnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ substrate-wasm-builder.workspace = true
[dependencies]
codec.workspace = true
docify.workspace = true
log.workspace = true
scale-info.workspace = true
smallvec.workspace = true

Expand Down Expand Up @@ -66,6 +67,7 @@ polkadot-runtime-common.workspace = true
xcm.workspace = true
xcm-builder.workspace = true
xcm-executor.workspace = true
xcm-runtime-apis.workspace = true

# Cumulus
cumulus-pallet-aura-ext.workspace = true
Expand Down Expand Up @@ -144,6 +146,7 @@ std = [
"sp-version/std",
"xcm-builder/std",
"xcm-executor/std",
"xcm-runtime-apis/std",
"xcm/std",
]

Expand Down Expand Up @@ -176,6 +179,7 @@ runtime-benchmarks = [
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"xcm-executor/runtime-benchmarks",
"xcm-runtime-apis/runtime-benchmarks",
]

try-runtime = [
Expand Down
85 changes: 81 additions & 4 deletions runtime/mainnet/src/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::vec::Vec;

use frame_support::{
genesis_builder_helper::{build_state, get_preset},
weights::Weight,
weights::{Weight, WeightToFee as _},
};
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
Expand All @@ -13,12 +13,22 @@ use sp_runtime::{
ApplyExtrinsicResult,
};
use sp_version::RuntimeVersion;
use xcm::{
latest::prelude::AssetId, VersionedAsset, VersionedAssetId, VersionedAssets, VersionedLocation,
VersionedXcm,
};
use xcm_runtime_apis::{
dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects},
fees::Error as XcmPaymentApiError,
trusted_query::XcmTrustedQueryResult,
};

// Local module imports
use super::{
AccountId, Balance, Block, Executive,
ExtrinsicInclusionMode, InherentDataExt, Nonce, ParachainSystem, Runtime,
RuntimeCall, RuntimeGenesisConfig, SessionKeys, System, TransactionPayment, VERSION,
config::xcm as xcm_config, fee::WeightToFee, AccountId, Balance, Block, Executive,
ExtrinsicInclusionMode, InherentDataExt, Nonce, OriginCaller, ParachainSystem, PolkadotXcm,
Runtime, RuntimeCall, RuntimeEvent, RuntimeGenesisConfig, SessionKeys, System,
TransactionPayment, VERSION,
};

impl_runtime_apis! {
Expand Down Expand Up @@ -257,4 +267,71 @@ impl_runtime_apis! {
Default::default()
}
}

impl xcm_runtime_apis::fees::XcmPaymentApi<Block> for Runtime {
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
let native_token = xcm_config::RelayLocation::get();
let acceptable_assets = Vec::from([AssetId(native_token)]);
PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets)
}

fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
let native_asset = xcm_config::RelayLocation::get();
let fee_in_native = WeightToFee::weight_to_fee(&weight);
match asset.try_as::<AssetId>() {
Ok(asset_id) if asset_id.0 == native_asset => {
// for native asset
Ok(fee_in_native)
},
Ok(asset_id) => {
log::trace!(target: "xcm::xcm_runtime_apis", "query_weight_to_asset_fee - unhandled asset_id: {asset_id:?}!");
Err(XcmPaymentApiError::AssetNotFound)
},
Err(_) => {
log::trace!(target: "xcm::xcm_runtime_apis", "query_weight_to_asset_fee - failed to convert asset: {asset:?}!");
Err(XcmPaymentApiError::VersionedConversionFailed)
}
}
}

fn query_xcm_weight(message: VersionedXcm<()>) -> Result<Weight, XcmPaymentApiError> {
PolkadotXcm::query_xcm_weight(message)
}

fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result<VersionedAssets, XcmPaymentApiError> {
PolkadotXcm::query_delivery_fees(destination, message)
}
}

impl xcm_runtime_apis::dry_run::DryRunApi<Block, RuntimeCall, RuntimeEvent, OriginCaller> for Runtime {
fn dry_run_call(origin: OriginCaller, call: RuntimeCall) -> Result<CallDryRunEffects<RuntimeEvent>, XcmDryRunApiError> {
PolkadotXcm::dry_run_call::<Runtime, xcm_config::XcmRouter, OriginCaller, RuntimeCall>(origin, call)
}

fn dry_run_xcm(origin_location: VersionedLocation, xcm: VersionedXcm<RuntimeCall>) -> Result<XcmDryRunEffects<RuntimeEvent>, XcmDryRunApiError> {
PolkadotXcm::dry_run_xcm::<Runtime, xcm_config::XcmRouter, RuntimeCall, xcm_config::XcmConfig>(origin_location, xcm)
}
}

impl xcm_runtime_apis::conversions::LocationToAccountApi<Block, AccountId> for Runtime {
fn convert_location(location: VersionedLocation) -> Result<
AccountId,
xcm_runtime_apis::conversions::Error
> {
xcm_runtime_apis::conversions::LocationToAccountHelper::<
AccountId,
xcm_config::LocationToAccountId,
>::convert_location(location)
}
}

impl xcm_runtime_apis::trusted_query::TrustedQueryApi<Block> for Runtime {
fn is_trusted_reserve(asset: VersionedAsset, location: VersionedLocation) -> XcmTrustedQueryResult {
PolkadotXcm::is_trusted_reserve(asset, location)
}

fn is_trusted_teleporter(asset: VersionedAsset, location: VersionedLocation) -> XcmTrustedQueryResult {
PolkadotXcm::is_trusted_teleporter(asset, location)
}
}
}

0 comments on commit 4d18a39

Please sign in to comment.