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
  • how do you put address ?? i mean the format like address[0],address[1] this type or format or what ? please help me i cant add address to the whitelist i gets error like > transact to TokenSale.addWhitelist errored: Error encoding arguments: > Error: expected array value (argument=null, > value="0xfb6fCcF4b68XXXXXXXX6B11bE2d9F4Add", code=INVALID_ARGUMENT, > version=abi/5.1.2)
    – Bee Nis
    Commented Jul 26, 2021 at 3:08

1 Answer 1

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;
    }

}
1
  • 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
    Commented Dec 5, 2017 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.