0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 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.