GovernorSequentialProposalId.sol 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0) (governance/extensions/GovernorSequentialProposalId.sol)
  3. pragma solidity ^0.8.24;
  4. import {IGovernor, Governor} from "../Governor.sol";
  5. /**
  6. * @dev Extension of {Governor} that changes the numbering of proposal ids from the default hash-based approach to
  7. * sequential ids.
  8. */
  9. abstract contract GovernorSequentialProposalId is Governor {
  10. uint256 private _latestProposalId;
  11. mapping(uint256 proposalHash => uint256 proposalId) private _proposalIds;
  12. /**
  13. * @dev The {latestProposalId} may only be initialized if it hasn't been set yet
  14. * (through initialization or the creation of a proposal).
  15. */
  16. error GovernorAlreadyInitializedLatestProposalId();
  17. /// @inheritdoc IGovernor
  18. function getProposalId(
  19. address[] memory targets,
  20. uint256[] memory values,
  21. bytes[] memory calldatas,
  22. bytes32 descriptionHash
  23. ) public view virtual override returns (uint256) {
  24. uint256 proposalHash = hashProposal(targets, values, calldatas, descriptionHash);
  25. uint256 storedProposalId = _proposalIds[proposalHash];
  26. if (storedProposalId == 0) {
  27. revert GovernorNonexistentProposal(0);
  28. }
  29. return storedProposalId;
  30. }
  31. /**
  32. * @dev Returns the latest proposal id. A return value of 0 means no proposals have been created yet.
  33. */
  34. function latestProposalId() public view virtual returns (uint256) {
  35. return _latestProposalId;
  36. }
  37. /**
  38. * @dev See {IGovernor-_propose}.
  39. * Hook into the proposing mechanism to increment proposal count.
  40. */
  41. function _propose(
  42. address[] memory targets,
  43. uint256[] memory values,
  44. bytes[] memory calldatas,
  45. string memory description,
  46. address proposer
  47. ) internal virtual override returns (uint256) {
  48. uint256 proposalHash = hashProposal(targets, values, calldatas, keccak256(bytes(description)));
  49. uint256 storedProposalId = _proposalIds[proposalHash];
  50. if (storedProposalId == 0) {
  51. _proposalIds[proposalHash] = ++_latestProposalId;
  52. }
  53. return super._propose(targets, values, calldatas, description, proposer);
  54. }
  55. /**
  56. * @dev Internal function to set the {latestProposalId}. This function is helpful when transitioning
  57. * from another governance system. The next proposal id will be `newLatestProposalId` + 1.
  58. *
  59. * May only call this function if the current value of {latestProposalId} is 0.
  60. */
  61. function _initializeLatestProposalId(uint256 newLatestProposalId) internal virtual {
  62. if (_latestProposalId != 0) {
  63. revert GovernorAlreadyInitializedLatestProposalId();
  64. }
  65. _latestProposalId = newLatestProposalId;
  66. }
  67. }