Gas and Fees
Users need to pay a fee to submit transactions on the Match network. As fees are handled differently on Ethereum and Cosmos, it is important to understand how the Match blockchain implements an Ethereum-type fee calculation, that is compatible with the Cosmos SDK.
Therefore this overview explains the basics of gas calculation, how to provide fees for transactions and how the Ethereum-type fee calculation uses a fee market (EIP1559) for prioritizing transactions.
Also, note the fees that are paid for interacting with smart contracts on Match can earn smart contract deployers a revenue. For information on this, head to develop.
Prerequisite Readings
Basics
Why do Transactions Need Fees?
If anyone can submit transactions to a network at no cost, the network can be overrun by a handful of actors sending large numbers of fraudulent transactions to clog up the network and stop it from working.
The solution to this is a concept called “gas," which is a resource consumed throughout transaction execution. In practice, a small amount of gas is spent on each step of code execution, thus effectively charging for use of a validator’s resources and preventing malicious actors from halting a network at will.
What is Gas?
In general, gas is a unit that measures the computational intensity of a particular transaction — in other words, how much work would be required to evaluate and perform the job.
When referring to a transaction, “gas” refers to the total quantity of gas required for the transaction. For example, a transaction may require 300,000 units of gas to be executed.
Gas can be thought of as electricity (kWh) within a house or factory, or fuel for automobiles. The idea is that it costs something to get somewhere.
More on Gas:
How is Gas Calculated?
In general, there’s no way to know exactly how much gas a transaction will cost without simply running it. There are ways to estimate the amount of gas a transaction will require, based on the details of the transaction fields, and data. In the case of the EVM, for example, each bytecode operation has a corresponding amount of gas.
More on Gas Calculations:
How does Gas Relate to Fees?
While gas refers to the computational work required for execution, fees refer to the amount of the tokens you actually spend to execute the transaction. They are derived using the following formula:
If “gas” was measured in kWh, the “gas price” would be the rate (in dollars per kWh) determined by your energy provider, and the “fees” would be your bill. Just as with electricity, gas price is liable to fluctuate over a given day, depending on network traffic.
More on Gas vs. Fees:
How are Fees Handled on Ethereum?
Fees on Ethereum include multiple implementations that were introduced over time.
Originally, a user would specify a GasPrice
and GasLimit
within a transaction. A block proposer would receive the entire gas fee from each transaction in the block, and they would select transactions to include accordingly.
With proposal EIP-1559 and the London Hard fork, gas calculation changed. The GasPrice
from above has now been split into two separate components: a BaseFee
and PriorityFee
. The BaseFee
is calculated automatically based on the block size and is burned once the block is mined. The PriorityFee
goes to the proposer and represents a tip, or an incentive for a proposer to include the transaction in a block.
Within a transaction, users can specify a max_fee_per_gas
corresponding to the total GasPrice
and a max_priority_fee_per_gas
corresponding to a maximum PriorityFee
, in addition to specifying the gas_limit
as before. All surplus gas that was not required for execution is refunded to the user.
More on Ethereum Fees:
Implementation
How are Gas and Fees Handled on Match?
Fundamentally, Match is a Cosmos SDK chain that enables EVM compatibility as part of a Cosmos SDK module. As a result of this architecture, all EVM transactions are ultimately encoded as Cosmos SDK transactions and update a Cosmos SDK-managed state.
Since all transactions are represented as Cosmos SDK transactions, transaction fees can be treated identically across execution layers. In practice, dealing with fees includes standard Cosmos SDK logic, some Ethereum logic, and custom Match logic. For the most part, fees are collected by the fee_collector
module, then paid out to validators and delegators. A few key distinctions are as follows:
Fee Market Module
In order to support EIP-1559 gas and fee calculation on Match’ EVM layer, Match tracks the gas supplied for each block and uses that to calculate a base fee for future EVM transactions, thus enabling EVM dynamic fees and transaction prioritization as specified by EIP-1559.
For EVM transactions, each node bypasses their local
min-gas-prices
configuration, and instead applies EIP-1559 fee logic—the gas price simply must be greater than both the globalmin-gas-price
and the block'sBaseFee
, and the surplus is considered a priority tip. This allows validators to compute Ethereum fees without applying Cosmos SDK fee logic.Unlike on Ethereum, the
BaseFee
on Match is not burned, and instead is distributed to validators and delegators. Furthermore, theBaseFee
is lower-bounded by the globalmin-gas-price
(currently, the globalmin-gas-price
parameter is set to zero, although it can be updated via Governance).Revenue Module
Match developed the Revenue Module as a way to reward developers for creating useful dApps—any contract rewards a fraction of the transaction fee (currently 20%) from each transaction that interacts with the contract to the contract developer. Validators and delegators earn the remaining portion.
Detailed Timeline
Nodes execute the previous block and run the
EndBlock
hookAs part of this hook, the FeeMarket (EIP-1559) module tracks the total
TransientGasWanted
from the transactions on this block. This will be used for the next block’sBaseFee
.
Nodes receive transactions for a subsequent block and gossip these transactions to peers
These can be sorted and prioritized by the included fee price (using EIP-1559 fee priority mechanics for EVM transactions), to be included in the next block
Nodes run
BeginBlock
for the subsequent blockThe FeeMarket module calculates the
BaseFee
to be applied for this block using the totalGasWanted
from the previous block.The Distribution module distributes the previous block’s fee rewards to validators and delegators
For each valid transaction that will be included in this block, nodes perform the following:
They run an
AnteHandler
corresponding to the transaction type. This process:Performs basic transaction validation
Verifies the fees provided are greater than the global and local minimum validator values and greater than the
BaseFee
calculatedPreemptively consumes gas for the EVM transaction
Deducts the transaction fees from the user and transfers them to the
fee_collector
moduleIncrements the
TransientGasWanted
in the current block, to be used to calculate the next block’sBaseFee
For Ethereum Transactions, nodes:
Execute the transaction and update the state
Send a fraction of the fees used as revenue to contract developers as part of the Revenue Module, if the transaction interacted with smart contract
Nodes run
EndBlock
for this block and store the block’sGasWanted
Detailed Mechanics
Matching EVM Gas consumption
Match is an EVM-compatible chain that supports Ethereum Web3 tooling. For this reason, gas consumption must be equatable with other EVMs, most importantly Ethereum.
The main difference between EVM and Cosmos state transitions, is that the EVM uses a gas table for each OPCODE, whereas Cosmos uses a GasConfig
that charges gas for each CRUD operation by setting a flat and per-byte cost for accessing the database.
In order to match the gas consumed by the EVM, the gas consumption logic from the SDK is ignored, and instead the gas consumed is calculated by subtracting the state transition leftover gas.
To ignore the SDK gas consumption, we reset the transaction GasMeter
count to 0 and manually set it to the gasUsed
value computed by the EVM module at the end of the execution.
AnteHandler
AnteHandler
AnteHandler
performs basic checks prior to transaction execution. These checks are usually signature verification, transaction field validation, transaction fees, etc.
Regarding gas consumption and fees, the AnteHandler
checks that the user has enough balance to cover for the tx cost (amount plus fees) as well as checking that the gas limit defined in the message is greater or equal than the computed intrinsic gas for the message.
Gas Refunds
In the EVM, gas can be specified prior to execution. The totality of the gas specified is consumed at the beginning of the execution (during the AnteHandler
step) and the remaining gas is refunded back to the user if any gas is left over after the execution. Additionally the EVM can also define gas to be refunded back to the user but those will be capped to a fraction of the used gas depending on the fork/version being used.
Zero-Fee Transactions
In Cosmos, a minimum gas price is not enforced by the AnteHandler
as the min-gas-prices
is checked against the local node/validator. In other words, the minimum fees accepted are determined by the validators of the network, and each validator can specify a different minimum value for their fees. This potentially allows end users to submit 0 fee transactions if there is at least one single validator that is willing to include transactions with 0
gas price in their blocks proposed.
For this same reason, in Match it is possible to send transactions with 0
fees for transaction types other than the ones defined by the evm
module. EVM module transactions cannot have 0
fees as gas is required inherently by the EVM. This check is done by the EVM transactions stateless validation (i.e ValidateBasic
) function as well as on the custom AnteHandler
defined by Match.
Gas Estimation
Ethereum provides a JSON-RPC endpoint eth_estimateGas
to help users set up a correct gas limit in their transactions.
For that reason, a specific query API EstimateGas
is implemented in Match. It will apply the transaction against the current block/state and perform a binary search in order to find the optimal gas value to return to the user (the same transaction will be applied over and over until we find the minimum gas needed before it fails). The reason we need to use a binary search is that the gas required for the transaction might be higher than the value returned by the EVM after applying the transaction, so we need to try until we find the optimal value.
A cache context will be used during the whole execution to avoid changes be persisted in the state.
Dealing with gas and fees with the Match CLI
When broadcasting a transaction using the Match CLI client, users should keep into consideration the options available. There are three flags to consider when sending a transaction to the network:
--fees
: fees to pay along with transaction; eg: 10amatch. Defaults toauto
.--gas
: the gas limit to set per-transaction; the default value is 20000.--gas-prices
: gas prices to determine the transaction fee (e.g. 10amatch).
However, not all of them need to be defined on each transaction. The correct combinations are:
without gas/fees related flags: estimates fees and gas automatically. Because
--fees
default isauto
.--fees=auto
: estimates fees and gas automatically (same behavior as--gas=auto
). Throws an error if using any other fees-related flag (e.i,--gas-prices
,--fees
)--gas=auto
: same behavior as--fees=auto
. Throws an error if using any other fees-related flag (e.i,--gas-prices
,--fees
)--fees={int}{denom}
: uses the required fees for the tx. Uses gas default value (20000) for the tx.--fees={int}{denom} --gas={int}
: uses specified gas and fees. Calculates gas-prices with the provided params--gas-prices={int}{denom} --gas={int} --fees=""
: uses the gas specified on for the tx and calculates the fee with the corresponding parameters.--gas-prices={int}{denom} --fees={int}{denom}
: uses fee provided and calculates gas limit with the provided params
The reader should note that the former two options provide a frendlier user experience for new users, and the latter are for more advanced users, who desire more control over these parameters.
The team introduced the auto
flag option that calculates automatically the gas and fees required to execute a transaction. In this way, new users or developers can perform transactions without the hustle of defining specific gas and fees values.
Keep in mind that there are other possible combinations that will fail. These are:
--gas={int}
: Error message will explain that failed because the default value for--fees
flag isauto
--gas-prices={int}{denom}
: Error message will explain that failed because the default value for--fees
flag isauto
--gas-prices={int}{denom} --gas={int}
: Error message will explain that failed because the default value for--fees
flag isauto
Last updated