1

Was trying to hard code a whitelist array of addresses using mapping got this error: my code

enter image description here

my code:

contract IntelliContract {

    string public constant name = "CustomEnergyToken"; // solidity automatically creates a getter function for public variables
    string public constant symbol = "CET"; 
    uint8 public constant decimals = 18; 
    address public constant deadAddress = 0x000000000000000000000000000000000000dEaD;
    mapping(address => bool) privatewhiteList;
    whiteList [0x4A5B301C57FA01F1F432a9776c863D9645C17bBa]=true;
    whiteList [0x98deB3352Be46cB12f3d59160E5c6291880B1001]=true;
    whiteList [0x000000000000000000000000000000000000dEaD]=true;

    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
    event Transfer(address indexed from, address indexed to, uint tokens);


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

    mapping(address => uint256) balances; 

    mapping(address => mapping (address => uint256)) allowed; 

    uint256 totalSupply_;
    address ownerCon;


    using SafeMath for uint256;


    constructor(uint256 total) public {  // special function , only called at time of deployemnet
    totalSupply_ = total ;
    balances[msg.sender] = total ;  // To Deposit all the newly generated tokens in owner's account
    ownerCon = msg.sender;
    }  

    function totalSupply() public view returns (uint256) {
    return totalSupply_;
    }
    // getter function

    function balanceOf(address inputAddress) public view returns (uint) {
        return balances[inputAddress];
    }
    // getter function 
    
    function transfer(address receiver, uint numTokens) public returns (bool) {
        require(numTokens <= balances[msg.sender], "Not Sufficient Balance");
        balances[msg.sender] = balances[msg.sender].sub(numTokens);
        balances[receiver] = balances[receiver].add(numTokens);
        emit Transfer(msg.sender, receiver, numTokens); // logging these values using events
        return true;
    }


    function approve(address approved_addr, uint numTokens) public returns (bool) {
        allowed[msg.sender][approved_addr] = numTokens;
        emit Approval(msg.sender, approved_addr, numTokens);
        return true;
    }


    function allowance(address owner, address token_manger) public view returns (uint) {
        return allowed[owner][token_manger];
    }// what allowance has been provided by token_owner to Token_manager



    function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
        require(numTokens <= balances[owner]);    
        require(numTokens <= allowed[owner][msg.sender]);
        balances[owner] = balances[owner].sub(numTokens);
        allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens);
        balances[buyer] = balances[buyer].add(numTokens);
        emit Transfer(owner, buyer, numTokens);
        return true;
    }
//IncreaseTotalSupply

    function IncreaseSupply(uint256 total) public {  // special function , only called at time of deployemnet
    totalSupply_ += total ;
    balances[msg.sender] = balances[msg.sender].add(total) ;  // To Deposit all the newly generated tokens in owner's account
    }

// 2) You have to implement a function to decreace the total supply/Burning tokens.

    function BurnTokens(uint numTokens) public returns(bool){
        require(numTokens <= balances[msg.sender]);
        totalSupply_-=numTokens;
        balances[msg.sender]=balances[msg.sender].sub(numTokens);
        balances[deadAddress] = balances[deadAddress].add(numTokens);
        emit Transfer(msg.sender,deadAddress,numTokens);
        return true;
    }

    // 3) Whitelist Feature. (Transfer Tokens From One account to another can only take place for whitelisted address)
        function whiteListTransfer(address receiver, uint numTokens) public returns (bool) {
            require(WhiteList[receiver]==true,"not a whitelist address");
            require(numTokens <= balances[msg.sender], "Not Sufficient Balance");
            balances[msg.sender] = balances[msg.sender].sub(numTokens);
            balances[receiver] = balances[receiver].add(numTokens);
            emit Transfer(msg.sender, receiver, numTokens); 
            return true;
        
    }
}

2 Answers 2

0

As Ali Murtaza suggested, you can move the setup of the whitelist into the constructor.

Also, I suggest to add a logic to dynamically add/remove whitelisted addresses, that only the owner can access:

    constructor(uint256 total) public {  // special function , only called at time of deployemnet
        totalSupply_ = total ;
        balances[msg.sender] = total ;  // To Deposit all the newly generated tokens in owner's account
        ownerCon = msg.sender;
        whiteList[0x4A5B301C57FA01F1F432a9776c863D9645C17bBa] = true;
        whiteList[0x98deB3352Be46cB12f3d59160E5c6291880B1001] = true;
        whiteList[0x000000000000000000000000000000000000dEaD] = true;
    }

    function updateWhitelist(address _address, bool whiteListed) public onlyOwner {
        whiteList[_address] = whiteListed;
        // or:
        // remove whiteList[_address]; // if whiteListed is false, for example.
    }
1
  • 1
    thx man the error was solved and I also appreciate the contribution of adding new address to whitelist logic. Commented Aug 12, 2022 at 4:53
1

You cannot initialize like this at the state level, but you can perform the same functionality inside the constructor.

And please make sure to post your code, not upload the images.

1
  • thanks I will try, and also i have uploaded the whole code thx for suggestion Commented Aug 10, 2022 at 17:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.