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

Assets in pool with native can be used in query_weight_to_asset_fee #6080

Merged
merged 22 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
17b080b
fix: assets in pool with native can be used to query for fees
franciscoaguirre Oct 15, 2024
375908a
chore: refactor pools helper function to assets-common
franciscoaguirre Oct 18, 2024
aec5b5b
test(asset-hub-westend-integration-tests): XcmPaymentApi
franciscoaguirre Oct 18, 2024
dcbb796
chore: move test to macro and use in both asset hubs
franciscoaguirre Oct 18, 2024
30cf711
chore: remove comment
franciscoaguirre Oct 18, 2024
a4e80ac
doc: add prdoc
franciscoaguirre Oct 18, 2024
e4e1ca0
Merge branch 'master' into query-weight-to-asset-fee-multiple-assets
franciscoaguirre Oct 18, 2024
9121790
".git/.scripts/commands/fmt/fmt.sh"
Oct 18, 2024
9b1f15b
fix: used invalid character in prdoc
franciscoaguirre Oct 18, 2024
932becb
Merge branch 'master' into query-weight-to-asset-fee-multiple-assets
franciscoaguirre Oct 18, 2024
86b9da8
Merge branch 'master' into query-weight-to-asset-fee-multiple-assets
franciscoaguirre Oct 21, 2024
860856b
chore(assets-common): make get_assets_in_pool_with more generic
franciscoaguirre Oct 21, 2024
4b01538
Merge branch 'master' into query-weight-to-asset-fee-multiple-assets
franciscoaguirre Oct 21, 2024
66897ac
Merge branch 'master' into query-weight-to-asset-fee-multiple-assets
franciscoaguirre Oct 22, 2024
385137b
chore(assets-common): make get_assets_in_pool_with more generic
franciscoaguirre Oct 22, 2024
2ff5cf7
Merge branch 'master' into query-weight-to-asset-fee-multiple-assets
franciscoaguirre Oct 22, 2024
db4040b
chore(assets-common): return a vec instead of an iterator in get_asse…
franciscoaguirre Oct 22, 2024
88d2f2d
fix(asset-hub-rococo): update get_assets_in_pool_with
franciscoaguirre Oct 22, 2024
14eb027
".git/.scripts/commands/fmt/fmt.sh"
Oct 22, 2024
e650905
fix: fmt
franciscoaguirre Oct 22, 2024
82ada8a
chore: get_asset_in_pool_with returns asset id instead of location
franciscoaguirre Oct 22, 2024
010299d
chore: any() instead of find().is_some()
franciscoaguirre Oct 22, 2024
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
46 changes: 29 additions & 17 deletions cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,31 +1338,31 @@ impl_runtime_apis! {
// We accept the native token to pay fees.
let mut acceptable_assets = vec![AssetId(native_token.clone())];
// We also accept all assets in a pool with the native token.
acceptable_assets.extend(
pallet_asset_conversion::Pools::<Runtime>::iter_keys().filter_map(
|(asset_1, asset_2)| {
if asset_1 == native_token {
Some(asset_2.clone().into())
} else if asset_2 == native_token {
Some(asset_1.clone().into())
} else {
None
}
},
),
);
acceptable_assets.extend(get_assets_in_pool_with(native_token.clone()));
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::TokenLocation::get();
let fee_in_native = WeightToFee::weight_to_fee(&weight);
match asset.try_as::<AssetId>() {
Ok(asset_id) if asset_id.0 == xcm_config::TokenLocation::get() => {
Ok(asset_id) if asset_id.0 == native_asset => {
// for native token
Ok(WeightToFee::weight_to_fee(&weight))
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)
let assets_in_pool_with_native_token: Vec<_> = get_assets_in_pool_with(native_asset.clone()).collect();
if assets_in_pool_with_native_token.contains(&asset_id) {
pallet_asset_conversion::Pallet::<Runtime>::quote_price_tokens_for_exact_tokens(
asset_id.clone().0,
native_asset,
fee_in_native,
true, // We include the fee.
).ok_or(XcmPaymentApiError::AssetNotFound)
} else {
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:?}!");
Expand Down Expand Up @@ -1799,6 +1799,18 @@ impl_runtime_apis! {
}
}

fn get_assets_in_pool_with(asset: xcm::v4::Location) -> impl Iterator<Item = AssetId> {
franciscoaguirre marked this conversation as resolved.
Show resolved Hide resolved
franciscoaguirre marked this conversation as resolved.
Show resolved Hide resolved
pallet_asset_conversion::Pools::<Runtime>::iter_keys().filter_map(move |(asset_1, asset_2)| {
if asset_1 == asset {
Some(asset_2.clone().into())
} else if asset_2 == asset {
Some(asset_1.clone().into())
} else {
None
}
})
}

cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
Expand Down
48 changes: 30 additions & 18 deletions cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,31 +1371,31 @@ impl_runtime_apis! {
// We accept the native token to pay fees.
let mut acceptable_assets = vec![AssetId(native_token.clone())];
// We also accept all assets in a pool with the native token.
acceptable_assets.extend(
pallet_asset_conversion::Pools::<Runtime>::iter_keys().filter_map(
|(asset_1, asset_2)| {
if asset_1 == native_token {
Some(asset_2.clone().into())
} else if asset_2 == native_token {
Some(asset_1.clone().into())
} else {
None
}
},
),
);
acceptable_assets.extend(get_assets_in_pool_with(native_token.clone()));
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::WestendLocation::get();
let fee_in_native = WeightToFee::weight_to_fee(&weight);
match asset.try_as::<AssetId>() {
Ok(asset_id) if asset_id.0 == xcm_config::WestendLocation::get() => {
// for native token
Ok(WeightToFee::weight_to_fee(&weight))
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)
let assets_in_pool_with_native_token: Vec<_> = get_assets_in_pool_with(native_asset.clone()).collect();
if assets_in_pool_with_native_token.contains(&asset_id) {
pallet_asset_conversion::Pallet::<Runtime>::quote_price_tokens_for_exact_tokens(
asset_id.clone().0,
native_asset,
fee_in_native,
true, // We include the fee.
).ok_or(XcmPaymentApiError::AssetNotFound)
} else {
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:?}!");
Expand Down Expand Up @@ -1895,6 +1895,18 @@ impl_runtime_apis! {
}
}

fn get_assets_in_pool_with(asset: xcm::v4::Location) -> impl Iterator<Item = AssetId> {
pallet_asset_conversion::Pools::<Runtime>::iter_keys().filter_map(move |(asset_1, asset_2)| {
if asset_1 == asset {
Some(asset_2.clone().into())
} else if asset_2 == asset {
Some(asset_1.clone().into())
} else {
None
}
})
}

cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
Expand Down
Loading