Skip to content

Commit

Permalink
add tests for custom gas API
Browse files Browse the repository at this point in the history
  • Loading branch information
ogtownsend committed Oct 3, 2024
1 parent 2c95e64 commit b298388
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
14 changes: 7 additions & 7 deletions core/chains/evm/gas/rollups/op_l1_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,43 +104,43 @@ func NewOpStackL1GasOracle(lggr logger.Logger, ethClient l1OracleClient, chainTy
// Encode calldata for l1BaseFee method
l1BaseFeeCalldata, _, err := encodeCalldata(L1BaseFeeAbiString, l1BaseFeeMethod)
if err != nil {
return nil, fmt.Errorf("failed to encode l1BaseFee calldata: %w", err)
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() calldata for chain: %s; %w", l1BaseFeeMethod, chainType, err)
}

// Encode calldata for isEcotone method
isEcotoneCalldata, isEcotoneMethodAbi, err := encodeCalldata(OPIsEcotoneAbiString, isEcotoneMethod)
if err != nil {
return nil, fmt.Errorf("failed to encode isEcotone calldata: %w", err)
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() calldata for chain: %s; %w", isEcotoneMethod, chainType, err)
}

// Encode calldata for isFjord method
isFjordCalldata, isFjordMethodAbi, err := encodeCalldata(OPIsFjordAbiString, isFjordMethod)
if err != nil {
return nil, fmt.Errorf("failed to encode isFjord calldata: %w", err)
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() calldata for chain: %s; %w", isFjordMethod, chainType, err)
}

// Encode calldata for baseFeeScalar method
baseFeeScalarCalldata, _, err := encodeCalldata(OPBaseFeeScalarAbiString, baseFeeScalarMethod)
if err != nil {
return nil, fmt.Errorf("failed to encode baseFeeScalar calldata: %w", err)
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() calldata for chain: %s; %w", baseFeeScalarMethod, chainType, err)
}

// Encode calldata for blobBaseFee method
blobBaseFeeCalldata, _, err := encodeCalldata(OPBlobBaseFeeAbiString, blobBaseFeeMethod)
if err != nil {
return nil, fmt.Errorf("failed to encode blobBaseFee calldata: %w", err)
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() calldata for chain: %s; %w", blobBaseFeeMethod, chainType, err)
}

// Encode calldata for blobBaseFeeScalar method
blobBaseFeeScalarCalldata, _, err := encodeCalldata(OPBlobBaseFeeScalarAbiString, blobBaseFeeScalarMethod)
if err != nil {
return nil, fmt.Errorf("failed to encode blobBaseFeeScalar calldata: %w", err)
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() calldata for chain: %s; %w", blobBaseFeeScalarMethod, chainType, err)
}

// Encode calldata for decimals method
decimalsCalldata, _, err := encodeCalldata(OPDecimalsAbiString, decimalsMethod)
if err != nil {
return nil, fmt.Errorf("failed to encode decimals calldata: %w", err)
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() calldata for chain: %s; %w", decimalsMethod, chainType, err)
}

return &optimismL1Oracle{
Expand Down
57 changes: 57 additions & 0 deletions core/chains/evm/gas/rollups/op_l1_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,60 @@ func TestOPL1Oracle_CalculateFjordGasPrice(t *testing.T) {
assert.Error(t, err)
})
}

func TestOPL1Oracle_CalculateCustomCalldataGasPrice(t *testing.T) {
baseFee := big.NewInt(100000000)
blobBaseFee := big.NewInt(25000000)
baseFeeScalar := big.NewInt(10)
blobBaseFeeScalar := big.NewInt(5)
decimals := big.NewInt(6)
oracleAddress := common.HexToAddress("0x0000000000000000000000000000000044433322").String()

t.Parallel()

t.Run("correctly fetches gas price if chain has custom calldata", func(t *testing.T) {
ethClient := setupUpgradeCheck(t, oracleAddress, true, false)
expectedPriceHex := "0x0000000000000000000000000000000000000000000000000000000000000032"

daOracle := CreateTestDAOracle(t, toml.OPOracle, oracleAddress, "0x0000000000000000000000000000000000001234")
oracle, err := NewOpStackL1GasOracle(logger.Test(t), ethClient, chaintype.ChainOptimismBedrock, daOracle)
require.NoError(t, err)

ethClient.On("CallContract", mock.Anything, mock.IsType(ethereum.CallMsg{}), mock.IsType(&big.Int{})).Run(func(args mock.Arguments) {
callMsg := args.Get(1).(ethereum.CallMsg)
blockNumber := args.Get(2).(*big.Int)
require.NotNil(t, callMsg.To)
require.Equal(t, oracleAddress, callMsg.To.String())
require.Nil(t, blockNumber)
}).Return(hexutil.MustDecode(expectedPriceHex), nil).Once()

price, err := oracle.GetDAGasPrice(tests.Context(t))
require.NoError(t, err)
require.Equal(t, big.NewInt(50), price)
})

t.Run("malformed custom API response falls back to default OP gas oracle API", func(t *testing.T) {
ethClient := setupUpgradeCheck(t, oracleAddress, true, false)
mockBatchContractCall(t, ethClient, oracleAddress, baseFee, baseFeeScalar, blobBaseFee, blobBaseFeeScalar, decimals)
malformedCustomGasAPIResponse := "0x0000000000000000000000000000000000000032"

daOracle := CreateTestDAOracle(t, toml.OPOracle, oracleAddress, "0x0000000000000000000000000000000000001234")
oracle, err := NewOpStackL1GasOracle(logger.Test(t), ethClient, chaintype.ChainOptimismBedrock, daOracle)
require.NoError(t, err)

ethClient.On("CallContract", mock.Anything, mock.IsType(ethereum.CallMsg{}), mock.IsType(&big.Int{})).Run(func(args mock.Arguments) {
callMsg := args.Get(1).(ethereum.CallMsg)
blockNumber := args.Get(2).(*big.Int)
require.NotNil(t, callMsg.To)
require.Equal(t, oracleAddress, callMsg.To.String())
require.Nil(t, blockNumber)
}).Return(hexutil.MustDecode(malformedCustomGasAPIResponse), nil).Once()

gasPrice, err := oracle.GetDAGasPrice(tests.Context(t))
require.NoError(t, err)
scaledGasPrice := big.NewInt(16125000000) // baseFee * scalar * 16 + blobBaseFee * scalar
scale := big.NewInt(16000000) // Scaled by 16 * 10 ^ decimals
expectedGasPrice := new(big.Int).Div(scaledGasPrice, scale)
assert.Equal(t, expectedGasPrice, gasPrice)
})
}

0 comments on commit b298388

Please sign in to comment.