Day 5 / 100DaysOfWeb3
I will learn and explain What is Gas and How it is used in Ethereum
What is Gas ?
To execute Ethereum transactions or smart contracts in blockchain network, we need some additional fees called Gas. At Ethereum official website, They refer to unit that measures the amount of computational effort required to execute specific operations on the Ethereum network.
Why they exist ?
They exist for two reasons. First, any transactions or execution of a smart contract must be run on every node of Ethereum blockchain, to guarantee validity. Second, smart contracts are Turing complete, so they can execute forever no matter how complex or needs in time and resources. So, to ensure that these rules are respected, the network needs the computing power that the miners make available in exchange for gas
How Gas Costs are calculated ?
It is the sum of the gas for each operation executed by Ethereum Virtual Machine for a specific Program. For example, in a smart contract, sending a transaction costs 21,000 gas and 3 gas where used to add to numbers, the total cost will be :
21003 x 'price of a gas sets by the Miner based on supply and demand'
To take some precautions, price limits can be set for gas and based on it, Miners can determine the Max Transaction fee.
Gas execution
If a transaction is successful before all the gas is consumed, the rest is returned to the sender, but if the gas is finished before the transaction is complete, no gas will be returned and the transaction will be automatically cancelled, reverting all changes brought to the blockchain.
Thus, we can conclude by saying that, gas is a fee claimed by the miner, who wants to make available the resources in energies and calculations, necessary for the execution of a transaction or our smart contract deployed in the Ethereum network.