For OwnableUpgradeable is it mandatory to call __Ownable_init?

For OwnableUpgradeable is it mandatory to call __Ownable_init ? If I'm the deployer do I still need to initialize with __Ownable_init? Or can I completely ignore it?

:1234: Code to reproduce

contract contractA is
    OwnableUpgradeable,
    PausableUpgradeable
{
       __Pausable_init();
       __Ownable_init(msg.sender); // Is this a must?
}

:computer: Environment

Yes, you should call __Ownable_init from your initializer function, and ensure your initializer function is called when deploying the proxy. Otherwise, the owner won't be set.

1 Like

According to OwnableUpgradeable contract:

 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.

So why need to call the __Ownable_init function? Since you the deployer will be the owner of the deployed contract?

What happens if you did not call __Ownable_init function? Can I input _transferOwnership(_factory); in the initializer function and call it to transfer ownership??

@eeshenggoh What you described is from OpenZeppelin Contracts 4.x, where the owner is set to msg.sender in the initializer. See https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/v4.9.6/contracts/access/OwnableUpgradeable.sol#L29. Whereas in version 5.x, you need to pass in the initialOwner parameter.

But either way, the recommendation is to call the __Ownable_init function, otherwise the storage is not set.

Can I input _transferOwnership(_factory); in the initializer function and call it to transfer ownership??

Yes, you can see in the above OwnableUpgradeable code that it just calls _transferOwnership, so you could do that in your initializer if needed.