Skip to content

Add a virtual pure function that can be overriden to customize the storage location #156

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions src/transform-namespaces.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ Generated by [AVA](https://avajs.dev).
string s1;␊
}␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C2")) - 1)) & ~bytes32(uint256(0xff))␊
bytes32 private constant C2StorageLocation = 0xf05a05e0e3d15983ea921cad031aaea3040e9d631039045748753d29c5d24800;␊
/// @dev Consider updating the struct's storage location annotation␊
/// See https://docs.openzeppelin.com/upgrades-plugins/writing-upgradeable#namespaced-storage-layout␊
function _C2StorageLocation() internal pure virtual returns (bytes32) {␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C2")) - 1)) & ~bytes32(uint256(0xff))␊
return 0xf05a05e0e3d15983ea921cad031aaea3040e9d631039045748753d29c5d24800;␊
}␊
function _getC2Storage() private pure returns (C2Storage storage $) {␊
bytes32 slot = _C2StorageLocation();␊
assembly {␊
$.slot := C2StorageLocation
$.slot := slot
}␊
}␊
Expand Down Expand Up @@ -75,12 +80,17 @@ Generated by [AVA](https://avajs.dev).
address x;␊
}␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C3")) - 1)) & ~bytes32(uint256(0xff))␊
bytes32 private constant C3StorageLocation = 0xaa7e0685867d809c517036b8f21e99d58bd04b1da2b202f167c355fdf82b4a00;␊
/// @dev Consider updating the struct's storage location annotation␊
/// See https://docs.openzeppelin.com/upgrades-plugins/writing-upgradeable#namespaced-storage-layout␊
function _C3StorageLocation() internal pure virtual returns (bytes32) {␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C3")) - 1)) & ~bytes32(uint256(0xff))␊
return 0xaa7e0685867d809c517036b8f21e99d58bd04b1da2b202f167c355fdf82b4a00;␊
}␊
function _getC3Storage() private pure returns (C3Storage storage $) {␊
bytes32 slot = _C3StorageLocation();␊
assembly {␊
$.slot := C3StorageLocation
$.slot := slot
}␊
}␊
}␊
Expand All @@ -91,12 +101,17 @@ Generated by [AVA](https://avajs.dev).
address x;␊
}␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C4")) - 1)) & ~bytes32(uint256(0xff))␊
bytes32 private constant C4StorageLocation = 0x536a56e760d844b098efcc16711808e0b18bad9e07c2e82c78312ab719318d00;␊
/// @dev Consider updating the struct's storage location annotation␊
/// See https://docs.openzeppelin.com/upgrades-plugins/writing-upgradeable#namespaced-storage-layout␊
function _C4StorageLocation() internal pure virtual returns (bytes32) {␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C4")) - 1)) & ~bytes32(uint256(0xff))␊
return 0x536a56e760d844b098efcc16711808e0b18bad9e07c2e82c78312ab719318d00;␊
}␊
function _getC4Storage() private pure returns (C4Storage storage $) {␊
bytes32 slot = _C4StorageLocation();␊
assembly {␊
$.slot := C4StorageLocation
$.slot := slot
}␊
}␊
function __C4_init() internal onlyInitializing {␊
Expand All @@ -123,12 +138,17 @@ Generated by [AVA](https://avajs.dev).
address x;␊
}␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C5")) - 1)) & ~bytes32(uint256(0xff))␊
bytes32 private constant C5StorageLocation = 0xd94dd1cf5c0ce3bfbbd2555b11ad43bf11eeff03081ca744441b0fb7c0a6ec00;␊
/// @dev Consider updating the struct's storage location annotation␊
/// See https://docs.openzeppelin.com/upgrades-plugins/writing-upgradeable#namespaced-storage-layout␊
function _C5StorageLocation() internal pure virtual returns (bytes32) {␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C5")) - 1)) & ~bytes32(uint256(0xff))␊
return 0xd94dd1cf5c0ce3bfbbd2555b11ad43bf11eeff03081ca744441b0fb7c0a6ec00;␊
}␊
function _getC5Storage() private pure returns (C5Storage storage $) {␊
bytes32 slot = _C5StorageLocation();␊
assembly {␊
$.slot := C5StorageLocation
$.slot := slot
}␊
}␊
}␊
Expand All @@ -154,12 +174,17 @@ Generated by [AVA](https://avajs.dev).
uint y;␊
}␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C7")) - 1)) & ~bytes32(uint256(0xff))␊
bytes32 private constant C7StorageLocation = 0x931371859ca5c5440d3850c7cf9c14adb9d1257b7ddd15562d561cd48871d300;␊
/// @dev Consider updating the struct's storage location annotation␊
/// See https://docs.openzeppelin.com/upgrades-plugins/writing-upgradeable#namespaced-storage-layout␊
function _C7StorageLocation() internal pure virtual returns (bytes32) {␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C7")) - 1)) & ~bytes32(uint256(0xff))␊
return 0x931371859ca5c5440d3850c7cf9c14adb9d1257b7ddd15562d561cd48871d300;␊
}␊
function _getC7Storage() private pure returns (C7Storage storage $) {␊
bytes32 slot = _C7StorageLocation();␊
assembly {␊
$.slot := C7StorageLocation
$.slot := slot
}␊
}␊
// a separate comment␊
Expand All @@ -172,12 +197,17 @@ Generated by [AVA](https://avajs.dev).
address y;␊
}␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C8")) - 1)) & ~bytes32(uint256(0xff))␊
bytes32 private constant C8StorageLocation = 0xb93ad5010b0e46eab6ce47e44cb42c85a263d4e8daf058e8a66e4f114144f200;␊
/// @dev Consider updating the struct's storage location annotation␊
/// See https://docs.openzeppelin.com/upgrades-plugins/writing-upgradeable#namespaced-storage-layout␊
function _C8StorageLocation() internal pure virtual returns (bytes32) {␊
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.C8")) - 1)) & ~bytes32(uint256(0xff))␊
return 0xb93ad5010b0e46eab6ce47e44cb42c85a263d4e8daf058e8a66e4f114144f200;␊
}␊
function _getC8Storage() private pure returns (C8Storage storage $) {␊
bytes32 slot = _C8StorageLocation();␊
assembly {␊
$.slot := C8StorageLocation
$.slot := slot
}␊
}␊
Expand Down
Binary file modified src/transform-namespaces.test.ts.snap
Binary file not shown.
12 changes: 9 additions & 3 deletions src/transformations/add-namespace-struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,17 @@ export function addNamespaceStruct(include?: (source: string) => boolean) {
...rest.split('\n'),
`}`,
``,
`// keccak256(abi.encode(uint256(keccak256("${id}")) - 1)) & ~bytes32(uint256(0xff))`,
`bytes32 private constant ${namespace}Location = ${erc7201Location(id)};`,
`/// @dev Consider updating the struct's storage location annotation`,
`/// See https://docs.openzeppelin.com/upgrades-plugins/writing-upgradeable#namespaced-storage-layout`,
`function _${namespace}Location() internal pure virtual returns (bytes32) {`,
[
`// keccak256(abi.encode(uint256(keccak256("${id}")) - 1)) & ~bytes32(uint256(0xff))`,
`return ${erc7201Location(id)};`,
],
`}`,
``,
`function _get${namespace}() private pure returns (${namespace} storage $) {`,
[`assembly {`, [`$.slot := ${namespace}Location`], `}`],
[`bytes32 slot = _${namespace}Location();`, `assembly {`, [`$.slot := slot`], `}`],
`}`,
]).trimEnd()
);
Expand Down