Whenever I do modification in a contract and deploy the contract again, the data in the existing contract is completely lost as the new contract is deployed in a new address.

For instance, I have a registration Contract. When I first Deployed I forgot to retrieve the Name of the user. So, i had to modify the contract and deploy it again. In this case i lost all the registration details of the users who had registered to my application.

Now my question is: Is there a method by which i can retrieve back all the data stored in my old contract to my new contract?


Sure, there are several ways to do this, all with their pros and contras:

  1. Use proxy contract. The idea is that your deploy two contracts: proxy contract and implementation contract. Users work with proxy contract and may not even know that implementation contract exists. Proxy contract delegates almost all requests to implementation contract via DELEGATECALL opcode, so implementation opcode is executed in proxy contract's scope, thus using proxy contract's storage. When you need to upgrade contract logic, you deploy new version of implementation contract and tell proxy to delegate requests to the new address. As long as both, old and new implementation contracts actually use proxy's storage, and proxy address didn't change, the data will not be lost. Another benefit is that from user's point of new contract address will not change as well. There is an implementation for this pattern in Open Zeppelin

  2. Use storage contract. The idea is that you deploy two contracts: controller contract and storage contract. Storage contract allows controller to read/write arbitrary storage slots of the storage contract. Controller contract does not store any business data in its own storage but instead calls storage contract for storing/retrieving the data. When you need to upgrade contract logic, you deploy new version of controller contract that use the same instance of storage contract, and then tells storage contract to accept requests from new controller (and stop accepting requests from the old one). As long as both, old and new controller contracts use the same storage contract, the data will not be lost.

  3. Wrap old contract into the new one. The idea is that you deploy one normal contract, and when you need to to upgrade it, you switch it to read-only mode and deploy a new version at a new address. This new version stores only the data that was added/changed after upgrade, and for all other data it just queries the old contract operating in read-only mode. This approach works very well for ERC-20 token contract that support transfer freezing.

Let me know if you need more details about some of these options.

  • Thank you, this might work. I will try and let you know. – Geethapriya G H Dec 20 '19 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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