Skip to main content
Every Cosmos SDK transaction requires a fee to compensate validators for executing it. This guide explains how gas and fees work, how CosmJS calculates them, and how to configure gas pricing.

Gas vs Fees

These two terms are related but distinct:
  • Gas is a unit of computational work. Every operation in a transaction (reading state, writing state, signature verification, etc.) costs a certain amount of gas. The total gas consumed by a transaction is its gas used.
  • Gas limit is the maximum gas a transaction is allowed to consume. If execution exceeds this limit, the transaction fails (and the fee is still charged).
  • Gas price is the price per unit of gas, denominated in a token (e.g. 0.025uatom). It determines how much you pay per unit of work.
  • Fee is the total cost: gas limit × gas price. It is what the signer actually pays.
In CosmJS, a fee is represented as StdFee:
The gas field is the gas limit (as a string), and amount is the total fee in coins. A fee of 200,000 gas at 0.025 uatom/gas costs 5,000 uatom:

Setting Gas Price

GasPrice represents the cost per unit of gas. Set it once when creating the signing client and use "auto" fees everywhere:
You can also construct a GasPrice from its parts:
GasPrice.fromString parses a <amount><denom> string. The input regex requires the denom to start with a letter followed by alphanumeric characters or /, :, ., _, -. It also validates that the denom is 3–128 characters in length.

Three Ways to Specify Fees

Every transaction method (signAndBroadcast, sendTokens, delegateTokens, etc.) accepts a fee parameter in one of three forms:

1. "auto" — Simulate and Calculate

The simplest option. CosmJS simulates the transaction to estimate gas, applies a safety multiplier (default 1.4x), and calculates the fee from the configured gas price:
This requires gasPrice to be set in the client options. If it is not set, an error is thrown.

2. A Number — Custom Gas Multiplier

Pass a number to override the default 1.4x safety buffer. The simulation still runs, but your multiplier is used instead:

3. StdFee — Explicit Fee

For full control, pass a StdFee object directly. No simulation runs:

Calculating Fees Manually

calculateFee multiplies a gas limit by a gas price and returns a StdFee:
calculateFee also accepts a gas price string directly:
The multiplication uses Decimal arithmetic internally, so it handles gas prices that would overflow JavaScript’s safe integer range (e.g. chains with 18-decimal tokens):

Common Gas Limits

These are rough estimates for common transactions. Actual gas varies by chain version, state, and message complexity: Use "auto" unless you have a specific reason to set gas manually. The simulation-based approach adapts to the actual cost of your transaction.

Choosing the Right Approach

Next Steps

Gas Simulation

Simulate transactions to estimate gas.

Dynamic Gas Pricing

Use fee market pricing for chains like Osmosis.