github.com/ChainSafe/chainbridge-core@v1.4.2/chains/evm/calls/evmgaspricer/london_test.go (about)

     1  package evmgaspricer
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	mock_evmgaspricer "github.com/ChainSafe/chainbridge-core/chains/evm/calls/evmgaspricer/mock"
     8  
     9  	"github.com/golang/mock/gomock"
    10  	"github.com/stretchr/testify/suite"
    11  )
    12  
    13  type LondonGasPriceTestSuite struct {
    14  	suite.Suite
    15  	gasPricerMock *mock_evmgaspricer.MockLondonGasClient
    16  }
    17  
    18  func TestRuLondonTestSuite(t *testing.T) {
    19  	suite.Run(t, new(LondonGasPriceTestSuite))
    20  }
    21  
    22  func (s *LondonGasPriceTestSuite) SetupSuite()    {}
    23  func (s *LondonGasPriceTestSuite) TearDownSuite() {}
    24  func (s *LondonGasPriceTestSuite) SetupTest() {
    25  	gomockController := gomock.NewController(s.T())
    26  	s.gasPricerMock = mock_evmgaspricer.NewMockLondonGasClient(gomockController)
    27  }
    28  func (s *LondonGasPriceTestSuite) TearDownTest() {}
    29  
    30  func (s *LondonGasPriceTestSuite) TestLondonGasPricerNoOpts() {
    31  	twentyGwei := big.NewInt(20000000000)
    32  	twoGwei := big.NewInt(2000000000)
    33  	gpd := NewLondonGasPriceClient(s.gasPricerMock, nil)
    34  	s.gasPricerMock.EXPECT().BaseFee().Return(twentyGwei, nil)
    35  	s.gasPricerMock.EXPECT().SuggestGasTipCap(gomock.Any()).Return(twoGwei, nil)
    36  
    37  	res, err := gpd.GasPrice(nil)
    38  	s.Nil(err)
    39  	s.Equal(len(res), 2)
    40  	s.Equal(res[0].Cmp(twoGwei), 0)
    41  	s.Equal(0, res[1].Cmp(big.NewInt(42000000000))) // Base fee 20Gwei * 2 + maxTipCap = 42Gwei
    42  }
    43  
    44  func (s *LondonGasPriceTestSuite) TestLondonGasPricerWithUpperLimit() {
    45  	twentyGwei := big.NewInt(20000000000)
    46  	thirtyGwei := big.NewInt(30000000000)
    47  	twoGwei := big.NewInt(2000000000)
    48  	gpd := NewLondonGasPriceClient(s.gasPricerMock, &GasPricerOpts{UpperLimitFeePerGas: thirtyGwei})
    49  	s.gasPricerMock.EXPECT().BaseFee().Return(twentyGwei, nil)
    50  	s.gasPricerMock.EXPECT().SuggestGasTipCap(gomock.Any()).Return(twoGwei, nil)
    51  
    52  	res, err := gpd.GasPrice(nil)
    53  	s.Nil(err)
    54  	s.Equal(len(res), 2)
    55  	s.Equal(res[0].Cmp(big.NewInt(10000000000)), 0) // 10 gwei. Bcs MaxFee is UpperLimit and BaseFee is 20Gwei so PriorityFee is 30-20=10Gwei
    56  	s.Equal(0, res[1].Cmp(thirtyGwei))              // Equals to UpperLimit
    57  }
    58  
    59  func (s *LondonGasPriceTestSuite) TestLondonGasPricerWithUpperLimitLowerBaseFee() {
    60  	twentyGwei := big.NewInt(20000000000)
    61  	thirtyGwei := big.NewInt(30000000000)
    62  	gpd := NewLondonGasPriceClient(s.gasPricerMock, &GasPricerOpts{UpperLimitFeePerGas: twentyGwei})
    63  	s.gasPricerMock.EXPECT().BaseFee().Return(thirtyGwei, nil)
    64  	//s.gasPricerMock.EXPECT().SuggestGasTipCap(gomock.Any()).Return(twoGwei, nil) // Code is not get to the point where this call happens
    65  
    66  	res, err := gpd.GasPrice(nil)
    67  	s.Nil(err)
    68  	s.Equal(len(res), 2)
    69  	s.Equal(res[0].Cmp(big.NewInt(TwoAndTheHalfGwei)), 0) // Lowest MaxPriorityFee
    70  	s.Equal(0, res[1].Cmp(big.NewInt(32500000000)))       // Equals to BaseFee  + MaxPriorityFee (22,5 gwei)
    71  }