I’m building an NFT marketplace with Solidity and using OpenZeppelin’s ERC-721 as the base contract. Right now my tokens have 5 properties: id, image hash, description, collection name, and creator address. I store the IPFS hash for the actual image file.
I’m confused about where to store this metadata. Currently I have an Asset struct with all these properties, put it in an array, then mint the NFT using the asset’s id and owner address. This means I’m keeping all the important data outside the actual ERC-721 contract.
This makes me wonder what the NFT actually represents if all the real information lives in my custom struct rather than in the token itself.
Is this the right approach? Does ERC-721 just provide the basic token functions while the actual metadata goes elsewhere?
Here’s my current implementation:
pragma solidity ^0.5.0;
import "./ERC721Full.sol";
contract ArtMarket 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("ArtMarket", "ART") {
contractName = "ArtMarket";
}
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 advice on improving this code would be great. I’m new to Ethereum development so not sure if I’m doing this right.