In order to determine exactly how much work a certain action takes, an internal pricing unit called gas is used. A simple transaction doesn’t require as much computational power to be written into the blockchain as a smart contract or a decentralized application (dapp). Consequently, the former takes just a little bit of gas, while the latter two can take a lot of gas.
Gas is paid for in ether (ETH), Ethereum’s native cryptocurrency. There is no 1:1 correlation between them; instead, users themselves determine how much they are willing to pay for gas in order for their actions to be executed. The higher the price, the more eager the miners are to prioritize that action.
- Gas is a unit of the internal pricing system for executing all actions in the Ethereum blockchain.
- It represents the amount of computational work needed to write a simple transaction, or the result of a smart contract or a DApp into the blockchain.
- Gas is paid for in ether, Ethereum’s native cryptocurrency.
- Each user determines how much they are willing to spend for every unit of gas.
- The higher they set the price, the faster miners will pick their transaction from the mempool.
Why do you need gas?
Because of Ethereum’s nature of public trades and transfers, the market price of ether can change very quickly. That is why an internal pricing system is used to keep the fee of transactions relatively stable compared to the fast-changing market price of ether.
As an example, imagine that the fee for running a transaction was paid at the static cost of 0.1 ETH. When the market price for 1 ETH was 10 USD, you needed to pay 1 USD for a transaction. But if the price of 1 ETH rises to 1,000 USD, you would need to pay 100 USD for that same transaction.
That is why an internal pricing unit called gas was created. It stabilizes the cost of transactions by decoupling it from ether’s ever-changing market price.
How much gas do you need?
All transactions on the Ethereum network need computational power to be executed. Each action has an associated gas cost, be it a simple transaction, smart contract or even a DApp. In general, the amount of gas required for the action is directly proportional to the amount of work necessary to write it into the blockchain.
When making a transaction, your wallet usually gives you a rough estimate for how much gas it will take. It is then up to you to determine how much you want to pay for that gas. For instance, if your transaction takes 21,000 gas and you’re willing to pay 5 Gwei (0.000000005 ETH) per one unit of gas, you will pay a total of 0.000105 ETH in fees which, at the current exchange rate, means a single US cent.
Transaction gas limit
You can never be quite sure exactly how much work an action will take. So, instead of attaching a precise fee or allowing the fee to adjust automatically, you set a gas limit. This is the highest amount of gas you are willing to spend on a transaction. Combined with the gas price, the gas limit effectively determines the highest amount of ETH that can be potentially spent on the transaction. All of the gas that isn’t spent on writing the transaction into the blockchain is refunded back to your wallet.
gas price × gas limit =maximum cost (in ETH)
If you set a precise fee and it turns out that it cannot cover the computational expenses, you’ll end up with a failed transaction, for which you still have to pay the price you’ve set. The idea is that the miner who has included your transaction in the block has done as much work as they could for the given price.
If you allow the fee to adjust automatically, you run the risk of depleting all your funds in case the smart contract that you are interfering with has a buggy code or infinite loops, which would make it much bigger, and consequently much more expensive to write into the blockchain, than initially anticipated. With a gas limit, you avoid both problems, since it more or less ensures your transaction fees are covered, and protects you from losing all your funds if something goes wrong.
Note that transaction gas limit is not the same as block gas limit, which is Ethereum’s way of controlling block size.
Optimal gas price
It should be noted that miners can choose which transactions to process first and will usually prioritize transactions with a higher gas price. This is because miners use their own time, electricity and computational power when writing your transaction in the blockchain and they are looking to make their work as profitable as possible.
Setting the right gas price and gas limit ensures your transaction gets mined and added to the blockchain at the optimal price-to-time ratio. A transaction with too small a fee may never be included in a block, while a transaction with too large a fee may be a waste of money, as it gives no particular time advantage over an average fee.
When transacting in ETH on the Bitstamp exchange, you do not need to worry about gas, since the exchange covers all fees incurred by crypto trading for free. The only fees you need to pay when buying items or services with crypto, or simply transferring crypto funds to another address, are the exchange’s own fees – and these are fixed. Creating an account at Bitstamp is free of charge.