Skip to content

Commit

Permalink
[CCIP-3376] revert
Browse files Browse the repository at this point in the history
  • Loading branch information
valerii-kabisov-cll committed Sep 23, 2024
1 parent fbbbb43 commit c271962
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/shopspring/decimal"

evmClient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups"
Expand All @@ -23,20 +22,17 @@ const (
// tokenRatio fetches the tokenRatio used for Mantle's gas price calculation
tokenRatioMethod = "tokenRatio"
mantleTokenRatioAbiString = `[{"inputs":[],"name":"tokenRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]`
// decimals fetches the number of chainDecimals used in the scalar for gas price calculation
decimalsMethod = "decimals"
)

type Interceptor struct {
client evmClient.Client
tokenRatioCallData []byte
decimalsCallData []byte

Check failure on line 30 in core/services/ocr2/plugins/ccip/estimatorconfig/interceptors/mantle/interceptor.go

View workflow job for this annotation

GitHub Actions / lint

field `decimalsCallData` is unused (unused)
tokenRatio decimal.Decimal
chainDecimals *big.Int
tokenRatio *big.Int
tokenRatioLastUpdate time.Time
}

func NewInterceptor(ctx context.Context, client evmClient.Client) *Interceptor {
func NewInterceptor(_ context.Context, client evmClient.Client) *Interceptor {
// Encode calldata for tokenRatio method
tokenRatioMethodAbi, err := abi.JSON(strings.NewReader(mantleTokenRatioAbiString))
if err != nil {
Expand All @@ -47,20 +43,9 @@ func NewInterceptor(ctx context.Context, client evmClient.Client) *Interceptor {
log.Panicf("failed to parse GasPriceOracle %s() calldata for Mantle; %v", tokenRatioMethod, err)
}

// Encode calldata for decimals method
decimalsMethodAbi, err := abi.JSON(strings.NewReader(rollups.OPDecimalsAbiString))
if err != nil {
log.Panicf("failed to parse GasPriceOracle %s() method ABI for Mantle; %v", decimalsMethod, err)
}
decimalsCallData, err := decimalsMethodAbi.Pack(decimalsMethod)
if err != nil {
log.Panicf("failed to parse GasPriceOracle %s() calldata for Mantle; %v", decimalsMethod, err)
}

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

Expand All @@ -76,58 +61,22 @@ func (i *Interceptor) ModifyGasPriceComponents(ctx context.Context, execGasPrice
}

// multiply daGasPrice and execGas price by tokenRatio
dExecGasPrice := decimal.NewFromBigInt(execGasPrice, 0)
newExecGasPrice := dExecGasPrice.Mul(i.tokenRatio).BigInt()

dDAGasPrice := decimal.NewFromBigInt(daGasPrice, 0)
newDAGasPrice := dDAGasPrice.Mul(i.tokenRatio).BigInt()

newExecGasPrice := new(big.Int).Mul(execGasPrice, i.tokenRatio)
newDAGasPrice := new(big.Int).Mul(daGasPrice, i.tokenRatio)
return newExecGasPrice, newDAGasPrice, nil
}

// getMantleTokenRatio Requests and returns the token ratio value for the Mantle chain.
func (i *Interceptor) getMantleTokenRatio(ctx context.Context) (decimal.Decimal, error) {
// getMantleTokenRatio Requests and returns a token ratio value for the Mantle chain.
func (i *Interceptor) getMantleTokenRatio(ctx context.Context) (*big.Int, error) {
precompile := common.HexToAddress(rollups.OPGasOracleAddress)
tokenRatio, err := i.client.CallContract(ctx, ethereum.CallMsg{
To: &precompile,
Data: i.tokenRatioCallData,
}, nil)

if err != nil {
return decimal.Zero, fmt.Errorf("getMantleTokenRatio call failed: %w", err)
}

bigIntTokenRatio := new(big.Int).SetBytes(tokenRatio)

// request chainDecimals value once, it rarely changed and use cached value during the app lifecycle
if i.chainDecimals == nil {
decimals, err := i.getMantleDecimals(ctx)
if err != nil {
return decimal.Zero, err
}

i.chainDecimals = decimals
}

// convert bigInt token ratio to the decimal format
// rawTokenRatio = bigIntTokenRatio * 10 ^ -i.chainDecimals
exp := int32(-1 * i.chainDecimals.Int64())
rawTokenRatio := decimal.NewFromBigInt(bigIntTokenRatio, exp)

return rawTokenRatio, nil
}

// getMantleDecimals Requests and returns the decimals value for the Mantle chain.
func (i *Interceptor) getMantleDecimals(ctx context.Context) (*big.Int, error) {
precompile := common.HexToAddress(rollups.OPGasOracleAddress)
decimals, err := i.client.CallContract(ctx, ethereum.CallMsg{
To: &precompile,
Data: i.decimalsCallData,
}, nil)

if err != nil {
return nil, fmt.Errorf("getMantleDecimals call failed: %w", err)
return nil, fmt.Errorf("getMantleTokenRatio call failed: %w", err)
}

return new(big.Int).SetBytes(decimals), nil
return new(big.Int).SetBytes(tokenRatio), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@ func TestInterceptor(t *testing.T) {
ethClient := mocks.NewClient(t)
ctx := context.Background()

tokenRatio := big.NewInt(100)
decimals := big.NewInt(1)
tokenRatio := big.NewInt(10)
interceptor := NewInterceptor(ctx, ethClient)

// request token ratio
ethClient.On("CallContract", ctx, mock.IsType(ethereum.CallMsg{}), mock.IsType(&big.Int{})).
Return(common.BigToHash(tokenRatio).Bytes(), nil).Once()

// request decimals
ethClient.On("CallContract", ctx, mock.IsType(ethereum.CallMsg{}), mock.IsType(&big.Int{})).
Return(common.BigToHash(decimals).Bytes(), nil).Once()

modExecGasPrice, modDAGasPrice, err := interceptor.ModifyGasPriceComponents(ctx, big.NewInt(1), big.NewInt(1))
require.NoError(t, err)
require.Equal(t, int64(10), modExecGasPrice.Int64())
Expand All @@ -48,55 +43,34 @@ func TestModifyGasPriceComponents(t *testing.T) {
tokenRatio *big.Int
resultExecGasPrice *big.Int
resultDAGasPrice *big.Int
decimals *big.Int
}{
"regular": {
execGasPrice: big.NewInt(1000),
daGasPrice: big.NewInt(100),
resultExecGasPrice: big.NewInt(500),
resultDAGasPrice: big.NewInt(50),
tokenRatio: big.NewInt(500), // 0.5
decimals: big.NewInt(3),
resultExecGasPrice: big.NewInt(2000),
resultDAGasPrice: big.NewInt(200),
tokenRatio: big.NewInt(2),
},
"zero DAGasPrice": {
execGasPrice: big.NewInt(1000),
daGasPrice: big.NewInt(0),
resultExecGasPrice: big.NewInt(500),
resultExecGasPrice: big.NewInt(5000),
resultDAGasPrice: big.NewInt(0),
tokenRatio: big.NewInt(5), // 0.5
decimals: big.NewInt(1),
tokenRatio: big.NewInt(5),
},
"zero ExecGasPrice": {
execGasPrice: big.NewInt(0),
daGasPrice: big.NewInt(10),
resultExecGasPrice: big.NewInt(0),
resultDAGasPrice: big.NewInt(5),
tokenRatio: big.NewInt(5), // 0.5
decimals: big.NewInt(1),
resultDAGasPrice: big.NewInt(50),
tokenRatio: big.NewInt(5),
},
"zero token ratio": {
execGasPrice: big.NewInt(15),
daGasPrice: big.NewInt(10),
resultExecGasPrice: big.NewInt(0),
resultDAGasPrice: big.NewInt(0),
tokenRatio: big.NewInt(0), // 0
decimals: big.NewInt(6),
},
"result under precision": {
execGasPrice: big.NewInt(10),
daGasPrice: big.NewInt(0),
resultExecGasPrice: big.NewInt(0),
resultDAGasPrice: big.NewInt(0),
tokenRatio: big.NewInt(1), // 0.01
decimals: big.NewInt(2),
},
"missing decimal places": {
execGasPrice: big.NewInt(1234),
daGasPrice: big.NewInt(0),
resultExecGasPrice: big.NewInt(12),
resultDAGasPrice: big.NewInt(0),
tokenRatio: big.NewInt(1), // 0.01
decimals: big.NewInt(2),
tokenRatio: big.NewInt(0),
},
}

Expand All @@ -111,10 +85,6 @@ func TestModifyGasPriceComponents(t *testing.T) {
ethClient.On("CallContract", ctx, mock.IsType(ethereum.CallMsg{}), mock.IsType(&big.Int{})).
Return(common.BigToHash(tc.tokenRatio).Bytes(), nil).Once()

// request decimals
ethClient.On("CallContract", ctx, mock.IsType(ethereum.CallMsg{}), mock.IsType(&big.Int{})).
Return(common.BigToHash(tc.decimals).Bytes(), nil).Once()

modExecGasPrice, modDAGasPrice, err := interceptor.ModifyGasPriceComponents(ctx, tc.execGasPrice, tc.daGasPrice)
require.NoError(t, err)
require.Equal(t, tc.resultExecGasPrice.Int64(), modExecGasPrice.Int64())
Expand Down

0 comments on commit c271962

Please sign in to comment.