From 29c6332022c2b84a19de14356e4adc942484ea64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCn=20=C3=96zerk?= Date: Wed, 17 Jul 2024 16:03:45 +0300 Subject: [PATCH] Feemanager for evm (#239) --- .../runtime/src/configs/xcm_config.rs | 70 ++++++++++++++++--- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/evm-template/runtime/src/configs/xcm_config.rs b/evm-template/runtime/src/configs/xcm_config.rs index 6d2b451..98f962b 100644 --- a/evm-template/runtime/src/configs/xcm_config.rs +++ b/evm-template/runtime/src/configs/xcm_config.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + use frame_support::{ parameter_types, traits::{ConstU32, Contains, Everything, Nothing, PalletInfoAccess}, @@ -5,17 +7,21 @@ use frame_support::{ }; use frame_system::EnsureRoot; use pallet_xcm::XcmPassthrough; -use polkadot_parachain_primitives::primitives::Sibling; -use xcm::latest::prelude::*; +use polkadot_parachain_primitives::primitives::{self, Sibling}; +use xcm::latest::prelude::{Assets as XcmAssets, *}; use xcm_builder::{ AccountKey20Aliases, AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom, ConvertedConcreteId, DenyReserveTransferToRelayChain, DenyThenTry, EnsureXcmOrigin, - FixedWeightBounds, FrameTransactionalProcessor, FungibleAdapter, FungiblesAdapter, IsConcrete, - NativeAsset, NoChecking, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, - SiblingParachainConvertsVia, SignedAccountKey20AsNative, SovereignSignedViaLocation, - TakeWeightCredit, TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic, + FixedWeightBounds, FrameTransactionalProcessor, FungibleAdapter, FungiblesAdapter, HandleFee, + IsChildSystemParachain, IsConcrete, NativeAsset, NoChecking, ParentIsPreset, + RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, + SignedAccountKey20AsNative, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId, + UsingComponents, WithComputedOrigin, WithUniqueTopic, XcmFeeManagerFromComponents, +}; +use xcm_executor::{ + traits::{FeeReason, JustTry, TransactAsset}, + XcmExecutor, }; -use xcm_executor::{traits::JustTry, XcmExecutor}; use xcm_primitives::AsAssetType; use crate::{ @@ -25,6 +31,7 @@ use crate::{ }, types::{AccountId, AssetId, Balance, XcmFeesToAccount}, weights, AllPalletsWithSystem, AssetManager, Assets, Balances, ParachainInfo, PolkadotXcm, + Treasury, }; parameter_types! { @@ -141,6 +148,50 @@ pub type Barrier = TrailingSetTopicAsId< >, >; +/// A `HandleFee` implementation that simply deposits the fees into a specific on-chain +/// `ReceiverAccount`. +/// +/// It reuses the `AssetTransactor` configured on the XCM executor to deposit fee assets. If +/// the `AssetTransactor` returns an error while calling `deposit_asset`, then a warning will be +/// logged and the fee burned. +pub struct XcmFeeToAccount( + PhantomData<(AssetTransactor, AccountId, ReceiverAccount)>, +); +impl< + AssetTransactor: TransactAsset, + AccountId: Clone + Into<[u8; 20]>, + ReceiverAccount: Get, + > HandleFee for XcmFeeToAccount +{ + fn handle_fee(fee: XcmAssets, context: Option<&XcmContext>, _reason: FeeReason) -> XcmAssets { + deposit_or_burn_fee::(fee, context, ReceiverAccount::get()); + + XcmAssets::new() + } +} + +pub fn deposit_or_burn_fee>( + fee: XcmAssets, + context: Option<&XcmContext>, + receiver: AccountId, +) { + let dest = AccountKey20 { network: None, key: receiver.into() }.into(); + for asset in fee.into_inner() { + if let Err(e) = AssetTransactor::deposit_asset(&asset, &dest, context) { + log::trace!( + target: "xcm::fees", + "`AssetTransactor::deposit_asset` returned error: {:?}. Burning fee: {:?}. \ + They might be burned.", + e, asset, + ); + } + } +} + +parameter_types! { + pub TreasuryAccount: AccountId = Treasury::account_id(); +} + pub struct XcmConfig; impl xcm_executor::Config for XcmConfig { type Aliasers = Nothing; @@ -152,7 +203,10 @@ impl xcm_executor::Config for XcmConfig { type AssetTrap = PolkadotXcm; type Barrier = Barrier; type CallDispatcher = RuntimeCall; - type FeeManager = (); + type FeeManager = XcmFeeManagerFromComponents< + IsChildSystemParachain, + XcmFeeToAccount, + >; type HrmpChannelAcceptedHandler = (); type HrmpChannelClosingHandler = (); type HrmpNewChannelOpenRequestHandler = ();