GovernorStorage.sol 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (governance/extensions/GovernorStorage.sol)
  3. pragma solidity ^0.8.20;
  4. import {Governor} from "../Governor.sol";
  5. /**
  6. * @dev Extension of {Governor} that implements storage of proposal details. This modules also provides primitives for
  7. * the enumerability of proposals.
  8. *
  9. * Use cases for this module include:
  10. * - UIs that explore the proposal state without relying on event indexing.
  11. * - Using only the proposalId as an argument in the {Governor-queue} and {Governor-execute} functions for L2 chains
  12. * where storage is cheap compared to calldata.
  13. */
  14. abstract contract GovernorStorage is Governor {
  15. struct ProposalDetails {
  16. address[] targets;
  17. uint256[] values;
  18. bytes[] calldatas;
  19. bytes32 descriptionHash;
  20. }
  21. uint256[] private _proposalIds;
  22. mapping(uint256 proposalId => ProposalDetails) private _proposalDetails;
  23. /**
  24. * @dev Hook into the proposing mechanism
  25. */
  26. function _propose(
  27. address[] memory targets,
  28. uint256[] memory values,
  29. bytes[] memory calldatas,
  30. string memory description,
  31. address proposer
  32. ) internal virtual override returns (uint256) {
  33. uint256 proposalId = super._propose(targets, values, calldatas, description, proposer);
  34. // store
  35. _proposalIds.push(proposalId);
  36. _proposalDetails[proposalId] = ProposalDetails({
  37. targets: targets,
  38. values: values,
  39. calldatas: calldatas,
  40. descriptionHash: keccak256(bytes(description))
  41. });
  42. return proposalId;
  43. }
  44. /**
  45. * @dev Version of {IGovernor-queue} with only `proposalId` as an argument.
  46. */
  47. function queue(uint256 proposalId) public virtual {
  48. // here, using storage is more efficient than memory
  49. ProposalDetails storage details = _proposalDetails[proposalId];
  50. queue(details.targets, details.values, details.calldatas, details.descriptionHash);
  51. }
  52. /**
  53. * @dev Version of {IGovernor-execute} with only `proposalId` as an argument.
  54. */
  55. function execute(uint256 proposalId) public payable virtual {
  56. // here, using storage is more efficient than memory
  57. ProposalDetails storage details = _proposalDetails[proposalId];
  58. execute(details.targets, details.values, details.calldatas, details.descriptionHash);
  59. }
  60. /**
  61. * @dev ProposalId version of {IGovernor-cancel}.
  62. */
  63. function cancel(uint256 proposalId) public virtual {
  64. // here, using storage is more efficient than memory
  65. ProposalDetails storage details = _proposalDetails[proposalId];
  66. cancel(details.targets, details.values, details.calldatas, details.descriptionHash);
  67. }
  68. /**
  69. * @dev Returns the number of stored proposals.
  70. */
  71. function proposalCount() public view virtual returns (uint256) {
  72. return _proposalIds.length;
  73. }
  74. /**
  75. * @dev Returns the details of a proposalId. Reverts if `proposalId` is not a known proposal.
  76. */
  77. function proposalDetails(
  78. uint256 proposalId
  79. )
  80. public
  81. view
  82. virtual
  83. returns (address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
  84. {
  85. // here, using memory is more efficient than storage
  86. ProposalDetails memory details = _proposalDetails[proposalId];
  87. if (details.descriptionHash == 0) {
  88. revert GovernorNonexistentProposal(proposalId);
  89. }
  90. return (details.targets, details.values, details.calldatas, details.descriptionHash);
  91. }
  92. /**
  93. * @dev Returns the details (including the proposalId) of a proposal given its sequential index.
  94. */
  95. function proposalDetailsAt(
  96. uint256 index
  97. )
  98. public
  99. view
  100. virtual
  101. returns (
  102. uint256 proposalId,
  103. address[] memory targets,
  104. uint256[] memory values,
  105. bytes[] memory calldatas,
  106. bytes32 descriptionHash
  107. )
  108. {
  109. proposalId = _proposalIds[index];
  110. (targets, values, calldatas, descriptionHash) = proposalDetails(proposalId);
  111. }
  112. }