GovernorTimelockCompound.sol 6.0 KB

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