Skip to content

Commit

Permalink
Fix evm fee selection (#1608)
Browse files Browse the repository at this point in the history
* fix 10% buffer computational logic

* Correct gas fee parameter
  • Loading branch information
SunTiebing authored Jan 15, 2025
1 parent ad1ee93 commit b448607
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pallets/flexible-fee/src/impls/account_fee_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<T: Config> AccountFeeCurrency<T::AccountId> for Pallet<T> {

// Add a 10% buffer to account for potential inaccuracies in fee estimation.
let fee = fee
.checked_mul(10)
.checked_mul(110)
.and_then(|v| v.checked_div(100))
.ok_or(Error::<T>::PercentageCalculationFailed)?;
for maybe_currency in currency_list.iter() {
Expand Down
46 changes: 46 additions & 0 deletions pallets/flexible-fee/src/tests/common_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,52 @@ fn get_fee_currency_should_work_with_default_currency() {
});
}

#[test]
fn get_fee_currency_should_work_with_dot() {
new_test_ext().execute_with(|| {
ini_meta_data();

let origin_signed_alice = RuntimeOrigin::signed(ALICE);
assert_ok!(FlexibleFee::set_user_default_fee_currency(
origin_signed_alice.clone(),
Some(DOT)
));

assert_ok!(Currencies::deposit(
DOT,
&ALICE,
660u128 * 10u128.pow(10) + 1
));
assert_ok!(Currencies::deposit(WETH, &ALICE, 100u128.pow(18)));

let currency = FlexibleFee::get_fee_currency(&ALICE, 10u128.pow(18).into()).unwrap();
assert_eq!(currency, DOT);
});
}

#[test]
fn get_fee_currency_should_work_with_dot_weth() {
new_test_ext().execute_with(|| {
ini_meta_data();

let origin_signed_alice = RuntimeOrigin::signed(ALICE);
assert_ok!(FlexibleFee::set_user_default_fee_currency(
origin_signed_alice.clone(),
Some(DOT)
));

assert_ok!(Currencies::deposit(
DOT,
&ALICE,
660u128 * 10u128.pow(10) - 1
));
assert_ok!(Currencies::deposit(WETH, &ALICE, 100u128.pow(18)));

let currency = FlexibleFee::get_fee_currency(&ALICE, 10u128.pow(18).into()).unwrap();
assert_eq!(currency, WETH);
});
}

#[test]
fn get_fee_currency_should_work_with_default_currency_poor() {
new_test_ext().execute_with(|| {
Expand Down
14 changes: 9 additions & 5 deletions runtime/bifrost-paseo/src/evm/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use bifrost_primitives::{
AccountFeeCurrency, AccountFeeCurrencyBalanceInCurrency, Balance, CurrencyId,
OraclePriceProvider,
};
use core::ops::Mul;
use fp_evm::{Account, TransactionValidationError};
use frame_support::traits::{
tokens::{Fortitude, Preservation},
Expand Down Expand Up @@ -121,11 +122,14 @@ where
let account_id = T::AddressMapping::into_account_id(source);
let account_nonce = frame_system::Pallet::<T>::account_nonce(&account_id);

let (balance, b_weight) =
match B::get_balance_in_currency(evm_currency, &account_id, base_fee) {
Ok((balance, b_weight)) => (balance, b_weight),
Err(_) => (0, T::DbWeight::get().reads(2)),
};
let (balance, b_weight) = match B::get_balance_in_currency(
evm_currency,
&account_id,
base_fee.mul(U256::from(gas_limit)),
) {
Ok((balance, b_weight)) => (balance, b_weight),
Err(_) => (0, T::DbWeight::get().reads(2)),
};

let (source_account, inner_weight) = (
Account {
Expand Down
14 changes: 9 additions & 5 deletions runtime/bifrost-polkadot/src/evm/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use bifrost_primitives::{
AccountFeeCurrency, AccountFeeCurrencyBalanceInCurrency, Balance, CurrencyId,
OraclePriceProvider,
};
use core::ops::Mul;
use fp_evm::{Account, TransactionValidationError};
use frame_support::traits::{
tokens::{Fortitude, Preservation},
Expand Down Expand Up @@ -121,11 +122,14 @@ where
let account_id = T::AddressMapping::into_account_id(source);
let account_nonce = frame_system::Pallet::<T>::account_nonce(&account_id);

let (balance, b_weight) =
match B::get_balance_in_currency(evm_currency, &account_id, base_fee) {
Ok((balance, b_weight)) => (balance, b_weight),
Err(_) => (0, T::DbWeight::get().reads(2)),
};
let (balance, b_weight) = match B::get_balance_in_currency(
evm_currency,
&account_id,
base_fee.mul(U256::from(gas_limit)),
) {
Ok((balance, b_weight)) => (balance, b_weight),
Err(_) => (0, T::DbWeight::get().reads(2)),
};

let (source_account, inner_weight) = (
Account {
Expand Down

0 comments on commit b448607

Please sign in to comment.