CompTimelockUpgradeable.sol 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. // solhint-disable private-vars-leading-underscore
  3. /**
  4. * Copyright 2020 Compound Labs, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  7. * following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
  10. * disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
  13. * following disclaimer in the documentation and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
  16. * products derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  19. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. pragma solidity ^0.8.0;
  27. import "../../proxy/utils/Initializable.sol";
  28. contract CompTimelockUpgradeable is Initializable {
  29. event NewAdmin(address indexed newAdmin);
  30. event NewPendingAdmin(address indexed newPendingAdmin);
  31. event NewDelay(uint256 indexed newDelay);
  32. event CancelTransaction(
  33. bytes32 indexed txHash,
  34. address indexed target,
  35. uint256 value,
  36. string signature,
  37. bytes data,
  38. uint256 eta
  39. );
  40. event ExecuteTransaction(
  41. bytes32 indexed txHash,
  42. address indexed target,
  43. uint256 value,
  44. string signature,
  45. bytes data,
  46. uint256 eta
  47. );
  48. event QueueTransaction(
  49. bytes32 indexed txHash,
  50. address indexed target,
  51. uint256 value,
  52. string signature,
  53. bytes data,
  54. uint256 eta
  55. );
  56. uint256 public constant GRACE_PERIOD = 14 days;
  57. uint256 public constant MINIMUM_DELAY = 2 days;
  58. uint256 public constant MAXIMUM_DELAY = 30 days;
  59. address public admin;
  60. address public pendingAdmin;
  61. uint256 public delay;
  62. mapping(bytes32 => bool) public queuedTransactions;
  63. function __CompTimelock_init(address admin_, uint256 delay_) internal onlyInitializing {
  64. __CompTimelock_init_unchained(admin_, delay_);
  65. }
  66. function __CompTimelock_init_unchained(address admin_, uint256 delay_) internal onlyInitializing {
  67. require(delay_ >= MINIMUM_DELAY, "Timelock::constructor: Delay must exceed minimum delay.");
  68. require(delay_ <= MAXIMUM_DELAY, "Timelock::setDelay: Delay must not exceed maximum delay.");
  69. admin = admin_;
  70. delay = delay_;
  71. }
  72. receive() external payable {}
  73. function setDelay(uint256 delay_) public {
  74. require(msg.sender == address(this), "Timelock::setDelay: Call must come from Timelock.");
  75. require(delay_ >= MINIMUM_DELAY, "Timelock::setDelay: Delay must exceed minimum delay.");
  76. require(delay_ <= MAXIMUM_DELAY, "Timelock::setDelay: Delay must not exceed maximum delay.");
  77. delay = delay_;
  78. emit NewDelay(delay);
  79. }
  80. function acceptAdmin() public {
  81. require(msg.sender == pendingAdmin, "Timelock::acceptAdmin: Call must come from pendingAdmin.");
  82. admin = msg.sender;
  83. pendingAdmin = address(0);
  84. emit NewAdmin(admin);
  85. }
  86. function setPendingAdmin(address pendingAdmin_) public {
  87. require(msg.sender == address(this), "Timelock::setPendingAdmin: Call must come from Timelock.");
  88. pendingAdmin = pendingAdmin_;
  89. emit NewPendingAdmin(pendingAdmin);
  90. }
  91. function queueTransaction(
  92. address target,
  93. uint256 value,
  94. string memory signature,
  95. bytes memory data,
  96. uint256 eta
  97. ) public returns (bytes32) {
  98. require(msg.sender == admin, "Timelock::queueTransaction: Call must come from admin.");
  99. require(
  100. eta >= getBlockTimestamp() + delay,
  101. "Timelock::queueTransaction: Estimated execution block must satisfy delay."
  102. );
  103. bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
  104. queuedTransactions[txHash] = true;
  105. emit QueueTransaction(txHash, target, value, signature, data, eta);
  106. return txHash;
  107. }
  108. function cancelTransaction(
  109. address target,
  110. uint256 value,
  111. string memory signature,
  112. bytes memory data,
  113. uint256 eta
  114. ) public {
  115. require(msg.sender == admin, "Timelock::cancelTransaction: Call must come from admin.");
  116. bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
  117. queuedTransactions[txHash] = false;
  118. emit CancelTransaction(txHash, target, value, signature, data, eta);
  119. }
  120. function executeTransaction(
  121. address target,
  122. uint256 value,
  123. string memory signature,
  124. bytes memory data,
  125. uint256 eta
  126. ) public payable returns (bytes memory) {
  127. require(msg.sender == admin, "Timelock::executeTransaction: Call must come from admin.");
  128. bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
  129. require(queuedTransactions[txHash], "Timelock::executeTransaction: Transaction hasn't been queued.");
  130. require(getBlockTimestamp() >= eta, "Timelock::executeTransaction: Transaction hasn't surpassed time lock.");
  131. require(getBlockTimestamp() <= eta + GRACE_PERIOD, "Timelock::executeTransaction: Transaction is stale.");
  132. queuedTransactions[txHash] = false;
  133. bytes memory callData;
  134. if (bytes(signature).length == 0) {
  135. callData = data;
  136. } else {
  137. callData = abi.encodePacked(bytes4(keccak256(bytes(signature))), data);
  138. }
  139. // solium-disable-next-line security/no-call-value
  140. (bool success, bytes memory returnData) = target.call{value: value}(callData);
  141. require(success, "Timelock::executeTransaction: Transaction execution reverted.");
  142. emit ExecuteTransaction(txHash, target, value, signature, data, eta);
  143. return returnData;
  144. }
  145. function getBlockTimestamp() internal view returns (uint256) {
  146. // solium-disable-next-line security/no-block-members
  147. return block.timestamp;
  148. }
  149. /**
  150. * This empty reserved space is put in place to allow future versions to add new
  151. * variables without shifting down storage in the inheritance chain.
  152. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  153. */
  154. uint256[46] private __gap;
  155. }