Getting NFTs from ERC-721 contract on Goerli

Good day there! I’ve encountered a problem when using on of the functions for fetching NFTs from a contract. I am trying to fetch all NFTs from an ERC-721 contract on Goerli, one that I found randomly on OpenSea (“0x0F2D965Db84DFDF2e9F5690eF5E9017C5Ff652fb”). It doesn’t work for this specific address, and another ERC-721 contract address on Goerli that I found. However, if I call the function specifying an ERC-1155 contract, then it works. Can anybody tell me why I cannot fetch the NFTs from an ERC-721 contract?

Code:

const result = await Moralis.Web3API.token.getAllTokenIds({chain: "goerli", address: "0x0F2D965Db84DFDF2e9F5690eF5E9017C5Ff652fb"});
    console.log(result);

Error:

Did you try directly in the docs interface?
What type of contract is that?

It should work without issues with erc721 contracts if the contracts implement the expected interfaces.

I just picked two ERC-721 contracts randomly that I found on OpenSea. The reason for even doing this is that I noticed that when I fetch NFTs from an ERC-721 contract that I deployed, I can get the metadata from the NFTs in the response. However, when fetching the NFTs from an 1155 contract I made on Goerli, it fetches the NFTs, but not any data except for token id and token address. So I was thinking to try and see if there is something wrong with how I constructed my contract or if it was another problem. I tried another 1155 contract on goerli that I found at random on OpenSea. The same happened there, it fetched the NFTs but no metadata. That’s why I wanted to try with ERC-721, to see if it is a problem with Goerli or something else.

The issue is with the metadata with your contract?

Well, I don’t know because I tried to qwuery another person’s contract, and I couldn’t get metadata there either. And when it comes to ERC-721 contracts, I cannot fetch NFTs at all.

Let’s concentrate on a single contract. There could be different reasons from contract to contract.

Did you try resync metadata endpoint? Is the metadata based on token uri? Is the token uri properly displayed in the api output?

Here is an example: https://testnets.opensea.io/assets/goerli/0x95a11aaa0d61f5e390efa5c0495b89816a2821b6/1 This is a link to an NFT I minted on a contract I deployed on Goerli. As you can see, if you click on “details” and then on the “token id” (1), you are redirected to the metadata. So one can conclude that the metadata exists. However, when fetching these NFTs with Moralis SDK, I get the following:

Try resume metadata endpoint for that token id from that contract.

Do you know what should be the token uri for that token id?

From that data it could be the case that token uri is invalid or it is not returned by the contract

It could be that the token uri format is not the one expected for erc1155 standard

I’m not sure what you mean that I should do. This is the code in the contract that I use:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

import "@openzeppelin/contracts/access/AccessControl.sol";

import "@openzeppelin/contracts/utils/Counters.sol";

import "@openzeppelin/contracts/token/common/ERC2981.sol";

contract ST1155 is ERC1155, AccessControl, ERC2981 {

    using Counters for Counters.Counter;

    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

    mapping(uint => string) public tokenURI;

    Counters.Counter private _tokenIdCounter;

    constructor() ERC1155("") {

        _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);

        _grantRole(MINTER_ROLE, msg.sender);

    }

    function mint(address to, uint id, uint amount) public onlyRole(MINTER_ROLE) {

    _mint(to, id, amount, "");

    }

    function mintNew(address to, uint amount, string memory uri, address royaltyRecipient, uint96 royaltyValue) public onlyRole(MINTER_ROLE){

    uint256 id = _tokenIdCounter.current();

    _tokenIdCounter.increment();

    _mint(to,id,amount, "");

    if (royaltyValue > 0) {

        _setTokenRoyalty(id, royaltyRecipient, royaltyValue);

    }

    tokenURI[id] = uri;

    emit URI(uri, id);

  }

    function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)

        public

        onlyRole(MINTER_ROLE)

    {

        _mintBatch(to, ids, amounts, data);

    }

    // The following functions are overrides required by Solidity.

    function supportsInterface(bytes4 interfaceId) public view override(ERC1155, AccessControl, ERC2981) returns (bool) {

        return super.supportsInterface(interfaceId);

    }

}

If you validate the contract then you can call the functions directly in etherscan to see what it returns as token uri.
Does it show the expected metadata on opensea?

I don’t know how to validate the contract right now. I have to fill in a lot of information I don’t know how to to such as contract name and contract address from contracts I have imported from Github.

Regarding metadata, yes it displays on OpenSea.

Try that resync metadata endpoint to see what it returns. You can try it directly in docs interface.

Again I’m sorry but I don’t understand what you mean by that. What docs interface?

docs.moralis.io is the docs interface
Then you will see a tab specific to api

Okay so here is what I have found out:

I can set the URI properly with the contract I have created and deployed. If I use the following code:

const provider = new ethers.providers.Web3Provider(window.ethereum);
const address = "0x95a11AaA0d61f5e390eFa5c0495B89816A2821b6";

const myAbi = [
  "function tokenURI(uint) view returns (string)",
];

// The Contract object
const myContract = new ethers.Contract(address, myAbi, provider);

const tokenURI = await myContract.tokenURI(1);
console.log("tokenURI: " + tokenURI);

I get the following response in the console:

tokenURIresponse

So Ethers.js works to get it. Furthermore, OpenSea both displays the image, name and description hence the metadata (https://testnets.opensea.io/assets/goerli/0x95a11aaa0d61f5e390efa5c0495b89816a2821b6/1). Consequently, there must be a different approach to fetch this data that the Moralis SDK uses, since it doesn’t fetch the metadata, token uri etc.

Is there a way to solve this? I would really like to use your SDK since it simplifies code. However, if I cannot get the information I need through that function call (getAllTokenIds), I won’t be able to use that. Would you please look i to the implementation behind your SDK?

it seems to be something related to that token uri, are you sure that is the exact token uri? no special characters there ?

That is the token uri that I set with the help of Moralis SDK:

const fileInput = document.getElementById("file"); //Get the image file

    const data = fileInput.files[0];
    const imageFile = new Moralis.File(data.name, data);
    await imageFile.saveIPFS();
    const imageURI = "ipfs://" + imageFile.hash();

    const metadata = {
      "name":document.getElementById("name").value,
      "description":document.getElementById("description").value,
      "image":imageURI,
    }

    const metadataFile = new Moralis.File("metadata.json", {base64 : btoa(JSON.stringify(metadata))});
    await metadataFile.saveIPFS();
    const metadata_URI = "https://ipfs.io/ipfs/" + metadataFile.hash();
    console.log("Metadatalänk är: ", metadata_URI);
    console.log("Bildlänk är", imageURI);
    const txt = await mintToken1155(metadata_URI).then(notify);

Sorry some Swedish in there as well, hope you understand.