-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20TimeLock.sol
More file actions
154 lines (131 loc) · 4.31 KB
/
20TimeLock.sol
File metadata and controls
154 lines (131 loc) · 4.31 KB
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
/*
# Time Lock
TimeLock is a contract that publishes a transaction to be executed in the future. After a mimimum waiting period, the transaction can be executed.
TimeLocks are commonly used in DAOs.
*/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract TimeLock {
error NotOwnerError();
error AlreadyQueuedError(bytes32 txId);
error TimestampNotInRangeError(uint blockTimestamp, uint timestamp);
error NotQueuedError(bytes32 txId);
error TimestampNotPassedError(uint blockTimestmap, uint timestamp);
error TimestampExpiredError(uint blockTimestamp, uint expiresAt);
error TxFailedError();
event Queue(
bytes32 indexed txId,
address indexed target,
uint value,
string func,
bytes data,
uint timestamp
);
event Execute(
bytes32 indexed txId,
address indexed target,
uint value,
string func,
bytes data,
uint timestamp
);
event Cancel(bytes32 indexed txId);
uint public constant MIN_DELAY = 10; // seconds
uint public constant MAX_DELAY = 1000; // seconds
uint public constant GRACE_PERIOD = 1000; // seconds
address public owner;
// tx id => queued
mapping(bytes32 => bool) public queued;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
if (msg.sender != owner) {
revert NotOwnerError();
}
_;
}
receive() external payable {}
function getTxId(
address _target,
uint _value,
string calldata _func,
bytes calldata _data,
uint _timestamp
) public pure returns (bytes32) {
return keccak256(abi.encode(_target, _value, _func, _data, _timestamp));
}
/**
* @param _target Address of contract or account to call
* @param _value Amount of ETH to send
* @param _func Function signature, for example "foo(address,uint256)"
* @param _data ABI encoded data send.
* @param _timestamp Timestamp after which the transaction can be executed.
*/
function queue(
address _target,
uint _value,
string calldata _func,
bytes calldata _data,
uint _timestamp
) external onlyOwner returns (bytes32 txId) {
txId = getTxId(_target, _value, _func, _data, _timestamp);
if (queued[txId]) {
revert AlreadyQueuedError(txId);
}
// ---|------------|---------------|-------
// block block + min block + max
if (
_timestamp < block.timestamp + MIN_DELAY ||
_timestamp > block.timestamp + MAX_DELAY
) {
revert TimestampNotInRangeError(block.timestamp, _timestamp);
}
queued[txId] = true;
emit Queue(txId, _target, _value, _func, _data, _timestamp);
}
function execute(
address _target,
uint _value,
string calldata _func,
bytes calldata _data,
uint _timestamp
) external payable onlyOwner returns (bytes memory) {
bytes32 txId = getTxId(_target, _value, _func, _data, _timestamp);
if (!queued[txId]) {
revert NotQueuedError(txId);
}
// ----|-------------------|-------
// timestamp timestamp + grace period
if (block.timestamp < _timestamp) {
revert TimestampNotPassedError(block.timestamp, _timestamp);
}
if (block.timestamp > _timestamp + GRACE_PERIOD) {
revert TimestampExpiredError(block.timestamp, _timestamp + GRACE_PERIOD);
}
queued[txId] = false;
// prepare data
bytes memory data;
if (bytes(_func).length > 0) {
// data = func selector + _data
data = abi.encodePacked(bytes4(keccak256(bytes(_func))), _data);
} else {
// call fallback with data
data = _data;
}
// call target
(bool ok, bytes memory res) = _target.call{value: _value}(data);
if (!ok) {
revert TxFailedError();
}
emit Execute(txId, _target, _value, _func, _data, _timestamp);
return res;
}
function cancel(bytes32 _txId) external onlyOwner {
if (!queued[_txId]) {
revert NotQueuedError(_txId);
}
queued[_txId] = false;
emit Cancel(_txId);
}
}