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

xcm-builder: PayOverXcm supports fallible convertors for asset kind and beneficiary conversion #1572

Merged
merged 2 commits into from
Sep 18, 2023
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
8 changes: 4 additions & 4 deletions polkadot/xcm/xcm-builder/src/location_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::universal_exports::ensure_is_remote;
use frame_support::traits::Get;
use parity_scale_codec::{Compact, Decode, Encode};
use sp_io::hashing::blake2_256;
use sp_runtime::traits::{AccountIdConversion, Convert, TrailingZeroInput};
use sp_runtime::traits::{AccountIdConversion, TrailingZeroInput, TryConvert};
use sp_std::{marker::PhantomData, prelude::*};
use xcm::latest::prelude::*;
use xcm_executor::traits::ConvertLocation;
Expand Down Expand Up @@ -322,10 +322,10 @@ impl<Network: Get<Option<NetworkId>>, AccountId: From<[u8; 32]> + Into<[u8; 32]>
/// network (provided by `Network`) and the `AccountId`'s `[u8; 32]` datum for the `id`.
pub struct AliasesIntoAccountId32<Network, AccountId>(PhantomData<(Network, AccountId)>);
impl<'a, Network: Get<Option<NetworkId>>, AccountId: Clone + Into<[u8; 32]> + Clone>
Convert<&'a AccountId, MultiLocation> for AliasesIntoAccountId32<Network, AccountId>
TryConvert<&'a AccountId, MultiLocation> for AliasesIntoAccountId32<Network, AccountId>
{
fn convert(who: &AccountId) -> MultiLocation {
AccountId32 { network: Network::get(), id: who.clone().into() }.into()
fn try_convert(who: &AccountId) -> Result<MultiLocation, &AccountId> {
Ok(AccountId32 { network: Network::get(), id: who.clone().into() }.into())
}
}

Expand Down
18 changes: 10 additions & 8 deletions polkadot/xcm/xcm-builder/src/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use frame_support::traits::{
tokens::{Pay, PaymentStatus},
Get,
};
use sp_runtime::traits::Convert;
use sp_runtime::traits::TryConvert;
use sp_std::{marker::PhantomData, vec};
use xcm::{opaque::lts::Weight, prelude::*};
use xcm_executor::traits::{QueryHandler, QueryResponseStatus};
Expand Down Expand Up @@ -71,8 +71,8 @@ impl<
Timeout: Get<Querier::BlockNumber>,
Beneficiary: Clone,
AssetKind,
AssetKindToLocatableAsset: Convert<AssetKind, LocatableAssetId>,
BeneficiaryRefToLocation: for<'a> Convert<&'a Beneficiary, MultiLocation>,
AssetKindToLocatableAsset: TryConvert<AssetKind, LocatableAssetId>,
BeneficiaryRefToLocation: for<'a> TryConvert<&'a Beneficiary, MultiLocation>,
> Pay
for PayOverXcm<
Interior,
Expand All @@ -96,12 +96,14 @@ impl<
asset_kind: Self::AssetKind,
amount: Self::Balance,
) -> Result<Self::Id, Self::Error> {
let locatable = AssetKindToLocatableAsset::convert(asset_kind);
let locatable = AssetKindToLocatableAsset::try_convert(asset_kind)
.map_err(|_| xcm::latest::Error::InvalidLocation)?;
let LocatableAssetId { asset_id, location: asset_location } = locatable;
let destination = Querier::UniversalLocation::get()
.invert_target(&asset_location)
.map_err(|()| Self::Error::LocationNotInvertible)?;
let beneficiary = BeneficiaryRefToLocation::convert(&who);
let beneficiary = BeneficiaryRefToLocation::try_convert(&who)
.map_err(|_| xcm::latest::Error::InvalidLocation)?;

let query_id = Querier::new_query(asset_location, Timeout::get(), Interior::get());

Expand Down Expand Up @@ -196,10 +198,10 @@ pub struct LocatableAssetId {
/// Adapter `struct` which implements a conversion from any `AssetKind` into a [`LocatableAssetId`]
/// value using a fixed `Location` for the `location` field.
pub struct FixedLocation<Location>(sp_std::marker::PhantomData<Location>);
impl<Location: Get<MultiLocation>, AssetKind: Into<AssetId>> Convert<AssetKind, LocatableAssetId>
impl<Location: Get<MultiLocation>, AssetKind: Into<AssetId>> TryConvert<AssetKind, LocatableAssetId>
for FixedLocation<Location>
{
fn convert(value: AssetKind) -> LocatableAssetId {
LocatableAssetId { asset_id: value.into(), location: Location::get() }
fn try_convert(value: AssetKind) -> Result<LocatableAssetId, AssetKind> {
Ok(LocatableAssetId { asset_id: value.into(), location: Location::get() })
}
}
6 changes: 3 additions & 3 deletions polkadot/xcm/xcm-builder/src/tests/pay/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub struct AssetKind {
}

pub struct LocatableAssetKindConverter;
impl sp_runtime::traits::Convert<AssetKind, LocatableAssetId> for LocatableAssetKindConverter {
fn convert(value: AssetKind) -> LocatableAssetId {
LocatableAssetId { asset_id: value.asset_id, location: value.destination }
impl sp_runtime::traits::TryConvert<AssetKind, LocatableAssetId> for LocatableAssetKindConverter {
fn try_convert(value: AssetKind) -> Result<LocatableAssetId, AssetKind> {
Ok(LocatableAssetId { asset_id: value.asset_id, location: value.destination })
}
}

Expand Down