Can't enquire balance of ERC1155 contract

Hi, I created an ERC1155 contract and deployed it to Polygon Mumbai testnet. The contract just mints 5 NFT's as a test

//SPDX-License-Identifier: UNLICENSED

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/Context.sol";

pragma solidity ^0.8.0;

contract MyContract is ERC1155, Ownable {

//public unint16 totalTokens = 5;
string public name;
string public symbol;

constructor() ERC1155("https://bafybeic3s5lnqldp2poeoyilwhxf672huzqvjk4imd2effxxbjpycnir6a.ipfs.dweb.link/{id}.json") {
    //this should be in a for loop countind down from totaltokens to 0.
    _mint(msg.sender, 0, 1, "");
    _mint(msg.sender, 1, 1, "");
    _mint(msg.sender, 2, 1, "");
    _mint(msg.sender, 3, 1, "");
    _mint(msg.sender, 4, 1, "");

    name = "Live NFT test";
    symbol = "LFTT";

    super;
}

function uri(uint256 tokenId) override public view returns(string memory){
    return(
        string(abi.encodePacked(
            "https://bafybeic3s5lnqldp2poeoyilwhxf672huzqvjk4imd2effxxbjpycnir6a.ipfs.dweb.link/",
            Strings.toString(tokenId),
            ".json"
        ))
    );
}

I can see the deployment and 5 minting transactions on polygonscan but when I try to enquire the balance of a token using online tools (like ethplorer) it shows as 0 and when I try to import a token to metamask it shows me a warning that the balance could not be loaded.

I created a script in ethers.js to try and enquire the balance myself and see what was returned and I received an object as follows:

BigNumber { _hex: '0x01', _isBigNumber: true }

In ethers.js I am using the following ABI:

const ERC1155_ABI = [
  "function name() view returns (string)",
  "function symbol() view returns (string)",
  "function balanceOf(address, uint256) public view returns (uint256)",
  " function safeTransferFrom(address, address, uint256, uint256, bytes) public",
];

Does anyone know if I am doing something wrong or why my token balances cannot be enquired, it's driving me nuts!! Thanks in advance!

Your code seems correct. You have check your status here https://mumbai.polygonscan.com/. Also while importing token in matamask, make sure you are connected to mumbai testnet.

Hi Keerthana, thanks for the reply. I already checked the contract in polygonscan as I mentioned and it is there.I can also see the 5 tokens that were minted but even in Polygonscan it has a small exclamation mark next to Total Supply. You can see the actual contract here: https://mumbai.polygonscan.com/token/0x113E1797Df35651DA10EF5a21C682408F75452Ed
You can also see that polygonscan recognises it is an ERC1155 contract.

When I try to import the tokens into Metamask (using the same wallet and account that deployed the contract and minted the tokens - therefore the current owner of the tokens) It asks for the contract address and automatically fills in the correct symbol from the contract but it then asks for the decimal (ERC1155 doesn't have decimals so I enter 0). So it does see the contract but it cannot read the balance of the tokens.
Does the openzepelin balanceOf function need to be overridden or something?

Hello @tonycastle

When I query the contract using remix I get the balances correctly.

Are you sure you have configures ethplorer to look on mumbai and not on mainnet ?

Metamask does not support ERC1155 natively, and wont be able to display them.

That's super helpful, thank you. Can I just ask one further question. Can I use metamask wallet as a signer for an ERC-1155 contract. I have written an application with ethers.js that calls the safeTransferFrom function and it fails with 'transaction failed'. "CALL EXCEPTION". every time. Strangely it worked on mumbai testnet. I have set the gas fees way above the current network gas fees so it looks like the contract is just not doing the transfer. But no idea why. I'm currently trying to research providing better error messages from the contract but I'm very new to blockchain. :slight_smile:

Yes, you can basically sign anything with metamask. Its just that metamask doesn't know what to look form or sign on its own, so you need an app (ethers.js is good for that) to tell metamask what tx to make.

1 Like

Thanks a lot. In the end it turns out I had hard coded a tokenID when testing and not changed it back to a variable so the contract was reverting the transfer. It's always the small things!! Thanks again for your time and help. Your answer pushed me to recheck my own code and got me over the line :slight_smile: