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, block explorers) for ERC1155 as compared with ERC20 or ERC721.

2 Likes