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

difftreelog

source

runtime/common/config/xcm/nativeassets.rs4.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::{18	traits::{tokens::currency::Currency as CurrencyT, OnUnbalanced as OnUnbalancedT, Get},19	weights::WeightToFeePolynomial,20};21use sp_runtime::traits::{CheckedConversion, Zero, Convert};22use xcm::latest::{23	AssetId::{Concrete},24	Fungibility::Fungible as XcmFungible,25	MultiAsset, Error as XcmError, Weight,26	Junction::*,27	MultiLocation,28	Junctions::*,29};30use xcm_builder::{CurrencyAdapter, NativeAsset};31use xcm_executor::{32	Assets,33	traits::{MatchesFungible, WeightTrader},34};35use pallet_foreign_assets::{AssetIds, NativeCurrency};36use sp_std::marker::PhantomData;37use crate::{Balances, ParachainInfo};38use super::{LocationToAccountId, RelayLocation};3940use up_common::types::{AccountId, Balance};4142pub struct OnlySelfCurrency;43impl<B: TryFrom<u128>> MatchesFungible<B> for OnlySelfCurrency {44	fn matches_fungible(a: &MultiAsset) -> Option<B> {45		let paraid = Parachain(ParachainInfo::parachain_id().into());46		match (&a.id, &a.fun) {47			(48				Concrete(MultiLocation {49					parents: 1,50					interior: X1(loc),51				}),52				XcmFungible(ref amount),53			) if paraid == *loc => CheckedConversion::checked_from(*amount),54			(55				Concrete(MultiLocation {56					parents: 0,57					interior: Here,58				}),59				XcmFungible(ref amount),60			) => CheckedConversion::checked_from(*amount),61			_ => None,62		}63	}64}6566/// Means for transacting assets on this chain.67pub type LocalAssetTransactor = CurrencyAdapter<68	// Use this currency:69	Balances,70	// Use this currency when it is a fungible asset matching the given location or name:71	OnlySelfCurrency,72	// Do a simple punn to convert an AccountId32 MultiLocation into a native chain account ID:73	LocationToAccountId,74	// Our chain's account ID type (we can't get away without mentioning it explicitly):75	AccountId,76	// We don't track any teleports.77	(),78>;7980pub type AssetTransactor = LocalAssetTransactor;8182pub type IsReserve = NativeAsset;8384pub struct UsingOnlySelfCurrencyComponents<85	WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,86	AssetId: Get<MultiLocation>,87	AccountId,88	Currency: CurrencyT<AccountId>,89	OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,90>(91	Weight,92	Currency::Balance,93	PhantomData<(WeightToFee, AssetId, AccountId, Currency, OnUnbalanced)>,94);95impl<96		WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,97		AssetId: Get<MultiLocation>,98		AccountId,99		Currency: CurrencyT<AccountId>,100		OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,101	> WeightTrader102	for UsingOnlySelfCurrencyComponents<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced>103{104	fn new() -> Self {105		// FIXME: benchmark106		Self(Weight::from_parts(0, 0), Zero::zero(), PhantomData)107	}108109	fn buy_weight(&mut self, _weight: Weight, payment: Assets) -> Result<Assets, XcmError> {110		Ok(payment)111	}112}113impl<114		WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,115		AssetId: Get<MultiLocation>,116		AccountId,117		Currency: CurrencyT<AccountId>,118		OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,119	> Drop120	for UsingOnlySelfCurrencyComponents<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced>121{122	fn drop(&mut self) {123		OnUnbalanced::on_unbalanced(Currency::issue(self.1));124	}125}126127pub type Trader<T> = UsingOnlySelfCurrencyComponents<128	pallet_configuration::WeightToFee<T, Balance>,129	RelayLocation,130	AccountId,131	Balances,132	(),133>;134135pub struct CurrencyIdConvert;136impl Convert<AssetIds, Option<MultiLocation>> for CurrencyIdConvert {137	fn convert(id: AssetIds) -> Option<MultiLocation> {138		match id {139			AssetIds::NativeAssetId(NativeCurrency::Here) => Some(MultiLocation::new(140				1,141				X1(Parachain(ParachainInfo::get().into())),142			)),143			_ => None,144		}145	}146}