erc1967.js 674 B

123456789101112131415161718192021222324
  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. module.exports = {
  14. ImplementationLabel,
  15. AdminLabel,
  16. BeaconLabel,
  17. ImplementationSlot: labelToSlot(ImplementationLabel),
  18. AdminSlot: labelToSlot(AdminLabel),
  19. BeaconSlot: labelToSlot(BeaconLabel),
  20. getSlot,
  21. };