Skip to content

Commit

Permalink
Deducting foreign tokens as an execution fee
Browse files Browse the repository at this point in the history
  • Loading branch information
imstar15 committed Nov 7, 2023
1 parent 9c7e873 commit 1be03c5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
4 changes: 2 additions & 2 deletions pallets/automation-price/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ where

let currency_id = T::CurrencyIdConvert::convert(self.schedule_fee_location)
.ok_or("IncoveribleMultilocation")?;
let currency_id = currency_id.into();

match T::MultiCurrency::withdraw(currency_id, &self.owner, fee) {
match T::MultiCurrency::withdraw(currency_id.into(), &self.owner, fee) {
Ok(_) => {
TR::take_revenue(MultiAsset {
id: AssetId::Concrete(self.schedule_fee_location),
Expand All @@ -112,6 +111,7 @@ where

if self.execution_fee_amount > MultiBalanceOf::<T>::zero() {
T::XcmpTransactor::pay_xcm_fee(
currency_id,
self.owner.clone(),
self.execution_fee_amount.saturated_into(),
)?;
Expand Down
17 changes: 8 additions & 9 deletions pallets/automation-time/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ where

let execution_fee_amount = execution_fee.amount;
if !execution_fee_amount.is_zero() {
T::MultiCurrency::withdraw(
currency_id.into(),
&self.owner,
execution_fee_amount,
)
.map_err(|_| DispatchError::Token(BelowMinimum))?;
// T::MultiCurrency::withdraw(
// currency_id.into(),
// &self.owner,
// execution_fee_amount,
// )
// .map_err(|_| DispatchError::Token(BelowMinimum))?;

T::XcmpTransactor::pay_xcm_fee(
currency_id,
self.owner.clone(),
execution_fee_amount.saturated_into(),
)?;
Expand All @@ -177,9 +178,7 @@ where
Pallet::<T>::calculate_schedule_fee_amount(action, executions)?.saturated_into();

let execution_fee = match action.clone() {
Action::XCMP { execution_fee, instruction_sequence, .. }
if instruction_sequence == InstructionSequence::PayThroughSovereignAccount =>
{
Action::XCMP { execution_fee, instruction_sequence, .. } => {
let location = MultiLocation::try_from(execution_fee.asset_location)
.map_err(|()| Error::<T>::BadVersion)?;
let amount =
Expand Down
40 changes: 28 additions & 12 deletions pallets/xcmp-handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,24 @@ use xcm::latest::prelude::*;
pub mod pallet {
use super::*;
use frame_support::traits::Currency;
use orml_traits::MultiCurrency;
use polkadot_parachain::primitives::Sibling;
use sp_runtime::traits::{AccountIdConversion, Convert, SaturatedConversion};
use sp_std::prelude::*;
use xcm_executor::traits::WeightBounds;

pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub type MultiCurrencyId<T> = <<T as Config>::MultiCurrency as MultiCurrency<
<T as frame_system::Config>::AccountId,
>>::CurrencyId;

#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

type RuntimeCall: From<Call<Self>> + Encode;

/// The Currency type for interacting with balances
type Currency: Currency<Self::AccountId>;
/// The MultiCurrency type for interacting with balances
type MultiCurrency: MultiCurrency<Self::AccountId>;

/// The currencyIds that our chain supports.
type CurrencyId: Parameter
Expand All @@ -71,7 +73,9 @@ pub mod pallet {
+ MaybeSerializeDeserialize
+ Ord
+ TypeInfo
+ MaxEncodedLen;
+ MaxEncodedLen
+ From<MultiCurrencyId<Self>>
+ Into<MultiCurrencyId<Self>>;

/// The currencyId for the native currency.
#[pallet::constant]
Expand Down Expand Up @@ -408,15 +412,19 @@ pub mod pallet {
/// Pay for XCMP fees.
/// Transfers fee from payer account to the local chain sovereign account.
///
pub fn pay_xcm_fee(source: T::AccountId, fee: u128) -> Result<(), DispatchError> {
pub fn pay_xcm_fee(
currency_id: T::CurrencyId,
source: T::AccountId,
fee: u128,
) -> Result<(), DispatchError> {
let local_sovereign_account =
Sibling::from(T::SelfParaId::get()).into_account_truncating();

match T::Currency::transfer(
match T::MultiCurrency::transfer(
currency_id.into(),
&source,
&local_sovereign_account,
<BalanceOf<T>>::saturated_from(fee),
frame_support::traits::ExistenceRequirement::KeepAlive,
fee.saturated_into(),
) {
Ok(_number) => Self::deposit_event(Event::XcmFeesPaid {
source,
Expand Down Expand Up @@ -446,7 +454,11 @@ pub trait XcmpTransactor<AccountId, CurrencyId> {
flow: InstructionSequence,
) -> Result<(), sp_runtime::DispatchError>;

fn pay_xcm_fee(source: AccountId, fee: u128) -> Result<(), sp_runtime::DispatchError>;
fn pay_xcm_fee(
currency_id: CurrencyId,
source: AccountId,
fee: u128,
) -> Result<(), sp_runtime::DispatchError>;
}

impl<T: Config> XcmpTransactor<T::AccountId, T::CurrencyId> for Pallet<T> {
Expand Down Expand Up @@ -474,8 +486,12 @@ impl<T: Config> XcmpTransactor<T::AccountId, T::CurrencyId> for Pallet<T> {
Ok(())
}

fn pay_xcm_fee(source: T::AccountId, fee: u128) -> Result<(), sp_runtime::DispatchError> {
Self::pay_xcm_fee(source, fee)?;
fn pay_xcm_fee(
currency_id: T::CurrencyId,
source: T::AccountId,
fee: u128,
) -> Result<(), sp_runtime::DispatchError> {
Self::pay_xcm_fee(currency_id, source, fee)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/neumann/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl pallet_xcmp_handler::Config for Runtime {
type RuntimeCall = RuntimeCall;
type CurrencyId = TokenId;
type GetNativeCurrencyId = GetNativeCurrencyId;
type Currency = pallet_balances::Pallet<Runtime>;
type MultiCurrency = Currencies;
type SelfParaId = parachain_info::Pallet<Runtime>;
type AccountIdToMultiLocation = AccountIdToMultiLocation;
type CurrencyIdToMultiLocation = TokenIdConvert;
Expand Down
2 changes: 1 addition & 1 deletion runtime/oak/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ parameter_types! {
impl pallet_xcmp_handler::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = pallet_balances::Pallet<Runtime>;
type MultiCurrency = Currencies;
type CurrencyId = TokenId;
type GetNativeCurrencyId = GetNativeCurrencyId;
type SelfParaId = parachain_info::Pallet<Runtime>;
Expand Down
2 changes: 1 addition & 1 deletion runtime/turing/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ parameter_types! {
impl pallet_xcmp_handler::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = pallet_balances::Pallet<Runtime>;
type MultiCurrency = Currencies;
type CurrencyId = TokenId;
type GetNativeCurrencyId = GetNativeCurrencyId;
type SelfParaId = parachain_info::Pallet<Runtime>;
Expand Down

0 comments on commit 1be03c5

Please sign in to comment.