Polygon
Prêt à utiliser cela en production ?
Le niveau gratuit couvre les projets personnels. Le paiement à l'usage évolue sans carte bancaire.
Polygon
Le niveau gratuit couvre les projets personnels. Le paiement à l'usage évolue sans carte bancaire.
Every Polygon transaction pays for execution in MATIC, and the price it pays is set by block-level conditions — the base fee that rises and falls with demand, the priority tip validators collect, and the gas limit each Bor block can hold. Reading those conditions before you sign is what keeps an application reliable: a transaction priced too low stalls in the mempool, and one priced blindly high wastes user funds. With blocks arriving every ~2 seconds on chain 137, fees on Polygon are usually low but not static, and tracking them lets you confirm transactions promptly while keeping MATIC costs predictable. This guide shows how to read gas data from https://polygon.therpc.io/YOUR_API_KEY and turn it into pricing decisions that benefit both your users and your operations.
getBlockGasLimit example reads it from the latest block; your transaction's gas usage must stay under this ceiling, and a near-full block is a signal of congestion.baseFeePerGas (burned) plus a maxPriorityFeePerGas tip that goes to the validator. You set maxFeePerGas as the ceiling you will pay and maxPriorityFeePerGas as the tip; the network charges the current base fee plus your tip, refunding the difference. The getOptimalGasPrice example averages recent blocks' baseFeePerGas as a starting point.eth_estimateGas to size the gas limit for a specific call, and recent-block base-fee sampling to set the fee. Static estimates are simple but can under- or over-shoot during fast-moving congestion; sampling the last several blocks adapts to current conditions at the cost of extra RPC calls. Always add a margin to the estimated gas limit so a slightly heavier execution path does not run out of gas.baseFeePerGas from the last several Bor blocks right before signing and set maxFeePerGas to that base fee plus a buffer (commonly 1.5–2x to ride out the next few blocks) plus your tip. Re-estimate per transaction rather than hardcoding a price, since Polygon's base fee shifts block to block.eth_estimateGas for the exact transaction, then add a modest safety margin (around 10–20%). Too tight a limit risks an out-of-gas revert that still costs MATIC; too loose wastes nothing on EIP-1559 chains since unused gas is not charged, but a wildly high limit can be rejected by tooling and obscures real cost — estimate, don't guess.maxFeePerGas and a healthy maxPriorityFeePerGas so you land in the next block. For routine or background work, set a conservative ceiling near the current base fee with a small tip and accept that inclusion may take a few extra ~2s blocks in exchange for lower cost.