GovernorTimelockCompound.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.3.0) (governance/extensions/GovernorTimelockCompound.sol)
  3. pragma solidity ^0.8.20;
  4. import {IGovernor, Governor} from "../Governor.sol";
  5. import {ICompoundTimelock} from "../../vendor/compound/ICompoundTimelock.sol";
  6. import {Address} from "../../utils/Address.sol";
  7. import {SafeCast} from "../../utils/math/SafeCast.sol";
  8. /**
  9. * @dev Extension of {Governor} that binds the execution process to a Compound Timelock. This adds a delay, enforced by
  10. * the external timelock to all successful proposals (in addition to the voting duration). The {Governor} needs to be
  11. * the admin of the timelock for any operation to be performed. A public, unrestricted,
  12. * {GovernorTimelockCompound-__acceptAdmin} is available to accept ownership of the timelock.
  13. *
  14. * Using this model means the proposal will be operated by the {TimelockController} and not by the {Governor}. Thus,
  15. * the assets and permissions must be attached to the {TimelockController}. Any asset sent to the {Governor} will be
  16. * inaccessible from a proposal, unless executed via {Governor-relay}.
  17. */
  18. abstract contract GovernorTimelockCompound is Governor {
  19. ICompoundTimelock private _timelock;
  20. /**
  21. * @dev Emitted when the timelock controller used for proposal execution is modified.
  22. */
  23. event TimelockChange(address oldTimelock, address newTimelock);
  24. /**
  25. * @dev Set the timelock.
  26. */
  27. constructor(ICompoundTimelock timelockAddress) {
  28. _updateTimelock(timelockAddress);
  29. }
  30. /**
  31. * @dev Overridden version of the {Governor-state} function with added support for the `Expired` state.
  32. */
  33. function state(uint256 proposalId) public view virtual override returns (ProposalState) {
  34. ProposalState currentState = super.state(proposalId);
  35. return
  36. (currentState == ProposalState.Queued &&
  37. block.timestamp >= proposalEta(proposalId) + _timelock.GRACE_PERIOD())
  38. ? ProposalState.Expired
  39. : currentState;
  40. }
  41. /**
  42. * @dev Public accessor to check the address of the timelock
  43. */
  44. function timelock() public view virtual returns (address) {
  45. return address(_timelock);
  46. }
  47. /**
  48. * @dev See {IGovernor-proposalNeedsQueuing}.
  49. */
  50. function proposalNeedsQueuing(uint256) public view virtual override returns (bool) {
  51. return true;
  52. }
  53. /**
  54. * @dev Function to queue a proposal to the timelock.
  55. */
  56. function _queueOperations(
  57. uint256 proposalId,
  58. address[] memory targets,
  59. uint256[] memory values,
  60. bytes[] memory calldatas,
  61. bytes32 /*descriptionHash*/
  62. ) internal virtual override returns (uint48) {
  63. uint48 etaSeconds = SafeCast.toUint48(block.timestamp + _timelock.delay());
  64. for (uint256 i = 0; i < targets.length; ++i) {
  65. if (
  66. _timelock.queuedTransactions(keccak256(abi.encode(targets[i], values[i], "", calldatas[i], etaSeconds)))
  67. ) {
  68. revert GovernorAlreadyQueuedProposal(proposalId);
  69. }
  70. _timelock.queueTransaction(targets[i], values[i], "", calldatas[i], etaSeconds);
  71. }
  72. return etaSeconds;
  73. }
  74. /**
  75. * @dev Overridden version of the {Governor-_executeOperations} function that run the already queued proposal
  76. * through the timelock.
  77. */
  78. function _executeOperations(
  79. uint256 proposalId,
  80. address[] memory targets,
  81. uint256[] memory values,
  82. bytes[] memory calldatas,
  83. bytes32 /*descriptionHash*/
  84. ) internal virtual override {
  85. uint256 etaSeconds = proposalEta(proposalId);
  86. if (etaSeconds == 0) {
  87. revert GovernorNotQueuedProposal(proposalId);
  88. }
  89. Address.sendValue(payable(_timelock), msg.value);
  90. for (uint256 i = 0; i < targets.length; ++i) {
  91. _timelock.executeTransaction(targets[i], values[i], "", calldatas[i], etaSeconds);
  92. }
  93. }
  94. /**
  95. * @dev Overridden version of the {Governor-_cancel} function to cancel the timelocked proposal if it has already
  96. * been queued.
  97. */
  98. function _cancel(
  99. address[] memory targets,
  100. uint256[] memory values,
  101. bytes[] memory calldatas,
  102. bytes32 descriptionHash
  103. ) internal virtual override returns (uint256) {
  104. uint256 proposalId = super._cancel(targets, values, calldatas, descriptionHash);
  105. uint256 etaSeconds = proposalEta(proposalId);
  106. if (etaSeconds > 0) {
  107. // do external call later
  108. for (uint256 i = 0; i < targets.length; ++i) {
  109. _timelock.cancelTransaction(targets[i], values[i], "", calldatas[i], etaSeconds);
  110. }
  111. }
  112. return proposalId;
  113. }
  114. /**
  115. * @dev Address through which the governor executes action. In this case, the timelock.
  116. */
  117. function _executor() internal view virtual override returns (address) {
  118. return address(_timelock);
  119. }
  120. /**
  121. * @dev Accept admin right over the timelock.
  122. */
  123. // solhint-disable-next-line private-vars-leading-underscore
  124. function __acceptAdmin() public {
  125. _timelock.acceptAdmin();
  126. }
  127. /**
  128. * @dev Public endpoint to update the underlying timelock instance. Restricted to the timelock itself, so updates
  129. * must be proposed, scheduled, and executed through governance proposals.
  130. *
  131. * For security reasons, the timelock must be handed over to another admin before setting up a new one. The two
  132. * operations (hand over the timelock) and do the update can be batched in a single proposal.
  133. *
  134. * Note that if the timelock admin has been handed over in a previous operation, we refuse updates made through the
  135. * timelock if admin of the timelock has already been accepted and the operation is executed outside the scope of
  136. * governance.
  137. * CAUTION: It is not recommended to change the timelock while there are other queued governance proposals.
  138. */
  139. function updateTimelock(ICompoundTimelock newTimelock) external virtual onlyGovernance {
  140. _updateTimelock(newTimelock);
  141. }
  142. function _updateTimelock(ICompoundTimelock newTimelock) private {
  143. emit TimelockChange(address(_timelock), address(newTimelock));
  144. _timelock = newTimelock;
  145. }
  146. }