How to change storage safely of upgradeable contract

Hi, I was writing an upgradeable contract using openzeppelin's upgrades contract.

Is it possible to change the storage of the upgrade implementation ?

I was thinking of adding new variables to the end. Will it work?

For example if my contract is something like this

contract TestContract is Initializable {
    address public DAO;

    address public Treasury;

    struct Protocol{
        mapping(address => bool) protocolAssets;
        address depositor;
        uint256[] whitelistGroup;
        address admin;
 
    }


    mapping(address => bool) assets;

    function something() public {
    dao = address(0)
}

Will adding a new variable after the assets mapping, for the new upgrade implementation, work?

yes, you should be fine if you only add new variables after the last variable of the old version, as described here https://docs.openzeppelin.com/upgrades-plugins/1.x/proxies#storage-collisions-between-implementation-versions and here https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#modifying-your-contracts

2 Likes

some patterns you might also want to look into are the gap variable and diamond storage: The better way to define the variable __gap in the proxy contract

2 Likes

Yes, this is the way to go.

1 Like