0
0
Blockchain / Solidityprogramming~3 mins

Why Require, assert, and revert in Blockchain / Solidity? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if a tiny missing check could cost millions in a smart contract? Learn how to stop that now.

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
Before
if (balance >= amount) {
  // proceed
} else {
  // error handling
}
After
require(balance >= amount, "Not enough balance");
What It Enables

This makes smart contracts safer and easier to understand by clearly enforcing rules and stopping errors early.

Real Life Example

When sending tokens, require ensures the sender has enough tokens before transfer, assert checks internal logic, and revert undoes changes if something goes wrong.

Key Takeaways

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.