GovernorTimelockCompound.sol 7.2 KB

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