An Ethereum application that I want to use talks about needing "gas" to run. What is gas, and where do I get it?
"Gas" is the name for a special unit used in Ethereum. It measures how much "work" an action or set of actions takes to perform: for example, to calculate one Keccak256 cryptographic hash it will take 30 gas each time a hash is calculated, plus a cost of 6 more gas for every 256 bits of data being hashed. Every operation that can be performed by a transaction or contract on the Ethereum platform costs a certain number of gas, with operations that require more computational resources costing more gas than operations that require few computational resources.
The reason gas is important is that it helps to ensure an appropriate fee is being paid by transactions submitted to the network. By requiring that a transaction pay for each operation it performs (or causes a contract to perform), we ensure that network doesn't become bogged down with performing a lot of intensive work that isn't valuable to anyone. This is a different strategy than the Bitcoin transaction fee, which is based only on the size in kilobytes of a transaction. Since Ethereum allows arbitrarily complex computer code to be run, a short length of code can actually result in a lot of computational work being done. So it's important to measure the work done directly instead of just choosing a fee based on the length of a transaction or contract.
So if gas is basically a transaction fee, how do you pay it? This is where it gets a little tricky. Although gas is a unit that things can be measured in, there isn't any actual token for gas. That is, you can't own 1000 gas. Instead, gas exists only inside of the Ethereum virtual machine as a count of how much work is being performed. When it comes to actually paying for the gas, the transaction fee is charged as a certain number of ether, the built-in token on the Ethereum network and the token with which miners are rewarded for producing blocks.
This might seem odd at first. Why don't operations just have a cost measured in ether directly? The answer is that ether, like bitcoins, have a market price that can change rapidly! But the cost of computation doesn't go up or down just because the price of ether changes. So it's helpful to separate out the price of computation from the price of the ether token, so that the cost of an operation doesn't have to be changed every time the market moves.
The terminology here gets a little messy. Operations in the EVM have gas cost, but gas itself also has a gas price measured in terms of ether. Every transaction specifies the gas price it is willing to pay in ether for each unit of gas, allowing the market to decide the relationship between the price of ether and the cost of computing operations (as measured in gas). It's the combination of the two, total gas used multiplied by gas price paid, that results in the total fee paid by a transaction.
As tricky as it is, it's important to understand this distinction, because it results in one of the most confusing things about Ethereum transactions to the initial learner: there is a difference between your transaction running out of gas and your transaction not having a high enough fee. If the gas price I set in my transaction is too low, no one will even bother to run my transaction in the first place. It will simply not be included in the blockchain by miners. But if I provide an acceptable gas price, and then my transaction results in so much computational work that the combined gas costs go past the amount I attached as a fee, that gas counts as "spent" and I don't get it back. The miner will stop processing the transaction, revert any changes it made, but still include it in the blockchain as a "failed transaction", collecting the fees for it. This may seem harsh, but when you realise that the real work for the miner was in performing the computation, you can see that they will never get those resources back either. So it's only fair that you pay them for the work they did, even though your badly designed transaction ran out of gas.
Providing too big of a fee is also different than providing too much ether. If you set a very high gas price, you will end up paying lots of ether for only a few operations, just like setting a super high transaction fee in bitcoin. You'll definitely be prioritised to the front of the line, but your money is gone. If you provided a normal gas price, however, and just attached more ether than was needed to pay for the gas that your transaction consumed, the excess amount will be refunded back to you. Miners only charge you for the work that they actually do. You can think of the gas price as the hourly wage for the miner, and the gas cost as their timesheet of work performed.
There are a lot of other subtleties to gas, but that should give you the basics! Gas is the key mechanism that makes the complex computations in Ethereum "safe" for the network to work on, because any programs that run out of control will only last as long as the money provided by the people who requested they be run. When the money stops, the miners stop working on it. And the mistakes you make in your program will only affect the people who pay to use it--the rest of the network can't suffer performance issues due to your error. They will simply get a big payday when the performance issues consume all of your ether! Without this critical technique, the idea of a general-purpose blockchain would have been completely impossible.
- Gas is the way that fees are calculated
- The fees are still paid in ether, though, which is different from gas
- The gas cost is the amount of work that goes into something, like the number of hours of labour, whereas the gas price is like the hourly wage you pay for the work to be done. The combination of the two determines your total transaction fee.
- If your gas price is too low, no one will process your transaction
- If your gas price is fine but the gas cost of your transaction runs "over budget" the transaction fails but still goes into the blockchain, and you don't get the money back for the work that the labourers did.
- This makes sure that nothing runs forever, and that people will be careful about the code that they run. It keeps both miners and users safe from bad code!
Gas is the execution fee for every operation made on ethereum. Its price is expressed in ether and it's decided by the miners, which can refuse to process transaction with less than a certain gas price. To get gas you simply need to add ether to your account.
Ethereum implements on the blockchain an execution environment called the Ethereum Virtual Machine (EVM). When you are running a decentralized application (dApp), every instruction is executed on every node of the network. This has a cost: for every operation that a script can execute there is a specified cost, expressed in number of gas units, that you can see in the EVM specification.
The price of one unit of gas is decided by the miners, currently it is around 5 to 21 GWei (One GWei is 10^9 Wei or 10^-9 Ether). Ethereum uses ether as its internal currency/token. Your accounts holding are expressed in ether. When you deploy a contract, or execute a transaction the gas will be taken from your account balance. You are free to specify a gasprice, or keep the suggest one.
The following response is taken from Ethereum, Gas, Fuel & Fees
What is Gas?
Gas is the metering unit for use of the Ethereum "World Computer". As an analogy, electricity is metered by kilowatt hours. Using more computation and storage in Ethereum means that more gas is used. One fundamental reason for metering is that it provides an incentive for people (miners) to operate the World Computer. These miners get a fee for processing transactions, which is determined by the metering scheme: gas.
Each operation in the EVM consumes gas. For example, a multiplication (MUL) consumes 5 gas and an addition (ADD) consumes 3 gas. Here is a spreadsheet of Ethereum’s operations and their gas consumption. (It's also possible to consider them as gas costs, but it probably makes explanations more difficult to follow with costs, fees, prices flying around.)
Metering is different from fees and gas is different from Ether. To help clarify this, consider gas to be synonymous with fuel. A transaction must provide enough fuel, or startGas, to cover its entire use of the EVM’s computation and storage facilities. All remaining gas is refunded to the transaction’s originator: the user who initiated the transaction. A transaction that runs Out of Gas is reverted, but is still included in a block and the associated fee is paid to the miner.
With an overview from the perspective of fuel, let’s turn to an overview of fees. While every operation in the EVM consumes a predefined amount of gas that is fixed (for example, a MUL operation always consumes 5 gas), a user can specify a gas price in every transaction. The current gas price is 0.02µ Ethers, or 0.00000002 ETH. The fee an originator pays a miner is the transaction’s
(startGas - remainingGas) × gas price.
Here is a summary of the influences of transaction fuel and transaction fee:
At the start of a transaction, the Ether required for the startGas is set aside [1b], and the remainingGas is set to startGas [1a]. [ 2 ] With each operation of the transaction, gas is consumed and remainingGas is lowered. [ 3 ] If there’s an Out of Gas exception, all operations are reverted and all the Ether that was initially set aside is given to the miner [ 4 ]. [ 5 ] If the transaction completes successfully, all the remainingGas is refunded to the originator and the rest is paid to the miner.
In the following mock scenario, assume that a STORE consumes 45 gas and an ADD consumes 10 gas. The scenario involves storing the number 31 in the EVM, summing 2 numbers, and then storing the sum. Let’s assume that the originator specified a startGas of 150 and a gas price of 0.02µETH, Below is an illustration as the transaction is processed by the EVM:
The originator pays the miner a fee of (150 - 50) × 0.02µETH = 2µETH = 0.000002 ETH.
Fuel vs. Fee
There is a difference between an originator providing enough fuel and providing enough fees. Here are the likely effects on a transaction:
[ 1 ] A transaction with too little fuel will not even reach miners, regardless of the fee supplied.
[ 2 ] If adequate fuel is provided for a transaction, but the fee is too low, even though the transaction may reach miners, upon examining the transaction miners will not perform any computation.
[ 3 ] A low fuel transaction implies that it will use little computation or storage resources of the EVM, or that at runtime it will hit an Out of Gas exception and be reverted.
[4, 5, 6] Fees determine the order in which transactions will be included in the blockchain.
[ 7 ] The reason why providing high fuel can lead to a transaction taking longer to get mined is discussed in the section Potential delays with high startGas.
In Bitcoin, metering is done with bytes: the number of bytes in the transaction. In Ethereum, computation also needs to be metered because a small amount of code could still be a program that runs forever. Metering computation is one of the reasons for gas. But having gas doesn’t mean requiring fees.
For example, in a private chain each account could have X gas per day, or each account could have Y gas per transaction, or some other scheme. On the flip side, having fees doesn’t mean requiring gas: fees can be based on different metering, such as bytes. Security in a public blockchain requires both gas and fees, while the alternatives are more applicable to private chains (for example, a scheme where each account has X gas per day can be Sybil-attacked in a public chain where anyone can create an account).
An ultimate example of metering not requiring fees, is when a contract is invoked with a call vs. transaction. As explained in Who gets the fee from contract call?, an invocation with a "call" still uses metering but has no fees.
Additional useful content on gas in Ethreum from: http://ethdocs.org/en/latest/ether.html#gas-and-ether
Gas and ether
Gas is supposed to be the constant cost of network resources/utilisation. You want the real cost of sending a transaction to always be the same, so you can’t really expect Gas to be issued, currencies in general are volatile.
So instead, we issue Ether whose value is supposed to vary, but also implement a Gas Price in terms of Ether. If the price of Ether goes up, the Gas Price in terms of Ether should go down to keep the real cost of Gas the same.
Gas has multiple associated terms with it: Gas Prices, Gas Cost, Gas Limit, and Gas Fees. The principle behind Gas is to have a stable value for how much a transaction or computation costs on the Ethereum network.
- Gas Cost is a static value for how much a computation costs in terms of Gas, and the intent is that the real value of the Gas never changes, so this cost should always stay stable over time.
- Gas Price is how much Gas costs in terms of another currency or token like Ether. To stabilise the value of gas, the Gas Price is a floating value such that if the cost of tokens or currency fluctuates, the Gas Price changes to keep the same real value. The Gas Price is set by the equilibrium price of how much users are willing to spend, and how much processing nodes are willing to accept.
- Gas Limit is the maximum amount of Gas that can be used per block, it is considered the maximum computational load, transaction volume, or block size of a block, and miners can slowly change this value over time.
- Gas Fee is effectively the amount of Gas needed to be paid to run a particular transaction or program (called a contract). The Gas Fees of a block can be used to imply the computational load, transaction volume, or size of a block. The gas fees are paid to the miners (or bonded contractors in PoS).
Other useful links:
https://www.reddit.com/r/ethereum/comments/271qdz/can_someone_explain_the_concept_of_gas_in_ethereum/ https://www.reddit.com/r/ethereum/comments/3fnpr1/can_someone_possibly_explain_the_concept_of/ https://www.reddit.com/r/ethereum/comments/49gol3/can_ether_be_used_as_a_currency_eli5_ether_gas/
Gas is basically the internal pricing for running a transaction or a contract.
The gas price per transaction or contract is set up to deal with the Turing Complete nature of Ethereum and its EVM (Ethereum Virtual Machine Code)
So, the more complex the transaction or the operation, the more gas it would cost.
Gas is what you pay to execute code on the blockchain and to transfer ether to another address. For each instruction on the Ethereum Virtual Machine you pay a certain amount of gas. Some instructions are expensive and some are cheap.
If you call a function and you run out of gas while executing this function call, all changes performed by the function will be rolled back and you will lose all the gas that you provided.
The reason that gas is needed for each instruction instead of simply a set fee to execute any function is that you could otherwise write a function which had an infinite loop, deploy this contract, and then call the function. If you did this, the miners attempting to include your function call in the next block would be stuck in an infinite loop.
First I want to tell about EVM (Ethereum Virtual Machine), As we know that Ethereum Network is a collection of computers and each computer has EVM software installed on their system and also all those systems have a copy of the blockchain where all miners are connected through EVM. When developer writes a smart contract then the code is compiled with the machine code and the machine code gets saved on the blockchain which is executed by EVM. I am not going to explain that what happens with this code in the blockchain in this question.
Now, Ether vs Gas ??
In the whole code, there are many instructions which are doing their work according to that instruction or code , for example code written to store value in memory or to perform SHA hash operation. Each operation has a cost. In order to execute your code or smart contract, you need to pay for all your operations which is paid in ether. The operation cost is in gas which is translated into ether and then it is paid in ether.
The reason for that is to decouple the price of an operation with the market price of an ether and the value of each gas which is a constant number in ether can easily be adjusted by the miners.
protected by Afri May 22 '17 at 10:06
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?