GovernorSettings.sol 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (governance/extensions/GovernorSettings.sol)
  3. pragma solidity ^0.8.19;
  4. import {Governor} from "../Governor.sol";
  5. /**
  6. * @dev Extension of {Governor} for settings updatable through governance.
  7. *
  8. * _Available since v4.4._
  9. */
  10. abstract contract GovernorSettings is Governor {
  11. // amount of token
  12. uint256 private _proposalThreshold;
  13. // timepoint: limited to uint48 in core (same as clock() type)
  14. uint48 private _votingDelay;
  15. // duration: limited to uint32 in core
  16. uint32 private _votingPeriod;
  17. event VotingDelaySet(uint256 oldVotingDelay, uint256 newVotingDelay);
  18. event VotingPeriodSet(uint256 oldVotingPeriod, uint256 newVotingPeriod);
  19. event ProposalThresholdSet(uint256 oldProposalThreshold, uint256 newProposalThreshold);
  20. /**
  21. * @dev Initialize the governance parameters.
  22. */
  23. constructor(uint48 initialVotingDelay, uint32 initialVotingPeriod, uint256 initialProposalThreshold) {
  24. _setVotingDelay(initialVotingDelay);
  25. _setVotingPeriod(initialVotingPeriod);
  26. _setProposalThreshold(initialProposalThreshold);
  27. }
  28. /**
  29. * @dev See {IGovernor-votingDelay}.
  30. */
  31. function votingDelay() public view virtual override returns (uint256) {
  32. return _votingDelay;
  33. }
  34. /**
  35. * @dev See {IGovernor-votingPeriod}.
  36. */
  37. function votingPeriod() public view virtual override returns (uint256) {
  38. return _votingPeriod;
  39. }
  40. /**
  41. * @dev See {Governor-proposalThreshold}.
  42. */
  43. function proposalThreshold() public view virtual override returns (uint256) {
  44. return _proposalThreshold;
  45. }
  46. /**
  47. * @dev Update the voting delay. This operation can only be performed through a governance proposal.
  48. *
  49. * Emits a {VotingDelaySet} event.
  50. */
  51. function setVotingDelay(uint48 newVotingDelay) public virtual onlyGovernance {
  52. _setVotingDelay(newVotingDelay);
  53. }
  54. /**
  55. * @dev Update the voting period. This operation can only be performed through a governance proposal.
  56. *
  57. * Emits a {VotingPeriodSet} event.
  58. */
  59. function setVotingPeriod(uint32 newVotingPeriod) public virtual onlyGovernance {
  60. _setVotingPeriod(newVotingPeriod);
  61. }
  62. /**
  63. * @dev Update the proposal threshold. This operation can only be performed through a governance proposal.
  64. *
  65. * Emits a {ProposalThresholdSet} event.
  66. */
  67. function setProposalThreshold(uint256 newProposalThreshold) public virtual onlyGovernance {
  68. _setProposalThreshold(newProposalThreshold);
  69. }
  70. /**
  71. * @dev Internal setter for the voting delay.
  72. *
  73. * Emits a {VotingDelaySet} event.
  74. */
  75. function _setVotingDelay(uint48 newVotingDelay) internal virtual {
  76. emit VotingDelaySet(_votingDelay, newVotingDelay);
  77. _votingDelay = newVotingDelay;
  78. }
  79. /**
  80. * @dev Internal setter for the voting period.
  81. *
  82. * Emits a {VotingPeriodSet} event.
  83. */
  84. function _setVotingPeriod(uint32 newVotingPeriod) internal virtual {
  85. // voting period must be at least one block long
  86. if (newVotingPeriod == 0) {
  87. revert GovernorInvalidVotingPeriod(0);
  88. }
  89. emit VotingPeriodSet(_votingPeriod, newVotingPeriod);
  90. _votingPeriod = newVotingPeriod;
  91. }
  92. /**
  93. * @dev Internal setter for the proposal threshold.
  94. *
  95. * Emits a {ProposalThresholdSet} event.
  96. */
  97. function _setProposalThreshold(uint256 newProposalThreshold) internal virtual {
  98. emit ProposalThresholdSet(_proposalThreshold, newProposalThreshold);
  99. _proposalThreshold = newProposalThreshold;
  100. }
  101. }