Skip to content

ERC1155Supply: Add totalSupply() function that returns overall supply count #3301

@philipSKYBIT

Description

@philipSKYBIT

🧐 Motivation
ERC1155Supply only counts by token ID. In some cases we may want a general overall count of all tokens that have been minted under the contract. e.g. etherscan and polygonscan tries to call totalSupply() when showing information about an NFT, and shows an error on the page if it failed.

image

📝 Details
Having only a count for each token ID in ERC1155Supply means to get an overall total count, options are:

  • loop through all minted token IDs and call totalSupply(id)
  • or not use ERC1155Supply and within the ERC1155 contract:
    -- use Counters. But Counters cannot increment by more than 1, which we'd want after a batch mint. Here is an issue about this: Allow Counter increment by more than just 1 #3296
    -- or just use uint256 instead of both ERC1155Supply and Counters

I still want to use ERC1155Supply as its purpose is supply tracking. I propose to add overloaded function totalSupply() that returns the overall count.

Some code that could be added are:

uint256 private _totalSupplyAll;

    function totalSupply() public view virtual returns (uint256) {
        return _totalSupplyAll;
    }

In _beforeTokenTransfer:

        if (from == address(0)) {
            for (uint256 i = 0; i < ids.length; ++i) {
                _totalSupply[ids[i]] += amounts[i];
                _totalSupplyAll += amounts[i];
            }
        }

        if (to == address(0)) {
            for (uint256 i = 0; i < ids.length; ++i) {
                _totalSupply[ids[i]] -= amounts[i];
                _totalSupplyAll -= amounts[i];
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions