0
0
Blockchain / Solidityprogramming~20 mins

Token metadata and URI in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Token Metadata Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Solidity function fetching token URI?
Consider this Solidity function that returns a token URI based on token ID. What will be the output when calling tokenURI(42)?
Blockchain / Solidity
function tokenURI(uint256 tokenId) public view returns (string memory) {
    string memory baseURI = "https://example.com/metadata/";
    return string(abi.encodePacked(baseURI, Strings.toString(tokenId), ".json"));
}
A"https://example.com/metadata/42.json"
B"https://example.com/metadata/token42.json"
C"https://example.com/metadata/42"
D"https://example.com/metadata/.json"
Attempts:
2 left
💡 Hint
Look at how the base URI and token ID are concatenated with the file extension.
🧠 Conceptual
intermediate
1:30remaining
Which standard defines the metadata URI for ERC-721 tokens?
In Ethereum, which standard specifies how token metadata URI should be exposed for NFTs?
AERC-721
BERC-20
CERC-1155
DERC-777
Attempts:
2 left
💡 Hint
This standard is the original NFT standard.
🔧 Debug
advanced
2:30remaining
Why does this tokenURI function revert with 'URI query for nonexistent token' error?
Given this Solidity snippet, why does calling tokenURI(10) revert?
function tokenURI(uint256 tokenId) public view returns (string memory) {
    require(_exists(tokenId), "URI query for nonexistent token");
    return _tokenURIs[tokenId];
}
Blockchain / Solidity
mapping(uint256 => string) private _tokenURIs;

function _exists(uint256 tokenId) internal view returns (bool) {
    return _owners[tokenId] != address(0);
}

mapping(uint256 => address) private _owners;
AThe _owners mapping is private and cannot be accessed causing revert.
BToken ID 10 has no owner, so _exists returns false causing require to fail.
CThe function tokenURI is missing the override keyword causing revert.
DThe _tokenURIs mapping is empty, so returning _tokenURIs[10] causes revert.
Attempts:
2 left
💡 Hint
Check what _exists function checks before returning URI.
📝 Syntax
advanced
2:00remaining
Which option correctly implements a tokenURI function with base URI and token ID?
Select the correct Solidity function that returns the token URI by concatenating a base URI and token ID.
A
function tokenURI(uint256 tokenId) public view returns (string memory) {
    return string(abi.encodePacked(baseURI, tokenId));
}
B
function tokenURI(uint256 tokenId) public view returns (string memory) {
    return baseURI + tokenId;
}
C
function tokenURI(uint256 tokenId) public view returns (string memory) {
    return baseURI.concat(tokenId);
}
D
function tokenURI(uint256 tokenId) public view returns (string memory) {
    return string(abi.encodePacked(baseURI, Strings.toString(tokenId)));
}
Attempts:
2 left
💡 Hint
Remember tokenId is a number and needs conversion to string before concatenation.
🚀 Application
expert
3:00remaining
How many metadata JSON files are needed for an ERC-1155 contract with 3 token types?
An ERC-1155 contract manages 3 different token types with IDs 1, 2, and 3. Each token type has unique metadata. How many separate JSON metadata files should be hosted to fully support tokenURI calls?
A1 JSON file containing metadata for all tokens combined
B0 JSON files because ERC-1155 does not use metadata files
C3 JSON files, one per token ID
D6 JSON files, two per token ID for different formats
Attempts:
2 left
💡 Hint
Each token type in ERC-1155 can have its own metadata URI.