/
TimeLock.sol
77 lines (59 loc) · 1.92 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {TimeLockCommon} from "./TimeLockCommon.sol";
abstract contract TimeLock is TimeLockCommon {
uint256 private _timeLockDuration;
mapping(address user => uint256 timestamp) private _timeLocks;
error LockedUser(address user);
event DurationChanged(
uint256 previousDuration,
uint256 newDuration
);
event UserLockTimeChanged(
address indexed user,
uint256 timestamp
);
constructor(uint256 timeLockDuration) {
_setDuration(timeLockDuration);
}
modifier timeLocked() {
address caller = msg.sender;
if (isLocked(caller)) {
revert LockedUser(caller);
}
_lock(caller);
_;
}
function duration() public view returns (uint256) {
return _timeLockDuration;
}
function releaseTime(address user) public view returns (uint256) {
return _timeLocks[user];
}
function lockTimeRemaining(address user) public view returns (uint256) {
uint256 userLockTime = releaseTime(user);
uint256 timeNow = _now();
if (timeNow >= userLockTime) {
return 0;
}
return userLockTime - timeNow;
}
function isLocked(address user) public view returns (bool) {
return _now() < releaseTime(user);
}
function _lock(address user) internal {
_setUserLockTime(user, _now() + _timeLockDuration);
}
function _setDuration(uint256 newDuration) internal {
uint256 previousDuration = _timeLockDuration;
_timeLockDuration = newDuration;
emit DurationChanged(previousDuration, newDuration);
}
function _clear(address user) internal {
_setUserLockTime(user, 0);
}
function _setUserLockTime(address user, uint256 timestamp) internal {
_timeLocks[user] = timestamp;
emit UserLockTimeChanged(user, timestamp);
}
}