Ethers.js: Set default gas price per signer/provider

Created on 27 Jun 2021  ·  3Comments  ·  Source: ethers-io/ethers.js

Is your feature request related to a problem? Please describe.
Often I want to execute all tx with the same gas price (ex. kovan, or rollups without any gas market).

Describe the solution you'd like
I would like to provide a default gas price per signer or provider so all transactions share the same price. The alternative is passing gasPrice explicitly to each send tx.

Additional context
I feel like it should be easy to implement by overwriting getGasPrice on Signer. Is there a better/easier way?

All 3 comments

Okay, it's pretty trivial in the userland:

export class FixedGasPriceProvider extends JsonRpcProvider {
  constructor(url: string, public defaultGasPrice: number) {
    super(url)
  }

  async getGasPrice() {
    return BigNumber.from(this.defaultGasPrice.toString())
  }
}

I read this as I rolled out of bed, but you answered you own questions before I had breakfast and and got to my computer. :)

Also note that the new getFeeData() calls this.getGasPrice(), so it works in that API too. :)

(Oh, but you should make the input a BigNumberish, and since BigNumbers are immutable, you can set it once in the constructor as a BigNumber and avoid doing it every time in the getGasPrice())

Was this page helpful?
0 / 5 - 0 ratings