git.delta.rocks / unique-network / refs/commits / a64769ff6bff

difftreelog

source

runtime/common/config/xcm/foreignassets.rs6.3 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use frame_support::{traits::Get, parameter_types};18use sp_runtime::traits::Convert;19use xcm::latest::{prelude::*, MultiAsset, MultiLocation};20use xcm_builder::{FungiblesAdapter, NoChecking, ConvertedConcreteId};21use xcm_executor::traits::{TransactAsset, Convert as ConvertXcm, JustTry};22use pallet_foreign_assets::{23	AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, NativeCurrency, FreeForAll, TryAsForeign,24	ForeignAssetId, CurrencyId,25};26use sp_std::{borrow::Borrow, marker::PhantomData};27use orml_traits::location::AbsoluteReserveProvider;28use orml_xcm_support::MultiNativeAsset;29use crate::{Runtime, Balances, ParachainInfo, PolkadotXcm, ForeignAssets};3031use super::{LocationToAccountId, RelayLocation};3233use up_common::types::{AccountId, Balance};3435parameter_types! {36	pub CheckingAccount: AccountId = PolkadotXcm::check_account();37}3839pub struct AsInnerId<ConvertAssetId>(PhantomData<(AssetId, ConvertAssetId)>);40impl<ConvertAssetId: MaybeEquivalence<AssetId, AssetId>> MaybeEquivalence<MultiLocation, AssetId>41	for AsInnerId<ConvertAssetId>42{43	fn convert(id: &MultiLocation) -> Option<AssetId> {44		log::trace!(45			target: "xcm::AsInnerId::Convert",46			"AsInnerId {:?}",47			id48		);4950		let parent = MultiLocation::parent();51		let here = MultiLocation::here();52		let self_location = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));5354		if *id == parent {55			return ConvertAssetId::convert(&AssetId::NativeAssetId(NativeCurrency::Parent));56		}5758		if *id == here || *id == self_location {59			return ConvertAssetId::convert(&AssetId::NativeAssetId(NativeCurrency::Here));60		}6162		match XcmForeignAssetIdMapping::<Runtime>::get_currency_id(*id) {63			Some(AssetId::ForeignAssetId(foreign_asset_id)) => {64				ConvertAssetId::convert(&AssetId::ForeignAssetId(foreign_asset_id))65			}66			_ => None,67		}68	}6970	fn convert_back(asset_id: &AssetId) -> Option<MultiLocation> {71		log::trace!(72			target: "xcm::AsInnerId::Reverse",73			"AsInnerId",74		);7576		let parent_id =77			ConvertAssetId::convert(&AssetId::NativeAssetId(NativeCurrency::Parent)).unwrap();78		let here_id =79			ConvertAssetId::convert(&AssetId::NativeAssetId(NativeCurrency::Here)).unwrap();8081		if asset_id.clone() == parent_id {82			return Some(MultiLocation::parent());83		}8485		if asset_id.clone() == here_id {86			return Some(MultiLocation::new(87				1,88				X1(Parachain(ParachainInfo::get().into())),89			));90		}9192		let fid =93			<AssetId as TryAsForeign<AssetId, ForeignAssetId>>::try_as_foreign(asset_id.clone())?;94		XcmForeignAssetIdMapping::<Runtime>::get_multi_location(fid)95	}96}9798/// Means for transacting assets besides the native currency on this chain.99pub type FungiblesTransactor = FungiblesAdapter<100	// Use this fungibles implementation:101	ForeignAssets,102	// Use this currency when it is a fungible asset matching the given location or name:103	ConvertedConcreteId<AssetId, Balance, AsInnerId<JustTry>, JustTry>,104	// Convert an XCM MultiLocation into a local account id:105	LocationToAccountId,106	// Our chain's account ID type (we can't get away without mentioning it explicitly):107	AccountId,108	// No Checking for teleported assets since we disallow teleports at all.109	NoChecking,110	// The account to use for tracking teleports.111	CheckingAccount,112>;113114/// Means for transacting assets on this chain.115pub struct AssetTransactor;116impl TransactAsset for AssetTransactor {117	fn can_check_in(118		_origin: &MultiLocation,119		_what: &MultiAsset,120		_context: &XcmContext,121	) -> XcmResult {122		Err(XcmError::Unimplemented)123	}124125	fn check_in(_origin: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}126127	fn can_check_out(128		_dest: &MultiLocation,129		_what: &MultiAsset,130		_context: &XcmContext,131	) -> XcmResult {132		Err(XcmError::Unimplemented)133	}134135	fn check_out(_dest: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}136137	fn deposit_asset(what: &MultiAsset, who: &MultiLocation, context: &XcmContext) -> XcmResult {138		FungiblesTransactor::deposit_asset(what, who, context)139	}140141	fn withdraw_asset(142		what: &MultiAsset,143		who: &MultiLocation,144		maybe_context: Option<&XcmContext>,145	) -> Result<staging_xcm_executor::Assets, XcmError> {146		FungiblesTransactor::withdraw_asset(what, who, maybe_context)147	}148149	fn internal_transfer_asset(150		what: &MultiAsset,151		from: &MultiLocation,152		to: &MultiLocation,153		context: &XcmContext,154	) -> Result<staging_xcm_executor::Assets, XcmError> {155		FungiblesTransactor::internal_transfer_asset(what, from, to, context)156	}157}158159pub type IsReserve = MultiNativeAsset<AbsoluteReserveProvider>;160161pub type Trader<T> = FreeForAll<162	pallet_configuration::WeightToFee<T, Balance>,163	RelayLocation,164	AccountId,165	Balances,166	(),167>;168169pub struct CurrencyIdConvert;170impl Convert<AssetId, Option<MultiLocation>> for CurrencyIdConvert {171	fn convert(id: AssetId) -> Option<MultiLocation> {172		match id {173			AssetId::NativeAssetId(NativeCurrency::Here) => Some(MultiLocation::new(174				1,175				X1(Parachain(ParachainInfo::get().into())),176			)),177			AssetId::NativeAssetId(NativeCurrency::Parent) => Some(MultiLocation::parent()),178			AssetId::ForeignAssetId(foreign_asset_id) => {179				XcmForeignAssetIdMapping::<Runtime>::get_multi_location(foreign_asset_id)180			}181		}182	}183}184185impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {186	fn convert(location: MultiLocation) -> Option<CurrencyId> {187		if location == MultiLocation::here()188			|| location == MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())))189		{190			return Some(AssetId::NativeAssetId(NativeCurrency::Here));191		}192193		if location == MultiLocation::parent() {194			return Some(AssetId::NativeAssetId(NativeCurrency::Parent));195		}196197		if let Some(currency_id) = XcmForeignAssetIdMapping::<Runtime>::get_currency_id(location) {198			return Some(currency_id);199		}200201		None202	}203}