GovernorTimelockCompound.sol 8.2 KB

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