Merge branch 'master' into next-v5.0
This commit is contained in:
@ -13,15 +13,10 @@ function getVersion(path) {
|
||||
}
|
||||
}
|
||||
|
||||
for (const [file, template] of Object.entries({
|
||||
'utils/math/SafeCast.sol': './templates/SafeCast.js',
|
||||
'utils/structs/EnumerableSet.sol': './templates/EnumerableSet.js',
|
||||
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
|
||||
'utils/Checkpoints.sol': './templates/Checkpoints.js',
|
||||
})) {
|
||||
function generateFromTemplate(file, template, outputPrefix = '') {
|
||||
const script = path.relative(path.join(__dirname, '../..'), __filename);
|
||||
const input = path.join(path.dirname(script), template);
|
||||
const output = `./contracts/${file}`;
|
||||
const output = path.join(outputPrefix, file);
|
||||
const version = getVersion(output);
|
||||
const content = format(
|
||||
'// SPDX-License-Identifier: MIT',
|
||||
@ -34,3 +29,21 @@ for (const [file, template] of Object.entries({
|
||||
fs.writeFileSync(output, content);
|
||||
cp.execFileSync('prettier', ['--write', output]);
|
||||
}
|
||||
|
||||
// Contracts
|
||||
for (const [file, template] of Object.entries({
|
||||
'utils/math/SafeCast.sol': './templates/SafeCast.js',
|
||||
'utils/structs/EnumerableSet.sol': './templates/EnumerableSet.js',
|
||||
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
|
||||
'utils/Checkpoints.sol': './templates/Checkpoints.js',
|
||||
'utils/StorageSlot.sol': './templates/StorageSlot.js',
|
||||
})) {
|
||||
generateFromTemplate(file, template, './contracts/');
|
||||
}
|
||||
|
||||
// Tests
|
||||
for (const [file, template] of Object.entries({
|
||||
'utils/Checkpoints.t.sol': './templates/Checkpoints.t.js',
|
||||
})) {
|
||||
generateFromTemplate(file, template, './test/');
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
const format = require('../format-lines');
|
||||
const { OPTS, LEGACY_OPTS } = require('./Checkpoints.opts.js');
|
||||
|
||||
const VALUE_SIZES = [224, 160];
|
||||
|
||||
// TEMPLATE
|
||||
const header = `\
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
@ -46,7 +46,7 @@ function push(
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the value in the oldest checkpoint with key greater or equal than the search key, or zero if there is none.
|
||||
* @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if there is none.
|
||||
*/
|
||||
function lowerLookup(${opts.historyTypeName} storage self, ${opts.keyTypeName} key) internal view returns (${opts.valueTypeName}) {
|
||||
uint256 len = self.${opts.checkpointFieldName}.length;
|
||||
@ -55,13 +55,38 @@ function lowerLookup(${opts.historyTypeName} storage self, ${opts.keyTypeName} k
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the value in the most recent checkpoint with key lower or equal than the search key.
|
||||
* @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
|
||||
*/
|
||||
function upperLookup(${opts.historyTypeName} storage self, ${opts.keyTypeName} key) internal view returns (${opts.valueTypeName}) {
|
||||
uint256 len = self.${opts.checkpointFieldName}.length;
|
||||
uint256 pos = _upperBinaryLookup(self.${opts.checkpointFieldName}, key, 0, len);
|
||||
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
|
||||
*
|
||||
* NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high keys).
|
||||
*/
|
||||
function upperLookupRecent(${opts.historyTypeName} storage self, ${opts.keyTypeName} key) internal view returns (${opts.valueTypeName}) {
|
||||
uint256 len = self.${opts.checkpointFieldName}.length;
|
||||
|
||||
uint256 low = 0;
|
||||
uint256 high = len;
|
||||
|
||||
if (len > 5) {
|
||||
uint256 mid = len - Math.sqrt(len);
|
||||
if (key < _unsafeAccess(self.${opts.checkpointFieldName}, mid)._key) {
|
||||
high = mid;
|
||||
} else {
|
||||
low = mid + 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint256 pos = _upperBinaryLookup(self.${opts.checkpointFieldName}, key, low, high);
|
||||
|
||||
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
|
||||
}
|
||||
`;
|
||||
|
||||
const legacyOperations = opts => `\
|
||||
@ -108,13 +133,6 @@ function getAtProbablyRecentBlock(${opts.historyTypeName} storage self, uint256
|
||||
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns checkpoint at given position.
|
||||
*/
|
||||
function getAtPosition(History storage self, uint32 pos) internal view returns (Checkpoint memory) {
|
||||
return self._checkpoints[pos];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Pushes a value onto a History so that it is stored as the checkpoint for the current block.
|
||||
*
|
||||
@ -177,6 +195,13 @@ function length(${opts.historyTypeName} storage self) internal view returns (uin
|
||||
return self.${opts.checkpointFieldName}.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns checkpoint at given position.
|
||||
*/
|
||||
function at(${opts.historyTypeName} storage self, uint32 pos) internal view returns (${opts.checkpointTypeName} memory) {
|
||||
return self.${opts.checkpointFieldName}[pos];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Pushes a (\`key\`, \`value\`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
|
||||
* or by updating the last one.
|
||||
@ -209,7 +234,7 @@ function _insert(
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Return the index of the oldest checkpoint whose key is greater than the search key, or \`high\` if there is none.
|
||||
* @dev Return the index of the last (most recent) checkpoint with key lower or equal than the search key, or \`high\` if there is none.
|
||||
* \`low\` and \`high\` define a section where to do the search, with inclusive \`low\` and exclusive \`high\`.
|
||||
*
|
||||
* WARNING: \`high\` should not be greater than the array's length.
|
||||
@ -232,7 +257,7 @@ function _upperBinaryLookup(
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or \`high\` if there is none.
|
||||
* @dev Return the index of the first (oldest) checkpoint with key is greater or equal than the search key, or \`high\` if there is none.
|
||||
* \`low\` and \`high\` define a section where to do the search, with inclusive \`low\` and exclusive \`high\`.
|
||||
*
|
||||
* WARNING: \`high\` should not be greater than the array's length.
|
||||
@ -270,26 +295,6 @@ function _unsafeAccess(${opts.checkpointTypeName}[] storage self, uint256 pos)
|
||||
`;
|
||||
/* eslint-enable max-len */
|
||||
|
||||
// OPTIONS
|
||||
const defaultOpts = size => ({
|
||||
historyTypeName: `Trace${size}`,
|
||||
checkpointTypeName: `Checkpoint${size}`,
|
||||
checkpointFieldName: '_checkpoints',
|
||||
keyTypeName: `uint${256 - size}`,
|
||||
keyFieldName: '_key',
|
||||
valueTypeName: `uint${size}`,
|
||||
valueFieldName: '_value',
|
||||
});
|
||||
|
||||
const OPTS = VALUE_SIZES.map(size => defaultOpts(size));
|
||||
|
||||
const LEGACY_OPTS = {
|
||||
...defaultOpts(224),
|
||||
historyTypeName: 'History',
|
||||
checkpointTypeName: 'Checkpoint',
|
||||
keyFieldName: '_blockNumber',
|
||||
};
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header.trimEnd(),
|
||||
|
||||
22
scripts/generate/templates/Checkpoints.opts.js
Normal file
22
scripts/generate/templates/Checkpoints.opts.js
Normal file
@ -0,0 +1,22 @@
|
||||
// OPTIONS
|
||||
const VALUE_SIZES = [224, 160];
|
||||
|
||||
const defaultOpts = size => ({
|
||||
historyTypeName: `Trace${size}`,
|
||||
checkpointTypeName: `Checkpoint${size}`,
|
||||
checkpointFieldName: '_checkpoints',
|
||||
keyTypeName: `uint${256 - size}`,
|
||||
keyFieldName: '_key',
|
||||
valueTypeName: `uint${size}`,
|
||||
valueFieldName: '_value',
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
OPTS: VALUE_SIZES.map(size => defaultOpts(size)),
|
||||
LEGACY_OPTS: {
|
||||
...defaultOpts(224),
|
||||
historyTypeName: 'History',
|
||||
checkpointTypeName: 'Checkpoint',
|
||||
keyFieldName: '_blockNumber',
|
||||
},
|
||||
};
|
||||
256
scripts/generate/templates/Checkpoints.t.js
Normal file
256
scripts/generate/templates/Checkpoints.t.js
Normal file
@ -0,0 +1,256 @@
|
||||
const format = require('../format-lines');
|
||||
const { capitalize } = require('../../helpers');
|
||||
const { OPTS, LEGACY_OPTS } = require('./Checkpoints.opts.js');
|
||||
|
||||
// TEMPLATE
|
||||
const header = `\
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "forge-std/Test.sol";
|
||||
import "../../contracts/utils/Checkpoints.sol";
|
||||
import "../../contracts/utils/math/SafeCast.sol";
|
||||
`;
|
||||
|
||||
/* eslint-disable max-len */
|
||||
const common = opts => `\
|
||||
using Checkpoints for Checkpoints.${opts.historyTypeName};
|
||||
|
||||
// Maximum gap between keys used during the fuzzing tests: the \`_prepareKeys\` function with make sure that
|
||||
// key#n+1 is in the [key#n, key#n + _KEY_MAX_GAP] range.
|
||||
uint8 internal constant _KEY_MAX_GAP = 64;
|
||||
|
||||
Checkpoints.${opts.historyTypeName} internal _ckpts;
|
||||
|
||||
// helpers
|
||||
function _bound${capitalize(opts.keyTypeName)}(
|
||||
${opts.keyTypeName} x,
|
||||
${opts.keyTypeName} min,
|
||||
${opts.keyTypeName} max
|
||||
) internal view returns (${opts.keyTypeName}) {
|
||||
return SafeCast.to${capitalize(opts.keyTypeName)}(bound(uint256(x), uint256(min), uint256(max)));
|
||||
}
|
||||
|
||||
function _prepareKeys(
|
||||
${opts.keyTypeName}[] memory keys,
|
||||
${opts.keyTypeName} maxSpread
|
||||
) internal view {
|
||||
${opts.keyTypeName} lastKey = 0;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
${opts.keyTypeName} key = _bound${capitalize(opts.keyTypeName)}(keys[i], lastKey, lastKey + maxSpread);
|
||||
keys[i] = key;
|
||||
lastKey = key;
|
||||
}
|
||||
}
|
||||
|
||||
function _assertLatestCheckpoint(
|
||||
bool exist,
|
||||
${opts.keyTypeName} key,
|
||||
${opts.valueTypeName} value
|
||||
) internal {
|
||||
(bool _exist, ${opts.keyTypeName} _key, ${opts.valueTypeName} _value) = _ckpts.latestCheckpoint();
|
||||
assertEq(_exist, exist);
|
||||
assertEq(_key, key);
|
||||
assertEq(_value, value);
|
||||
}
|
||||
`;
|
||||
|
||||
const testTrace = opts => `\
|
||||
// tests
|
||||
function testPush(
|
||||
${opts.keyTypeName}[] memory keys,
|
||||
${opts.valueTypeName}[] memory values,
|
||||
${opts.keyTypeName} pastKey
|
||||
) public {
|
||||
vm.assume(values.length > 0 && values.length <= keys.length);
|
||||
_prepareKeys(keys, _KEY_MAX_GAP);
|
||||
|
||||
// initial state
|
||||
assertEq(_ckpts.length(), 0);
|
||||
assertEq(_ckpts.latest(), 0);
|
||||
_assertLatestCheckpoint(false, 0, 0);
|
||||
|
||||
uint256 duplicates = 0;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
${opts.keyTypeName} key = keys[i];
|
||||
${opts.valueTypeName} value = values[i % values.length];
|
||||
if (i > 0 && key == keys[i-1]) ++duplicates;
|
||||
|
||||
// push
|
||||
_ckpts.push(key, value);
|
||||
|
||||
// check length & latest
|
||||
assertEq(_ckpts.length(), i + 1 - duplicates);
|
||||
assertEq(_ckpts.latest(), value);
|
||||
_assertLatestCheckpoint(true, key, value);
|
||||
}
|
||||
|
||||
if (keys.length > 0) {
|
||||
${opts.keyTypeName} lastKey = keys[keys.length - 1];
|
||||
if (lastKey > 0) {
|
||||
pastKey = _bound${capitalize(opts.keyTypeName)}(pastKey, 0, lastKey - 1);
|
||||
|
||||
vm.expectRevert();
|
||||
this.push(pastKey, values[keys.length % values.length]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// used to test reverts
|
||||
function push(${opts.keyTypeName} key, ${opts.valueTypeName} value) external {
|
||||
_ckpts.push(key, value);
|
||||
}
|
||||
|
||||
function testLookup(
|
||||
${opts.keyTypeName}[] memory keys,
|
||||
${opts.valueTypeName}[] memory values,
|
||||
${opts.keyTypeName} lookup
|
||||
) public {
|
||||
vm.assume(values.length > 0 && values.length <= keys.length);
|
||||
_prepareKeys(keys, _KEY_MAX_GAP);
|
||||
|
||||
${opts.keyTypeName} lastKey = keys.length == 0 ? 0 : keys[keys.length - 1];
|
||||
lookup = _bound${capitalize(opts.keyTypeName)}(lookup, 0, lastKey + _KEY_MAX_GAP);
|
||||
|
||||
${opts.valueTypeName} upper = 0;
|
||||
${opts.valueTypeName} lower = 0;
|
||||
${opts.keyTypeName} lowerKey = type(${opts.keyTypeName}).max;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
${opts.keyTypeName} key = keys[i];
|
||||
${opts.valueTypeName} value = values[i % values.length];
|
||||
|
||||
// push
|
||||
_ckpts.push(key, value);
|
||||
|
||||
// track expected result of lookups
|
||||
if (key <= lookup) {
|
||||
upper = value;
|
||||
}
|
||||
// find the first key that is not smaller than the lookup key
|
||||
if (key >= lookup && (i == 0 || keys[i-1] < lookup)) {
|
||||
lowerKey = key;
|
||||
}
|
||||
if (key == lowerKey) {
|
||||
lower = value;
|
||||
}
|
||||
}
|
||||
|
||||
// check lookup
|
||||
assertEq(_ckpts.lowerLookup(lookup), lower);
|
||||
assertEq(_ckpts.upperLookup(lookup), upper);
|
||||
assertEq(_ckpts.upperLookupRecent(lookup), upper);
|
||||
}
|
||||
`;
|
||||
|
||||
const testHistory = opts => `\
|
||||
// tests
|
||||
function testPush(
|
||||
${opts.keyTypeName}[] memory keys,
|
||||
${opts.valueTypeName}[] memory values,
|
||||
${opts.keyTypeName} pastKey
|
||||
) public {
|
||||
vm.assume(values.length > 0 && values.length <= keys.length);
|
||||
_prepareKeys(keys, _KEY_MAX_GAP);
|
||||
|
||||
// initial state
|
||||
assertEq(_ckpts.length(), 0);
|
||||
assertEq(_ckpts.latest(), 0);
|
||||
_assertLatestCheckpoint(false, 0, 0);
|
||||
|
||||
uint256 duplicates = 0;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
${opts.keyTypeName} key = keys[i];
|
||||
${opts.valueTypeName} value = values[i % values.length];
|
||||
if (i > 0 && key == keys[i - 1]) ++duplicates;
|
||||
|
||||
// push
|
||||
vm.roll(key);
|
||||
_ckpts.push(value);
|
||||
|
||||
// check length & latest
|
||||
assertEq(_ckpts.length(), i + 1 - duplicates);
|
||||
assertEq(_ckpts.latest(), value);
|
||||
_assertLatestCheckpoint(true, key, value);
|
||||
}
|
||||
|
||||
// Can't push any key in the past
|
||||
if (keys.length > 0) {
|
||||
${opts.keyTypeName} lastKey = keys[keys.length - 1];
|
||||
if (lastKey > 0) {
|
||||
pastKey = _bound${capitalize(opts.keyTypeName)}(pastKey, 0, lastKey - 1);
|
||||
|
||||
vm.roll(pastKey);
|
||||
vm.expectRevert();
|
||||
this.push(values[keys.length % values.length]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// used to test reverts
|
||||
function push(${opts.valueTypeName} value) external {
|
||||
_ckpts.push(value);
|
||||
}
|
||||
|
||||
function testLookup(
|
||||
${opts.keyTypeName}[] memory keys,
|
||||
${opts.valueTypeName}[] memory values,
|
||||
${opts.keyTypeName} lookup
|
||||
) public {
|
||||
vm.assume(keys.length > 0);
|
||||
vm.assume(values.length > 0 && values.length <= keys.length);
|
||||
_prepareKeys(keys, _KEY_MAX_GAP);
|
||||
|
||||
${opts.keyTypeName} lastKey = keys[keys.length - 1];
|
||||
vm.assume(lastKey > 0);
|
||||
lookup = _bound${capitalize(opts.keyTypeName)}(lookup, 0, lastKey - 1);
|
||||
|
||||
${opts.valueTypeName} upper = 0;
|
||||
for (uint256 i = 0; i < keys.length; ++i) {
|
||||
${opts.keyTypeName} key = keys[i];
|
||||
${opts.valueTypeName} value = values[i % values.length];
|
||||
|
||||
// push
|
||||
vm.roll(key);
|
||||
_ckpts.push(value);
|
||||
|
||||
// track expected result of lookups
|
||||
if (key <= lookup) {
|
||||
upper = value;
|
||||
}
|
||||
}
|
||||
|
||||
// check lookup
|
||||
assertEq(_ckpts.getAtBlock(lookup), upper);
|
||||
assertEq(_ckpts.getAtProbablyRecentBlock(lookup), upper);
|
||||
|
||||
vm.expectRevert(); this.getAtBlock(lastKey);
|
||||
vm.expectRevert(); this.getAtBlock(lastKey + 1);
|
||||
vm.expectRevert(); this.getAtProbablyRecentBlock(lastKey);
|
||||
vm.expectRevert(); this.getAtProbablyRecentBlock(lastKey + 1);
|
||||
}
|
||||
|
||||
// used to test reverts
|
||||
function getAtBlock(${opts.keyTypeName} key) external view {
|
||||
_ckpts.getAtBlock(key);
|
||||
}
|
||||
|
||||
// used to test reverts
|
||||
function getAtProbablyRecentBlock(${opts.keyTypeName} key) external view {
|
||||
_ckpts.getAtProbablyRecentBlock(key);
|
||||
}
|
||||
`;
|
||||
/* eslint-enable max-len */
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header,
|
||||
// HISTORY
|
||||
`contract Checkpoints${LEGACY_OPTS.historyTypeName}Test is Test {`,
|
||||
[common(LEGACY_OPTS), testHistory(LEGACY_OPTS)],
|
||||
'}',
|
||||
// TRACEXXX
|
||||
...OPTS.flatMap(opts => [
|
||||
`contract Checkpoints${opts.historyTypeName}Test is Test {`,
|
||||
[common(opts), testTrace(opts)],
|
||||
'}',
|
||||
]),
|
||||
);
|
||||
@ -25,7 +25,7 @@ import "./EnumerableSet.sol";
|
||||
* (O(1)).
|
||||
* - Entries are enumerated in O(n). No guarantees are made on the ordering.
|
||||
*
|
||||
* \`\`\`
|
||||
* \`\`\`solidity
|
||||
* contract Example {
|
||||
* // Add the library methods
|
||||
* using EnumerableMap for EnumerableMap.UintToAddressMap;
|
||||
|
||||
@ -22,7 +22,7 @@ pragma solidity ^0.8.0;
|
||||
* (O(1)).
|
||||
* - Elements are enumerated in O(n). No guarantees are made on the ordering.
|
||||
*
|
||||
* \`\`\`
|
||||
* \`\`\`solidity
|
||||
* contract Example {
|
||||
* // Add the library methods
|
||||
* using EnumerableSet for EnumerableSet.AddressSet;
|
||||
|
||||
87
scripts/generate/templates/StorageSlot.js
Normal file
87
scripts/generate/templates/StorageSlot.js
Normal file
@ -0,0 +1,87 @@
|
||||
const format = require('../format-lines');
|
||||
const { capitalize, unique } = require('../../helpers');
|
||||
|
||||
const TYPES = [
|
||||
{ type: 'address', isValueType: true, version: '4.1' },
|
||||
{ type: 'bool', isValueType: true, name: 'Boolean', version: '4.1' },
|
||||
{ type: 'bytes32', isValueType: true, version: '4.1' },
|
||||
{ type: 'uint256', isValueType: true, version: '4.1' },
|
||||
{ type: 'string', isValueType: false, version: '4.9' },
|
||||
{ type: 'bytes', isValueType: false, version: '4.9' },
|
||||
].map(type => Object.assign(type, { struct: (type.name ?? capitalize(type.type)) + 'Slot' }));
|
||||
|
||||
const VERSIONS = unique(TYPES.map(t => t.version)).map(
|
||||
version =>
|
||||
`_Available since v${version} for ${TYPES.filter(t => t.version == version)
|
||||
.map(t => `\`${t.type}\``)
|
||||
.join(', ')}._`,
|
||||
);
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
/**
|
||||
* @dev Library for reading and writing primitive types to specific storage slots.
|
||||
*
|
||||
* Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
|
||||
* This library helps with reading and writing to such slots without the need for inline assembly.
|
||||
*
|
||||
* The functions in this library return Slot structs that contain a \`value\` member that can be used to read or write.
|
||||
*
|
||||
* Example usage to set ERC1967 implementation slot:
|
||||
* \`\`\`solidity
|
||||
* contract ERC1967 {
|
||||
* bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
|
||||
*
|
||||
* function _getImplementation() internal view returns (address) {
|
||||
* return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
|
||||
* }
|
||||
*
|
||||
* function _setImplementation(address newImplementation) internal {
|
||||
* require(newImplementation.code.length > 0, "ERC1967: new implementation is not a contract");
|
||||
* StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
|
||||
* }
|
||||
* }
|
||||
* \`\`\`
|
||||
*
|
||||
${VERSIONS.map(s => ` * ${s}`).join('\n')}
|
||||
*/
|
||||
`;
|
||||
|
||||
const struct = type => `\
|
||||
struct ${type.struct} {
|
||||
${type.type} value;
|
||||
}
|
||||
`;
|
||||
|
||||
const get = type => `\
|
||||
/**
|
||||
* @dev Returns an \`${type.struct}\` with member \`value\` located at \`slot\`.
|
||||
*/
|
||||
function get${type.struct}(bytes32 slot) internal pure returns (${type.struct} storage r) {
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {
|
||||
r.slot := slot
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const getStorage = type => `\
|
||||
/**
|
||||
* @dev Returns an \`${type.struct}\` representation of the ${type.type} storage pointer \`store\`.
|
||||
*/
|
||||
function get${type.struct}(${type.type} storage store) internal pure returns (${type.struct} storage r) {
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {
|
||||
r.slot := store.slot
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header.trimEnd(),
|
||||
'library StorageSlot {',
|
||||
[...TYPES.map(struct), ...TYPES.flatMap(type => [get(type), type.isValueType ? '' : getStorage(type)])],
|
||||
'}',
|
||||
);
|
||||
Reference in New Issue
Block a user