forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Timers.sol
104 lines (78 loc) · 3.06 KB
/
Timers.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
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.3.2 (utils/Timers.sol)
pragma solidity ^0.8.8;
/**
* @dev Tooling for timepoints, timers and delays
*/
library Timers {
enum Type { TIMESTAMP, BLOCKNUMBER }
type Timer is uint64;
function toTimer(uint64 timer, Type typeSelector) internal pure returns (Timer) {
require(timer >> 63 == 0, 'Timer only support 63 bits values');
return Timer.wrap(uint8(typeSelector) << 63 | timer);
}
function getType(Timer timer) internal pure returns (Type) {
return Type(Timer.unwrap(timer) >> 63);
}
function getValue(Timer timer) internal pure returns (uint64) {
return Timer.unwrap(timer) & (type(uint64).max >> 1);
}
function isUnset(Timer timer) internal pure returns (bool) {
return getValue(timer) == 0;
}
function isStarted(Timer timer) internal pure returns (bool) {
return getValue(timer) > 0;
}
function isPending(Timer timer) internal view returns (bool) {
return getType(timer) == Type.TIMESTAMP
? getValue(timer) > block.timestamp
: getValue(timer) > block.number;
}
function isExpired(Timer timer) internal view returns (bool) {
return isStarted(timer) && !isPending(timer);
}
function unset() internal pure returns (Timer) {
return Timer.wrap(0);
}
function never() internal pure returns (Timer) {
return Timer.wrap(type(uint64).max);
}
type Timestamp is uint64;
function toTimestamp(uint64 timer) internal pure returns (Timestamp) {
return Timestamp.wrap(timer);
}
function toUint64(Timestamp timer) internal pure returns (uint64) {
return Timestamp.unwrap(timer);
}
function isUnset(Timestamp timer) internal pure returns (bool) {
return Timestamp.unwrap(timer) == 0;
}
function isStarted(Timestamp timer) internal pure returns (bool) {
return Timestamp.unwrap(timer) > 0;
}
function isPending(Timestamp timer) internal view returns (bool) {
return Timestamp.unwrap(timer) > block.timestamp;
}
function isExpired(Timestamp timer) internal view returns (bool) {
return isStarted(timer) && Timestamp.unwrap(timer) <= block.timestamp;
}
type BlockNumber is uint64;
function toBlockNumber(uint64 timer) internal pure returns (BlockNumber) {
return BlockNumber.wrap(timer);
}
function toUint64(BlockNumber timer) internal pure returns (uint64) {
return BlockNumber.unwrap(timer);
}
function isUnset(BlockNumber timer) internal pure returns (bool) {
return BlockNumber.unwrap(timer) == 0;
}
function isStarted(BlockNumber timer) internal pure returns (bool) {
return BlockNumber.unwrap(timer) > 0;
}
function isPending(BlockNumber timer) internal view returns (bool) {
return BlockNumber.unwrap(timer) > block.number;
}
function isExpired(BlockNumber timer) internal view returns (bool) {
return isStarted(timer) && BlockNumber.unwrap(timer) <= block.number;
}
}