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

orml-xtokens Astar integration. #1000

Merged
merged 2 commits into from
Aug 16, 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
5 changes: 3 additions & 2 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ xcm = { workspace = true }
xcm-builder = { workspace = true }
xcm-executor = { workspace = true }

# ORML dependencies
orml-traits = { workspace = true }

# Frontier dependencies
pallet-evm = { workspace = true }

Expand All @@ -55,6 +58,7 @@ std = [
"xcm/std",
"xcm-builder/std",
"xcm-executor/std",
"orml-traits/std",
"pallet-xc-asset-config/std",
"fp-evm/std",
"pallet-assets/std",
Expand Down
40 changes: 36 additions & 4 deletions primitives/src/xcm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@

#![cfg_attr(not(feature = "std"), no_std)]

use crate::AccountId;

use frame_support::{
traits::{tokens::fungibles, ContainsPair, Get},
weights::constants::WEIGHT_REF_TIME_PER_SECOND,
};
use sp_runtime::traits::{Bounded, Zero};
use sp_runtime::traits::{Bounded, Convert, Zero};
use sp_std::{borrow::Borrow, marker::PhantomData, vec::Vec};

// Polkadot imports
use xcm::latest::{prelude::*, Weight};
use xcm_builder::TakeRevenue;
use xcm_executor::traits::{MatchesFungibles, WeightTrader};

// ORML imports
use orml_traits::location::{RelativeReserveProvider, Reserve};

use pallet_xc_asset_config::{ExecutionPaymentRate, XcAssetLocation};

#[cfg(test)]
Expand All @@ -53,9 +58,7 @@ mod tests;
///
/// This implementation relies on `XcAssetConfig` pallet to handle mapping.
/// In case asset location hasn't been mapped, it means the asset isn't supported (yet).
pub struct AssetLocationIdConverter<AssetId, AssetMapper>(
sp_std::marker::PhantomData<(AssetId, AssetMapper)>,
);
pub struct AssetLocationIdConverter<AssetId, AssetMapper>(PhantomData<(AssetId, AssetMapper)>);
impl<AssetId, AssetMapper> xcm_executor::traits::Convert<MultiLocation, AssetId>
for AssetLocationIdConverter<AssetId, AssetMapper>
where
Expand Down Expand Up @@ -384,3 +387,32 @@ impl<AccountId: From<[u8; 32]> + Clone, Describe: DescribeLocation>
}
}
}

/// Convert `AccountId` to `MultiLocation`.
pub struct AccountIdToMultiLocation;
impl Convert<AccountId, MultiLocation> for AccountIdToMultiLocation {
fn convert(account: AccountId) -> MultiLocation {
X1(AccountId32 {
network: None,
id: account.into(),
})
.into()
}
}

/// `MultiAsset` reserve location provider. It's based on `RelativeReserveProvider` and in
/// addition will convert self absolute location to relative location.
pub struct AbsoluteAndRelativeReserveProvider<AbsoluteLocation>(PhantomData<AbsoluteLocation>);
impl<AbsoluteLocation: Get<MultiLocation>> Reserve
for AbsoluteAndRelativeReserveProvider<AbsoluteLocation>
{
fn reserve(asset: &MultiAsset) -> Option<MultiLocation> {
RelativeReserveProvider::reserve(asset).map(|reserve_location| {
if reserve_location == AbsoluteLocation::get() {
MultiLocation::here()
} else {
reserve_location
}
})
}
}
8 changes: 8 additions & 0 deletions runtime/astar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ xcm = { workspace = true }
xcm-builder = { workspace = true }
xcm-executor = { workspace = true }

# orml dependencies
orml-xcm-support = { workspace = true }
orml-xtokens = { workspace = true }

# benchmarking
array-bytes = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }
Expand Down Expand Up @@ -198,6 +202,8 @@ std = [
"xcm-executor/std",
"pallet-xc-asset-config/std",
"substrate-wasm-builder",
"orml-xtokens/std",
"orml-xcm-support/std",
"astar-primitives/std",
]
runtime-benchmarks = [
Expand All @@ -218,6 +224,7 @@ runtime-benchmarks = [
"pallet-xc-asset-config/runtime-benchmarks",
"pallet-collator-selection/runtime-benchmarks",
"polkadot-runtime/runtime-benchmarks",
"orml-xtokens/runtime-benchmarks",
"astar-primitives/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
]
Expand Down Expand Up @@ -258,6 +265,7 @@ try-runtime = [
"pallet-base-fee/try-runtime",
"pallet-contracts/try-runtime",
"pallet-evm/try-runtime",
"orml-xtokens/try-runtime",
]
evm-tracing = [
"moonbeam-evm-tracer",
Expand Down
1 change: 1 addition & 0 deletions runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ construct_runtime!(
CumulusXcm: cumulus_pallet_xcm = 52,
DmpQueue: cumulus_pallet_dmp_queue = 53,
XcAssetConfig: pallet_xc_asset_config = 54,
Xtokens: orml_xtokens = 55,

EVM: pallet_evm = 60,
Ethereum: pallet_ethereum = 61,
Expand Down
57 changes: 53 additions & 4 deletions runtime/astar/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use frame_support::{
weights::Weight,
};
use frame_system::EnsureRoot;
use sp_runtime::traits::Convert;

// Polkadot imports
use xcm::latest::prelude::*;
Expand All @@ -40,12 +41,18 @@ use xcm_builder::{
SovereignSignedViaLocation, TakeWeightCredit, UsingComponents,
};
use xcm_executor::{
traits::{JustTry, WithOriginFilter},
traits::{Convert as XcmConvert, JustTry, WithOriginFilter},
XcmExecutor,
};

// ORML imports
use orml_xcm_support::DisabledParachainFee;

// Astar imports
use astar_primitives::xcm::{FixedRateOfForeignAsset, ReserveAssetFilter, XcmFungibleFeeHandler};
use astar_primitives::xcm::{
AbsoluteAndRelativeReserveProvider, AccountIdToMultiLocation, FixedRateOfForeignAsset,
ReserveAssetFilter, XcmFungibleFeeHandler,
};

parameter_types! {
pub RelayNetwork: Option<NetworkId> = Some(NetworkId::Polkadot);
Expand Down Expand Up @@ -242,7 +249,7 @@ impl xcm_executor::Config for XcmConfig {
type IsTeleporter = ();
type UniversalLocation = UniversalLocation;
type Barrier = XcmBarrier;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type Weigher = Weigher;
type Trader = (
UsingComponents<WeightToFee, AstarLocation, AccountId, Balances, DealWithFees>,
FixedRateOfForeignAsset<XcAssetConfig, AstarXcmFungibleFeeHandler>,
Expand Down Expand Up @@ -284,6 +291,8 @@ parameter_types! {
pub ReachableDest: Option<MultiLocation> = Some(Parent.into());
}

pub type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;

impl pallet_xcm::Config for Runtime {
const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;

Expand All @@ -295,7 +304,7 @@ impl pallet_xcm::Config for Runtime {
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmTeleportFilter = Nothing;
type XcmReserveTransferFilter = Everything;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type Weigher = Weigher;
type UniversalLocation = UniversalLocation;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
Expand Down Expand Up @@ -334,3 +343,43 @@ impl cumulus_pallet_dmp_queue::Config for Runtime {
type XcmExecutor = XcmExecutor<XcmConfig>;
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
}

parameter_types! {
/// The absolute location in perspective of the whole network.
pub AstarLocationAbsolute: MultiLocation = MultiLocation {
parents: 1,
interior: X1(
Parachain(ParachainInfo::parachain_id().into())
)
};
/// Max asset types for one cross-chain transfer. `2` covers all current use cases.
/// Can be updated with extra test cases in the future if needed.
pub const MaxAssetsForTransfer: usize = 2;
}

/// Convert `AssetId` to optional `MultiLocation`. The impl is a wrapper
/// on `ShidenAssetLocationIdConverter`.
pub struct AssetIdConvert;
impl Convert<AssetId, Option<MultiLocation>> for AssetIdConvert {
fn convert(asset_id: AssetId) -> Option<MultiLocation> {
AstarAssetLocationIdConverter::reverse_ref(&asset_id).ok()
}
}

impl orml_xtokens::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type CurrencyId = AssetId;
type CurrencyIdConvert = AssetIdConvert;
type AccountIdToMultiLocation = AccountIdToMultiLocation;
type SelfLocation = AstarLocation;
type XcmExecutor = XcmExecutor<XcmConfig>;
type Weigher = Weigher;
type BaseXcmWeight = UnitWeightCost;
type UniversalLocation = UniversalLocation;
type MaxAssetsForTransfer = MaxAssetsForTransfer;
// Default impl. Refer to `orml-xtokens` docs for more details.
type MinXcmFee = DisabledParachainFee;
type MultiLocationsFilter = Everything;
type ReserveProvider = AbsoluteAndRelativeReserveProvider<AstarLocationAbsolute>;
}
2 changes: 0 additions & 2 deletions runtime/shibuya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ xcm-builder = { workspace = true }
xcm-executor = { workspace = true }

# orml dependencies
orml-traits = { workspace = true }
orml-xcm-support = { workspace = true }
orml-xtokens = { workspace = true }

Expand Down Expand Up @@ -233,7 +232,6 @@ std = [
"substrate-wasm-builder",
"pallet-chain-extension-assets/std",
"orml-xtokens/std",
"orml-traits/std",
"orml-xcm-support/std",
"astar-primitives/std",
]
Expand Down
38 changes: 4 additions & 34 deletions runtime/shibuya/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ use frame_support::{
weights::Weight,
};
use frame_system::EnsureRoot;
use sp_runtime::traits::{Convert, Get};
use sp_std::marker::PhantomData;
use sp_runtime::traits::Convert;

// Polkadot imports
use xcm::latest::prelude::*;
Expand All @@ -47,13 +46,13 @@ use xcm_executor::{
};

// ORML imports
use orml_traits::location::{RelativeReserveProvider, Reserve};
use orml_xcm_support::DisabledParachainFee;

// Astar imports
use astar_primitives::xcm::{
DescribeAllTerminal, DescribeFamily, FixedRateOfForeignAsset, HashedDescription,
ReserveAssetFilter, XcmFungibleFeeHandler,
AbsoluteAndRelativeReserveProvider, AccountIdToMultiLocation, DescribeAllTerminal,
DescribeFamily, FixedRateOfForeignAsset, HashedDescription, ReserveAssetFilter,
XcmFungibleFeeHandler,
};

parameter_types! {
Expand Down Expand Up @@ -276,18 +275,6 @@ impl cumulus_pallet_dmp_queue::Config for Runtime {
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
}

/// Convert `AccountId` to `MultiLocation`.
pub struct AccountIdToMultiLocation;
impl Convert<AccountId, MultiLocation> for AccountIdToMultiLocation {
fn convert(account: AccountId) -> MultiLocation {
X1(AccountId32 {
network: None,
id: account.into(),
})
.into()
}
}

parameter_types! {
/// The absolute location in perspective of the whole network.
pub ShibuyaLocationAbsolute: MultiLocation = MultiLocation {
Expand All @@ -310,23 +297,6 @@ impl Convert<AssetId, Option<MultiLocation>> for AssetIdConvert {
}
}

/// `MultiAsset` reserve location provider. It's based on `RelativeReserveProvider` and in
/// addition will convert self absolute location to relative location.
pub struct AbsoluteAndRelativeReserveProvider<AbsoluteLocation>(PhantomData<AbsoluteLocation>);
impl<AbsoluteLocation: Get<MultiLocation>> Reserve
for AbsoluteAndRelativeReserveProvider<AbsoluteLocation>
{
fn reserve(asset: &MultiAsset) -> Option<MultiLocation> {
RelativeReserveProvider::reserve(asset).map(|reserve_location| {
if reserve_location == AbsoluteLocation::get() {
MultiLocation::here()
} else {
reserve_location
}
})
}
}

impl orml_xtokens::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
Expand Down
2 changes: 0 additions & 2 deletions runtime/shiden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ xcm-builder = { workspace = true }
xcm-executor = { workspace = true }

# orml dependencies
orml-traits = { workspace = true }
orml-xcm-support = { workspace = true }
orml-xtokens = { workspace = true }

Expand Down Expand Up @@ -207,7 +206,6 @@ std = [
"pallet-xc-asset-config/std",
"substrate-wasm-builder",
"orml-xtokens/std",
"orml-traits/std",
"orml-xcm-support/std",
"astar-primitives/std",
]
Expand Down
Loading