1

While making a upgradeable contract - i created a implementation contract named Game.sol which contains upgradeTo() and a proxy contract named Proxy.sol. Now I want to know if i wanna add new functionality like suppose i wanna add a function in the implementation contract, this:

function getGreetings() public pure returns(string memory){
        return "You are doing well!";
    }

To update the implementation contract with this function what steps should i take?? Do i need to do create a new contract with this function and make this contract inherited from Game.sol and deploy it, then call the upgradeTo() from Proxy.sol with the address? If not then what?? I am confused.

1 Answer 1

1

At the most basic level, you simply deploy a new contract and call the upgradeTo in proxy to change the address to be the new address.

However, because the contract storage is in the proxy, you have to be careful about storage clashes when switching contracts. The easiest way to avoid issues with storage is to use the original contract (Game.sol) and just add the required functionality to the end. Also if you add new storage variables, make sure to add them as the last variables in the contract.

There are some other minor nuances as well, but this will get you through most of the issues.

In reality, make sure to first fork the network where you're upgrading, perform the upgrade locally against the forked network and make sure everything works as expected. Only when you're sure, perform the upgrade to the real contract.

2
  • Do i need to inherit the new contract from old implementation contract?? Asking because if we don't do that then new address can't access the methods of old implementation contract.
    – Hacek
    Jun 6, 2023 at 19:09
  • if there are no contracts inheriting your contract, then the easiest is just to extend the old contract directly (so just take the old code and add new stuff) and deploy a new instance. or you can extend the old contract in sub-contract. Jun 7, 2023 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.