This repository has been archived by the owner on Jan 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStatus.sol
91 lines (69 loc) · 1.7 KB
/
Status.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
pragma solidity ^0.4.21;
library Status {
enum Category {
Generic,
Permission,
Match,
Offer,
Availability,
x50,
x60,
x70,
x80,
x90,
AppCategory,
xB0,
xC0,
xD0,
Cryptography,
OffChain
}
enum Reason {
Failure,
Success,
Acceptance,
Before,
ActionRequired,
Expired,
x06,
x07,
x08,
x09,
AppReason,
x0B,
x0C,
x0D,
x0E,
Info
}
function toCode(Category _category, Reason _reason) public pure returns (byte code) {
return toCode(uint(_category), uint(_reason));
}
function toCode(uint _category, uint _reason) public pure returns (byte code) {
return byte((_category << 4) + _reason);
}
function appCode(uint _appReason) public pure returns (byte code) {
return byte(160 + _appReason);
}
// Get nibbles
function categoryOf(byte _status) public pure returns (uint category) {
return uint(_status >> 4);
}
function reasonOf(byte _status) public pure returns (uint reason) {
return uint(_status & hex"0F");
}
// Check common statuses
function isFailure(byte _status) public pure returns (bool) {
return reasonOf(_status) == 0;
}
function isOk(byte _status) public pure returns (bool) {
return reasonOf(_status) == 1;
}
// `require`s
function requireOk(byte _status) public pure {
require(isOk(_status));
}
function requireOk(byte _status, string message) public pure {
require(isOk(_status), message);
}
}