Skip to content

Commit

Permalink
Use latest VersionedXcm instead of V4 for `xcm-fee-payment-runtime-…
Browse files Browse the repository at this point in the history
…api` tests/mock
  • Loading branch information
bkontur committed May 20, 2024
1 parent 59a2c93 commit 143f5a7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 38 deletions.
53 changes: 30 additions & 23 deletions polkadot/xcm/xcm-fee-payment-runtime-api/tests/fee_estimation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ fn fee_estimation_for_teleport() {
let runtime_api = client.runtime_api();
let extrinsic = TestXt::new(
RuntimeCall::XcmPallet(pallet_xcm::Call::transfer_assets {
dest: Box::new(VersionedLocation::V4((Parent, Parachain(1000)).into())),
beneficiary: Box::new(VersionedLocation::V4(
AccountId32 { id: [0u8; 32], network: None }.into(),
)),
assets: Box::new(VersionedAssets::V4(
vec![(Here, 100u128).into(), (Parent, 20u128).into()].into(),
)),
dest: Box::new(VersionedLocation::from((Parent, Parachain(1000)))),
beneficiary: Box::new(VersionedLocation::from(AccountId32 {
id: [0u8; 32],
network: None,
})),
assets: Box::new(VersionedAssets::from(vec![
(Here, 100u128).into(),
(Parent, 20u128).into(),
])),
fee_asset_item: 1, // Fees are paid with the RelayToken
weight_limit: Unlimited,
}),
Expand Down Expand Up @@ -168,7 +170,7 @@ fn fee_estimation_for_teleport() {
.query_delivery_fees(H256::zero(), destination.clone(), remote_message.clone())
.unwrap()
.unwrap();
assert_eq!(delivery_fees, VersionedAssets::V4((Here, 20u128).into()));
assert_eq!(delivery_fees, VersionedAssets::from((Here, 20u128)));

// This would have to be the runtime API of the destination,
// which we have the location for.
Expand All @@ -182,7 +184,7 @@ fn fee_estimation_for_teleport() {
.query_weight_to_asset_fee(
H256::zero(),
remote_execution_weight,
VersionedAssetId::V4(HereLocation::get().into()),
VersionedAssetId::from(AssetId(HereLocation::get())),
)
.unwrap()
.unwrap();
Expand Down Expand Up @@ -216,11 +218,12 @@ fn dry_run_reserve_asset_transfer() {
let runtime_api = client.runtime_api();
let extrinsic = TestXt::new(
RuntimeCall::XcmPallet(pallet_xcm::Call::transfer_assets {
dest: Box::new(VersionedLocation::V4((Parent, Parachain(1000)).into())),
beneficiary: Box::new(VersionedLocation::V4(
AccountId32 { id: [0u8; 32], network: None }.into(),
)),
assets: Box::new(VersionedAssets::V4((Parent, 100u128).into())),
dest: Box::new(VersionedLocation::from((Parent, Parachain(1000)))),
beneficiary: Box::new(VersionedLocation::from(AccountId32 {
id: [0u8; 32],
network: None,
})),
assets: Box::new(VersionedAssets::from((Parent, 100u128))),
fee_asset_item: 0,
weight_limit: Unlimited,
}),
Expand All @@ -231,7 +234,7 @@ fn dry_run_reserve_asset_transfer() {

assert_eq!(
dry_run_effects.local_xcm,
Some(VersionedXcm::V4(
Some(VersionedXcm::from(
Xcm::builder_unsafe()
.withdraw_asset((Parent, 100u128))
.burn_asset((Parent, 100u128))
Expand All @@ -251,8 +254,8 @@ fn dry_run_reserve_asset_transfer() {
assert_eq!(
dry_run_effects.forwarded_xcms,
vec![(
VersionedLocation::V4(send_destination.clone()),
vec![VersionedXcm::V4(send_message.clone())],
VersionedLocation::from(send_destination.clone()),
vec![VersionedXcm::from(send_message.clone())],
),],
);

Expand Down Expand Up @@ -310,11 +313,15 @@ fn dry_run_xcm() {
let client = TestClient;
let runtime_api = client.runtime_api();
let xcm_weight = runtime_api
.query_xcm_weight(H256::zero(), VersionedXcm::V4(xcm_to_weigh.clone().into()))
.query_xcm_weight(H256::zero(), VersionedXcm::from(xcm_to_weigh.clone().into()))
.unwrap()
.unwrap();
let execution_fees = runtime_api
.query_weight_to_asset_fee(H256::zero(), xcm_weight, VersionedAssetId::V4(Here.into()))
.query_weight_to_asset_fee(
H256::zero(),
xcm_weight,
VersionedAssetId::from(AssetId(Here.into())),
)
.unwrap()
.unwrap();
let xcm = Xcm::<RuntimeCall>::builder_unsafe()
Expand All @@ -331,16 +338,16 @@ fn dry_run_xcm() {
let dry_run_effects = runtime_api
.dry_run_xcm(
H256::zero(),
VersionedLocation::V4(AccountIndex64 { index: 1, network: None }.into()),
VersionedXcm::V4(xcm),
VersionedLocation::from([AccountIndex64 { index: 1, network: None }]),
VersionedXcm::from(xcm),
)
.unwrap()
.unwrap();
assert_eq!(
dry_run_effects.forwarded_xcms,
vec![(
VersionedLocation::V4((Parent, Parachain(2100)).into()),
vec![VersionedXcm::V4(
VersionedLocation::from((Parent, Parachain(2100))),
vec![VersionedXcm::from(
Xcm::<()>::builder_unsafe()
.reserve_asset_deposited((
(Parent, Parachain(2000)),
Expand Down
44 changes: 29 additions & 15 deletions polkadot/xcm/xcm-fee-payment-runtime-api/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,23 +429,37 @@ impl sp_api::ProvideRuntimeApi<Block> for TestClient {
sp_api::mock_impl_runtime_apis! {
impl XcmPaymentApi<Block> for RuntimeApi {
fn query_acceptable_payment_assets(xcm_version: XcmVersion) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
if xcm_version != 4 { return Err(XcmPaymentApiError::UnhandledXcmVersion) };
Ok(vec![VersionedAssetId::V4(HereLocation::get().into())])
Ok(vec![
VersionedAssetId::from(AssetId(HereLocation::get()))
.into_version(xcm_version)
.or_else(|_| Err(XcmPaymentApiError::VersionedConversionFailed))?
])
}

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

fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
let local_asset = VersionedAssetId::V4(HereLocation::get().into());
let asset = asset
.into_version(4)
.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?;

if asset != local_asset { return Err(XcmPaymentApiError::AssetNotFound); }

Ok(WeightToFee::weight_to_fee(&weight))
match asset.try_as::<AssetId>() {
Ok(asset_id) if asset_id.0 == HereLocation::get() => {
Ok(WeightToFee::weight_to_fee(&weight))
},
Ok(asset_id) => {
log::trace!(
target: "xcm::XcmPaymentApi::query_weight_to_asset_fee",
"query_weight_to_asset_fee - unhandled asset_id: {asset_id:?}!"
);
Err(XcmPaymentApiError::AssetNotFound)
},
Err(_) => {
log::trace!(
target: "xcm::XcmPaymentApi::query_weight_to_asset_fee",
"query_weight_to_asset_fee - failed to convert asset: {asset:?}!"
);
Err(XcmPaymentApiError::VersionedConversionFailed)
}
}
}

fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result<VersionedAssets, XcmPaymentApiError> {
Expand All @@ -471,12 +485,12 @@ sp_api::mock_impl_runtime_apis! {
let forwarded_xcms = sent_xcm()
.into_iter()
.map(|(location, message)| (
VersionedLocation::V4(location),
vec![VersionedXcm::V4(message)],
VersionedLocation::from(location),
vec![VersionedXcm::from(message)],
)).collect();
let events: Vec<RuntimeEvent> = System::events().iter().map(|record| record.event.clone()).collect();
Ok(ExtrinsicDryRunEffects {
local_xcm: local_xcm.map(VersionedXcm::<()>::V4),
local_xcm: local_xcm.map(VersionedXcm::<()>::from),
forwarded_xcms,
emitted_events: events,
execution_result: result,
Expand Down Expand Up @@ -511,8 +525,8 @@ sp_api::mock_impl_runtime_apis! {
let forwarded_xcms = sent_xcm()
.into_iter()
.map(|(location, message)| (
VersionedLocation::V4(location),
vec![VersionedXcm::V4(message)],
VersionedLocation::from(location),
vec![VersionedXcm::from(message)],
)).collect();
let events: Vec<RuntimeEvent> = System::events().iter().map(|record| record.event.clone()).collect();
Ok(XcmDryRunEffects {
Expand Down

0 comments on commit 143f5a7

Please sign in to comment.