Introduction
Polygon is a Layer 2 scaling solution for Ethereum. It uses a Proof-of-Stake consensus mechanism, which means that miners are rewarded for processing transactions. Miners are free to choose which transactions they want to process, and they will only process transactions that have a high enough gas price.
The “transaction underpriced” error occurs when the gas price specified for a transaction is too low. This can cause the transaction to be rejected by the miners and not included in a block.
Why does this error happen?
Polygon wanted to keep backwards compatibility with Geth, which is a popular Ethereum client. However, Geth does not support EIP-1559. So, Polygon decided to implement a “fake” version of EIP-1559. In this fake version, the baseFee is hard-coded and the priorityFee is allowed to float.
This has two negative consequences:
- The baseFee is not predictable, which makes it difficult to estimate the cost of transactions.
- The priorityFee can lead to bidding wars, which can drive up gas fees even further.
In short, Polygon’s attempt to keep backwards compatibility with Geth has made the problem of high gas fees worse.
Here are some additional information:
- EIP-1559: This is a proposal that was implemented on Ethereum in August 2021. It has two main goals: to make gas fees more predictable and to burn a portion of the gas fees paid, which reduces the overall supply of ETH.
- BaseFee: This is the minimum fee that must be paid for a transaction to be included in a block. It is set by miners and can fluctuate based on network congestion.
- PriorityFee: This is an additional fee that can be paid to miners to prioritize a transaction.
How to rectify the error
The Polygon error can be fixed by using a gas station to estimate the gas price that is likely to get a transaction included in a block within a reasonable time. The gas station will track the mempool and generate histograms that show the relationship between gas price and wait time. These histograms can then be used to pick a gas price that is likely to get a transaction included in a block within a few minutes.
However, it is important to note that gas stations are not always accurate. During periods of high network congestion, the gas price may be higher than the gas station predicts. In these cases, it may be necessary to pay a higher gas price to get a transaction included in a block quickly.
Here are some additional explanations
- Gas station: This is a service that tracks the Ethereum mempool (the queue of transactions waiting to be included in a block) and provides information about the average gas price and wait time for transactions.
- Mempool: This is the queue of transactions waiting to be included in a block on a blockchain network.
- Gas price: This is the amount of ETH that is paid to miners to include a transaction in a block.
- Wait time: This is the amount of time it takes for a transaction to be included in a block.
- Histogram: This is a graph that shows the distribution of data. In the context of gas prices, a histogram would show the number of transactions that were included in blocks at different gas prices.
Sample Code
const gasStationURL = 'https://gasstation.polygon.technology/v2';
let maxFeePerGas = BigInt('80000000000'); // fallback to 80 gwei
let maxPriorityFeePerGas = BigInt('40000000000'); // fallback to 40 gwei
// Call this function every time before a contract call
async function setOptimalGas() {
try {
const { data } = await axios.get(gasStationURL);
maxFeePerGas = ethers.utils.parseUnits(
Math.ceil(data.fast.maxFee) + '',
'gwei'
);
maxPriorityFeePerGas = ethers.utils.parseUnits(
Math.ceil(data.fast.maxPriorityFee) + '',
'gwei'
);
} catch {
// ignore
}
return { maxFeePerGas, maxPriorityFeePerGas };
}
const optimalGasFee = await setOptimalGas();
await contractInstance.myMethod(myParams, optimalGasFee);
TL;DR
The “transaction underpriced” error on Polygon Mainnet occurs when the gas price specified for a transaction is too low. This can be caused by the baseFee being unpredictable or by bidding wars. To fix the error, you can use a gas station to estimate the gas price that is likely to get your transaction included in a block within a reasonable time.