GovernorTimelockCompound.sol 7.0 KB

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