A Smart Contract that allows you to add stories to a given non-fungible token. The tokens developed start from OpenZeppelin code on ERC721 with URIStorage extension and are therefore fully compatible with Opensea and the various marketplaces. Below you can find an explanation of the functions and state variables.
When the token is minted the contract creates a state variable that tracks the stories for each tokenId present.
mapping(uint256=>string[]) private dynamicData;
Modifier that checks if the msg.sender of the transaction is the owner of that particular tokenId in order to push new dynamicData inside the array.
modifier onlyTokenOwner(uint256 _tokenId){
require(ownerOf(_tokenId)==msg.sender, "Sender is not the owner of the token");
_;
}
Function that adds a new story or dynamic data to the array of string. It takes the tokenId of the token to be modified and the data string pointing to the IPFS in order to store metadata.
function addDataToDynamicNFT(uint256 _tokenId, string memory _data) public onlyTokenOwner(_tokenId) {
require(_exists(_tokenId), "ERC721URIStorage: URI set of nonexistent token");
dynamicData[_tokenId].push(_data);
}
Function that retrieves data from a particular token. It takes the tokenId and return an array of string pointing to the metadata in the IPFS.
function getDynamiData(uint256 _tokenId) external view returns(string[] memory) {
require(_exists(_tokenId), "ERC721URIStorage: URI set of nonexistent token");
return dynamicData[_tokenId];
}
Author: Guazzo Gianmarco