From 9b72a3a55cc6b17d5fd66724c1e9311c554432b3 Mon Sep 17 00:00:00 2001 From: Dev Date: Mon, 29 Aug 2022 10:07:24 +0000 Subject: [PATCH] Weight trader removed --- --- a/node/cli/src/chain_spec.rs +++ b/node/cli/src/chain_spec.rs @@ -65,7 +65,6 @@ Unknown(String), } - #[cfg(not(feature = "unique-runtime"))] /// PARA_ID for Opal/Quartz const PARA_ID: u32 = 2095; --- a/pallets/foreing-assets/src/lib.rs +++ b/pallets/foreing-assets/src/lib.rs @@ -458,7 +458,7 @@ use xcm::latest::{Fungibility::Fungible as XcmFungible}; -pub struct UsingAnyCurrencyComponents< +pub struct FreeForAll< WeightToFee: WeightToFeePolynomial, AssetId: Get, AccountId, @@ -476,8 +476,7 @@ AccountId, Currency: CurrencyT, OnUnbalanced: OnUnbalancedT, - > WeightTrader - for UsingAnyCurrencyComponents + > WeightTrader for FreeForAll { fn new() -> Self { Self(0, Zero::zero(), PhantomData) @@ -485,46 +484,7 @@ fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result { log::trace!(target: "fassets::weight", "buy_weight weight: {:?}, payment: {:?}", weight, payment); - - let amount: Currency::Balance = (0 as u32).into(); - let u128_amount: u128 = amount.try_into().map_err(|_| XcmError::Overflow)?; - - let asset_id = payment - .fungible - .iter() - .next() - .map_or(Err(XcmError::TooExpensive), |v| Ok(v.0))?; - - // First fungible pays fee - let required = MultiAsset { - id: asset_id.clone(), - fun: XcmFungible(u128_amount), - }; - - log::trace!( - target: "fassets::weight", "buy_weight payment: {:?}, required: {:?}", - payment, required, - ); - - let unused = payment - .checked_sub(required) - .map_err(|_| XcmError::TooExpensive)?; - self.0 = self.0.saturating_add(weight); - self.1 = self.1.saturating_add(amount); - Ok(unused) - } - - fn refund_weight(&mut self, weight: Weight) -> Option { - let weight = weight.min(self.0); - let amount = WeightToFee::weight_to_fee(&weight); - self.0 -= weight; - self.1 = self.1.saturating_sub(amount); - let amount: u128 = amount.saturated_into(); - if amount > 0 { - Some((AssetId::get(), amount).into()) - } else { - None - } + Ok(payment) } } impl< @@ -533,7 +493,7 @@ AccountId, Currency: CurrencyT, OnUnbalanced: OnUnbalancedT, - > Drop for UsingAnyCurrencyComponents + > Drop for FreeForAll { fn drop(&mut self) { OnUnbalanced::on_unbalanced(Currency::issue(self.1)); --- a/pallets/fungible/src/lib.rs +++ b/pallets/fungible/src/lib.rs @@ -171,10 +171,9 @@ /// Foreign collection flag #[pallet::storage] pub type ForeignCollection = - StorageMap; + StorageMap; } - /// Wrapper around untyped collection handle, asserting inner collection is of fungible type. /// Required for interaction with Fungible collections, type safety and implementation [`solidity_interface`][`evm_coder::solidity_interface`]. pub struct FungibleHandle(pallet_common::CollectionHandle); @@ -340,7 +339,7 @@ to: H160::default(), value: amount.into(), } - .to_log(collection_id_to_address(collection.id)), + .to_log(collection_id_to_address(collection.id)), ); >::deposit_event(CommonEvent::ItemDestroyed( collection.id, @@ -560,7 +559,7 @@ to: *user.as_eth(), value: amount.into(), } - .to_log(collection_id_to_address(collection.id)), + .to_log(collection_id_to_address(collection.id)), ); >::deposit_event(CommonEvent::ItemCreated( collection.id, --- a/runtime/common/config/pallets/foreign_asset.rs +++ b/runtime/common/config/pallets/foreign_asset.rs @@ -2,8 +2,8 @@ use up_common::types::AccountId; impl pallet_foreing_assets::Config for Runtime { - type Event = Event; - type Currency = Balances; - type RegisterOrigin = frame_system::EnsureRoot; - type WeightInfo = (); + type Event = Event; + type Currency = Balances; + type RegisterOrigin = frame_system::EnsureRoot; + type WeightInfo = (); } --- a/runtime/common/config/xcm.rs +++ b/runtime/common/config/xcm.rs @@ -16,8 +16,8 @@ use frame_support::{ traits::{ - Contains, tokens::currency::Currency as CurrencyT, OnUnbalanced as OnUnbalancedT, Get, Everything, - fungibles, + Contains, tokens::currency::Currency as CurrencyT, OnUnbalanced as OnUnbalancedT, Get, + Everything, fungibles, }, weights::{Weight, WeightToFeePolynomial, WeightToFee}, parameter_types, match_types, @@ -37,21 +37,23 @@ }; use xcm_builder::{ AccountId32Aliases, AllowTopLevelPaidExecutionFrom, CurrencyAdapter, EnsureXcmOrigin, - FixedWeightBounds, FungiblesAdapter, LocationInverter, NativeAsset, ParentAsSuperuser, RelayChainAsNative, - SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, - SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, ParentIsPreset, - ConvertedConcreteAssetId + FixedWeightBounds, FungiblesAdapter, LocationInverter, NativeAsset, ParentAsSuperuser, + RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, + SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, + ParentIsPreset, ConvertedConcreteAssetId, }; use xcm_executor::{Config, XcmExecutor, Assets}; -use xcm_executor::traits::{Convert as ConvertXcm, JustTry, MatchesFungible, WeightTrader, FilterAssetLocation}; +use xcm_executor::traits::{ + Convert as ConvertXcm, JustTry, MatchesFungible, WeightTrader, FilterAssetLocation, +}; use pallet_foreing_assets::{ - AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, CurrencyId, NativeCurrency, - UsingAnyCurrencyComponents, TryAsForeing, ForeignAssetId, + AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, CurrencyId, NativeCurrency, FreeForAll, + TryAsForeing, ForeignAssetId, }; use sp_std::{borrow::Borrow, marker::PhantomData, vec, vec::Vec}; use crate::{ Runtime, Call, Event, Origin, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, XcmpQueue, - xcm_config::Barrier + xcm_config::Barrier, }; #[cfg(feature = "foreign-assets")] use crate::ForeingAssets; @@ -83,8 +85,22 @@ pub struct OnlySelfCurrency; impl> MatchesFungible for OnlySelfCurrency { fn matches_fungible(a: &MultiAsset) -> Option { + let paraid = Parachain(ParachainInfo::parachain_id().into()); match (&a.id, &a.fun) { - (Concrete(_), XcmFungible(ref amount)) => CheckedConversion::checked_from(*amount), + ( + Concrete(MultiLocation { + parents: 1, + interior: X1(loc), + }), + XcmFungible(ref amount), + ) if paraid == *loc => CheckedConversion::checked_from(*amount), + ( + Concrete(MultiLocation { + parents: 0, + interior: Here, + }), + XcmFungible(ref amount), + ) => CheckedConversion::checked_from(*amount), _ => None, } } @@ -188,48 +204,7 @@ } fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result { - let amount: Currency::Balance = (0 as u32).into(); - //let amount = WeightToFee::weight_to_fee(&weight); - let u128_amount: u128 = amount.try_into().map_err(|_| XcmError::Overflow)?; - - // location to this parachain through relay chain - let option1: xcm::v1::AssetId = Concrete(MultiLocation { - parents: 1, - interior: X1(Parachain(ParachainInfo::parachain_id().into())), - }); - // direct location - let option2: xcm::v1::AssetId = Concrete(MultiLocation { - parents: 0, - interior: Here, - }); - - let required = if payment.fungible.contains_key(&option1) { - (option1, u128_amount).into() - } else if payment.fungible.contains_key(&option2) { - (option2, u128_amount).into() - } else { - (Concrete(MultiLocation::default()), u128_amount).into() - }; - - let unused = payment - .checked_sub(required) - .map_err(|_| XcmError::TooExpensive)?; - self.0 = self.0.saturating_add(weight); - self.1 = self.1.saturating_add(amount); - Ok(unused) - } - - fn refund_weight(&mut self, weight: Weight) -> Option { - let weight = weight.min(self.0); - let amount = WeightToFee::weight_to_fee(&weight); - self.0 -= weight; - self.1 = self.1.saturating_sub(amount); - let amount: u128 = amount.saturated_into(); - if amount > 0 { - Some((AssetId::get(), amount).into()) - } else { - None - } + Ok(payment) } } impl< @@ -255,9 +230,9 @@ #[cfg(feature = "foreign-assets")] impl Contains<>::AssetId> -for NonZeroIssuance - where - ForeingAssets: fungibles::Inspect, + for NonZeroIssuance +where + ForeingAssets: fungibles::Inspect, { fn contains(id: &>::AssetId) -> bool { !ForeingAssets::total_issuance(*id).is_zero() @@ -268,11 +243,11 @@ pub struct AsInnerId(PhantomData<(AssetId, ConvertAssetId)>); #[cfg(feature = "foreign-assets")] impl> -ConvertXcm for AsInnerId - where - AssetId: Borrow, - AssetId: TryAsForeing, - AssetIds: Borrow, + ConvertXcm for AsInnerId +where + AssetId: Borrow, + AssetId: TryAsForeing, + AssetIds: Borrow, { fn convert_ref(id: impl Borrow) -> Result { let id = id.borrow(); @@ -377,10 +352,13 @@ pub type IsReserve = NativeAsset; #[cfg(feature = "foreign-assets")] -type Trader = - UsingAnyCurrencyComponents< - pallet_configuration::WeightToFee, - RelayLocation, AccountId, Balances, ()>; +type Trader = FreeForAll< + pallet_configuration::WeightToFee, + RelayLocation, + AccountId, + Balances, + (), +>; #[cfg(not(feature = "foreign-assets"))] type Trader = UsingOnlySelfCurrencyComponents< pallet_configuration::WeightToFee, @@ -451,4 +429,3 @@ type XcmExecutor = XcmExecutor>; type ExecuteOverweightOrigin = frame_system::EnsureRoot; } - --- a/runtime/opal/src/xcm_config.rs +++ b/runtime/opal/src/xcm_config.rs @@ -32,9 +32,9 @@ v1::{BodyId, Junction::*, Junctions::*, MultiLocation, NetworkId}, }; use xcm_builder::{ - AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, - EnsureXcmOrigin, FixedWeightBounds, FungiblesAdapter, LocationInverter, ParentAsSuperuser, - ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, + AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, EnsureXcmOrigin, + FixedWeightBounds, FungiblesAdapter, LocationInverter, ParentAsSuperuser, ParentIsPreset, + RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, ConvertedConcreteAssetId, }; @@ -49,8 +49,8 @@ }; use crate::{ - Balances, Call, DmpQueue, Event, ForeingAssets, Origin, ParachainInfo, - ParachainSystem, PolkadotXcm, Runtime, XcmpQueue, + Balances, Call, DmpQueue, Event, ForeingAssets, Origin, ParachainInfo, ParachainSystem, + PolkadotXcm, Runtime, XcmpQueue, }; use crate::runtime_common::config::substrate::{TreasuryModuleId, MaxLocks, MaxReserves}; use crate::runtime_common::config::pallets::TreasuryAccountId; @@ -58,8 +58,8 @@ use crate::*; use pallet_foreing_assets::{ - AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, CurrencyId, NativeCurrency, - UsingAnyCurrencyComponents, TryAsForeing, ForeignAssetId, + AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, CurrencyId, NativeCurrency, FreeForAll, + TryAsForeing, ForeignAssetId, }; // Signed version of balance @@ -330,7 +330,7 @@ type Barrier = Barrier; type Weigher = FixedWeightBounds; type Trader = - UsingAnyCurrencyComponents, RelayLocation, AccountId, Balances, ()>; + FreeForAll, RelayLocation, AccountId, Balances, ()>; type ResponseHandler = (); // Don't handle responses for now. type SubscriptionService = PolkadotXcm; type AssetTrap = PolkadotXcm; --- a/runtime/quartz/src/xcm_config.rs +++ b/runtime/quartz/src/xcm_config.rs @@ -50,7 +50,7 @@ }; use pallet_foreing_assets::{ AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, CurrencyId, NativeCurrency, - UsingAnyCurrencyComponents, TryAsForeing, ForeignAssetId, + FreeForAll, TryAsForeing, ForeignAssetId, }; use crate::{ Balances, Call, DmpQueue, Event, Origin, ParachainInfo, --- a/runtime/unique/src/xcm_config.rs +++ b/runtime/unique/src/xcm_config.rs @@ -50,7 +50,7 @@ }; use pallet_foreing_assets::{ AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, CurrencyId, NativeCurrency, - UsingAnyCurrencyComponents, TryAsForeing, ForeignAssetId, + FreeForAll, TryAsForeing, ForeignAssetId, }; use crate::{ Balances, Call, DmpQueue, Event, Origin, ParachainInfo, -- gitstuff