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

     1  package evmgaspricer
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  
     7  	"github.com/rs/zerolog/log"
     8  )
     9  
    10  // StaticGasPriceDeterminant for when you want to always use generic `GasPrice()` method from an EVM client.
    11  //
    12  // Client should implement `GasPrice()` to query first for a gas price field that is set on client construction
    13  // This way a developer can use a specific gas price for transactions, such as in the CLI
    14  //
    15  // Currently, if the client being used is created by the `EVMClientFromParams` constructor a constant gas price is then set
    16  // and will be returned by this gas pricer
    17  type StaticGasPriceDeterminant struct {
    18  	client GasPriceClient
    19  	opts   *GasPricerOpts
    20  }
    21  
    22  func NewStaticGasPriceDeterminant(client GasPriceClient, opts *GasPricerOpts) *StaticGasPriceDeterminant {
    23  	return &StaticGasPriceDeterminant{client: client, opts: opts}
    24  
    25  }
    26  
    27  func (gasPricer *StaticGasPriceDeterminant) SetClient(client LondonGasClient) {
    28  	gasPricer.client = client
    29  }
    30  func (gasPricer *StaticGasPriceDeterminant) SetOpts(opts *GasPricerOpts) {
    31  	gasPricer.opts = opts
    32  }
    33  
    34  func (gasPricer *StaticGasPriceDeterminant) GasPrice(priority *uint8) ([]*big.Int, error) {
    35  	gp, err := gasPricer.client.SuggestGasPrice(context.TODO())
    36  	log.Debug().Msgf("Suggested GP %s", gp.String())
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	if gasPricer.opts != nil {
    41  		if gasPricer.opts.GasPriceFactor != nil {
    42  			gp = multiplyGasPrice(gp, gasPricer.opts.GasPriceFactor)
    43  		}
    44  		if gasPricer.opts.UpperLimitFeePerGas != nil {
    45  			if gp.Cmp(gasPricer.opts.UpperLimitFeePerGas) == 1 {
    46  				gp = gasPricer.opts.UpperLimitFeePerGas
    47  			}
    48  		}
    49  	}
    50  	gasPrices := make([]*big.Int, 1)
    51  	gasPrices[0] = gp
    52  	return gasPrices, nil
    53  }