This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfriendship.sol
48 lines (42 loc) · 1.54 KB
/
friendship.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
pragma solidity ^0.5.0;
contract Friendship {
mapping(address => address[]) internal friends;
mapping(address => mapping(address => bool)) public friendship;
constructor() public {}
function MyFriends() public view returns (address[] memory) {
return friends[msg.sender];
}
function AddFriend(address _friend) public {
require(_friend != msg.sender, "Can't add yourself to friend list");
require(!friendship[msg.sender][_friend], "Friend already added");
friends[msg.sender].push(_friend);
friendship[msg.sender][_friend] = true;
}
function AddFriendList(address[] memory list) public {
for (uint i = 0; i < list.length; ++i) {
address cur = list[i];
if (friendship[msg.sender][cur] || cur == msg.sender) {
continue;
}
friends[msg.sender].push(cur);
friendship[msg.sender][cur] = true;
}
}
function DelFriend(address _friend) public {
if (!friendship[msg.sender][_friend]) {
return;
}
delete friendship[msg.sender][_friend];
for (uint i = 0; i < friends[msg.sender].length; ++i) {
if (_friend != friends[msg.sender][i]) {
continue;
}
if (i == friends[msg.sender].length) {
friends[msg.sender].length--;
return;
}
friends[msg.sender][i] = friends[msg.sender][friends[msg.sender].length - 1];
friends[msg.sender].length--;
}
}
}