Skip to content

Commit

Permalink
[CCIP-3376] refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
valerii-kabisov-cll committed Sep 24, 2024
1 parent e9ba06a commit 5afd6f5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mantle
import (
"context"
"fmt"
"log"
"math/big"
"strings"
"time"
Expand Down Expand Up @@ -31,21 +30,21 @@ type Interceptor struct {
tokenRatioLastUpdate time.Time
}

func NewInterceptor(_ context.Context, client evmClient.Client) *Interceptor {
func NewInterceptor(_ context.Context, client evmClient.Client) (*Interceptor, error) {
// Encode calldata for tokenRatio method
tokenRatioMethodAbi, err := abi.JSON(strings.NewReader(mantleTokenRatioAbiString))
if err != nil {
log.Panicf("failed to parse GasPriceOracle %s() method ABI for Mantle; %v", tokenRatioMethod, err)
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() method ABI for Mantle; %v", tokenRatioMethod, err)
}
tokenRatioCallData, err := tokenRatioMethodAbi.Pack(tokenRatioMethod)
if err != nil {
log.Panicf("failed to parse GasPriceOracle %s() calldata for Mantle; %v", tokenRatioMethod, err)
return nil, fmt.Errorf("failed to parse GasPriceOracle %s() calldata for Mantle; %v", tokenRatioMethod, err)
}

return &Interceptor{
client: client,
tokenRatioCallData: tokenRatioCallData,
}
}, nil
}

// ModifyGasPriceComponents returns modified gasPrice.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func TestInterceptor(t *testing.T) {
ctx := context.Background()

tokenRatio := big.NewInt(10)
interceptor := NewInterceptor(ctx, ethClient)
interceptor, err := NewInterceptor(ctx, ethClient)
require.NoError(t, err)

// request token ratio
ethClient.On("CallContract", ctx, mock.IsType(ethereum.CallMsg{}), mock.IsType(&big.Int{})).
Expand Down Expand Up @@ -79,7 +80,8 @@ func TestModifyGasPriceComponents(t *testing.T) {
ethClient := mocks.NewClient(t)
ctx := context.Background()

interceptor := NewInterceptor(ctx, ethClient)
interceptor, err := NewInterceptor(ctx, ethClient)
require.NoError(t, err)

// request token ratio
ethClient.On("CallContract", ctx, mock.IsType(ethereum.CallMsg{}), mock.IsType(&big.Int{})).
Expand Down
3 changes: 2 additions & 1 deletion core/services/ocr2/plugins/ccip/prices/da_price_estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func NewDAGasPriceEstimator(
}

func (g DAGasPriceEstimator) GetGasPrice(ctx context.Context) (*big.Int, error) {
gasPrice, err := g.execEstimator.GetGasPrice(ctx)
execGasPrice, err := g.execEstimator.GetGasPrice(ctx)
if err != nil {
return nil, err
}
var gasPrice *big.Int = execGasPrice

if gasPrice.BitLen() > int(g.priceEncodingLength) {
return nil, fmt.Errorf("native gas price exceeded max range %+v", gasPrice)
Expand Down
18 changes: 16 additions & 2 deletions core/services/relay/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,15 @@ func (r *Relayer) NewCCIPCommitProvider(rargs commontypes.RelayArgs, pargs commo
destStartBlock := commitPluginConfig.DestStartBlock

feeEstimatorConfig := estimatorconfig.NewFeeEstimatorConfigService()

// CCIPCommit reads only when source chain is Mantle, then reports to dest chain
// to minimize misconfigure risk, might make sense to wire Mantle only when Commit + Mantle + IsSourceProvider
if r.chain.Config().EVM().ChainType() == chaintype.ChainMantle && commitPluginConfig.IsSourceProvider {
feeEstimatorConfig.AddGasPriceInterceptor(mantle.NewInterceptor(ctx, r.chain.Client()))
mantleInterceptor, err := mantle.NewInterceptor(ctx, r.chain.Client())

Check failure on line 417 in core/services/relay/evm/evm.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "err" shadows declaration at line 405 (govet)
if err != nil {
return nil, err
}
feeEstimatorConfig.AddGasPriceInterceptor(mantleInterceptor)
}

// The src chain implementation of this provider does not need a configWatcher or contractTransmitter;
Expand Down Expand Up @@ -484,8 +491,15 @@ func (r *Relayer) NewCCIPExecProvider(rargs commontypes.RelayArgs, pargs commont
usdcConfig := execPluginConfig.USDCConfig

feeEstimatorConfig := estimatorconfig.NewFeeEstimatorConfigService()

// CCIPExec reads when dest chain is mantle, and uses it to calc boosting in batching
// to minimize misconfigure risk, make sense to wire Mantle only when Exec + Mantle + !IsSourceProvider
if r.chain.Config().EVM().ChainType() == chaintype.ChainMantle && !execPluginConfig.IsSourceProvider {
feeEstimatorConfig.AddGasPriceInterceptor(mantle.NewInterceptor(ctx, r.chain.Client()))
mantleInterceptor, err := mantle.NewInterceptor(ctx, r.chain.Client())

Check failure on line 498 in core/services/relay/evm/evm.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "err" shadows declaration at line 486 (govet)
if err != nil {
return nil, err
}
feeEstimatorConfig.AddGasPriceInterceptor(mantleInterceptor)
}

// The src chain implementation of this provider does not need a configWatcher or contractTransmitter;
Expand Down

0 comments on commit 5afd6f5

Please sign in to comment.