1

This is my idea about an upgradable contract, i dont wanna use openzeppelin/hardhat-upgrades so i am sharing my understanding only based on openzeppelin-upgradable-contracts library. correct me i wrong:

There should be 2 contract,1. implementation and 2. proxy contract. implementation contract will hold all the main logic of my dapp where proxy contract will hold just function of changing implementation contract's address. First i will have to deploy the implementation contract.Now, proxy contract, should be inherited from UUPSUpgradeable, must have _authorizeUpgrade(address implementation). Then i will have to deploy the proxy contract. Now whenever i will have to change the code i change it and redeploy it with new code, then pass the implementation contract address to the authorizeUpgrade() to update the contract. After upgrading only the contract code will change, the state and record for every user will remain unchanged.

1 Answer 1

1

yes indeed, you describe exactly how it works:

  1. deploy implementation logic contract implemantationA

  2. deploy UUPSproxy (UUPSUpgradeable inheritance) and set implementation to implementation

  3. you interact with your UUPSProxy and update the state

  4. when new features have been coded, then deploy the new implementation code to implementationB

  5. set implementationB in the UUPSProxy

As this pattern uses the delegateCall() the state is always within the UUPSProxy contract and therefore maintained.

When working with this pattern we have to bear in mind that the structure/order of the state can't be changed to avoid storage collision, so if new variables have to be used, they have to be input at the end of the old state. If the contract inherits other contracts with variables, these variables will be stored before the ones you define in your contract.

Hope it helps!

4
  • Have 2 question regarding your ans.
    – Hacek
    Apr 30, 2023 at 9:44
  • 1. As u said if new variables have to be used, they have to be input at the end of the old state means i will have to declare new variable below the last variable is declared, right? As slot0, slot1,slot2 is used according to the variable declaration order. Am i right? 2. If the contract inherits other contracts with variables, these variables will be stored before the ones you define in your contract - here we dont need to do anything becaue variables of the contract which i am inheriting is automatically declared before my contract's variables. Am i right?
    – Hacek
    Apr 30, 2023 at 9:44
  • yes, completely right in 1 and 2. Only pay attention to inherited contracts that are imported from a third-party and ensure that don't change the storage layout. Because as you said, their varaibles are declared before your variables and if they add one, then all the storage will be wrong.
    – donoso.eth
    Apr 30, 2023 at 15:55
  • Thanks, this is my complete repo: github.com/itsabinashb/NFT-based-Upgradeable-game . It will be very helpful if u check it and tell me if i did something wrong. 🙂
    – Hacek
    Apr 30, 2023 at 21:36

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.