[SOLVED] getNFTsForContract(options) with token_id not working anymore

Until two days ago this was working just fine. Today not anymore. Probably something was changed and now my application doesn’t work and I have to find another way. Anyone knows why this isn’t working anymore?

const options = {
    chain: CHAIN,
    address: myAddress,
    token_address: CARTERO_NFT,
    token_id: cardId
  };
  console.log(options)
  const balance = await Moralis.Web3API.account.getNFTsForContract(options);

There isn’t that parameter named token_id for getNFTsForContract, you can use getTokenIdMetadata if you want information for a specific token id and token address

Until two days ago it was working just fine and use to return the balance of specific token id. My app was built relying on that. Now suddenly stopped to work. I need to know exactly the balance of a token id without going through a loop.

can you give an exact example with exact values to those parameters?

Here is the full function that I’ve been using for the last two months:

async function getBalances( cardId ) {
const options = {
chain: CHAIN,
address: myAddress,
token_address: CARTERO_NFT,
token_id: cardId
};
console.log(options)
const balance = await Moralis.Web3API.account.getNFTsForContract(options);
if( balance.result.length > 0 ) {
return balance.result[0].amount;
} else {
return 0;
}
}

you can make a read only call to the smart contract with a specific token id and owner too

/**
        @notice Get the balance of an account's tokens.
        @param _owner  The address of the token holder
        @param _id     ID of the token
        @return        The _owner's balance of the token type requested
     */
    function balanceOf(address _owner, uint256 _id) external view returns (uint256);

Thanks! but I was just trying to avoid calling the contract directly due to the delay :frowning:

we will have to check

can you check again now?

I’ll do it right now!

Yes, it is working just fine right now:

Bellow you can see the code that I temporarily wrote in order to keep my app working.

async function getBalances( cardId ) {

/* old code that doesn't work anymore */
const options = {
    chain: CHAIN,
    address: myAddress,
    token_address: CARTERO_NFT,
    token_id: cardId
  };
  const balance = await Moralis.Web3API.account.getNFTsForContract(options);
  if( balance.result.length > 0 ) {
    console.log(balance)
    return balance.result[0].amount;
  } else {
      return 0;
  }
  

  /*
//new ugly code for getting the cards amount
let allUserCards = await getAllUserCards();

if (allUserCards['result'].length) {
    for(let i = 0; i < allUserCards['result'].length; i++) {
        if(  allUserCards['result'][i].token_id == cardId){
            return allUserCards['result'][i].amount;
        }
    }
}
return 0;
*/

}

Here is the full object returned after you fixed the problem:

{
“total”: 1,
“page”: 1,
“page_size”: 100,
“cursor”: null,
“result”: [
{
“token_address”: “0x5f312161aaf0beffe514177118be47dd81cc8412”,
“token_id”: “72”,
“owner_of”: “0xa1805583f58f645f60bc345972a4de0fcd9ea598”,
“block_number”: “20932604”,
“block_number_minted”: “20057563”,
“token_hash”: “9c76532db048c919ff28879210f0d9aa”,
“amount”: “3”,
“updated_at”: “1657441434.569”,
“contract_type”: “ERC1155”,
“name”: null,
“symbol”: null,
“token_uri”: “https://5oxgrye0adsz.usemoralis.com/{id}.json”,
“metadata”: null,
“last_token_uri_sync”: “2022-06-11T10:41:35.182Z”,
“last_metadata_sync”: “2022-06-11T12:53:46.688Z”
}
],
“status”: “INSERTING”
}

Thank you so much! This kind of help makes a HUGE DIFFERENCE.

1 Like