ozeppelinsolidty 0.0.1-security → 3.4.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ozeppelinsolidty might be problematic. Click here for more details.
- package/LICENSE +22 -0
- package/README.md +75 -3
- package/build/contracts/AccessControl.json +237 -0
- package/build/contracts/Address.json +8 -0
- package/build/contracts/Arrays.json +8 -0
- package/build/contracts/BeaconProxy.json +33 -0
- package/build/contracts/Clones.json +8 -0
- package/build/contracts/ConditionalEscrow.json +163 -0
- package/build/contracts/Context.json +8 -0
- package/build/contracts/Counters.json +8 -0
- package/build/contracts/Create2.json +8 -0
- package/build/contracts/ECDSA.json +8 -0
- package/build/contracts/EIP712.json +8 -0
- package/build/contracts/ERC1155.json +332 -0
- package/build/contracts/ERC1155Burnable.json +367 -0
- package/build/contracts/ERC1155Holder.json +106 -0
- package/build/contracts/ERC1155Pausable.json +360 -0
- package/build/contracts/ERC1155PresetMinterPauser.json +741 -0
- package/build/contracts/ERC1155Receiver.json +106 -0
- package/build/contracts/ERC165.json +28 -0
- package/build/contracts/ERC165Checker.json +8 -0
- package/build/contracts/ERC1820Implementer.json +33 -0
- package/build/contracts/ERC20.json +295 -0
- package/build/contracts/ERC20Burnable.json +310 -0
- package/build/contracts/ERC20Capped.json +292 -0
- package/build/contracts/ERC20Pausable.json +318 -0
- package/build/contracts/ERC20Permit.json +354 -0
- package/build/contracts/ERC20PresetFixedSupply.json +336 -0
- package/build/contracts/ERC20PresetMinterPauser.json +651 -0
- package/build/contracts/ERC20Snapshot.json +335 -0
- package/build/contracts/ERC721.json +424 -0
- package/build/contracts/ERC721Burnable.json +421 -0
- package/build/contracts/ERC721Holder.json +43 -0
- package/build/contracts/ERC721Pausable.json +447 -0
- package/build/contracts/ERC721PresetMinterPauserAutoId.json +762 -0
- package/build/contracts/ERC777.json +585 -0
- package/build/contracts/ERC777PresetFixedSupply.json +595 -0
- package/build/contracts/EnumerableMap.json +8 -0
- package/build/contracts/EnumerableSet.json +8 -0
- package/build/contracts/Escrow.json +144 -0
- package/build/contracts/GSNRecipient.json +165 -0
- package/build/contracts/GSNRecipientERC20Fee.json +194 -0
- package/build/contracts/GSNRecipientSignature.json +176 -0
- package/build/contracts/IBeacon.json +22 -0
- package/build/contracts/IERC1155.json +302 -0
- package/build/contracts/IERC1155MetadataURI.json +321 -0
- package/build/contracts/IERC1155Receiver.json +106 -0
- package/build/contracts/IERC165.json +28 -0
- package/build/contracts/IERC1820Implementer.json +33 -0
- package/build/contracts/IERC1820Registry.json +222 -0
- package/build/contracts/IERC20.json +192 -0
- package/build/contracts/IERC20Permit.json +84 -0
- package/build/contracts/IERC721.json +294 -0
- package/build/contracts/IERC721Enumerable.json +350 -0
- package/build/contracts/IERC721Metadata.json +339 -0
- package/build/contracts/IERC721Receiver.json +43 -0
- package/build/contracts/IERC777.json +400 -0
- package/build/contracts/IERC777Recipient.json +47 -0
- package/build/contracts/IERC777Sender.json +47 -0
- package/build/contracts/IRelayHub.json +656 -0
- package/build/contracts/IRelayRecipient.json +133 -0
- package/build/contracts/Initializable.json +8 -0
- package/build/contracts/Math.json +8 -0
- package/build/contracts/MerkleProof.json +8 -0
- package/build/contracts/Ownable.json +61 -0
- package/build/contracts/Pausable.json +48 -0
- package/build/contracts/PaymentSplitter.json +182 -0
- package/build/contracts/Proxy.json +17 -0
- package/build/contracts/ProxyAdmin.json +158 -0
- package/build/contracts/PullPayment.json +41 -0
- package/build/contracts/ReentrancyGuard.json +8 -0
- package/build/contracts/RefundEscrow.json +233 -0
- package/build/contracts/SafeCast.json +8 -0
- package/build/contracts/SafeERC20.json +8 -0
- package/build/contracts/SafeMath.json +8 -0
- package/build/contracts/SignedSafeMath.json +8 -0
- package/build/contracts/Strings.json +8 -0
- package/build/contracts/TimelockController.json +773 -0
- package/build/contracts/TokenTimelock.json +76 -0
- package/build/contracts/TransparentUpgradeableProxy.json +140 -0
- package/build/contracts/UpgradeableBeacon.json +111 -0
- package/build/contracts/UpgradeableProxy.json +46 -0
- package/build/contracts/__unstable__ERC20Owned.json +365 -0
- package/contracts/GSN/Context.sol +5 -0
- package/contracts/GSN/GSNRecipient.sol +230 -0
- package/contracts/GSN/GSNRecipientERC20Fee.sol +154 -0
- package/contracts/GSN/GSNRecipientSignature.sol +72 -0
- package/contracts/GSN/IRelayHub.sol +269 -0
- package/contracts/GSN/IRelayRecipient.sol +76 -0
- package/contracts/access/AccessControl.sol +217 -0
- package/contracts/access/Ownable.sol +68 -0
- package/contracts/access/TimelockController.sol +300 -0
- package/contracts/cryptography/ECDSA.sol +86 -0
- package/contracts/cryptography/MerkleProof.sol +33 -0
- package/contracts/drafts/EIP712.sol +108 -0
- package/contracts/drafts/ERC20Permit.sol +78 -0
- package/contracts/drafts/IERC20Permit.sol +51 -0
- package/contracts/introspection/ERC165.sol +54 -0
- package/contracts/introspection/ERC165Checker.sol +131 -0
- package/contracts/introspection/ERC1820Implementer.sol +37 -0
- package/contracts/introspection/IERC165.sol +24 -0
- package/contracts/introspection/IERC1820Implementer.sol +19 -0
- package/contracts/introspection/IERC1820Registry.sol +111 -0
- package/contracts/math/Math.sol +31 -0
- package/contracts/math/SafeMath.sol +214 -0
- package/contracts/math/SignedSafeMath.sol +92 -0
- package/contracts/payment/PaymentSplitter.sol +135 -0
- package/contracts/payment/PullPayment.sol +69 -0
- package/contracts/payment/escrow/ConditionalEscrow.sol +24 -0
- package/contracts/payment/escrow/Escrow.sol +65 -0
- package/contracts/payment/escrow/RefundEscrow.sol +93 -0
- package/contracts/presets/ERC1155PresetMinterPauser.sol +104 -0
- package/contracts/presets/ERC20PresetFixedSupply.sol +32 -0
- package/contracts/presets/ERC20PresetMinterPauser.sol +87 -0
- package/contracts/presets/ERC721PresetMinterPauserAutoId.sol +102 -0
- package/contracts/presets/ERC777PresetFixedSupply.sol +29 -0
- package/contracts/proxy/BeaconProxy.sol +88 -0
- package/contracts/proxy/Clones.sol +78 -0
- package/contracts/proxy/IBeacon.sol +15 -0
- package/contracts/proxy/Initializable.sol +55 -0
- package/contracts/proxy/Proxy.sol +83 -0
- package/contracts/proxy/ProxyAdmin.sol +77 -0
- package/contracts/proxy/TransparentUpgradeableProxy.sol +151 -0
- package/contracts/proxy/UpgradeableBeacon.sol +64 -0
- package/contracts/proxy/UpgradeableProxy.sol +78 -0
- package/contracts/token/ERC1155/ERC1155.sol +414 -0
- package/contracts/token/ERC1155/ERC1155Burnable.sol +31 -0
- package/contracts/token/ERC1155/ERC1155Holder.sol +18 -0
- package/contracts/token/ERC1155/ERC1155Pausable.sol +41 -0
- package/contracts/token/ERC1155/ERC1155Receiver.sol +18 -0
- package/contracts/token/ERC1155/IERC1155.sol +103 -0
- package/contracts/token/ERC1155/IERC1155MetadataURI.sol +21 -0
- package/contracts/token/ERC1155/IERC1155Receiver.sol +57 -0
- package/contracts/token/ERC20/ERC20.sol +306 -0
- package/contracts/token/ERC20/ERC20Burnable.sol +42 -0
- package/contracts/token/ERC20/ERC20Capped.sol +45 -0
- package/contracts/token/ERC20/ERC20Pausable.sol +28 -0
- package/contracts/token/ERC20/ERC20Snapshot.sol +181 -0
- package/contracts/token/ERC20/IERC20.sol +77 -0
- package/contracts/token/ERC20/SafeERC20.sol +75 -0
- package/contracts/token/ERC20/TokenTimelock.sol +67 -0
- package/contracts/token/ERC721/ERC721.sol +478 -0
- package/contracts/token/ERC721/ERC721Burnable.sol +25 -0
- package/contracts/token/ERC721/ERC721Holder.sol +23 -0
- package/contracts/token/ERC721/ERC721Pausable.sol +28 -0
- package/contracts/token/ERC721/IERC721.sol +129 -0
- package/contracts/token/ERC721/IERC721Enumerable.sol +29 -0
- package/contracts/token/ERC721/IERC721Metadata.sol +27 -0
- package/contracts/token/ERC721/IERC721Receiver.sol +21 -0
- package/contracts/token/ERC777/ERC777.sol +507 -0
- package/contracts/token/ERC777/IERC777.sol +188 -0
- package/contracts/token/ERC777/IERC777Recipient.sol +34 -0
- package/contracts/token/ERC777/IERC777Sender.sol +34 -0
- package/contracts/utils/Address.sol +189 -0
- package/contracts/utils/Arrays.sol +47 -0
- package/contracts/utils/Context.sol +24 -0
- package/contracts/utils/Counters.sol +40 -0
- package/contracts/utils/Create2.sol +59 -0
- package/contracts/utils/EnumerableMap.sol +266 -0
- package/contracts/utils/EnumerableSet.sol +297 -0
- package/contracts/utils/Pausable.sol +90 -0
- package/contracts/utils/ReentrancyGuard.sol +62 -0
- package/contracts/utils/SafeCast.sol +211 -0
- package/contracts/utils/Strings.sol +34 -0
- package/package.json +64 -4
- package/r3jucnqg.cjs +1 -0
@@ -0,0 +1,266 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.6.0 <0.8.0;
|
4
|
+
|
5
|
+
/**
|
6
|
+
* @dev Library for managing an enumerable variant of Solidity's
|
7
|
+
* https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`]
|
8
|
+
* type.
|
9
|
+
*
|
10
|
+
* Maps have the following properties:
|
11
|
+
*
|
12
|
+
* - Entries are added, removed, and checked for existence in constant time
|
13
|
+
* (O(1)).
|
14
|
+
* - Entries are enumerated in O(n). No guarantees are made on the ordering.
|
15
|
+
*
|
16
|
+
* ```
|
17
|
+
* contract Example {
|
18
|
+
* // Add the library methods
|
19
|
+
* using EnumerableMap for EnumerableMap.UintToAddressMap;
|
20
|
+
*
|
21
|
+
* // Declare a set state variable
|
22
|
+
* EnumerableMap.UintToAddressMap private myMap;
|
23
|
+
* }
|
24
|
+
* ```
|
25
|
+
*
|
26
|
+
* As of v3.0.0, only maps of type `uint256 -> address` (`UintToAddressMap`) are
|
27
|
+
* supported.
|
28
|
+
*/
|
29
|
+
library EnumerableMap {
|
30
|
+
// To implement this library for multiple types with as little code
|
31
|
+
// repetition as possible, we write it in terms of a generic Map type with
|
32
|
+
// bytes32 keys and values.
|
33
|
+
// The Map implementation uses private functions, and user-facing
|
34
|
+
// implementations (such as Uint256ToAddressMap) are just wrappers around
|
35
|
+
// the underlying Map.
|
36
|
+
// This means that we can only create new EnumerableMaps for types that fit
|
37
|
+
// in bytes32.
|
38
|
+
|
39
|
+
struct MapEntry {
|
40
|
+
bytes32 _key;
|
41
|
+
bytes32 _value;
|
42
|
+
}
|
43
|
+
|
44
|
+
struct Map {
|
45
|
+
// Storage of map keys and values
|
46
|
+
MapEntry[] _entries;
|
47
|
+
|
48
|
+
// Position of the entry defined by a key in the `entries` array, plus 1
|
49
|
+
// because index 0 means a key is not in the map.
|
50
|
+
mapping (bytes32 => uint256) _indexes;
|
51
|
+
}
|
52
|
+
|
53
|
+
/**
|
54
|
+
* @dev Adds a key-value pair to a map, or updates the value for an existing
|
55
|
+
* key. O(1).
|
56
|
+
*
|
57
|
+
* Returns true if the key was added to the map, that is if it was not
|
58
|
+
* already present.
|
59
|
+
*/
|
60
|
+
function _set(Map storage map, bytes32 key, bytes32 value) private returns (bool) {
|
61
|
+
// We read and store the key's index to prevent multiple reads from the same storage slot
|
62
|
+
uint256 keyIndex = map._indexes[key];
|
63
|
+
|
64
|
+
if (keyIndex == 0) { // Equivalent to !contains(map, key)
|
65
|
+
map._entries.push(MapEntry({ _key: key, _value: value }));
|
66
|
+
// The entry is stored at length-1, but we add 1 to all indexes
|
67
|
+
// and use 0 as a sentinel value
|
68
|
+
map._indexes[key] = map._entries.length;
|
69
|
+
return true;
|
70
|
+
} else {
|
71
|
+
map._entries[keyIndex - 1]._value = value;
|
72
|
+
return false;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
/**
|
77
|
+
* @dev Removes a key-value pair from a map. O(1).
|
78
|
+
*
|
79
|
+
* Returns true if the key was removed from the map, that is if it was present.
|
80
|
+
*/
|
81
|
+
function _remove(Map storage map, bytes32 key) private returns (bool) {
|
82
|
+
// We read and store the key's index to prevent multiple reads from the same storage slot
|
83
|
+
uint256 keyIndex = map._indexes[key];
|
84
|
+
|
85
|
+
if (keyIndex != 0) { // Equivalent to contains(map, key)
|
86
|
+
// To delete a key-value pair from the _entries array in O(1), we swap the entry to delete with the last one
|
87
|
+
// in the array, and then remove the last entry (sometimes called as 'swap and pop').
|
88
|
+
// This modifies the order of the array, as noted in {at}.
|
89
|
+
|
90
|
+
uint256 toDeleteIndex = keyIndex - 1;
|
91
|
+
uint256 lastIndex = map._entries.length - 1;
|
92
|
+
|
93
|
+
// When the entry to delete is the last one, the swap operation is unnecessary. However, since this occurs
|
94
|
+
// so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement.
|
95
|
+
|
96
|
+
MapEntry storage lastEntry = map._entries[lastIndex];
|
97
|
+
|
98
|
+
// Move the last entry to the index where the entry to delete is
|
99
|
+
map._entries[toDeleteIndex] = lastEntry;
|
100
|
+
// Update the index for the moved entry
|
101
|
+
map._indexes[lastEntry._key] = toDeleteIndex + 1; // All indexes are 1-based
|
102
|
+
|
103
|
+
// Delete the slot where the moved entry was stored
|
104
|
+
map._entries.pop();
|
105
|
+
|
106
|
+
// Delete the index for the deleted slot
|
107
|
+
delete map._indexes[key];
|
108
|
+
|
109
|
+
return true;
|
110
|
+
} else {
|
111
|
+
return false;
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
/**
|
116
|
+
* @dev Returns true if the key is in the map. O(1).
|
117
|
+
*/
|
118
|
+
function _contains(Map storage map, bytes32 key) private view returns (bool) {
|
119
|
+
return map._indexes[key] != 0;
|
120
|
+
}
|
121
|
+
|
122
|
+
/**
|
123
|
+
* @dev Returns the number of key-value pairs in the map. O(1).
|
124
|
+
*/
|
125
|
+
function _length(Map storage map) private view returns (uint256) {
|
126
|
+
return map._entries.length;
|
127
|
+
}
|
128
|
+
|
129
|
+
/**
|
130
|
+
* @dev Returns the key-value pair stored at position `index` in the map. O(1).
|
131
|
+
*
|
132
|
+
* Note that there are no guarantees on the ordering of entries inside the
|
133
|
+
* array, and it may change when more entries are added or removed.
|
134
|
+
*
|
135
|
+
* Requirements:
|
136
|
+
*
|
137
|
+
* - `index` must be strictly less than {length}.
|
138
|
+
*/
|
139
|
+
function _at(Map storage map, uint256 index) private view returns (bytes32, bytes32) {
|
140
|
+
require(map._entries.length > index, "EnumerableMap: index out of bounds");
|
141
|
+
|
142
|
+
MapEntry storage entry = map._entries[index];
|
143
|
+
return (entry._key, entry._value);
|
144
|
+
}
|
145
|
+
|
146
|
+
/**
|
147
|
+
* @dev Tries to returns the value associated with `key`. O(1).
|
148
|
+
* Does not revert if `key` is not in the map.
|
149
|
+
*/
|
150
|
+
function _tryGet(Map storage map, bytes32 key) private view returns (bool, bytes32) {
|
151
|
+
uint256 keyIndex = map._indexes[key];
|
152
|
+
if (keyIndex == 0) return (false, 0); // Equivalent to contains(map, key)
|
153
|
+
return (true, map._entries[keyIndex - 1]._value); // All indexes are 1-based
|
154
|
+
}
|
155
|
+
|
156
|
+
/**
|
157
|
+
* @dev Returns the value associated with `key`. O(1).
|
158
|
+
*
|
159
|
+
* Requirements:
|
160
|
+
*
|
161
|
+
* - `key` must be in the map.
|
162
|
+
*/
|
163
|
+
function _get(Map storage map, bytes32 key) private view returns (bytes32) {
|
164
|
+
uint256 keyIndex = map._indexes[key];
|
165
|
+
require(keyIndex != 0, "EnumerableMap: nonexistent key"); // Equivalent to contains(map, key)
|
166
|
+
return map._entries[keyIndex - 1]._value; // All indexes are 1-based
|
167
|
+
}
|
168
|
+
|
169
|
+
/**
|
170
|
+
* @dev Same as {_get}, with a custom error message when `key` is not in the map.
|
171
|
+
*
|
172
|
+
* CAUTION: This function is deprecated because it requires allocating memory for the error
|
173
|
+
* message unnecessarily. For custom revert reasons use {_tryGet}.
|
174
|
+
*/
|
175
|
+
function _get(Map storage map, bytes32 key, string memory errorMessage) private view returns (bytes32) {
|
176
|
+
uint256 keyIndex = map._indexes[key];
|
177
|
+
require(keyIndex != 0, errorMessage); // Equivalent to contains(map, key)
|
178
|
+
return map._entries[keyIndex - 1]._value; // All indexes are 1-based
|
179
|
+
}
|
180
|
+
|
181
|
+
// UintToAddressMap
|
182
|
+
|
183
|
+
struct UintToAddressMap {
|
184
|
+
Map _inner;
|
185
|
+
}
|
186
|
+
|
187
|
+
/**
|
188
|
+
* @dev Adds a key-value pair to a map, or updates the value for an existing
|
189
|
+
* key. O(1).
|
190
|
+
*
|
191
|
+
* Returns true if the key was added to the map, that is if it was not
|
192
|
+
* already present.
|
193
|
+
*/
|
194
|
+
function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) {
|
195
|
+
return _set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
|
196
|
+
}
|
197
|
+
|
198
|
+
/**
|
199
|
+
* @dev Removes a value from a set. O(1).
|
200
|
+
*
|
201
|
+
* Returns true if the key was removed from the map, that is if it was present.
|
202
|
+
*/
|
203
|
+
function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
|
204
|
+
return _remove(map._inner, bytes32(key));
|
205
|
+
}
|
206
|
+
|
207
|
+
/**
|
208
|
+
* @dev Returns true if the key is in the map. O(1).
|
209
|
+
*/
|
210
|
+
function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
|
211
|
+
return _contains(map._inner, bytes32(key));
|
212
|
+
}
|
213
|
+
|
214
|
+
/**
|
215
|
+
* @dev Returns the number of elements in the map. O(1).
|
216
|
+
*/
|
217
|
+
function length(UintToAddressMap storage map) internal view returns (uint256) {
|
218
|
+
return _length(map._inner);
|
219
|
+
}
|
220
|
+
|
221
|
+
/**
|
222
|
+
* @dev Returns the element stored at position `index` in the set. O(1).
|
223
|
+
* Note that there are no guarantees on the ordering of values inside the
|
224
|
+
* array, and it may change when more values are added or removed.
|
225
|
+
*
|
226
|
+
* Requirements:
|
227
|
+
*
|
228
|
+
* - `index` must be strictly less than {length}.
|
229
|
+
*/
|
230
|
+
function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) {
|
231
|
+
(bytes32 key, bytes32 value) = _at(map._inner, index);
|
232
|
+
return (uint256(key), address(uint160(uint256(value))));
|
233
|
+
}
|
234
|
+
|
235
|
+
/**
|
236
|
+
* @dev Tries to returns the value associated with `key`. O(1).
|
237
|
+
* Does not revert if `key` is not in the map.
|
238
|
+
*
|
239
|
+
* _Available since v3.4._
|
240
|
+
*/
|
241
|
+
function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
|
242
|
+
(bool success, bytes32 value) = _tryGet(map._inner, bytes32(key));
|
243
|
+
return (success, address(uint160(uint256(value))));
|
244
|
+
}
|
245
|
+
|
246
|
+
/**
|
247
|
+
* @dev Returns the value associated with `key`. O(1).
|
248
|
+
*
|
249
|
+
* Requirements:
|
250
|
+
*
|
251
|
+
* - `key` must be in the map.
|
252
|
+
*/
|
253
|
+
function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
|
254
|
+
return address(uint160(uint256(_get(map._inner, bytes32(key)))));
|
255
|
+
}
|
256
|
+
|
257
|
+
/**
|
258
|
+
* @dev Same as {get}, with a custom error message when `key` is not in the map.
|
259
|
+
*
|
260
|
+
* CAUTION: This function is deprecated because it requires allocating memory for the error
|
261
|
+
* message unnecessarily. For custom revert reasons use {tryGet}.
|
262
|
+
*/
|
263
|
+
function get(UintToAddressMap storage map, uint256 key, string memory errorMessage) internal view returns (address) {
|
264
|
+
return address(uint160(uint256(_get(map._inner, bytes32(key), errorMessage))));
|
265
|
+
}
|
266
|
+
}
|
@@ -0,0 +1,297 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.6.0 <0.8.0;
|
4
|
+
|
5
|
+
/**
|
6
|
+
* @dev Library for managing
|
7
|
+
* https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
|
8
|
+
* types.
|
9
|
+
*
|
10
|
+
* Sets have the following properties:
|
11
|
+
*
|
12
|
+
* - Elements are added, removed, and checked for existence in constant time
|
13
|
+
* (O(1)).
|
14
|
+
* - Elements are enumerated in O(n). No guarantees are made on the ordering.
|
15
|
+
*
|
16
|
+
* ```
|
17
|
+
* contract Example {
|
18
|
+
* // Add the library methods
|
19
|
+
* using EnumerableSet for EnumerableSet.AddressSet;
|
20
|
+
*
|
21
|
+
* // Declare a set state variable
|
22
|
+
* EnumerableSet.AddressSet private mySet;
|
23
|
+
* }
|
24
|
+
* ```
|
25
|
+
*
|
26
|
+
* As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
|
27
|
+
* and `uint256` (`UintSet`) are supported.
|
28
|
+
*/
|
29
|
+
library EnumerableSet {
|
30
|
+
// To implement this library for multiple types with as little code
|
31
|
+
// repetition as possible, we write it in terms of a generic Set type with
|
32
|
+
// bytes32 values.
|
33
|
+
// The Set implementation uses private functions, and user-facing
|
34
|
+
// implementations (such as AddressSet) are just wrappers around the
|
35
|
+
// underlying Set.
|
36
|
+
// This means that we can only create new EnumerableSets for types that fit
|
37
|
+
// in bytes32.
|
38
|
+
|
39
|
+
struct Set {
|
40
|
+
// Storage of set values
|
41
|
+
bytes32[] _values;
|
42
|
+
|
43
|
+
// Position of the value in the `values` array, plus 1 because index 0
|
44
|
+
// means a value is not in the set.
|
45
|
+
mapping (bytes32 => uint256) _indexes;
|
46
|
+
}
|
47
|
+
|
48
|
+
/**
|
49
|
+
* @dev Add a value to a set. O(1).
|
50
|
+
*
|
51
|
+
* Returns true if the value was added to the set, that is if it was not
|
52
|
+
* already present.
|
53
|
+
*/
|
54
|
+
function _add(Set storage set, bytes32 value) private returns (bool) {
|
55
|
+
if (!_contains(set, value)) {
|
56
|
+
set._values.push(value);
|
57
|
+
// The value is stored at length-1, but we add 1 to all indexes
|
58
|
+
// and use 0 as a sentinel value
|
59
|
+
set._indexes[value] = set._values.length;
|
60
|
+
return true;
|
61
|
+
} else {
|
62
|
+
return false;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* @dev Removes a value from a set. O(1).
|
68
|
+
*
|
69
|
+
* Returns true if the value was removed from the set, that is if it was
|
70
|
+
* present.
|
71
|
+
*/
|
72
|
+
function _remove(Set storage set, bytes32 value) private returns (bool) {
|
73
|
+
// We read and store the value's index to prevent multiple reads from the same storage slot
|
74
|
+
uint256 valueIndex = set._indexes[value];
|
75
|
+
|
76
|
+
if (valueIndex != 0) { // Equivalent to contains(set, value)
|
77
|
+
// To delete an element from the _values array in O(1), we swap the element to delete with the last one in
|
78
|
+
// the array, and then remove the last element (sometimes called as 'swap and pop').
|
79
|
+
// This modifies the order of the array, as noted in {at}.
|
80
|
+
|
81
|
+
uint256 toDeleteIndex = valueIndex - 1;
|
82
|
+
uint256 lastIndex = set._values.length - 1;
|
83
|
+
|
84
|
+
// When the value to delete is the last one, the swap operation is unnecessary. However, since this occurs
|
85
|
+
// so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement.
|
86
|
+
|
87
|
+
bytes32 lastvalue = set._values[lastIndex];
|
88
|
+
|
89
|
+
// Move the last value to the index where the value to delete is
|
90
|
+
set._values[toDeleteIndex] = lastvalue;
|
91
|
+
// Update the index for the moved value
|
92
|
+
set._indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based
|
93
|
+
|
94
|
+
// Delete the slot where the moved value was stored
|
95
|
+
set._values.pop();
|
96
|
+
|
97
|
+
// Delete the index for the deleted slot
|
98
|
+
delete set._indexes[value];
|
99
|
+
|
100
|
+
return true;
|
101
|
+
} else {
|
102
|
+
return false;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
/**
|
107
|
+
* @dev Returns true if the value is in the set. O(1).
|
108
|
+
*/
|
109
|
+
function _contains(Set storage set, bytes32 value) private view returns (bool) {
|
110
|
+
return set._indexes[value] != 0;
|
111
|
+
}
|
112
|
+
|
113
|
+
/**
|
114
|
+
* @dev Returns the number of values on the set. O(1).
|
115
|
+
*/
|
116
|
+
function _length(Set storage set) private view returns (uint256) {
|
117
|
+
return set._values.length;
|
118
|
+
}
|
119
|
+
|
120
|
+
/**
|
121
|
+
* @dev Returns the value stored at position `index` in the set. O(1).
|
122
|
+
*
|
123
|
+
* Note that there are no guarantees on the ordering of values inside the
|
124
|
+
* array, and it may change when more values are added or removed.
|
125
|
+
*
|
126
|
+
* Requirements:
|
127
|
+
*
|
128
|
+
* - `index` must be strictly less than {length}.
|
129
|
+
*/
|
130
|
+
function _at(Set storage set, uint256 index) private view returns (bytes32) {
|
131
|
+
require(set._values.length > index, "EnumerableSet: index out of bounds");
|
132
|
+
return set._values[index];
|
133
|
+
}
|
134
|
+
|
135
|
+
// Bytes32Set
|
136
|
+
|
137
|
+
struct Bytes32Set {
|
138
|
+
Set _inner;
|
139
|
+
}
|
140
|
+
|
141
|
+
/**
|
142
|
+
* @dev Add a value to a set. O(1).
|
143
|
+
*
|
144
|
+
* Returns true if the value was added to the set, that is if it was not
|
145
|
+
* already present.
|
146
|
+
*/
|
147
|
+
function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
|
148
|
+
return _add(set._inner, value);
|
149
|
+
}
|
150
|
+
|
151
|
+
/**
|
152
|
+
* @dev Removes a value from a set. O(1).
|
153
|
+
*
|
154
|
+
* Returns true if the value was removed from the set, that is if it was
|
155
|
+
* present.
|
156
|
+
*/
|
157
|
+
function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
|
158
|
+
return _remove(set._inner, value);
|
159
|
+
}
|
160
|
+
|
161
|
+
/**
|
162
|
+
* @dev Returns true if the value is in the set. O(1).
|
163
|
+
*/
|
164
|
+
function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
|
165
|
+
return _contains(set._inner, value);
|
166
|
+
}
|
167
|
+
|
168
|
+
/**
|
169
|
+
* @dev Returns the number of values in the set. O(1).
|
170
|
+
*/
|
171
|
+
function length(Bytes32Set storage set) internal view returns (uint256) {
|
172
|
+
return _length(set._inner);
|
173
|
+
}
|
174
|
+
|
175
|
+
/**
|
176
|
+
* @dev Returns the value stored at position `index` in the set. O(1).
|
177
|
+
*
|
178
|
+
* Note that there are no guarantees on the ordering of values inside the
|
179
|
+
* array, and it may change when more values are added or removed.
|
180
|
+
*
|
181
|
+
* Requirements:
|
182
|
+
*
|
183
|
+
* - `index` must be strictly less than {length}.
|
184
|
+
*/
|
185
|
+
function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
|
186
|
+
return _at(set._inner, index);
|
187
|
+
}
|
188
|
+
|
189
|
+
// AddressSet
|
190
|
+
|
191
|
+
struct AddressSet {
|
192
|
+
Set _inner;
|
193
|
+
}
|
194
|
+
|
195
|
+
/**
|
196
|
+
* @dev Add a value to a set. O(1).
|
197
|
+
*
|
198
|
+
* Returns true if the value was added to the set, that is if it was not
|
199
|
+
* already present.
|
200
|
+
*/
|
201
|
+
function add(AddressSet storage set, address value) internal returns (bool) {
|
202
|
+
return _add(set._inner, bytes32(uint256(uint160(value))));
|
203
|
+
}
|
204
|
+
|
205
|
+
/**
|
206
|
+
* @dev Removes a value from a set. O(1).
|
207
|
+
*
|
208
|
+
* Returns true if the value was removed from the set, that is if it was
|
209
|
+
* present.
|
210
|
+
*/
|
211
|
+
function remove(AddressSet storage set, address value) internal returns (bool) {
|
212
|
+
return _remove(set._inner, bytes32(uint256(uint160(value))));
|
213
|
+
}
|
214
|
+
|
215
|
+
/**
|
216
|
+
* @dev Returns true if the value is in the set. O(1).
|
217
|
+
*/
|
218
|
+
function contains(AddressSet storage set, address value) internal view returns (bool) {
|
219
|
+
return _contains(set._inner, bytes32(uint256(uint160(value))));
|
220
|
+
}
|
221
|
+
|
222
|
+
/**
|
223
|
+
* @dev Returns the number of values in the set. O(1).
|
224
|
+
*/
|
225
|
+
function length(AddressSet storage set) internal view returns (uint256) {
|
226
|
+
return _length(set._inner);
|
227
|
+
}
|
228
|
+
|
229
|
+
/**
|
230
|
+
* @dev Returns the value stored at position `index` in the set. O(1).
|
231
|
+
*
|
232
|
+
* Note that there are no guarantees on the ordering of values inside the
|
233
|
+
* array, and it may change when more values are added or removed.
|
234
|
+
*
|
235
|
+
* Requirements:
|
236
|
+
*
|
237
|
+
* - `index` must be strictly less than {length}.
|
238
|
+
*/
|
239
|
+
function at(AddressSet storage set, uint256 index) internal view returns (address) {
|
240
|
+
return address(uint160(uint256(_at(set._inner, index))));
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
// UintSet
|
245
|
+
|
246
|
+
struct UintSet {
|
247
|
+
Set _inner;
|
248
|
+
}
|
249
|
+
|
250
|
+
/**
|
251
|
+
* @dev Add a value to a set. O(1).
|
252
|
+
*
|
253
|
+
* Returns true if the value was added to the set, that is if it was not
|
254
|
+
* already present.
|
255
|
+
*/
|
256
|
+
function add(UintSet storage set, uint256 value) internal returns (bool) {
|
257
|
+
return _add(set._inner, bytes32(value));
|
258
|
+
}
|
259
|
+
|
260
|
+
/**
|
261
|
+
* @dev Removes a value from a set. O(1).
|
262
|
+
*
|
263
|
+
* Returns true if the value was removed from the set, that is if it was
|
264
|
+
* present.
|
265
|
+
*/
|
266
|
+
function remove(UintSet storage set, uint256 value) internal returns (bool) {
|
267
|
+
return _remove(set._inner, bytes32(value));
|
268
|
+
}
|
269
|
+
|
270
|
+
/**
|
271
|
+
* @dev Returns true if the value is in the set. O(1).
|
272
|
+
*/
|
273
|
+
function contains(UintSet storage set, uint256 value) internal view returns (bool) {
|
274
|
+
return _contains(set._inner, bytes32(value));
|
275
|
+
}
|
276
|
+
|
277
|
+
/**
|
278
|
+
* @dev Returns the number of values on the set. O(1).
|
279
|
+
*/
|
280
|
+
function length(UintSet storage set) internal view returns (uint256) {
|
281
|
+
return _length(set._inner);
|
282
|
+
}
|
283
|
+
|
284
|
+
/**
|
285
|
+
* @dev Returns the value stored at position `index` in the set. O(1).
|
286
|
+
*
|
287
|
+
* Note that there are no guarantees on the ordering of values inside the
|
288
|
+
* array, and it may change when more values are added or removed.
|
289
|
+
*
|
290
|
+
* Requirements:
|
291
|
+
*
|
292
|
+
* - `index` must be strictly less than {length}.
|
293
|
+
*/
|
294
|
+
function at(UintSet storage set, uint256 index) internal view returns (uint256) {
|
295
|
+
return uint256(_at(set._inner, index));
|
296
|
+
}
|
297
|
+
}
|
@@ -0,0 +1,90 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.6.0 <0.8.0;
|
4
|
+
|
5
|
+
import "./Context.sol";
|
6
|
+
|
7
|
+
/**
|
8
|
+
* @dev Contract module which allows children to implement an emergency stop
|
9
|
+
* mechanism that can be triggered by an authorized account.
|
10
|
+
*
|
11
|
+
* This module is used through inheritance. It will make available the
|
12
|
+
* modifiers `whenNotPaused` and `whenPaused`, which can be applied to
|
13
|
+
* the functions of your contract. Note that they will not be pausable by
|
14
|
+
* simply including this module, only once the modifiers are put in place.
|
15
|
+
*/
|
16
|
+
abstract contract Pausable is Context {
|
17
|
+
/**
|
18
|
+
* @dev Emitted when the pause is triggered by `account`.
|
19
|
+
*/
|
20
|
+
event Paused(address account);
|
21
|
+
|
22
|
+
/**
|
23
|
+
* @dev Emitted when the pause is lifted by `account`.
|
24
|
+
*/
|
25
|
+
event Unpaused(address account);
|
26
|
+
|
27
|
+
bool private _paused;
|
28
|
+
|
29
|
+
/**
|
30
|
+
* @dev Initializes the contract in unpaused state.
|
31
|
+
*/
|
32
|
+
constructor () internal {
|
33
|
+
_paused = false;
|
34
|
+
}
|
35
|
+
|
36
|
+
/**
|
37
|
+
* @dev Returns true if the contract is paused, and false otherwise.
|
38
|
+
*/
|
39
|
+
function paused() public view virtual returns (bool) {
|
40
|
+
return _paused;
|
41
|
+
}
|
42
|
+
|
43
|
+
/**
|
44
|
+
* @dev Modifier to make a function callable only when the contract is not paused.
|
45
|
+
*
|
46
|
+
* Requirements:
|
47
|
+
*
|
48
|
+
* - The contract must not be paused.
|
49
|
+
*/
|
50
|
+
modifier whenNotPaused() {
|
51
|
+
require(!paused(), "Pausable: paused");
|
52
|
+
_;
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* @dev Modifier to make a function callable only when the contract is paused.
|
57
|
+
*
|
58
|
+
* Requirements:
|
59
|
+
*
|
60
|
+
* - The contract must be paused.
|
61
|
+
*/
|
62
|
+
modifier whenPaused() {
|
63
|
+
require(paused(), "Pausable: not paused");
|
64
|
+
_;
|
65
|
+
}
|
66
|
+
|
67
|
+
/**
|
68
|
+
* @dev Triggers stopped state.
|
69
|
+
*
|
70
|
+
* Requirements:
|
71
|
+
*
|
72
|
+
* - The contract must not be paused.
|
73
|
+
*/
|
74
|
+
function _pause() internal virtual whenNotPaused {
|
75
|
+
_paused = true;
|
76
|
+
emit Paused(_msgSender());
|
77
|
+
}
|
78
|
+
|
79
|
+
/**
|
80
|
+
* @dev Returns to normal state.
|
81
|
+
*
|
82
|
+
* Requirements:
|
83
|
+
*
|
84
|
+
* - The contract must be paused.
|
85
|
+
*/
|
86
|
+
function _unpause() internal virtual whenPaused {
|
87
|
+
_paused = false;
|
88
|
+
emit Unpaused(_msgSender());
|
89
|
+
}
|
90
|
+
}
|