Gas and Fees
Gas is the mechanism used to track and limit resource consumption in Cosmos SDK. Every operation consumes gas, and transactions must include sufficient fees to cover their gas consumption.Gas Metering
Gas Type and Limits
store/types/gas.go
GasMeter Interface
TheGasMeter tracks gas consumption during transaction execution:
store/types/gas.go
Basic Gas Meter
store/types/gas.go
Infinite Gas Meter
Used for genesis, queries, and simulations:store/types/gas.go
Gas Consumption
Storage Operations
KVStore operations consume gas based on operation type and data size:store/types/gas.go
Gas Consumption Examples
Custom Gas Consumption
Modules can consume gas for computational operations:Fee Calculation
Fee Structure
Fees are denominated in native tokens:types/coin.go
Minimum Gas Prices
Validators can set minimum gas prices they’ll accept:Gas Price Calculation
Gas Estimation
Simulating Transactions
Clients can simulate transactions to estimate gas:Gas Wanted vs Gas Used
Fee Payment
Fee Deduction
Fees are deducted before transaction execution:Fee Grants
One account can pay fees for another using fee grants:Gas Refunds
Unused gas can be refunded (used in EVM compatibility):store/types/gas.go
Transaction Gas Example
Complete example of gas in a transaction lifecycle:Best Practices
- Always simulate before submitting transactions to estimate gas
- Add gas buffer (10-20%) to avoid out-of-gas errors
- Set reasonable limits - excessively high gas limits waste resources
- Monitor gas costs in your modules to optimize performance
- Use descriptors when consuming gas for better debugging
- Charge gas before expensive operations to prevent DOS attacks
- Use transient stores for temporary data to reduce gas costs
- Batch operations when possible to amortize gas costs
Common Gas Costs
Typical gas consumption for common operations:| Operation | Gas Cost |
|---|---|
| Basic transfer | ~75,000 |
| Delegate tokens | ~120,000 |
| Vote on proposal | ~80,000 |
| Create validator | ~180,000 |
| IBC transfer | ~150,000 |
| Smart contract call | Varies widely |
| Signature verification | ~5,000 per sig |
Related Concepts
- Accounts - Account management and addresses
- Transactions - Transaction structure and signing
- AnteHandler - Pre-execution validation and fee deduction