Skip to content

Commit 12c21b9

Browse files
authored
fix with_fee_currency (#2194)
1 parent f3d2874 commit 12c21b9

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

modules/transaction-payment/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,6 @@ where
802802
call: &CallOf<T>,
803803
reason: WithdrawReasons,
804804
) -> Result<(T::AccountId, Balance), DispatchError> {
805-
let custom_fee_surplus = T::CustomFeeSurplus::get().mul_ceil(fee);
806-
let custom_fee_amount = fee.saturating_add(custom_fee_surplus);
807805
match call.is_sub_type() {
808806
Some(Call::with_fee_path { fee_swap_path, .. }) => {
809807
ensure!(
@@ -812,6 +810,9 @@ where
812810
&& fee_swap_path.last() == Some(&T::NativeCurrencyId::get()),
813811
Error::<T>::InvalidSwapPath
814812
);
813+
let fee = Self::check_native_is_not_enough(who, fee, reason).map_or_else(|| fee, |amount| amount);
814+
let custom_fee_surplus = T::CustomFeeSurplus::get().mul_ceil(fee);
815+
let custom_fee_amount = fee.saturating_add(custom_fee_surplus);
815816
T::DEX::swap_with_specific_path(
816817
who,
817818
fee_swap_path,
@@ -824,12 +825,15 @@ where
824825
TokenExchangeRate::<T>::contains_key(currency_id),
825826
Error::<T>::InvalidToken
826827
);
827-
let fee_amount = if T::DefaultFeeTokens::get().contains(currency_id) {
828-
fee.saturating_add(T::AlternativeFeeSurplus::get().mul_ceil(fee))
828+
let fee = Self::check_native_is_not_enough(who, fee, reason).map_or_else(|| fee, |amount| amount);
829+
let alternative_fee_surplus = T::AlternativeFeeSurplus::get().mul_ceil(fee);
830+
let custom_fee_surplus = T::CustomFeeSurplus::get().mul_ceil(fee);
831+
let (fee_amount, fee_surplus) = if T::DefaultFeeTokens::get().contains(currency_id) {
832+
(fee.saturating_add(alternative_fee_surplus), alternative_fee_surplus)
829833
} else {
830-
custom_fee_amount
834+
(fee.saturating_add(custom_fee_surplus), custom_fee_surplus)
831835
};
832-
Self::swap_from_pool_or_dex(who, fee_amount, *currency_id).map(|_| (who.clone(), fee_amount))
836+
Self::swap_from_pool_or_dex(who, fee_amount, *currency_id).map(|_| (who.clone(), fee_surplus))
833837
}
834838
Some(Call::with_fee_paid_by {
835839
call: _,

modules/transaction-payment/src/tests.rs

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -506,26 +506,27 @@ fn charges_fee_when_validate_with_fee_path_call() {
506506
// Enable dex with Alice, and initialize tx charge fee pool
507507
builder_with_dex_and_fee_pool(true).execute_with(|| {
508508
let dex_acc: AccountId = PalletId(*b"aca/dexm").into_account();
509-
let dex_ausd = Currencies::free_balance(ACA, &dex_acc);
509+
let dex_aca = Currencies::free_balance(ACA, &dex_acc);
510510

511-
let fee: Balance = 50 * 2 + 100;
512-
let fee_perc = CustomFeeSurplus::get();
513-
let surplus = fee_perc.mul_ceil(fee);
514-
let fee_surplus = fee + surplus;
511+
let fee: Balance = 50 * 2 + 100 + 10;
512+
let fee_surplus = fee + CustomFeeSurplus::get().mul_ceil(fee);
513+
assert_eq!(315, fee_surplus);
514+
515+
assert_ok!(Currencies::update_balance(Origin::root(), BOB, AUSD, 10000));
515516

516517
// AUSD - ACA
517518
assert_ok!(ChargeTransactionPayment::<Runtime>::from(0).validate(
518-
&ALICE,
519+
&BOB,
519520
&with_fee_path_call(vec![AUSD, ACA]),
520521
&INFO2,
521522
50
522523
));
523524
System::assert_has_event(crate::mock::Event::DEXModule(module_dex::Event::Swap {
524-
trader: ALICE,
525+
trader: BOB,
525526
path: vec![AUSD, ACA],
526-
liquidity_changes: vec![31, fee_surplus], // 31 AUSD - 300 ACA
527+
liquidity_changes: vec![33, fee_surplus], // 33 AUSD - 315 ACA
527528
}));
528-
assert_eq!(dex_ausd - fee_surplus, Currencies::free_balance(ACA, &dex_acc));
529+
assert_eq!(dex_aca - fee_surplus, Currencies::free_balance(ACA, &dex_acc));
529530

530531
// DOT - ACA swap dex is invalid
531532
assert_noop!(
@@ -539,18 +540,26 @@ fn charges_fee_when_validate_with_fee_path_call() {
539540
);
540541

541542
// DOT - AUSD - ACA
543+
let fee: Balance = 50 * 2 + 100;
544+
let fee_surplus2 = fee + CustomFeeSurplus::get().mul_ceil(fee);
545+
assert_eq!(300, fee_surplus2);
546+
547+
assert_ok!(Currencies::update_balance(Origin::root(), BOB, DOT, 10000));
542548
assert_ok!(ChargeTransactionPayment::<Runtime>::from(0).validate(
543-
&ALICE,
549+
&BOB,
544550
&with_fee_path_call(vec![DOT, AUSD, ACA]),
545551
&INFO2,
546552
50
547553
));
548554
System::assert_has_event(crate::mock::Event::DEXModule(module_dex::Event::Swap {
549-
trader: ALICE,
555+
trader: BOB,
550556
path: vec![DOT, AUSD, ACA],
551-
liquidity_changes: vec![4, 33, fee_surplus], // 4 DOT - 33 AUSD - 300 ACA
557+
liquidity_changes: vec![4, 34, fee_surplus2], // 4 DOT - 34 AUSD - 300 ACA
552558
}));
553-
assert_eq!(dex_ausd - fee_surplus * 2, Currencies::free_balance(ACA, &dex_acc));
559+
assert_eq!(
560+
dex_aca - fee_surplus - fee_surplus2,
561+
Currencies::free_balance(ACA, &dex_acc)
562+
);
554563
});
555564
}
556565

@@ -565,28 +574,48 @@ fn charges_fee_when_validate_with_fee_currency_call() {
565574
let sub_dot_aca = Currencies::free_balance(ACA, &dot_acc);
566575
let sub_dot_dot = Currencies::free_balance(DOT, &dot_acc);
567576

568-
let fee: Balance = 50 * 2 + 100;
577+
let fee: Balance = 50 * 2 + 100 + 10;
569578
let fee_perc = AlternativeFeeSurplus::get();
570-
let surplus = fee_perc.mul_ceil(fee);
571-
let fee_amount = fee + surplus;
579+
let surplus = fee_perc.mul_ceil(fee); // 53
580+
let fee_amount = fee + surplus; // 263
572581

582+
assert_ok!(Currencies::update_balance(Origin::root(), BOB, AUSD, 10000));
583+
assert_eq!(0, Currencies::free_balance(ACA, &BOB));
573584
assert_ok!(ChargeTransactionPayment::<Runtime>::from(0).validate(
574-
&ALICE,
585+
&BOB,
575586
&with_fee_currency_call(AUSD),
576587
&INFO2,
577588
50
578589
));
590+
assert_eq!(10, Currencies::free_balance(ACA, &BOB)); // ED
591+
assert_eq!(7370, Currencies::free_balance(AUSD, &BOB));
592+
System::assert_has_event(crate::mock::Event::Tokens(orml_tokens::Event::Transfer {
593+
currency_id: AUSD,
594+
from: BOB,
595+
to: ausd_acc.clone(),
596+
amount: 2630,
597+
}));
598+
System::assert_has_event(crate::mock::Event::PalletBalances(pallet_balances::Event::Transfer {
599+
from: ausd_acc.clone(),
600+
to: BOB,
601+
amount: 263,
602+
}));
603+
579604
assert_eq!(sub_ausd_aca - fee_amount, Currencies::free_balance(ACA, &ausd_acc));
580605
assert_eq!(
581606
sub_ausd_usd + fee_amount * 10,
582607
Currencies::free_balance(AUSD, &ausd_acc)
583608
);
584609

610+
let fee: Balance = 50 * 2 + 100;
585611
let fee_perc = CustomFeeSurplus::get();
586612
let surplus = fee_perc.mul_ceil(fee);
587613
let fee_amount = fee + surplus;
614+
615+
assert_ok!(Currencies::update_balance(Origin::root(), BOB, DOT, 10000));
616+
assert_eq!(10, Currencies::free_balance(ACA, &BOB));
588617
assert_ok!(ChargeTransactionPayment::<Runtime>::from(0).validate(
589-
&ALICE,
618+
&BOB,
590619
&with_fee_currency_call(DOT),
591620
&INFO2,
592621
50

0 commit comments

Comments
 (0)