Upload AddressBook.sol
Browse files- AddressBook.sol +72 -0
AddressBook.sol
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// SPDX-License-Identifier: GPL-3.0
|
| 2 |
+
pragma solidity ^0.8.8;
|
| 3 |
+
|
| 4 |
+
import "@openzeppelin/contracts/access/Ownable.sol";
|
| 5 |
+
|
| 6 |
+
contract AddressBook is Ownable(msg.sender) {
|
| 7 |
+
// Define a private salt value for internal use
|
| 8 |
+
string private salt = "value";
|
| 9 |
+
|
| 10 |
+
// Define a struct to represent a contact
|
| 11 |
+
struct Contact {
|
| 12 |
+
uint id; // Unique identifier for the contact
|
| 13 |
+
string firstName; // First name of the contact
|
| 14 |
+
string lastName; // Last name of the contact
|
| 15 |
+
uint[] phoneNumbers; // Array to store multiple phone numbers for the contact
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
// Array to store all contacts
|
| 19 |
+
Contact[] private contacts;
|
| 20 |
+
|
| 21 |
+
// Mapping to store the index of each contact in the contacts array using its ID
|
| 22 |
+
mapping(uint => uint) private idToIndex;
|
| 23 |
+
|
| 24 |
+
// Variable to keep track of the ID for the next contact
|
| 25 |
+
uint private nextId = 1;
|
| 26 |
+
|
| 27 |
+
// Custom error for when a contact is not found
|
| 28 |
+
error ContactNotFound(uint id);
|
| 29 |
+
|
| 30 |
+
// Function to add a new contact
|
| 31 |
+
function addContact(string calldata firstName, string calldata lastName, uint[] calldata phoneNumbers) external onlyOwner {
|
| 32 |
+
// Create a new contact with the provided details and add it to the contacts array
|
| 33 |
+
contacts.push(Contact(nextId, firstName, lastName, phoneNumbers));
|
| 34 |
+
// Map the ID of the new contact to its index in the array
|
| 35 |
+
idToIndex[nextId] = contacts.length - 1;
|
| 36 |
+
// Increment the nextId for the next contact
|
| 37 |
+
nextId++;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// Function to delete a contact by its ID
|
| 41 |
+
function deleteContact(uint id) external onlyOwner {
|
| 42 |
+
// Get the index of the contact to be deleted
|
| 43 |
+
uint index = idToIndex[id];
|
| 44 |
+
// Check if the index is valid and if the contact with the provided ID exists
|
| 45 |
+
if (index >= contacts.length || contacts[index].id != id) revert ContactNotFound(id);
|
| 46 |
+
|
| 47 |
+
// Replace the contact to be deleted with the last contact in the array
|
| 48 |
+
contacts[index] = contacts[contacts.length - 1];
|
| 49 |
+
// Update the index mapping for the moved contact
|
| 50 |
+
idToIndex[contacts[index].id] = index;
|
| 51 |
+
// Remove the last contact from the array
|
| 52 |
+
contacts.pop();
|
| 53 |
+
// Delete the mapping entry for the deleted contact ID
|
| 54 |
+
delete idToIndex[id];
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// Function to retrieve a contact by its ID
|
| 58 |
+
function getContact(uint id) external view returns (Contact memory) {
|
| 59 |
+
// Get the index of the contact
|
| 60 |
+
uint index = idToIndex[id];
|
| 61 |
+
// Check if the index is valid and if the contact with the provided ID exists
|
| 62 |
+
if (index >= contacts.length || contacts[index].id != id) revert ContactNotFound(id);
|
| 63 |
+
// Return the contact details
|
| 64 |
+
return contacts[index];
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// Function to retrieve all contacts
|
| 68 |
+
function getAllContacts() external view returns (Contact[] memory) {
|
| 69 |
+
// Return the array of all contacts
|
| 70 |
+
return contacts;
|
| 71 |
+
}
|
| 72 |
+
}
|