Guest User

contract

a guest
Oct 5th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. // COPIED FROM https://github.com/compound-finance/compound-protocol/blob/master/contracts/Governance/GovernorAlpha.sol
  2. // Copyright 2020 Compound Labs, Inc.
  3. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  4. // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  5. // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  6. // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8. //
  9. // Ctrl+f for XXX to see all the modifications.
  10.  
  11. // XXX: pragma solidity ^0.5.16;
  12. pragma solidity 0.6.12;
  13.  
  14. // XXX: import "./SafeMath.sol";
  15. import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.1.0/contracts/math/SafeMath.sol";
  16.  
  17. contract Timelock {
  18. using SafeMath for uint;
  19.  
  20. event NewAdmin(address indexed newAdmin);
  21. event NewPendingAdmin(address indexed newPendingAdmin);
  22. event NewDelay(uint indexed newDelay);
  23. event CancelTransaction(bytes32 indexed txHash, address indexed target, uint value, string signature, bytes data, uint eta);
  24. event ExecuteTransaction(bytes32 indexed txHash, address indexed target, uint value, string signature, bytes data, uint eta);
  25. event QueueTransaction(bytes32 indexed txHash, address indexed target, uint value, string signature, bytes data, uint eta);
  26.  
  27. uint public constant GRACE_PERIOD = 14 days;
  28. uint public constant MINIMUM_DELAY = 2 days;
  29. uint public constant MAXIMUM_DELAY = 30 days;
  30.  
  31. address public admin;
  32. address public pendingAdmin;
  33. uint public delay;
  34. bool public admin_initialized;
  35.  
  36. mapping (bytes32 => bool) public queuedTransactions;
  37.  
  38.  
  39. constructor(address admin_, uint delay_) public {
  40. require(delay_ >= MINIMUM_DELAY, "Timelock::constructor: Delay must exceed minimum delay.");
  41. require(delay_ <= MAXIMUM_DELAY, "Timelock::constructor: Delay must not exceed maximum delay.");
  42.  
  43. admin = admin_;
  44. delay = delay_;
  45. admin_initialized = false;
  46. }
  47.  
  48. // XXX: function() external payable { }
  49. receive() external payable { }
  50.  
  51. function setDelay(uint delay_) public {
  52. require(msg.sender == address(this), "Timelock::setDelay: Call must come from Timelock.");
  53. require(delay_ >= MINIMUM_DELAY, "Timelock::setDelay: Delay must exceed minimum delay.");
  54. require(delay_ <= MAXIMUM_DELAY, "Timelock::setDelay: Delay must not exceed maximum delay.");
  55. delay = delay_;
  56.  
  57. emit NewDelay(delay);
  58. }
  59.  
  60. function acceptAdmin() public {
  61. require(msg.sender == pendingAdmin, "Timelock::acceptAdmin: Call must come from pendingAdmin.");
  62. admin = msg.sender;
  63. pendingAdmin = address(0);
  64.  
  65. emit NewAdmin(admin);
  66. }
  67.  
  68. function setPendingAdmin(address pendingAdmin_) public {
  69. // allows one time setting of admin for deployment purposes
  70. if (admin_initialized) {
  71. require(msg.sender == address(this), "Timelock::setPendingAdmin: Call must come from Timelock.");
  72. } else {
  73. require(msg.sender == admin, "Timelock::setPendingAdmin: First call must come from admin.");
  74. admin_initialized = true;
  75. }
  76. pendingAdmin = pendingAdmin_;
  77.  
  78. emit NewPendingAdmin(pendingAdmin);
  79. }
  80.  
  81. function queueTransaction(address target, uint value, string memory signature, bytes memory data, uint eta) public returns (bytes32) {
  82. require(msg.sender == admin, "Timelock::queueTransaction: Call must come from admin.");
  83. require(eta >= getBlockTimestamp().add(delay), "Timelock::queueTransaction: Estimated execution block must satisfy delay.");
  84.  
  85. bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
  86. queuedTransactions[txHash] = true;
  87.  
  88. emit QueueTransaction(txHash, target, value, signature, data, eta);
  89. return txHash;
  90. }
  91.  
  92. function cancelTransaction(address target, uint value, string memory signature, bytes memory data, uint eta) public {
  93. require(msg.sender == admin, "Timelock::cancelTransaction: Call must come from admin.");
  94.  
  95. bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
  96. queuedTransactions[txHash] = false;
  97.  
  98. emit CancelTransaction(txHash, target, value, signature, data, eta);
  99. }
  100.  
  101. function executeTransaction(address target, uint value, string memory signature, bytes memory data, uint eta) public payable returns (bytes memory) {
  102. require(msg.sender == admin, "Timelock::executeTransaction: Call must come from admin.");
  103.  
  104. bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
  105. require(queuedTransactions[txHash], "Timelock::executeTransaction: Transaction hasn't been queued.");
  106. require(getBlockTimestamp() >= eta, "Timelock::executeTransaction: Transaction hasn't surpassed time lock.");
  107. require(getBlockTimestamp() <= eta.add(GRACE_PERIOD), "Timelock::executeTransaction: Transaction is stale.");
  108.  
  109. queuedTransactions[txHash] = false;
  110.  
  111. bytes memory callData;
  112.  
  113. if (bytes(signature).length == 0) {
  114. callData = data;
  115. } else {
  116. callData = abi.encodePacked(bytes4(keccak256(bytes(signature))), data);
  117. }
  118.  
  119. // solium-disable-next-line security/no-call-value
  120. (bool success, bytes memory returnData) = target.call.value(value)(callData);
  121. require(success, "Timelock::executeTransaction: Transaction execution reverted.");
  122.  
  123. emit ExecuteTransaction(txHash, target, value, signature, data, eta);
  124.  
  125. return returnData;
  126. }
  127.  
  128. function getBlockTimestamp() internal view returns (uint) {
  129. // solium-disable-next-line security/no-block-members
  130. return block.timestamp;
  131. }
  132. }
Add Comment
Please, Sign In to add comment