Skip to content

Commit

Permalink
Change the type of schedule_fee to FeePayment
Browse files Browse the repository at this point in the history
  • Loading branch information
imstar15 committed Nov 10, 2023
1 parent c2260c4 commit b6392d5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
42 changes: 26 additions & 16 deletions pallets/automation-time/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ pub trait HandleFees<T: Config> {
pub struct FeePayment<T: Config> {
pub asset_location: MultiLocation,
pub amount: MultiBalanceOf<T>,
pub is_local_deduction: bool,
pub is_local: bool,
}

pub struct FeeHandler<T: Config, TR> {
owner: T::AccountId,
pub schedule_fee_location: MultiLocation,
pub schedule_fee_amount: MultiBalanceOf<T>,
pub schedule_fee: FeePayment<T>,
pub execution_fee: Option<FeePayment<T>>,
_phantom_data: PhantomData<TR>,
}
Expand Down Expand Up @@ -109,15 +108,16 @@ where
// If the locations of schedule_fee and execution_fee are equal,
// we need to add the fees to check whether they are sufficient,
// otherwise check them separately.
if exec_fee.is_local_deduction && exec_fee.asset_location == self.schedule_fee_location
if exec_fee.is_local_deduction &&
exec_fee.asset_location == self.schedule_fee.asset_location
{
let fee = self.schedule_fee_amount.saturating_add(exec_fee.amount);
let fee = self.schedule_fee.amount.saturating_add(exec_fee.amount);
Self::ensure_can_withdraw(self, exec_fee.asset_location, fee)?;
} else {
Self::ensure_can_withdraw(
self,
self.schedule_fee_location,
self.schedule_fee_amount,
self.schedule_fee.asset_location,
self.schedule_fee.amount,
)?;
Self::ensure_can_withdraw(self, exec_fee.asset_location, exec_fee.amount)?;
}
Expand All @@ -129,16 +129,16 @@ where
/// Withdraw the fee.
fn withdraw_fee(&self) -> Result<(), DispatchError> {
// Withdraw schedule fee
if !self.schedule_fee_amount.is_zero() {
let currency_id = T::CurrencyIdConvert::convert(self.schedule_fee_location)
if !self.schedule_fee.amount.is_zero() {
let currency_id = T::CurrencyIdConvert::convert(self.schedule_fee.asset_location)
.ok_or("InconvertibleMultilocation")?;

T::MultiCurrency::withdraw(currency_id.into(), &self.owner, self.schedule_fee_amount)
T::MultiCurrency::withdraw(currency_id.into(), &self.owner, self.schedule_fee.amount)
.map_err(|_| DispatchError::Token(BelowMinimum))?;

TR::take_revenue(MultiAsset {
id: AssetId::Concrete(self.schedule_fee_location),
fun: Fungibility::Fungible(self.schedule_fee_amount.saturated_into()),
id: AssetId::Concrete(self.schedule_fee.asset_location),
fun: Fungibility::Fungible(self.schedule_fee.amount.saturated_into()),
});
}

Expand Down Expand Up @@ -180,21 +180,31 @@ where
let schedule_fee_amount: u128 =
Pallet::<T>::calculate_schedule_fee_amount(action, executions)?.saturated_into();

let schedule_fee = FeePayment {
asset_location: schedule_fee_location,
amount: schedule_fee_amount.saturated_into(),
is_local_deduction: true,
};

let execution_fee = match action.clone() {
Action::XCMP { execution_fee, .. } => {
Action::XCMP { execution_fee, instruction_sequence, .. } => {
let location = MultiLocation::try_from(execution_fee.asset_location)
.map_err(|()| Error::<T>::BadVersion)?;
let amount =
execution_fee.amount.saturating_mul(executions.into()).saturated_into();
Some(FeePayment { asset_location: location, amount, is_local_deduction: true })
Some(FeePayment {
asset_location: location,
amount,
is_local_deduction: instruction_sequence ==
InstructionSequence::PayThroughSovereignAccount,
})
},
_ => None,
};

Ok(Self {
owner: owner.clone(),
schedule_fee_location,
schedule_fee_amount: schedule_fee_amount.saturated_into(),
schedule_fee,
execution_fee,
_phantom_data: Default::default(),
})
Expand Down
9 changes: 6 additions & 3 deletions runtime/neumann/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ use xcm::latest::{prelude::*, MultiLocation, NetworkId};
use xcm_builder::Account32Hash;
use xcm_executor::traits::Convert;

use orml_traits::location::AbsoluteReserveProvider;

use frame_support::{
construct_runtime,
dispatch::DispatchClass,
Expand Down Expand Up @@ -81,7 +83,7 @@ use polkadot_runtime_common::BlockHashCount;

// XCM configurations.
pub mod xcm_config;
use xcm_config::{FeePerSecondProvider, ToTreasury, TokenIdConvert};
use xcm_config::{FeePerSecondProvider, SelfLocation, ToTreasury, TokenIdConvert};

pub mod weights;

Expand Down Expand Up @@ -919,8 +921,9 @@ impl pallet_automation_time::Config for Runtime {
type ScheduleAllowList = ScheduleAllowList;
type EnsureProxy = AutomationEnsureProxy;
type UniversalLocation = UniversalLocation;
type SelfParaId = parachain_info::Pallet<Runtime>;
type TransferCallCreator = MigrationTransferCallCreator;
type ReserveProvider = AbsoluteReserveProvider;
type SelfLocation = SelfLocation;
}

impl pallet_automation_price::Config for Runtime {
Expand Down Expand Up @@ -1215,7 +1218,7 @@ impl_runtime_apis! {

let execution_fee = fee_handler.execution_fee.map(|fee| fee.amount).unwrap_or(0);
Ok(AutomationFeeDetails {
schedule_fee: fee_handler.schedule_fee_amount,
schedule_fee: fee_handler.schedule_fee.amount,
execution_fee,
})
}
Expand Down
9 changes: 6 additions & 3 deletions runtime/oak/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ use xcm::latest::{prelude::*, MultiLocation};
use xcm_builder::Account32Hash;
use xcm_executor::traits::Convert;

use orml_traits::location::AbsoluteReserveProvider;

use sp_std::{cmp::Ordering, prelude::*};
#[cfg(feature = "std")]
use sp_version::NativeVersion;
Expand Down Expand Up @@ -87,7 +89,7 @@ use polkadot_runtime_common::BlockHashCount;

// XCM configurations.
pub mod xcm_config;
use xcm_config::{FeePerSecondProvider, ToTreasury, TokenIdConvert};
use xcm_config::{FeePerSecondProvider, SelfLocation, ToTreasury, TokenIdConvert};

pub mod weights;

Expand Down Expand Up @@ -944,8 +946,9 @@ impl pallet_automation_time::Config for Runtime {
type ScheduleAllowList = ScheduleAllowList;
type EnsureProxy = AutomationEnsureProxy;
type UniversalLocation = UniversalLocation;
type SelfParaId = parachain_info::Pallet<Runtime>;
type TransferCallCreator = MigrationTransferCallCreator;
type ReserveProvider = AbsoluteReserveProvider;
type SelfLocation = SelfLocation;
}

impl pallet_automation_price::Config for Runtime {
Expand Down Expand Up @@ -1244,7 +1247,7 @@ impl_runtime_apis! {

let execution_fee = fee_handler.execution_fee.map(|fee| fee.amount).unwrap_or(0);
Ok(AutomationFeeDetails {
schedule_fee: fee_handler.schedule_fee_amount,
schedule_fee: fee_handler.schedule_fee.amount,
execution_fee,
})
}
Expand Down
9 changes: 6 additions & 3 deletions runtime/turing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ use xcm::latest::{prelude::*, MultiLocation};
use xcm_builder::Account32Hash;
use xcm_executor::traits::Convert;

use orml_traits::location::AbsoluteReserveProvider;

use sp_std::{cmp::Ordering, prelude::*};
#[cfg(feature = "std")]
use sp_version::NativeVersion;
Expand Down Expand Up @@ -84,7 +86,7 @@ use polkadot_runtime_common::BlockHashCount;

// XCM configurations.
pub mod xcm_config;
use xcm_config::{FeePerSecondProvider, ToTreasury, TokenIdConvert};
use xcm_config::{FeePerSecondProvider, SelfLocation, ToTreasury, TokenIdConvert};

pub mod weights;

Expand Down Expand Up @@ -1061,8 +1063,9 @@ impl pallet_automation_time::Config for Runtime {
type ScheduleAllowList = ScheduleAllowList;
type EnsureProxy = AutomationEnsureProxy;
type UniversalLocation = UniversalLocation;
type SelfParaId = parachain_info::Pallet<Runtime>;
type TransferCallCreator = MigrationTransferCallCreator;
type ReserveProvider = AbsoluteReserveProvider;
type SelfLocation = SelfLocation;
}

impl pallet_automation_price::Config for Runtime {
Expand Down Expand Up @@ -1360,7 +1363,7 @@ impl_runtime_apis! {

let execution_fee = fee_handler.execution_fee.map(|fee| fee.amount).unwrap_or(0);
Ok(AutomationFeeDetails {
schedule_fee: fee_handler.schedule_fee_amount,
schedule_fee: fee_handler.schedule_fee.amount,
execution_fee,
})
}
Expand Down

0 comments on commit b6392d5

Please sign in to comment.