Lesson 11 - OpenSea metadata loading besides picture #1073
-
Hi all, Just finished up the NFT lesson, amazing course so far. In the final step where we deploy, create, assign metadata and set a tokenURI for our dog NFT, everything appears to be working, although when I go to check out the NFT created on opensea, the metadata loads except for the picture. Idk if this would have anything todo with it but I used pinata for storing the image/token uri instead of the method shown in the video (was running into some issues) and used a custom method for pulling the tokenURI from pinata based off the breed name instead of hard coding it into a dictionary. If you go into the contract on etherscan, and view the tokenURI of the token at index 0, it displays the metadata properly along with the image URI, and that loads fine there. Any help would be greatly appreciated! Here is the link to the opensea url: https://testnets.opensea.io/assets/0xbcCe6c9Ce29735173b4Ab17b21CFad1bF5900ef0/0 ------create_metadata.py------ from brownie import AdvancedCollectible, network
from scripts.helpful_scripts import get_breed
from scripts.upload_to_pinata import upload_to_pinata
from metadata.sample_metadata import metadata_template
from pathlib import Path
import requests
import json
def main():
advanced_collectible = AdvancedCollectible[-1]
number_of_advanced_collectibles = advanced_collectible.tokenCounter()
print(f"You have created {number_of_advanced_collectibles} collectibles")
for token_id in range(number_of_advanced_collectibles):
breed = get_breed(advanced_collectible.tokenIdToBreed(token_id))
metadata_file_name = (
f"./metadata/{network.show_active()}/{token_id}-{breed}.json"
)
collectible_metadata = metadata_template
if Path(metadata_file_name).exists():
print(f"{metadata_file_name} already exists, delete to override")
else:
print(f"Creating metadata file: {metadata_file_name}")
collectible_metadata["name"] = breed
collectible_metadata["description"] = f"A adorable {breed} Dogie!"
image_path = "./img/" + breed.lower().replace("_", "-") + ".png"
image_uri = upload_to_pinata(image_path)
collectible_metadata["image_uri"] = image_uri
with open(metadata_file_name, "w") as file:
json.dump(collectible_metadata, file)
upload_to_pinata(metadata_file_name) -----set_tokenuri,py------ from lib2to3.pgen2 import token
from brownie import network, AdvancedCollectible
from scripts.helpful_scripts import OPENSEA_URL, get_breed, get_account
from scripts.upload_to_pinata import get_tokenURI
def main():
print(f"Working on {network.show_active()}")
advanced_collectible = AdvancedCollectible[-1]
number_of_collectibles = advanced_collectible.tokenCounter()
print(f"You have {number_of_collectibles} tokenIds")
for token_id in range(number_of_collectibles):
breed = get_breed(advanced_collectible.tokenIdToBreed(token_id))
if not advanced_collectible.tokenURI(token_id).startswith("https://"):
print(f"Setting tokenURI of {token_id}")
set_tokenURI(token_id, advanced_collectible, get_tokenURI(breed))
def set_tokenURI(token_id, nft_contract, token_uri):
account = get_account()
tx = nft_contract.setTokenURI(token_id, token_uri, {"from": account})
tx.wait(1)
print(f"View nft at {OPENSEA_URL.format(nft_contract.address, token_id)}")
print("Wait up to 20 min") -----upload_to_pinata.py------ import os
from pathlib import Path
import requests
PINATA_BASE_URL = "https://api.pinata.cloud"
endpoint = "/pinning/pinFileToIPFS"
headers = {
"pinata_api_key": os.getenv("PINATA_API_KEY"),
"pinata_secret_api_key": os.getenv("PINATA_API_SECRET"),
}
def upload_to_pinata(filepath):
with Path(filepath).open("rb") as fp:
filename = filepath.split("/")[-1:][0]
image_binary = fp.read()
response = requests.post(
PINATA_BASE_URL + endpoint,
files={"file": (filename, image_binary)},
headers=headers,
)
image_uri = f"https://gateway.pinata.cloud/ipfs/{response.json()['IpfsHash']}"
print(response.json())
return image_uri
def get_tokenURI(breed):
get_endpoint = f"/data/pinList?status=pinned&metadata[name]=%{breed}.json"
response = requests.get(PINATA_BASE_URL + get_endpoint, headers=headers)
print(response.json()["rows"][0]["ipfs_pin_hash"])
token_uri = response.json()["rows"][0]["ipfs_pin_hash"]
token_uri_url = f"https://gateway.pinata.cloud/ipfs/{token_uri}"
print(token_uri_url)
return token_uri_url |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hello @catanman-eng Also does this make more sense about how to upload to pinata? I strongly suggest you to compare the code with the advanced collective on the repo: |
Beta Was this translation helpful? Give feedback.
Hello @catanman-eng
Well I've checked the Opensea link and the image is missing indeed, your code appears to be just fine so just as a confirmation
Please share the metada link here (URI), does the link linked to the metadata open correctly by itself?
Also does this make more sense about how to upload to pinata?
I strongly suggest you to compare the code with the advanced collective on the repo:
https://github.com/PatrickAlphaC/nft-demo/tree/main/scripts/advanced_collectible