/
AddressBook.sol
39 lines (31 loc) · 1.23 KB
/
AddressBook.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
// SPDX-License-Identifier: MIT
//
// https://cryptomarketpool.com/address-book
pragma solidity ^0.8.0;
contract AddressBook {
mapping(address => address[]) private contacts;
mapping(address => mapping(address => string)) private aliases;
function getContacts() public view returns (address[] memory) {
return contacts[msg.sender];
}
function addContact(address contactAddress, string memory aliasName) public {
contacts[msg.sender].push(contactAddress);
aliases[msg.sender][contactAddress] = aliasName;
}
function getAlias(address contactAddress) public view returns (string memory) {
return aliases[msg.sender][contactAddress];
}
function removeContact(address contactAddress) public {
uint256 length = contacts[msg.sender].length;
for (uint256 i = 0; i < length; i++) {
if (contactAddress == contacts[msg.sender][i]) {
if (contacts[msg.sender].length > 1 && i < length - 1) {
contacts[msg.sender][i] = contacts[msg.sender][length - 1];
}
contacts[msg.sender].pop();
delete aliases[msg.sender][contactAddress];
break;
}
}
}
}