Does state stay the same after upgrade?

I’m curious about upgradeable contracts. If I was to upgrade a contract that already had many states, such as staking states, once the contract is upgraded would the states of the contract also be the same?

Ex. Contract A has stake values for addresses x y and z on a variable mapping called currentStakesOf which maps the users address with the amount of coins staked. Contract A get’s upgraded, what happens to the stake values for x y and z

1 Like

I recommend you have a look at this doc: Proxy Upgrade Pattern.
All the state are saved in the proxy contract, implementation contract has the logic, so as long as you do not add too much variables and do not place new variables out of order, there is theoretically no conflict.

2 Likes

I appreciate the answer. So here’s my only issue. The contracts are already written, and have mapped states. I plan to relaunch my contracts, will i have to move my mapped states out of the contract or does openzeppelin implementation take care of all of this for me?

It seems in the “writing an upgradeable contract” it says exactly what you said that any new veriables must come after the old ones. So that is fine, there is no issue with mappings is basically my question, since the documentation doesn’t specificially mention mappings, i want to ensure mappings will be safe on upgrade.

1 Like

Hi @Dellybro,

If you have an existing contract that is not upgradeable, you cannot change it to be upgradeable and maintain state. You would need to migrate to a new contract instead.