Skip to content

Commit

Permalink
more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
palango committed Jan 10, 2024
1 parent 1c01410 commit c589dc6
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions contracts/fee_currencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit c589dc6

Please sign in to comment.