Building event tickets with ERC-1155 contract

Image courtesy Preethi Kasireddy

ERC-1155 is the new so-called multi-token standard. Technically, we called it the fungible-agnostic standard because the interface can handle both fungible and non-fungible tokens in a single contract “natively” without any kind of hack. We will look at how we can implement a fictitious hackathon event issuing passes as Ethereum tokens.

ERC-1155 can handle fungible-agnostic tokens in a single contract without resorting to ERC-20 for fungible and ERC-721 for non-fungible tokens.

Fungible-agnostic means the quality to be both fungible (breakable into small units and thus interchangeable) and non-fungible (atomic, non-breakable a.k.a NFTs)

ERC-1155’s balanceOf(address account, uint256 id) differs from ERC20’s and ERC777’s — it has an additional idargument. This idis not a conventional “token ID” of an NFT but a type ID telling us which type of token we are referring to.

Unlike ERC-721, whose balanceOf(address account) only counts the amount of a single token type represented by a single contract, ERC-1155 contract can hold many types of tokens, each with its own type id. Non-fungible tokens are then implemented by simply minting a single one per type (one idfor one NFT, thus is unique).

Batch operators

The ERC-1155 standard provides two methods, balanceOfBatchand safeBatchTransferFrom, making querying multiple tokens’ balance and transferring simpler and less gas-intensive. Especially the standard provided the function _mintBatch , a counterpart of _mintwhich allows batch minting of several token ids of any amount. For example, to batch-mint 100 NFTs, you would call it with an array of type IDs and amounts for each type:

ERC1155._mintBatch(
receiverAddress,
[1, 2, 3, 4, 5, 6, 7, 8, /* ... */, 105],
[1, 1, 1, 1, 1, 1, 1, 1, /* ... */, 1],
""
);

The last parameter is a data field that can be left as an empty string for now.

Event tickets example

To give you an example of minting event tickets as NFTs. As we sell the tickets, we do not necessarily want to mint each token for each sale. We may have a tiny server to track who bought the tickets and mint them all just before the event.

Here is an implementation of three types of event passes — General, RSVP, and VIP:

As you can see, the VIP ticket is an NFT because only a single one is minted, while the GENERAL (for general admission) and RSVP (for reserved seating) are fungible tokens.

We could, of course, replace the 3 calls to _mint with a single call to _mintBatch like so:

_mintBatch(
msg.sender,
[GENERAL, VIP, RSVP],
[10**2, 1, 20],
“”
);

Conclusion

If you are in the position to start a token project from scratch, it is much easier and simple to build on the ERC-1155 for extensibility and maintainability.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store