In our scripts folder, create new file: create_metadata.py
We will use this script to assign the URI for each NFT.
talk about NFT token URI etc - what, how
create_metadata.py
from brownie import AdvancedCollectible, networkfrom metadata.sample_metadata import metadata_templatefrom pathlib import Pathbreed_mapping ={0:"PUG",1:"SHIBA_INU",2:"ST_BERNARD"}defget_breed(breed_number):return breed_mapping[breed_number]defmain(): advanced_collectible = AdvancedCollectible[-1] number_of_NFTS = advanced_collectible.tokenCounter()print(f"The number of NFTs minted so far is {number_of_NFTS}")for tokenID inrange(number_of_NFTS): breed =get_breed(advanced_collectible.tokenIdToBreed(tokenID)) metadata_filename =f"./metadata/{network.show_active()}/{tokenID}-{breed}.json" collectible_metadata = metadata_templateifPath(metadata_filename).exists():print(f"{metadata_filename} exists! Delete to overwrite")else:print(f"Creating Metadata file: {metadata_filename}") collectible_metadata["name"]= breed collectible_metadata["description"]=f"An adorable {breed} pup!"print(collectible_metadata) image_path ="./img"+ breed.lower().replace("_", "-")+".png" image_uri =upload_to_ipfs() collectible_metadata["image_uri"]= image_uri
call on previous rinkeby deployment
check number of NFTs minted
for each tokenID, get the breed via getbreed(), and structure the filename and path as metadata_filename
collectible_metadata collects the metadata_template to be used as a base for modification
We then check if the required metadata_filename exists, using Path library.
Else, we will create a new one using the template as reference, adding the following
name
description
image URI -> collectible_metadata["image_uri"] = image_uri
image-uri
How do we get the image URI? Currently the images are in our local img folder. We need them to be hosted on IPFS and pass their IPFS URI into collectible_metadata["image_uri"]
The image will be available as long as you are running your ipfs node.
To make the data highly available without needing to run a local IPFS daemon 24/7, you can request that a remote pinning service store a copy of your IPFS data on their IPFS nodes.
Creating metadate files
Now that we have filled all the fields in the metadata file, we are going to write it as a json file into our directory.