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

Polkadot-1.7.2: restricted transfer location changes #1853

Merged
merged 8 commits into from
Jun 6, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 5 additions & 25 deletions libs/types/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ use cfg_primitives::AccountId;
use frame_support::RuntimeDebugNoBound;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::{crypto::AccountId32, H256};
use sp_runtime::traits::{BlakeTwo256, Hash};
use sp_core::crypto::AccountId32;
use sp_std::boxed::Box;
use staging_xcm::VersionedLocation;

use crate::domain_address::DomainAddress;

/// Location types for destinations that can receive restricted transfers
#[derive(Clone, RuntimeDebugNoBound, Encode, Decode, Eq, PartialEq, MaxEncodedLen, TypeInfo)]
pub enum RestrictedTransferLocation {
/// Local chain account sending destination.
Local(AccountId),
/// XCM MultiLocation sending destinations.
/// Using hash value here as Multilocation is large -- v1 is 512 bytes, but
/// next largest is only 40 bytes other values aren't hashed as we have
/// blake2 hashing on storage map keys, and we don't want the extra overhead
XCM(H256),
/// XCM Location sending destinations.
Xcm(Box<VersionedLocation>),
/// DomainAddress sending location from a liquidity pools' instance
Address(DomainAddress),
}
Expand All @@ -38,21 +36,3 @@ impl From<AccountId32> for RestrictedTransferLocation {
Self::Local(value)
}
}

impl From<VersionedLocation> for RestrictedTransferLocation {
fn from(vml: VersionedLocation) -> Self {
lemunozm marked this conversation as resolved.
Show resolved Hide resolved
// using hash here as multilocation is significantly larger than any other enum
// type here -- 592 bytes, vs 40 bytes for domain address (next largest)
Self::XCM(BlakeTwo256::hash(&vml.encode()))

// TODO-1.7: I'm afraid of locations translated from v3 to v4 will
// generate a different hash here. How this affect our current chain
// state?
}
}

impl From<DomainAddress> for RestrictedTransferLocation {
fn from(da: DomainAddress) -> Self {
Self::Address(da)
}
}
42 changes: 12 additions & 30 deletions pallets/restricted-xtokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,37 @@ use staging_xcm::{v4::prelude::*, VersionedAsset, VersionedAssets, VersionedLoca
pub enum TransferEffects<AccountId, CurrencyId, Balance> {
Transfer {
sender: AccountId,
destination: Location,
destination: VersionedLocation,
currency_id: CurrencyId,
amount: Balance,
},
TransferMultiAsset {
sender: AccountId,
destination: Location,
destination: VersionedLocation,
asset: Asset,
},
TransferWithFee {
sender: AccountId,
destination: Location,
destination: VersionedLocation,
currency_id: CurrencyId,
amount: Balance,
fee: Balance,
},
TransferMultiAssetWithFee {
sender: AccountId,
destination: Location,
destination: VersionedLocation,
asset: Asset,
fee_asset: Asset,
},
TransferMultiCurrencies {
sender: AccountId,
destination: Location,
destination: VersionedLocation,
currencies: Vec<(CurrencyId, Balance)>,
fee: (CurrencyId, Balance),
},
TransferMultiAssets {
sender: AccountId,
destination: Location,
destination: VersionedLocation,
assets: Assets,
fee_asset: Asset,
},
Expand Down Expand Up @@ -130,14 +130,11 @@ pub mod pallet {
dest: Box<VersionedLocation>,
dest_weight_limit: WeightLimit,
) -> DispatchResult {
let destination: Location = (*dest.clone())
.try_into()
.map_err(|()| Error::<T>::BadVersion)?;
let sender = ensure_signed(origin.clone())?;

T::PreTransfer::check(TransferEffects::Transfer {
sender,
destination,
destination: (*dest.clone()),
currency_id: currency_id.clone(),
amount,
})?;
Expand Down Expand Up @@ -175,13 +172,10 @@ pub mod pallet {
let multi_asset: Asset = (*asset.clone())
.try_into()
.map_err(|()| Error::<T>::BadVersion)?;
let destination: Location = (*dest.clone())
.try_into()
.map_err(|()| Error::<T>::BadVersion)?;

T::PreTransfer::check(TransferEffects::TransferMultiAsset {
sender,
destination,
destination: (*dest.clone()),
asset: multi_asset,
})?;

Expand Down Expand Up @@ -220,13 +214,10 @@ pub mod pallet {
dest_weight_limit: WeightLimit,
) -> DispatchResult {
let sender = ensure_signed(origin.clone())?;
let destination: Location = (*dest.clone())
.try_into()
.map_err(|()| Error::<T>::BadVersion)?;

T::PreTransfer::check(TransferEffects::TransferWithFee {
sender,
destination,
destination: (*dest.clone()),
currency_id: currency_id.clone(),
amount,
fee,
Expand Down Expand Up @@ -279,13 +270,10 @@ pub mod pallet {
let fee_asset: Asset = (*fee.clone())
.try_into()
.map_err(|()| Error::<T>::BadVersion)?;
let destination: Location = (*dest.clone())
.try_into()
.map_err(|()| Error::<T>::BadVersion)?;

T::PreTransfer::check(TransferEffects::TransferMultiAssetWithFee {
sender,
destination,
destination: (*dest.clone()),
asset: multi_asset,
fee_asset,
})?;
Expand Down Expand Up @@ -324,16 +312,13 @@ pub mod pallet {
dest_weight_limit: WeightLimit,
) -> DispatchResult {
let sender = ensure_signed(origin.clone())?;
let destination: Location = (*dest.clone())
.try_into()
.map_err(|()| Error::<T>::BadVersion)?;
let fee = currencies
.get(fee_item as usize)
.ok_or(orml_xtokens::Error::<T>::AssetIndexNonExistent)?;

T::PreTransfer::check(TransferEffects::TransferMultiCurrencies {
sender,
destination,
destination: (*dest.clone()),
currencies: currencies.clone(),
fee: fee.clone(),
})?;
Expand Down Expand Up @@ -375,16 +360,13 @@ pub mod pallet {
let multi_assets: Assets = (*assets.clone())
.try_into()
.map_err(|()| Error::<T>::BadVersion)?;
let destination: Location = (*dest.clone())
.try_into()
.map_err(|()| Error::<T>::BadVersion)?;
let fee_asset: &Asset = multi_assets
.get(fee_item as usize)
.ok_or(orml_xtokens::Error::<T>::AssetIndexNonExistent)?;

T::PreTransfer::check(TransferEffects::TransferMultiAssets {
sender,
destination,
destination: (*dest.clone()),
assets: multi_assets.clone(),
fee_asset: fee_asset.clone(),
})?;
Expand Down
Loading
Loading