How to retrieve the data that ERC1155 is minted with?

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

contract MyTest is ERC1155 {
    uint256 public constant GOLD = 0;

    constructor() ERC1155("https://game.example/api/item/{id}.json") {
        _mint(msg.sender, GOLD, 1, "yaaar 1");
        _mint(msg.sender, GOLD, 1, "yaaar 2");
    }
}

How can I know whether my GOLD token was minted with “yaaar 1” or “yaaar 2”?

You can check and see if balanceOf(msg.sender, GOLD) returns 2, however, it would be unable to tell if the tokens are either yaaar 1 or yaaar 2 since they are in the data field.

Hi, welcome! :wave:

Maybe you can have a check the event:

TransferSingle(operator, address(0), account, id, amount);

Emmmm, I think for the function balanceOf() has two parameters:

function balanceOf(address account, uint256 id) {}

What do you think of it?

1 Like

Right, and thanks for making the correction. This is ERC1155, and balanceOf(msg.sender, GOLD) returns 2.

1 Like

Was anyone ever able to return the data of the ERC1155?

As describe in the code and in the documentation, the data field is used for the onERC1155Received acceptance check. This means that if the receiver is NOT a smart contract, this is just ignored.

If the target (token receiver) is a smart contract, then it must implement the IERC1155Receiver interface. This is what will receive and process the data.

Simply put:

  • The data is not stored onchain by the token contract
  • There is no generic format for this data
  • The data format and usage depends entirely on the receiving contract.

Putting funny message here will in most cases be useless, and in some case cause a revert.

Also note that the data is NOT part of the event, so there is no simple way to capture that at the token level (that is not what it is for)

A typical example of use case for the data field.

Here is an example of transfer with data ... and the detailed trace

  • The sender transfers a bunch of ERC1155 from the OpenSea contract to the ApeGang contract
  • In the same operation, the ApeGang's onERC1155BatchReceived hook is called.
  • This allows the ApeGang to react to the token being received.
  • The data includes merkle proofs that the token sent are in fact of the right collection (and not any random opensea token)
  • That is used to mint new ERC721s in exchange for the old OpenSea ERC1155.

→ You have a seamless migration that is atomic and doesn't require approvals/transferFrom.