I’m working on an NFT marketplace using Solidity and OpenZeppelin’s ERC-721 contract as a foundation. Each NFT has five key attributes: id, image hash, description, collection name, and the creator’s address, where the image hash is retrieved from IPFS upon upload.
I’m unsure about the best way to manage this data. Currently, I have an Image struct that contains all these attributes, and I am saving them in an array. When I mint a new NFT, I use the id from the Image struct and the creator’s address. This setup means that essential data is stored outside of the ERC-721 contract itself.
This raises my question: what exactly defines an NFT if its crucial attributes are housed in a separate struct rather than with the NFT?
Is this method correct? Does the ERC-721 standard only offer fundamental token functions, while the metadata is stored elsewhere?
pragma solidity ^0.5.0;
import "./ERC721Full.sol";
contract TokenMarket is ERC721Full {
string public contractName;
Asset[] public tokens;
uint public assetCounter = 0;
mapping(uint => bool) public _tokenExists;
mapping(uint => Asset) public assets;
struct Asset {
uint tokenId;
string ipfsHash;
string title;
string category;
address payable creator;
}
event AssetMinted(
uint tokenId,
string ipfsHash,
string title,
string category,
address payable creator
);
constructor() public payable ERC721Full("TokenMarket", "TMARKET") {
contractName = "TokenMarket";
}
function createAsset(string memory _hash, string memory _title, string memory _category) public {
require(bytes(_hash).length > 0);
require(bytes(_title).length > 0);
require(bytes(_category).length > 0);
require(msg.sender != address(0));
assetCounter++;
assets[assetCounter] = Asset(assetCounter, _hash, _title, _category, msg.sender);
require(!_tokenExists[assetCounter]);
uint _tokenId = tokens.push(assets[assetCounter]);
_mint(msg.sender, _tokenId);
_tokenExists[assetCounter] = true;
emit AssetMinted(assetCounter, _hash, _title, _category, msg.sender);
}
}
Any feedback on improving this code would be great. I’m new to Ethereum development.