Skip to content

Commit

Permalink
trigger error when paid is less than refund
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadkaouk committed Jan 25, 2024
1 parent a5df801 commit 35ebd17
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ pub trait OnChargeEVMTransaction<T: Config> {
corrected_fee: U256,
base_fee: U256,
already_withdrawn: Self::LiquidityInfo,
) -> Self::LiquidityInfo;
) -> Result<Self::LiquidityInfo, Error<T>>;

/// Introduced in EIP1559 to handle the priority tip.
fn pay_priority_fee(tip: Self::LiquidityInfo);
Expand Down Expand Up @@ -1004,7 +1004,7 @@ where
corrected_fee: U256,
base_fee: U256,
already_withdrawn: Self::LiquidityInfo,
) -> Self::LiquidityInfo {
) -> Result<Self::LiquidityInfo, Error<T>> {
if let Some(paid) = already_withdrawn {
let account_id = T::AddressMapping::into_account_id(*who);

Expand Down Expand Up @@ -1040,14 +1040,14 @@ where
let adjusted_paid = paid
.offset(refund_imbalance)
.same()
.unwrap_or_else(|_| C::NegativeImbalance::zero());
.map_err(|_| Error::<T>::Undefined)?;

let (base_fee, tip) = adjusted_paid.split(base_fee.unique_saturated_into());
// Handle base fee. Can be either burned, rationed, etc ...
OU::on_unbalanced(base_fee);
return Some(tip);
return Ok(Some(tip));
}
None
Ok(None)
}

fn pay_priority_fee(tip: Self::LiquidityInfo) {
Expand Down Expand Up @@ -1107,7 +1107,7 @@ where
corrected_fee: U256,
base_fee: U256,
already_withdrawn: Self::LiquidityInfo,
) -> Self::LiquidityInfo {
) -> Result<Self::LiquidityInfo, Error<T>> {
if let Some(paid) = already_withdrawn {
let account_id = T::AddressMapping::into_account_id(*who);

Expand All @@ -1128,9 +1128,9 @@ where
let (base_fee, tip) = adjusted_paid.split(base_fee.unique_saturated_into());
// Handle base fee. Can be either burned, rationed, etc ...
OU::on_unbalanced(base_fee);
return Some(tip);
return Ok(Some(tip));
}
None
Ok(None)
}

fn pay_priority_fee(tip: Self::LiquidityInfo) {
Expand Down Expand Up @@ -1175,7 +1175,7 @@ U256: UniqueSaturatedInto<BalanceOf<T>>,
corrected_fee: U256,
base_fee: U256,
already_withdrawn: Self::LiquidityInfo,
) -> Self::LiquidityInfo {
) -> Result<Self::LiquidityInfo, Error<T>> {
<EVMCurrencyAdapter::<<T as Config>::Currency, ()> as OnChargeEVMTransaction<T>>::correct_and_deposit_fee(who, corrected_fee, base_fee, already_withdrawn)
}

Expand Down
2 changes: 1 addition & 1 deletion frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ where
actual_base_fee,
// Fee initially withdrawn.
fee,
);
).map_err(|e| RunnerError { error: e, weight })?;
T::OnChargeTransaction::pay_priority_fee(actual_priority_fee);

let state = executor.into_state();
Expand Down
2 changes: 1 addition & 1 deletion frame/evm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ fn fee_deduction() {
assert_eq!(Balances::free_balance(substrate_addr), 90);

// Refund fees as 5 units
<<Test as Config>::OnChargeTransaction as OnChargeEVMTransaction<Test>>::correct_and_deposit_fee(&evm_addr, U256::from(5), U256::from(5), imbalance);
<<Test as Config>::OnChargeTransaction as OnChargeEVMTransaction<Test>>::correct_and_deposit_fee(&evm_addr, U256::from(5), U256::from(5), imbalance).unwrap();
assert_eq!(Balances::free_balance(substrate_addr), 95);
});
}
Expand Down

0 comments on commit 35ebd17

Please sign in to comment.