|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.4; |
| 3 | + |
| 4 | +import "@openzeppelin/[email protected]/token/ERC721/ERC721.sol"; |
| 5 | +import "@openzeppelin/[email protected]/token/ERC721/extensions/ERC721URIStorage.sol"; |
| 6 | +import "@openzeppelin/[email protected]/access/Ownable.sol"; |
| 7 | +import "@openzeppelin/[email protected]/utils/Counters.sol"; |
| 8 | + |
| 9 | +contract dynNFT is ERC721, ERC721URIStorage, Ownable { |
| 10 | + using Counters for Counters.Counter; |
| 11 | + |
| 12 | + Counters.Counter private _tokenIdCounter; |
| 13 | + |
| 14 | + // Metadata information for each stage of the NFT on IPFS. |
| 15 | + string[] IpfsUri = [ |
| 16 | + "https://ipfs.io/ipfs/QmYaTsyxTDnrG4toc8721w62rL4ZBKXQTGj9c9Rpdrntou/seed.json", |
| 17 | + "https://ipfs.io/ipfs/QmYaTsyxTDnrG4toc8721w62rL4ZBKXQTGj9c9Rpdrntou/purple-sprout.json", |
| 18 | + "https://ipfs.io/ipfs/QmYaTsyxTDnrG4toc8721w62rL4ZBKXQTGj9c9Rpdrntou/purple-blooms.json" |
| 19 | + ]; |
| 20 | + |
| 21 | + uint256 lastTimeStamp; |
| 22 | + uint256 interval; |
| 23 | + |
| 24 | + constructor(uint256 _interval) ERC721("dNFTs", "dNFT") { |
| 25 | + interval = _interval; |
| 26 | + lastTimeStamp = block.timestamp; |
| 27 | + } |
| 28 | + |
| 29 | + function checkUpkeep( |
| 30 | + bytes calldata /* checkData */ |
| 31 | + ) |
| 32 | + external |
| 33 | + view |
| 34 | + returns ( |
| 35 | + bool upkeepNeeded, |
| 36 | + bytes memory /* performData */ |
| 37 | + ) |
| 38 | + { |
| 39 | + upkeepNeeded = (block.timestamp - lastTimeStamp) > interval; |
| 40 | + // We don't use the checkData in this example. The checkData is defined when the Upkeep was registered. |
| 41 | + } |
| 42 | + |
| 43 | + function performUpkeep( |
| 44 | + bytes calldata /* performData */ |
| 45 | + ) external { |
| 46 | + //We highly recommend revalidating the upkeep in the performUpkeep function |
| 47 | + if ((block.timestamp - lastTimeStamp) > interval) { |
| 48 | + lastTimeStamp = block.timestamp; |
| 49 | + growFlower(0); |
| 50 | + } |
| 51 | + // We don't use the performData in this example. The performData is generated by the Keeper's call to your checkUpkeep function |
| 52 | + } |
| 53 | + |
| 54 | + function safeMint(address to) public onlyOwner { |
| 55 | + uint256 tokenId = _tokenIdCounter.current(); |
| 56 | + _tokenIdCounter.increment(); |
| 57 | + _safeMint(to, tokenId); |
| 58 | + _setTokenURI(tokenId, IpfsUri[0]); |
| 59 | + } |
| 60 | + |
| 61 | + function growFlower(uint256 _tokenId) public { |
| 62 | + if (flowerStage(_tokenId) >= 2) { |
| 63 | + return; |
| 64 | + } |
| 65 | + // Get the current stage of the flower and add 1 |
| 66 | + uint256 newVal = flowerStage(_tokenId) + 1; |
| 67 | + // store the new URI |
| 68 | + string memory newUri = IpfsUri[newVal]; |
| 69 | + // Update the URI |
| 70 | + _setTokenURI(_tokenId, newUri); |
| 71 | + } |
| 72 | + |
| 73 | + // determine the stage of the flower growth |
| 74 | + function flowerStage(uint256 _tokenId) public view returns (uint256) { |
| 75 | + string memory _uri = tokenURI(_tokenId); |
| 76 | + // Seed |
| 77 | + if (compareStrings(_uri, IpfsUri[0])) { |
| 78 | + return 0; |
| 79 | + } |
| 80 | + // Sprout |
| 81 | + if (compareStrings(_uri, IpfsUri[1])) { |
| 82 | + return 1; |
| 83 | + } |
| 84 | + // Must be a Bloom |
| 85 | + return 2; |
| 86 | + } |
| 87 | + |
| 88 | + /* |
| 89 | + ******************** |
| 90 | + * HELPER FUNCTIONS * |
| 91 | + ******************** |
| 92 | + */ |
| 93 | + // helper function to compare strings |
| 94 | + function compareStrings(string memory a, string memory b) |
| 95 | + public |
| 96 | + pure |
| 97 | + returns (bool) |
| 98 | + { |
| 99 | + return (keccak256(abi.encodePacked((a))) == |
| 100 | + keccak256(abi.encodePacked((b)))); |
| 101 | + } |
| 102 | + |
| 103 | + // The following functions are overrides required by Solidity. |
| 104 | + |
| 105 | + function _burn(uint256 tokenId) |
| 106 | + internal |
| 107 | + override(ERC721, ERC721URIStorage) |
| 108 | + { |
| 109 | + super._burn(tokenId); |
| 110 | + } |
| 111 | + |
| 112 | + function tokenURI(uint256 tokenId) |
| 113 | + public |
| 114 | + view |
| 115 | + override(ERC721, ERC721URIStorage) |
| 116 | + returns (string memory) |
| 117 | + { |
| 118 | + return super.tokenURI(tokenId); |
| 119 | + } |
| 120 | +} |
0 commit comments