GovernorTimelockCompound.sol 7.3 KB

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