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 XCM Runtime APIs #414

Merged
merged 1 commit into from
Oct 14, 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
3 changes: 3 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 @@ -187,6 +187,7 @@ xcm-builder = { version = "14.0.0", package = 'staging-xcm-builder', default-fea
xcm-executor = { version = "14.0.0", package = 'staging-xcm-executor', default-features = false }
polkadot-runtime-common = { version = "14.0.0", default-features = false }
polkadot-primitives = { version = "14.0.0", default-features = false }
xcm-fee-payment-runtime-api = { version = "0.4.0", default-features = false }

# Cumulus (with default disabled)
cumulus-pallet-aura-ext = { version = "0.14.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pallet-aura.workspace = true
pallet-session.workspace = true
pallet-proxy-bonding.workspace = true
pallet-skip-feeless-payment.workspace = true
xcm-fee-payment-runtime-api.workspace = true

# Runtimes
polkadot-runtime.workspace = true
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod governance;
mod oracle;
mod otm_edge_cases;
mod reserve_backed_transfers;
mod runtime_apis;
mod transaction_payment;
mod vest;
mod xcm_config;
75 changes: 75 additions & 0 deletions integration-tests/src/tests/runtime_apis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use crate::{constants::*, *};

mod xcm_payment_api {
use super::*;
use sp_arithmetic::FixedU128;
use xcm::v4::Junctions::X3;
use xcm_fee_payment_runtime_api::fees::runtime_decl_for_xcm_payment_api::XcmPaymentApiV1;

#[test]
fn query_acceptable_payment_assets() {
PolimecNet::execute_with(|| {
let accepted_payment_assets = PolimecRuntime::query_acceptable_payment_assets(4u32).unwrap();
assert_eq!(
accepted_payment_assets,
vec![
VersionedAssetId::V4(AssetId(Location { parents: 0, interior: Here },),),
VersionedAssetId::V4(AssetId(Location { parents: 1, interior: Here },),),
VersionedAssetId::V4(AssetId(Location {
parents: 1,
interior: X3([Parachain(1000,), PalletInstance(50,), GeneralIndex(1984,),].into(),),
},),),
VersionedAssetId::V4(AssetId(Location {
parents: 1,
interior: X3([Parachain(1000,), PalletInstance(50,), GeneralIndex(1337,),].into(),),
},),),
]
);
});
}

#[test]
fn query_weight_to_asset_fee() {
polimec::set_prices(
PricesBuilder::new()
.usdt(FixedU128::from_float(1.0f64))
.plmc(FixedU128::from_float(0.5f64))
.dot(FixedU128::from_float(10.0f64))
.build(),
);

let compute_weight = Weight::from_parts(100_000_000, 20_000);

// Native Asset
PolimecNet::execute_with(|| {
let plmc_fee = PolimecRuntime::query_weight_to_asset_fee(
compute_weight,
VersionedAssetId::V4(AssetId(Location { parents: 0, interior: Here })),
)
.unwrap();
dbg!(plmc_fee);

let dot_fee = PolimecRuntime::query_weight_to_asset_fee(
compute_weight,
VersionedAssetId::V4(AssetId(Location { parents: 1, interior: Here })),
)
.unwrap();
dbg!(dot_fee);

let usdt_fee = PolimecRuntime::query_weight_to_asset_fee(
compute_weight,
VersionedAssetId::V4(AssetId(Location {
parents: 1,
interior: X3([Parachain(1000), PalletInstance(50), GeneralIndex(1984)].into()),
})),
)
.unwrap();
dbg!(usdt_fee);

// PLMC and dot have the same decimals, so a simple conversion is enough
assert_eq!(dot_fee, plmc_fee / 20);
// USDT has 6 decimals, so we need to divide by 10^(10-6)= 10_000
assert_eq!(usdt_fee, plmc_fee / 2 / 10_000);
});
}
}
1 change: 1 addition & 0 deletions polimec-common/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sp-std.workspace = true
sp-runtime.workspace = true
itertools.workspace = true
xcm.workspace = true
xcm-builder.workspace = true


[features]
Expand Down
6 changes: 6 additions & 0 deletions polimec-common/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use sp_runtime::{
};
use sp_std::prelude::*;
pub use xcm::v4::{opaque::Xcm, Assets, Location, QueryId, SendError, SendResult, SendXcm, XcmHash};
use xcm::{VersionedLocation, VersionedXcm};

pub mod credentials;

Expand Down Expand Up @@ -239,6 +240,11 @@ impl SendXcm for DummyXcmSender {
Ok([0u8; 32])
}
}
impl xcm_builder::InspectMessageQueues for DummyXcmSender {
fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)> {
vec![]
}
}

pub trait ProvideAssetPrice {
type AssetId;
Expand Down
1 change: 1 addition & 0 deletions runtimes/polimec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ polkadot-runtime-common.workspace = true
xcm.workspace = true
xcm-builder.workspace = true
xcm-executor.workspace = true
xcm-fee-payment-runtime-api.workspace = true

# Cumulus
cumulus-pallet-aura-ext.workspace = true
Expand Down
60 changes: 58 additions & 2 deletions runtimes/polimec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,23 @@ use sp_runtime::{
};
use sp_std::{cmp::Ordering, prelude::*};
use sp_version::RuntimeVersion;
use xcm::{IntoVersion, VersionedAssets, VersionedLocation, VersionedXcm};

// XCM Imports
use xcm::v3::{
Junction::{GeneralIndex, PalletInstance, Parachain},
Junctions::{Here, X3},
MultiLocation,
};
use xcm_config::{PriceForSiblingParachainDelivery, XcmOriginToTransactDispatchOrigin};

#[cfg(not(feature = "runtime-benchmarks"))]
use xcm_config::XcmConfig;

use xcm_config::{PriceForSiblingParachainDelivery, XcmOriginToTransactDispatchOrigin};
use xcm_fee_payment_runtime_api::{
dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects},
fees::Error as XcmPaymentApiError,
};

// Polimec Shared Imports
pub use pallet_parachain_staking;
pub use shared_configuration::{
Expand All @@ -96,6 +101,7 @@ use sp_version::NativeVersion;

#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
use xcm::VersionedAssetId;

#[cfg(feature = "runtime-benchmarks")]
mod benchmark_helpers;
Expand Down Expand Up @@ -1084,6 +1090,7 @@ impl pallet_funding::Config for Runtime {
}

use polimec_common::{PLMC_DECIMALS, PLMC_FOREIGN_ID, USD_DECIMALS};

parameter_types! {
// Fee is defined as 1.5% of the usd_amount. Since fee is applied to the plmc amount, and that is always 5 times
// less than the usd_amount (multiplier of 5), we multiply the 1.5 by 5 to get 7.5%
Expand Down Expand Up @@ -1155,6 +1162,7 @@ impl Convert<MultiLocation, AssetId> for ConvertMultilocationToAssetId {
MultiLocation { parents: 1, interior: Here } => 10,
MultiLocation { parents: 1, interior: X3(Parachain(1000), PalletInstance(50), GeneralIndex(1337)) } => 1337,
MultiLocation { parents: 1, interior: X3(Parachain(1000), PalletInstance(50), GeneralIndex(1984)) } => 1984,
// asset 0 should be invalid.
_ => 0,
}
}
Expand Down Expand Up @@ -1673,6 +1681,54 @@ impl_runtime_apis! {
Default::default()
}
}

impl xcm_fee_payment_runtime_api::fees::XcmPaymentApi<Block> for Runtime {
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
let acceptable_assets = vec![
xcm_config::HereLocation::get().into(),
xcm_config::DotLocation::get().into(),
xcm_config::UsdtLocation::get().into(),
xcm_config::UsdcLocation::get().into()
];

PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets)
}

fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
match xcm::v3::AssetId::try_from(asset) {
Ok(xcm::v3::AssetId::Concrete(multilocation)) if multilocation == MultiLocation::here() => {
// for native token
Ok(TransactionPayment::weight_to_fee(weight))
},
Ok(xcm::v3::AssetId::Concrete(multilocation)) => {
let native_fee = TransactionPayment::weight_to_fee(weight);
let converted_asset_id = ConvertMultilocationToAssetId::convert(multilocation);
PLMCToFundingAssetBalance::to_asset_balance(native_fee, converted_asset_id).map_err(|_| XcmPaymentApiError::AssetNotFound)
},
_ => {
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_fee_payment_runtime_api::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)
}
}
}

cumulus_pallet_parachain_system::register_validate_block! {
Expand Down
4 changes: 2 additions & 2 deletions runtimes/polimec/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ const DOT_PER_MB_PROOF: u128 = 0_2_000_000_000; // 0.0000001 DOT per Megabyte of
// USDT from Polkadot Asset Hub
const USDT_PER_SECOND_EXECUTION: u128 = 1_000_000; // 1 USDT per second of execution time
const USDT_PER_MB_PROOF: u128 = 1_000_000; // 1 USDT per Megabyte of proof size
const USDT_JUNCTION: &[Junction] = &[Parachain(1000), PalletInstance(50), GeneralIndex(1984)];
pub const USDT_JUNCTION: &[Junction] = &[Parachain(1000), PalletInstance(50), GeneralIndex(1984)];

// USDC from Polkadot Asset Hub
const USDC_PER_SECOND_EXECUTION: u128 = 1_000_000; // 1 USDC per second of execution time
const USDC_PER_MB_PROOF: u128 = 1_000_000; // 1 USDC per Megabyte of proof size
const USDC_JUNCTION: &[Junction] = &[Parachain(1000), PalletInstance(50), GeneralIndex(1337)];
pub const USDC_JUNCTION: &[Junction] = &[Parachain(1000), PalletInstance(50), GeneralIndex(1337)];

parameter_types! {
pub const RelayLocation: Location = Location::parent();
Expand Down
1 change: 1 addition & 0 deletions scripts/zombienet/polimec-paseo-local.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

[settings]
timeout = 1000
provider = "native"
Expand Down