CompTimelock.sol 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. contract CompTimelock {
  28. event NewAdmin(address indexed newAdmin);
  29. event NewPendingAdmin(address indexed newPendingAdmin);
  30. event NewDelay(uint256 indexed newDelay);
  31. event CancelTransaction(
  32. bytes32 indexed txHash,
  33. address indexed target,
  34. uint256 value,
  35. string signature,
  36. bytes data,
  37. uint256 eta
  38. );
  39. event ExecuteTransaction(
  40. bytes32 indexed txHash,
  41. address indexed target,
  42. uint256 value,
  43. string signature,
  44. bytes data,
  45. uint256 eta
  46. );
  47. event QueueTransaction(
  48. bytes32 indexed txHash,
  49. address indexed target,
  50. uint256 value,
  51. string signature,
  52. bytes data,
  53. uint256 eta
  54. );
  55. uint256 public constant GRACE_PERIOD = 14 days;
  56. uint256 public constant MINIMUM_DELAY = 2 days;
  57. uint256 public constant MAXIMUM_DELAY = 30 days;
  58. address public admin;
  59. address public pendingAdmin;
  60. uint256 public delay;
  61. mapping(bytes32 => bool) public queuedTransactions;
  62. constructor(address admin_, uint256 delay_) {
  63. require(delay_ >= MINIMUM_DELAY, "Timelock::constructor: Delay must exceed minimum delay.");
  64. require(delay_ <= MAXIMUM_DELAY, "Timelock::setDelay: Delay must not exceed maximum delay.");
  65. admin = admin_;
  66. delay = delay_;
  67. }
  68. receive() external payable {}
  69. function setDelay(uint256 delay_) public {
  70. require(msg.sender == address(this), "Timelock::setDelay: Call must come from Timelock.");
  71. require(delay_ >= MINIMUM_DELAY, "Timelock::setDelay: Delay must exceed minimum delay.");
  72. require(delay_ <= MAXIMUM_DELAY, "Timelock::setDelay: Delay must not exceed maximum delay.");
  73. delay = delay_;
  74. emit NewDelay(delay);
  75. }
  76. function acceptAdmin() public {
  77. require(msg.sender == pendingAdmin, "Timelock::acceptAdmin: Call must come from pendingAdmin.");
  78. admin = msg.sender;
  79. pendingAdmin = address(0);
  80. emit NewAdmin(admin);
  81. }
  82. function setPendingAdmin(address pendingAdmin_) public {
  83. require(msg.sender == address(this), "Timelock::setPendingAdmin: Call must come from Timelock.");
  84. pendingAdmin = pendingAdmin_;
  85. emit NewPendingAdmin(pendingAdmin);
  86. }
  87. function queueTransaction(
  88. address target,
  89. uint256 value,
  90. string memory signature,
  91. bytes memory data,
  92. uint256 eta
  93. ) public returns (bytes32) {
  94. require(msg.sender == admin, "Timelock::queueTransaction: Call must come from admin.");
  95. require(
  96. eta >= getBlockTimestamp() + delay,
  97. "Timelock::queueTransaction: Estimated execution block must satisfy delay."
  98. );
  99. bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
  100. queuedTransactions[txHash] = true;
  101. emit QueueTransaction(txHash, target, value, signature, data, eta);
  102. return txHash;
  103. }
  104. function cancelTransaction(
  105. address target,
  106. uint256 value,
  107. string memory signature,
  108. bytes memory data,
  109. uint256 eta
  110. ) public {
  111. require(msg.sender == admin, "Timelock::cancelTransaction: Call must come from admin.");
  112. bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
  113. queuedTransactions[txHash] = false;
  114. emit CancelTransaction(txHash, target, value, signature, data, eta);
  115. }
  116. function executeTransaction(
  117. address target,
  118. uint256 value,
  119. string memory signature,
  120. bytes memory data,
  121. uint256 eta
  122. ) public payable returns (bytes memory) {
  123. require(msg.sender == admin, "Timelock::executeTransaction: Call must come from admin.");
  124. bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
  125. require(queuedTransactions[txHash], "Timelock::executeTransaction: Transaction hasn't been queued.");
  126. require(getBlockTimestamp() >= eta, "Timelock::executeTransaction: Transaction hasn't surpassed time lock.");
  127. require(getBlockTimestamp() <= eta + GRACE_PERIOD, "Timelock::executeTransaction: Transaction is stale.");
  128. queuedTransactions[txHash] = false;
  129. bytes memory callData;
  130. if (bytes(signature).length == 0) {
  131. callData = data;
  132. } else {
  133. callData = abi.encodePacked(bytes4(keccak256(bytes(signature))), data);
  134. }
  135. // solium-disable-next-line security/no-call-value
  136. (bool success, bytes memory returnData) = target.call{value: value}(callData);
  137. require(success, "Timelock::executeTransaction: Transaction execution reverted.");
  138. emit ExecuteTransaction(txHash, target, value, signature, data, eta);
  139. return returnData;
  140. }
  141. function getBlockTimestamp() internal view returns (uint256) {
  142. // solium-disable-next-line security/no-block-members
  143. return block.timestamp;
  144. }
  145. }