git.delta.rocks / unique-network / refs/commits / 9532bdeed19c

difftreelog

fix convert_ref AsInnerId

Daniel Shiposha2022-12-22parent: #a03ab6a.patch.diff
in: master

1 file changed

modifiedruntime/common/config/xcm/foreignassets.rsdiffbeforeafterboth
before · runtime/common/config/xcm/foreignassets.rs
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::{18	traits::{Contains, Get, fungibles},19	parameter_types,20};21use sp_runtime::traits::{Zero, Convert};22use xcm::v1::{Junction::*, MultiLocation, Junctions::*};23use xcm::latest::MultiAsset;24use xcm_builder::{FungiblesAdapter, ConvertedConcreteAssetId};25use xcm_executor::traits::{Convert as ConvertXcm, JustTry, FilterAssetLocation};26use pallet_foreign_assets::{27	AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, NativeCurrency, FreeForAll, TryAsForeign,28	ForeignAssetId, CurrencyId,29};30use sp_std::{borrow::Borrow, marker::PhantomData};31use crate::{Runtime, Balances, ParachainInfo, PolkadotXcm, ForeignAssets};3233use super::{LocationToAccountId, RelayLocation};3435use up_common::types::{AccountId, Balance};3637parameter_types! {38	pub CheckingAccount: AccountId = PolkadotXcm::check_account();39}4041/// Allow checking in assets that have issuance > 0.42pub struct NonZeroIssuance<AccountId, ForeignAssets>(PhantomData<(AccountId, ForeignAssets)>);4344impl<AccountId, ForeignAssets> Contains<<ForeignAssets as fungibles::Inspect<AccountId>>::AssetId>45	for NonZeroIssuance<AccountId, ForeignAssets>46where47	ForeignAssets: fungibles::Inspect<AccountId>,48{49	fn contains(id: &<ForeignAssets as fungibles::Inspect<AccountId>>::AssetId) -> bool {50		!ForeignAssets::total_issuance(*id).is_zero()51	}52}5354pub struct AsInnerId<AssetId, ConvertAssetId>(PhantomData<(AssetId, ConvertAssetId)>);55impl<AssetId: Clone + PartialEq, ConvertAssetId: ConvertXcm<AssetId, AssetId>>56	ConvertXcm<MultiLocation, AssetId> for AsInnerId<AssetId, ConvertAssetId>57where58	AssetId: Borrow<AssetId>,59	AssetId: TryAsForeign<AssetId, ForeignAssetId>,60	AssetIds: Borrow<AssetId>,61{62	fn convert_ref(id: impl Borrow<MultiLocation>) -> Result<AssetId, ()> {63		let id = id.borrow();6465		log::trace!(66			target: "xcm::AsInnerId::Convert",67			"AsInnerId {:?}",68			id69		);7071		let parent = MultiLocation::parent();72		let here = MultiLocation::here();73		let self_location = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));7475		if *id == parent {76			return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent));77		}7879		if *id == here || *id == self_location {80			return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here));81		}8283		match XcmForeignAssetIdMapping::<Runtime>::get_currency_id(id.clone()) {84			Some(AssetIds::ForeignAssetId(foreign_asset_id)) => {85				ConvertAssetId::convert_ref(AssetIds::ForeignAssetId(foreign_asset_id))86			}87			_ => ConvertAssetId::convert_ref(AssetIds::ForeignAssetId(0)),88		}89	}9091	fn reverse_ref(what: impl Borrow<AssetId>) -> Result<MultiLocation, ()> {92		log::trace!(93			target: "xcm::AsInnerId::Reverse",94			"AsInnerId",95		);9697		let asset_id = what.borrow();9899		let parent_id =100			ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent)).unwrap();101		let here_id =102			ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here)).unwrap();103104		if asset_id.clone() == parent_id {105			return Ok(MultiLocation::parent());106		}107108		if asset_id.clone() == here_id {109			return Ok(MultiLocation::new(110				1,111				X1(Parachain(ParachainInfo::get().into())),112			));113		}114115		match <AssetId as TryAsForeign<AssetId, ForeignAssetId>>::try_as_foreign(asset_id.clone()) {116			Some(fid) => match XcmForeignAssetIdMapping::<Runtime>::get_multi_location(fid) {117				Some(location) => Ok(location),118				None => Err(()),119			},120			None => Err(()),121		}122	}123}124125/// Means for transacting assets besides the native currency on this chain.126pub type FungiblesTransactor = FungiblesAdapter<127	// Use this fungibles implementation:128	ForeignAssets,129	// Use this currency when it is a fungible asset matching the given location or name:130	ConvertedConcreteAssetId<AssetIds, Balance, AsInnerId<AssetIds, JustTry>, JustTry>,131	// Convert an XCM MultiLocation into a local account id:132	LocationToAccountId,133	// Our chain's account ID type (we can't get away without mentioning it explicitly):134	AccountId,135	// We only want to allow teleports of known assets. We use non-zero issuance as an indication136	// that this asset is known.137	NonZeroIssuance<AccountId, ForeignAssets>,138	// The account to use for tracking teleports.139	CheckingAccount,140>;141142/// Means for transacting assets on this chain.143pub type AssetTransactors = FungiblesTransactor;144145pub struct AllAsset;146impl FilterAssetLocation for AllAsset {147	fn filter_asset_location(_asset: &MultiAsset, _origin: &MultiLocation) -> bool {148		true149	}150}151152pub type IsReserve = AllAsset;153154pub type Trader<T> = FreeForAll<155	pallet_configuration::WeightToFee<T, Balance>,156	RelayLocation,157	AccountId,158	Balances,159	(),160>;161162pub struct CurrencyIdConvert;163impl Convert<AssetIds, Option<MultiLocation>> for CurrencyIdConvert {164	fn convert(id: AssetIds) -> Option<MultiLocation> {165		match id {166			AssetIds::NativeAssetId(NativeCurrency::Here) => Some(MultiLocation::new(167				1,168				X1(Parachain(ParachainInfo::get().into())),169			)),170			AssetIds::NativeAssetId(NativeCurrency::Parent) => Some(MultiLocation::parent()),171			AssetIds::ForeignAssetId(foreign_asset_id) => {172				XcmForeignAssetIdMapping::<Runtime>::get_multi_location(foreign_asset_id)173			}174		}175	}176}177178impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {179	fn convert(location: MultiLocation) -> Option<CurrencyId> {180		if location == MultiLocation::here()181			|| location == MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())))182		{183			return Some(AssetIds::NativeAssetId(NativeCurrency::Here));184		}185186		if location == MultiLocation::parent() {187			return Some(AssetIds::NativeAssetId(NativeCurrency::Parent));188		}189190		if let Some(currency_id) =191			XcmForeignAssetIdMapping::<Runtime>::get_currency_id(location.clone())192		{193			return Some(currency_id);194		}195196		None197	}198}
after · runtime/common/config/xcm/foreignassets.rs
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::{18	traits::{Contains, Get, fungibles},19	parameter_types,20};21use sp_runtime::traits::{Zero, Convert};22use xcm::v1::{Junction::*, MultiLocation, Junctions::*};23use xcm::latest::MultiAsset;24use xcm_builder::{FungiblesAdapter, ConvertedConcreteAssetId};25use xcm_executor::traits::{Convert as ConvertXcm, JustTry, FilterAssetLocation};26use pallet_foreign_assets::{27	AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, NativeCurrency, FreeForAll, TryAsForeign,28	ForeignAssetId, CurrencyId,29};30use sp_std::{borrow::Borrow, marker::PhantomData};31use crate::{Runtime, Balances, ParachainInfo, PolkadotXcm, ForeignAssets};3233use super::{LocationToAccountId, RelayLocation};3435use up_common::types::{AccountId, Balance};3637parameter_types! {38	pub CheckingAccount: AccountId = PolkadotXcm::check_account();39}4041/// Allow checking in assets that have issuance > 0.42pub struct NonZeroIssuance<AccountId, ForeignAssets>(PhantomData<(AccountId, ForeignAssets)>);4344impl<AccountId, ForeignAssets> Contains<<ForeignAssets as fungibles::Inspect<AccountId>>::AssetId>45	for NonZeroIssuance<AccountId, ForeignAssets>46where47	ForeignAssets: fungibles::Inspect<AccountId>,48{49	fn contains(id: &<ForeignAssets as fungibles::Inspect<AccountId>>::AssetId) -> bool {50		!ForeignAssets::total_issuance(*id).is_zero()51	}52}5354pub struct AsInnerId<AssetId, ConvertAssetId>(PhantomData<(AssetId, ConvertAssetId)>);55impl<AssetId: Clone + PartialEq, ConvertAssetId: ConvertXcm<AssetId, AssetId>>56	ConvertXcm<MultiLocation, AssetId> for AsInnerId<AssetId, ConvertAssetId>57where58	AssetId: Borrow<AssetId>,59	AssetId: TryAsForeign<AssetId, ForeignAssetId>,60	AssetIds: Borrow<AssetId>,61{62	fn convert_ref(id: impl Borrow<MultiLocation>) -> Result<AssetId, ()> {63		let id = id.borrow();6465		log::trace!(66			target: "xcm::AsInnerId::Convert",67			"AsInnerId {:?}",68			id69		);7071		let parent = MultiLocation::parent();72		let here = MultiLocation::here();73		let self_location = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));7475		if *id == parent {76			return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent));77		}7879		if *id == here || *id == self_location {80			return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here));81		}8283		match XcmForeignAssetIdMapping::<Runtime>::get_currency_id(id.clone()) {84			Some(AssetIds::ForeignAssetId(foreign_asset_id)) => {85				ConvertAssetId::convert_ref(AssetIds::ForeignAssetId(foreign_asset_id))86			}87			_ => Err(()),88		}89	}9091	fn reverse_ref(what: impl Borrow<AssetId>) -> Result<MultiLocation, ()> {92		log::trace!(93			target: "xcm::AsInnerId::Reverse",94			"AsInnerId",95		);9697		let asset_id = what.borrow();9899		let parent_id =100			ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent)).unwrap();101		let here_id =102			ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here)).unwrap();103104		if asset_id.clone() == parent_id {105			return Ok(MultiLocation::parent());106		}107108		if asset_id.clone() == here_id {109			return Ok(MultiLocation::new(110				1,111				X1(Parachain(ParachainInfo::get().into())),112			));113		}114115		match <AssetId as TryAsForeign<AssetId, ForeignAssetId>>::try_as_foreign(asset_id.clone()) {116			Some(fid) => match XcmForeignAssetIdMapping::<Runtime>::get_multi_location(fid) {117				Some(location) => Ok(location),118				None => Err(()),119			},120			None => Err(()),121		}122	}123}124125/// Means for transacting assets besides the native currency on this chain.126pub type FungiblesTransactor = FungiblesAdapter<127	// Use this fungibles implementation:128	ForeignAssets,129	// Use this currency when it is a fungible asset matching the given location or name:130	ConvertedConcreteAssetId<AssetIds, Balance, AsInnerId<AssetIds, JustTry>, JustTry>,131	// Convert an XCM MultiLocation into a local account id:132	LocationToAccountId,133	// Our chain's account ID type (we can't get away without mentioning it explicitly):134	AccountId,135	// We only want to allow teleports of known assets. We use non-zero issuance as an indication136	// that this asset is known.137	NonZeroIssuance<AccountId, ForeignAssets>,138	// The account to use for tracking teleports.139	CheckingAccount,140>;141142/// Means for transacting assets on this chain.143pub type AssetTransactors = FungiblesTransactor;144145pub struct AllAsset;146impl FilterAssetLocation for AllAsset {147	fn filter_asset_location(_asset: &MultiAsset, _origin: &MultiLocation) -> bool {148		true149	}150}151152pub type IsReserve = AllAsset;153154pub type Trader<T> = FreeForAll<155	pallet_configuration::WeightToFee<T, Balance>,156	RelayLocation,157	AccountId,158	Balances,159	(),160>;161162pub struct CurrencyIdConvert;163impl Convert<AssetIds, Option<MultiLocation>> for CurrencyIdConvert {164	fn convert(id: AssetIds) -> Option<MultiLocation> {165		match id {166			AssetIds::NativeAssetId(NativeCurrency::Here) => Some(MultiLocation::new(167				1,168				X1(Parachain(ParachainInfo::get().into())),169			)),170			AssetIds::NativeAssetId(NativeCurrency::Parent) => Some(MultiLocation::parent()),171			AssetIds::ForeignAssetId(foreign_asset_id) => {172				XcmForeignAssetIdMapping::<Runtime>::get_multi_location(foreign_asset_id)173			}174		}175	}176}177178impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {179	fn convert(location: MultiLocation) -> Option<CurrencyId> {180		if location == MultiLocation::here()181			|| location == MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())))182		{183			return Some(AssetIds::NativeAssetId(NativeCurrency::Here));184		}185186		if location == MultiLocation::parent() {187			return Some(AssetIds::NativeAssetId(NativeCurrency::Parent));188		}189190		if let Some(currency_id) =191			XcmForeignAssetIdMapping::<Runtime>::get_currency_id(location.clone())192		{193			return Some(currency_id);194		}195196		None197	}198}