Storing ERC 721 metadata on chain

Hey! I’ve been looking at the Autoglyphs NFT (https://etherscan.io/address/0xd4e4078ca3495de5b1d4db434bebc5a986197782#code) and the token URI they have points to a draw function.

In such a case, can we store the metadata conforming to the ERC721 Metadata JSON Schema in the contract itself? Like {“image”: “raw data of the image”}. The raw data need not necessarily be like in the Autoglyphs contract, it could be a simple/complex SVG. For example,

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="50"/>
</svg>

For applications such as these, where the raw data of the image can be in the contract itself, it doesn’t make much sense to have a separate external link for tokenURI which stores the rendered image.

Is there a way to have a tokenURI function for such cases which doesn’t link to an external URL and is compliant with the ERC721 metadata standard?

In such a case, can we store the metadata conforming to the ERC721 Metadata JSON Schema in the contract itself? Like {“image”: “raw data of the image”}.

Yes you can store basically any string or text you imagine into an NFT.

For applications such as these, where the raw data of the image can be in the contract itself, it doesn’t make much sense to have a separate external link for tokenURI which stores the rendered image.

If you want to do that then sure, it’s your gas :money_mouth_face:. But aside from price of minting the point is to have metadata that is meaningful. Putting a massive amount of text into the metadata isn’t good because you want your metadata to be easily readable and understandable by dApps when they read it.

I’m not sure what would be considered compliant with the metadata standard, but I think you should follow basic metadata formats.

For images in specific you really should use IPFS. You should also have a way to update the metadata if needed, that way if you want to change your image provider later on, you have a way to do so in your contract.