github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/L2/GasPriceOracle.t.sol (about)

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity 0.8.15;
     3  
     4  // Testing utilities
     5  import { CommonTest } from "test/setup/CommonTest.sol";
     6  
     7  // Libraries
     8  import { Encoding } from "src/libraries/Encoding.sol";
     9  
    10  contract GasPriceOracle_Test is CommonTest {
    11      event OverheadUpdated(uint256);
    12      event ScalarUpdated(uint256);
    13      event DecimalsUpdated(uint256);
    14  
    15      address depositor;
    16  
    17      // The initial L1 context values
    18      uint64 constant number = 10;
    19      uint64 constant timestamp = 11;
    20      uint256 constant baseFee = 2 * (10 ** 6);
    21      uint256 constant blobBaseFee = 3 * (10 ** 6);
    22      bytes32 constant hash = bytes32(uint256(64));
    23      uint64 constant sequenceNumber = 0;
    24      bytes32 constant batcherHash = bytes32(uint256(777));
    25      uint256 constant l1FeeOverhead = 310;
    26      uint256 constant l1FeeScalar = 10;
    27      uint32 constant blobBaseFeeScalar = 15;
    28      uint32 constant baseFeeScalar = 20;
    29  
    30      /// @dev Sets up the test suite.
    31      function setUp() public virtual override {
    32          super.setUp();
    33          depositor = l1Block.DEPOSITOR_ACCOUNT();
    34      }
    35  }
    36  
    37  contract GasPriceOracleBedrock_Test is GasPriceOracle_Test {
    38      /// @dev Sets up the test suite.
    39      function setUp() public virtual override {
    40          super.setUp();
    41  
    42          vm.prank(depositor);
    43          l1Block.setL1BlockValues({
    44              _number: number,
    45              _timestamp: timestamp,
    46              _basefee: baseFee,
    47              _hash: hash,
    48              _sequenceNumber: sequenceNumber,
    49              _batcherHash: batcherHash,
    50              _l1FeeOverhead: l1FeeOverhead,
    51              _l1FeeScalar: l1FeeScalar
    52          });
    53      }
    54  
    55      /// @dev Tests that `l1BaseFee` is set correctly.
    56      function test_l1BaseFee_succeeds() external {
    57          assertEq(gasPriceOracle.l1BaseFee(), baseFee);
    58      }
    59  
    60      /// @dev Tests that `gasPrice` is set correctly.
    61      function test_gasPrice_succeeds() external {
    62          vm.fee(100);
    63          uint256 gasPrice = gasPriceOracle.gasPrice();
    64          assertEq(gasPrice, 100);
    65      }
    66  
    67      /// @dev Tests that `baseFee` is set correctly.
    68      function test_baseFee_succeeds() external {
    69          vm.fee(64);
    70          uint256 gasPrice = gasPriceOracle.baseFee();
    71          assertEq(gasPrice, 64);
    72      }
    73  
    74      /// @dev Tests that `scalar` is set correctly.
    75      function test_scalar_succeeds() external {
    76          assertEq(gasPriceOracle.scalar(), l1FeeScalar);
    77      }
    78  
    79      /// @dev Tests that `overhead` is set correctly.
    80      function test_overhead_succeeds() external {
    81          assertEq(gasPriceOracle.overhead(), l1FeeOverhead);
    82      }
    83  
    84      /// @dev Tests that `decimals` is set correctly.
    85      function test_decimals_succeeds() external {
    86          assertEq(gasPriceOracle.decimals(), 6);
    87          assertEq(gasPriceOracle.DECIMALS(), 6);
    88      }
    89  
    90      /// @dev Tests that `setGasPrice` reverts since it was removed in bedrock.
    91      function test_setGasPrice_doesNotExist_reverts() external {
    92          (bool success, bytes memory returndata) =
    93              address(gasPriceOracle).call(abi.encodeWithSignature("setGasPrice(uint256)", 1));
    94  
    95          assertEq(success, false);
    96          assertEq(returndata, hex"");
    97      }
    98  
    99      /// @dev Tests that `setL1BaseFee` reverts since it was removed in bedrock.
   100      function test_setL1BaseFee_doesNotExist_reverts() external {
   101          (bool success, bytes memory returndata) =
   102              address(gasPriceOracle).call(abi.encodeWithSignature("setL1BaseFee(uint256)", 1));
   103  
   104          assertEq(success, false);
   105          assertEq(returndata, hex"");
   106      }
   107  }
   108  
   109  contract GasPriceOracleEcotone_Test is GasPriceOracle_Test {
   110      /// @dev Sets up the test suite.
   111      function setUp() public virtual override {
   112          super.setUp();
   113  
   114          bytes memory calldataPacked = Encoding.encodeSetL1BlockValuesEcotone(
   115              baseFeeScalar, blobBaseFeeScalar, sequenceNumber, timestamp, number, baseFee, blobBaseFee, hash, batcherHash
   116          );
   117  
   118          // Execute the function call
   119          vm.prank(depositor);
   120          (bool success,) = address(l1Block).call(calldataPacked);
   121          require(success, "Function call failed");
   122  
   123          vm.prank(depositor);
   124          gasPriceOracle.setEcotone();
   125      }
   126  
   127      /// @dev Tests that `setEcotone` is only callable by the depositor.
   128      function test_setEcotone_wrongCaller_reverts() external {
   129          vm.expectRevert("GasPriceOracle: only the depositor account can set isEcotone flag");
   130          gasPriceOracle.setEcotone();
   131      }
   132  
   133      /// @dev Tests that `gasPrice` is set correctly.
   134      function test_gasPrice_succeeds() external {
   135          vm.fee(100);
   136          uint256 gasPrice = gasPriceOracle.gasPrice();
   137          assertEq(gasPrice, 100);
   138      }
   139  
   140      /// @dev Tests that `baseFee` is set correctly.
   141      function test_baseFee_succeeds() external {
   142          vm.fee(64);
   143          uint256 gasPrice = gasPriceOracle.baseFee();
   144          assertEq(gasPrice, 64);
   145      }
   146  
   147      /// @dev Tests that `overhead` reverts since it was removed in ecotone.
   148      function test_overhead_legacyFunction_reverts() external {
   149          vm.expectRevert("GasPriceOracle: overhead() is deprecated");
   150          gasPriceOracle.overhead();
   151      }
   152  
   153      /// @dev Tests that `scalar` reverts since it was removed in ecotone.
   154      function test_scalar_legacyFunction_reverts() external {
   155          vm.expectRevert("GasPriceOracle: scalar() is deprecated");
   156          gasPriceOracle.scalar();
   157      }
   158  
   159      /// @dev Tests that `l1BaseFee` is set correctly.
   160      function test_l1BaseFee_succeeds() external {
   161          assertEq(gasPriceOracle.l1BaseFee(), baseFee);
   162      }
   163  
   164      /// @dev Tests that `blobBaseFee` is set correctly.
   165      function test_blobBaseFee_succeeds() external {
   166          assertEq(gasPriceOracle.blobBaseFee(), blobBaseFee);
   167      }
   168  
   169      /// @dev Tests that `baseFeeScalar` is set correctly.
   170      function test_baseFeeScalar_succeeds() external {
   171          assertEq(gasPriceOracle.baseFeeScalar(), baseFeeScalar);
   172      }
   173  
   174      /// @dev Tests that `blobBaseFeeScalar` is set correctly.
   175      function test_blobBaseFeeScalar_succeeds() external {
   176          assertEq(gasPriceOracle.blobBaseFeeScalar(), blobBaseFeeScalar);
   177      }
   178  
   179      /// @dev Tests that `decimals` is set correctly.
   180      function test_decimals_succeeds() external {
   181          assertEq(gasPriceOracle.decimals(), 6);
   182          assertEq(gasPriceOracle.DECIMALS(), 6);
   183      }
   184  
   185      /// @dev Tests that `getL1GasUsed` and `getL1Fee` return expected values
   186      function test_getL1Fee_succeeds() external {
   187          bytes memory data = hex"0000010203"; // 2 zero bytes, 3 non-zero bytes
   188          // (2*4) + (3*16) + (68*16) == 1144
   189          uint256 gas = gasPriceOracle.getL1GasUsed(data);
   190          assertEq(gas, 1144);
   191          uint256 price = gasPriceOracle.getL1Fee(data);
   192          // gas * (2M*16*20 + 3M*15) / 16M == 48977.5
   193          assertEq(price, 48977);
   194      }
   195  }