-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWhitelisted.sol
80 lines (64 loc) · 2.52 KB
/
Whitelisted.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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.10;
/**
* @title Whitelisted contract
*
* @dev Contract module which provides a basic whitelist access controler
* mechanism where an account can be added to a whitelist that can be
* granted exclusive access to specific functions.
*
* Accounts in the whitelist are changeable through the internal
* _addToWhitelist and _removeFromWhitelist functions.
*
* This module is used through inheritance. It will make available the
* onlyWhitelisted modifier, which can be applied to your functions to
* restrict their use to the whitelisted accounts.
*
* @author byterocket
*/
abstract contract Whitelisted {
//--------------------------------------------------------------------------
// Errors
/// @notice Function is only callable by whitelisted address.
error OnlyCallableByWhitelistedAddress();
//--------------------------------------------------------------------------
// Events
/// @notice Event emitted when a new address is added to the whitelist.
event AddressAddedToWhitelist(address indexed who);
/// @notice Event emitted when an address is removed from the whitelist.
event AddressRemovedFromWhitelist(address indexed who);
//--------------------------------------------------------------------------
// Modifiers
/// @notice Modifier to guarantee function is only callable by whitelisted
/// addresses.
modifier onlyWhitelisted() {
if (!whitelist[msg.sender]) {
revert OnlyCallableByWhitelistedAddress();
}
_;
}
//--------------------------------------------------------------------------
// Storage
/// @notice The whitelisted address mapping.
mapping(address => bool) public whitelist;
//--------------------------------------------------------------------------
// Internal Mutating Functions
/// @dev Adds an address to the whitelist.
/// @param who The address to add to the whitelist.
function _addToWhitelist(address who) internal {
if (whitelist[who]) {
return;
}
whitelist[who] = true;
emit AddressAddedToWhitelist(who);
}
/// @dev Removes an address from the whitelist.
/// @param who The address to remove from the whitelist.
function _removeFromWhitelist(address who) internal {
if (!whitelist[who]) {
return;
}
whitelist[who] = false;
emit AddressRemovedFromWhitelist(who);
}
}