1

Say I use UUPS pattern and want to add ERC721 by inheriting it, is this possible?

i.e.

contract MyContractV1 is OwnableUpgradeable {

and for the next version

contract MyContractV2 is OwnableUpgradeable, ERC721Upgradeable {

1 Answer 1

2

It is possible, but normally it won't work as you change the storage layout.

The ERC721Upgradeable has some state (token symbol, name, owners, etc..).

When you inherit a contract, the state variables will reserve the storage slot before the state variables in the main contract, and then your state will not be in the same storage slot as before:

A practical example:

contract A {
uint256 public myNumber;
 
 function setNumber(uint256 _number) external {
  myNumber = _number;
   }
}

You assign the number 6 by calling setNumer(6), and if you query myNumber you will receive 6. This state variable is stored at the first storage slot;

Now you have a contract B that you will inherit and upgrade your uups proxy

contract B {
uint256 placeholder;
}

Now you inherit Contract B

contract A is B {
  uint256 myNumber;
function setNumber(uint256 _number) external {
  myNumber = _number;
   }
}

Now after the upgrade the first storage slot is the state variable in Contract B, placeholder and the second storage slot is assigned to the myNumber As the first slot has already the value 6, when querying the placeholder the result will be 6 and when querying myNumber the result will be 0. The state in the contract is wrong.

One alternative to preserve the storage is to include the ERC721 code into your main contract and the variable state definitions just after the variables previously defined.

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.