People new to blockchain often fall into two camps: Those that don’t even look at the gas cost and those that are surprised by the gas cost. And it makes sense – Gas, as a concept, can be a bit difficult to wrap your head around. Let’s demystify what’s going on behind the scenes.
When you interact on the blockchain, you pay fees for the transactions you submit. Want to send ETH from your wallet to a friends? That’ll require a certain amount of gas. Minting the hottest new NFT? That’ll require a different amount of gas. But what decides how much gas? And how much that will cost?
Gas Usage
Each transaction will require a different amount of gas. For example, sending ETH from one address to another address will always use 21,000 gas. It’s part of the Ethereum standard. But there are other things you can do in smart contracts. Every last little operation done inside a smart contract requires some gas defined in the Ethereum yellow paper. Adding numbers, storing values, initializing variables. The gas usage of a transaction is simply the sum of all the operations that smart contract does when the user interacts with them.
The only thing developer can do to affect gas usage is make their contract take fewer or less costly steps to perform the action that the user is transacting.
As a simple example, let’s imagine that a single addition on Ethereum costs 3 gas.
5 + 5 + 5 + 5 + 5 would requires 12 gas (four additions)
But
5 + 20 would requires 3 gas (one addition)
As contracts become more and more complex, there are more opportunities to fine tune them to make them more efficient. However, there’s a limit. At some point it’s always going to require some gas to accomplish what the user is submitting to the contract.
Gas Price
However, gas usage is not the only factor in determining how much a transaction will actually cost. Because gas price is how many gwei (1,000,000 wei) you’re willing to spend per gas the contract requires. When a user decides to spend 10 gas on a transaction, this is the cost, in gwei of each gas the contract consumes when it performs all of its operations.
So using our example above, let’s assume a user selects 10 gwei as the gas price:
5 + 5 + 5 + 5 + 5 requires 12 gas which means the user spends 120 gwei on the transaction.
But
5 + 20 would require 3 gas which means the user spends 30 gwei on the transaction.
The price of each gas can go up when the network is really busy to incentivize miners to mine transactions and disincentivize users from submitting so many transactions. It then falls back down as the network becomes less congested.
Then what’s the gas limit?
Think of gas costs as going on a road trip. The distance you’re traveling is the gas usage. And the map says you’re going to need to travel 200 miles. However, a road was closed and you had to make a detour. This drive is going to take a lot more gas than you expected! And you’re going to have to pay for it. Setting a gas limit is similar to setting a moment where you say “At this point, let’s just go back home.”
When you’re about to submit a transaction to the blockchain, it estimates how much gas you’re going to need to use. However, it might be wrong. Maybe someone submitted something right before you that makes your operation cost more. What if the developer made a mistake and accidentally put an infinite loop in the contract? You don’t want to spend your precious ETH unwittingly.
The gas limit is a safety mechanism to say “if we use this much gas, just go ahead and cancel the transaction.” You’ll still be on the hook for the gas you did use, but at least the transaction won’t go further beyond that point.
An important thing to note – If you set the gas limit to less than the estimate, it’s likely your transaction will fail outright! It’s like saying you won’t use more than 150 gallons worth of gas to drive 200 miles. You’ll never make it! So a good rule of thumb is to always use the estimate plus 10-20%. Many wallets automatically add some buffer already.
Conclusion
Transactions can get expensive on the blockchain, but there’s a few things to be aware of:
- Not all contracts are created equal. Both complexity and attention to detail by developers can have a serious effect.
- Don’t skimp on the gas limit thinking it’ll save you money! It’ll often mean your transaction is doomed before it starts.
- Gas prices can go up and down with time. If you’re doing routine transactions, keep an eye out for when the network is slow, like late at night.
There are other ways to safe gas too, like batching work on the blockchain together. That was part of the reason I built Omnisender, so that if you need to send a ton of tokens from one address to many others you can do them in a single transaction. It can’t affect the underlying code beneath the hood of the tokens you’re sending, but it can save the overhead (and time) of sending out each transaction individually!