Add slot derivation library (#4975)
This commit is contained in:
@ -1,14 +1,5 @@
|
||||
const format = require('../format-lines');
|
||||
const { capitalize } = require('../../helpers');
|
||||
|
||||
const TYPES = [
|
||||
{ type: 'address', isValueType: true },
|
||||
{ type: 'bool', isValueType: true, name: 'Boolean' },
|
||||
{ type: 'bytes32', isValueType: true },
|
||||
{ type: 'uint256', isValueType: true },
|
||||
{ type: 'string', isValueType: false },
|
||||
{ type: 'bytes', isValueType: false },
|
||||
].map(type => Object.assign(type, { struct: (type.name ?? capitalize(type.type)) + 'Slot' }));
|
||||
const { TYPES } = require('./Slot.opts');
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.20;
|
||||
@ -24,6 +15,7 @@ pragma solidity ^0.8.20;
|
||||
* Example usage to set ERC-1967 implementation slot:
|
||||
* \`\`\`solidity
|
||||
* contract ERC1967 {
|
||||
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
|
||||
* bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
|
||||
*
|
||||
* function _getImplementation() internal view returns (address) {
|
||||
@ -36,20 +28,22 @@ pragma solidity ^0.8.20;
|
||||
* }
|
||||
* }
|
||||
* \`\`\`
|
||||
*
|
||||
* TIP: Consider using this library along with {SlotDerivation}.
|
||||
*/
|
||||
`;
|
||||
|
||||
const struct = type => `\
|
||||
struct ${type.struct} {
|
||||
${type.type} value;
|
||||
const struct = ({ type, name }) => `\
|
||||
struct ${name}Slot {
|
||||
${type} value;
|
||||
}
|
||||
`;
|
||||
|
||||
const get = type => `\
|
||||
const get = ({ name }) => `\
|
||||
/**
|
||||
* @dev Returns an \`${type.struct}\` with member \`value\` located at \`slot\`.
|
||||
* @dev Returns an \`${name}Slot\` with member \`value\` located at \`slot\`.
|
||||
*/
|
||||
function get${type.struct}(bytes32 slot) internal pure returns (${type.struct} storage r) {
|
||||
function get${name}Slot(bytes32 slot) internal pure returns (${name}Slot storage r) {
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {
|
||||
r.slot := slot
|
||||
@ -57,11 +51,11 @@ function get${type.struct}(bytes32 slot) internal pure returns (${type.struct} s
|
||||
}
|
||||
`;
|
||||
|
||||
const getStorage = type => `\
|
||||
const getStorage = ({ type, name }) => `\
|
||||
/**
|
||||
* @dev Returns an \`${type.struct}\` representation of the ${type.type} storage pointer \`store\`.
|
||||
* @dev Returns an \`${name}Slot\` representation of the ${type} storage pointer \`store\`.
|
||||
*/
|
||||
function get${type.struct}(${type.type} storage store) internal pure returns (${type.struct} storage r) {
|
||||
function get${name}Slot(${type} storage store) internal pure returns (${name}Slot storage r) {
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {
|
||||
r.slot := store.slot
|
||||
@ -73,6 +67,7 @@ function get${type.struct}(${type.type} storage store) internal pure returns (${
|
||||
module.exports = format(
|
||||
header.trimEnd(),
|
||||
'library StorageSlot {',
|
||||
[...TYPES.map(struct), ...TYPES.flatMap(type => [get(type), type.isValueType ? '' : getStorage(type)])],
|
||||
TYPES.map(type => struct(type)),
|
||||
TYPES.flatMap(type => [get(type), type.isValueType ? '' : getStorage(type)]),
|
||||
'}',
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user