I have a question on the usage of contracts-upgradable. I moved my contracts to @openzeppelin/contracts-upgradeable@solc-0.7. I adjusted my import statements as well as moved my constructor code to initialize. I am having problem with the ownership of the contracts during deployment. Here is the general structure of one contract. There is no constructor so I did not add any initialize function using the Initialiazable.
contract ThisContract is OwnableUpgradeable {
OtherContract otherContract ;
// no constructor
// no initialize
....
function setOtherContract(OtherContract _otherContract) public onlyOwner {
otherContract = _otherContract;
}
...
}
After ThisContract is deployed I placed a console.log for its owner() in the deployment script. It shows zero-address. Therefore, thisContract.setOtherContract(otherContract) fails with ownership error as well.
How do you assign the initial ownership during deployment? Is it not automatically getting it as it is doing in the non-upgradable version? Also, once this is set properly the deploymentAccount could just call transferOwnership for the final owner. Am I correct?
By the way, although I am using upgradable contracts I am still using my normal deployment script for now. So, there is no proxy as of now? Can these upgradable contracts be deployed as such? Maybe this is my problem.
You need to call __Ownable_init() to initialize the owner in your initialize function.
When you deploy your contract you also need to call the initialize function. You can do this using the Upgrade Plugins.
If you were to deploy a contract with an initialize function without using the Upgrades Plugins (e.g. as a regular, non-upgradeable contract), you should ensure that you deploy and call initialize in the same transaction, as any account can call initialize.
I thought I understood it… Could you please shed some light for me? I am still getting zero-address for the owner of the contract. What am I missing? Please note that I’m deploying without the Upgrades Plugins as an intermediate step. [PS: using upgrades plugin worked fine. I would still appreciate an explanation if one has why the below is not working.]
I have the following contract:
import "@openzeppelin/contracts-upgradeable/proxy/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
contract Contract1 is Initializable, OwnableUpgradeable {
// storage
function initialize() public initializer {
__Ownable_init(); // here I tried with or without super
}
// other functions
}