/
PACTTimelock.sol
174 lines (144 loc) · 5.33 KB
/
PACTTimelock.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
//SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.4;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract PACTTimelock {
using SafeMath for uint256;
event NewAdmin(address indexed newAdmin);
event NewPendingAdmin(address indexed newPendingAdmin);
event NewDelay(uint256 indexed newDelay);
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
);
uint256 public constant GRACE_PERIOD = 14 days;
uint256 public constant MINIMUM_DELAY = 1 hours;
uint256 public constant MAXIMUM_DELAY = 30 days;
address public admin;
address public pendingAdmin;
uint256 public delay;
mapping(bytes32 => bool) public queuedTransactions;
constructor(address _admin, uint256 _delay) {
require(_delay >= MINIMUM_DELAY, "Timelock::constructor: Delay must exceed minimum delay.");
require(
_delay <= MAXIMUM_DELAY,
"Timelock::setDelay: Delay must not exceed maximum delay."
);
admin = _admin;
delay = _delay;
}
receive() external payable {}
function setDelay(uint256 _delay) public {
require(msg.sender == address(this), "Timelock::setDelay: Call must come from Timelock.");
require(_delay >= MINIMUM_DELAY, "Timelock::setDelay: Delay must exceed minimum delay.");
require(
_delay <= MAXIMUM_DELAY,
"Timelock::setDelay: Delay must not exceed maximum delay."
);
delay = _delay;
emit NewDelay(delay);
}
function acceptAdmin() public {
require(
msg.sender == pendingAdmin,
"Timelock::acceptAdmin: Call must come from pendingAdmin."
);
admin = msg.sender;
pendingAdmin = address(0);
emit NewAdmin(admin);
}
function setPendingAdmin(address _pendingAdmin) public {
require(
msg.sender == address(this),
"Timelock::setPendingAdmin: Call must come from Timelock."
);
pendingAdmin = _pendingAdmin;
emit NewPendingAdmin(pendingAdmin);
}
function queueTransaction(
address _target,
uint256 _value,
string memory _signature,
bytes memory _data,
uint256 _eta
) public returns (bytes32) {
require(msg.sender == admin, "Timelock::queueTransaction: Call must come from admin.");
require(
_eta >= getBlockTimestamp().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 {
require(msg.sender == admin, "Timelock::cancelTransaction: Call must come from admin.");
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 returns (bytes memory) {
require(msg.sender == admin, "Timelock::executeTransaction: Call must come from admin.");
bytes32 _txHash = keccak256(abi.encode(_target, _value, _signature, _data, _eta));
require(
queuedTransactions[_txHash],
"Timelock::executeTransaction: Transaction hasn't been queued."
);
require(
getBlockTimestamp() >= _eta,
"Timelock::executeTransaction: Transaction hasn't surpassed time lock."
);
require(
getBlockTimestamp() <= _eta.add(GRACE_PERIOD),
"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);
}
// solium-disable-next-line security/no-call-value
(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;
}
function getBlockTimestamp() internal view returns (uint256) {
// solium-disable-next-line security/no-block-members
return block.timestamp;
}
}