useNFTBalances returns "empty balance"

This is the answer I’m getting:
{
“total”: 0,
“page”: 0,
“page_size”: 500,
“cursor”: “”,
“result”: [],
“status”: “SYNCED”
}

If I call the contract directly through Remix, balanceOf(my address) returns 1, however…any idea what could be the reason behind this?

How long ago was the nft minted and can you show us how you are calling useNFTBalances?

this is how I’m calling it:

const { getNFTBalances, data, error, isLoading, isFetching } = useNFTBalances();

  return (
    <div>
      {error && <>{JSON.stringify(error)}</>}
      <button onClick={() => getNFTBalances({ params: { chain: "0x4", address: "0xb1Fd6b8de50cdfA72a2b809605a8E2a5754b7c27" } })}>
        Refetch NFTBalances
      </button>
      <pre>{JSON.stringify(data, null, 2)}</pre>
    </div>
  )

and the nft was minted a little over an hour ago:
here’s the contract on ethscan: https://rinkeby.etherscan.io/address/0xaB4e9BFD0Eb1727b1a643602709e6d123404899A

Hm, I don’t see anything wrong with your code. Could you try calling with this address 0x1685Bb9d8Ad57B78da8D5b8c649a974e36f19202 - it should have some nfts on it and if it provides any results it means the rinkeby nodes are a bit slow atm

that address worked perfectly…maybe the issue is with my smart contract code?

It is possible I guess. Could you share the contract if it isn’t too long or at least the minting function?

function addNewToken(string memory _uri) public uniqueURI(_uri) onlyCreator {    
        uint256 newId = createToken(_uri, msg.sender);
        uriOriginalToken[_uri] = newId;
    }

function createToken(string memory _uri, address _creator) private  returns (uint256){
      uint256 newId = totalSupply() + 1;
      _mint(_creator, newId);
      tokenCreator[newId] = _creator;
      tokenToURI[newId] = _uri;
      return newId;
    }

function _mint(address _to, uint256 _tokenId) internal {
    require(_to != address(0));
    addToken(_to, _tokenId);
    emit Transfer(address(0), _to, _tokenId);
  }

function addToken(address _to, uint256 _tokenId) private {
    require(tokenOwner[_tokenId] == address(0));
    tokenOwner[_tokenId] = _to;
    uint256 length = balanceOf(_to);
    ownedTokens[_to].push(_tokenId);
    ownedTokensIndex[_tokenId] = length;
    totalTokens = totalTokens.add(1);
  }

function balanceOf(address _owner) override public view returns (uint256) {
    return ownedTokens[_owner].length;
  }

function tokensOf(address _owner) public view returns (uint256[] memory) {
    return ownedTokens[_owner];
  }

these are the relevant functions I think…please let me know if you need more of the code…btw this code is from SuperRareV1’s smart contract, with a few edits to make it compatible with later solidity version

I see, you didn’t use openzeppelin?

I copied and pasted their code from ethscan…they used openzeppelin in the source code though.
I think what I need to fix this is to know what interface my smart contract needs to support exactly…it shouldnt be required to use openzeppelin, right?

I think so, yeah, because I am pretty sure that is how the blockchain is indexed for nfts - using the standardized functions etc. (which is basically openzeppelin)

so maybe I’m not emitting the right events?

I am actually not 100% sure what’s wrong, but it will definitely work if you use the openzeppelin nft token contract standards

ok, thanks!
I will try that and let you know if it worked

1 Like

it is working now that I’m using open zeppelin’s latest. Thanks!

1 Like