Hello team openzeppelin,
I hope everyone is doing great.
I had an issue while trying to upgrade my SM using UUPS pattern. It is not really an issue; more like it's just how it works. Wanted to share it here, hoping that there is maybe a way around this...
Here is the code of my upgradable smart contract,
It is a simple contract that keeps track of the type of the contract(ERC721 or ERC1155) and the address of the contract to interact with.
There will be only one address for each type.
The issue is basically, if I want to upgrade my contract, and in the upgraded contract I want to add more types in the enum, that wont be possible and compilation would fail.
If I declare a new enum (with a different name and all the new types) I will also need to declare new function signatures for my functions.
I most importantly wanted to keep same function signature of getContractByType fct but that seems to not be possible once I declare a new enum.
Thank you in advance for any feedbacks or any possible workarounds for this,
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
// Open Zeppelin libraries for controlling upgradability and access.
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "hardhat/console.sol";
contract MyRegistry is Initializable, UUPSUpgradeable, OwnableUpgradeable {
enum CollectionType {
ERC721,
ERC1155
}
mapping(CollectionType => address) private contractByType;
function initialize() public initializer {
///@dev as there is no constructor, we need to initialise the OwnableUpgradeable explicitly
__Ownable_init();
}
///@dev required by the OZ UUPS module
function _authorizeUpgrade(address) internal override onlyOwner {}
function addContract(
CollectionType _collectionType,
address _contractAddress
) public virtual onlyOwner {
require(
contractByType[_collectionType] == address(0),
"Contract is already set for this type"
);
contractByType[_collectionType] = _contractAddress;
}
function getContractByType(
CollectionType _type
) public view virtual returns (address) {
return contractByType[_type];
}
///@dev returns the contract version
function registryVersion() external pure virtual returns (uint256) {
return 1;
}
}