Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the gas cost for a transaction.
Blockchain / Solidity
uint gasUsed = [1]; // gas used by transaction uint gasPrice = 20 gwei; uint cost = gasUsed * gasPrice;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasleft() which returns remaining gas, not used gas.
✗ Incorrect
The typical gas used for a simple transaction is 21000 units.
2fill in blank
mediumComplete the code to calculate the total transaction cost in Ether.
Blockchain / Solidity
uint gasUsed = 21000; uint gasPrice = 50 gwei; uint costInWei = gasUsed * [1]; uint costInEther = costInWei / 1 ether;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing gasPrice by 1 ether instead of using it directly.
✗ Incorrect
The cost in wei is gasUsed multiplied by gasPrice.
3fill in blank
hardFix the error in the code that calculates gas cost in Ether.
Blockchain / Solidity
uint gasUsed = 30000; uint gasPrice = 100 gwei; uint costInWei = gasUsed * gasPrice; uint costInEther = costInWei [1] 1 ether;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of / causes wrong conversion.
✗ Incorrect
To convert wei to ether, divide by 1 ether (10^18 wei).
4fill in blank
hardFill both blanks to create a dictionary of function gas costs and filter those above 50000 gas.
Blockchain / Solidity
gasCosts = {
"transfer": 21000,
"mint": 60000,
"burn": 45000
};
expensiveFunctions = {k: gasCosts[k] for k in gasCosts if gasCosts[k] [1] [2]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > filters wrong functions.
✗ Incorrect
We want functions with gas cost greater than 50000.
5fill in blank
hardFill all three blanks to create a function that returns the gas cost in Ether for a given gasUsed and gasPrice.
Blockchain / Solidity
function calculateCost(uint gasUsed, uint gasPrice) public pure returns (uint) {
uint costInWei = gasUsed [1] gasPrice;
uint costInEther = costInWei [2] [3];
return costInEther;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding instead of multiplying or dividing causes wrong results.
✗ Incorrect
Multiply gasUsed by gasPrice to get wei, then divide by 1 ether to get Ether.