GovernorTimelockCompound.sol 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.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 proposal (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.
  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 eta = SafeCast.toUint48(block.timestamp + _timelock.delay());
  64. for (uint256 i = 0; i < targets.length; ++i) {
  65. if (_timelock.queuedTransactions(keccak256(abi.encode(targets[i], values[i], "", calldatas[i], eta)))) {
  66. revert GovernorAlreadyQueuedProposal(proposalId);
  67. }
  68. _timelock.queueTransaction(targets[i], values[i], "", calldatas[i], eta);
  69. }
  70. return eta;
  71. }
  72. /**
  73. * @dev Overridden version of the {Governor-_executeOperations} function that run the already queued proposal through
  74. * the timelock.
  75. */
  76. function _executeOperations(
  77. uint256 proposalId,
  78. address[] memory targets,
  79. uint256[] memory values,
  80. bytes[] memory calldatas,
  81. bytes32 /*descriptionHash*/
  82. ) internal virtual override {
  83. uint256 eta = proposalEta(proposalId);
  84. if (eta == 0) {
  85. revert GovernorNotQueuedProposal(proposalId);
  86. }
  87. Address.sendValue(payable(_timelock), msg.value);
  88. for (uint256 i = 0; i < targets.length; ++i) {
  89. _timelock.executeTransaction(targets[i], values[i], "", calldatas[i], eta);
  90. }
  91. }
  92. /**
  93. * @dev Overridden version of the {Governor-_cancel} function to cancel the timelocked proposal if it has already
  94. * been queued.
  95. */
  96. function _cancel(
  97. address[] memory targets,
  98. uint256[] memory values,
  99. bytes[] memory calldatas,
  100. bytes32 descriptionHash
  101. ) internal virtual override returns (uint256) {
  102. uint256 proposalId = super._cancel(targets, values, calldatas, descriptionHash);
  103. uint256 eta = proposalEta(proposalId);
  104. if (eta > 0) {
  105. // do external call later
  106. for (uint256 i = 0; i < targets.length; ++i) {
  107. _timelock.cancelTransaction(targets[i], values[i], "", calldatas[i], eta);
  108. }
  109. }
  110. return proposalId;
  111. }
  112. /**
  113. * @dev Address through which the governor executes action. In this case, the timelock.
  114. */
  115. function _executor() internal view virtual override returns (address) {
  116. return address(_timelock);
  117. }
  118. /**
  119. * @dev Accept admin right over the timelock.
  120. */
  121. // solhint-disable-next-line private-vars-leading-underscore
  122. function __acceptAdmin() public {
  123. _timelock.acceptAdmin();
  124. }
  125. /**
  126. * @dev Public endpoint to update the underlying timelock instance. Restricted to the timelock itself, so updates
  127. * must be proposed, scheduled, and executed through governance proposals.
  128. *
  129. * For security reasons, the timelock must be handed over to another admin before setting up a new one. The two
  130. * operations (hand over the timelock) and do the update can be batched in a single proposal.
  131. *
  132. * Note that if the timelock admin has been handed over in a previous operation, we refuse updates made through the
  133. * timelock if admin of the timelock has already been accepted and the operation is executed outside the scope of
  134. * governance.
  135. * CAUTION: It is not recommended to change the timelock while there are other queued governance proposals.
  136. */
  137. function updateTimelock(ICompoundTimelock newTimelock) external virtual onlyGovernance {
  138. _updateTimelock(newTimelock);
  139. }
  140. function _updateTimelock(ICompoundTimelock newTimelock) private {
  141. emit TimelockChange(address(_timelock), address(newTimelock));
  142. _timelock = newTimelock;
  143. }
  144. }