Adding Variables To Upgradable Contracts After Mapping Variable

Hi, i have a UUPSUpgradable contract and adding a field after the mapping:

contract V1 {
 mapping(uint256 => LoanInfo) public details;
}
contract V2 {
 mapping(uint256 => LoanInfo) public details;
 uint256 premium;
}

I have roughly 12 other variable before the mapping, but would adding a variable to the end ever cause any issues with the mapping?

Hi, welcome to community! :wave:

Yes, to preserve the original layout of the storage variables, when introducing new variables, ensure they are added at the end of the contract. This practice helps uphold the storage layout of existing variables and mitigates conflicts. Additionally, consider utilizing OpenZeppelin plugins like Hardhat and Foundry to facilitate the management of upgradable contracts. These tools offer checks for storage layout compatibility errors. For more details, refer to the upgrade-proxy documentation.

1 Like