На якому блоці EIP 1559На якому блоці EIP 1559

0 Comment

How to Send an EIP-1559 Transaction

While Ethereum has been trying to scale, it has encountered some gas price issues. Many layer 2 solutions and sidechains sprang into existence to solve this problem, but Ethereum is the main chain, and at some point, it has to be improved. EIP-1559 was introduced to reduce the volatility of gas prices on-chain. In this guide, we will learn how to send an EIP-1559 transaction using ethers.js.

Prerequisites

  • An Ethereum Sepolia Endpoint (you can create a free account here)
  • Ethereum wallet (e.g., MetaMask)
  • Node.js installed
  • Code editor and CLI.
  • Water, because water is important.

What is EIP-1559?​

EIP stands for Ethereum Improvement Proposals, which defines the standards around Ethereum protocol. Anyone can propose EIPs as Ethereum is open source. Later these EIPs can integrate into the core protocol after going through an approval process.

Before EIP-1559, the transactions with a lesser gas fee on the Ethereum chain often remained pending for a long time because the blocks are always filled with the highest paying transactions. To eliminate this, EIP-1559 introduced a more sophisticated and fair system of gas fees with a base fee per block and a tip for the miner. The base fee makes sure that the transactions get included in the blocks, and the tip is to reward the miners. With EIP-1559, the gas limit of the blocks doubled. A 100% full block pre EIP-1559 is only 50% full post EIP-1559, which means there is more room for additional transactions.

Now let us understand the new variables/mechanics of gas after 1559:

  • baseFeePerGas: This is the base fee per gas generated with every block header by the protocol. baseFeePerGas requires the minimum multiplier for the gasUsed attribute for a transaction to be added to a block. For example, the gas for your transaction will be baseFeePerGas * gasUsed. This is the part of the transaction fee that is burned in every block. Each block’s baseFeePerGas can increase or decrease by 12.5% depending on how full the block is compared to the previous block. For example, if 100% full then baseFeePerGas goes +12.5%, if 50% full then it remains the same, if 0% full then -12.5%.
  • maxPriorityFeePerGas: Set by user. This is the part of the gas that goes to miners. Users can pay a premium for a high-priority transaction using this variable. Whenever a block gets 100% full, this is the deciding factor for transaction priority, just like the pre-1559 era.
  • maxFeePerGas: Set by user. This represents the maximum amount of gas fee a user is willing to pay for a transaction (inclusive of baseFeePerGas + maxPriorityFeePerGas). Once the transaction is confirmed, the difference between maxFeePerGas and baseFeePerGas + maxPriorityFeePerGas is refunded back to the user/sender of the transaction.

Now that we know about the EIP-1559, it would be good to see it in action by sending an EIP-1559 transaction.

Set Up Your QuickNode Ethereum Endpoint​

The first step on our journey to send a transaction would be to have an Ethereum Testnet endpoint set up; we will be using the Sepolia Testnet to make the transaction, as sending transactions on the Ethereum Mainnet would cost real ETH. For convenience, we will create a free QuickNode account here and easily generate a new endpoint.

Save the HTTP URL as we will be using it later.

Getting funds in the wallet​

If you’re in need of ETH on Sepolia testnet, the Multi-Chain QuickNode Faucet makes it easy to obtain test ETH!

Navigate to the Multi-Chain QuickNode Faucet and connect your wallet or paste in your wallet address to retrieve test ETH. You can also tweet out your request to get a bonus!

Next, let’s cover the required libraries.

Installing required libraries​

We will use the ethers.js library to send our transaction and the log-timestamp library to log the time of each step of our script execution.

Create a project directory and cd into it:

Then, initialize an npm project with the default options:

To install these libraries, we will use the node.js package manager, npm.

npm install log-timestamp [email protected] 

Make sure that your ethers.js installation version is 5.7

Now, we should have our libraries installed in our project directory.

Making an EIP-1559 transaction​

Create a JavaScript file named index.js, and paste the following code in it:

require("log-timestamp"); const ethers = require("ethers");  const privateKey = ("ADD_YOUR_PRIVATE_KEY_HERE").toString('hex'); const wallet = new ethers.Wallet(privateKey);  const address = wallet.address; console.log("Public Address:", address);  const httpsUrl = "ADD_YOUR_HTTP_URL_HERE"; console.log("HTTPS Target", httpsUrl);  const init = async function ()  const httpsProvider = new ethers.providers.JsonRpcProvider(httpsUrl);  let nonce = await httpsProvider.getTransactionCount(address); console.log("Nonce:", nonce);  let feeData = await httpsProvider.getFeeData(); console.log("Fee Data:", feeData);  const tx =  type: 2, nonce: nonce, to: "0x8D97689C9818892B700e27F316cc3E41e17fBeb9", // Address you want to send to maxPriorityFeePerGas: feeData["maxPriorityFeePerGas"], // Recommended maxPriorityFeePerGas maxFeePerGas: feeData["maxFeePerGas"], // Recommended maxFeePerGas value: ethers.utils.parseEther("0.01"), // .01 ETH gasLimit: "21000", // Basic transactions cost exactly 21000 gas chainId: 11155111, // Sepolia chain ID; change if using another network >; console.log("Transaction Data:", tx);  const signedTx = await wallet.signTransaction(tx); console.log("Signed Transaction:", signedTx);  const txHash = ethers.utils.keccak256(signedTx); console.log("Precomputed txHash:", txHash); console.log(`https://sepolia.etherscan.io/tx/$txHash>`);  httpsProvider.sendTransaction(signedTx).then(console.log);  >;  init(); 

Replace ADD _ YOUR _ PRIVATE _ KEY _ HERE on line 4 with your wallet private key, and ADD _ YOUR _ HTTP _ URL _ HERE on line 10 with your endpoint’s HTTP URL we retrieved earlier.

Explanation of the code above:

Line 1-2: Importing the log-timestamp and ethers libraries.

Line 4-5: Saving our private key in the privateKey variable and initializing the wallet using privateKey.

Line 7-8: Getting the address of our wallet and printing it to the console.

Line 10-11: Saving our Endpoints HTTP URL in the httpUrl variable and printing it to the console.

Line 13: Starting an async init function.

Line 14: Initializing our provider and saving to the httpsProvider variable.

Line 16-17: Getting nonce from the provider and printing it to the console.

Line 19-20: Getting fee data, i.e., gasPrice, maxPriorityFeePerGas, maxFeePerGas from the node and saving it to the feeData variable, then printing it to the console.

Line 22-30: Transaction object tx: mentioning transaction as type 2 which is EIP-1559 transaction, mentioning nonce parameter, mentioning the receiver’s address, maxPriorityFeePerGas parameter and getting the info from feeData variable, maxFeePerGas parameter and getting the data from feeData variable, value parameter has the amount of ETH to be sent, gasLimit and chainId which is 11155111 here as the chain is Sepolia.

Line 32: Printing the transaction object.

Line 34-35: Signing the transaction using our wallet and printing the signed transaction raw data to the console.

Line 37: Applying keccak256 to the raw transaction to get transaction hash and saving it to txHash variable.

Line 38-39: Printing the transaction has to console, along with the link to Sepolia Etherscan.

Line 41: Sending our transaction using our node.

Save the file and run the script using:

Upon successful execution, the output will look like this:

You can also navigate to a block explorer such as Etherscan to view the transaction. Copy the transaction hash or search for the address you sent from/to.

Once you find the transaction, you can confirm it was sent using EIP-1559 by clicking the Click to show more text on the More Details field.

Conclusion​

I hope now you have a better understanding of EIP-1559 as in this guide, we learned about EIP-1559, and how to send an EIP-1559 transaction.

Subscribe to our newsletter for more articles and guides on Web3 and blockchain. If you have any questions, check out the QuickNode Forum for help. Stay up to date with the latest by following us on Twitter (@QuickNode) or Discord.

We ❤️ Feedback!

Let us know if you have any feedback or requests for new topics. We’d love to hear from you.

Technical Walkthrough of EIP-1559

What even is EIP-1559? Take a dive into the technicalities of the Ethereum fee market mechanism upgrade — and how it will affect Ethereum’s supply.

Table of Contents

What Is EIP-1559?

Ethereum Improvement Proposal (EIP) 1559 was launched in August 2021 as part of the London hard fork, which aimed to improve the Ethereum fee mechanism. In the six months since it launched, over 1.85 million Ether were burned. As of July 5, 2022, over 2.5 million ETH — worth some $2.73B currently — have been burned.

To understand EIP-1559, we must first understand the reason for it to exist in the first place. Let’s first analyze some basic design components of Ethereum, starting with the transaction fee.

Join us in showcasing the cryptocurrency revolution, one newsletter at a time. Subscribe now to get daily news and market updates right to your inbox, along with our millions of other subscribers (that’s right, millions love us!) — what are you waiting for?

Why Does Ethereum Need EIP-1559?

Why is there a transaction fee at all? Why even bother having that component in Ethereum?

Ethereum has a relatively good problem: demand exceeds supply. The supply of Ethereum is the amount of EVM (Ethereum Virtual Machine) computation that is available per block. There is, on average, 1 block produced every 13 seconds.

Thanks to EIP-1559, each block uses a variable amount of gas in the range of 1 single gas unit to 30 million gas units — the Ethereum computer itself is programmed to consider half of this number range, 15 million gas units, the target gas usage per block.

The actual size of the block will end up depending on the network demand — we will explore the consequences of a block consuming either greater or less than the 15 million gas unit target further below.

What Is Gas Used For in the Ethereum Network?

Gas is a proxy unit for how much computation and storage costs on the Ethereum network. That means we’re dealing with a finite resource — 15 million gas on Ethereum every ~13 seconds; this is the supply of the blockchain. Users wanting to use up those gas resources to process their transactions, per block, is the equivalent demand.

Ok, but you haven’t mentioned anything regarding the transaction fee!

The transaction fee is a direct meter-mechanism that addresses the supply-demand problem mentioned above: demand exceeding supply. Fees like this exist across most infrastructures: a concert venue must set an entry fee in order to meter a finite resource — seats and space, to its equivalent demand, people wanting to buy a seat to see the concert.

If there was no transaction fee at all, the demand for space on the Ethereum blockchain would be way more than just 15 million gas worth of transactions per 13 seconds.

Just like with a concert venue, you can’t give purely open access — everyone would overcrowd the venue wanting a seat! Transaction fees, however much nobody likes paying them, are a feature — not a bug!

How Do Transactions Get Included in the Ethereum Blockchain?

So, in Ethereum blocks, you can’t include everyone’s transactions willy-nilly — you must include some and exclude others for the network to be efficient. So what transactions do we include or exclude?

One natural approach is to include the transactions whose inclusions are the most valuable. This is exactly what transaction fees accomplish — if you have a very valuable transaction that you want to get mined on the Ethereum blockchain, you can signal that it’s a very valuable transaction by offering to pay a non-trivially large fee.

This is why you need transaction fees: they separate the populace of transactions into valuable ones: those willing to pay a non-trivial fee and not-so-valuable ones: those not willing to pay non-trivial fee.

Transaction fee revenue, where the fees themselves end up going, is a secondary side effect of some of the primary features you really want out of fees: to have each block be as efficiently utilized as possible.

Using block space efficiently means the network produces blocks that are as full as possible (as close to 15,000,000 gas units used!) with the transactions that are the most valuable filling that block space up. The only way to achieve this efficiency then is to charge transaction fees, which generate revenue. They are the necessary evil to an efficient blockchain.

What Was Ethereum’s Transaction Fee Mechanism Prior to EIP-1559?

The way a first-price auction works is when you submit a transaction to the network, you submit a bid along with it: an offer to pay the transaction fee. Then it’s up to the miners. Miners are monitoring the mempool, they see all the transactions that come in along with their attached bids.

It is expected that miners will simply pack blocks as full as possible with the highest-bid transactions. Why? Because miners, pre-EIP-1559, get the entirety of the transaction fees in a block.

In a first-price auction, you are responsible for coming up with a bid to get mined and once your transaction gets mined, that bid is paid directly to the miner.

What Does EIP-1559 Bring to the Ethereum Network?

Most people were mainly excited about EIP-1559 because of its novel upgrade to the Ethereum tokenomic design: a portion of the transaction fees are now burned. Who likes that? Well, anyone who is a holder of ETH!

It’s kind of like the protocol doing a buyback: by decreasing the total supply of ETH, in theory, that increases the value of all of the remaining supply, assuming no changes to the demand.

But EIP-1559 is much much more than just burning fees to theoretically make the value of ETH rise. The fee burning-mechanism is a secondary side effect of the intended primary features of EIP-1559.

There are three key implementations behind the design of EIP-1559: a base fee per block, variable block size and miner “tips.”

Key Features of EIP-1559

1. Implementation of a Base Fee

There is now a base fee in each Ethereum block. The base fee is the minimum gas price that someone must pay in order to be included in a single block. So if the base fee of a block is 90 gwei and you bid less than that, according to EIP-1559, you are ineligible to be included in that block — you do not meet the minimum fee requirement to be considered.

How is a block’s base fee computed?

The base fee is completely independent of the contents of a block; the fee is not affected by any of a block’s transactions. It is a history-dependent, present-independent fee.

How is the history of the blockchain used to calculate the next block’s base fee? Quite simple: the protocol algorithmically sets the base fee of the next block based on how “full” the last mined block was. Ethereum wants max utilization of blocks. If it appears the base fee was too low, it must be adjusted upward and if it appears too high, it must be adjusted down.

At this point, can you guess what gas unit number is used as the measuring point? This is where the second key idea of EIP-1559 directly ties into the first: variable size blocks.

2. Implementation of a Variable Block Size

Before EIP-1559, Ethereum blocks had a maximum size of 15 million gas worth of transactions. After the upgrade, blocks have a variable size of 1 gas unit to 30 million gas units worth of transactions. Hopefully it starts to make sense why Ethereum considers 15 million gas the “optimal” case — 15 million is the midpoint of the block size range; this is tied directly to the algorithmic calculation of the base fee.

If a block is below 15 million — thus not using the optimal amount of resources — this means the base fee is too high. On the flip side, if a block is above 15 million gas (up to 30 million max), according to EIP-1559, this means the base fee is too low.

The variable size of the Ethereum block is an important mechanism in allowing the protocol to calculate the base fee of each block in an algorithmic and on-chain manner.

What does it mean if the base fee is too high or too low?

There are three situations to consider: the base fee is too high, just right or too low. The “just right” point is when a block has exactly 15 million gas worth of transactions. Think of Ethereum’s optimal target of gas usage per block to be 15 million.

Too high: Suppose the current base fee on the network is 200 gwei, but at that given time, there weren’t even 15 million gas worth of transactions willing to pay 200 gwei in the mempool. There are surely tons of transactions in the mempool, but just not enough willing to pay 200 gwei to collectively add to the 15 million gas unit target. Remember: any transaction in the mempool under the current base fee will NOT be considered for the current block.

So the miners work with what they can: they fill the current block with as many transactions that were posted that are eligible to be mined (those with ≥200 gwei base fee). Say they managed to fill the block up to 10 million gas worth of transactions and they mine the block; this isn’t optimal as in any blockchain — you want to use the most block space possible per block!

The last block being 5 million gas units short of the 15 million mark is your on-chain signal that the base fee is too high! Your meter is charging too high and this is leading to your supply of resources being under-utilized.

Just right: Imagine the block that just got mined is exactly “full”: it uses 15 million gas worth of transactions. A full block is optimal. The base fee is exactly right, you have exactly the right amount of people willing to pay to be included in a block. Nothing changes.

Too low: Due to the variable block size of Ethereum, if over 15 million gas worth of transactions gets included in a block — that is your on-chain signal that the base fee is too low. Your meter is charging too low and this is leading to your supply of resources being over-utilized!

Pretty cool right? The base fee is directly related to the variable block size mechanism implementation.

So what happens to the revenues of the base fee then? Does it go to the miners?

No, these revenues get burned! If a transaction is submitted with a 100 gwei base fee, that 100 gwei will be burned when that transaction gets mined.

Before EIP-1559, the entire fee of a transaction went directly to the miner of the block. Now, the base fee component of a transaction fee is burned from the protocol.

So how do miners make money? This leads us into the third and final key design mechanism behind EIP-1559: the miner tip.

3. Implementation of a Miner Tip

An interesting aspect of EIP-1559 is that there is still technically a first-price auction system in place.

To bring back the concert venue analogy, the base fee will assure us that every fee-payer is assured a seat to see the band. But what if some people are willing to pay more to see the band up-close? Some people will surely pay a premium for seats closer to the band. The base fee, again, is the minimum to get into the venue. The miner tip would be considered this premium seat charge.

On top of the base fee, a priority fee system exists so that users are able to specify a payment fee that goes directly to the miner. The miner tip is for the purpose of incentivizing miners to include a transaction as fast as possible.

The miner tips are activated mainly to account for times of high demand on the network. If a block is constantly filling 30 million gas, then this means there is a lot of demand for block space and thus the first-price auction system is activated: users begin to incentivize miners to include their transactions as quickly as possible so they include higher fee amounts directly in the miner tip. Otherwise, usually a miner tip of 1–3 gwei per transaction is appropriate. You can use Etherscan’s gas tracker to find out the latest base and priority fee.

So there you have it, the three key design mechanisms implemented in EIP-1559 that put together make up for incredible network game theory that Ethereum has now enjoyed since the London hard fork.

This article contains links to third-party websites or other content for information purposes only (“Third-Party Sites”). The Third-Party Sites are not under the control of CoinMarketCap, and CoinMarketCap is not responsible for the content of any Third-Party Site, including without limitation any link contained in a Third-Party Site, or any changes or updates to a Third-Party Site. CoinMarketCap is providing these links to you only as a convenience, and the inclusion of any link does not imply endorsement, approval or recommendation by CoinMarketCap of the site or any association with its operators. This article is intended to be used and must be used for informational purposes only. It is important to do your own research and analysis before making any material decisions related to any of the products or services described. This article is not intended as, and shall not be construed as, financial advice. The views and opinions expressed in this article are the author’s [company’s] own and do not necessarily reflect those of CoinMarketCap.