1234567891011121314151617use frame_support::{18 traits::{tokens::currency::Currency as CurrencyT, OnUnbalanced as OnUnbalancedT, Get},19 weights::{Weight, WeightToFeePolynomial},20};21use sp_runtime::traits::{CheckedConversion, Zero};22use xcm::v1::{Junction::*, MultiLocation, Junctions::*};23use xcm::latest::{24 AssetId::{Concrete},25 Fungibility::Fungible as XcmFungible,26 MultiAsset, Error as XcmError,27};28use xcm_builder::{CurrencyAdapter, NativeAsset};29use xcm_executor::{30 Assets,31 traits::{MatchesFungible, WeightTrader},32};33use sp_std::marker::PhantomData;34use crate::{Balances, ParachainInfo};35use super::{LocationToAccountId, RelayLocation};3637use up_common::types::{AccountId, Balance};3839pub struct OnlySelfCurrency;40impl<B: TryFrom<u128>> MatchesFungible<B> for OnlySelfCurrency {41 fn matches_fungible(a: &MultiAsset) -> Option<B> {42 let paraid = Parachain(ParachainInfo::parachain_id().into());43 match (&a.id, &a.fun) {44 (45 Concrete(MultiLocation {46 parents: 1,47 interior: X1(loc),48 }),49 XcmFungible(ref amount),50 ) if paraid == *loc => CheckedConversion::checked_from(*amount),51 (52 Concrete(MultiLocation {53 parents: 0,54 interior: Here,55 }),56 XcmFungible(ref amount),57 ) => CheckedConversion::checked_from(*amount),58 _ => None,59 }60 }61}626364pub type LocalAssetTransactor = CurrencyAdapter<65 66 Balances,67 68 OnlySelfCurrency,69 70 LocationToAccountId,71 72 AccountId,73 74 (),75>;7677pub type AssetTransactors = LocalAssetTransactor;7879pub type IsReserve = NativeAsset;8081pub struct UsingOnlySelfCurrencyComponents<82 WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,83 AssetId: Get<MultiLocation>,84 AccountId,85 Currency: CurrencyT<AccountId>,86 OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,87>(88 Weight,89 Currency::Balance,90 PhantomData<(WeightToFee, AssetId, AccountId, Currency, OnUnbalanced)>,91);92impl<93 WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,94 AssetId: Get<MultiLocation>,95 AccountId,96 Currency: CurrencyT<AccountId>,97 OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,98 > WeightTrader99 for UsingOnlySelfCurrencyComponents<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced>100{101 fn new() -> Self {102 Self(0, Zero::zero(), PhantomData)103 }104105 fn buy_weight(&mut self, _weight: Weight, payment: Assets) -> Result<Assets, XcmError> {106 Ok(payment)107 }108}109impl<110 WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,111 AssetId: Get<MultiLocation>,112 AccountId,113 Currency: CurrencyT<AccountId>,114 OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,115 > Drop116 for UsingOnlySelfCurrencyComponents<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced>117{118 fn drop(&mut self) {119 OnUnbalanced::on_unbalanced(Currency::issue(self.1));120 }121}122123pub type Trader<T> = UsingOnlySelfCurrencyComponents<124 pallet_configuration::WeightToFee<T, Balance>,125 RelayLocation,126 AccountId,127 Balances,128 (),129>;