Blockchain | How can I read and update an URI assigned to an ERC1155 token?

I received an interesting question regarding the ERC1155 token, so I decided to share it with you!

Question

Where to enter and read values since my token is already created as you understand. […] So the challenge for me is to understanding where I can enter, one value with one URL as many as I want.

Answer

Each token has its own uri in order to provide more metadata about him. Each time you create a new token, a URI event is emitted:

1
2
3
4
5
6
/**
        @dev MUST emit when the URI is updated for a token ID.
        URIs are defined in RFC 3986.
        The URI MUST point a JSON file that conforms to the "ERC-1155 Metadata URI JSON Schema".
    */
    event URI(string _value, uint256 indexed _id);

In order to check that, you can watch these events and then get their data. For your information, here is an example of a URI event emitted after a token creation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
"from": "0x0fdf4894a3b7c5a101686829063be52ad45bcfb7",
"topic": "0x6bb7ff708619ba0610cba295a58592e0451dee2622938c8755667688daf3529b",
"event": "URI",
"args": {
"0": "https://website/idToken",
"1": "3",
"_value": "https://website/idToken",
"_id": "3",
"length": 2
}
}

As described in the interface, you can find the token id and his linked uri as a _value.

Or maybe you prefer directly calling the contract with a getUri(idContract) function. If you want to do so, you can add a getter to your contract, but I would stick to the first solution which is simply listening to the uri events.

From another sight, if you used the same ERC1155 implementation as me (ERC1155Mintable) you should have a setURI function. If you use it to change the token uri, it will trigger a new URI event that you can then catch in order to read the values. For easier read, you could deploy a little watching api waiting for these events and store them in a database. I already did it with Go or NodeJS thanks to web3.

I also found a proposal about an ERC1155Metadata_URI interface which makes the read simpler. However, it is still open: https://github.com/ethereum/eips/issues/1155

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
pragma solidity ^0.5.9;

/**
    Note: The ERC-165 identifier for this interface is 0x0e89341c.
*/
interface ERC1155Metadata_URI {
    /**
        @notice A distinct Uniform Resource Identifier (URI) for a given token.
        @dev URIs are defined in RFC 3986.
        The URI MUST point to a JSON file that conforms to the "ERC-1155 Metadata URI JSON Schema".       
        @return URI string
    */
    function uri(uint256 _id) external view returns (string memory);
}

Last read option I have in mind would be to read the contract’s past events with web3 like so: https://web3js.readthedocs.io/en/v1.2.9/web3-eth-contract.html#getpastevents

I hope I answered your question. If not, feel free to tell me and I will try again. It’s always a pleasure to help!

Have a great day!

Built with Hugo
Theme Stack designed by Jimmy