Bird
Raised Fist0
Blockchain / Solidityprogramming~30 mins

Require, assert, and revert in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style10 modes available
Require, assert, and revert in Solidity
📖 Scenario: You are building a simple smart contract for a token sale. You want to make sure that certain conditions are met before allowing users to buy tokens. This helps keep your contract safe and predictable.
🎯 Goal: Learn how to use require, assert, and revert statements in Solidity to check conditions and handle errors properly.
📋 What You'll Learn
Create a contract called TokenSale
Add a public variable tokensSold to track how many tokens are sold
Add a public variable maxTokens to set the maximum tokens available
Use require to check if the buyer sends enough Ether
Use assert to check internal state consistency
Use revert to cancel the transaction with a message if conditions fail
💡 Why This Matters
🌍 Real World
Smart contracts on blockchains must check conditions carefully to avoid errors and protect funds. Using require, assert, and revert helps enforce rules and handle mistakes safely.
💼 Career
Blockchain developers use these statements daily to write secure contracts that handle money and data correctly.
Progress0 / 4 steps
1
Create the TokenSale contract with initial variables
Create a Solidity contract called TokenSale. Inside it, declare a public unsigned integer variable tokensSold and set it to 0. Also declare a public unsigned integer variable maxTokens and set it to 100.
Blockchain / Solidity
Hint

Use contract TokenSale { ... } to start. Declare tokensSold and maxTokens as uint public variables.

2
Add a price variable and a buyTokens function with require
Inside the TokenSale contract, add a public unsigned integer variable tokenPrice and set it to 1 ether. Then create a public function called buyTokens that takes a parameter amount of type uint. Inside the function, use require to check that the message value (msg.value) is at least amount * tokenPrice.
Blockchain / Solidity
Hint

Declare tokenPrice as uint public and set to 1 ether. The buyTokens function should be public payable. Use require to check msg.value.

3
Add checks with require, assert, and revert inside buyTokens
Inside the buyTokens function, after the require you wrote, add another require to check that tokensSold + amount is less than or equal to maxTokens. Then increase tokensSold by amount. Use assert to check that tokensSold is not greater than maxTokens. Finally, add an if statement that checks if amount is zero, and if so, use revert with the message "Cannot buy zero tokens".
Blockchain / Solidity
Hint

Use require to check token limits, increase tokensSold, then assert the limit is not exceeded. Use if and revert to block zero token buys.

4
Test the buyTokens function by simulating a purchase
Add a public view function called tokensLeft that returns the number of tokens still available (which is maxTokens - tokensSold). Then add a public function called testPurchase that calls buyTokens with amount 10 and sends 10 ether as value. Finally, add a public view function called getTokensSold that returns tokensSold. Print the value of getTokensSold() after calling testPurchase() to show tokens sold.
Blockchain / Solidity
Hint

Create tokensLeft to return remaining tokens. testPurchase calls buyTokens with 10 tokens and 10 ether. getTokensSold returns tokens sold.

Practice

(1/5)
1. Which statement best describes the purpose of require in Solidity smart contracts?
easy
A. It checks user inputs or conditions and stops execution with an error message if the condition is false.
B. It is used to check for internal errors that should never happen in the code.
C. It immediately stops execution without any error message.
D. It logs information but does not stop the contract execution.

Solution

  1. Step 1: Understand the role of require

    require is used to validate inputs or conditions before continuing execution.
  2. Step 2: Compare with other statements

    assert checks internal errors, and revert stops execution with a message, but require is specifically for input validation.
  3. Final Answer:

    It checks user inputs or conditions and stops execution with an error message if the condition is false. -> Option A
  4. Quick Check:

    require = input check with error [OK]
Hint: Require is for input checks and stops if false [OK]
Common Mistakes:
  • Confusing require with assert for internal errors
  • Thinking require does not stop execution
  • Believing require logs info without stopping
2. Which of the following is the correct syntax to use assert in Solidity?
easy
A. assert(condition);
B. assert(condition, "Error message");
C. assert {condition};
D. assert condition;

Solution

  1. Step 1: Recall assert syntax

    In Solidity, assert takes only a condition without an error message.
  2. Step 2: Check each option

    assert(condition, "Error message"); wrongly includes an error message, C and D have invalid syntax.
  3. Final Answer:

    assert(condition); -> Option A
  4. Quick Check:

    assert syntax = assert(condition); [OK]
Hint: Assert takes only a condition, no message [OK]
Common Mistakes:
  • Adding error messages to assert
  • Using braces or missing parentheses
  • Writing assert as a statement without parentheses
3. What will happen when the following Solidity code runs?
function test(uint x) public {
    require(x > 10, "x must be greater than 10");
    assert(x != 15);
    if (x == 20) {
        revert("x cannot be 20");
    }
}

Input: x = 15
medium
A. Execution stops at require with error 'x must be greater than 10'.
B. Execution stops at revert with error 'x cannot be 20'.
C. Execution stops at assert because x equals 15.
D. Function completes successfully without errors.

Solution

  1. Step 1: Check require condition

    Input x=15 is greater than 10, so require passes.
  2. Step 2: Check assert condition

    assert(x != 15) fails because x is 15, so execution stops here.
  3. Final Answer:

    Execution stops at assert because x equals 15. -> Option C
  4. Quick Check:

    assert fails if condition false [OK]
Hint: Assert fails if condition is false, stopping execution [OK]
Common Mistakes:
  • Thinking require fails for x=15
  • Assuming revert triggers for x=15
  • Believing function runs fully without errors
4. Identify the error in this Solidity function:
function check(uint value) public {
    require(value > 0, "Value must be positive");
    assert(value < 100, "Value too large");
}
medium
A. The require statement is missing a message string.
B. There is no error; the code is correct.
C. The function should use revert instead of require.
D. The assert statement incorrectly includes an error message.

Solution

  1. Step 1: Review require usage

    Require correctly uses a condition and an error message.
  2. Step 2: Review assert usage

    Assert does not accept an error message; including one causes a syntax error.
  3. Final Answer:

    The assert statement incorrectly includes an error message. -> Option D
  4. Quick Check:

    Assert has no message parameter [OK]
Hint: Assert never takes error messages, only conditions [OK]
Common Mistakes:
  • Adding messages to assert
  • Confusing require and revert usage
  • Assuming no error in assert syntax
5. You want to write a Solidity function that checks if a user has enough balance to withdraw an amount. If the balance is insufficient, it should stop with a clear error message. If an unexpected internal error occurs, it should stop immediately without a message. Which combination of require, assert, and revert is best to use?
hard
A. Use assert for balance check and require for internal errors.
B. Use require to check balance and assert for internal errors; no need for revert.
C. Use revert to check balance with message and assert for internal errors.
D. Use only revert for both balance check and internal errors.

Solution

  1. Step 1: Identify correct use of require

    Require is ideal for checking user inputs like balance with error messages.
  2. Step 2: Identify correct use of assert

    Assert is for internal errors that should never happen, stopping immediately without messages.
  3. Step 3: Consider revert usage

    Revert can be used for complex checks but is not necessary here if require and assert cover cases.
  4. Final Answer:

    Use require to check balance and assert for internal errors; no need for revert. -> Option B
  5. Quick Check:

    Require for inputs, assert for internal errors [OK]
Hint: Require for inputs, assert for internal errors, revert optional [OK]
Common Mistakes:
  • Using assert for input validation
  • Using revert unnecessarily for simple checks
  • Mixing assert and require roles
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