# Gas

Gas on Nibiru Chain represents the amount of computational effort required to execute specific operations on the state machine.

# Ante Handler

The term "ante" originates from the game of poker, where it refers to the minimum amount that each player must contribute to the pot before a new round of the game can begin.

In analagous fashion, before any transaction is processed on Nibiru, it must pass through an initial checkpoint known as the Ante Handler, which is responsible for preliminary checks and modifications to the gas requirements of a transaction before it is passed on before it is passed onto the actual message handling.

The Ante Handler plays a crucial role in maintaining the integrity of Nibiru by checking the authenticity of transactions, deducting fees, and controlling resource usage. It serves as a protective barrier, ensuring that only valid transactions are processed, imposes costs, and discourages users from overloading the network with unnecessary data.

# Responsibilities of the Ante Handler

  1. Authentication: The Ante Handler verifies that the transaction is valid by checking the signatures and sequence numbers. This ensures that the transaction is indeed from the stated sender, and prevents replay attacks.

  2. Fee Deduction: Nibiru transactions may include a fee that is deducted from the sender's account. The Ante Handler handles this deduction.

  3. Gas Meter Setup: Every transaction in Nibiru consumes computational resources, quantified as gas. The Ante Handler sets up a gas meter for the transaction, deducting the gas fees from the user's account.

  4. Check Memo Size: A transaction memo is an optional piece of metadata that can be stored on chain with a transaction. Memos don't change message payloads for state transitions, so someone could potentially spam the network by sending memos that take up lots of memory. For this reason, the chain enforces a maximum allowed size for each transaction memo. The Ante Handler verifies that this maximum size is not exceeded.

# Gas on Ethereum

Gas was created on Ethereum to disallow the EVM (Ethereum Virtual Machine) from running infinite loops by allocating a small amount of monetary value into the system. A unit of gas, usually taken to be some fraction of a network's native coin, is consumed for every operation on the EVM and requires a user to pay for stateful operations and calling smart contracts.

# Gas on Nibiru

Exactly like Ethereum, Nibiru utilizes the concept of gas to track the resource consumption during message execution. Operations on Nibiru are represented as "reads" or "writes" done to the chain's key-value store. In both networks, gas is used to make sure that operations do not require an excess amount of computational power to complete and as a way to deter bad-acting users from spamming the network.

On Nibiru, a fee is calculated and charged to the user during a message execution. This fee is calculated from the sum of all gas consumed in an message execution:

Copy gas fee = gas * gas price

Gas is tracked in the main GasMeter and the BlockGasMeter:

  • GasMeter: keeps track of the gas consumed during executions that lead to state transitions. It is reset on every transaction execution.
  • BlockGasMeter: keeps track of the gas consumed in a block and enforces that the gas does not go over a predefined limit. This limit is defined in the Tendermint consensus parameters and can be changed via governance parameter change proposals.

# Difference - Gas Refunds

In Ethereum, gas can be specified prior to execution and the remaining gas will be refunded back to the user if any gas is left over - should fail with out of gas if not enough gas was provided.

On Nibiru, the concept of gas refunds does not exist. Fees paid to execute a transaction are not refunded back to the user due to the way instant finality works on Tendermint consensus. The fees exacted on a transaction will be collected by the validator and no refunds are

Since gas refunds are not applicable on Nibiru, it's extremely important to specify the correct gas as upfront.

To prevent overspending on fees, providing the --gas-adjustment flag for a cosmos transactions will determine the fees automatically. Often, setting the --fee to "auto" will simulate the transaction and give back the correct gas cost for the transaction, however this simulation is not perfect.

# 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 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, it is possible to send certain transaction types on Nibiru with 0 gas fees.


# Readings to Dive Deeper