Writing and deploying first ERC1155 contract using Remix

ERC1155 smart contract can represent any number of fungible and non-fungible token types. Existing standards such as ERC-20 require deployment of separate contracts per token type. The ERC-721 standard’s token ID is a single non-fungible index and the group of these non-fungibles is deployed as a single contract with settings for the entire collection. In contrast, the ERC-1155 Multi Token Standard allows for each token ID to represent a new configurable token type, which may have its own metadata, supply and other attributes.
For Bacics of ERC 1155 You can refer to Introduction to ERC1155 .

Goal

  1. Create a barebone 1155 compliant contract.
  2. Deploy contract on TestNet.
  3. Interact with deployed contract.

Requirements

  1. Chrome brower
  2. Metamask Wallet

Approach

. To Deploy contract, we use:

  1. Remix as online IDE
  2. OpenZeppelin’s contract
  3. MetaMask as wallet
  4. Remix to deploy contract

Steps to create ERC1155 Contract.

  1. Open remix online code editor.

  2. Create a file “MyNftContract.sol”.
    1

  3. Then write barebone ERC1155 Contract using OpenZeppelin.

OpenZeppelin has largest collection of open source contracts.
OpenZeppelin Contracts helps you minimize risk by using battle-tested libraries of smart contracts for Ethereum and other blockchains. It includes the most used implementations of ERC standards.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

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

import "@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC1155, Ownable {

    constructor() ERC1155("") {}

    function setURI(string memory newuri) public onlyOwner {

        _setURI(newuri);

    }

    function mint(address account, uint256 id, uint256 amount, bytes memory data)

        public

        onlyOwner

    {

        _mint(account, id, amount, data);

    }

    function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)

        public

        onlyOwner

    {

        _mintBatch(to, ids, amounts, data);

    }

}

Let’s understand the code,

  • First line specifies SPDX license type, which is added after Solidity ^0.6.8. Whenever the source code of a smart contract is made available to the public, these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special value UNLICENSED or simply skip the whole comment (it will not result in an error, just a warning).

  • Then the solidity version is declared.

  • Further, two contracts are imported from OpenZepplin

    • First one is ERC1155, it’s a ERC1155 compliant contract, you can also read about the contract on github.
    • Second one is Ownable, it’s an access control contract and have modifiers that can make functions accessible to a specific user.
  • Then created a contract named “MyToken” that inherits ERC1155 and Ownable and declared a constructor.

  • Created three functions that can be called only by the owner of contract.
    setURI() will set URI of metadata and mint() and mintBatch() will make new tokens.

  1. Compile created contract by clicking on Compile button referenced below.
    image
  2. Now connect metamask wallet to remix.
    If you dont have metamask wallet you can install chrome extension from chrome web store and follow the instructions.
    Once you’re done setting up your wallet, switch chain to Goerli test network and make sure you have testnet ether, if you dont have have ether you can get if from goerlifaucet.
    On “DEPLOY & RUN TRANSACTIONS” window on Remix set ENVIRONMENT to “Injected Provider - MetaMask”. A metamask pop will appear click on connect.
    image
  3. After successful connection, select your contract on “DEPLOY & RUN TRANSACTIONS” window and click Deploy.
    Again a metamask pop up will appear click confirm to send transaction.
    image
    After click transaction will take some time to confirm.
    If the contract is successfully deployed, you will be able to see all the available functions of the contract.
  4. Once you have deployed an ERC 1155 contract, you are ready to mint tokens.
    You will always need contract address to interact with it so you can save it for yo
    ur ease.
    image
  5. You can mint tokens directly from Remix by clicking mint function on deployed contracts section.
    image
    On Metamask pop-up, click Confirm.

image
Now we have successfully deployed our contract and minted ERC1155 tokens in it.

Refrences

[1] Introduction to ERC1155 .
[2] OpenZeppelin Contracts.
[3] ERC-1155: Multi Token Standard (ethereum.org)