Skip to content

Commit

Permalink
Support DOT reseve on AH and Snowfork bridged assets (#2858)
Browse files Browse the repository at this point in the history
* Support DOT reseve on AH and Snowfork bridged assets

* allow transfer assets from pallet xcm

* indent
  • Loading branch information
xlc authored Feb 10, 2025
1 parent c9d49d7 commit ac37316
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
8 changes: 4 additions & 4 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ impl Contains<RuntimeCall> for BaseCallFilter {
| pallet_xcm::Call::teleport_assets { .. }
| pallet_xcm::Call::reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_teleport_assets { .. }
| pallet_xcm::Call::transfer_assets { .. }
| pallet_xcm::Call::transfer_assets_using_type_and_then { .. } => {
| pallet_xcm::Call::limited_teleport_assets { .. } => {
return false;
}
// user xcm calls
pallet_xcm::Call::claim_assets { .. } => {
pallet_xcm::Call::claim_assets { .. }
| pallet_xcm::Call::transfer_assets { .. }
| pallet_xcm::Call::transfer_assets_using_type_and_then { .. } => {
return true;
}
// xcm operations call
Expand Down
23 changes: 20 additions & 3 deletions runtime/acala/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ use parity_scale_codec::{Decode, Encode};
use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery;
use primitives::evm::is_system_contract;
use runtime_common::{
local_currency_location, native_currency_location, AcalaDropAssets, EnsureRootOrHalfGeneralCouncil,
local_currency_location, native_currency_location, xcm_config::RelayLocationFilter,
xcm_impl::IsBridgedConcreteAssetFrom, AcalaDropAssets, EnsureRootOrHalfGeneralCouncil,
EnsureRootOrThreeFourthsGeneralCouncil, FixedRateOfAsset, RuntimeBlockWeights,
};
use sp_runtime::Perbill;
use xcm::{prelude::*, v3::Weight as XcmWeight};
use xcm_builder::{
EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, FrameTransactionalProcessor, SignedToAccountId32,
Case, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, FrameTransactionalProcessor, SignedToAccountId32,
};

parameter_types! {
Expand Down Expand Up @@ -94,8 +95,24 @@ parameter_types! {
0
);
pub BaseRate: u128 = aca_per_second();

/// Location of Asset Hub
pub AssetHubLocation: Location = Location::new(1, [Parachain(1000)]);
pub RelayChainNativeAssetFromAssetHub: (AssetFilter, Location) = (
RelayLocationFilter::get(),
AssetHubLocation::get()
);
}

type Reserves = (
// Assets bridged from different consensus systems held in reserve on Asset Hub.
IsBridgedConcreteAssetFrom<AssetHubLocation>,
// Relaychain (DOT) from Asset Hub
Case<RelayChainNativeAssetFromAssetHub>,
// Assets which the reserve is the same as the origin.
MultiNativeAsset<AbsoluteReserveProvider>,
);

pub type Trader = (
FixedRateOfAsset<BaseRate, ToTreasury, BuyWeightRateOfTransactionFeePool<Runtime, CurrencyIdConvert>>,
FixedRateOfFungible<AcaPerSecond, ToTreasury>,
Expand All @@ -115,7 +132,7 @@ impl xcm_executor::Config for XcmConfig {
// How to withdraw and deposit an asset.
type AssetTransactor = LocalAssetTransactor;
type OriginConverter = XcmOriginToCallOrigin;
type IsReserve = MultiNativeAsset<AbsoluteReserveProvider>;
type IsReserve = Reserves;
type IsTeleporter = runtime_common::xcm_config::TrustedTeleporters;
type UniversalLocation = UniversalLocation;
type Barrier = Barrier;
Expand Down
4 changes: 4 additions & 0 deletions runtime/common/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ impl Convert<AccountId, Location> for AccountIdToLocation {

parameter_types! {
pub const RelayLocation: Location = Location::parent();
pub RelayLocationFilter: AssetFilter = Wild(AllOf {
fun: WildFungible,
id: xcm::prelude::AssetId(RelayLocation::get()),
});
}

// define assets that can be trusted to teleport by remotes
Expand Down
25 changes: 24 additions & 1 deletion runtime/common/src/xcm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

//! Common xcm implementation
use frame_support::{traits::Get, weights::constants::WEIGHT_REF_TIME_PER_SECOND};
use frame_support::{
traits::{ContainsPair, Get},
weights::constants::WEIGHT_REF_TIME_PER_SECOND,
};
use module_support::BuyWeightRate;
use orml_traits::GetByKey;
use parity_scale_codec::Encode;
Expand Down Expand Up @@ -295,6 +298,26 @@ where
}
}

/// Matches foreign assets from a given origin.
/// Foreign assets are assets bridged from other consensus systems. i.e parents > 1.
pub struct IsBridgedConcreteAssetFrom<Origin>(PhantomData<Origin>);
impl<Origin> ContainsPair<Asset, Location> for IsBridgedConcreteAssetFrom<Origin>
where
Origin: Get<Location>,
{
fn contains(asset: &Asset, origin: &Location) -> bool {
let loc = Origin::get();
&loc == origin
&& matches!(
asset,
Asset {
id: AssetId(Location { parents: 2, .. }),
fun: Fungibility::Fungible(_)
},
)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 4 additions & 4 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ impl Contains<RuntimeCall> for BaseCallFilter {
| pallet_xcm::Call::teleport_assets { .. }
| pallet_xcm::Call::reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_teleport_assets { .. }
| pallet_xcm::Call::transfer_assets { .. }
| pallet_xcm::Call::transfer_assets_using_type_and_then { .. } => {
| pallet_xcm::Call::limited_teleport_assets { .. } => {
return false;
}
// user xcm calls
pallet_xcm::Call::claim_assets { .. } => {
pallet_xcm::Call::claim_assets { .. }
| pallet_xcm::Call::transfer_assets { .. }
| pallet_xcm::Call::transfer_assets_using_type_and_then { .. } => {
return true;
}
// xcm operations call
Expand Down
23 changes: 20 additions & 3 deletions runtime/karura/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ use parity_scale_codec::{Decode, Encode};
use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery;
use primitives::evm::is_system_contract;
use runtime_common::{
local_currency_location, native_currency_location, AcalaDropAssets, EnsureRootOrHalfGeneralCouncil,
local_currency_location, native_currency_location, xcm_config::RelayLocationFilter,
xcm_impl::IsBridgedConcreteAssetFrom, AcalaDropAssets, EnsureRootOrHalfGeneralCouncil,
EnsureRootOrThreeFourthsGeneralCouncil, FixedRateOfAsset, RuntimeBlockWeights,
};
use sp_runtime::Perbill;
use xcm::{prelude::*, v3::Weight as XcmWeight};
use xcm_builder::{
EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, FrameTransactionalProcessor, SignedToAccountId32,
Case, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, FrameTransactionalProcessor, SignedToAccountId32,
};

parameter_types! {
Expand Down Expand Up @@ -136,8 +137,24 @@ parameter_types! {
);

pub BaseRate: u128 = kar_per_second();

/// Location of Asset Hub
pub AssetHubLocation: Location = Location::new(1, [Parachain(1000)]);
pub RelayChainNativeAssetFromAssetHub: (AssetFilter, Location) = (
RelayLocationFilter::get(),
AssetHubLocation::get()
);
}

type Reserves = (
// Assets bridged from different consensus systems held in reserve on Asset Hub.
IsBridgedConcreteAssetFrom<AssetHubLocation>,
// Relaychain (DOT) from Asset Hub
Case<RelayChainNativeAssetFromAssetHub>,
// Assets which the reserve is the same as the origin.
MultiNativeAsset<AbsoluteReserveProvider>,
);

pub type Trader = (
FixedRateOfAsset<BaseRate, ToTreasury, BuyWeightRateOfTransactionFeePool<Runtime, CurrencyIdConvert>>,
FixedRateOfFungible<KarPerSecond, ToTreasury>,
Expand All @@ -162,7 +179,7 @@ impl xcm_executor::Config for XcmConfig {
// How to withdraw and deposit an asset.
type AssetTransactor = LocalAssetTransactor;
type OriginConverter = XcmOriginToCallOrigin;
type IsReserve = MultiNativeAsset<AbsoluteReserveProvider>;
type IsReserve = Reserves;
type IsTeleporter = runtime_common::xcm_config::TrustedTeleporters;
type UniversalLocation = UniversalLocation;
type Barrier = Barrier;
Expand Down

0 comments on commit ac37316

Please sign in to comment.