Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Gas usage testing in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Gas usage testing
Write smart contract function
↓
Deploy contract to test network
↓
Call function with test inputs
↓
Measure gas used by transaction
↓
Analyze gas usage
↓
Optimize code if needed
↓
Repeat testing
This flow shows how to write, deploy, call, measure, and optimize gas usage in smart contracts.
Execution Sample
Blockchain / Solidity
pragma solidity ^0.8.0;

contract GasTest {
    uint public count;
    function increment() public {
        count += 1;
    }
}
A simple contract with a function that increments a counter, used to test gas cost of calling increment.
Execution Table
StepActionGas Used (approx.)State ChangeNotes
1Deploy contract1500000count = 0Initial deployment cost
2Call increment()2300count = 1First increment call
3Call increment()2300count = 2Second increment call
4Call increment()2300count = 3Third increment call
5Stop testing--No more calls
💡 Testing stops after desired number of calls to measure gas usage.
Variable Tracker
VariableStartAfter 1After 2After 3
count0123
Key Moments - 2 Insights
Why does the gas used for deployment differ from gas used for function calls?
Deployment includes creating the contract on the blockchain, which costs more gas (see Step 1 in execution_table), while function calls only execute code and update state.
Why is gas usage roughly the same for each increment call?
Each increment call does the same simple operation (count += 1), so gas used is consistent (see Steps 2-4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after Step 3?
A2
B1
C3
D0
💡 Hint
Check the 'State Change' column in row for Step 3.
At which step does the contract get deployed?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for the action 'Deploy contract' in the execution_table.
If the increment function did more complex calculations, how would gas usage change?
AGas used would decrease
BGas used would stay the same
CGas used would increase
DGas used would be zero
💡 Hint
More operations in a function increase gas cost; see gas used in Steps 2-4.
Concept Snapshot
Gas usage testing steps:
1. Write and deploy contract
2. Call functions with test inputs
3. Measure gas used per call
4. Analyze and optimize
Gas cost depends on code complexity and state changes.
Full Transcript
Gas usage testing in blockchain means checking how much gas a smart contract function uses when called. First, you write a contract and deploy it to a test network. Deployment costs gas because it stores the contract on the blockchain. Then, you call the function you want to test, like incrementing a number. Each call uses gas depending on what the function does. You record gas used and state changes after each call. If gas is too high, you can optimize your code and test again. This helps save money when using real networks.

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

  1. Step 1: Understand gas usage testing

    Gas usage testing measures the amount of gas consumed by smart contract functions during execution.
  2. Step 2: Differentiate from other tests

    It is not about syntax errors, logic correctness, or changing gas prices but about cost measurement.
  3. Final Answer:

    To measure how much gas a function consumes when executed -> Option D
  4. 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

  1. Step 1: Identify the correct property for gas used

    Transaction receipts have a property called gasUsed that shows the gas consumed.
  2. Step 2: Eliminate incorrect properties

    gas_price is gas price per unit, gasLimit is max gas allowed, and gasCost is not a standard property.
  3. Final Answer:

    const gasUsed = receipt.gasUsed; -> Option A
  4. 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

  1. Step 1: Understand what receipt.gasUsed represents

    The receipt.gasUsed property contains the gas consumed by the transaction.
  2. Step 2: Confirm the code logs gas used

    The code logs receipt.gasUsed, so it outputs the gas used by myFunction.
  3. Final Answer:

    The amount of gas used by myFunction execution -> Option C
  4. Quick Check:

    receipt.gasUsed logs gas used [OK]
Hint: receipt.gasUsed shows gas consumed by function call [OK]
Common Mistakes:
  • Thinking gasUsed is gas price or block gas limit
  • Expecting gasUsed to be undefined or cause error
  • Confusing gasUsed with transaction cost in ether
4. You wrote this test code to measure gas usage:
const receipt = await contract.methods.transfer(to, amount).send({ from: sender });
const gasUsed = receipt.gasPrice;
console.log(gasUsed);

What is the error here?
medium
A. Using receipt.gasPrice instead of receipt.gasUsed to get gas consumed
B. Missing await keyword before contract method call
C. Incorrect method name; should be transferFrom
D. send() does not return a receipt object

Solution

  1. Step 1: Check property used for gas consumption

    The code uses receipt.gasPrice, which is gas price per unit, not gas used.
  2. Step 2: Identify correct property for gas used

    The correct property to get gas consumed is receipt.gasUsed.
  3. Final Answer:

    Using receipt.gasPrice instead of receipt.gasUsed to get gas consumed -> Option A
  4. Quick Check:

    Gas used = receipt.gasUsed, not gasPrice [OK]
Hint: Use receipt.gasUsed, not gasPrice, for gas consumption [OK]
Common Mistakes:
  • Confusing gasPrice with gasUsed
  • Omitting await causing promise issues
  • Assuming send() returns something other than receipt
5. You want to compare gas usage of two smart contract functions, funcA and funcB, to find which is cheaper. You run:
const receiptA = await contract.methods.funcA().send({ from: user });
const receiptB = await contract.methods.funcB().send({ from: user });
console.log(receiptA.gasUsed < receiptB.gasUsed ? 'funcA cheaper' : 'funcB cheaper');

What is a potential issue with this approach?
hard
A. You cannot compare gasUsed values directly; must convert to ether
B. Gas usage can vary due to blockchain state; single runs may mislead
C. You must add gasPrice to gasUsed to get total cost
D. send() does not return gasUsed in receipt

Solution

  1. Step 1: Understand gas usage variability

    Gas used depends on blockchain state and inputs; single runs may not reflect average cost.
  2. Step 2: Identify why single comparison can mislead

    Different states or conditions can cause gas usage to fluctuate, so multiple tests or averaging is better.
  3. Final Answer:

    Gas usage can vary due to blockchain state; single runs may mislead -> Option B
  4. Quick Check:

    Gas varies by state; test multiple times [OK]
Hint: Gas varies; test multiple times for accurate comparison [OK]
Common Mistakes:
  • Assuming gasUsed is fixed for all runs
  • Adding gasPrice to gasUsed incorrectly
  • Thinking gasUsed must convert to ether for comparison
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard