Using a base uri with token id to access metadata

Hi,

I am trying set up an erc721 to access metadata so I can use the same .json file for multiple NFTs.

I have created my .json metadata file link

https://jsonkeeper.com/b/2D2V

I'm using a .json host for testing but proposing to move it IPFS if I can get it working.

I have added a "/" to the end of my https://jsonkeeper.com/b/2D2V to set my base uri to https://jsonkeeper.com/b/2D2V/ so that when it concatenates with token id it should give for example https://jsonkeeper.com/b/2D2V/0 for say token id = 0.

Doing this doesn't read the data for the token from .json file.

I looked on loads of threads and websites but can't see what I am doing wrong as I am not a very experienced programmer.

Any help would be greatly appreciated.

Thanks
Rob

1 Like

You would have to show that part of the code specifically for anyone to help.

Your string should probably have 64 zeros prefixed to ".json"

0000000000000000000000000000000000000000000000000000000000000000.json
0000000000000000000000000000000000000000000000000000000000000001.json

etc

Hi Matt,

Thanks for your reply.

Here is a copy of the code for the .json file and the erc721.

image

image

Again any help would be greatly appreciated.

Rob

Hello Rob

Have checked the standard ?
https://eips.ethereum.org/EIPS/eip-721

[code]
interface ERC721Metadata /* is ERC721 */ {
    /// @notice A descriptive name for a collection of NFTs in this contract
    function name() external view returns (string _name);

    /// @notice An abbreviated name for NFTs in this contract
    function symbol() external view returns (string _symbol);

    /// @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);
}

{
    "title": "Asset Metadata",
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
            "description": "Identifies the asset to which this NFT represents"
        },
        "description": {
            "type": "string",
            "description": "Describes the asset to which this NFT represents"
        },
        "image": {
            "type": "string",
            "description": "A URI pointing to a resource with mime type image/* representing the asset to which this NFT represents. Consider making any images at a width between 320 and 1080 pixels and aspect ratio between 1.91:1 and 4:5 inclusive."
        }
    }
}
[/code]

Also if you search around these forums there are quite a few posts regarding this.

I've been using the ERC1155 and I have not deployed an ERC721

However if your still stuck on this later. I will come and figure it out with you.

If will be something or nothing.

Thanks Matt for your support.

I am now beginning to think that what I am trying to do is not possible with an ERC721.

It looks like I will have to create a separate .json metadata file for each NFT and wrap them into an IPFS folder.

I was hoping to create one .json metadata file as shown in my previous post and looking up the contents of the array within that by appending to token uri to a base URI to create some form of URL which would pull out the metadata for each NFT by using its token id as each token is minted.

Again thanks for your support and any further help would be greatly appreciated.

All the best

Rob

1 Like

You're correct that ERC721URIStorage concatenates the base URI with the token URI.

If I'm understanding your issue correctly, I think the problem you're running into here stems from the challenge of forming a URL that links to a specific section of a single .json file. Accessing a given array within a single .json object is straightforward if you're using Javascript but I'm not sure that there is the equivalent to HTML anchor tags in IPFS for linking directly to arrays within .json files.

If it were me, I would probably stick with the standard implementation and split my data into separate .json files.

1 Like

Thanks iink for your reply.

I agree that does seem the best possible route for this and thanks for confirming the conclusion I was coming to. I just didn't want to miss out on trick if there had of been one available.

Also thanks Matt for your input also.

All the best
Rob

Having one single json file is your issue.
The URI will point to a file. And that file will be loaded. There is no active script in the process to target one specific id within that file.

You need a folder that contain many file
folder/0.json
folder/1.json

Each file contain the metadata for a single NFT.

Once you have all your file in a folder. You upload this folder to IPFS.
Then this hash become your base URI.
so when you fetch an id you get the data for that NFT only.

eg;
https://ipfs.io/ipfs/folder_hash/id

1 Like

Hi Rob,

Did you solve the issue? I am on the same point with a master practise.

I think the trick is modify this function:

function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721URIStorage: URI query for nonexistent token");

        string memory _tokenURI = _tokenURIs[tokenId];
        string memory base = _baseURI();

        // If there is no base URI, return the token URI.
        if (bytes(base).length == 0) {
            return _tokenURI;
        }
        // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
        if (bytes(_tokenURI).length > 0) {
            return string(abi.encodePacked(base, _tokenURI));
        }

        return super.tokenURI(tokenId);
    }

Best regards,

No, I didn't in the end. I had to create an individual .json file for each token uri.

Will give that a go thanks

rojo