What are transactions, and why do they have fees?
What is a Transaction?
Ethereum is a global computer that executes smart contract and stores its data (generally referred as contract state).
In addition to smart contract's state, it also stores account balances i.e ETH owned by accounts.
Ethereum global state
Ethereum's global state is updated on the following three scenarios.
- Transfer of ETH between two accounts.
- Creation of a smart contract.
- Execution of a method in an existing smart contract.
Any changes to the Ethereum's global state are made through transactions. Transactions are the means by which external users interact with the system.
Every transaction gets executed in every node, and the resulting state gets saved in all the nodes. It involves CPU, memory, and network costs. Miners/Validators are in charge of setting up and maintaining nodes.
Why would miners(validators) spend time and effort to execute every transaction and store the state?
The primary objective for miners(validators in Ethereum 2.0) is to earn money. They get incentives in the form of
- Block reward for every block produced.
- Transaction fees paid by the users.
- Miner Extracted Value (MEV).
You may be wondering why, in addition to the other incentives, they require transaction fees.
Without a fee to include transactions in the block, lazy miners(validators) may choose to just propose empty blocks, thus saving their CPU and network costs.
Furthermore, if there are no transaction fees, malicious users may flood the network and disrupt other transactions. Since the resources are constrained, users must pay a fee based on the level of transaction complexity.
How are transaction fees computed?
The transaction cost is expressed in gas units.
Each unit of gas represents a certain amount of computational work done by EVM (Ethereum Virtual Machine).
- An ETH transfer would cost 21,000 gas units in transaction fee.
- An NFT transfer or a more complex transaction would cost more.
How much is 21,000 gas units?
It depends on how much gas price the user is willing to pay per gas unit.
Gas prices are expressed in gwei (equal to 0.000000001 ETH).
Transaction Gas Fees
In the above case, if the user is willing to pay 10 gwei per gas unit, they must spend 0.00021 ETH in transaction (gas) fees to have their transaction processed by the network.
Now that we understand how gas fees are calculated, let's dive into Dune Analytics to query the blockchain and calculate the total gas fees paid by users thus far.
When the user initiates a transaction, they input the gas price they are willing to pay.
The EVM records the transaction as it is in the blockchain.
Once the transaction is executed, the EVM updates the outcome that includes actual gas units consumed in
transaction receipt trie.
Dune Data Load
Dune analytics extracts the transaction input and transaction outcome data and loads them into the
We can query the
gas_price columns in
ethereum.transactions table to figure out the gas price paid by the users for every transaction. We can then add up the gas fees to get the total amount paid so far.
SELECT --Divided by 1e9 to convert gas_price to gwei and again by 1e9 to convert gwei to eth --Divided by 1e6 to report the results in millions SUM(gas_used * gas_price/1e18)/1e6 AS total_gas_fees_in_eth FROM ethereum.transactions tx
👨🏽💻 Fork the above query and give it a try.
Users have paid ~7 million in ETH thus far (as of Sep'22) to transact on the Ethereum network 😮.
🏋️ Let's query the chain
Can you identify the most profitable month for miners in terms of transaction fees?
Hint: Solve https://dune.com/queries/1275288