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.– AdamCommented Sep 15, 2017 at 21:45
Add a comment
|
2 Answers
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 {
//....
}
}
-
1See also github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/… Commented Jan 18, 2017 at 15:22
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;
}
}