5

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

  • 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 Sep 15 '17 at 21:45
8

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 {
        //....
    }
}
|improve this answer|||||
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;
    }
}
|improve this answer|||||

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.