Here is the first pattern to upgrade a smart contract via a middle registry contract. Which returns the new version contract. Below is the structure of Registry Upgradeable Smart Contract Pattern.
Onwer.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract ownerContract{
address owner;
event OWNERSHIPCHANGE(address indexed previousOwner,address indexed NewOwner);
constructor(){
owner = msg.sender;
emit OWNERSHIPCHANGE(address(0),owner);
}
modifier onlyOWNER(){
require(msg.sender == owner,"only Owner Allowed");
_;
}
}
registry.sol this contract act as a middle man to get the address of upgraded of latest backend contract.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import './owner.sol';
contract registry is ownerContract{
uint latestVersion = 0;
mapping(uint=>address) versionHistory;
constructor(){
}
function updateVersion(address newUpdatedContract)public onlyOWNER{
latestVersion++;
versionHistory[latestVersion] = newUpdatedContract;
}
function getAddressofLatestContract()public view returns(address){
return versionHistory[latestVersion];
}
function getContractByVersionNumber(uint version)public view returns(address){
return versionHistory[version];
}
}
BackendContract.sol this is the main contract to perform functionality.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract ABC{
uint z;
constructor(){}
function add(uint x, uint y)public {
z = x + y;
}
function getValue()public view returns(uint){
return z;
}
}
Limitations:
- Storage issue.