Using proxy and Ownable implementation

I'm trying to understand how to create an "ownable" implementation that is used with a proxy.
the "Ownable" pattern assign the initial owner to msg.sender in the constructor, and later transferOwnership() validates the call is from the current owner.

However, when initializing a proxy clone using an "implementation" contract, its constructor is not called: we use the "Initializer" pattern, where we have a plain method to act as a "constructor" to initialize the object.

Now from within this initializer method, we can't use transferOwnership(): the current owner is "address(0)", and can't be changed.

What we need is a mechanism to allow us force the owner (but only from the within the initializer function)
As far as I understand, this can't be done by overriding either Ownable or Initializable template contracts, since both Ownable._setOwner() and Initializable._initializing are private . It requires (at a minimum) creating a copy of Ownable with non-private _setOwner

1 Like

Hi @Dror_Tirosh, in your implementation's initializer, you can call OwnableUpgradeable's __Ownable_init() function, and from that, you can see it calls _transferOwnership(_msgSender());. Since the implementation is called through the proxy with delegatecall, that owner would be address that called the initializer function through the proxy.

1 Like

Thanks. I was unaware of the OwnableUpgradeable contract.