4

Sorry if this is hella n00b, but I was wondering when overriding tokenURI you can just return a string containing all the metadata which is calculated dynamically on-chain, eliminating the need for hosted JSON files.

e.g.

function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
     require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");

      string memory newstring = string(abi.encodePacked("{ \"name\" :" , getname(tokenId), "}")); 
    
     return newstring;
 }


Will marketplaces like Opensea only expect a URL which LINKS to the metadata? Why wouldn't they take the entirety of the data straight from output of the function?

This would eliminate the need for manually (or automatically) updating things like the stats of your NFT, etc. and everything would stay on chain. I'm sorry for the maybe n00b question, but I can't find any other posts that have tried this.

Any light on this subject would be much appreciated.

1
  • Alternatively, can a PHP file be specified as the BaseURI, example.com/metdata.php?tokenid = (token id) which will output a JSON formatted metadata string???
    – Pethereum
    Sep 6, 2021 at 13:45

1 Answer 1

2

Yes, it's possible using Data URLs. OpenSea requires the tokenURI method to return a URI of a JSON containing properties such as image (which themselves can also have data URLs as values).

Here's a great tutorial on how to store metadata for NFTs on-chain: https://andyhartnett.medium.com/solidity-tutorial-how-to-store-nft-metadata-and-svgs-on-the-blockchain-6df44314406b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.