Storage layout upgrade with `hardhat-upgrades`

Hi there,
I so similar questions on the forum, but did not find a solution yet.

I have to upgrade storage layout in my contract(which is already deployed on mainnet)

Current layout

uint256[50] private __gap;

New layout

address private _forwarder;
uint256[49] private __gap;


await upgrades.upgradeProxy(contract.address, ContractV2)


Error: New storage layout is incompatible

contracts/metatx/ERC2771Context.sol:21: Inserted `_forwarder`
  > New variables should be placed after all existing inherited variables

contracts/metatx/ERC2771Context.sol:85: Upgraded `__gap` to an incompatible type
  - Bad array resize from 50 to 49
    Size cannot decrease


  • "@openzeppelin/contracts-upgradeable": "^4.1.0",
  • "@openzeppelin/hardhat-upgrades": "^1.8.2",

As far as I understand hardhat-upgrades does not support it yet.
Is there a way to do this?

Hi @aquiladev. I'm sorry this is not natively supported by the plugin yet. I also don't want to rush to implement this feature for you because it's sensitive and would want to review it and test it thoroughly.

For your particular use case, I would recommend manually modifying the network file under .openzeppelin to include this variable you want to add, as if it had been included in your previous version.

I realize this is a little tricky, if you can share a repository link I can help you figure out the exact change you should make.

I think I've got your point, thanks