Skip to main content

Command Palette

Search for a command to run...

Difference Between require, revert, and assert in Solidity!

Updated
2 min read
Difference Between require, revert, and assert in Solidity!
S
Relentlessly curious and fiercely high‑agency, I'm a senior software engineer who thrives on taking products from zero to production‑ready in record time. Having worked across startups and fast-paced organizations, I've built a reputation as a cracked engineer who solves hard problems fast and owns the entire product stack end-to-end. My knack for rapid problem‑solving and end‑to‑end ownership turns complex ideas into polished, scalable products that ship fast and scale even faster. phewww... I love a cup of coffee ☕ while writing code. So let's connect and have a meet over something you wanna discuss. I'll be more than happy to have it. Book a call with me: https://cal.com/xshivank

Introduction

Hey folks, in this article we're gonna explore the differences between require, revert, and assert functions in Solidity! If you aren't aware about Solidity, then you can get a brief idea about it by clicking here.

  • What is require function?

  1. The require function is used to verify some conditions before any statement is executed. If the given condition tends to be false, then it would give an error and the execution would stop.

  2. In short, require statements declare the prerequisites for running the function, which has to be satisfied to work.

  3. During the event, the execution is terminated due to a false condition, the unused gas is returned to the caller and the state is reversed to the original state.

For Example-

require(num == 23, "The number entered is incorrect!");

Advantages

  1. Imply conditions before execution.

  2. Authenticate user responses.

  • What is revert function?

  1. The revert function does not evaluate any condition and does not depend on any state or statement.

  2. It is used for handling more complex operations.

  3. When a revert statement is called the unused gas is returned and the state reverts to its original state.

For Example-

revert("There's an error!");

Advantages

  1. Unused gas is returned.

  2. Capable of handling complicated operations.

  • What is assert function?

  1. The assert function is used to check those statements which can never be false.

  2. It plays a vital role in preventing impossible scenarios. The output is declared in boolean form.

  3. In contrast to require and revert functions, assert doesn't return any unused gas instead it consumes the gas to reverse the program to its original state.

For Example-

assert(value > 345);

Advantages

  1. Prevents impossible scenarios.

  2. Used for cases where statements can never be false.

I hope that you must have found this article quite helpful. If yes, then do give a read to some of my other articles!

Who knows you might become a great programmer 🤔!

O

Succint

More from this blog

Shivank Kapur

57 posts

learning distributed systems 🦀 / dev-rel eng @routerprotocol / member @superteam / previously @thedogecapital @pushprotocol