0
0
Blockchain / Solidityprogramming~20 mins

Gas and transaction fees in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Gas and Fees Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Calculate total transaction cost in Ethereum

Given the following transaction parameters, what is the total cost in Ether for this transaction?

Gas limit: 21000
Gas price: 50 Gwei (1 Gwei = 10-9 Ether)

Blockchain / Solidity
gas_limit = 21000
gas_price_gwei = 50
gas_price_ether = gas_price_gwei * 1e-9
total_cost = gas_limit * gas_price_ether
print(total_cost)
A0.0000105
B0.00105
C0.000105
D0.0105
Attempts:
2 left
💡 Hint

Multiply gas limit by gas price converted to Ether.

🧠 Conceptual
intermediate
1:30remaining
Understanding Gas Limit and Gas Price

Which statement correctly describes the relationship between gas limit and gas price in a blockchain transaction?

AGas limit and gas price are the same and used interchangeably.
BGas price is the maximum gas a transaction can use; gas limit is the amount paid per unit of gas.
CGas limit is the maximum amount of gas a transaction can use; gas price is the amount paid per unit of gas.
DGas limit is the total fee paid; gas price is the transaction size.
Attempts:
2 left
💡 Hint

Think about how much gas you allow and how much you pay per gas unit.

🔧 Debug
advanced
2:00remaining
Identify the error in gas fee calculation code

What error does the following code produce when calculating transaction fees?

gas_limit = 30000
gas_price = '20'
total_fee = gas_limit * gas_price
print(total_fee)
ANo error, outputs '20' repeated 30000 times
BTypeError: can't multiply sequence by non-int of type 'str'
CTypeError: unsupported operand type(s) for *: 'int' and 'str'
DTypeError: can't multiply sequence by non-int of type 'int'
Attempts:
2 left
💡 Hint

Check the data types of variables before multiplication.

Predict Output
advanced
2:00remaining
Calculate transaction fee with dynamic gas price

What is the output of this code snippet?

def calculate_fee(gas_limit, gas_price_gwei):
    gas_price_ether = gas_price_gwei * 1e-9
    return gas_limit * gas_price_ether

fee = calculate_fee(50000, 100)
print(f"{fee:.6f}")
A0.005000
B0.000500
C0.050000
D0.000050
Attempts:
2 left
💡 Hint

Multiply gas limit by gas price converted to Ether.

🧠 Conceptual
expert
2:30remaining
Why do blockchain transactions require gas fees?

Which is the main reason blockchain networks require gas fees for transactions?

ATo increase the value of the cryptocurrency by burning tokens.
BTo limit the number of transactions by setting a fixed fee per transaction.
CTo pay miners directly for storing user data permanently.
DTo prevent spam and allocate network resources fairly by charging for computation.
Attempts:
2 left
💡 Hint

Think about why networks charge fees for running code.