Hi All,
I am trying to fetch all the NFTs owned by the connected wallet. I am using this end-point here:
getwalletnfts-1 on the v2 Moralis end points.
My Axios React hook is as follows:
import axios from "axios";
import { useEffect, useState } from "react";
import { useAccount, useNetwork } from "wagmi";
export function useNftForWallets(): {
data: any;
loading: boolean;
error: any;
} {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const { address } = useAccount();
const { chain } = useNetwork();
const [error, setError] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
const { data: response } = await axios.request({
method: "GET",
url: `https://deep-index.moralis.io/api/v2/${address}/nft`,
params: {
chain: `0x${chain?.id.toString(16) || 1}`,
format: "decimal",
},
headers: {
accept: "application/json",
"X-API-Key": process.env.NEXT_PUBLIC_MORALIS_API_KEY,
},
});
setData(response);
} catch (error) {
setError(error);
console.error(error);
} finally {
setLoading(false);
}
};
fetchData();
}, [address, chain]);
return {
data: data,
loading: loading,
error: error,
};
}
However, when I connect my wallet to the app I can see only one NFT shown. (I have 8)
I can see all the 8 NFTs on the block explorer:
In order to recreate this issue you can run the query yourselves on this API call:
getwalletnfts-1
My test wallet address is: 0xe31f4CB714260274Df74dbF1ae1Ca28e7aa746F7
Chain to select: sepolia
In case you are wondering the contract I am using, then the contract is this:
/// @title A title that should describe the contract/interface
/// @author The name of the author
/// @notice Explain to an end user what this does
/// @dev Explain to a developer any extra details
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.4.22 <0.9.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "hardhat/console.sol";
contract NFT is ERC721, ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
address cmmMarketPlace;
constructor(address cmmMarketPlaceAddr) ERC721("NFT", "NFT") {
cmmMarketPlace = cmmMarketPlaceAddr;
}
function transferNFT(string memory _uri) public returns (uint256){
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, _uri);
setApprovalForAll(cmmMarketPlace, true);
return newItemId;
}
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
super._burn(tokenId);
}
function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) {
return super.tokenURI(tokenId);
}
}
and the way I am minting NFTs from react is:
const contract = new web3.eth.Contract(contractAbi, contractAddress);
try {
const response = await contract.methods.transferNFT(metadataIpfsUrl).send({ from: address });
}
Any help will be much appreciated
Let me know if any more information would be necessary