0
0
Blockchain / Solidityprogramming~10 mins

Gas usage testing in Blockchain / Solidity - Interactive Code Practice

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

Complete the code to declare a gas variable initialized to zero.

Blockchain / Solidity
uint256 gasUsed = [1];
Drag options to blanks, or click blank then click option'
Agasleft()
B1
C0
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasleft() directly instead of zero
Initializing with 1 or 100
2fill in blank
medium

Complete the code to record the starting gas before a function call.

Blockchain / Solidity
uint256 startGas = [1]();
Drag options to blanks, or click blank then click option'
Agasleft
BgasUsed
Cblock.gaslimit
Dgas
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasUsed variable
Using block.gaslimit which is total gas limit
3fill in blank
hard

Fix the error in calculating gas used after function execution.

Blockchain / Solidity
gasUsed = startGas - [1]();
Drag options to blanks, or click blank then click option'
AgasUsed
Bblock.gaslimit
Cgasleft
Dgasleft()
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasleft without parentheses
Using block.gaslimit incorrectly
4fill in blank
hard

Fill both blanks to create a function that returns gas used by a call.

Blockchain / Solidity
function measureGas() public returns (uint256) {
    uint256 start = [1]();
    // some operation
    uint256 end = [2]();
    return start - end;
}
Drag options to blanks, or click blank then click option'
Agasleft
Bblock.timestamp
Cgasleft()
Dblock.number
Attempts:
3 left
💡 Hint
Common Mistakes
Using block.timestamp or block.number instead of gasleft()
5fill in blank
hard

Fill all three blanks to create a gas measurement wrapper function.

Blockchain / Solidity
function gasWrapper() public returns (uint256) {
    uint256 [1] = [2]();
    performAction();
    uint256 gasUsed = [1] - [3]();
    return gasUsed;
}
Drag options to blanks, or click blank then click option'
AstartGas
Bgasleft
Cgasleft()
DendGas
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasleft without parentheses
Using wrong variable names