Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasleft() directly instead of zero
Initializing with 1 or 100
✗ Incorrect
The gasUsed variable should start at zero to measure gas consumption properly.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasUsed variable
Using block.gaslimit which is total gas limit
✗ Incorrect
gasleft() returns the remaining gas at the current point in execution.
3fill in blank
hardFix the error in calculating gas used after function execution.
Blockchain / Solidity
gasUsed = startGas - [1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasleft without parentheses
Using block.gaslimit incorrectly
✗ Incorrect
gasleft() must be called as a function to get current gas left.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using block.timestamp or block.number instead of gasleft()
✗ Incorrect
Both start and end gas measurements use gasleft() function to get current gas left.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using gasleft without parentheses
Using wrong variable names
✗ Incorrect
We declare startGas variable, assign gasleft() at start, then subtract gasleft() after action.