📖 Scenario: You are developing a simple smart contract on the Ethereum blockchain. You want to measure how much gas your contract uses when calling a function that stores a number.
🎯 Goal: Build a smart contract and write a test script that measures the gas used when calling a storeNumber function.
📋 What You'll Learn
Create a smart contract with a storeNumber function that saves a number.
Write a test script that calls storeNumber and measures gas used.
Print the gas used in the test output.
💡 Why This Matters
🌍 Real World
Measuring gas usage helps developers optimize smart contracts to save money and improve performance on blockchains like Ethereum.
💼 Career
Blockchain developers must understand gas costs to write efficient contracts and manage deployment and transaction expenses.
Progress0 / 4 steps
1
Create the smart contract
Create a Solidity contract named GasTest with a public uint256 variable called storedNumber and a function storeNumber that takes a uint256 parameter called num and sets storedNumber to num.
Blockchain / Solidity
Hint
Define a contract with a public variable and a function that updates it.
2
Set up the test environment
In a JavaScript test file, import ethers from hardhat and declare a variable gasTest to hold the deployed contract instance.
Blockchain / Solidity
Hint
Use require("hardhat") to import ethers and declare a variable for the contract.
3
Deploy the contract and call the function
Write an async function main that deploys GasTest using ethers.getContractFactory, assigns it to gasTest, then calls storeNumber(42) and waits for the transaction receipt.
Blockchain / Solidity
Hint
Deploy the contract, then call the function and wait for the transaction receipt.
4
Print the gas used
Add a line in main to print the gas used by accessing receipt.gasUsed.toString() with console.log. Then call main() and catch errors to print them.
Blockchain / Solidity
Hint
Use console.log to print the gas used from the receipt, then run main() with error handling.
Practice
(1/5)
1. What is the main purpose of gas usage testing in blockchain smart contracts?
easy
A. To check if the smart contract code has syntax errors
B. To increase the gas price for faster transactions
C. To verify the correctness of smart contract logic
D. To measure how much gas a function consumes when executed
Solution
Step 1: Understand gas usage testing
Gas usage testing measures the amount of gas consumed by smart contract functions during execution.
Step 2: Differentiate from other tests
It is not about syntax errors, logic correctness, or changing gas prices but about cost measurement.
Final Answer:
To measure how much gas a function consumes when executed -> Option D
Quick Check:
Gas usage testing = measure gas cost [OK]
Hint: Gas testing measures cost, not correctness or syntax [OK]
Common Mistakes:
Confusing gas usage testing with syntax or logic testing
Thinking gas usage testing changes gas prices
Assuming gas usage testing verifies contract correctness
2. Which of the following is the correct way to get gas used from a transaction receipt in Solidity testing frameworks?
easy
A. const gasUsed = receipt.gasUsed;
B. const gasUsed = receipt.gas_price;
C. const gasUsed = receipt.gasLimit;
D. const gasUsed = receipt.gasCost;
Solution
Step 1: Identify the correct property for gas used
Transaction receipts have a property called gasUsed that shows the gas consumed.
Step 2: Eliminate incorrect properties
gas_price is gas price per unit, gasLimit is max gas allowed, and gasCost is not a standard property.
Final Answer:
const gasUsed = receipt.gasUsed; -> Option A
Quick Check:
Gas used = receipt.gasUsed [OK]
Hint: Gas used is in receipt.gasUsed property [OK]
Common Mistakes:
Using gas_price instead of gasUsed
Confusing gasLimit with gas used
Trying to access non-existent gasCost property
3. Consider this JavaScript test snippet for a smart contract function:
const receipt = await contract.methods.myFunction().send({ from: user });
console.log(receipt.gasUsed);
What will this code output?
medium
A. An error because gasUsed is undefined
B. The gas price set for the transaction
C. The amount of gas used by myFunction execution
D. The total gas limit of the block
Solution
Step 1: Understand what receipt.gasUsed represents
The receipt.gasUsed property contains the gas consumed by the transaction.
Step 2: Confirm the code logs gas used
The code logs receipt.gasUsed, so it outputs the gas used by myFunction.
Final Answer:
The amount of gas used by myFunction execution -> Option C
Quick Check:
receipt.gasUsed logs gas used [OK]
Hint: receipt.gasUsed shows gas consumed by function call [OK]