access-manager.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const { time } = require('@openzeppelin/test-helpers');
  2. const { MAX_UINT64 } = require('./constants');
  3. const { namespaceSlot } = require('./namespaced-storage');
  4. const {
  5. time: { setNextBlockTimestamp },
  6. } = require('@nomicfoundation/hardhat-network-helpers');
  7. function buildBaseRoles() {
  8. const roles = {
  9. ADMIN: {
  10. id: web3.utils.toBN(0),
  11. },
  12. SOME_ADMIN: {
  13. id: web3.utils.toBN(17),
  14. },
  15. SOME_GUARDIAN: {
  16. id: web3.utils.toBN(35),
  17. },
  18. SOME: {
  19. id: web3.utils.toBN(42),
  20. },
  21. PUBLIC: {
  22. id: MAX_UINT64,
  23. },
  24. };
  25. // Names
  26. Object.entries(roles).forEach(([name, role]) => (role.name = name));
  27. // Defaults
  28. for (const role of Object.keys(roles)) {
  29. roles[role].admin = roles.ADMIN;
  30. roles[role].guardian = roles.ADMIN;
  31. }
  32. // Admins
  33. roles.SOME.admin = roles.SOME_ADMIN;
  34. // Guardians
  35. roles.SOME.guardian = roles.SOME_GUARDIAN;
  36. return roles;
  37. }
  38. const formatAccess = access => [access[0], access[1].toString()];
  39. const MINSETBACK = time.duration.days(5);
  40. const EXPIRATION = time.duration.weeks(1);
  41. const EXECUTION_ID_STORAGE_SLOT = namespaceSlot('AccessManager', 3n);
  42. const CONSUMING_SCHEDULE_STORAGE_SLOT = namespaceSlot('AccessManaged', 0n);
  43. /**
  44. * @requires this.{manager, caller, target, calldata}
  45. */
  46. async function scheduleOperation(manager, { caller, target, calldata, delay }) {
  47. const timestamp = await time.latest();
  48. const scheduledAt = timestamp.addn(1);
  49. await setNextBlockTimestamp(scheduledAt); // Fix next block timestamp for predictability
  50. const { receipt } = await manager.schedule(target, calldata, scheduledAt.add(delay), {
  51. from: caller,
  52. });
  53. return {
  54. receipt,
  55. scheduledAt,
  56. operationId: hashOperation(caller, target, calldata),
  57. };
  58. }
  59. const hashOperation = (caller, target, data) =>
  60. web3.utils.keccak256(web3.eth.abi.encodeParameters(['address', 'address', 'bytes'], [caller, target, data]));
  61. module.exports = {
  62. buildBaseRoles,
  63. formatAccess,
  64. MINSETBACK,
  65. EXPIRATION,
  66. EXECUTION_ID_STORAGE_SLOT,
  67. CONSUMING_SCHEDULE_STORAGE_SLOT,
  68. scheduleOperation,
  69. hashOperation,
  70. };