Hi @dportabella,
To use Ownable in an upgradeable contract we need to do the following:
Import OwnableUpgradeSafe as contracts in OpenZeppelin Contracts Ethereum Package have an UpgradeSafe
suffix to avoid confusion with their counterparts in OpenZeppelin Contracts.
Create an initialize
function to initialize the contract and use the initializer
modifier from Initializable
to ensure that it is only called once.
An upgradeable version of Box.sol (from: https://docs.openzeppelin.com/learn/developing-smart-contracts#importing_openzeppelin_contracts) using Ownable is shown below:
Box.sol
// contracts/Box.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
// Import Ownable from the OpenZeppelin Contracts library
import "@openzeppelin/contracts-ethereum-package/contracts/access/Ownable.sol";
import "@openzeppelin/contracts-ethereum-package/contracts/Initializable.sol";
// Make Box inherit from the Ownable contract
contract Box is Initializable, OwnableUpgradeSafe {
uint256 private value;
event ValueChanged(uint256 newValue);
function initialize() public initializer {
__Context_init_unchained();
__Ownable_init_unchained();
}
// The onlyOwner modifier restricts who can call the store function
function store(uint256 newValue) public onlyOwner {
value = newValue;
emit ValueChanged(newValue);
}
function retrieve() public view returns (uint256) {
return value;
}
}
I recommend looking at Role-Based Access Control for more finely controlled access control.
I assume you mean a version of OpenZeppelin Contracts that could be used with regular contracts and upgradeable contracts.
A single version would require contracts to have both constructors and initializers, increasing the complexity, along with the gas costs of deployment.
Please ask all the questions that you need.