Token Contract – How to Transfer Ownership of a Token Contract

openzeppelinsoliditytokens

I'm trying to create a token contract using openzeppelin. I managed to create the token using the ownable contract from ownable.sol, which let me transfer the ownership of the token contract.

However, I need to transfer the token contract ownership to a crowdsale contract. I'm wondering if there is a way to reclaim/transfer the token contract ownership to another address.

Here's at a glance what I'm trying to do.

  1. Token contract created [solved]
  2. Crowdsale contract created [solved]
  3. Token contract ownership transferred to the crowdsale contract address [solved]
  4. Transfer the Token contract ownership to another address. [NEED SOLUTION]

Any suggestion?

Best Answer

Ownable.sol has the following method:

/**
   * @dev Allows the current owner to transfer control of the contract to a newOwner.
   * @param newOwner The address to transfer ownership to.
   */
  function transferOwnership(address newOwner) public onlyOwner {
    require(newOwner != address(0));
    OwnershipTransferred(owner, newOwner);
    owner = newOwner;
  }

If you want to transfer ownership of the token contract (and assuming the token is Ownable) you can call transferOwnership(address_of_new_owner) using the account that currently owns the token contract.

Have in mind that unless the crowdsale contract that now owns the token also implements a function that can call this function, you won't be able to transfer ownership back again if you wanted so.


Here's an example.

  1. You have the token contract with the transferOwnership() function above. The current owner is your own account.

  2. You call that function passing the address of the crowdsale. Now the owner = (the crowdsale).

  3. If you now wanted to transfer ownership from the crowdsale to another account, the only one that can do so is the actual crowdsale contract as it is the current owner.

For that to be possible, the crowdsale contract would have to have the following code:

function transferOwnership(address _newOwner) public onlyOwner {
  require(msg.sender == owner); // Only the owner of the crowdsale contract should be able to call this function.

  // I assume the crowdsale contract holds a reference to the token contract.
  token.transferOwnership(_newOwner);

}
Related Topic