Skip to content

Commit

Permalink
fix tests and some more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ogtownsend committed Oct 3, 2024
1 parent 51140a9 commit 2c95e64
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 46 deletions.
6 changes: 3 additions & 3 deletions core/chains/evm/gas/rollups/l1_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestL1Oracle(t *testing.T) {
t.Run("Unsupported ChainType returns nil", func(t *testing.T) {
ethClient := mocks.NewL1OracleClient(t)

daOracle := CreateTestDAOracle(t, toml.OPOracle, "0x420000000000000000000000000000000000000F", "")
daOracle := CreateTestDAOracle(t, toml.OPOracle, OPGasOracleAddress, "")
oracle, err := NewL1GasOracle(logger.Test(t), ethClient, chaintype.ChainCelo, daOracle)
require.NoError(t, err)
assert.Nil(t, oracle)
Expand All @@ -42,7 +42,7 @@ func TestL1Oracle_GasPrice(t *testing.T) {
t.Run("Calling GasPrice on unstarted L1Oracle returns error", func(t *testing.T) {
ethClient := mocks.NewL1OracleClient(t)

daOracle := CreateTestDAOracle(t, toml.OPOracle, "0x420000000000000000000000000000000000000F", "")
daOracle := CreateTestDAOracle(t, toml.OPOracle, OPGasOracleAddress, "")
oracle, err := NewL1GasOracle(logger.Test(t), ethClient, chaintype.ChainOptimismBedrock, daOracle)
require.NoError(t, err)

Expand Down Expand Up @@ -124,7 +124,7 @@ func TestL1Oracle_GasPrice(t *testing.T) {
assert.Nil(t, blockNumber)
}).Return(common.BigToHash(l1BaseFee).Bytes(), nil)

daOracle := CreateTestDAOracle(t, toml.OPOracle, "0x420000000000000000000000000000000000000F", "")
daOracle := CreateTestDAOracle(t, toml.OPOracle, OPGasOracleAddress, "")
oracle, err := NewL1GasOracle(logger.Test(t), ethClient, chaintype.ChainOptimismBedrock, daOracle)
require.NoError(t, err)
servicetest.RunHealthy(t, oracle)
Expand Down
69 changes: 27 additions & 42 deletions core/chains/evm/gas/rollups/op_l1_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,73 +102,45 @@ func NewOpStackL1GasOracle(lggr logger.Logger, ethClient l1OracleClient, chainTy

// encode calldata for each method; these calldata will remain the same for each call, we can encode them just once
// Encode calldata for l1BaseFee method
l1BaseFeeMethodAbi, err := abi.JSON(strings.NewReader(L1BaseFeeAbiString))
l1BaseFeeCalldata, _, err := encodeCalldata(L1BaseFeeAbiString, l1BaseFeeMethod)
if err != nil {
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() method ABI for chain: %s; %w", l1BaseFeeMethod, chainType, err)
}
l1BaseFeeCalldata, err := l1BaseFeeMethodAbi.Pack(l1BaseFeeMethod)
if err != nil {
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() calldata for chain: %s; %w", l1BaseFeeMethod, chainType, err)
return nil, fmt.Errorf("failed to encode l1BaseFee calldata: %w", err)
}

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

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

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

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

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

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

return &optimismL1Oracle{
Expand Down Expand Up @@ -242,6 +214,7 @@ func (o *optimismL1Oracle) run() {
}
}
}

func (o *optimismL1Oracle) refresh() {
err := o.refreshWithError()
if err != nil {
Expand Down Expand Up @@ -552,3 +525,15 @@ func (o *optimismL1Oracle) getEcotoneFjordGasPrice(ctx context.Context) (*big.In

return new(big.Int).Div(scaledGasPrice, scale), nil
}

func encodeCalldata(abiString, methodName string) ([]byte, abi.ABI, error) {
methodAbi, err := abi.JSON(strings.NewReader(abiString))
if err != nil {
return nil, abi.ABI{}, fmt.Errorf("failed to parse ABI: %w", err)
}
calldata, err := methodAbi.Pack(methodName)
if err != nil {
return nil, abi.ABI{}, fmt.Errorf("failed to pack calldata: %w", err)
}
return calldata, methodAbi, nil
}
2 changes: 1 addition & 1 deletion core/config/docs/chains-evm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ OracleType = 'OP' # Example
# OracleAddress is the address of the oracle contract.
OracleAddress = '0x420000000000000000000000000000000000000F' # Example
# CustomGasPriceAPICalldata is optional and can be set to call a custom gas price API at the given OracleAddress.
CustomGasPriceAPICalldata = ''
CustomGasPriceAPICalldata = '' # Default

[EVM.GasEstimator.LimitJobType]
# OCR overrides LimitDefault for OCR jobs.
Expand Down
3 changes: 3 additions & 0 deletions core/services/chainlink/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,9 @@ func TestConfig_full(t *testing.T) {
if got.EVM[c].Transactions.AutoPurge.DetectionApiUrl == nil {
got.EVM[c].Transactions.AutoPurge.DetectionApiUrl = new(commoncfg.URL)
}
if got.EVM[c].GasEstimator.DAOracle.OracleAddress == nil {
got.EVM[c].GasEstimator.DAOracle.OracleAddress = new(types.EIP55Address)
}
}

cfgtest.AssertFieldsNotNil(t, got)
Expand Down
27 changes: 27 additions & 0 deletions docs/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9243,6 +9243,33 @@ TipCapMinimum is the minimum gas tip to use when submitting transactions to the

(Only applies to EIP-1559 transactions)

## EVM.GasEstimator.DAOracle
```toml
[EVM.GasEstimator.DAOracle]
OracleType = 'OP' # Example
OracleAddress = '0x420000000000000000000000000000000000000F' # Example
CustomGasPriceAPICalldata = '' # Default
```


### OracleType
```toml
OracleType = 'OP' # Example
```
OracleType refers to the oracle family this config belongs to (OP, Arbitrum, etc).

### OracleAddress
```toml
OracleAddress = '0x420000000000000000000000000000000000000F' # Example
```
OracleAddress is the address of the oracle contract.

### CustomGasPriceAPICalldata
```toml
CustomGasPriceAPICalldata = '' # Default
```
CustomGasPriceAPICalldata is optional and can be set to call a custom gas price API at the given OracleAddress.

## EVM.GasEstimator.LimitJobType
```toml
[EVM.GasEstimator.LimitJobType]
Expand Down

0 comments on commit 2c95e64

Please sign in to comment.