what tools/services would you recommend for pulling metadata from ERC721 contracts? It seems like it's going to be a huge hassle to return the metadata directly from the blockchain. Any clever ideas?
3 Answers
Alchemy now has the query getNFTMetadata
. Here is an example response it returns:
{
"contract": {
"address": "0x34d85c9cdeb23fa97cb08333b511ac86e1c4e258"
},
"id": {
"tokenId": "3",
"tokenMetadata": {
"tokenType": "ERC721"
}
},
"title": "",
"description": "",
"tokenUri": {
"raw": "https://api.otherside.xyz/lands/3",
"gateway": "https://api.otherside.xyz/lands/3"
},
"media": [
{
"raw": "https://assets.otherside.xyz/otherdeeds/54abf723d8b4a7a885753fd4345b8d182f590ab150f7c9778d0a320b27b134bd.jpg",
"gateway": "https://res.cloudinary.com/alchemyapi/image/upload/mainnet/51f809642d9c5d15861b56dbf07f66b1.jpg",
"thumbnail": "https://res.cloudinary.com/alchemyapi/image/upload/w_256,h_256/mainnet/51f809642d9c5d15861b56dbf07f66b1.jpg",
"format": "jpg",
"bytes": 957484
}
],
"metadata": {
"image": "https://assets.otherside.xyz/otherdeeds/54abf723d8b4a7a885753fd4345b8d182f590ab150f7c9778d0a320b27b134bd.jpg",
"attributes": [
{
"value": "Harsh",
"trait_type": "Category"
},
{
"value": "Biogenic Swamp",
"trait_type": "Sediment"
},
{
"display_type": "number",
"value": 2,
"trait_type": "Sediment Tier"
},
{
"value": "Splinter",
"trait_type": "Environment"
},
{
"display_type": "number",
"value": 4,
"trait_type": "Environment Tier"
},
{
"value": "Spikeweed",
"trait_type": "Eastern Resource"
},
{
"display_type": "number",
"value": 3,
"trait_type": "Eastern Resource Tier"
},
{
"value": "Whisper",
"trait_type": "Southern Resource"
},
{
"display_type": "number",
"value": 3,
"trait_type": "Southern Resource Tier"
},
{
"display_type": "number",
"value": 3,
"trait_type": "Plot"
},
{
"value": "First Trip",
"trait_type": "Obelisk Piece"
}
]
},
"timeLastUpdated": "2022-08-31T15:40:43.391Z",
"contractMetadata": {
"name": "Otherdeed",
"symbol": "OTHR",
"totalSupply": "100000",
"tokenType": "ERC721"
}
}
You need to return some information from the blockchain, otherwise nobody can know which source is authoritative.
Typically, people set up the contract to return URLs. (ERC-721 allows URIs, a superset of URLs).
Here is one such URL for the Su Squares contract: https://tenthousandsu.com/erc721/00050.json
And accordingly, that is the URI for the Su Square token with tokenId = 50.
You can query those URLs from the blockchain using JavaScript Web3.
I think this question is not correct because "metadata" is not actually stored in the blockchain but a URI to the metadata itself; So the real question is:
Q: How to read the URI from the token?
A: You will need a Ethereum
RPC Node provider with APIS that read it from the blockchain for you. There are several providers with free tiers that can fulfill your development requirements
Moralis
Infura
Alchemy. <-- someone answered getNFTMetadata
which is a valid answer
As an extra info that might help to better understand this question is, "how does it work?"
Mint: When you mint a token in any NFT Contract, either ERC721 or ERC1155, the smart contract generates a new token with a unique id, stores the ID and then transfers it to the buyer's wallet
Metadata: Contracts have a function that returns the right URI according to a preset "base prefix" (can be a ipfs endpoint or a webserver); to read this you just need the "smart contract address" and "token id"
Example of the metadata function:
const prefix = "www.someendpoint.com/";
const anotherprefix = "ipfs:/"; //if the metadata is hosted in ipfs
function uri(uint256 tokenId)
public
view
override()
returns (string memory)
{
return string(
abi.encodePacked(
prefix + tokenId
)
);
}