From c2719623be880d693b2245111c2a7758887e8509 Mon Sep 17 00:00:00 2001 From: "valerii.kabisov" Date: Tue, 24 Sep 2024 01:19:46 +0900 Subject: [PATCH] [CCIP-3376] revert --- .../interceptors/mantle/interceptor.go | 67 +++---------------- .../interceptors/mantle/interceptor_test.go | 48 +++---------- 2 files changed, 17 insertions(+), 98 deletions(-) diff --git a/core/services/ocr2/plugins/ccip/estimatorconfig/interceptors/mantle/interceptor.go b/core/services/ocr2/plugins/ccip/estimatorconfig/interceptors/mantle/interceptor.go index f21f60b5b9..5858230e0b 100644 --- a/core/services/ocr2/plugins/ccip/estimatorconfig/interceptors/mantle/interceptor.go +++ b/core/services/ocr2/plugins/ccip/estimatorconfig/interceptors/mantle/interceptor.go @@ -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" @@ -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 - 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 { @@ -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, } } @@ -76,17 +61,13 @@ 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, @@ -94,40 +75,8 @@ func (i *Interceptor) getMantleTokenRatio(ctx context.Context) (decimal.Decimal, }, 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 } diff --git a/core/services/ocr2/plugins/ccip/estimatorconfig/interceptors/mantle/interceptor_test.go b/core/services/ocr2/plugins/ccip/estimatorconfig/interceptors/mantle/interceptor_test.go index a555ffcc42..762d3aabd5 100644 --- a/core/services/ocr2/plugins/ccip/estimatorconfig/interceptors/mantle/interceptor_test.go +++ b/core/services/ocr2/plugins/ccip/estimatorconfig/interceptors/mantle/interceptor_test.go @@ -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()) @@ -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), }, } @@ -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())