Files
openzeppelin-contracts/test/helpers/namespaced-storage.js
Ernesto García cf6ff90b6d Migrate AccessManager tests to ethers (#4710)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
2023-11-09 15:56:54 +00:00

29 lines
776 B
JavaScript

const { keccak256, id, toBeHex, MaxUint256 } = require('ethers');
const { artifacts } = require('hardhat');
function namespaceId(contractName) {
return `openzeppelin.storage.${contractName}`;
}
function namespaceLocation(value) {
const hashIdBN = BigInt(id(value)) - 1n; // keccak256(id) - 1
const mask = MaxUint256 - 0xffn; // ~0xff
return BigInt(keccak256(toBeHex(hashIdBN, 32))) & mask;
}
function namespaceSlot(contractName, offset) {
try {
// Try to get the artifact paths, will throw if it doesn't exist
artifacts._getArtifactPathSync(`${contractName}Upgradeable`);
return offset + namespaceLocation(namespaceId(contractName));
} catch (_) {
return offset;
}
}
module.exports = {
namespaceSlot,
namespaceLocation,
namespaceId,
};