diff --git a/contracts/fee_currencies.go b/contracts/fee_currencies.go index a2e146e583..632914c296 100644 --- a/contracts/fee_currencies.go +++ b/contracts/fee_currencies.go @@ -55,6 +55,7 @@ func ConvertGoldToCurrency(exchangeRates map[common.Address]*big.Rat, feeCurrenc return new(big.Int).Div(new(big.Int).Mul(goldAmount, exchangeRate.Num()), exchangeRate.Denom()), nil } +// Debits transaction fees from the transaction sender and stores them in the temporary address func DebitFees(evm *vm.EVM, address common.Address, amount *big.Int, feeCurrency *common.Address) error { if amount.Cmp(big.NewInt(0)) == 0 { return nil @@ -77,6 +78,11 @@ func DebitFees(evm *vm.EVM, address common.Address, amount *big.Int, feeCurrency return err } +// Credits fees to the respective parties +// - the base fee goes to the fee handler +// - the transaction tip goes to the miner +// - the l1 data fee goes the the data fee receiver, is the node runs in rollup mode +// - remaining funds are refunded to the transaction sender func CreditFees( evm *vm.EVM, feeCurrency *common.Address, @@ -96,17 +102,22 @@ func CreditFees( return fmt.Errorf("base fee: %w", err) } - if tipReceiver != common.ZeroAddress && feeTip.Cmp(common.Big0) == 1 { - leftoverGas, err = transferErc20(evm, abi, caller, feeCurrency, &tipReceiver, feeTip, leftoverGas) - if err != nil { - return fmt.Errorf("fee tip: %w", err) + // If the tip is non-zero, but the coinbase of the miner is not set, send the tip back to the tx sender + if feeTip.Cmp(common.Big0) == 1 { + if tipReceiver != common.ZeroAddress { + leftoverGas, err = transferErc20(evm, abi, caller, feeCurrency, &tipReceiver, feeTip, leftoverGas) + if err != nil { + return fmt.Errorf("fee tip: %w", err) + } + } else { + // If the coinbase is the zero address, instead refund the tip to the transaction sender + refund.Add(refund, feeTip) } - } else { - // If the coinbase is the zero address, instead refund the tip to the transaction sender - refund.Add(refund, feeTip) } - if l1DataFee != nil { + // The data fee is `nil` when the node is not running in rollup mode + // If the data fee is non-zero, but the data fee receiver is not set, send the tip back to the tx sender + if l1DataFee != nil && l1DataFee.Cmp(common.Big0) == 1 { if l1DataFeeReceiver != common.ZeroAddress { leftoverGas, err = transferErc20(evm, abi, caller, feeCurrency, &l1DataFeeReceiver, l1DataFee, leftoverGas) if err != nil { @@ -116,6 +127,7 @@ func CreditFees( refund.Add(refund, l1DataFee) } } + if refund.Cmp(common.Big0) == 1 { leftoverGas, err = transferErc20(evm, abi, caller, feeCurrency, &txSender, refund, leftoverGas) if err != nil {