Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __gap properties to allow base contract variable adding #33

Open
samuelmanzanera opened this issue Apr 23, 2024 · 0 comments
Open
Labels
feature New feature request

Comments

@samuelmanzanera
Copy link
Member

On Solidity we cannot add new variable to a parent class, if the child class have some custom properties, as the index of the variable must be consistent.

A known solution is to use __gap properties.

Storage gaps are a convention for reserving storage slots in a base contract, allowing future versions of that contract to use up those slots without affecting the storage layout of child contracts.

To create a storage gap, declare a fixed-size array in the base contract with an initial number of slots. This can be an array of uint256 so that each element reserves a 32 byte slot. Use the name __gap or a name starting with _gap for the array so that OpenZeppelin Upgrades will recognize the gap:

ontract Base {
    uint256 base1;
    uint256[49] __gap;
}

contract Child is Base {
    uint256 child;
}

with a new version reducing the __gap property

contract Base {
    uint256 base1;
    uint256 base2; // 32 bytes
    uint256[48] __gap;
}

or

contract Base {
    uint256 base1;
    uint128 base2a; // 16 bytes
    uint128 base2b; // 16 bytes - continues from the same slot as above
    uint256[48] __gap;
}
@samuelmanzanera samuelmanzanera added the feature New feature request label Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature request
Projects
None yet
Development

No branches or pull requests

1 participant