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

Fix XcmPaymentApi::query_weight_to_asset_fee version conversion #6459

Merged
merged 17 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,8 @@ impl_runtime_apis! {
fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
let native_asset = xcm_config::TokenLocation::get();
let fee_in_native = WeightToFee::weight_to_fee(&weight);
match asset.try_as::<AssetId>() {
let latest_asset_id: Result<AssetId, ()> = asset.clone().try_into();
match latest_asset_id {
Ok(asset_id) if asset_id.0 == native_asset => {
// for native token
Ok(fee_in_native)
Expand Down
25 changes: 25 additions & 0 deletions cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,3 +1471,28 @@ fn location_conversion_works() {
assert_eq!(got, expected, "{}", tc.description);
}
}

#[test]
fn calling_payment_api_with_a_lower_version_works() {
use xcm::prelude::*;
use xcm_runtime_apis::fees::runtime_decl_for_xcm_payment_api::XcmPaymentApiV1;

ExtBuilder::<Runtime>::default().build().execute_with(|| {
let transfer_amount = 100u128;
let xcm_to_weigh = Xcm::<RuntimeCall>::builder_unsafe()
.withdraw_asset((Here, transfer_amount))
.buy_execution((Here, transfer_amount), Unlimited)
.deposit_asset(AllCounted(1), [1u8; 32])
.build();
let versioned_xcm_to_weigh = VersionedXcm::from(xcm_to_weigh.clone().into());
let lower_version_xcm_to_weigh =
versioned_xcm_to_weigh.into_version(XCM_VERSION - 1).unwrap();
let xcm_weight = Runtime::query_xcm_weight(lower_version_xcm_to_weigh);
assert!(xcm_weight.is_ok());
let native_token = VersionedAssetId::from(AssetId(Parent.into()));
let lower_version_native_token = native_token.into_version(XCM_VERSION - 1).unwrap();
let execution_fees =
Runtime::query_weight_to_asset_fee(xcm_weight.unwrap(), lower_version_native_token);
assert!(execution_fees.is_ok());
});
}
franciscoaguirre marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,8 @@ impl_runtime_apis! {
fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
let native_asset = xcm_config::WestendLocation::get();
let fee_in_native = WeightToFee::weight_to_fee(&weight);
match asset.try_as::<AssetId>() {
let latest_asset_id: Result<AssetId, ()> = asset.clone().try_into();
match latest_asset_id {
Ok(asset_id) if asset_id.0 == native_asset => {
// for native asset
Ok(fee_in_native)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1446,3 +1446,28 @@ fn location_conversion_works() {
assert_eq!(got, expected, "{}", tc.description);
}
}

#[test]
fn calling_payment_api_with_a_lower_version_works() {
use xcm::prelude::*;
use xcm_runtime_apis::fees::runtime_decl_for_xcm_payment_api::XcmPaymentApiV1;

ExtBuilder::<Runtime>::default().build().execute_with(|| {
let transfer_amount = 100u128;
let xcm_to_weigh = Xcm::<RuntimeCall>::builder_unsafe()
.withdraw_asset((Here, transfer_amount))
.buy_execution((Here, transfer_amount), Unlimited)
.deposit_asset(AllCounted(1), [1u8; 32])
.build();
let versioned_xcm_to_weigh = VersionedXcm::from(xcm_to_weigh.clone().into());
let lower_version_xcm_to_weigh =
versioned_xcm_to_weigh.into_version(XCM_VERSION - 1).unwrap();
let xcm_weight = Runtime::query_xcm_weight(lower_version_xcm_to_weigh);
assert!(xcm_weight.is_ok());
let native_token = VersionedAssetId::from(AssetId(Parent.into()));
let lower_version_native_token = native_token.into_version(XCM_VERSION - 1).unwrap();
let execution_fees =
Runtime::query_weight_to_asset_fee(xcm_weight.unwrap(), lower_version_native_token);
assert!(execution_fees.is_ok());
});
}
23 changes: 23 additions & 0 deletions polkadot/xcm/xcm-runtime-apis/tests/fee_estimation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,26 @@ fn dry_run_xcm() {
);
});
}

#[test]
fn calling_payment_api_with_a_lower_version_works() {
let transfer_amount = 100u128;
let xcm_to_weigh = Xcm::<RuntimeCall>::builder_unsafe()
.withdraw_asset((Here, transfer_amount))
.buy_execution((Here, transfer_amount), Unlimited)
.deposit_asset(AllCounted(1), [1u8; 32])
.build();
let versioned_xcm_to_weigh = VersionedXcm::from(xcm_to_weigh.clone().into());
let lower_version_xcm_to_weigh = versioned_xcm_to_weigh.into_version(XCM_VERSION - 1).unwrap();
let client = TestClient;
let runtime_api = client.runtime_api();
let xcm_weight =
runtime_api.query_xcm_weight(H256::zero(), lower_version_xcm_to_weigh).unwrap();
assert!(xcm_weight.is_ok());
let native_token = VersionedAssetId::from(AssetId(Here.into()));
let lower_version_native_token = native_token.into_version(XCM_VERSION - 1).unwrap();
let execution_fees = runtime_api
.query_weight_to_asset_fee(H256::zero(), xcm_weight.unwrap(), lower_version_native_token)
.unwrap();
assert!(execution_fees.is_ok());
}
3 changes: 2 additions & 1 deletion polkadot/xcm/xcm-runtime-apis/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ sp_api::mock_impl_runtime_apis! {
}

fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
match asset.try_as::<AssetId>() {
let latest_asset_id: Result<AssetId, ()> = asset.clone().try_into();
match latest_asset_id {
Ok(asset_id) if asset_id.0 == HereLocation::get() => {
Ok(WeightToFee::weight_to_fee(&weight))
},
Expand Down
20 changes: 20 additions & 0 deletions prdoc/pr_6459.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Fix version conversion in XcmPaymentApi::query_weight_to_asset_fee.

doc:
- audience: Runtime Dev
description: |
The `query_weight_to_asset_fee` function of the `XcmPaymentApi` was trying
to convert versions in the wrong way.
This resulted in all calls made with lower versions failing.
The version conversion is now done correctly and these same calls will now succeed.

crates:
- name: asset-hub-westend-runtime
bump: patch
- name: asset-hub-rococo-runtime
bump: patch
- name: xcm-runtime-apis
bump: patch
Loading