1

I recently came accross an ICO that used temp wallets. The tokens were sent to the investors manually which means that not everyone received their tokens at the same time. Until everyone received their tokens, they were locked for transfers. However the owner was able to transfer the tokens to the investors. For learning purposes I tried to reproduce this logic and I was wondering if my attempt could actually be used in terms of efficiency and security. The transferFrom function has the same logic.

Thanks for your feedback.

function transfer(address _to, uint256 _value) public returns (bool success) {
    require(
        balances[msg.sender] >= _value
        && _value > 0
    );
    if(locked == true){
        if(msg.sender == owner){
            balances[msg.sender] = balances[msg.sender].sub(_value);
            balances[_to] = balances[_to].add(_value);
            emit Transfer(msg.sender, _to, _value);
            return true;
        }else{
            revert();
        }
    }else{
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);
        emit Transfer(msg.sender, _to, _value);
        return true;
    }
}
0

To avoid repeating logic:

function transfer(address _to, uint256 _value) public returns (bool success) {
    require(balances[msg.sender] >= _value);

    require(!locked || (msg.sender == owner));

    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    emit Transfer(msg.sender, _to, _value);
    return true;
}

(I dropped the requirement that _value >= 0 because ERC20 actually expects transfers of 0 to succeed.)

0

An alternative is to use a white list and a modifier

mapping (address => bool) allowTransfer;

modifier canTransfer(address user) {
    require(!locked || allowTransfer[user]);
    _;
}

function enableTransfer(address user, bool enable) onlyOwner {
    allowTransfer[user] = enable;
}

And you have to slightly modify the transfer function

function transfer(address _to, uint256 _value) 
        canTransfer(msg.sender) 
        public returns (bool success) {
    require(balances[msg.sender] >= _value);
    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    emit Transfer(msg.sender, _to, _value);
    return true;
}

Credits to the implementation of ReleseableToken in TokenMarket repos.

Your Answer

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

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