As the Polygon network is a decentralised blockchain with no singular entity or authority overseeing its operation, a mechanism is put in place to avoid the network from being congested or spammed with transactions. This mechanism charges senders of transactions with a small fee called gas fee which is then used to reward validators who validate transactions on the network.
This fee is in the form of MATIC which is also used on the Polygon network to facilitate value transfers, payouts to validators and smart contract executions.
How gas fee works is similar to how gas/fuel works for our vehicles. In order to get from Point A to Point B, the vehicle needs to be filled up with a sufficient amount of gas.
Similarly, for a transaction on the Polygon network to be successful, the sender must provide a sufficient amount of gas to pay for gas fees.
How To Provide Gas Fee For A Transaction?
The gas fee is deducted from the remaining MATIC balance of your address and not from the amount of MATIC or tokens that you are sending. It is also deducted automatically/concurrently with your transaction in a single event so you do not have to worry about "forgetting" to pay gas fees.
As an example, say you have a balance of 10 MATIC in your address and are looking to send 1 MATIC to a friend of yours. The gas fee for this transaction is (as an example) 0.5 MATIC. From such, your friend will receive 1 MATIC while the gas fee of 0.5 MATIC is deducted from your balance. Both the 1 MATIC and 0.5 MATIC will be deducted in one single transaction hash simultaneously. Once this transaction is successful, your address should have a balance of 8.5 MATIC left.
How To Set/Calculate Gas Fee?
Depending on the wallet used, you may or may not be able to set the gas fee manually. With that said, in setting the gas fee there are two variables to keep in mind.
Gas Price: Similar to how petroleum gas/fuel is priced (price per litre, e.g.: $1/litre), gas price is the price of a unit of gas that a user is willing to pay for a transaction (Gas price is calculated in GWEI, 1 GWEI is 0.000000001 MATIC).
Gas Limit: Continuing the petroleum analogy, gas limit is how much gas a user is willing to allocate for the transaction.
In conclusion, Gas Fee = Gas Price x Gas Limit.
As an example, say the price of petroleum is $1 a litre and we are estimating that 10 litres of it would be enough for a road trip. We will need to allocate 10 litres of $1/litre fuel for the trip, which amounts to a total of $10 that we need to have prepared for fuel.
Similarly, if we set the gas price at 1 GWEI and the gas limit at 21,000, we will need to have at least 0.000021 MATIC (gas price x gas limit / 0.000000001 x 21,000) inside of our wallet to pay for the transaction's gas fee.
Do take note that for a normal transaction (sending MATIC), a gas limit of 21,000 is often enough. But for a transaction that involves interacting with a smart contract, 21,000 is not enough. If you are interacting with smart contracts, please set a higher gas limit.
How Much Is Enough?
As gas fee is ultimately used as an incentive for validators in the Polygon network, the answer to this question can never be one objective figure. This is because gas fee is subject to the congestion of the blockchain at any given time. In times of higher congestion, you would usually need to set a higher gas fee than in times of normal congestion.
To tackle the subjectivity of gas fee, Polygonscan has a feature called Gas Tracker which you can refer to here. It analyses the traffic on the blockchain and recommends suitable gas prices for transactions to be made at a particular time.
Out of Gas
When a transaction has failed due to an “Out of Gas” error, this means the gas limit set for a transaction is below the required gas needed to execute it. The transaction value does not leave your address but gas fee is deducted because of the computational cost incurred. You will need to reinitiate the transaction with an appropriate gas limit. Wallet services will usually suggest a gas limit for your transactions.
Alternatively, you can also look at similar/past transactions made using related contracts which have been successfully processed to estimate a suitable gas limit to set.
Possible reasons for an “Out of Gas” error warning:
- Insufficient amount of gas limit provided.
- If you are interacting with a Contract, the minimum amount of gas limit required is > 21,000 (try increasing your gas limit)
- Contract execution exceeds the minimum gas limit required
- Contract execution error occured, but the Virtual Machine (VM) execution continues until all of the gas limit is exhausted.
Always remember to have a little extra MATIC than you need inside your address. Regardless of whether you are sending MATIC or tokens, please take note that the gas fee is always in the form of MATIC and it is always wise to keep a little extra MATIC inside your address for gas purposes.