Creating an ERC721 and an ERC1155 Token

Posted By : Pankaj

May 30, 2023

What are ERC721 and ERC1155 Tokens


ERC721 tokens are non-fungible tokens (NFTs) that represent one-of-a-kind assets. Each token is unique and cannot be traded for any other ERC721 token. For example, if you develop an ERC721 token that represents a work of art, each token represents a unique work of art that cannot be swapped for another work of art.

 

ERC1155 tokens, on the other hand, are semi-fungible. They let you create a batch of tokens, each with its own unique ID but identical metadata. This means that each token can be traded for another token from the same batch, but not for a token from a different batch. For example, if you build an ERC1155 token that represents a specific sort of sword in a game, you can produce a batch of 100 tokens, each with its own unique ID but identical metadata (i.e., the same qualities and abilities).

 

Developing an ERC721 Token

 

Step 1: Setting up the Remix IDE

 

-  Open the Remix IDE in your web browser by visiting https://remix.ethereum.org/.

-  On the Remix IDE interface, you'll find the file explorer panel on the left-hand side. Click on the "+" button to create a new file.

-  Name the file "MyToken.sol" (or any name you prefer) and click on "OK."

 

Step 2: Writing the ERC1155 Token Contract

 

pragma solidity ^0.8.0;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ERC721Token {
    string private _name;
    string private _symbol;
    mapping(uint256 => address) private _tokenOwners;
    mapping(address => uint256) private _tokenBalances;
    constructor(string memory name, string memory symbol) {
        _name = name;
        _symbol = symbol;
    }
    function name() public view returns (string memory) {
        return _name;
    }
    function symbol() public view returns (string memory) {
        return _symbol;
    }
    function balanceOf(address _owner) public view returns (uint256) {
        return _tokenBalances[_owner];
    }
    function ownerOf(uint256 _tokenId) public view returns (address) {
        address owner = _tokenOwners[_tokenId];
        require(owner != address(0), "Token does not exist");
        return owner;
    }
    function mint(address _to, uint256 _tokenId) public {
        require(_tokenOwners[_tokenId] == address(0), "Token already exists");
        _tokenOwners[_tokenId] = _to;
        _tokenBalances[_to]++;
    }
    function transfer(address _to, uint256 _tokenId) public {
        address owner = ownerOf(_tokenId);
        require(owner == msg.sender || msg.sender == address(this), "Unauthorized transfer");
        _transfer(owner, _to, _tokenId);
    }
    function _transfer(address _from, address _to, uint256 _tokenId) internal {
        require(_tokenOwners[_tokenId] == _from, "Token does not belong to sender");
        _tokenOwners[_tokenId] = _to;
        _tokenBalances[_from]--;
        _tokenBalances[_to]++;
    }
}


Step 3: Click on the "Deploy" button to deploy the contract.

 

After the contract is deployed, you'll see the deployed contract address in the "Deployed Contracts" section.

 

Developing an ERC1155 Token

 

 Step 1: Setting up the Remix IDE

 

-  Open the Remix IDE in your web browser by visiting https://remix.ethereum.org/.

-  On the Remix IDE interface, you'll find the file explorer panel on the left-hand side. Click on the "+" button to create a new file.

-  Name the file "MyToken.sol" (or any name you prefer) and click on "OK."

 

Step 2: Writing the ERC1155 Token Contract

 

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ERC1155Token {
    mapping(address => mapping(uint256 => uint256)) balances;
    mapping(uint256 => mapping(address => uint256)) approvals;
    function balanceOf(address _owner, uint256 _id) public view returns (uint256) {
        return balances[_owner][_id];
    }
    function mint(address _to, uint256 _id, uint256 _amount) public {
        balances[_to][_id] += _amount;
    }
    function transfer(address _to, uint256 _id, uint256 _amount) public {
        require(balances[msg.sender][_id] >= _amount, "Insufficient balance");
        balances[msg.sender][_id] -= _amount;
        balances[_to][_id] += _amount;
    }
    function approve(address _spender, uint256 _id, uint256 _amount) public {
        approvals[_id][_spender] = _amount;
    }
    function transferFrom(address _from, address _to, uint256 _id, uint256 _amount) public {
        require(balances[_from][_id] >= _amount, "Insufficient balance");
        require(approvals[_id][msg.sender] >= _amount, "Not approved");
        balances[_from][_id] -= _amount;
        balances[_to][_id] += _amount;
        approvals[_id][msg.sender] -= _amount;
    }
}

 

Step 3: Click on the "Deploy" button to deploy the contract.
 

After the contract is deployed, you'll see the deployed contract address in the "Deployed Contracts" section.

 

Use Cases for ERC-721 and ERC-1155 Tokens

 

ERC-721 and ERC-1155 tokens are utilized in a variety of Ethereum blockchain applications. Here are some of the most common uses for these tokens:

 

Gaming

 

ERC-721 and ERC-1155 tokens are widely used in the gaming industry. Games such as CryptoKitties, Axie Infinity, and The Sandbox use ERC-721 tokens to represent unique digital assets such as collectible characters, whereas Gods Unchained uses ERC-1155 tokens to represent fungible game elements such as weapons and armor.

 

Arts

 

ERC-721 tokens are used to represent one-of-a-kind digital art creations that can be purchased and sold on the blockchain. Platforms such as SuperRare and Nifty Gateway enable artists to create their own ERC-721 tokens and sell them to collectors.

 

You may also like to explore | NFT Royalties Explained | Empowering Artists and Investors

 

Real Estate

 

On the Ethereum blockchain, ERC-1155 tokens can be used to represent virtual real estate. Users can buy and trade virtual land using ERC-1155 tokens through projects such as Decentraland and The Sandbox.

 

Tokenized Assets:

 

Tokenized assets such as gold, real estate, and other physical assets can be represented by ERC-1155 tokens. Tokenized assets enable users to utilize cryptocurrencies to invest in real-world assets.

 

If you have a project related to NFT or token development or are seeking token development services, you may also connect with our skilled Ethereum blockchain developers
 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 21, 2024 at 12:31 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button

Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text