Search code examples
blockchainethereum

ERC1155 Token, How To Create ERC1155 Token


I am trying to create a ERC1155 token using openzeppelins's git repositery.

I am trying to write the contract.sol file but I am getting some sort of errors.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";

contract KapilERC1155Token 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() ERC1155("https://game.example/api/item/{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, "");
    }
         function getBalance(address account, uint256 id) external {
             _mint(account, id);
          
      }
    }

Above is my code. Below is My Error

TypeError: Wrong argument count for function call: 2 arguments given but expected 4.
--> ERC1155token.sol:22:14:
|
22 | _mint(account, id);
| ^^^^^^^^^^^^^^^^^^

I can understand that the _mint() which I am using inside the getbalance() requires 4 arguments and the arguments are.

        address account,
        uint256 id,
        uint256 amount,
        bytes memory data

The _mint() is-

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

So I tried passing 4 arguments and got new error while passing the third argument.

    function getBalance(address account, uint256 id, uint256 amount) external {
             _mint(account, id, amount);
          
      }

And the Error I got-

TypeError: Wrong argument count for function call: 3 arguments given but expected 4.
--> ERC1155token.sol:22:14:
|
22 | _mint(account, id, amount);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

This is not working at all. so What i did. I used the same function defined inside the openzepplin's(https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol) contract which I am inheriting inside my contract.

  function balanceOf(address account, uint256 id) external{
             _mint(account, id);
         }

This gives me another Error see below.

1st-

TypeError: Overriding function is missing "override" specifier.
--> ERC1155token.sol:21:10:
|
21 | function balanceOf(address account, uint256 id) external{
| ^ (Relevant source part starts here and spans across multiple lines).
Note: Overridden function is here:
--> https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol:69:5:
|
69 | function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
| ^ (Relevant source part starts here and spans across multiple lines).

Now I am totally stuck while creating ERC1155 token. I am looking for some help. One last thing I am using remix editor to write my contract.


Solution

  • why you _mint inside getBalance function

    1. this is balanceOf function

      function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
               require(account != address(0), "ERC1155: balance query for the zero address");
               return _balances[id][account];
           }
      

      This function is already inside ERC1155 so you don't need to write it

      1. You need to write mint function cause it is internal

        function mint( address account, uint256 id, uint256 amount ) public { _mint(account,id,amount,""); }