GovernorTimelockCompound.sol 6.1 KB

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