0
0
Blockchain / Solidityprogramming~10 mins

Gas and transaction fees in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Gas and transaction fees
User creates transaction
Estimate gas needed
Set gas price
Calculate total fee = gas * gas price
Send transaction with fee
Miner validates and executes
Transaction included in block
User pays fee from balance
This flow shows how a blockchain transaction uses gas to measure work and fees to pay miners.
Execution Sample
Blockchain / Solidity
transaction = {
  'gas_limit': 21000,
  'gas_price': 50,
  'value': 1
}
fee = transaction['gas_limit'] * transaction['gas_price']
print(fee)
Calculate the transaction fee by multiplying gas limit by gas price.
Execution Table
StepVariableValueActionOutput
1transaction['gas_limit']21000Set gas limit for transaction
2transaction['gas_price']50Set gas price per gas unit
3fee21000 * 50Calculate total fee1050000
4print(fee)Output fee to user1050000
5--Transaction sent with fee
6--Miner executes transaction, fee paid
7--Transaction included in block
💡 Fee calculated and transaction processed with fee paid to miner
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
transaction['gas_limit']undefined21000210002100021000
transaction['gas_price']undefinedundefined505050
feeundefinedundefinedundefined10500001050000
Key Moments - 3 Insights
Why do we multiply gas limit by gas price to get the fee?
Because gas limit is how much work the transaction can use, and gas price is how much you pay per unit of work. Multiplying gives total cost (see execution_table step 3).
What happens if the gas limit is too low?
The transaction may fail because it runs out of gas during execution, but you still pay the fee for used gas (not shown in table but important).
Why does the miner get the fee?
Miners include transactions in blocks and get fees as reward for processing (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'fee' after step 3?
A21000
B1050000
C50
DUndefined
💡 Hint
Check the 'Value' column for 'fee' at step 3 in execution_table.
At which step is the transaction fee actually paid to the miner?
AStep 3
BStep 4
CStep 6
DStep 7
💡 Hint
Look for the step mentioning miner execution in execution_table.
If the gas price doubles, how does the fee change in the execution_table?
AFee doubles
BFee halves
CFee stays the same
DFee becomes zero
💡 Hint
Fee = gas_limit * gas_price, so changing gas_price affects fee proportionally.
Concept Snapshot
Gas measures work units for a transaction.
Gas price is cost per unit of gas.
Fee = gas limit * gas price.
User pays fee to miner for processing.
If gas limit too low, transaction fails but fee is still paid.
Full Transcript
This visual execution shows how blockchain transactions use gas and fees. First, the user sets a gas limit and gas price. The fee is calculated by multiplying these two values. The transaction is sent with this fee. Miners execute the transaction and receive the fee as payment. Variables like gas_limit, gas_price, and fee change step by step. Key points include why we multiply gas limit by gas price, what happens if gas limit is too low, and why miners get the fee. The quiz tests understanding of fee calculation and payment steps.