Create an ERC1155

In this example we will create an ERC1155.

The following contract is a fixed supply ERC1155 with metadata from GitHub pages (see below for more on metadata). You can replace the uri with your own uri hosting your metadata.
It is based on the example in the documentation:

GameItems.sol

// contracts/GameItems.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

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

contract GameItems is ERC1155 {
    uint256 public constant GOLD = 0;
    uint256 public constant SILVER = 1;
    uint256 public constant THORS_HAMMER = 2;
    uint256 public constant SWORD = 3;
    uint256 public constant SHIELD = 4;

    constructor() public ERC1155("https://abcoathup.github.io/SampleERC1155/api/token/{id}.json") {
        _mint(msg.sender, GOLD, 10**18, "");
        _mint(msg.sender, SILVER, 10**27, "");
        _mint(msg.sender, THORS_HAMMER, 1, "");
        _mint(msg.sender, SWORD, 10**9, "");
        _mint(msg.sender, SHIELD, 10**9, "");
    }
}

We can deploy GameItems using Truffle or Hardhat (see: https://docs.openzeppelin.com/learn/deploying-and-interacting)

OpenSea supports Rinkeby public testnet so we can deploy to Rinkeby (see: https://docs.openzeppelin.com/learn/connecting-to-public-test-networks)

Deployed contract:
https://rinkeby.etherscan.io/address/0x15d90858d519e09f9569Ea2287412c5A1caF491c

We can also deploy using Remix by using GitHub imports (replace npm imports with GitHub reference specifying a release tag)

GameItems.sol (GitHub imports)

// contracts/GameItems.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.2.0/contracts/token/ERC1155/ERC1155.sol";

contract GameItems is ERC1155 {
    uint256 public constant GOLD = 0;
    uint256 public constant SILVER = 1;
    uint256 public constant THORS_HAMMER = 2;
    uint256 public constant SWORD = 3;
    uint256 public constant SHIELD = 4;

    constructor() public ERC1155("https://abcoathup.github.io/SampleERC1155/api/token/{id}.json") {
        _mint(msg.sender, GOLD, 10**18, "");
        _mint(msg.sender, SILVER, 10**27, "");
        _mint(msg.sender, THORS_HAMMER, 1, "");
        _mint(msg.sender, SWORD, 10**9, "");
        _mint(msg.sender, SHIELD, 10**9, "");
    }
}

Metadata

The EIP supports ID substitution by clients (see: https://eips.ethereum.org/EIPS/eip-1155#metadata)

_From: https://docs.openzeppelin.com/contracts/3.x/erc1155#constructing_an_erc1155_token_contract_

The uri can include the string {id} which clients must replace with the actual token ID, in lowercase hexadecimal (with no 0x prefix) and leading zero padded to 64 hex characters.

For simplicity I created metadata and hosted on GitHub pages.
:exclamation: In production you would want to host (or create a server) to appropriately host your metadata.

https://abcoathup.github.io/SampleERC1155/api/token/0000000000000000000000000000000000000000000000000000000000000000.json

https://abcoathup.github.io/SampleERC1155/api/token/0000000000000000000000000000000000000000000000000000000000000001.json

https://abcoathup.github.io/SampleERC1155/api/token/0000000000000000000000000000000000000000000000000000000000000002.json

https://abcoathup.github.io/SampleERC1155/api/token/0000000000000000000000000000000000000000000000000000000000000003.json

https://abcoathup.github.io/SampleERC1155/api/token/0000000000000000000000000000000000000000000000000000000000000004.json

See OpenSea's documentation on Metadata: https://docs.opensea.io/docs/metadata-standards

--

Validate Metadata

Metadata can be validated using OpenSea (replace the contract address and token ID)

https://rinkeby-api.opensea.io/asset/0x15d90858d519e09f9569Ea2287412c5A1caF491c/0/validate/

--

The sample tokens can then be found on OpenSea (Rinkeby)

You can see all of the tokens held by a test address:

Please note that there is less ecosystem support (e.g. wallets such as MetaMask, block explorers such as Etherscan) for ERC1155 as compared with ERC20 or ERC721.

6 Likes

A post was split to a new topic: What is “data” used for in ERC1155 mint

hello, I want to ask how can I change {id}. when I call balanceOf i get …{id}.json
and why its not linked to all token in the smart contract. for example gold to id=1 silver to id = 2 …

Hi, welcome! :wave:

I am not sure what do you mean, do you want to set a new URI?
And I think when you try to call balanceOf(), it will return a number, such as:
balanceOf(user, 0), it will return 1e18.

Do you mean this?

doesn’t work, won’t mint… constant error…

awesome platform, but you all are hoarding up the 1st page on google with unhelpful information…
barely any of your code works

@treelon Hi, welcome! :wave:

So which part do you mean? Which code can not run correctly?

Hi, I want to build a Blockchain Game.

On registration, I want the user profile (dynamic), and all the items to be an ERC1155.

Can they have that function :

Recieving additional items ?
Changing one ERC721 data (with setters) with it ?
Being able to recieve additional coins ? Or you got to hard-code all of that :confused: ?

Thanks, also, I love openzeppelin! Keep the good work! <3

Hello, how do we update the contract so it's not shown as "Unidentified Contract" on opensea?

1 Like

Thank you for pointing out this as well. It must have been something inside OpenSea that they show this contract as "Unidentified Contract". Could you solve this in the meanwhile? I was also asking on OpenSea Discord Channel, but with no luck...

Hey @Vinh_Trinh and @oxuw4. I came across the same issue and just found the root cause: Unidentified contract: OpenSea is unable to "understand" ERC-1155 - #2 by itinance

Long story short: add a public variable called name into your contract (and probably a property symbol as well) and your contract will be shown properly on OpenSea. This here is my working example, based on OpenZeppelin ERC1155: https://testnets.opensea.io/assets/0xfdBd4652390618aca3f60c9B78c4eE9AAFD74914/1

2 Likes

Hello! We are developing an online service (like NFT marketplace). And we want to use a token ERC-1155. There was a problem with understanding the token index (like serial number). Do you have an example of a smart contract that implements the functionality of the token index (number in order)? I mean that:

...
   // ..and the non-fungible index in the lower 128
   uint256 constant NF_INDEX_MASK = uint128(~0);

   // The top bit is a flag to tell if this is a NFI.
   uint256 constant TYPE_NF_BIT = 1 << 255;
...

   function getNonFungibleIndex(uint256 _id) public pure returns(uint256) {
       return _id & NF_INDEX_MASK;
   }

Thanks and best regards!

Sorry for the question I'm still learning about the contracts I'm very beginner but if I want to create new items using the same contract that was already generated in this creation how should I do it?

Is the padding with 64 zeros somehow documented or a Standard from NFT-marketplaces like opensea?
How do you ensure that these marketplaces will format the tokenID in this format?

How can i change the name of the tracker " ERC1155" to my token name here:

ive seen at ERC721 contracts at the tracker name is their token name.

The token standard doesn't support name and ticker I'm afraid. From the EIP-1155 docs:

The symbol function (found in the ERC-20 and ERC-721 standards) was not included as we do not believe this is a globally useful piece of data to identify a generic virtual item / asset and are also prone to collisions. Short-hand symbols are used in tickers and currency trading, but they aren’t as useful outside of that space.

The name function (for human-readable asset names, on-chain) was removed from the standard to allow the Metadata JSON to be the definitive asset name and reduce duplication of data. This also allows localization for names, which would otherwise be prohibitively expensive if each language string was stored on-chain, not to mention bloating the standard interface. While this decision may add a small burden on implementers to host a JSON file containing metadata, we believe any serious implementation of ERC-1155 will already utilize JSON Metadata.

Hey @abcoathup, I want users to be able to create new items after the contract has been minted. I read everything about ID substitution with ERC1155 and checked your examples/links but every implementation I have seen had all items ready before contract creation.

Simply put, I just need a way to store files in the form {server}/{new_item_id}.json to store new metadata objects.

The options I thought of were to (a) create a separate server/database or (b) use a Github repo just for that. But I can't imagine that I'm the only person who is struggling with this and if there's someone who might have seen this it's probably you...

Cheers!

I have read the EIP of erc1155 and I can't understand why we should concatenate the 64 zeros on id token. Is there a technical reason for this?

And finally, thanking you for your precious time, the data argument that is requested in the _mint function has been talked about as something useless. Can you give me an example of the use of this parameter by which it is necessary to arrange it?