Blockchain | ERC1155 101

Alright guys, today I’m gonna share with you my favorite discovery of the week: the ERC1155 Token Standard! In this article, we will learn what an ERC1155 (official Ethereum token standard) contract is, and secondly we will deploy it and play with! Let’s go!

Overview

What is an ERC1155?

A standard interface for contracts that manage multiple token types. A single deployed contract may include any combination of fungible tokens, non-fungible tokens or other configurations (e.g. semi-fungible tokens). From: https://github.com/ethereum/EIPs/issues/1155

We can use it when we want to tokenize many little assets. For instance, in case of a big building, we still can imagine deploying a specific contract just for him, due to his value and the amount of token generated. But what about soccer cards, game items or books (in case of book sharing), and so forth. That means a lot of calls (hi-frequency) on each contracts linked to specific assets. It also implies a lot of writings operation on the blockchain, which cost a lot. Actually, the processing time is dependent on the number of assets, and that’s a big scalability problem.

ECR1155 brings a solution to this use case: it allows us to manage all these things inside one contract. Which is pretty cool because we don’t have to manage many. Another big feature is the bulk update: we can easily update many tokens in the same transaction, and by so improve the scalability.

Doing many operations in one call/action is called a batch/bulk.

Deployment with Remix

As always, you can also deploy this contract with Truffle, but I chose Remix in this article.

First step, go to Remix and import the following contracts

erc1155-contracts

You can find the contracts on Enjin’s official repository.

  • Who’s Enjin? ERC1155’s creators.

Then compile ERC1155Mintable with the 0.5.15 compiler (double check the compiler, some versions are buggy and can generate illogical errors).

erc1155-contracts

But why using the ERC1155Mintable and not simply the ERC1155? Cuz ERC1155 is an implemented specification, it is cleaner to leave this contract as his original form and then create another contract with your specific logic. Somehow not so far from the Separation of Concerns ;) If you need to change something, you’ll only update your own contract and not the ERC1155. It’s easier to understand, maintain and limit the risk of doing something bad. For this example, I chose the ERC1155Mintable but you can create your own or just use another one provided by Enijn like ERC1155MixedFungibleMintable for instance.

“Ok ok, gotcha, everything sounds fine to me, except two things: what does Mintable and Fungible mean?”

Fungible

Fungibility implies that two things are identical in specification, where individual units can be mutually substituted. from investopedia.com

The gold is fungible, one hans of gold has the exact same value than any other hans of gold. Cars are not fungible, you can’t easily exchange two cars from different brands with different miles, they are different.

Mintable

Most mintable tokens are ERC20 tokens with an additional mint function whil helps to increase the supply. This means that though you can define the initial supply in the contract, it is not fixed. from quora

Can’t wait! Let’s deploy it now!

erc1155-contracts

Be careful to choose the good contract like on the screenshot. Cuz many dependent contracts have been compiled in the same time! So, if you deploy Address - browser/ERC1155Mintable.sol you will only see Address’s functions and it will be obviously impossible for you to call ERC1155Mintable’s functions from Remix.

We can now interact with the functions below. Are you ready for creating your first token? “Oh yeahh, love the idea!”

To do so, use the create function: Parameters

  • Supply: Set a supply -> the number of generated tokens.

  • URI: We don’t need it at this time. Just remember that it is from this URI that external services will have an access to your token information(JSON format), like the token name, the functions, the image, etc… For example, an exchange can display your token’s information thanks to this URI.

The URI value allows for ID substitution by clients. If the string {id} exists in any URI, clients MUST replace this with the actual token ID in hexadecimal form. This allows for large number of tokens to use the same on-chain string by defining a URI once, for a large collection of tokens. Example of such a URI: https://token-cdn-domain/{id}.json would be replaced with https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json if the client is referring to token ID 314592/0x4CCE0. from github

erc1155-contracts

As described in the documentation, you need to catch the transaction event in order to know your new token ID. As you can see, it’s easy to find the event’s information in the Remix console.

1
2
3
4
5
6
"_operator": "0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c",
"_from": "0x0000000000000000000000000000000000000000",
"_to": "0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c",
"_id": "1",
"_value": "2000",
"length": 5

Good news! By knowing this ID, you can now get the balance of a specific account by passing the concerned address with the token ID.

erc1155-contracts

One more operation! Let’s transfer some tokens to a new account and then get the new balance:

erc1155-contracts

Be careful with the address, you can’t just change a bit the Ethereum address, you have to generate a valid new one. For testing purpose, you can use vanity-eth.tk.

Alright, enough today for this quick presentation of what an ERC1155 is. You know now what an ERC1155 is, why use it, how to deploy it and even how to play with the main features. We will maybe see some advanced functions later, but don’t worry, it is not mandatory to start using it ;)

Thanks for reading, hope you learn something and have a great day!

Additional references

Built with Hugo
Theme Stack designed by Jimmy