├── Other contracts Base Camp ├── Minimal Token Base Camp └── Mapping Base Camp /Other contracts Base Camp: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.8; 3 | 4 | // Import the AddressBook contract to interact with it 5 | import "./AddressBook.sol"; 6 | 7 | // Contract for creating new instances of AddressBook 8 | contract AddressBookFactory { 9 | // Define a private salt value for internal use 10 | string private salt = "value"; 11 | 12 | // Function to deploy a new instance of AddressBook 13 | function deploy() external returns (AddressBook) { 14 | // Create a new instance of AddressBook 15 | AddressBook newAddressBook = new AddressBook(); 16 | 17 | // Transfer ownership of the new AddressBook contract to the caller of this function 18 | newAddressBook.transferOwnership(msg.sender); 19 | 20 | // Return the newly created AddressBook contract 21 | return newAddressBook; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Minimal Token Base Camp: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.0; 3 | 4 | // Contract for an unburnable token 5 | contract UnburnableToken { 6 | string private salt = "value"; // A private string variable 7 | 8 | // Mapping to track token balances of addresses 9 | mapping(address => uint256) public balances; 10 | 11 | uint256 public totalSupply; // Total supply of tokens 12 | uint256 public totalClaimed; // Total number of tokens claimed 13 | mapping(address => bool) private claimed; // Mapping to track whether an address has claimed tokens 14 | 15 | // Custom errors 16 | error TokensClaimed(); // Error for attempting to claim tokens again 17 | error AllTokensClaimed(); // Error for attempting to claim tokens when all are already claimed 18 | error UnsafeTransfer(address _to); // Error for unsafe token transfer 19 | 20 | // Constructor to set the total supply of tokens 21 | constructor() { 22 | totalSupply = 100000000; // Set the total supply of tokens 23 | } 24 | 25 | // Public function to claim tokens 26 | function claim() public { 27 | // Check if all tokens have been claimed 28 | if (totalClaimed >= totalSupply) revert AllTokensClaimed(); 29 | 30 | // Check if the caller has already claimed tokens 31 | if (claimed[msg.sender]) revert TokensClaimed(); 32 | 33 | // Update balances and claimed status 34 | balances[msg.sender] += 1000; 35 | totalClaimed += 1000; 36 | claimed[msg.sender] = true; 37 | } 38 | 39 | // Public function for safe token transfer 40 | function safeTransfer(address _to, uint256 _amount) public { 41 | // Check for unsafe transfer conditions, including if the target address has a non-zero ether balance 42 | if (_to == address(0) || _to.balance == 0) revert UnsafeTransfer(_to); 43 | 44 | // Ensure the sender has enough balance to transfer 45 | require(balances[msg.sender] >= _amount, "Insufficient balance"); 46 | 47 | // Perform the transfer 48 | balances[msg.sender] -= _amount; 49 | balances[_to] += _amount; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Mapping Base Camp: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.17; 3 | 4 | /** 5 | * @title FavoriteRecords 6 | * @dev Contract to manage a list of approved music records and allow users to add them to their favorites 7 | */ 8 | contract FavoriteRecords { 9 | // Mapping to store whether a record is approved 10 | mapping(string => bool) private approvedRecords; 11 | // Array to store the index of approved records 12 | string[] private approvedRecordsIndex; 13 | 14 | // Mapping to store user's favorite records 15 | mapping(address => mapping(string => bool)) public userFavorites; 16 | // Mapping to store the index of user's favorite records 17 | mapping(address => string[]) private userFavoritesIndex; 18 | 19 | // Custom error to handle unapproved records 20 | error NotApproved(string albumName); 21 | 22 | /** 23 | * @dev Constructor that initializes the approved records list 24 | */ 25 | constructor() { 26 | // Predefined list of approved records 27 | approvedRecordsIndex = [ 28 | "Thriller", 29 | "Back in Black", 30 | "The Bodyguard", 31 | "The Dark Side of the Moon", 32 | "Their Greatest Hits (1971-1975)", 33 | "Hotel California", 34 | "Come On Over", 35 | "Rumours", 36 | "Saturday Night Fever" 37 | ]; 38 | // Initialize the approved records mapping 39 | for (uint i = 0; i < approvedRecordsIndex.length; i++) { 40 | approvedRecords[approvedRecordsIndex[i]] = true; 41 | } 42 | } 43 | 44 | /** 45 | * @dev Returns the list of approved records 46 | * @return An array of approved record names 47 | */ 48 | function getApprovedRecords() public view returns (string[] memory) { 49 | return approvedRecordsIndex; 50 | } 51 | 52 | /** 53 | * @dev Adds an approved record to the user's favorites 54 | * @param _albumName The name of the album to be added 55 | */ 56 | function addRecord(string memory _albumName) public { 57 | // Check if the record is approved 58 | if (!approvedRecords[_albumName]) { 59 | revert NotApproved({albumName: _albumName}); 60 | } 61 | // Check if the record is not already in the user's favorites 62 | if (!userFavorites[msg.sender][_albumName]) { 63 | // Add the record to the user's favorites 64 | userFavorites[msg.sender][_albumName] = true; 65 | // Add the record to the user's favorites index 66 | userFavoritesIndex[msg.sender].push(_albumName); 67 | } 68 | } 69 | 70 | /** 71 | * @dev Returns the list of a user's favorite records 72 | * @param _address The address of the user 73 | * @return An array of user's favorite record names 74 | */ 75 | function getUserFavorites(address _address) public view returns (string[] memory) { 76 | return userFavoritesIndex[_address]; 77 | } 78 | 79 | /** 80 | * @dev Resets the caller's list of favorite records 81 | */ 82 | function resetUserFavorites() public { 83 | // Iterate through the user's favorite records 84 | for (uint i = 0; i < userFavoritesIndex[msg.sender].length; i++) { 85 | // Delete each record from the user's favorites mapping 86 | delete userFavorites[msg.sender][userFavoritesIndex[msg.sender][i]]; 87 | } 88 | // Delete the user's favorites index 89 | delete userFavoritesIndex[msg.sender]; 90 | } 91 | } 92 | --------------------------------------------------------------------------------