diff --git a/crates/payload/basic/src/lib.rs b/crates/payload/basic/src/lib.rs index 53f7f6445384..3ee26b584f89 100644 --- a/crates/payload/basic/src/lib.rs +++ b/crates/payload/basic/src/lib.rs @@ -802,8 +802,9 @@ where })); // update add to total fees - let miner_fee = - tx.effective_tip_per_gas(base_fee).expect("fee is always valid; execution succeeded"); + let miner_fee = tx + .effective_tip_per_gas(Some(base_fee)) + .expect("fee is always valid; execution succeeded"); total_fees += U256::from(miner_fee) * U256::from(gas_used); // append transaction to the list of executed transactions diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 7f542e80d3a8..67384c50d6f5 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -307,50 +307,35 @@ impl Transaction { } } - // TODO: dedup with effective_tip_per_gas - /// Determine the effective gas limit for the given transaction and base fee. - /// If the base fee is `None`, the `max_priority_fee_per_gas`, or gas price for non-EIP1559 - /// transactions is returned. - /// - /// If the `max_fee_per_gas` is less than the base fee, `None` returned. - pub fn effective_gas_tip(&self, base_fee: Option) -> Option { - if let Some(base_fee) = base_fee { - let max_fee_per_gas = self.max_fee_per_gas(); - - if max_fee_per_gas < base_fee as u128 { - None - } else { - let effective_max_fee = max_fee_per_gas - base_fee as u128; - Some(std::cmp::min(effective_max_fee, self.priority_fee_or_price())) - } - } else { - Some(self.priority_fee_or_price()) - } - } - /// Returns the effective miner gas tip cap (`gasTipCap`) for the given base fee: /// `min(maxFeePerGas - baseFee, maxPriorityFeePerGas)` /// + /// If the base fee is `None`, the `max_priority_fee_per_gas`, or gas price for non-EIP1559 + /// transactions is returned. + /// /// Returns `None` if the basefee is higher than the [Transaction::max_fee_per_gas]. - pub fn effective_tip_per_gas(&self, base_fee: u64) -> Option { - let base_fee = base_fee as u128; + pub fn effective_tip_per_gas(&self, base_fee: Option) -> Option { + let base_fee = match base_fee { + Some(base_fee) => base_fee as u128, + None => return Some(self.priority_fee_or_price()), + }; + let max_fee_per_gas = self.max_fee_per_gas(); + // Check if max_fee_per_gas is less than base_fee if max_fee_per_gas < base_fee { return None } - // the miner tip is the difference between the max fee and the base fee or the - // max_priority_fee_per_gas, whatever is lower - - // SAFETY: max_fee_per_gas >= base_fee + // Calculate the difference between max_fee_per_gas and base_fee let fee = max_fee_per_gas - base_fee; + // Compare the fee with max_priority_fee_per_gas (or gas price for non-EIP1559 transactions) if let Some(priority_fee) = self.max_priority_fee_per_gas() { - return Some(fee.min(priority_fee)) + Some(fee.min(priority_fee)) + } else { + Some(fee) } - - Some(fee) } /// Get the transaction's input field. diff --git a/crates/rpc/rpc-types-compat/src/transaction/mod.rs b/crates/rpc/rpc-types-compat/src/transaction/mod.rs index cbbce7f68fc7..dcbe633aa123 100644 --- a/crates/rpc/rpc-types-compat/src/transaction/mod.rs +++ b/crates/rpc/rpc-types-compat/src/transaction/mod.rs @@ -54,7 +54,9 @@ fn fill( // baseFee` let gas_price = base_fee .and_then(|base_fee| { - signed_tx.effective_tip_per_gas(base_fee).map(|tip| tip + base_fee as u128) + signed_tx + .effective_tip_per_gas(Some(base_fee)) + .map(|tip| tip + base_fee as u128) }) .unwrap_or_else(|| signed_tx.max_fee_per_gas()); diff --git a/crates/rpc/rpc/src/eth/api/fees.rs b/crates/rpc/rpc/src/eth/api/fees.rs index 08822b58403b..7016c6291999 100644 --- a/crates/rpc/rpc/src/eth/api/fees.rs +++ b/crates/rpc/rpc/src/eth/api/fees.rs @@ -163,7 +163,7 @@ where Some(TxGasAndReward { gas_used, - reward: tx.effective_gas_tip(header.base_fee_per_gas).unwrap_or_default(), + reward: tx.effective_tip_per_gas(header.base_fee_per_gas).unwrap_or_default(), }) }) .collect::>(); diff --git a/crates/rpc/rpc/src/eth/bundle.rs b/crates/rpc/rpc/src/eth/bundle.rs index 495efb003eb2..0ce269b81908 100644 --- a/crates/rpc/rpc/src/eth/bundle.rs +++ b/crates/rpc/rpc/src/eth/bundle.rs @@ -96,7 +96,7 @@ where let tx = tx.into_ecrecovered_transaction(); hash_bytes.extend_from_slice(tx.hash().as_slice()); let gas_price = tx - .effective_gas_tip(basefee) + .effective_tip_per_gas(basefee) .ok_or_else(|| RpcInvalidTransactionError::FeeCapTooLow)?; tx.try_fill_tx_env(&mut evm.env.tx)?; let ResultAndState { result, state } = evm.transact()?; diff --git a/crates/rpc/rpc/src/eth/gas_oracle.rs b/crates/rpc/rpc/src/eth/gas_oracle.rs index 1fbd1874120c..f3afc3ff7be3 100644 --- a/crates/rpc/rpc/src/eth/gas_oracle.rs +++ b/crates/rpc/rpc/src/eth/gas_oracle.rs @@ -237,7 +237,7 @@ where let parent_hash = block.parent_hash; // sort the functions by ascending effective tip first - block.body.sort_by_cached_key(|tx| tx.effective_gas_tip(base_fee_per_gas)); + block.body.sort_by_cached_key(|tx| tx.effective_tip_per_gas(base_fee_per_gas)); let mut prices = Vec::with_capacity(limit); @@ -245,7 +245,7 @@ where let mut effective_gas_tip = None; // ignore transactions with a tip under the configured threshold if let Some(ignore_under) = self.ignore_price { - let tip = tx.effective_gas_tip(base_fee_per_gas); + let tip = tx.effective_tip_per_gas(base_fee_per_gas); effective_gas_tip = Some(tip); if tip < Some(ignore_under) { continue @@ -262,7 +262,7 @@ where // a `None` effective_gas_tip represents a transaction where the max_fee_per_gas is // less than the base fee which would be invalid let effective_gas_tip = effective_gas_tip - .unwrap_or_else(|| tx.effective_gas_tip(base_fee_per_gas)) + .unwrap_or_else(|| tx.effective_tip_per_gas(base_fee_per_gas)) .ok_or(RpcInvalidTransactionError::FeeCapTooLow)?; prices.push(U256::from(effective_gas_tip)); diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index 9d75721b47d6..7e7db8ec65b2 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -938,7 +938,7 @@ impl PoolTransaction for EthPooledTransaction { /// For EIP-1559 transactions: `min(max_fee_per_gas - base_fee, max_priority_fee_per_gas)`. /// For legacy transactions: `gas_price - base_fee`. fn effective_tip_per_gas(&self, base_fee: u64) -> Option { - self.transaction.effective_tip_per_gas(base_fee) + self.transaction.effective_tip_per_gas(Some(base_fee)) } /// Returns the max priority fee per gas if the transaction is an EIP-1559 transaction, and