4

How can I set the gasPrice in a contract interaction using ethers.js? I'm trying to override the gasPrice in the code below:

let txPromise = contract.populateTransaction.runAdventureVRF(0, false, { gasPrice: 800000 })

walletSigner.sendTransaction(txPromise)

and i'm receiving the error transaction underpriced. If i try to log txPromise.overrides it is undefined, which makes me think the gas price is never being set.

docs

additional code

const provider = new ethers.providers.AlchemyProvider('matic', process.env.ALCHEMY_API_KEY)
const wallet = new ethers.Wallet(process.env.PK)
const abi = '[{"inputs":[{"internalType":"uint256","name":"_teamId","type":"uint256"},{"internalType":"bool","name":"_energy","type":"bool"}],"name":"runAdventureVRF","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"}]'
const contract = new ethers.Contract(address, abi, provider);
let walletSigner = wallet.connect(provider)
user2954587
  • 4,661
  • 6
  • 43
  • 101
  • Did you figure this out? I'm either getting your same error, or It assigns 1.5 gwei as the default and then hangs indefinitely. Useless. – Sum None Apr 14 '22 at 19:01

2 Answers2

2

One solution is to add the gas price as an override to your transaction:

walletSigner.sendTransaction(txPromise, {gasPrice: ethers.utils.parseUnits('100', 'gwei'), gasLimit: 1000000});

This is the only solution I could find as getGasPrice() and estimateGas() were returning a empty json array. I don't know if it has to do with the provider (alchemy) or why those are failing.

The specific syntax to override the gas price is here:

Ethers Override Transaction Gas Price Manually

It's also in the following Ethers docs, but not as clear (if you don't know the syntax):

Ethers Contract Functions Send

At the time of this post, the default gas value of all my transactions is 1.5 gwei which is nowhere near enough on the matic network. This will also hang up the transaction indefinitely until you send a new transaction with the same nonce value. Even if you resolve your gas issue, until you deal with the hung transaction, other transactions will stack up behind it.

Sum None
  • 2,164
  • 3
  • 27
  • 32
  • 1
    Note that the second link "Ethers Contract Function Calls" is for read-only contract methods which do not have a gas price since there is no transaction. The correct link is the write contract method: https://docs.ethers.io/v5/api/contract/contract/#contract-functionsSend – Tomiwa Jun 18 '22 at 13:44
  • 1
    Thanks, good eye. Updated. By the time I found that info (in the original second link), I had already basically solved my issue and didn't give it much thought, other than something a little more official than a github comment. – Sum None Jun 18 '22 at 15:54
0

If you just want to increase gasLimit by a certain amount, you can make a helper function to do so:

const increaseGasLimit = (estimatedGasLimit: BigNumber) => {
  return estimatedGasLimit.mul(130).div(100) // increase by 30%
}

And use it like this:

const estimatedGas = await contract.estimateGas.method(args)
const tx = await contract.method(args, { gasLimit: increaseGasLimit(estimatedGas) })
irondsd
  • 1,140
  • 1
  • 17
  • 34