From 8374ba0c5da2757967ade91d079502a047854599 Mon Sep 17 00:00:00 2001 From: Patricio Napoli Date: Fri, 14 Jul 2023 01:44:08 -0300 Subject: [PATCH] avoided AssetConversionAdapter, now using Swap trait --- .../assets/asset-hub-westend/src/lib.rs | 11 +- .../asset-hub-westend/src/xcm_config.rs | 9 +- primitives/utility/Cargo.toml | 3 +- primitives/utility/src/lib.rs | 114 ++++++------------ 4 files changed, 43 insertions(+), 94 deletions(-) diff --git a/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index 7f0f4c80340..c181e63c289 100644 --- a/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -36,17 +36,11 @@ use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use frame_support::{construct_runtime, dispatch::DispatchClass, ord_parameter_types, parameter_types, traits::{ tokens::nonfungibles_v2::Inspect, AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, InstanceFilter, -}, weights::{ConstantMultiplier, Weight}, BoundedVec, PalletId, RuntimeDebug, ensure}; -use frame_support::traits::fungible; -use frame_support::traits::tokens::ConversionToAssetBalance; +}, weights::{ConstantMultiplier, Weight}, BoundedVec, PalletId, RuntimeDebug}; use frame_system::{ limits::{BlockLength, BlockWeights}, EnsureRoot, EnsureSigned, EnsureSignedBy, }; -use pallet_asset_conversion::MultiAssetIdConverter; -use pallet_asset_conversion_tx_payment::AssetConversionAdapter; -use pallet_asset_conversion_tx_payment::InitialPayment::Asset; -use pallet_assets::ConversionError; use pallet_nfts::PalletFeatures; pub use parachains_common as common; use parachains_common::{ @@ -56,7 +50,7 @@ use parachains_common::{ }; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; -use sp_runtime::{create_runtime_str, generic, impl_opaque_keys, traits::{AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, Verify}, transaction_validity::{TransactionSource, TransactionValidity}, ApplyExtrinsicResult, Permill, FixedPointOperand, FixedU128, FixedPointNumber}; +use sp_runtime::{create_runtime_str, generic, impl_opaque_keys, traits::{AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, Verify}, transaction_validity::{TransactionSource, TransactionValidity}, ApplyExtrinsicResult, Permill}; use sp_std::prelude::*; #[cfg(feature = "std")] use sp_version::NativeVersion; @@ -75,7 +69,6 @@ use assets_common::{ foreign_creators::ForeignCreators, matching::FromSiblingParachain, MultiLocationForAssetId, }; use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate}; -use sp_runtime::traits::Zero; use xcm_executor::XcmExecutor; use crate::xcm_config::ForeignCreatorsSovereignAccountOf; diff --git a/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs b/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs index dd456923c97..ba874b97ee6 100644 --- a/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs +++ b/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs @@ -18,7 +18,7 @@ use super::{ ParachainSystem, PolkadotXcm, PoolAssets, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, TrustBackedAssetsInstance, WeightToFee, XcmpQueue, }; -use crate::{ForeignAssets}; +use crate::ForeignAssets; use assets_common::matching::{ FromSiblingParachain, IsForeignConcreteAsset, StartsWith, StartsWithExplicitGlobalConsensus, }; @@ -27,7 +27,6 @@ use frame_support::{ traits::{ConstU32, Contains, Everything, Nothing, PalletInfoAccess}, }; use frame_system::EnsureRoot; -use pallet_asset_conversion_tx_payment::AssetConversionAdapter; use pallet_xcm::XcmPassthrough; use parachains_common::{impls::ToStakingPot, xcm_config::AssetFeeAsExistentialDepositMultiplier}; use polkadot_parachain::primitives::Sibling; @@ -443,12 +442,10 @@ impl xcm_executor::Config for XcmConfig { type Trader = ( UsingComponents>, cumulus_primitives_utility::SwapFirstAssetTrader< - AccountId, - LocationToAccountId, - Balance, Runtime, + LocationToAccountId, + pallet_asset_conversion::Pallet, WeightToFee, - AssetConversionAdapter, MultiAssetsConvertedConcreteId, LocalAndForeignAssets, cumulus_primitives_utility::XcmFeesTo32ByteAccount< diff --git a/primitives/utility/Cargo.toml b/primitives/utility/Cargo.toml index c368e2777a5..b1930d99d80 100644 --- a/primitives/utility/Cargo.toml +++ b/primitives/utility/Cargo.toml @@ -10,7 +10,7 @@ log = { version = "0.4.19", default-features = false } # Substrate frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } -pallet-asset-conversion-tx-payment = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } +pallet-asset-conversion = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } @@ -30,6 +30,7 @@ default = [ "std" ] std = [ "codec/std", "frame-support/std", + "pallet-asset-conversion/std", "sp-runtime/std", "sp-std/std", "sp-io/std", diff --git a/primitives/utility/src/lib.rs b/primitives/utility/src/lib.rs index fc37645b7f8..a4629f7ad05 100644 --- a/primitives/utility/src/lib.rs +++ b/primitives/utility/src/lib.rs @@ -28,8 +28,7 @@ use frame_support::{ }, weights::Weight, }; -use pallet_asset_conversion_tx_payment::OnChargeAssetTransaction; -use frame_support::traits::fungibles::SwapNative; +use pallet_asset_conversion::{Swap, Config, MultiAssetIdConverter}; use polkadot_runtime_common::xcm_sender::ConstantPrice; use sp_runtime::{traits::Saturating, SaturatedConversion}; use sp_std::{marker::PhantomData, prelude::*}; @@ -280,31 +279,28 @@ struct SwapAssetTraderRefunder { } pub struct SwapFirstAssetTrader< - AccountId, - AccountIdConverter: ConvertLocation, - Balance, - Runtime: pallet_asset_conversion_tx_payment::Config, - WeightToFee: frame_support::weights::WeightToFee, - ChargeAssetTransaction: OnChargeAssetTransaction, + T: Config, + AccountIdConverter: ConvertLocation, + SWP: Swap, + WeightToFee: frame_support::weights::WeightToFee, Matcher: MatchesFungibles, - ConcreteAssets: fungibles::Mutate + fungibles::Balanced, + ConcreteAssets: fungibles::Mutate + fungibles::Balanced, HandleRefund: TakeRevenue, >( Option, - PhantomData<(AccountId, AccountIdConverter, Balance, Runtime, WeightToFee, ChargeAssetTransaction, Matcher, ConcreteAssets, HandleRefund)>, + PhantomData<(T, AccountIdConverter, SWP, WeightToFee, Matcher, ConcreteAssets, HandleRefund)>, ); + impl< - AccountId, - AccountIdConverter: ConvertLocation, - Balance, - Runtime: pallet_asset_conversion_tx_payment::Config, - WeightToFee: frame_support::weights::WeightToFee, - ChargeAssetTransaction: OnChargeAssetTransaction, + T: Config, + AccountIdConverter: ConvertLocation, + SWP: Swap, + WeightToFee: frame_support::weights::WeightToFee, Matcher: MatchesFungibles, - ConcreteAssets: fungibles::Mutate + fungibles::Balanced, + ConcreteAssets: fungibles::Mutate + fungibles::Balanced, HandleRefund: TakeRevenue, > WeightTrader -for SwapFirstAssetTrader +for SwapFirstAssetTrader { fn new() -> Self { Self(None, PhantomData) @@ -342,82 +338,44 @@ for SwapFirstAssetTrader, - // Downstream functions IGNORE this runtime dispatch info argument, what is a proper value? - , - local_asset_id, - fee, - 0 - ).unwrap(); + // let (asset_balance) = SWP::swap_exact_tokens_for_tokens( + // acc, + // vec![first, T::MultiAssetIdConverter::get_native()], + // payment, + // Some(fee), + // acc, + // true + // ).map_err(|_| XcmError::AssetNotFound)?; // Convert to the same kind of multiasset, with the required fungible balance - let required = first.id.into_multiasset(asset_balance.into()); + // let required = first.id.into_multiasset(asset_balance.into()); // Substract payment - let unused = payment.checked_sub(required.clone()).map_err(|_| XcmError::TooExpensive)?; + // let unused = payment.checked_sub(required.clone()).map_err(|_| XcmError::TooExpensive)?; // record weight and multiasset - self.0 = Some(SwapAssetTraderRefunder { - outstanding_concrete_asset: required, - }); + // self.0 = Some(SwapAssetTraderRefunder { + // outstanding_concrete_asset: required, + // }); - Ok(unused) + Err(XcmError::AssetNotFound) } fn refund_weight(&mut self, ctx: &XcmContext, weight: Weight) -> Option { log::trace!(target: "xcm::weight", "SwapFirstAssetTrader::refund_weight weight: {:?}", weight); - - if let Some(SwapAssetTraderRefunder { - outstanding_concrete_asset: MultiAsset { id, fun }, - }) = self.0.clone() - { - // Get the local asset id in which we can refund fees - let (local_asset_id, outstanding_balance) = - Matcher::matches_fungibles(&(id, fun).into()).ok()?; - - // Re-calculate asset swap from native to MultiAsset provided - // ... - // TODO - - // Convert balances into u128 - let outstanding_minus_substracted: u128 = - outstanding_minus_substracted.saturated_into(); - let asset_balance: u128 = asset_balance.saturated_into(); - - // Construct outstanding_concrete_asset with the same location id and substracted balance - let outstanding_concrete_asset: MultiAsset = (id, outstanding_minus_substracted).into(); - - // Override SwapAssetTraderRefunder - self.0 = Some(SwapAssetTraderRefunder { outstanding_concrete_asset }); - - // Only refund if positive - if asset_balance > 0 { - return Some((id, asset_balance).into()) - } - } - None } } impl< - AccountId, - AccountIdConverter: ConvertLocation, - Balance, - Runtime: pallet_asset_conversion_tx_payment::Config, - WeightToFee: frame_support::weights::WeightToFee, - ChargeAssetTransaction: OnChargeAssetTransaction, + T: Config, + AccountIdConverter: ConvertLocation, + SWP: Swap, + WeightToFee: frame_support::weights::WeightToFee, Matcher: MatchesFungibles, - ConcreteAssets: fungibles::Mutate + fungibles::Balanced, + ConcreteAssets: fungibles::Mutate + fungibles::Balanced, HandleRefund: TakeRevenue, -> Drop for SwapFirstAssetTrader +> Drop for SwapFirstAssetTrader { fn drop(&mut self) { if let Some(asset_trader) = self.0.clone() { @@ -692,9 +650,9 @@ mod tests { let weight_to_buy = Weight::from_parts(1_000, 1_000); // lets do first call (success) - assert_ok!(trader.buy_weight(weight_to_buy, payment.clone())); + assert_ok!(trader.buy_weight(&XcmContext::with_message_id([0; 32]), weight_to_buy, payment.clone())); // lets do second call (error) - assert_eq!(trader.buy_weight(weight_to_buy, payment), Err(XcmError::NotWithdrawable)); + assert_eq!(trader.buy_weight(&XcmContext::with_message_id([0; 32]), weight_to_buy, payment), Err(XcmError::NotWithdrawable)); } }