Base Learn Prefect-4-——Own a(n) New Keyword Pin NFT
- 获取链接
- X
- 电子邮件
- 其他应用
感谢X用户@isnotberlin提供的代码
以下是,Base Learn Prefect-代码 Own a(n) New Keyword Pin NFT
1. Own a(n) Error Triage Pin NFT由两段代码组成AddressBook.sol和other contract.sol
AddressBook.sol
AddressBook.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.8;
import "@openzeppelin/contracts/access/Ownable.sol";
contract AddressBook is Ownable {
// Define a private salt value for internal use
string private salt = "value";
// Define a struct to represent a contact
struct Contact {
uint id; // Unique identifier for the contact
string firstName; // First name of the contact
string lastName; // Last name of the contact
uint[] phoneNumbers; // Array to store multiple phone numbers for the contact
}
// Array to store all contacts
Contact[] private contacts;
// Mapping to store the index of each contact in the contacts array using its ID
mapping(uint => uint) private idToIndex;
// Variable to keep track of the ID for the next contact
uint private nextId = 1;
// Custom error for when a contact is not found
error ContactNotFound(uint id);
// 🔧 FIX: Constructor eklendi - Ownable için gerekli
constructor() Ownable(msg.sender) {}
// 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;
}
}
这段代码被other contract引用
other contract.sol
other contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
// Import the AddressBook contract to interact with it
import "./AddressBook.sol";
// Contract for creating new instances of AddressBook
contract AddressBookFactory {
// Define a private salt value for internal use
string private salt = "value";
// Function to deploy a new instance of AddressBook
function deploy() external returns (AddressBook) {
// Create a new instance of AddressBook
AddressBook newAddressBook = new AddressBook();
// Transfer ownership of the new AddressBook contract to the caller of this function
newAddressBook.transferOwnership(msg.sender);
// Return the newly created AddressBook contract
return newAddressBook;
}
}
部署这段代码,在Remix IDE部署
- 获取链接
- X
- 电子邮件
- 其他应用
评论
发表评论