Address list pattern

The address list pattern is used to maintain a curated list of addresses by the owner.

In contracts, there are some situations in which you would need a curated list of addresses. For example, you would need a list of whitelisted addresses that are allowed to call a certain function of your contract. Another example is when you want to maintain a list of supported tokens addresses to allow your contracts to interact with these selected tokens only.

In the address list pattern, adding and removing addresses from the list can only be done by the owner of the contract. In the following code, we have an AddressList contract:

contract AddressList is Ownable {

mapping(address => bool) internal map;

function add(address _address) public onlyOwner {
map[_address] = true;
}

function remove(address _address) public onlyOwner {
map[_address] = false;
}

function isExists(address _address) public view returns (bool) {
return map[_address];
}
}

As you can see in the code, only the owner can add or remove addresses in the contract by calling the add() or remove() functions. The isExists() function is a view function and is open for anyone to call. Another contract can even call this function to check whether an address is present in this list or not.

Let's understand where the address list pattern should be applied.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.227.52