0

When I'm learning OpenZeppelin, I found its Ownable library has a function transferOwnership, which can give the owner of current contract to an address. I can understand change owner to an account address of someone, however, it can also change owner to a contract address. My question is: If I change owner of current contract to another contract address, how can I use the other contract to handle the owner of my original contract? I tried inheritance with super key word, it doesn't work.

The failure code is as follow.

BTW, if it's useful to change owner of current contract to another contract address? Is there any example project to use this case?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "@openzeppelin/contracts/access/Ownable.sol";

contract MyContract is Ownable {
    function getCurrentBalance() public view onlyOwner returns (uint) {
        return address(this).balance;
    }
    receive() external payable {}
}

contract ManageOwner is MyContract {
    function changeOwner(address newOwner) public  {
        super.transferOwnership(newOwner);
    }
}
E_net4
  • 27,810
  • 13
  • 101
  • 139
Huowuge
  • 41
  • 5
  • Just to clarify: 1) Both `MyContract` and `ManageOwner` are deployed on two separate addresses (e.g. `MyContract` on address A, and `ManageOwner` on address B)? 2) Your aim is to enable the `MyContract` deployer (i.e. the current `owner`) and nobody else to invoke `changeOwner()` and effectively change the `MyContract` (on address A) owner? – Petr Hejda Jan 28 '23 at 19:40
  • Yes! Absolutely right. – Huowuge Jan 29 '23 at 01:11
  • I use interface and success. The codes are as follow: – Huowuge Jan 29 '23 at 08:37

1 Answers1

0

I use interface and success. The codes are as follow:

import "@openzeppelin/contracts/access/Ownable.sol";

contract A is Ownable {

    receive() external payable {}

    function getCurrentBalance() public view onlyOwner returns (uint) {
        return address(this).balance;
    }
}

interface I {
    function getCurrentBalance() external view returns (uint) ;
    function transferOwnership(address newOwner) external;
}


contract B is Ownable {

    I public itf = I(contract_A_address_); 

    receive() external payable {}

    function getBalanceOfA() public view onlyOwner returns (uint) {
        return itf.getCurrentBalance();
    }

    function changeAOwner(address newOwner) public onlyOwner{
        itf.transferOwnership(newOwner);
    }
}

First, deploy contract A. 2nd, copy contract A address to contract B. 3rd, deploy B. 4th, call transferOwnership function of contract A with address of contract B as args. 5th, call changeAOwner function of contract B to handle ownership of contract A.

Huowuge
  • 41
  • 5