Ethers.js – Equivalent Gas Preferences for Low, Medium, and High

ethers.jsgasgas-estimatepolygon

I'm finding polygon main network transfers to be quite slow, taking ~5 minutes on average. If I execute them on polygonscan directly, I can speed them up by setting the gas preference slider to High in my wallet. I understand that gas can be estimated and overridden in ethers.js, but what would that actually look like? Here's an idea but it doesn't seem right:

const estimate = await contract.estimateGas.myMethod(args);
const result = await contract.myMethod(args, {
  gas: estimate * 1.1 // what would be a good number for 50% improvement?
});

Best Answer

EDIT:

The code below does not work. Due to some networks returning gasPrices with precision in the single wei dimension, just multiplying it by 1.x doesn't give a whole number value as a result. I wrote a method that converts to ether, multiplies, and converts back to wei with truncation of the extra precision digits:

async function getGasPrice(provider, mul) {
  const price = await provider.getGasPrice();
  const str = ethers.utils.formatEther(price);
  const eth = str * mul;
  return ethers.utils.parseEther(eth.toFixed(18));
}

Original Answer:

I think this is a good way of doing it. Set your percentage preference to your liking, I think .75 to 1.5 is a good bet:

const gasPrice = await provider.getGasPrice();
const tx = await contract.myMethod(args, {
  gasPrice: gasPrice * 1.5, // +50%
});