What is Require in Solidity & How to Use it?

What is Require in Solidity & How to Use it?

What is Solidity?

Solidity is a high level, object-oriented programming language for writing smart contracts in the Ethereum Blockchain. Smart contracts are used to manipulate the Ethereum Blockchain and govern the behaviour of the accounts within the Ethereum Blockchain.

Solidity is highly influenced by Javascript, C++ and Python. It is designed to target the EVM (Ethereum Virtual Machine). With Solidity, you can create interesting Web3.0 projects like a crowdfunding system, blind auctions, multi-signature wallets. Solidity keeps receiving many regular updates which means that you have to adapt to the new version as quickly as you can. The most recent Solidity version is 0.8x.

What is Require in Solidity?

As the word suggests, require basically means to demand something before availing the service to the users. For example, websites require you to create an account or login into an existing account before giving you access to the account which belongs to you. This is the basic flow of how ‘require’ works in Solidity also. Suppose there are is an account used by Suyash and he wants to send five Ethereum to an account used by Aditya. Now in order for Suyash to send Ethereum to Aditya, Suyash should have at least five Ethereum in his account to send the money to Aditya. So in the Solidity code, we can “require” the sender’s balance to be greater than or equal to the balance he wants to send to the receiver otherwise the transaction should obviously fail. This is just an example of the numerous use cases that use “require” and enable us to perform multiple checks before approving a transaction or anything else.

The above was just a real-life example, let’s take a look at how it works in technical terms. ‘require’ returns two boolean values that are either true or false, if the specified condition returns a true value it allows the code to flow and function accordingly. If the value returned is false, it throws an error and stops the code right there. “require” takes two parameters, first is the condition that you want to check and the second is an optional error message you want to show to the user when and if the value returned is false.

Let’s take a look at how to use “require” in Solidity

pragma solidity ^0.8.4; 

contract Bank { 
mapping(address => uint) balance;
address owner; 
constructor() { 
owner = msg.sender; 
// address that deploys contract will be the owner 

function addBalance(uint _toAdd) public returns(uint) {
 require(msg.sender == owner);
 balance[msg.sender] += _toAdd; 
 return balance[msg.sender]; 

function getBalance() public view returns(uint) {
 return balance[msg.sender]; 

function transfer(address recipient, uint amount) public { 
require(balance[msg.sender]>=amount, "Insufficient Balance"); 
require(msg.sender != recipient, "You can't send money to yourself!");
 _transfer(msg.sender, recipient, amount); 

function _transfer(address from, address to, uint amount) private { 
balance[from] -= amount; balance[to] += amount; 


In the above code sample, we will take a look at multiple uses of ‘require’. In the constructor, we are setting the owner of the contract to be the address that deploys the contract. In the addBalance() function, we are using require to ensure that only the owner of the smart contract can add balance to the contract and no one else can. In the transfer() function we are using require twice. The first time is to check the sender’s balance and ensure that it is above the required balance. The second time we are making sure that the user cannot send money to himself. In both of these “require” calls, we are using the second parameter to show the error to the user in case the ‘require’ returns a false value.

Be a Web3 & Blockchain Developer

How you can start learning Solidity via Codedamn?

Codedamn offers a concise learning path to help you get started with writing Smart Contracts in Solidity to help you build multiple projects in the Web3.0 space. You can learn about the Ethereum Blockchain, Solidity, Smart Contracts, MetaMask, Creating your own coin and launching it, ICO(Initial Coin Offering), etc.

You can access the course here

Codedamn’s Solidity related material:

  • Solidity Fundamentals
  1. What is Solidity? 
  2. Remix IDE
  3. Solidity Compilation Steps.
  4. Mainnet vs Testnet 
  • Solidity Programming
  1. Say “Hello World” in Solidity 
  2. Contract Development Environment 
  3. Solidity Sample Program
  4. State Variables 
  5. Local Variables
  6. Functions
  7. Create functions
  8. Pass an argument to the function
  9. View vs Pure 
  10. Constructor
  11. Integers 
  12. Strings 
  13. Storage vs Memory vs Stack on EVM   
  14. If Else 
  15. Booleans 
  16. Fixed Size Array
  17. Dynamic Size Array 
  18. Fixed (byte) Size Array
  19. Dynamic (byte ) Size Array 
  20. Dynamic Array
  21. Loops 
  22. Storage program 
  23. Struct 
  24. Enum 
  25. Mappings 
  26. Mappings Struct  
  27. Global Variables (Special functions and variables)
  • Solidity Advanced Concepts
  1. Inheritance 
  2. Abstract Contracts 
  3. Interface 
  4. Polymorphism 
  5. Visibility
  6. Require 
  7. Modifier 
  8. Payable Function 
  9. Payable Address 
  10. Fallback Function 

There are many practice labs that you can use to try out the recent concepts you have learned along the way!!

Where to write Solidity Code?

You can use Codedamn’s Solidity Playground. PlayGround lets you write and edit Solidity code which you can easily run and compile right in the browser. You just need to type your code and click on the RunCode button on the bottom left of the screen and the output of your code will be displayed in the terminal. You can use the Codedamn Playground to write Smart Contracts for your Web3 projects as well

You can use the Playground for free

Codedamn Compiler opens up a docker container in the backend of the website which then uses WebSockets to verify your credentials and then help run the code in the background and display the output to you in the Terminal. In simple terms, it opens a separate PC in the background which compiles your Solidity Code checks for any errors or problems in your code and shows the output to you on your computer in the Terminal of the Codedamn PlayGround. Codedamn playground uses Solc, which has been rated as the best compiler for Solidity. Making use of Solc compiles your code and displays the output in a matter of a few seconds. It produces various outputs ranging from assemblies and simple binaries over an abstract syntax tree to estimations of gas usage. 

Why use Playground Compiler?

Installing a separate code editor for only one specific language can be a hassle. Compiling your code on Codedamn Playground is very easy as it opens up another computer for you that does all the work in the background without making your own Computer Lag and also compiles it faster than any other compiler available anywhere. The ease of use is another crucial factor that ensures that all your files are in one place and are always safe, due to the AutoSave function which saves every line of code you write ensuring that you never lose your work.

Check our Online Solidity Compiler here – Just code from your browser

Sharing is caring

Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.


No comments so far