ERC-1155 – Why is ERC1155 Total Supply Not Showing?

contract-debuggingcontract-developmenterc-1155remixsolidity

I want to be able see the total supply of each token. I have imported the extension and followed the erc1155 documentation with regards to totalSupply, however when deploying on Remix and calling total supply it shows as 0 even after minting? I cannot figure out how to be able to show how many of each id has been minted?
Here is my contract code, thanks.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

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

contract MyToken is ERC1155, Ownable {
        uint256 public constant Poloy = 1;
        uint256 public constant Leafy = 2;

        mapping (uint256 => uint256) public tokenSupply;

    constructor()
        ERC1155("https://ipfs.io/ipfs/3bz4r7c6q3fjmt2uoicyibx2nhf7q6gtsdgze277wzlnadbvfme/{id}.json") {}

//make sure opensea can read the link
    function uri(uint256 _tokenId) override public pure returns (string memory){
        return string(
            abi.encodePacked(
            "https://ipfs.io/ipfs/bafybeid3bz4r7c6q3fjmt2uoicyibx2nhf7q6gtsdgze277wzlnadbvfme/",
            Strings.toString(_tokenId),
            ".json"
        )
        );
    } 

    // Minting - If you do not have any poloy the contract will let you buy one
        function mintPoloy() public{
        require(balanceOf(msg.sender,Poloy) == 0,"you already have a polo ");
        require(balanceOf(msg.sender,Leafy) == 0,"you already have a token cheaty cheater ");
        _mint(msg.sender, Poloy, 1, "0x000");
}

    // Minting - If you do not have any leafy the contract will let you buy one
        function mintLeafy() public {
        require(balanceOf(msg.sender,Leafy) == 0,"you already have a leaf ");
        require(balanceOf(msg.sender,Poloy) == 0,"you already have a token cheaty cheater ");
        _mint(msg.sender, Leafy, 1, "0x000");
}


function totalSupply(uint256 id) public view returns (uint256) {
    return tokenSupply[id];
  }   

}

Best Answer

your tokenSupply was never incremented. I did a few changes to your contract:

  • I added an increment on each function
  • Since your code was duplicated, I moved it all in a private function
  • I reduced the number of error messages (it costs a lot). This will optimize the size of your code

Try to make sure that your error messages don't exceed 32 characters (32bytes) otherwise it will be stored on 64bytes.

function mintPoloy() public {
    require(_hasMinted(), "Already minted cheater!");
    tokenSupply[Poloy]++;
    _mint(msg.sender, Poloy, 1, "0x000");

}

function mintLeafy() public {
    require(_hasMinted(), "Already minted cheater!");
    tokenSupply[Leafy]++;
    _mint(msg.sender, Leafy, 1, "0x000");
    
}

function _hasMinted() private view returns (bool) {
    return
        ((balanceOf(msg.sender, Poloy) + balanceOf(msg.sender, Leafy)) > 0)
            ? false
            : true;
}
Related Topic