GovernorTimelockCompound.sol 7.2 KB

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