Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
dda9308
pallet-xcm: deprecate unlimited_* weight extrinsics
acatangiu Mar 13, 2024
1db1cae
pallet-xcm: remove extrinsics guessed weight and rely on runtime benc…
acatangiu Mar 13, 2024
f5e1dfb
pallet-xcm: add new extrinsic for asset transfers using explicit reserve
acatangiu Mar 13, 2024
547eeee
add more trace logging to XCM transport
acatangiu Mar 13, 2024
c7ee1af
minor style fix
acatangiu Mar 13, 2024
ed77fde
integration-tests: add scenario for parachain sending asset over brid…
acatangiu Mar 13, 2024
a7a44c9
make penpal UniversalLocation configurable
acatangiu Mar 14, 2024
ffa6250
penpal: charge XCM delivery fees for accurate real-world testing
acatangiu Mar 22, 2024
4afba95
integration-tests: fix tests for penpal charging delivery fees
acatangiu Mar 22, 2024
210cdd2
Merge branch 'master' of github.com:paritytech/polkadot-sdk into tran…
acatangiu Mar 23, 2024
c918bea
fix test
acatangiu Mar 23, 2024
3db344b
add prdoc
acatangiu Mar 25, 2024
10d2c40
fix clippy
acatangiu Mar 25, 2024
5b48155
Merge branch 'master' of github.com:paritytech/polkadot-sdk into tran…
acatangiu Mar 27, 2024
dbe453d
add test for westend->rococo direction
acatangiu Mar 27, 2024
42e4041
nit
acatangiu Mar 27, 2024
e794df3
Merge branch 'master' of github.com:paritytech/polkadot-sdk into tran…
acatangiu Apr 1, 2024
81dc911
pallet-xcm: specify explicit transfer type
acatangiu Apr 3, 2024
e1dbf7a
add more tests
acatangiu Apr 3, 2024
ef83eab
fix prdoc
acatangiu Apr 4, 2024
a2b8708
Merge branch 'master' of github.com:paritytech/polkadot-sdk into tran…
acatangiu Apr 4, 2024
4250692
refactor tests
acatangiu Apr 4, 2024
888d403
even more tests
acatangiu Apr 4, 2024
1c86e6e
another test
acatangiu Apr 4, 2024
b82d0b9
add teleport test as well
acatangiu Apr 4, 2024
f4e88da
rename xt and update docs
acatangiu Apr 4, 2024
4f2381c
fix weights
acatangiu Apr 4, 2024
36af01f
add tests to westend
acatangiu Apr 4, 2024
bd9233a
Merge branch 'master' of github.com:paritytech/polkadot-sdk into tran…
acatangiu Apr 4, 2024
9c7bd39
remove leftover comment
acatangiu Apr 4, 2024
adf5128
Apply suggestions from code review
acatangiu Apr 11, 2024
1347f0b
fix logs
acatangiu Apr 11, 2024
e3acff2
fix api
acatangiu Apr 11, 2024
4a44194
Merge branch 'master' into transfer-using-explicit-reserve
acatangiu Apr 11, 2024
0d407eb
Update polkadot/xcm/pallet-xcm/src/lib.rs
acatangiu Apr 12, 2024
22093d0
".git/.scripts/commands/fmt/fmt.sh"
Apr 12, 2024
a673792
Merge branch 'master' into transfer-using-explicit-reserve
acatangiu Apr 12, 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
Prev Previous commit
Next Next commit
pallet-xcm: specify explicit transfer type
  • Loading branch information
acatangiu committed Apr 3, 2024
commit 81dc911e477fee0472d5a0c4aa62f3e64f5c096e
172 changes: 85 additions & 87 deletions polkadot/xcm/pallet-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,67 +1304,23 @@ pub mod pallet {
);

ensure!(assets.len() <= MAX_ASSETS_FOR_TRANSFER, Error::<T>::TooManyAssets);
let mut assets = assets.into_inner();
let assets = assets.into_inner();
let fee_asset_item = fee_asset_item as usize;
let fees = assets.get(fee_asset_item as usize).ok_or(Error::<T>::Empty)?.clone();
// Find transfer types for fee and non-fee assets.
let (fees_transfer_type, assets_transfer_type) =
Self::find_fee_and_assets_transfer_types(&assets, fee_asset_item, &dest)?;

// local and remote XCM programs to potentially handle fees separately
let fees = if fees_transfer_type == assets_transfer_type {
// no need for custom fees instructions, fees are batched with assets
FeesHandling::Batched { fees }
} else {
// Disallow _remote reserves_ unless assets & fees have same remote reserve (covered
// by branch above). The reason for this is that we'd need to send XCMs to separate
// chains with no guarantee of delivery order on final destination; therefore we
// cannot guarantee to have fees in place on final destination chain to pay for
// assets transfer.
ensure!(
!matches!(assets_transfer_type, TransferType::RemoteReserve(_)),
Error::<T>::InvalidAssetUnsupportedReserve
);
let weight_limit = weight_limit.clone();
// remove `fees` from `assets` and build separate fees transfer instructions to be
// added to assets transfers XCM programs
let fees = assets.remove(fee_asset_item);
let (local_xcm, remote_xcm) = match fees_transfer_type {
TransferType::LocalReserve => Self::local_reserve_fees_instructions(
origin.clone(),
dest.clone(),
fees,
weight_limit,
)?,
TransferType::DestinationReserve =>
Self::destination_reserve_fees_instructions(
origin.clone(),
dest.clone(),
fees,
weight_limit,
)?,
TransferType::Teleport => Self::teleport_fees_instructions(
origin.clone(),
dest.clone(),
fees,
weight_limit,
)?,
TransferType::RemoteReserve(_) =>
return Err(Error::<T>::InvalidAssetUnsupportedReserve.into()),
};
FeesHandling::Separate { local_xcm, remote_xcm }
};

let (local_xcm, remote_xcm) = Self::build_xcm_transfer_type(
origin.clone(),
dest.clone(),
Self::do_transfer_assets(
origin,
dest,
beneficiary,
assets,
assets_transfer_type,
fees,
fees_transfer_type,
weight_limit,
)?;
Self::execute_xcm_transfer(origin, dest, local_xcm, remote_xcm)
)
}

/// Claims assets trapped on this pallet because of leftover assets during XCM execution.
Expand Down Expand Up @@ -1491,8 +1447,9 @@ pub mod pallet {
dest: Box<VersionedLocation>,
beneficiary: Box<VersionedLocation>,
assets: Box<VersionedAssets>,
assets_transfer_type: Box<TransferType>,
fees: Box<VersionedAsset>,
using_reserve: Box<VersionedLocation>,
fees_transfer_type: Box<TransferType>,
weight_limit: WeightLimit,
) -> DispatchResult {
let origin_location = T::ExecuteXcmOrigin::ensure_origin(origin)?;
Expand All @@ -1501,18 +1458,14 @@ pub mod pallet {
(*beneficiary).try_into().map_err(|()| Error::<T>::BadVersion)?;
let assets: Assets = (*assets).try_into().map_err(|()| Error::<T>::BadVersion)?;
let fees: Asset = (*fees).try_into().map_err(|()| Error::<T>::BadVersion)?;
let reserve: Location =
(*using_reserve).try_into().map_err(|()| Error::<T>::BadVersion)?;
log::debug!(
target: "xcm::pallet_xcm::transfer_assets_using_reserve",
"origin {:?}, dest {:?}, beneficiary {:?}, assets {:?}, fees {:?}, using reserve {:?}",
origin_location, dest, beneficiary, assets, fees, reserve,
"origin {:?}, dest {:?}, beneficiary {:?}, assets {:?} through {:?}, fees {:?} through {:?}",
origin_location, dest, beneficiary, assets, assets_transfer_type, fees, fees_transfer_type,
);

let assets = assets.into_inner();
ensure!(assets.len() <= MAX_ASSETS_FOR_TRANSFER, Error::<T>::TooManyAssets);
let value = (origin_location, assets.into_inner());
ensure!(T::XcmReserveTransferFilter::contains(&value), Error::<T>::Filtered);
let (origin, assets) = value;

let fee_asset_index =
assets.iter().position(|a| a.id == fees.id).ok_or(Error::<T>::FeesNotMet)?;
Expand All @@ -1522,38 +1475,16 @@ pub mod pallet {
Error::<T>::FeesNotMet
);

// Find transfer types for fee and non-fee assets, to get to the explicit reserve.
let (fees_transfer_type, mut assets_transfer_type) =
Self::find_fee_and_assets_transfer_types(&assets, fee_asset_index, &reserve)?;
// Ensure `reserve` is configured reserve for `assets`.
// Future enhancement: also support teleports to local reserve chain.
ensure!(
matches!(assets_transfer_type, TransferType::DestinationReserve),
Error::<T>::InvalidAssetUnsupportedReserve
);

// local and remote XCM programs to potentially handle fees separately
let fees = if fees_transfer_type == assets_transfer_type {
// no need for custom fees instructions, fees are batched with assets, and we are
// doing classic remote reserve transfer
assets_transfer_type = TransferType::RemoteReserve(reserve);
FeesHandling::Batched { fees }
} else {
// Future enhancement: also support combinations of reserve and teleport for assets
// and fees.
return Err(Error::<T>::Filtered.into())
};

let (local_xcm, remote_xcm) = Self::build_xcm_transfer_type(
origin.clone(),
dest.clone(),
Self::do_transfer_assets(
origin_location,
dest,
beneficiary,
assets,
assets_transfer_type,
*assets_transfer_type,
fees,
*fees_transfer_type,
weight_limit,
)?;
Self::execute_xcm_transfer(origin, dest, local_xcm, remote_xcm)
)
}
}
}
Expand Down Expand Up @@ -1773,6 +1704,73 @@ impl<T: Config> Pallet<T> {
Self::execute_xcm_transfer(origin_location, dest, local_xcm, remote_xcm)
}

fn do_transfer_assets(
origin: Location,
dest: Location,
beneficiary: Location,
mut assets: Vec<Asset>,
assets_transfer_type: TransferType,
fees: Asset,
fees_transfer_type: TransferType,
weight_limit: WeightLimit,
) -> DispatchResult {
// local and remote XCM programs to potentially handle fees separately
let fees = if fees_transfer_type == assets_transfer_type {
// no need for custom fees instructions, fees are batched with assets
FeesHandling::Batched { fees }
} else {
// Disallow _remote reserves_ unless assets & fees have same remote reserve (covered
// by branch above). The reason for this is that we'd need to send XCMs to separate
// chains with no guarantee of delivery order on final destination; therefore we
// cannot guarantee to have fees in place on final destination chain to pay for
// assets transfer.
ensure!(
!matches!(assets_transfer_type, TransferType::RemoteReserve(_)),
Error::<T>::InvalidAssetUnsupportedReserve
);
let weight_limit = weight_limit.clone();
// remove `fees` from `assets` and build separate fees transfer instructions to be
// added to assets transfers XCM programs
let fee_asset_index =
assets.iter().position(|a| a.id == fees.id).ok_or(Error::<T>::FeesNotMet)?;
let fees = assets.remove(fee_asset_index);
let (local_xcm, remote_xcm) = match fees_transfer_type {
TransferType::LocalReserve => Self::local_reserve_fees_instructions(
origin.clone(),
dest.clone(),
fees,
weight_limit,
)?,
TransferType::DestinationReserve => Self::destination_reserve_fees_instructions(
origin.clone(),
dest.clone(),
fees,
weight_limit,
)?,
TransferType::Teleport => Self::teleport_fees_instructions(
origin.clone(),
dest.clone(),
fees,
weight_limit,
)?,
TransferType::RemoteReserve(_) =>
return Err(Error::<T>::InvalidAssetUnsupportedReserve.into()),
};
FeesHandling::Separate { local_xcm, remote_xcm }
};

let (local_xcm, remote_xcm) = Self::build_xcm_transfer_type(
origin.clone(),
dest.clone(),
beneficiary,
assets,
assets_transfer_type,
fees,
weight_limit,
)?;
Self::execute_xcm_transfer(origin, dest, local_xcm, remote_xcm)
}

fn build_xcm_transfer_type(
origin: Location,
dest: Location,
Expand Down Expand Up @@ -1818,7 +1816,7 @@ impl<T: Config> Pallet<T> {
};
let local = Self::remote_reserve_transfer_program(
origin.clone(),
reserve,
reserve.try_into().map_err(|()| Error::<T>::BadVersion)?,
dest.clone(),
beneficiary,
assets,
Expand Down
6 changes: 3 additions & 3 deletions polkadot/xcm/xcm-executor/src/traits/asset_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum Error {
}

/// Specify which type of asset transfer is required for a particular `(asset, dest)` combination.
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo)]
pub enum TransferType {
/// should teleport `asset` to `dest`
Teleport,
Expand All @@ -39,7 +39,7 @@ pub enum TransferType {
/// should reserve-transfer `asset` to `dest`, using `dest` as reserve
DestinationReserve,
/// should reserve-transfer `asset` to `dest`, using remote chain `Location` as reserve
RemoteReserve(Location),
RemoteReserve(VersionedLocation),
}

/// A trait for identifying asset transfer type based on `IsTeleporter` and `IsReserve`
Expand Down Expand Up @@ -77,7 +77,7 @@ pub trait XcmAssetTransfers {
Ok(TransferType::LocalReserve)
} else if Self::IsReserve::contains(asset, &asset_location) {
// remote location that is recognized as reserve location for asset
Ok(TransferType::RemoteReserve(asset_location))
Ok(TransferType::RemoteReserve(asset_location.into()))
} else {
// remote location that is not configured either as teleporter or reserve => cannot
// determine asset reserve
Expand Down