GovernorTimelockCompound.sol 8.3 KB

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