Understanding Gas Limit/Price: Ethereum

in #ethereum7 years ago

What is Gas?
If your transaction shows "Out of Gas", try doubling the gas limit you sent with before, or manually increasing to 400000

Ethereum is the network—the blockchain. Ether (ETH) is the fuel for that network. Whenever you send tokens, interact with a contract, send ETH, or do anything else on the blockchain, you must pay for that computation and that payment is known as Gas and gas is paid in ETH.

When you hear gas, the person is either talking about:
Gas Limit
Gas Price
The total cost of a transaction (transaction fee / TX fee) is the Gas Limit * Gas Price.

Typically, if someone just says "Gas", they are talking about the "Gas Limit".

Analogy Time!

You can think of the gas limit like the amount of liters/gallons/units of gas for a car. You can think of the gas price as the cost of that liter/gallon/unit of gas.

With a car, it's $2.50 (price) per gallon (unit).
With Ethereum, it's 20 GWEI (price) per gas (unit).
To fill up your "tank", it takes... - 10 gallons at $2.50 = $25 - 21000 units of gas at 20 GWEI = 0.00042 ETH.

Therefore, the total TX fee will be 0.00042 Ether.

Sending tokens will typically take ~50000 gas to ~100000 gas, so the total TX fee increases to 0.001 ETH - 0.002 ETH.

Gas Limit
The gas limit is actually the limit because it's the maximum amount of units of gas you are willing to spend on a transaction. This avoids situations where there is an error somewhere and you spend 1 ETH....10 ETH....1000 ETH..... going in circles but arriving no where.

However, the units of gas are already defined by how much code is executed on the blockchain. You can't decide you don't want to spend that much on gas and lower the gas limit. You must include enough gas to cover the computational resources you use or your transaction will fail due to an Out of Gas Error.

Gas Price
If you want to spend less on a transaction, you can do so by lowering the amount you pay per unit of gas. The price you pay for each unit increases or decreases how quickly your transaction will be mined.

40 GWEI Gas Price will almost always get you into the next block.
20 GWEI will usually get you within the next few blocks.
2 GWEI will usually get you within the next few minutes.
During ICOs those numbers go crazy though...

50 GWEI is the max gas price most new ICO contracts will accept. Anything above that and your TX will fail

50 GWEI is therefor the amount you should send

If you are trying to send during an ICO (but not to the ICO) you have 2 choices: wait a bit until the ICO is over or increase the gas price over 50 GWEI.

You can adjust the gas price on MyEtherWallet in the footer via the slider. It is capped at 50 GWEI in order to prevent people tying to send to ICOs from having all their transactions fail because they don't read anything.

Will increasing the gas price get it mined faster? Does setting a low gas price mean it won't ever be mined?
The transaction fees go to the miner who mines your block. When miners mine a block, they have to decide which transactions to include. They can choose to include no transactions, or they can choose to randomly select transactions. In order to encourage miners to include transactions in blocks you want to set a "Gas Price" that is high enough to make them want to include it (since it is entirely up to them).

Most miners follow a very simple strategy for inclusion. They include transactions they received sorted from highest Gas Price to lowest, then include them until either the block is full or they reach one that has a Gas Price set lower than they are willing to bother with.

You want to set the Gas Price high enough so that a miner includes your transaction in a block. If you are in a hurry, you can set the Gas Price higher, so that you jump ahead of everyone in line. If you are not in a hurry, you just need to set a number high enough so that someone eventually includes your transaction.

Why should I set a low Gas Price?
Because it's cheaper and because with the increasing price of ETH compared to USD, a transaction that used to cost half a cent, may cost a few cents. More expensive transactions, like bidding on an ENS name, can now cost a dollar or more! As a user, you should try sending non-urgent transactions with a lower gas price as the more transactions that occur at the lower gas price, the more likely miners will lower their minimums.

Should I increase the gas limit for token sales, though?
You should put whatever the token sale holders tell you to put. If you do not know, then ask, before the token sale. This ensures that your transaction won't fail due to an "Out of Gas" error. Typically, a 200000 gas limit will be enough, but some require more.

Increasing the amount to 1 500 000 or more will not increase the likelihood of getting in. All it will do is fill up the blocks faster and you will lose that TX fee if it doesn't go through. We have never seen a token sale that requires over a 800 000 gas limit.

So I should send with a hugeeeeeeeee gas price for token sales, right?
Not necessarily. The risk in increasing it is that you could still not get in, and pay the fee anyways. The gas will NOT be returned to you if you send with a too-low gas limit, too early, or too late in the ICO.

You should decide how much you are will to invest, and how much you are willing to spend on that attempt to invest. You will pay the full gas fee. There is no gas refund during ICOs. Before the BAT ICO, the average gas price was 20 GWEI (the default) for ICOs. I personally would choose to invest with a 40-50 GWEI gas price, as it is a good balance between increasing my chances of getting in and not spending too much. What you choose to do is 100% your choice.

40 GWEI * 200000 == 0.008 ETH == $2.08 USD
70 GWEI * 200000 == 0.014 ETH == $3.64 USD
100 GWEI * 200000 == 0.02 ETH == $5.20 USD
Absurd Gas Prices From the BAT ICO

118 GWEI * 200000 == 0.0236 ETH == $6.13 USD
7590 GWEI * 200000 == 1.518 ETH == $394.68 USD
58000 GWEI * 200000 == 11.6 ETH == $3,016 USD

Where can I see what miners are accepting?
Start here: http://ethgasstation.info/

Notice that below 20 GWEI the blocks are almost all full, which means if you set lower than that you'll have to wait in line with everyone else.

A very awesome miner, miningpoolhub, accepts a 2 GWEI price, but their blocks are always full and they only account for 9% of mining which means about 1 in every 10 blocks (~150 seconds) will accept transactions with 2 GWUI gas price and when it does it is full so you may have to wait for a few of those.

http://ethgasstation.info/calculator.php will let you estimate how long it will be before your transaction is accepted at a certain gas price.

For example, right now:

2 GWEI is ~ 226 seconds
29 GWEI is ~ 33 seconds

Sort:  

That post really helped me with understanding all these concepts. Thanks :)

Glad to be of help.

You have made things about gas, and Gwei so clear that I can make sense out of it all and how much value it will pivot on in ETH and USD at any given time during transaction time. Thank you kryptokayden for such educative write-up

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.034
BTC 66137.63
ETH 3161.38
USDT 1.00
SBD 4.13