Add Metadata
Following the previous two tutorials, we created our contract and minted an NFT with ID = 1337. However, our minted NFT has no metadata associated with it. Without metadata, an NFT token is nothing but a token with an ID.
Our smart contract implements the ERC721 meta extension, and it has the following method for getting the metadata URI of an NFT token:
/// @notice A distinct Uniform Resource Identifier (URI) for a given asset.
/// @dev Throws if `_tokenId` is not a valid NFT. URIs are defined in RFC
/// 3986. The URI may point to a JSON file that conforms to the "ERC721
/// Metadata JSON Schema".
function tokenURI(uint256 _tokenId) external view returns (string);
Prepare Metadata
The metadata of an NFT token is presented in a JSON file. Here is an example:
{
"description": "My NFT",
"image": "https://example.com/1337.png",
"name": "Hacker's Token 1337",
"attributes": [
{
"trait_type": "Mouth",
"value": "Surprised"
},
{
"trait_type": "Level",
"value": 5
}
]
}
You should create one metadata JSON for each of your NFT tokens. Let's assume you are hosting all the metadata JSON files on the host of my-nft-metadata.com
, and the metadata JSON of each NFT token should have the following format:
https://my-nft-metadata.com/1337
Set The Base URI
After uploading all the metadata JSON files somewhere and hosting them with the same base URI, we can set the base URI of the contract. Again, we can use the cast
command:
FOUNDRY_PROFILE=dcc_testnet \
cast send --private-key=XXXXXXXXXX \
--legacy \
0xd382De234f8d3B35823f1DB54C85cE6498a9DbC84c \
'setBaseURI(string)' "https://my-nft-metadata.com/"
Let's verify that our base URI works as expected. We use cast
to get the tokenURI
of the token of 1337
:
FOUNDRY_PROFILE=dcc_testnet \
cast call 0xd382De234f8d3B353f1DB54C85cE6498a9DbC84c \
'tokenURI(uint256)(string)' 1337
You should see the following URI printed out:
https://my-nft-metadata.com/1337
Last updated