erc1967.js 858 B

123456789101112131415161718192021222324252627282930
  1. const ImplementationLabel = 'eip1967.proxy.implementation';
  2. const AdminLabel = 'eip1967.proxy.admin';
  3. const BeaconLabel = 'eip1967.proxy.beacon';
  4. function labelToSlot(label) {
  5. return '0x' + web3.utils.toBN(web3.utils.keccak256(label)).subn(1).toString(16);
  6. }
  7. function getSlot(address, slot) {
  8. return web3.eth.getStorageAt(
  9. web3.utils.isAddress(address) ? address : address.address,
  10. web3.utils.isHex(slot) ? slot : labelToSlot(slot),
  11. );
  12. }
  13. async function getAddressInSlot(address, slot) {
  14. const slotValue = await getSlot(address, slot);
  15. return web3.utils.toChecksumAddress(slotValue.substr(-40));
  16. }
  17. module.exports = {
  18. ImplementationLabel,
  19. AdminLabel,
  20. BeaconLabel,
  21. ImplementationSlot: labelToSlot(ImplementationLabel),
  22. AdminSlot: labelToSlot(AdminLabel),
  23. BeaconSlot: labelToSlot(BeaconLabel),
  24. getSlot,
  25. getAddressInSlot,
  26. };