OpenZeppelin / contracts-wizard

Interactive smart contract generator based on OpenZeppelin Contracts.
https://wizard.openzeppelin.com
MIT License
245 stars 135 forks source link

Use namespaced storage for state variables when upgradeable #383

Open ericglau opened 1 week ago

ericglau commented 1 week ago

From https://wizard.openzeppelin.com/#erc721, enable "Mintable -> Auto Increment Ids" and "Upgradeability". This adds state variable uint256 private _nextTokenId;to the code, and this can be enhanced to use namespaced storage as a best practice for upgradeable contracts.

(This is currently the only state variable added by Wizard, but this should apply for any state variable(s) being added by Wizard when upgradeability is enabled.)

ericglau commented 1 week ago

We should consider what to use as the namespace id here. Since this namespace belongs in the user's contract rather than in a contract library, this id should be somewhat unique for each user and reflect something that they can add variables to. In that case, we might want to allow users to customize this with a prefix like myproject, which results in a namespace id like myproject.MyToken.

However, namespace id conflicts could occur if different users inherit each other's code that uses the same namespace id if they don't change the defaults. The Upgrades Plugins will detect this though, and I'm not sure how likely this conflict scenario would be in the first place.

Ideally, users would just see a namespace in their contract and then use https://github.com/OpenZeppelin/openzeppelin-vscode to edit the namespace id and hash to whatever they want :) So perhaps having a generic default is enough.