File size: 2,111 Bytes
f998fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

import "./Ownable.sol";

contract Whitelist is Ownable {
    mapping(address => bool) private whitelist;
    mapping(address => uint) private addressIdx;
    address[] private whiteArr;

    event WhitelistedAddressAdded(address addr);
    event WhitelistedAddressRemoved(address addr);

    constructor() {
        addAddressToWhitelist(msg.sender);
    }

    modifier onlyWhitelisted() {
        require(whitelist[msg.sender], "sender have to whitelisted");
        _;
    }

    function isWhitelist(address addr) public view returns (bool success) {
        return whitelist[addr];
    }

    function addAddressToWhitelist(address addr) onlyOwner public returns (bool success) {
        if (!whitelist[addr]) {
            whitelist[addr] = true;
            addressIdx[addr] = whiteArr.length;
            whiteArr.push(addr);

            emit WhitelistedAddressAdded(addr);
            success = true;
        }
    }

    function removeAddressFromWhitelist(address addr) onlyOwner external returns (bool success) {
        if (whitelist[addr]) {
            whitelist[addr] = false;
            uint deleteIdx = addressIdx[addr];
            address lastAddr = whiteArr[whiteArr.length - 1];
            addressIdx[lastAddr] = deleteIdx;
            delete addressIdx[addr];
            whiteArr[deleteIdx] = lastAddr;
            whiteArr.pop();

            emit WhitelistedAddressRemoved(addr);
            success = true;
        }
    }

    function getCountWhiteArr() public onlyOwner view returns (uint count) {
        count = whiteArr.length;
    }

    function getWhiteArrByIdx(uint idx) public onlyOwner view returns (address whiteAddress) {
        whiteAddress = whiteArr[idx];
    }

    function getWhiteArr() public onlyOwner view returns (address[] memory whiteArray) {
        whiteArray = whiteArr;
    }
}