GovernorTimelockCompound.sol 7.3 KB

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