๐ Scenario: You are working with a blockchain network where each transaction requires a certain amount of gas. Gas is the unit that measures the amount of computational effort needed to execute operations. Each unit of gas costs a certain price in the network's currency. You want to calculate the total transaction fee for multiple transactions.
๐ฏ Goal: Build a simple Python program that calculates the total transaction fees for a list of transactions based on their gas used and the gas price.
๐ What You'll Learn
Create a dictionary called transactions with transaction IDs as keys and gas used as values.
Create a variable called gas_price that holds the price per unit of gas.
Use a for loop with variables tx_id and gas_used to iterate over transactions.items() and calculate each transaction fee.
Store the fees in a new dictionary called transaction_fees with the same transaction IDs as keys.
Print the transaction_fees dictionary.
๐ก Why This Matters
๐ Real World
Blockchain developers and users need to understand how transaction fees are calculated to manage costs and optimize transactions.
๐ผ Career
This knowledge is important for blockchain engineers, smart contract developers, and anyone working with decentralized applications to estimate and control transaction expenses.
Progress0 / 4 steps
1
Create the transactions data
Create a dictionary called transactions with these exact entries: 'tx1': 21000, 'tx2': 35000, 'tx3': 50000 representing gas used by each transaction.
Blockchain / Solidity
Hint
Use curly braces {} to create a dictionary with keys and values separated by colons.
2
Set the gas price
Create a variable called gas_price and set it to 50 representing the price per unit of gas.
Blockchain / Solidity
Hint
Assign the number 50 to the variable gas_price using the equals sign.
3
Calculate transaction fees
Use a for loop with variables tx_id and gas_used to iterate over transactions.items(). Inside the loop, calculate the fee by multiplying gas_used by gas_price. Store each fee in a dictionary called transaction_fees with tx_id as the key.
Blockchain / Solidity
Hint
Start with an empty dictionary transaction_fees = {}. Use a for loop to go through each transaction and multiply gas used by gas price.
4
Print the transaction fees
Write a print statement to display the transaction_fees dictionary.
Blockchain / Solidity
Hint
Use print(transaction_fees) to show the dictionary with calculated fees.
Practice
(1/5)
1. What does gas represent in a blockchain transaction?
easy
A. The amount of computational work required to process the transaction
B. The total amount of cryptocurrency sent in the transaction
C. The number of transactions in a block
D. The time it takes to confirm a transaction
Solution
Step 1: Understand the role of gas in blockchain
Gas is a unit that measures how much computational effort is needed to execute operations like transactions or smart contracts.
Step 2: Differentiate gas from other transaction elements
Gas is not the amount of cryptocurrency sent, nor the number of transactions or confirmation time.
Final Answer:
The amount of computational work required to process the transaction -> Option A
Quick Check:
Gas = computational work [OK]
Hint: Gas measures work, not money or time [OK]
Common Mistakes:
Confusing gas with transaction value
Thinking gas is transaction speed
Mixing gas with block size
2. Which of the following is the correct formula to calculate transaction fees?
easy
A. Transaction fees = gas price รท gas used
B. Transaction fees = gas price - gas used
C. Transaction fees = gas used + gas price
D. Transaction fees = gas used x gas price
Solution
Step 1: Recall the transaction fee formula
Transaction fees are calculated by multiplying the gas used by the gas price.
Step 2: Verify the formula correctness
Dividing, adding, or subtracting gas used and gas price does not represent the fee calculation.
Final Answer:
Transaction fees = gas used x gas price -> Option D
Quick Check:
Fees = gas used x gas price [OK]
Hint: Multiply gas used by gas price for fees [OK]
Common Mistakes:
Using division instead of multiplication
Adding gas used and gas price
Subtracting gas values
3. Given a transaction that used 21000 gas units and a gas price of 50 gwei, what is the total transaction fee in gwei?
medium
A. 21000 gwei
B. 1,050,000 gwei
C. 420 gwei
D. 50 gwei
Solution
Step 1: Identify given values
Gas used = 21000 units, Gas price = 50 gwei per unit.
Step 2: Calculate total fee
Total fee = gas used x gas price = 21000 x 50 = 1,050,000 gwei.
Final Answer:
1,050,000 gwei -> Option B
Quick Check:
21000 x 50 = 1,050,000 [OK]
Hint: Multiply gas used by gas price directly [OK]
Common Mistakes:
Multiplying incorrectly
Confusing gas units with gwei
Using gas price as total fee
4. Identify the error in this transaction fee calculation code snippet:
A. Using addition instead of multiplication for fee calculation
B. Incorrect variable names
C. Missing print statement
D. Gas price should be divided by gas used
Solution
Step 1: Review the code logic
The code adds gas_used and gas_price instead of multiplying them.
Step 2: Understand correct fee calculation
Transaction fee should be gas_used x gas_price, not addition.
Final Answer:
Using addition instead of multiplication for fee calculation -> Option A
Quick Check:
Fee = gas_used x gas_price, not + [OK]
Hint: Fees multiply gas used by gas price, not add [OK]
Common Mistakes:
Adding gas values instead of multiplying
Confusing variable names
Ignoring correct formula
5. A user wants to speed up their transaction by increasing the gas price from 40 gwei to 60 gwei. If the gas used remains 21000, how much more will the user pay in transaction fees compared to before?
hard
A. 600,000 gwei more
B. 1,260,000 gwei more
C. 420,000 gwei more
D. 840,000 gwei more
Solution
Step 1: Calculate original fee
Original fee = 21000 x 40 = 840,000 gwei.
Step 2: Calculate new fee
New fee = 21000 x 60 = 1,260,000 gwei.
Step 3: Find difference
Difference = 1,260,000 - 840,000 = 420,000 gwei more.
Final Answer:
420,000 gwei more -> Option C
Quick Check:
Increase = (60-40)x21000 = 420,000 [OK]
Hint: Subtract old fee from new fee to find extra cost [OK]
Common Mistakes:
Calculating total fee instead of difference
Mixing gas price and gas used
Incorrect multiplication
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.