• Mark Tyneway's avatar
    gas-oracle: update to set L1 base fee · d89b5005
    Mark Tyneway authored
    The `gas-oracle` will now periodically ping L1 asking
    for the latest header and will update the `l1BaseFee`
    value in the `OVM_GasPriceOracle` oracle predeploy when
    the base fee changes more than a configurable amount.
    
    This is very similar to the way that the `gas-oracle` sets
    the L2 gas price based on the amount of gas that has
    been used in an amount of time.
    
    Note that this is a breaking change when it comes to config.
    Some config options are renamed and some config options are added.
    
    `--ethereum-http-url` now corresponds to the L1 URL and
    `--layer-two-http-url` corresponds to the L2 URL. Previously,
    only an L2 URL was provided and it was called `--ethereum-http-url`.
    
    Now there are flags for `--l1-chain-id` and `--l2-chain-id` when
    previously `--chain-id` corresponded to the L2 chain id.
    
    There are also now flags for turning on and off functionality.
    `--enable-l1-base-fee` and `--enable-l2-gas-price` are used
    to turn on updating of the corresponding values. They are on by
    default.
    
    The `--l1-base-fee-significant-factor` is expected to be a decimal
    and determines how much the base fee on L1 must change before the
    `gas-oracle` will consider updating the base fee by sending
    a transaction to the `OVM_GasPriceOracle`.
    d89b5005
base_fee.go 2.36 KB
package oracle

import (
	"context"
	"fmt"

	"github.com/ethereum-optimism/optimism/go/gas-oracle/bindings"
	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"github.com/ethereum/go-ethereum/common/hexutil"
	"github.com/ethereum/go-ethereum/log"
)

func wrapUpdateBaseFee(l1Backend bind.ContractTransactor, l2Backend DeployContractBackend, cfg *Config) (func() error, error) {
	if cfg.privateKey == nil {
		return nil, errNoPrivateKey
	}
	if cfg.l2ChainID == nil {
		return nil, errNoChainID
	}

	opts, err := bind.NewKeyedTransactorWithChainID(cfg.privateKey, cfg.l2ChainID)
	if err != nil {
		return nil, err
	}
	// Once https://github.com/ethereum/go-ethereum/pull/23062 is released
	// then we can remove setting the context here
	if opts.Context == nil {
		opts.Context = context.Background()
	}
	// Don't send the transaction using the `contract` so that we can inspect
	// it beforehand
	opts.NoSend = true

	// Create a new contract bindings in scope of the updateL2GasPriceFn
	// that is returned from this function
	contract, err := bindings.NewGasPriceOracle(cfg.gasPriceOracleAddress, l2Backend)
	if err != nil {
		return nil, err
	}

	return func() error {
		baseFee, err := contract.L1BaseFee(&bind.CallOpts{
			Context: context.Background(),
		})
		tip, err := l1Backend.HeaderByNumber(context.Background(), nil)
		if err != nil {
			return err
		}
		if tip.BaseFee == nil {
			return errNoBaseFee
		}
		if !isDifferenceSignificant(baseFee.Uint64(), tip.BaseFee.Uint64(), cfg.l1BaseFeeSignificanceFactor) {
			log.Debug("non significant base fee update", "tip", tip.BaseFee, "current", baseFee)
			return nil
		}
		tx, err := contract.SetL1BaseFee(opts, tip.BaseFee)
		if err != nil {
			return err
		}
		log.Debug("updating L1 base fee", "tx.gasPrice", tx.GasPrice(), "tx.gasLimit", tx.Gas(),
			"tx.data", hexutil.Encode(tx.Data()), "tx.to", tx.To().Hex(), "tx.nonce", tx.Nonce())
		if err := l2Backend.SendTransaction(context.Background(), tx); err != nil {
			return fmt.Errorf("cannot update base fee: %w", err)
		}
		log.Info("L1 base fee transaction sent", "hash", tx.Hash().Hex())

		if cfg.waitForReceipt {
			// Wait for the receipt
			receipt, err := waitForReceipt(l2Backend, tx)
			if err != nil {
				return err
			}

			log.Info("base-fee transaction confirmed", "hash", tx.Hash().Hex(),
				"gas-used", receipt.GasUsed, "blocknumber", receipt.BlockNumber)
		}
		return nil
	}, nil
}