logoAcademy

Gas Fees and Gas Limit

Learn about Gas Fees and Gas Limit in the context of the EVM.

Background

In the context of the EVM, gas is a unit that measures the computational effort required to execute specific operations. Each operation performed by a contract or transaction on an EVM chain consumes a certain number of gas units based on its complexity. Operations that require more computational resources cost more gas. The EVM calculates the required gas units automatically, and developers are encouraged to optimize their contract code to reduce gas consumption.

The cost of executing a transaction is determined by the gas units consumed and the gas price, calculated as follows:

Transaction Cost = Gas Units * Gas Price

For EVM Avalanche L1s, gas payment can be configured to better suit the use case of the Avalanche L1. This means that the Avalanche L1 design can decide whether the gas fees are burned, paid to incentivize validators, or used for any other custom behavior.

Purpose

The primary goal of setting and enforcing computational costs via gas is to prevent spam and abuse on the network. By requiring users to pay for each computational step, the network deters malicious actors from launching denial-of-service (DoS) attacks, which involve flooding the network with spurious transactions. Essentially, the gas system serves as a deterrent against such attacks.

Gas Price and Gas Limit

Each transaction specifies the gas price and gas limit:

Gas Price: The gas price is the amount of the Avalanche L1's native token that the sender is willing to spend per unit of gas, typically denoted in gwei (1 native token = 1,000,000,000 gwei). A well-designed gas mechanism adapts the gas price according to network activity to protect the network from spam.

Gas Limit: The gas limit is the maximum amount of gas the sender is willing to use for the transaction. It was introduced to prevent infinite loops in contract execution. In a Turing-complete language like Solidity (the main programming language in the EVM), it is possible to write a contract with an infinite loop, either accidentally or intentionally. While an infinite loop might be a nuisance in traditional computing, it could cause significant issues in a decentralized blockchain by causing the network to hang as it attempts to process a never-ending transaction. The gas limit prevents this by halting execution once the gas consumed reaches the limit.

If a transaction exceeds the gas limit, it fails, but the fee amounting to the gas limit is still paid by the sender.

On this page