DEV Community

Cover image for How to create a smart contract to whitelist users
Emanuel Ferreira
Emanuel Ferreira

Posted on

How to create a smart contract to whitelist users

Overview

In this article, we going to create a smart contract to whitelist users, that it will be possible to add an address, check a user, and verify each function if the user has been whitelisted.

Prerequisites

  • Code Editor (Remix, VSCode)
  • Basic Solidity Knowledge

Remix Editor

To create the smart contract we are going to use the Remix editor that will help us to create our smart contract.

Ownable contract

First, we'll start with the basic structure of a contract, we are going to create a contract Ownable.

Setting the contract owner

To whitelist the users we need to have a contract owner that will have permission to add users to the whitelist

Now create a variable called owner and add the deployer address as the owner of the contract

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Whitelist {

    address owner; // variable that will contain the address of the contract deployer

    constructor() {
        owner = msg.sender; // setting the owner the contract deployer
    }

}
Enter fullscreen mode Exit fullscreen mode

Create the Modifier

The modifier will help us when we need to restrict a function to only the owner of the contract to use.

modifier onlyOwner() {
    require(msg.sender == owner, "Ownable: caller is not the owner");
    _;
}
Enter fullscreen mode Exit fullscreen mode

Now when you try to call a function that contains the onlyOwner modifier just will work if you are the contract owner

Create a list of addresses

To store our addresses, we need to create a mapping that will receive the address of the user and return if he is whitelisted or not.

mapping(address => bool) whitelistedAddresses;
Enter fullscreen mode Exit fullscreen mode

Now you can store the addresses in the whitelistedAddresses mapping, Let's do it in the next steps.

Adding an address to the whitelist

Now we'll manipulate the whitelistedAddresses mapping to say if the address is whitelisted or not, so when we call the addUser function we'll change the whitelisted status from the address to true.

function addUser(address _addressToWhitelist) public onlyOwner {
    whitelistedAddresses[_addressToWhitelist] = true;
}
Enter fullscreen mode Exit fullscreen mode

You can see that we have the onlyOwner modifier in our function, so just the contract owner can call this function.

We have the parameter _addressToWhitelist that is the address of a user that we want to whitelist.

So in this line, we are changing the status of the address to true.

true = whitelisted
false = not whitelisted

whitelistedAddresses[_addressToWhitelist] = true;
Enter fullscreen mode Exit fullscreen mode

Verifying if the user has been whitelisted

Now create a function that will say to you if the user has been whitelisted (you don't need a function, you can access the list in another function or create a modifier to verify).

function verifyUser(address _whitelistedAddress) public view returns(bool) {
    bool userIsWhitelisted = whitelistedAddresses[_whitelistedAddress];
    return userIsWhitelisted;
}
Enter fullscreen mode Exit fullscreen mode

So when you call this function it will return false if the address has not whitelisted and true if you added the user to the whitelist

Creating a modifier to whitelisted users

If you want to allow only the whitelisted addresses to call a function you can create a modifier.

So Let's create a modifier and a function to test example

modifier isWhitelisted(address _address) {
  require(whitelistedAddresses[_address], "You need to be whitelisted");
  _;
}
Enter fullscreen mode Exit fullscreen mode
function exampleFunction() public view isWhitelisted(msg.sender) returns(bool){
  return (true);
}
Enter fullscreen mode Exit fullscreen mode

Then our modifier will verify in the whitelisted addresses if the user (msg.sender) is whitelisted and still following with the function or break returning an error.

Conclusion

Now your contract to add users to the whitelist is done, you can adapt it to our projects like NFT collections verifying if the address is whitelisted to mint the NFTs.

Repository with the Project (Final code): https://github.com/EmanuelCampos/whitelist_contract
Stars are appreciated <3

Follow me on Twitter

Top comments (6)

Collapse
 
taweechaiscg profile image
taweechai-scg

Thanks , save my time

Collapse
 
haaariss profile image
haaariss

β€’ A constructor that stores the address and name of the owner
β€’ A function that allows the owner to create a whitelist of addresses.
β€’ A function that allows only the user from the whitelist to deposit tokens into the smartcontract
β€’ A function that allows only the users from the whitelist to withdraw any amount from their own deposited tokens

Collapse
 
aqiibali profile image
aqiibali

nahi mela ge assignment bahi chill kar

Collapse
 
haaariss profile image
haaariss

β€’ A constructor that stores the address and name of the owner
β€’ A function that allows the owner to create a whitelist of addresses.
β€’ A function that allows only the user from the whitelist to deposit tokens into the smartcontract
β€’ A function that allows only the users from the whitelist to withdraw any amount from their own deposited tokens

Collapse
 
mohmmadanas profile image
Mohmmadanas

This is amazing 🀩 Thank You
But I have question
I want ask about the NFT price
How we can make another price for whitelist user’s?

Collapse
 
shking profile image
Oluwaseyi Israel Shittu

Set different variables for mint price, then inculcate them with the minting stages