tokenURI code block explanation (ERC721)

Please see the block of code below which is a function that returns the tokenURI based on on-chain data (timestamp).

CODE BLOCK
`function tokenURI(uint256 tokenId) public view virtual override returns (string memory)

{

    require(_exists(tokenId),"ERC721Metadata: URI query for nonexistent token");

   

    //if the block timestamp is divisible by 2 show the aURI

    if (block.timestamp % 2 == 0) {

        return bytes(aUri).length > 0

        ? string(abi.encodePacked(aUri, tokenId.toString(), baseExtension))

        : "";

    }

I am a little confused as to what the line of code in the return block means (what does the "?" signify, sorry for my noob question, I'm just dipping my feet into solidity.

Preformatted text return bytes(aUri).length > 0

        ? string(abi.encodePacked(aUri, tokenId.toString(), baseExtension))

        : "";

That is a conditional if that works like this: (boolean_expression)? if true execute this side: if false execute this side.

Meaning in your case:

if (return bytes(aUri).length > 0) {
   return string(abi.encodePacked(aUri, tokenId.toString(), baseExtension))
} else {
  return "";
}
1 Like