Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 4 additions & 48 deletions AddressBook.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT use this instead ( // SPDX-License-Identifier: GPL-3.0 )
pragma solidity ^0.8.8;

import "@openzeppelin/contracts/access/Ownable.sol";
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

pragma solidity >=0.8.12;
import "https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/access/Ownable2Step.sol";
contract AddressBook is Ownable(msg.sender) {
// Define a private salt value for internal use
string private salt = "value";
Expand All @@ -26,47 +26,3 @@ contract AddressBook is Ownable(msg.sender) {

// Custom error for when a contact is not found
error ContactNotFound(uint id);

// Function to add a new contact
function addContact(string calldata firstName, string calldata lastName, uint[] calldata phoneNumbers) external onlyOwner {
// Create a new contact with the provided details and add it to the contacts array
contacts.push(Contact(nextId, firstName, lastName, phoneNumbers));
// Map the ID of the new contact to its index in the array
idToIndex[nextId] = contacts.length - 1;
// Increment the nextId for the next contact
nextId++;
}

// Function to delete a contact by its ID
function deleteContact(uint id) external onlyOwner {
// Get the index of the contact to be deleted
uint index = idToIndex[id];
// Check if the index is valid and if the contact with the provided ID exists
if (index >= contacts.length || contacts[index].id != id) revert ContactNotFound(id);

// Replace the contact to be deleted with the last contact in the array
contacts[index] = contacts[contacts.length - 1];
// Update the index mapping for the moved contact
idToIndex[contacts[index].id] = index;
// Remove the last contact from the array
contacts.pop();
// Delete the mapping entry for the deleted contact ID
delete idToIndex[id];
}

// Function to retrieve a contact by its ID
function getContact(uint id) external view returns (Contact memory) {
// Get the index of the contact
uint index = idToIndex[id];
// Check if the index is valid and if the contact with the provided ID exists
if (index >= contacts.length || contacts[index].id != id) revert ContactNotFound(id);
// Return the contact details
return contacts[index];
}

// Function to retrieve all contacts
function getAllContacts() external view returns (Contact[] memory) {
// Return the array of all contacts
return contacts;
}
}