-
Notifications
You must be signed in to change notification settings - Fork 2
/
Timelock.sol
204 lines (176 loc) · 5.08 KB
/
Timelock.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.6;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/upgrades/contracts/Initializable.sol";
import "./Permissions.sol";
/// @title Timelock
/// @author 0xScotch <scotch@malt.money>
/// @notice Fairly basic timelock contract
contract Timelock is Initializable, Permissions {
using SafeMath for uint256;
// The amount of delay after which a delay can a queued can be executed.
uint256 public delay = 2 days;
// The the period within which an queued proposal can be executed.
uint256 public gracePeriod = 7 days;
mapping(bytes32 => bool) public queuedTransactions;
event NewDelay(uint256 indexed newDelay_);
event NewGracePeriod(uint256 indexed newGracePerios_);
event NewGovernor(address newGovernor);
event CancelTransaction(
bytes32 indexed txHash_,
address indexed target_,
uint256 value_,
string signature_,
bytes data_,
uint256 eta_
);
event ExecuteTransaction(
bytes32 indexed txHash_,
address indexed target_,
uint256 value_,
string signature_,
bytes data_,
uint256 eta_
);
event QueueTransaction(
bytes32 indexed txHash_,
address indexed target_,
uint256 value_,
string signature_,
bytes data_,
uint256 eta_
);
address public governor;
function initialize(address _admin, address _governor) external initializer {
_adminSetup(_admin);
_setupRole(GOVERNOR_ROLE, address(this));
_setupRole(GOVERNOR_ROLE, _governor);
_setRoleAdmin(GOVERNOR_ROLE, TIMELOCK_ROLE);
governor = _governor;
}
receive() external payable {}
/**
* @notice Sets the amount of time after which a proposal that has been queued can be executed.
*/
function setDelay(uint256 _delay)
public
onlyRole(GOVERNOR_ROLE, "Must have timelock role")
{
require(
_delay >= 0 && _delay < gracePeriod,
"Timelock::setDelay: Delay must not be greater equal to zero and less than gracePeriod"
);
delay = _delay;
emit NewDelay(delay);
}
/**
* @notice Sets the amount of time within which a queued proposal can be executed.
*/
function setGracePeriod(uint256 _gracePeriod)
public
onlyRole(GOVERNOR_ROLE, "Must have timelock role")
{
require(
_gracePeriod > delay,
"Timelock::gracePeriod: Grace period must be greater than delay"
);
gracePeriod = _gracePeriod;
emit NewGracePeriod(gracePeriod);
}
/**
* @notice Sets the governor address that is allowed to make proposals
*/
function setGovernor(address _governor)
public
onlyRole(GOVERNOR_ROLE, "Must have timelock role")
{
_swapRole(_governor, governor, GOVERNOR_ROLE);
governor = _governor;
emit NewGovernor(_governor);
}
function queueTransaction(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
)
public
onlyRole(GOVERNOR_ROLE, "Timelock::queueTransaction: Call must come from governor.")
returns (bytes32)
{
require(
eta >= block.timestamp.add(delay),
"Timelock::queueTransaction: Estimated execution block must satisfy delay."
);
bytes32 txHash = keccak256(
abi.encode(target, value, signature, data, eta)
);
queuedTransactions[txHash] = true;
emit QueueTransaction(txHash, target, value, signature, data, eta);
return txHash;
}
function cancelTransaction(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
)
public
onlyRole(GOVERNOR_ROLE, "Timelock::cancelTransaction: Call must come from governor.")
{
bytes32 txHash = keccak256(
abi.encode(target, value, signature, data, eta)
);
queuedTransactions[txHash] = false;
emit CancelTransaction(txHash, target, value, signature, data, eta);
}
function executeTransaction(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
)
public
payable
onlyRole(GOVERNOR_ROLE, "Timelock::executeTransaction: Call must come from governor.")
returns (bytes memory)
{
bytes32 txHash = keccak256(
abi.encode(target, value, signature, data, eta)
);
require(
queuedTransactions[txHash],
"Timelock::executeTransaction: Transaction hasn't been queued."
);
require(
block.timestamp >= eta,
"Timelock::executeTransaction: Transaction hasn't surpassed time lock."
);
require(
block.timestamp <= eta.add(gracePeriod),
"Timelock::executeTransaction: Transaction is stale."
);
queuedTransactions[txHash] = false;
bytes memory callData;
if (bytes(signature).length == 0) {
callData = data;
} else {
callData = abi.encodePacked(
bytes4(keccak256(bytes(signature))),
data
);
}
(bool success, bytes memory returnData) = target.call{value: value}(
callData
);
require(
success,
"Timelock::executeTransaction: Transaction execution reverted."
);
emit ExecuteTransaction(txHash, target, value, signature, data, eta);
return returnData;
}
}