Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Token metadata and URI in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Token metadata and URI
Token Created
↓
Assign Token ID
↓
Attach Metadata URI
↓
Metadata URI Points to JSON
↓
Client Fetches Metadata
↓
Display Token Info
This flow shows how a token is created with an ID, linked to a metadata URI, which points to JSON data that clients fetch to display token details.
Execution Sample
Blockchain / Solidity
function tokenURI(tokenId) {
  return baseURI + tokenId + '.json';
}

// Example: tokenURI(1) returns 'https://example.com/metadata/1.json'
This code returns the full URI for a token's metadata JSON file based on its ID.
Execution Table
SteptokenIdbaseURIComputed URIAction
11'https://example.com/metadata/''https://example.com/metadata/1.json'Concatenate baseURI + tokenId + '.json'
242'https://example.com/metadata/''https://example.com/metadata/42.json'Concatenate baseURI + tokenId + '.json'
3100'https://example.com/metadata/''https://example.com/metadata/100.json'Concatenate baseURI + tokenId + '.json'
4N/AN/AN/AStop - No more token IDs to process
💡 Execution stops after processing all requested token IDs.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
tokenIdN/A142100N/A
baseURI'https://example.com/metadata/''https://example.com/metadata/''https://example.com/metadata/''https://example.com/metadata/''https://example.com/metadata/'
Computed URIN/A'https://example.com/metadata/1.json''https://example.com/metadata/42.json''https://example.com/metadata/100.json'N/A
Key Moments - 2 Insights
Why do we add '.json' at the end of the URI?
Because the metadata is stored in JSON files, adding '.json' tells the client to fetch the correct file format as shown in the execution_table rows 1-3.
What happens if the tokenId does not exist?
The function still returns a URI string, but the client may get an error or empty data when fetching, as the URI is constructed regardless of token existence (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Computed URI when tokenId is 42?
A'https://example.com/metadata/42.json'
B'https://example.com/metadata/1.json'
C'https://example.com/metadata/100.json'
D'https://example.com/metadata/42'
💡 Hint
Check the row where tokenId is 42 in the execution_table.
At which step does the execution stop according to the exit_note?
AAfter step 1
BAfter step 4
CAfter step 3
DAfter step 2
💡 Hint
Look at the exit_note and the last row in the execution_table.
If baseURI changes to 'https://newsite.com/data/', what will be the Computed URI for tokenId 1?
A'https://newsite.com/data/1'
B'https://example.com/metadata/1.json'
C'https://newsite.com/data/1.json'
D'https://example.com/data/1.json'
💡 Hint
Refer to how Computed URI is formed by concatenating baseURI + tokenId + '.json' in the variable_tracker.
Concept Snapshot
Token metadata URI:
- Each token has a unique ID.
- Metadata URI = baseURI + tokenId + '.json'
- URI points to JSON with token details.
- Clients fetch this URI to show info.
- URI is a string, independent of token existence.
Full Transcript
This visual execution trace shows how token metadata URIs are constructed in blockchain tokens. Starting from token creation, each token gets an ID. The tokenURI function concatenates a base URI string with the token ID and '.json' to form the full metadata URI. This URI points to a JSON file containing the token's metadata. The execution table traces this concatenation for token IDs 1, 42, and 100, showing the computed URIs. The variable tracker records how tokenId, baseURI, and the computed URI change step by step. Key moments clarify why '.json' is added and what happens if a token ID does not exist. The visual quiz tests understanding of URI formation and execution stopping points. The concept snapshot summarizes the key points for quick review.

Practice

(1/5)
1. What is the main purpose of token metadata in blockchain tokens?
easy
A. To mine new tokens automatically
B. To describe the token and link to its details
C. To execute smart contract functions
D. To store the token's private key

Solution

  1. Step 1: Understand token metadata role

    Token metadata provides descriptive information about the token, such as its name, description, and image link.
  2. Step 2: Differentiate from other blockchain functions

    Metadata is not about private keys, mining, or executing contracts but about describing the token for users and apps.
  3. Final Answer:

    To describe the token and link to its details -> Option B
  4. Quick Check:

    Token metadata = description and details link [OK]
Hint: Metadata describes tokens, not keys or mining [OK]
Common Mistakes:
  • Confusing metadata with private keys
  • Thinking metadata controls token creation
  • Mixing metadata with smart contract code
2. Which of the following is the correct way to specify a token URI in a smart contract?
easy
A. bool tokenURI = true;
B. int tokenURI = 12345;
C. string public tokenURI = "https://example.com/token/1.json";
D. address tokenURI = 0x123;

Solution

  1. Step 1: Identify the data type for URI

    A URI is a web address, so it should be stored as a string in the contract.
  2. Step 2: Check the options for correct syntax

    string public tokenURI = "https://example.com/token/1.json"; uses a string with a valid URL format, while others use wrong types like int, bool, or address.
  3. Final Answer:

    string public tokenURI = "https://example.com/token/1.json"; -> Option C
  4. Quick Check:

    URI is a string URL [OK]
Hint: URIs are strings, not numbers or booleans [OK]
Common Mistakes:
  • Using int or bool for URI
  • Confusing address type with URI
  • Missing quotes around the URI string
3. Given the following Solidity snippet, what will tokenURI(1) return if _tokenURIs[1] is set to "https://example.com/metadata/1.json"?
mapping(uint256 => string) private _tokenURIs;

function tokenURI(uint256 tokenId) public view returns (string memory) {
    return _tokenURIs[tokenId];
}
medium
A. An error because tokenURI is private
B. tokenId
C. Empty string
D. "https://example.com/metadata/1.json"

Solution

  1. Step 1: Understand the mapping and function

    The mapping stores strings for each tokenId. The function returns the string stored for the given tokenId.
  2. Step 2: Check the value for tokenId 1

    Since _tokenURIs[1] is set to "https://example.com/metadata/1.json", calling tokenURI(1) returns that string.
  3. Final Answer:

    "https://example.com/metadata/1.json" -> Option D
  4. Quick Check:

    Mapping lookup returns stored URI string [OK]
Hint: Mapping returns stored URI for tokenId [OK]
Common Mistakes:
  • Thinking tokenURI is private and inaccessible
  • Confusing tokenId with URI string
  • Assuming function returns tokenId instead of URI
4. Identify the error in this Solidity function that tries to set a token URI:
mapping(uint256 => string) private _tokenURIs;

function setTokenURI(uint256 tokenId, string memory uri) public {
    _tokenURIs[tokenId] = uri;
}
medium
A. The function lacks access control and allows anyone to set URIs
B. The mapping type is incorrect for storing strings
C. The function should return a string
D. The parameter uri should be declared as calldata

Solution

  1. Step 1: Review function purpose and security

    The function sets a URI for a tokenId but has no restriction on who can call it.
  2. Step 2: Identify missing access control

    Without access control (like onlyOwner), anyone can change token URIs, which is a security risk.
  3. Final Answer:

    The function lacks access control and allows anyone to set URIs -> Option A
  4. Quick Check:

    Missing access control = security risk [OK]
Hint: Always add access control to setter functions [OK]
Common Mistakes:
  • Thinking mapping type is wrong for strings
  • Expecting setter functions to return values
  • Ignoring security risks of public setters
5. You want to create a function that returns a JSON metadata URI for a token, but if the token has no specific URI set, it should return a default URI. Which Solidity code snippet correctly implements this logic?
mapping(uint256 => string) private _tokenURIs;
string private _defaultURI = "https://example.com/default.json";

function tokenURI(uint256 tokenId) public view returns (string memory) {
    // Fill in the blank
}
hard
A. return bytes(_tokenURIs[tokenId]).length > 0 ? _tokenURIs[tokenId] : _defaultURI;
B. if (_tokenURIs[tokenId] == null) { return _defaultURI; } else { return _tokenURIs[tokenId]; }
C. return _tokenURIs[tokenId] || _defaultURI;
D. return _tokenURIs[tokenId] == "" ? _tokenURIs[tokenId] : _defaultURI;

Solution

  1. Step 1: Check how to test if string is empty in Solidity

    Solidity strings can't be compared to null or use || operator; use bytes length to check emptiness.
  2. Step 2: Verify correct conditional expression

    return bytes(_tokenURIs[tokenId]).length > 0 ? _tokenURIs[tokenId] : _defaultURI; uses bytes(_tokenURIs[tokenId]).length > 0 to check if URI exists, else returns default URI.
  3. Final Answer:

    return bytes(_tokenURIs[tokenId]).length > 0 ? _tokenURIs[tokenId] : _defaultURI; -> Option A
  4. Quick Check:

    Check string length with bytes() for empty [OK]
Hint: Use bytes().length to check empty string in Solidity [OK]
Common Mistakes:
  • Using null or empty string comparison directly
  • Using || operator for strings
  • Returning wrong URI when none set
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard