Ownable’s constructor unexecuted when inheriting.
Question asked by elishadrion on GitHub: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2084
Environment
oz : 2.6.0
Solidity compiler : 0.6.2
web3 : 1.2.6
Details
I have a contract inheriting Ownable
. Once the contract is deployed, the internal constructor of Ownable
isn’t executed, leaving the owner
attribute “empty”.
When the code is tested on Remix, it returns the correct owner address.
When the code is deployed programatically with a JS script using web3, it’s correct too.
Code to reproduce bug
The contract :
contract Test is Ownable {}
The commands :
npx oz call
? Pick a network development
? Pick an instance Test at 0x5475e9eE4106d5D1A4B2F0576bb3A7583A65Ff68
? Select which function owner()
✓ Method 'owner()' returned: 0x0000000000000000000000000000000000000000
0x0000000000000000000000000000000000000000
Here’s the web3 script too :
const Web3 = require('web3');
const { setupLoader } = require('@openzeppelin/contract-loader');
async function main() {
const web3 = new Web3('http://localhost:7545');
const loader = setupLoader({ provider: web3 }).web3;
const accounts = await web3.eth.getAccounts();
const default_address = accounts[0];
const Test = loader.fromArtifact('Test');
const test = await Test.deploy().send({from:default_address, gas: 1500000,});
const owner = await test.methods.owner().call();
console.log(owner == default_address);
}
main();
I assume that you deployed using OpenZeppelin CLI.
OpenZeppelin CLI currently supports deploying upgradeable contracts and minimal proxies. Though regular deploys are part of OpenZeppelin CLI 2.8 which is coming soon, see the roadmap for details: https://github.com/OpenZeppelin/openzeppelin-sdk/issues/1424
Upgradeable contracts don’t have constructors. So we need to initialize Ownable to set the owner. See the documentation on Initialization
:
https://docs.openzeppelin.com/learn/upgrading-smart-contracts#initialization
Please see an example contract, with deploying and interacting using the OpenZeppelin CLI below:
MyContract.sol
Inherifts from Initializable
and has an initialize
function.
pragma solidity ^0.5.0;
import "@openzeppelin/contracts-ethereum-package/contracts/ownership/Ownable.sol";
import "@openzeppelin/upgrades/contracts/Initializable.sol";
contract MyContract is Initializable, Ownable {
function initialize(address owner) public initializer {
Ownable.initialize(owner);
}
}
Get accounts
$ npx oz accounts
? Pick a network development
Accounts for dev-1581567174507:
Default: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
All:
- 0: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
- 1: 0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0
...
Deploy the contract
$ npx oz create
Nothing to compile, all contracts are up to date.
? Pick a contract to instantiate MyContract
? Pick a network development
✓ Deploying @openzeppelin/contracts-ethereum-package dependency to network dev-1581567174507
✓ Contract MyContract deployed
All implementations have been deployed
? Call a function to initialize the instance after creating it? Yes
? Select which function * initialize(owner: address)
? owner: address: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
✓ Setting everything up to create contract instances
✓ Instance created at 0xA57B8a5584442B467b4689F1144D269d096A3daF
0xA57B8a5584442B467b4689F1144D269d096A3daF
Check the owner
$ npx oz call
? Pick a network development
? Pick an instance MyContract at 0xA57B8a5584442B467b4689F1144D269d096A3daF
? Select which function owner()
✓ Method 'owner()' returned: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
Hi @elishadrion,
Just checking if the above solved your question?
Hi,
After your answer on github, I went looking here and found out the fix, thank you.
1 Like
Hi @elishadrion,
Welcome to the community
Feel free to ask all the questions that you need.
Hi @elishadrion,
The CLI now supports deploying regular (non-upgradeable) contracts in the release candidate of OpenZeppelin CLI 2.8
Would appreciate if you could give the release candidate a try and let us know what you think!