What if a tiny missing check could cost millions in a smart contract? Learn how to stop that now.
Why Require, assert, and revert in Blockchain / Solidity? - Purpose & Use Cases
Imagine you are writing a smart contract that handles money transfers. Without checks, anyone could send wrong amounts or call functions at the wrong time, causing loss or errors.
Manually checking every condition with complicated if-statements is slow and easy to forget. Mistakes can let bugs slip in, causing contracts to behave unexpectedly or lose funds.
Using require, assert, and revert lets you clearly state rules that must be true. If a rule breaks, the contract stops immediately and safely, preventing damage.
if (balance >= amount) { // proceed } else { // error handling }
require(balance >= amount, "Not enough balance");This makes smart contracts safer and easier to understand by clearly enforcing rules and stopping errors early.
When sending tokens, require ensures the sender has enough tokens before transfer, assert checks internal logic, and revert undoes changes if something goes wrong.
Require checks user inputs and conditions before running code.
Assert verifies internal errors that should never happen.
Revert stops execution and undoes changes when a problem occurs.
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
