BearNetworkChain
WebsiteBrnkScanBrnkTestScanFaucetSwapPrivacy
  • 💹BearNetworkChain - BRNKC
  • 🗂️Introduction
    • Why Choose BearNetworkChain
    • Features of BearNetworkChain
    • Ecosystem of BearNetworkChain
    • BearNetworkChain BRNKC
    • BearNetworkChain NFT
    • BRNKC Contracts (BSC)
  • ▶️Getting Started
    • Creating a Wallet
    • Changing the routing on the chain
    • Adding token types to your wallet
    • Connecting Wallet
    • Sending a transaction
  • 🖥️DEV Teaching
    • Web3
  • 🛠️developers
    • Bear Network Chain SDK
    • TestNet
    • JSON RPC
    • Token address
  • ERC Standard
    • ERC20
      • Creating ERC20 Supply
    • ERC721
  • 📋about us
    • LOGO
    • Roadmap
    • WhitePaper
    • Our Business Partner
    • Privacy Policy
Powered by GitBook
On this page
  • Constructing an ERC721 Token Contract
  • Preset ERC721 contract
  1. ERC Standard

ERC721

PreviousCreating ERC20 SupplyNextLOGO

Last updated 1 year ago

We’ve discussed how you can make a fungible token using , but what if not all tokens are alike? This comes up in situations like real estate, voting rights, or collectibles, where some items are valued more than others, due to their usefulness, rarity, etc. ERC721 is a standard for representing ownership of , that is, where each token is unique.

ERC721 is a more complex standard than ERC20, with multiple optional extensions, and is split across a number of contracts. The OpenZeppelin Contracts provide flexibility regarding how these are combined, along with custom useful extensions. Check out the to learn more about these.

Constructing an ERC721 Token Contract

We’ll use ERC721 to track items in our game, which will each have their own unique attributes. Whenever one is to be awarded to a player, it will be minted and sent to them. Players are free to keep their token or trade it with other people as they see fit, as they would any other asset on the blockchain! Please note any account can call awardItem to mint items. To restrict what accounts can mint items we can add .

Here’s what a contract for tokenized items might look like:

// contracts/GameItem.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract GameItem is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("GameItem", "ITM") {}

    function awardItem(address player, string memory tokenURI)
        public
        returns (uint256)
    {
        uint256 newItemId = _tokenIds.current();
        _mint(player, newItemId);
        _setTokenURI(newItemId, tokenURI);

        _tokenIds.increment();
        return newItemId;
    }
}

Also note that, unlike ERC20, ERC721 lacks a decimals field, since each token is distinct and cannot be partitioned.

New items can be created:

> gameItem.awardItem(playerAddress, "https://game.example/item-id-8u5h2m.json")
Transaction successful. Transaction hash: 0x...
Events emitted:
 - Transfer(0x0000000000000000000000000000000000000000, playerAddress, 7)

And the owner and metadata of each item queried:

> gameItem.ownerOf(7)
playerAddress
> gameItem.tokenURI(7)
"https://game.example/item-id-8u5h2m.json"

This tokenURI should resolve to a JSON document that might look something like:

{
    "name": "Thor's hammer",
    "description": "Mjölnir, the legendary hammer of the Norse god of thunder.",
    "image": "https://game.example/item-id-8u5h2m.png",
    "strength": 20
}

Preset ERC721 contract

This contract is ready to deploy without having to write any Solidity code. It can be used as-is for quick prototyping and testing but is also suitable for production environments.

The contract is an implementation of ERC721 that includes the metadata standard extensions () as well as a mechanism for per-token metadata. That’s where the method comes from: we use it to store an item’s metadata.

For more information about the tokenURI metadata JSON Schema, check out the .

A preset ERC721 is available, . It is preconfigured with token minting (creation) with token ID and URI auto generation, the ability to stop all token transfers (pause), and it allows holders to burn (destroy) their tokens. The contract uses to control access to the minting and pausing functionality. The account that deploys the contract will be granted the minter and pauser roles, as well as the default admin role.

ERC20
non-fungible tokens
API Reference
Access Control
ERC721URIStorage
IERC721Metadata
_setTokenURI
ERC721 specification
ERC721PresetMinterPauserAutoId
Access Control