From b4486070a1eaa5f0234b828f83f24dfe89eac3a3 Mon Sep 17 00:00:00 2001 From: SunTiebing <87381708+SunTiebing@users.noreply.github.com> Date: Wed, 15 Jan 2025 20:07:41 +0800 Subject: [PATCH] Fix evm fee selection (#1608) * fix 10% buffer computational logic * Correct gas fee parameter --- .../src/impls/account_fee_currency.rs | 2 +- .../flexible-fee/src/tests/common_tests.rs | 46 +++++++++++++++++++ runtime/bifrost-paseo/src/evm/runner.rs | 14 ++++-- runtime/bifrost-polkadot/src/evm/runner.rs | 14 ++++-- 4 files changed, 65 insertions(+), 11 deletions(-) diff --git a/pallets/flexible-fee/src/impls/account_fee_currency.rs b/pallets/flexible-fee/src/impls/account_fee_currency.rs index 01676ba0a3..10604229dd 100644 --- a/pallets/flexible-fee/src/impls/account_fee_currency.rs +++ b/pallets/flexible-fee/src/impls/account_fee_currency.rs @@ -62,7 +62,7 @@ impl AccountFeeCurrency for Pallet { // 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::::PercentageCalculationFailed)?; for maybe_currency in currency_list.iter() { diff --git a/pallets/flexible-fee/src/tests/common_tests.rs b/pallets/flexible-fee/src/tests/common_tests.rs index dd936d78fd..cda8b381aa 100644 --- a/pallets/flexible-fee/src/tests/common_tests.rs +++ b/pallets/flexible-fee/src/tests/common_tests.rs @@ -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(|| { diff --git a/runtime/bifrost-paseo/src/evm/runner.rs b/runtime/bifrost-paseo/src/evm/runner.rs index 4c84772410..8e3987aa20 100644 --- a/runtime/bifrost-paseo/src/evm/runner.rs +++ b/runtime/bifrost-paseo/src/evm/runner.rs @@ -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}, @@ -121,11 +122,14 @@ where let account_id = T::AddressMapping::into_account_id(source); let account_nonce = frame_system::Pallet::::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 { diff --git a/runtime/bifrost-polkadot/src/evm/runner.rs b/runtime/bifrost-polkadot/src/evm/runner.rs index 4c84772410..8e3987aa20 100644 --- a/runtime/bifrost-polkadot/src/evm/runner.rs +++ b/runtime/bifrost-polkadot/src/evm/runner.rs @@ -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}, @@ -121,11 +122,14 @@ where let account_id = T::AddressMapping::into_account_id(source); let account_nonce = frame_system::Pallet::::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 {