└── ERC721 /ERC721: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.0; 3 | 4 | // Importing OpenZeppelin ERC721 contract 5 | import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol"; 6 | 7 | // Interface for interacting with a submission contract 8 | interface ISubmission { 9 | // Struct representing a haiku 10 | struct Haiku { 11 | address author; // Address of the haiku author 12 | string line1; // First line of the haiku 13 | string line2; // Second line of the haiku 14 | string line3; // Third line of the haiku 15 | } 16 | 17 | // Function to mint a new haiku 18 | function mintHaiku( 19 | string memory _line1, 20 | string memory _line2, 21 | string memory _line3 22 | ) external; 23 | 24 | // Function to get the total number of haikus 25 | function counter() external view returns (uint256); 26 | 27 | // Function to share a haiku with another address 28 | function shareHaiku(uint256 _id, address _to) external; 29 | 30 | // Function to get haikus shared with the caller 31 | function getMySharedHaikus() external view returns (Haiku[] memory); 32 | } 33 | 34 | // Contract for managing Haiku NFTs 35 | contract HaikuNFT is ERC721, ISubmission { 36 | Haiku[] public haikus; // Array to store haikus 37 | mapping(address => mapping(uint256 => bool)) public sharedHaikus; // Mapping to track shared haikus 38 | uint256 public haikuCounter; // Counter for total haikus minted 39 | 40 | // Constructor to initialize the ERC721 contract 41 | constructor() ERC721("HaikuNFT", "HAIKU") { 42 | haikuCounter = 1; // Initialize haiku counter 43 | } 44 | 45 | string salt = "value"; // A private string variable 46 | 47 | // Function to get the total number of haikus 48 | function counter() external view override returns (uint256) { 49 | return haikuCounter; 50 | } 51 | 52 | // Function to mint a new haiku 53 | function mintHaiku( 54 | string memory _line1, 55 | string memory _line2, 56 | string memory _line3 57 | ) external override { 58 | // Check if the haiku is unique 59 | string[3] memory haikusStrings = [_line1, _line2, _line3]; 60 | for (uint256 li = 0; li < haikusStrings.length; li++) { 61 | string memory newLine = haikusStrings[li]; 62 | for (uint256 i = 0; i < haikus.length; i++) { 63 | Haiku memory existingHaiku = haikus[i]; 64 | string[3] memory existingHaikuStrings = [ 65 | existingHaiku.line1, 66 | existingHaiku.line2, 67 | existingHaiku.line3 68 | ]; 69 | for (uint256 eHsi = 0; eHsi < 3; eHsi++) { 70 | string memory existingHaikuString = existingHaikuStrings[ 71 | eHsi 72 | ]; 73 | if ( 74 | keccak256(abi.encodePacked(existingHaikuString)) == 75 | keccak256(abi.encodePacked(newLine)) 76 | ) { 77 | revert HaikuNotUnique(); 78 | } 79 | } 80 | } 81 | } 82 | 83 | // Mint the haiku NFT 84 | _safeMint(msg.sender, haikuCounter); 85 | haikus.push(Haiku(msg.sender, _line1, _line2, _line3)); 86 | haikuCounter++; 87 | } 88 | 89 | // Function to share a haiku with another address 90 | function shareHaiku(uint256 _id, address _to) external override { 91 | require(_id > 0 && _id <= haikuCounter, "Invalid haiku ID"); 92 | 93 | Haiku memory haikuToShare = haikus[_id - 1]; 94 | require(haikuToShare.author == msg.sender, "NotYourHaiku"); 95 | 96 | sharedHaikus[_to][_id] = true; 97 | } 98 | 99 | // Function to get haikus shared with the caller 100 | function getMySharedHaikus() 101 | external 102 | view 103 | override 104 | returns (Haiku[] memory) 105 | { 106 | uint256 sharedHaikuCount; 107 | for (uint256 i = 0; i < haikus.length; i++) { 108 | if (sharedHaikus[msg.sender][i + 1]) { 109 | sharedHaikuCount++; 110 | } 111 | } 112 | 113 | Haiku[] memory result = new Haiku[](sharedHaikuCount); 114 | uint256 currentIndex; 115 | for (uint256 i = 0; i < haikus.length; i++) { 116 | if (sharedHaikus[msg.sender][i + 1]) { 117 | result[currentIndex] = haikus[i]; 118 | currentIndex++; 119 | } 120 | } 121 | 122 | if (sharedHaikuCount == 0) { 123 | revert NoHaikusShared(); 124 | } 125 | 126 | return result; 127 | } 128 | 129 | // Custom errors 130 | error HaikuNotUnique(); // Error for attempting to mint a non-unique haiku 131 | error NotYourHaiku(); // Error for attempting to share a haiku not owned by the caller 132 | error NoHaikusShared(); // Error for no haikus shared with the caller 133 | } 134 | --------------------------------------------------------------------------------