How to create a smart contract to whitelist users
Tags: solidity, blockchain, web3, smartcontract
Published: 2021-12-14 13:10:30

## 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](https://remix.ethereum.org/) 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

```js
// 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
    }

}
```

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

```js
modifier onlyOwner() {
    require(msg.sender == owner, "Ownable: caller is not the owner");
    _;
}
```

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.

```js
mapping(address => bool) whitelistedAddresses;
```

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.

```js
function addUser(address _addressToWhitelist) public onlyOwner {
    whitelistedAddresses[_addressToWhitelist] = true;
}
```
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

```js
whitelistedAddresses[_addressToWhitelist] = true;
```
### 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).

```js
function verifyUser(address _whitelistedAddress) public view returns(bool) {
    bool userIsWhitelisted = whitelistedAddresses[_whitelistedAddress];
    return userIsWhitelisted;
}
```

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

```js
modifier isWhitelisted(address _address) {
  require(whitelistedAddresses[_address], "You need to be whitelisted");
  _;
}
```

```js
function exampleFunction() public view isWhitelisted(msg.sender) returns(bool){
  return (true);
}
```

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](https://twitter.com/manelferreira_)