These commands help check if things are right in your smart contract. If something is wrong, they stop the contract from running and can show a message.
Require, assert, and revert in Blockchain / Solidity
require(condition, "error message"); assert(condition); revert("error message");
require checks conditions and shows a message if false.
assert checks for errors that should never happen.
require(balance >= amount, "Not enough balance");assert(totalSupply == balancesSum);if (msg.sender != owner) { revert("Only owner can call this"); }
This contract lets users deposit and withdraw money. It uses require to check if users have enough money before withdrawing. It uses assert to make sure only the owner can do emergency withdrawal. It uses revert to stop anyone but the owner from calling a special function.
pragma solidity ^0.8.0; contract SimpleBank { mapping(address => uint) public balances; address public owner; constructor() { owner = msg.sender; } function deposit() public payable { balances[msg.sender] += msg.value; } function withdraw(uint amount) public { require(balances[msg.sender] >= amount, "Insufficient balance"); balances[msg.sender] -= amount; payable(msg.sender).transfer(amount); } function emergencyWithdraw() public { assert(msg.sender == owner); // Owner can withdraw all funds in emergency payable(owner).transfer(address(this).balance); } function onlyOwnerFunction() public { if (msg.sender != owner) { revert("Only owner allowed"); } // Some owner-only logic here } }
require refunds unused gas and returns your error message.
assert is for checking things that should never fail; if it fails, it uses all gas.
revert lets you stop execution and show a message anywhere in your code.
require checks inputs and conditions, stops with a message if false.
assert checks for bugs, stops if something impossible happens.
revert stops execution and shows a message, useful for custom checks.
Practice
require in Solidity smart contracts?Solution
Step 1: Understand the role of
requirerequireis used to validate inputs or conditions before continuing execution.Step 2: Compare with other statements
assertchecks internal errors, andrevertstops execution with a message, butrequireis specifically for input validation.Final Answer:
It checks user inputs or conditions and stops execution with an error message if the condition is false. -> Option AQuick Check:
require = input check with error [OK]
- Confusing require with assert for internal errors
- Thinking require does not stop execution
- Believing require logs info without stopping
assert in Solidity?Solution
Step 1: Recall assert syntax
In Solidity,asserttakes only a condition without an error message.Step 2: Check each option
assert(condition, "Error message"); wrongly includes an error message, C and D have invalid syntax.Final Answer:
assert(condition); -> Option AQuick Check:
assert syntax = assert(condition); [OK]
- Adding error messages to assert
- Using braces or missing parentheses
- Writing assert as a statement without parentheses
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 = 15Solution
Step 1: Check require condition
Input x=15 is greater than 10, so require passes.Step 2: Check assert condition
assert(x != 15) fails because x is 15, so execution stops here.Final Answer:
Execution stops at assert because x equals 15. -> Option CQuick Check:
assert fails if condition false [OK]
- Thinking require fails for x=15
- Assuming revert triggers for x=15
- Believing function runs fully without errors
function check(uint value) public {
require(value > 0, "Value must be positive");
assert(value < 100, "Value too large");
}Solution
Step 1: Review require usage
Require correctly uses a condition and an error message.Step 2: Review assert usage
Assert does not accept an error message; including one causes a syntax error.Final Answer:
The assert statement incorrectly includes an error message. -> Option DQuick Check:
Assert has no message parameter [OK]
- Adding messages to assert
- Confusing require and revert usage
- Assuming no error in assert syntax
require, assert, and revert is best to use?Solution
Step 1: Identify correct use of require
Require is ideal for checking user inputs like balance with error messages.Step 2: Identify correct use of assert
Assert is for internal errors that should never happen, stopping immediately without messages.Step 3: Consider revert usage
Revert can be used for complex checks but is not necessary here if require and assert cover cases.Final Answer:
Use require to check balance and assert for internal errors; no need for revert. -> Option BQuick Check:
Require for inputs, assert for internal errors [OK]
- Using assert for input validation
- Using revert unnecessarily for simple checks
- Mixing assert and require roles
Ready to prove it?
Pass the test and this pattern moves unknown
