Skip to content

Commit

Permalink
Merge pull request #7084 from jmcook1186/gas-page
Browse files Browse the repository at this point in the history
update gas page content for post-merge
  • Loading branch information
jmcook1186 authored Jul 28, 2022
2 parents 2cc0b51 + 6308a26 commit f58b896
Showing 1 changed file with 17 additions and 39 deletions.
56 changes: 17 additions & 39 deletions src/content/developers/docs/gas/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,29 @@ Gas refers to the unit that measures the amount of computational effort required

Since each Ethereum transaction requires computational resources to execute, each transaction requires a fee. Gas refers to the fee required to conduct a transaction on Ethereum successfully.

![A diagram showing where gas is needed in EVM operations](./gas.png)
![A diagram showing where gas is needed in EVM operations](/developers/docs/gas/gas.png)
_Diagram adapted from [Ethereum EVM illustrated](https://takenobu-hs.github.io/downloads/ethereum_evm_illustrated.pdf)_

Gas fees are paid in Ethereum's native currency, ether (ETH). Gas prices are denoted in gwei, which itself is a denomination of ETH - each gwei is equal to 0.000000001 ETH (10<sup>-9</sup> ETH). For example, instead of saying that your gas costs 0.000000001 ether, you can say your gas costs 1 gwei. The word 'gwei' itself means 'giga-wei', and it is equal to 1,000,000,000 wei. Wei itself (named after [Wei Dai](https://wikipedia.org/wiki/Wei_Dai), creator of [b-money](https://www.investopedia.com/terms/b/bmoney.asp)) is the smallest unit of ETH.

## Prior to the London upgrade {#pre-london}
## How Gas works {#how-gas-works}

The way transaction fees on the Ethereum network were calculated changed with [the London Upgrade](/history/#london) of August 2021. Here is a recap of how things used to work:
Since the [London Upgrade](/history/#london) every block has a base fee, the minimum price per unit of gas for inclusion in this block, calculated by the network based on demand for block space. As the base fee of the transaction fee is burnt, users are also expected to set a tip (priority fee) in their transactions. The tip compensates validators for executing and propagating user transactions in blocks and is expected to be set automatically by most wallets.

Let's say Alice had to pay Bob 1 ETH.
In the transaction, the gas limit is 21,000 units, and the gas price is 200 gwei.
Calculating the total transaction fee works as follows:

Total fee would have been: `Gas units (limit) * Gas price per unit`
i.e `21,000 * 200 = 4,200,000 gwei` or 0.0042 ETH
`Gas units (limit) * (Base fee + Tip)`

When Alice sent the money, 1.0042 ETH would be deducted from Alice's account.
Bob would be credited 1.0000 ETH.
Miner would receive 0.0042 ETH.
Let's say Jordan has to pay Taylor 1 ETH. In the transaction, the gas limit is 21,000 units and the base fee is 10 gwei. Jordan includes a tip of 2 gwei.

This video offers a concise overview of gas and why it exists:
Using the formula above we can calculate this as

<YouTube id="AJvzNICwcwc" />
`21,000 * (10 + 2) = 252,000 gwei` or 0.000252 ETH.

## After the London upgrade {#post-london}

[The London Upgrade](/history/#london) was implemented on August 5th, 2021, to make transacting on Ethereum more predictable for users by overhauling Ethereum's transaction-fee-mechanism. The high-level benefits introduced by this change include better transaction fee estimation, generally quicker transaction inclusion, and offsetting the ETH issuance by burning a percentage of transaction fees.

Starting with the London network upgrade, every block has a base fee, the minimum price per unit of gas for inclusion in this block, calculated by the network based on demand for block space. As the base fee of the transaction fee is burnt, users are also expected to set a tip (priority fee) in their transactions. The tip compensates miners for executing and propagating user transactions in blocks and is expected to be set automatically by most wallets.

Calculating the total transaction fee works as follows: `Gas units (limit) * (Base fee + Tip)`

Let's say Jordan has to pay Taylor 1 ETH. In the transaction, the gas limit is 21,000 units and the base fee is 100 gwei. Jordan includes a tip of 10 gwei.

Using the formula above we can calculate this as `21,000 * (100 + 10) = 2,310,000 gwei` or 0.00231 ETH.

When Jordan sends the money, 1.00231 ETH will be deducted from Jordan's account.
When Jordan sends the money, 1.000252 ETH will be deducted from Jordan's account.
Taylor will be credited 1.0000 ETH.
Miner receives the tip of 0.00021 ETH.
Base fee of 0.0021 ETH is burned.
Validator receives the tip of 0.000042 ETH.
Base fee of 0.00021 ETH is burned.

Additionally, Jordan can also set a max fee (`maxFeePerGas`) for the transaction. The difference between the max fee and the actual fee is refunded to Jordan, i.e. `refund = max fee - (base fee + priority fee)`. Jordan can set a maximum amount to pay for the transaction to execute and not worry about overpaying "beyond" the base fee when the transaction is executed.

Expand All @@ -68,7 +52,7 @@ The London Upgrade introduced variable-size blocks to Ethereum. Each block has a

### Base fee {#base-fee}

Every block has a base fee which acts as a reserve price. To be eligible for inclusion in a block the offered price per gas must at least equal the base fee. The base fee is calculated independently of the current block and is instead determined by the blocks before it - making transaction fees more predictable for users. When the block is mined this base fee is "burned", removing it from circulation.
Every block has a base fee which acts as a reserve price. To be eligible for inclusion in a block the offered price per gas must at least equal the base fee. The base fee is calculated independently of the current block and is instead determined by the blocks before it - making transaction fees more predictable for users. When the block is added to the blockchain this base fee is "burned", removing it from circulation.

The base fee is calculated by a formula that compares the size of the previous block (the amount of gas used for all the transactions) with the target size. The base fee will increase by a maximum of 12.5% per block if the target block size is exceeded. This exponential growth makes it economically non-viable for block size to remain high indefinitely.

Expand Down Expand Up @@ -97,23 +81,21 @@ It's also important to note it is unlikely we will see extended spikes of full b

### Priority fee (tips) {#priority-fee}

Before the London Upgrade, miners would receive the total gas fee from any transaction included in a block.

With the new base fee getting burned, the London Upgrade introduced a priority fee (tip) to incentivize miners to include a transaction in the block. Without tips, miners would find it economically viable to mine empty blocks, as they would receive the same block reward. Under normal conditions, a small tip provides miners a minimal incentive to include a transaction. For transactions that need to get preferentially executed ahead of other transactions in the same block, a higher tip will be necessary to attempt to outbid competing transactions.
In addition to the base fee, which is burned, there is also a priority fee (tip) to incentivize validators to include a transaction in the block. Without tips, validators would find it economically viable to propose empty blocks, as they would receive the same block reward. Under normal conditions, a small tip provides validators a sufficient incentive to include a transaction. For transactions that need to get preferentially executed ahead of other transactions in the same block, a higher tip will be necessary to attempt to outbid competing transactions.

### Max fee {#maxfee}

To execute a transaction on the network, users can specify a maximum limit they are willing to pay for their transaction to be executed. This optional parameter is known as the `maxFeePerGas`. For a transaction to be executed, the max fee must exceed the sum of the base fee and the tip. The transaction sender is refunded the difference between the max fee and the sum of the base fee and tip.

### Calculating fees {#calculating-fees}

One of the main benefits of the London upgrade is improving the user's experience when setting transaction fees. For wallets that support the upgrade, instead of explicitly stating how much you are willing to pay to get your transaction through, wallet providers will automatically set a recommended transaction fee (base fee + recommended priority fee) to reduce the amount of complexity burdened onto their users.
For wallets that support the London upgrade, instead of explicitly stating how much you are willing to pay to get your transaction through, wallet providers will automatically set a recommended transaction fee (base fee + recommended priority fee) to reduce the amount of complexity burdened onto their users.

## EIP-1559 {#eip-1559}

The implementation of [EIP-1559](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md) in the London Upgrade made the transaction fee mechanism more complex than the previous gas price auction, but it has the advantage of making gas fees more predictable, resulting in a more efficient transaction fee market. Users can submit transactions with a `maxFeePerGas` corresponding to how much they are willing to pay for the transaction to be executing, knowing that they will not pay more than the market price for gas (`baseFeePerGas`), and get any extra, minus their tip, refunded.

This video explains EIP-1559 and the benefits it brings:
This video explains EIP-1559 and the benefits it brought:

<YouTube id="MGemhK9t44Q" />

Expand All @@ -135,7 +117,7 @@ _Diagram adapted from [Ethereum EVM illustrated](https://takenobu-hs.github.io/d

Gas limit refers to the maximum amount of gas you are willing to consume on a transaction. More complicated transactions involving [smart contracts](/developers/docs/smart-contracts/) require more computational work, so they require a higher gas limit than a simple payment. A standard ETH transfer requires a gas limit of 21,000 units of gas.

For example, if you put a gas limit of 50,000 for a simple ETH transfer, the EVM would consume 21,000, and you would get back the remaining 29,000. However, if you specify too little gas, for example, a gas limit of 20,000 for a simple ETH transfer, the EVM will consume your 20,000 gas units attempting to fulfill the transaction, but it will not complete. The EVM then reverts any changes, but since the miner has already done 20k gas units worth of work, that gas is consumed.
For example, if you put a gas limit of 50,000 for a simple ETH transfer, the EVM would consume 21,000, and you would get back the remaining 29,000. However, if you specify too little gas, for example, a gas limit of 20,000 for a simple ETH transfer, the EVM will consume your 20,000 gas units attempting to fulfill the transaction, but it will not complete. The EVM then reverts any changes, but since the block-proposer has already done 20k gas units worth of work, that gas is consumed.

## Why can gas fees get so high? {#why-can-gas-fees-get-so-high}

Expand All @@ -151,7 +133,7 @@ Layer 2 scaling is a primary initiative to greatly improve gas costs, user exper

## Strategies for you to reduce gas costs {#strategies-for-you-to-reduce-gas-costs}

If you are looking to reduce gas costs for your ETH, you can set a tip to indicate the priority level of your transaction. Miners will 'work on' and execute transactions that offer a higher tip per gas, as they get to keep the tips that you pay and will be less inclined to execute transactions with lower tips set.
If you are looking to reduce gas costs for your ETH, you can set a tip to indicate the priority level of your transaction. Validators will execute transactions that offer a higher tip per gas, as they get to keep the tips that you pay and will be less inclined to execute transactions with lower tips set.

If you want to monitor gas prices, so you can send your ETH for less, you can use many different tools such as:

Expand All @@ -172,7 +154,3 @@ If you want to monitor gas prices, so you can send your ETH for less, you can us
- [Is Ethereum more expensive to use as price rises?](https://docs.ethhub.io/questions-about-ethereum/is-ethereum-more-expensive-to-use-as-price-rises/)
- [Reducing the gas consumption of your Smart Contracts](https://medium.com/coinmonks/8-ways-of-reducing-the-gas-consumption-of-your-smart-contracts-9a506b339c0a)
- [Proof of Stake versus Proof of Work](https://blockgeeks.com/guides/proof-of-work-vs-proof-of-stake/)

## Related topics {#related-topics}

- [Mining](/developers/docs/consensus-mechanisms/pow/mining/)

0 comments on commit f58b896

Please sign in to comment.