-
Notifications
You must be signed in to change notification settings - Fork 4
/
DistributorRole.sol
54 lines (43 loc) · 1.76 KB
/
DistributorRole.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
pragma solidity ^0.4.24;
// Import the library 'Roles'
import "./Roles.sol";
// Define a contract 'DistributorRole' to manage this role - add, remove, check
contract DistributorRole {
using Roles for Roles.Role;
// Define 2 events, one for Adding, and other for Removing
event DistributorAdded(address indexed account);
event DistributorRemoved(address indexed account);
// Define a struct 'distributors' by inheriting from 'Roles' library, struct Role
Roles.Role private distributors;
// In the constructor make the address that deploys this contract the 1st distributor
constructor() public {
_addDistributor(msg.sender);
}
// Define a modifier that checks to see if msg.sender has the appropriate role
modifier onlyDistributor() {
require(isDistributor(msg.sender), "sender is not a distibutor");
_;
}
// Define a function 'isDistributor' to check this role
function isDistributor(address account) public view returns (bool) {
return distributors.has(account);
}
// Define a function 'addDistributor' that adds this role
function addDistributor(address account) public onlyDistributor {
_addDistributor(account);
}
// Define a function 'renounceDistributor' to renounce this role
function renounceDistributor() public {
_removeDistributor(msg.sender);
}
// Define an internal function '_addDistributor' to add this role, called by 'addDistributor'
function _addDistributor(address account) internal {
distributors.add(account);
emit DistributorAdded(account);
}
// Define an internal function '_removeDistributor' to remove this role, called by 'removeDistributor'
function _removeDistributor(address account) internal {
distributors.remove(account);
emit DistributorRemoved(account);(account);
}
}