Add slot derivation library (#4975)

This commit is contained in:
Hadrien Croubois
2024-03-27 22:17:46 +01:00
committed by GitHub
parent 5e3ba29b08
commit cb2aaaa04a
18 changed files with 777 additions and 63 deletions

View File

@ -4,12 +4,14 @@ const generators = {
address: () => ethers.Wallet.createRandom().address,
bytes32: () => ethers.hexlify(ethers.randomBytes(32)),
uint256: () => ethers.toBigInt(ethers.randomBytes(32)),
int256: () => ethers.toBigInt(ethers.randomBytes(32)) + ethers.MinInt256,
hexBytes: length => ethers.hexlify(ethers.randomBytes(length)),
};
generators.address.zero = ethers.ZeroAddress;
generators.bytes32.zero = ethers.ZeroHash;
generators.uint256.zero = 0n;
generators.int256.zero = 0n;
generators.hexBytes.zero = '0x';
module.exports = {

View File

@ -5,18 +5,18 @@ const ImplementationLabel = 'eip1967.proxy.implementation';
const AdminLabel = 'eip1967.proxy.admin';
const BeaconLabel = 'eip1967.proxy.beacon';
const erc1967slot = label => ethers.toBeHex(ethers.toBigInt(ethers.id(label)) - 1n);
const erc7201slot = label => ethers.toBeHex(ethers.toBigInt(ethers.keccak256(erc1967slot(label))) & ~0xffn);
const erc1967Slot = label => ethers.toBeHex(ethers.toBigInt(ethers.id(label)) - 1n);
const erc7201Slot = label => ethers.toBeHex(ethers.toBigInt(ethers.keccak256(erc1967Slot(label))) & ~0xffn);
const erc7201format = contractName => `openzeppelin.storage.${contractName}`;
const getSlot = (address, slot) =>
ethers.provider.getStorage(address, ethers.isBytesLike(slot) ? slot : erc1967slot(slot));
ethers.provider.getStorage(address, ethers.isBytesLike(slot) ? slot : erc1967Slot(slot));
const setSlot = (address, slot, value) =>
Promise.all([
ethers.isAddressable(address) ? address.getAddress() : Promise.resolve(address),
ethers.isAddressable(value) ? value.getAddress() : Promise.resolve(value),
]).then(([address, value]) => setStorageAt(address, ethers.isBytesLike(slot) ? slot : erc1967slot(slot), value));
]).then(([address, value]) => setStorageAt(address, ethers.isBytesLike(slot) ? slot : erc1967Slot(slot), value));
const getAddressInSlot = (address, slot) =>
getSlot(address, slot).then(slotValue => ethers.AbiCoder.defaultAbiCoder().decode(['address'], slotValue)[0]);
@ -25,7 +25,7 @@ const upgradeableSlot = (contractName, offset) => {
try {
// Try to get the artifact paths, will throw if it doesn't exist
artifacts._getArtifactPathSync(`${contractName}Upgradeable`);
return offset + ethers.toBigInt(erc7201slot(erc7201format(contractName)));
return offset + ethers.toBigInt(erc7201Slot(erc7201format(contractName)));
} catch (_) {
return offset;
}
@ -35,11 +35,11 @@ module.exports = {
ImplementationLabel,
AdminLabel,
BeaconLabel,
ImplementationSlot: erc1967slot(ImplementationLabel),
AdminSlot: erc1967slot(AdminLabel),
BeaconSlot: erc1967slot(BeaconLabel),
erc1967slot,
erc7201slot,
ImplementationSlot: erc1967Slot(ImplementationLabel),
AdminSlot: erc1967Slot(AdminLabel),
BeaconSlot: erc1967Slot(BeaconLabel),
erc1967Slot,
erc7201Slot,
erc7201format,
setSlot,
getSlot,