4

How do I identify what token types have been created for a specific ERC1155?

I noticed the balanceOf function takes an address (owner) and the token type, but what if I dont know what all token types have been created? Do i need to look at all the past Events ever triggered for the specific ERC1155 contract? Or is there a way to get the mapping directly?

ie: is there a way to get the _balances variable in ERC1155 that is of type

mapping (uint256 => mapping(address => uint256)) private _balances;
5

So the question was an underlying question while trying to understand how all these platforms like OpenSeas are identifying all of the minted tokens for standard ERC721 and ERC1155 contracts. It was perplexing because there's no "getAllMintedTokens()" method in the standard smart contracts.

So without further adieu, the answer is by searching for all past events of type "TransferSingle". They then parse through the results and fetch the metadata using the metadata methods that are standard in ERC721/ERC1155.

For those curious about the technical how-to. Keep reading.

let transferEvents = await mega.getPastEvents('TransferSingle', { fromBlock: 0, toBlock: 'latest' });

The result of this is an array of events. There's a property called returnValues that contains an id and a value which correspond to the tokenId and the numberOfTokensToMint. And then a to property which corresponds to the recipient address.

The full data for a single element in the array looks like the JSON below.

{
  address: '0xAa45AcDa319377397805437340fdf70F730090f2',
  blockHash: '0x89b17a2efb883c731679681d06e5f694c345059277ac2a634fada532e3c35171',
  blockNumber: 8247859,
  logIndex: 1,
  removed: false,
  transactionHash: '0x65bd36cc73797bc09c86f9c7775f81fde5b641c94d08fdb4dde561ae9396c9e2',
  transactionIndex: 1,
  id: 'log_6c71ba76',
  returnValues: Result {
    '0': '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE',
    '1': '0x0000000000000000000000000000000000000000',
    '2': '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE',
    '3': '1',
    '4': '1',
    operator: '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE',
    from: '0x0000000000000000000000000000000000000000',
    to: '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE',
    id: '1',
    value: '1'
  },
  event: 'TransferSingle',
  signature: '0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62',
  raw: {
    data: '0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001',
    topics: [
      '0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62',
      '0x000000000000000000000000d954f4513bde1e00f3986630a7e73c4f9aa564fe',
      '0x0000000000000000000000000000000000000000000000000000000000000000',
      '0x000000000000000000000000d954f4513bde1e00f3986630a7e73c4f9aa564fe'
    ]
  },
  args: Result {
    '0': '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE',
    '1': '0x0000000000000000000000000000000000000000',
    '2': '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE',
    '3': BN { negative: 0, words: [Array], length: 1, red: null },
    '4': BN { negative: 0, words: [Array], length: 1, red: null },
    __length__: 5,
    operator: '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE',
    from: '0x0000000000000000000000000000000000000000',
    to: '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE',
    id: BN { negative: 0, words: [Array], length: 1, red: null },
    value: BN { negative: 0, words: [Array], length: 1, red: null }
  }
}
2
  • This should be the accepted answer Aug 23 at 12:36
  • 1
    Is there a way to find the address of the tokens? I want to call the uri() function of 1155 but that requires an address of the nft I think Aug 23 at 12:42
4

The common way to know the number of token types (aka. tokenId) is to have a counter inside the ERC1155 contract, for instance:

uint256 private _currentTokenID = 0;

This way, whenever you need to create a new token, you use the current one and then increment it by one. It also allows you to do a loop in case you need to retrieve a list of all current tokenIds.

This is how the majority of implementations work, so you can have access to this counter even if it is declared as private.

In case there is no counter and the tokenIds are assigned externally (i.e.: through a front-end), then you may have to go through the events alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.