Skip to content

Commit

Permalink
Opt flexible fee (#251)
Browse files Browse the repository at this point in the history
* change defult fee order list

* optimize flexible fee code
  • Loading branch information
herryho authored Aug 24, 2021
1 parent 9f92dbf commit ae5fe7f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 61 deletions.
4 changes: 2 additions & 2 deletions pallets/flexible-fee/src/fee_dealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<T: Config> FeeDealer<T::AccountId, PalletBalanceOf<T>, CurrencyIdOf<T>>
let consume_fee_currency_amount =
fee.saturating_mul(fee_currency_base.into()) / native_currency_base.into();
ensure!(
(T::Balance::from(consume_fee_currency_amount) + fee_currency_existential_deposit) <=
(consume_fee_currency_amount + fee_currency_existential_deposit) <=
fee_currency_balance,
Error::<T>::NotEnoughBalance
);
Expand All @@ -96,7 +96,7 @@ impl<T: Config> FeeDealer<T::AccountId, PalletBalanceOf<T>, CurrencyIdOf<T>>
fn cal_fee_token_and_amount(
who: &T::AccountId,
fee: PalletBalanceOf<T>,
) -> Result<(CurrencyId, PalletBalanceOf<T>), DispatchError> {
) -> Result<(CurrencyIdOf<T>, PalletBalanceOf<T>), DispatchError> {
// Make sure there are enough BNC to be deducted if the user has assets in other form of
// tokens rather than BNC.
let withdraw_reason = WithdrawReasons::TRANSACTION_PAYMENT;
Expand Down
63 changes: 20 additions & 43 deletions pallets/flexible-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use pallet::*;
use pallet_transaction_payment::OnChargeTransaction;
use sp_arithmetic::traits::SaturatedConversion;
use sp_runtime::{
traits::{AtLeast32Bit, CheckedSub, DispatchInfoOf, PostDispatchInfoOf, Saturating, Zero},
traits::{CheckedSub, DispatchInfoOf, PostDispatchInfoOf, Saturating, Zero},
transaction_validity::TransactionValidityError,
};
use sp_std::{vec, vec::Vec};
Expand All @@ -53,10 +53,6 @@ mod mock;
mod tests;
mod weights;

type CurrencyIdOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<
<T as frame_system::Config>::AccountId,
>>::CurrencyId;

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -65,23 +61,13 @@ pub mod pallet {
pub trait Config: frame_system::Config + pallet_transaction_payment::Config {
/// Event
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// The units in which we record balances.
type Balance: Member
+ Parameter
+ AtLeast32Bit
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Into<u128>
+ From<PalletBalanceOf<Self>>
+ Into<PalletBalanceOf<Self>>;
/// Weight information for the extrinsics in this module.
type WeightInfo: WeightInfo;
/// Handler for both NativeCurrency and MultiCurrency
type MultiCurrency: MultiCurrency<
Self::AccountId,
CurrencyId = CurrencyId,
Balance = Self::Balance,
Balance = PalletBalanceOf<Self>,
>;
/// The currency type in which fees will be paid.
type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
Expand All @@ -94,16 +80,19 @@ pub mod pallet {
type TreasuryAccount: Get<Self::AccountId>;

#[pallet::constant]
type NativeCurrencyId: Get<CurrencyId>;
type NativeCurrencyId: Get<CurrencyIdOf<Self>>;

#[pallet::constant]
type AlternativeFeeCurrencyId: Get<CurrencyId>;
type AlternativeFeeCurrencyId: Get<CurrencyIdOf<Self>>;

/// Alternative Fee currency exchange rate: ?x Fee currency: ?y Native currency
#[pallet::constant]
type AltFeeCurrencyExchangeRate: Get<(u32, u32)>;
}

pub type CurrencyIdOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<
<T as frame_system::Config>::AccountId,
>>::CurrencyId;
pub type PalletBalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
Expand All @@ -119,32 +108,24 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
FlexibleFeeExchanged(CurrencyId, u128), // token and amount
FixedRateFeeExchanged(CurrencyId, PalletBalanceOf<T>),
FlexibleFeeExchanged(CurrencyIdOf<T>, u128), // token and amount
FixedRateFeeExchanged(CurrencyIdOf<T>, PalletBalanceOf<T>),
}

#[pallet::type_value]
pub fn DefaultFeeChargeOrder<T: Config>() -> Vec<CurrencyIdOf<T>> {
[
CurrencyId::Native(TokenSymbol::ASG),
CurrencyId::Stable(TokenSymbol::KUSD),
CurrencyId::Token(TokenSymbol::DOT),
CurrencyId::VToken(TokenSymbol::DOT),
CurrencyId::Token(TokenSymbol::KSM),
CurrencyId::VToken(TokenSymbol::KSM),
]
.to_vec()
[T::NativeCurrencyId::get(), CurrencyId::Token(TokenSymbol::KSM)].to_vec()
}

#[pallet::storage]
#[pallet::getter(fn user_fee_charge_order_list)]
pub type UserFeeChargeOrderList<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, Vec<CurrencyId>, ValueQuery>;
StorageMap<_, Twox64Concat, T::AccountId, Vec<CurrencyIdOf<T>>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn default_fee_charge_order_list)]
pub type DefaultFeeChargeOrderList<T: Config> =
StorageValue<_, Vec<CurrencyId>, ValueQuery, DefaultFeeChargeOrder<T>>;
StorageValue<_, Vec<CurrencyIdOf<T>>, ValueQuery, DefaultFeeChargeOrder<T>>;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
Expand All @@ -161,7 +142,7 @@ pub mod pallet {
#[pallet::weight(<T as Config>::WeightInfo::set_user_fee_charge_order())]
pub fn set_user_fee_charge_order(
origin: OriginFor<T>,
asset_order_list_vec: Option<Vec<CurrencyId>>,
asset_order_list_vec: Option<Vec<CurrencyIdOf<T>>>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand All @@ -180,7 +161,7 @@ pub mod pallet {

impl<T: Config> Pallet<T> {
/// Get user fee charge assets order
fn inner_get_user_fee_charge_order_list(account_id: &T::AccountId) -> Vec<CurrencyId> {
fn inner_get_user_fee_charge_order_list(account_id: &T::AccountId) -> Vec<CurrencyIdOf<T>> {
let mut charge_order_list = UserFeeChargeOrderList::<T>::get(&account_id);
if charge_order_list.is_empty() {
charge_order_list = DefaultFeeChargeOrderList::<T>::get();
Expand Down Expand Up @@ -239,15 +220,11 @@ where
// This withdraw operation allows death. So it will succeed given the remaining amount
// less than the existential deposit.
let fee_currency_id = T::AlternativeFeeCurrencyId::get();
T::MultiCurrency::withdraw(fee_currency_id, who, T::Balance::from(fee_amount))
T::MultiCurrency::withdraw(fee_currency_id, who, fee_amount)
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;
// deposit the fee_currency amount to Treasury
T::MultiCurrency::deposit(
fee_currency_id,
&T::TreasuryAccount::get(),
T::Balance::from(fee_amount),
)
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;
T::MultiCurrency::deposit(fee_currency_id, &T::TreasuryAccount::get(), fee_amount)
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;

Self::deposit_event(Event::FixedRateFeeExchanged(fee_currency_id, fee_amount));

Expand Down Expand Up @@ -346,7 +323,7 @@ impl<T: Config> FeeDealer<T::AccountId, PalletBalanceOf<T>, CurrencyIdOf<T>> for
// If native token balance is below existential deposit requirement,
// go exchange fee + existential deposit. Else to exchange fee amount.
let amount_out: AssetBalance;
if native_balance > T::Balance::from(existential_deposit) {
if native_balance > existential_deposit {
amount_out = fee.saturated_into();
} else {
amount_out = (fee + existential_deposit).saturated_into();
Expand Down Expand Up @@ -385,9 +362,9 @@ impl<T: Config> FeeDealer<T::AccountId, PalletBalanceOf<T>, CurrencyIdOf<T>> for
fn cal_fee_token_and_amount(
who: &T::AccountId,
fee: PalletBalanceOf<T>,
) -> Result<(CurrencyId, PalletBalanceOf<T>), DispatchError> {
) -> Result<(CurrencyIdOf<T>, PalletBalanceOf<T>), DispatchError> {
let mut fee_token_id_out: CurrencyIdOf<T> = T::NativeCurrencyId::get();
let mut fee_token_amount_out: T::Balance = T::Balance::from(0 as u32);
let mut fee_token_amount_out = Zero::zero();

// get the user defined fee charge order list.
let user_fee_charge_order_list = Self::inner_get_user_fee_charge_order_list(who);
Expand Down
1 change: 0 additions & 1 deletion pallets/flexible-fee/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ parameter_types! {
}

impl crate::Config for Test {
type Balance = u64;
type Currency = Balances;
type DexOperator = ZenlinkProtocol;
type FeeDealer = FixedCurrencyFeeRate<Test>;
Expand Down
4 changes: 0 additions & 4 deletions pallets/flexible-fee/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ fn inner_get_user_fee_charge_order_list_should_work() {

let mut default_order_list: Vec<CurrencyId> = Vec::new();
default_order_list.push(CurrencyId::Native(TokenSymbol::ASG));
default_order_list.push(CurrencyId::Stable(TokenSymbol::KUSD));
default_order_list.push(CurrencyId::Token(TokenSymbol::DOT));
default_order_list.push(CurrencyId::VToken(TokenSymbol::DOT));
default_order_list.push(CurrencyId::Token(TokenSymbol::KSM));
default_order_list.push(CurrencyId::VToken(TokenSymbol::KSM));

assert_eq!(FlexibleFee::inner_get_user_fee_charge_order_list(&ALICE), default_order_list);

Expand Down
4 changes: 1 addition & 3 deletions runtime/asgard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,12 +920,10 @@ impl orml_tokens::Config for Runtime {
}

parameter_types! {
pub const AlternativeFeeCurrencyId: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
pub const AltFeeCurrencyExchangeRate: (u32, u32) = (1, 100);
}

impl bifrost_flexible_fee::Config for Runtime {
type Balance = Balance;
type Currency = Balances;
type DexOperator = ZenlinkProtocol;
// type FeeDealer = FlexibleFee;
Expand All @@ -934,7 +932,7 @@ impl bifrost_flexible_fee::Config for Runtime {
type MultiCurrency = Currencies;
type TreasuryAccount = BifrostTreasuryAccount;
type NativeCurrencyId = NativeCurrencyId;
type AlternativeFeeCurrencyId = AlternativeFeeCurrencyId;
type AlternativeFeeCurrencyId = RelayCurrencyId;
type AltFeeCurrencyExchangeRate = AltFeeCurrencyExchangeRate;
type OnUnbalanced = Treasury;
type WeightInfo = weights::bifrost_flexible_fee::WeightInfo<Runtime>;
Expand Down
8 changes: 3 additions & 5 deletions runtime/bifrost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub mod constants;
use bifrost_flexible_fee::fee_dealer::{FeeDealer, FixedCurrencyFeeRate};
use bifrost_runtime_common::{
xcm_impl::{
BifrostAccountIdToMultiLocation, BifrostAssetMatcher, BifrostCurrencyIdConvert,
BifrostFilteredAssets, BifrostXcmTransactFilter,
BifrostAssetMatcher, BifrostCurrencyIdConvert, BifrostFilteredAssets,
BifrostXcmTransactFilter,
},
SlowAdjustingFeeUpdate,
};
Expand Down Expand Up @@ -824,12 +824,10 @@ impl orml_tokens::Config for Runtime {

// Bifrost modules start
parameter_types! {
pub const AlternativeFeeCurrencyId: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
pub const AltFeeCurrencyExchangeRate: (u32, u32) = (1, 100);
}

impl bifrost_flexible_fee::Config for Runtime {
type Balance = Balance;
type Currency = Balances;
type DexOperator = ();
// type FeeDealer = FlexibleFee;
Expand All @@ -838,7 +836,7 @@ impl bifrost_flexible_fee::Config for Runtime {
type MultiCurrency = Currencies;
type TreasuryAccount = BifrostTreasuryAccount;
type NativeCurrencyId = NativeCurrencyId;
type AlternativeFeeCurrencyId = AlternativeFeeCurrencyId;
type AlternativeFeeCurrencyId = RelayCurrencyId;
type AltFeeCurrencyExchangeRate = AltFeeCurrencyExchangeRate;
type OnUnbalanced = Treasury;
type WeightInfo = weights::bifrost_flexible_fee::WeightInfo<Runtime>;
Expand Down
4 changes: 1 addition & 3 deletions runtime/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,12 +893,10 @@ impl orml_tokens::Config for Runtime {
}

parameter_types! {
pub const AlternativeFeeCurrencyId: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
pub const AltFeeCurrencyExchangeRate: (u32, u32) = (1, 100);
}

impl bifrost_flexible_fee::Config for Runtime {
type Balance = Balance;
type Currency = Balances;
type DexOperator = ZenlinkProtocol;
// type FeeDealer = FlexibleFee;
Expand All @@ -907,7 +905,7 @@ impl bifrost_flexible_fee::Config for Runtime {
type MultiCurrency = Currencies;
type TreasuryAccount = BifrostTreasuryAccount;
type NativeCurrencyId = NativeCurrencyId;
type AlternativeFeeCurrencyId = AlternativeFeeCurrencyId;
type AlternativeFeeCurrencyId = RelayCurrencyId;
type AltFeeCurrencyExchangeRate = AltFeeCurrencyExchangeRate;
type OnUnbalanced = Treasury;
type WeightInfo = weights::bifrost_flexible_fee::WeightInfo<Runtime>;
Expand Down

0 comments on commit ae5fe7f

Please sign in to comment.