-
Notifications
You must be signed in to change notification settings - Fork 27
/
Erasure_Users.sol
75 lines (52 loc) · 2.08 KB
/
Erasure_Users.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
pragma solidity ^0.5.0;
import "../helpers/HitchensUnorderedAddressSetLib.sol";
contract Erasure_Users {
using HitchensUnorderedAddressSetLib for HitchensUnorderedAddressSetLib.Set;
HitchensUnorderedAddressSetLib.Set private _users;
mapping (address => bytes) private _metadata;
event UserRegistered(address indexed user, bytes data);
event UserRemoved(address indexed user);
// state functions
function registerUser(bytes memory data) public {
require(!_users.exists(msg.sender), "user already exists");
// add user
_users.insert(msg.sender);
// set metadata
_metadata[msg.sender] = data;
// emit event
emit UserRegistered(msg.sender, data);
}
function removeUser() public {
// require user is registered
require(_users.exists(msg.sender), "user does not exist");
// remove user
_users.remove(msg.sender);
// delete metadata
delete _metadata[msg.sender];
// emit event
emit UserRemoved(msg.sender);
}
// view functions
function getUserData(address user) public view returns (bytes memory data) {
data = _metadata[user];
}
function getUsers() public view returns (address[] memory users) {
users = _users.keyList;
}
function getUserCount() public view returns (uint256 count) {
count = _users.count();
}
// Note: startIndex is inclusive, endIndex exclusive
function getPaginatedUsers(uint256 startIndex, uint256 endIndex) public view returns (address[] memory users) {
require(startIndex < endIndex, "startIndex must be less than endIndex");
require(endIndex <= _users.count(), "end index out of range");
// initialize fixed size memory array
address[] memory range = new address[](endIndex - startIndex);
// Populate array with addresses in range
for (uint256 i = startIndex; i < endIndex; i++) {
range[i - startIndex] = _users.keyAtIndex(i);
}
// return array of addresses
users = range;
}
}