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:

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.

4 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

1 Like

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?