0
0
Blockchain / Solidityprogramming~10 mins

Why gas efficiency saves money in Blockchain / Solidity - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Amsg.gas
B21000
Cblock.gaslimit
Dgasleft()
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasleft() which returns remaining gas, not used gas.
2fill in blank
medium

Complete 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'
AgasPrice
BgasPrice / 1 ether
CgasUsed
D1 ether
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing gasPrice by 1 ether instead of using it directly.
3fill in blank
hard

Fix 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'
A+
B*
C/
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of / causes wrong conversion.
4fill in blank
hard

Fill 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'
A>
B<
C50000
D60000
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > filters wrong functions.
5fill in blank
hard

Fill 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'
A*
B/
C1 ether
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Adding instead of multiplying or dividing causes wrong results.