Skip to content

Commit

Permalink
CfgLocation to RestrictedTransferLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed May 17, 2024
1 parent a783c9f commit 1f70efe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
16 changes: 8 additions & 8 deletions libs/types/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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 CfgLocation {
pub enum RestrictedTransferLocation {
/// Local chain account sending destination.
Local(AccountId),
/// XCM MultiLocation sending destinations.
Expand All @@ -33,13 +33,13 @@ pub enum CfgLocation {
Address(DomainAddress),
}

impl From<AccountId32> for CfgLocation {
impl From<AccountId32> for RestrictedTransferLocation {
fn from(value: AccountId32) -> Self {
Self::Local(value)
}
}

impl From<VersionedLocation> for CfgLocation {
impl From<VersionedLocation> for RestrictedTransferLocation {
fn from(vml: VersionedLocation) -> Self {
// using hash here as multilocation is significantly larger than any other enum
// type here -- 592 bytes, vs 40 bytes for domain address (next largest)
Expand All @@ -50,7 +50,7 @@ impl From<VersionedLocation> for CfgLocation {
}
}

impl From<DomainAddress> for CfgLocation {
impl From<DomainAddress> for RestrictedTransferLocation {
fn from(da: DomainAddress) -> Self {
Self::Address(da)
}
Expand All @@ -68,10 +68,10 @@ mod test {
fn from_xcm_versioned_address_works() {
// TODO-1.7: Must be changed to V4?
let xa = VersionedLocation::V3(MultiLocation::default());
let l = CfgLocation::from(xa.clone());
let l = RestrictedTransferLocation::from(xa.clone());
assert_eq!(
l,
CfgLocation::XCM(sp_core::H256(
RestrictedTransferLocation::XCM(sp_core::H256(
<[u8; 32]>::from_hex(
"a943e30c855a123a9506e69e678dc65ae9f5b70149cb6b26eb2ed58a59b4bf77"
)
Expand All @@ -86,8 +86,8 @@ mod test {
1284,
<[u8; 20]>::from_hex("1231231231231231231231231231231231231231").unwrap(),
);
let l = CfgLocation::from(da.clone());
let l = RestrictedTransferLocation::from(da.clone());

assert_eq!(l, CfgLocation::Address(da))
assert_eq!(l, RestrictedTransferLocation::Address(da))
}
}
56 changes: 38 additions & 18 deletions runtime/common/src/transfer_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use cfg_primitives::{AccountId, Balance};
use cfg_traits::{PreConditions, TransferAllowance};
use cfg_types::{
domain_address::DomainAddress,
locations::CfgLocation,
locations::RestrictedTransferLocation,
tokens::{CurrencyId, FilterCurrency},
};
use frame_support::{traits::IsSubType, RuntimeDebugNoBound};
Expand All @@ -34,7 +34,11 @@ use staging_xcm::v4::{Asset, Location};
pub struct PreXcmTransfer<T, C>(sp_std::marker::PhantomData<(T, C)>);

impl<
T: TransferAllowance<AccountId, CurrencyId = FilterCurrency, Location = CfgLocation>,
T: TransferAllowance<
AccountId,
CurrencyId = FilterCurrency,
Location = RestrictedTransferLocation,
>,
C: Convert<Location, Option<CurrencyId>>,
> PreConditions<TransferEffects<AccountId, CurrencyId, Balance>> for PreXcmTransfer<T, C>
{
Expand All @@ -45,12 +49,12 @@ impl<
amalgamate_allowance(
T::allowance(
sender.clone(),
CfgLocation::XCM(BlakeTwo256::hash(&destination.encode())),
RestrictedTransferLocation::XCM(BlakeTwo256::hash(&destination.encode())),
FilterCurrency::Specific(currency),
),
T::allowance(
sender,
CfgLocation::XCM(BlakeTwo256::hash(&destination.encode())),
RestrictedTransferLocation::XCM(BlakeTwo256::hash(&destination.encode())),
FilterCurrency::All,
),
)
Expand Down Expand Up @@ -136,21 +140,26 @@ impl<

pub struct PreNativeTransfer<T>(sp_std::marker::PhantomData<T>);

impl<T: TransferAllowance<AccountId, CurrencyId = FilterCurrency, Location = CfgLocation>>
PreConditions<TransferDetails<AccountId, CurrencyId, Balance>> for PreNativeTransfer<T>
impl<
T: TransferAllowance<
AccountId,
CurrencyId = FilterCurrency,
Location = RestrictedTransferLocation,
>,
> PreConditions<TransferDetails<AccountId, CurrencyId, Balance>> for PreNativeTransfer<T>
{
type Result = bool;

fn check(t: TransferDetails<AccountId, CurrencyId, Balance>) -> Self::Result {
amalgamate_allowance(
T::allowance(
t.send.clone(),
CfgLocation::Local(t.recv.clone()),
RestrictedTransferLocation::Local(t.recv.clone()),
FilterCurrency::Specific(t.id),
),
T::allowance(
t.send.clone(),
CfgLocation::Local(t.recv.clone()),
RestrictedTransferLocation::Local(t.recv.clone()),
FilterCurrency::All,
),
)
Expand All @@ -159,8 +168,13 @@ impl<T: TransferAllowance<AccountId, CurrencyId = FilterCurrency, Location = Cfg
}
pub struct PreLpTransfer<T>(sp_std::marker::PhantomData<T>);

impl<T: TransferAllowance<AccountId, CurrencyId = FilterCurrency, Location = CfgLocation>>
PreConditions<(AccountId, DomainAddress, CurrencyId)> for PreLpTransfer<T>
impl<
T: TransferAllowance<
AccountId,
CurrencyId = FilterCurrency,
Location = RestrictedTransferLocation,
>,
> PreConditions<(AccountId, DomainAddress, CurrencyId)> for PreLpTransfer<T>
{
type Result = DispatchResult;

Expand All @@ -170,10 +184,14 @@ impl<T: TransferAllowance<AccountId, CurrencyId = FilterCurrency, Location = Cfg
amalgamate_allowance(
T::allowance(
sender.clone(),
CfgLocation::Address(receiver.clone()),
RestrictedTransferLocation::Address(receiver.clone()),
FilterCurrency::Specific(currency),
),
T::allowance(sender, CfgLocation::Address(receiver), FilterCurrency::All),
T::allowance(
sender,
RestrictedTransferLocation::Address(receiver),
FilterCurrency::All,
),
)
}
}
Expand Down Expand Up @@ -297,8 +315,10 @@ where
+ pallet_utility::Config<RuntimeCall = <T as frame_system::Config>::RuntimeCall>
+ pallet_proxy::Config<RuntimeCall = <T as frame_system::Config>::RuntimeCall>
+ pallet_remarks::Config<RuntimeCall = <T as frame_system::Config>::RuntimeCall>
+ pallet_transfer_allowlist::Config<CurrencyId = FilterCurrency, Location = CfgLocation>
+ Sync
+ pallet_transfer_allowlist::Config<
CurrencyId = FilterCurrency,
Location = RestrictedTransferLocation,
> + Sync
+ Send,
<T as frame_system::Config>::RuntimeCall: IsSubType<pallet_balances::Call<T>>
+ IsSubType<pallet_utility::Call<T>>
Expand Down Expand Up @@ -329,12 +349,12 @@ where
amalgamate_allowance(
pallet_transfer_allowlist::pallet::Pallet::<T>::allowance(
who.clone(),
CfgLocation::Local(recv.clone()),
RestrictedTransferLocation::Local(recv.clone()),
FilterCurrency::All,
),
pallet_transfer_allowlist::pallet::Pallet::<T>::allowance(
who.clone(),
CfgLocation::Local(recv.clone()),
RestrictedTransferLocation::Local(recv.clone()),
FilterCurrency::Specific(CurrencyId::Native),
),
)
Expand All @@ -344,8 +364,8 @@ where
}

fn amalgamate_allowance(
first: Result<Option<CfgLocation>, DispatchError>,
second: Result<Option<CfgLocation>, DispatchError>,
first: Result<Option<RestrictedTransferLocation>, DispatchError>,
second: Result<Option<RestrictedTransferLocation>, DispatchError>,
) -> DispatchResult {
match (first, second) {
// There is an allowance set for `Specific(id)`, but NOT for the given recv
Expand Down

0 comments on commit 1f70efe

Please sign in to comment.