5

I have a contract belonging to one of my accounts. How can I transfer ownership to another account?

1
  • Can someone put this answer is less technical language and lay it out step by step please? I have some OMG tokens in an old contract which I can't send to a new wallet address. What do I have to do first? Use the contracts tab on MyEtherWallet? But then select what function and enter what information where? Thanks.
    – Adam
    Commented Sep 15, 2017 at 21:45

2 Answers 2

9

Your contract needs to implement an ownership transfer function. This will do it:

address public owner;
function transfer(address newOwner) public {
    if (msg.sender != owner) throw;
    owner = newOwner;
}

People often put this in an Owned contract, which you can inherit from in other contracts. For example:

contract Owned {
    address public owner;
    modifier onlyOwner() {
        if (msg.sender != owner) throw;
        _;
    }
    function transfer(address newOwner) public onlyOwner {
        owner = newOwner;
    }
}

Then in your other contracts:

contract myContract is Owned {
    function test() public onlyOwner {
        //....
    }
}
1
0

There is a catch in previous answer. The ownership would be transferred even if the owner sends a blank address by mistake. A better way would be to add a check before transferring ownership.

modifier onlyOwner() {
    if (msg.sender != owner) throw;
    _;
}
function transferOwnership (address newOwner) onlyOwner{
    //check if not empty
    if (newOwner != 0x0){
        owner = newOwner;
    }
}

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.