0

I'm working on a contract that pays a percentage of interest of a token. The idea is to enable the fallback function only once for an address, and then it can access again to the function when it is whitelisted again. Here's an extract of my code:

contract PercDistribution {

    mapping (address => uint256) balances;
    mapping (address => bool) public whitelist;
    Token public myToken;
    address public owner;

    function PercDistribution(address _tokenAddress, address _owner) {
        myToken = Token(_tokenAddress);
        owner = _owner;
    }

    function enableWhitelist(address[] _addresses) onlyOwner {
    for (uint i = 0; i < _addresses.length; i++) {
            whitelist[_addresses[i]] = true;
        }
    }

    function disableWhitelist(address[] _addresses) onlyOwner {
    for (uint i = 0; i < _addresses.length; i++) {
            whitelist[_addresses[i]] = false;
        }
    }

    modifier onlyWhitelist() {
        require(whitelist[msg.sender] == true);
        _;
    }

    modifier onlyOwner() {
        require(owner == msg.sender);
        _;
    }

    function () payable onlyWhitelist public {
        address investor = msg.sender;
        //do something
        whitelist[investor] = false;
    }

}

Everything works as expected; I whitelist addresses in batch and then when someone call the fallback function his address is blacklisted until I whitelist it again. My question is: is there a way to whitelist every address (and I mean every one that could be created) with a single function, without having to batch thousands of address with the enableWhitelist function?

1

What you can do is store the opposite, instead of having a whitelist have a blacklist.

Intially it will be empty so everyone is whitelisted, when you want to block someone you explicitely set the value to true, and set it back to false to allow the address.

contract PercDistribution {

    mapping (address => uint256) balances;
    mapping (address => bool) public blacklist;
    Token public myToken;
    address public owner;

    function PercDistribution(address _tokenAddress, address _owner) {
        myToken = Token(_tokenAddress);
        owner = _owner;
    }

    function enableWhitelist(address[] _addresses) onlyOwner {
    for (uint i = 0; i < _addresses.length; i++) {
            blacklist[_addresses[i]] = false;
        }
    }

    function disableWhitelist(address[] _addresses) onlyOwner {
    for (uint i = 0; i < _addresses.length; i++) {
            blacklist[_addresses[i]] = true;
        }
    }

    modifier onlyWhitelist() {
        require(blacklist[msg.sender] == false);
        _;
    }

    modifier onlyOwner() {
        require(owner == msg.sender);
        _;
    }

    function () payable onlyWhitelist public {
        address investor = msg.sender;
        //do something
        blacklist[investor] = true;
    }

}
|improve this answer|||||
  • This is a good solution if I need to have everyone whitelisted on the beginning, but still I need to use the enableWhitelist function for every address that used the fallback function. My question is: is there a way to trigger a whitelist again for all the addresses? Is there a way for example to map again the addresses with mapping (address => bool) public blacklist;? Thank you – user19489 Dec 5 '17 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy