Loading...
This standard outlines a smart contract interface that 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.
Openzeppelin is a library for secure smart contract development. Build on a solid foundation of community-vetted code. The library provids some features like -
// contracts/GameItems.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.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() 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, "");
}
}
First the solidity version is declared and ERC1155 solidity file is imported from openzeppelin.
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
then contract GameItems is defined and extended with ERC1155 which is defined in the solidity file imported, also some variables are declared with ID.
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;
Now the constructor is defined and in ERC1155 some url is passed as argument.
constructor() ERC1155("https://game.example/api/item/{id}.json") {
This ERC1155 is the constructor defined in the imported file
// ERC1155.sol file from openzeppelin file
constructor(string memory uri_) {
_setURI(uri_);
}
this _setURI function is also defined in the same file below
// ERC1155.sol file from openzeppelin github
function _setURI(string memory newuri) internal virtual {
_uri = newuri;
}
this function sets the uri to somethis called _uri which is also declared in ERC1155.sol file
// ERC1155.sol from openzeppelin github
string private _uri;
So the complete this will be like this from constructor ERC1155(_uir) -> this goes to the ERC1155.sol constructor(_uri) -> this goes to _setURI(_uir) -> private string variable _uri.
But excatly what is this _uri ?
With the help of this uri token metadata can be obtained.
> gameItems.uri(2)
"https://game.example/api/item/{id}.json"
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 token ID 2 and uri https://game.example/api/item/{id}.json clients would replace {id} with 0000000000000000000000000000000000000000000000000000000000000002 to retrieve JSON at https://game.example/api/item/0000000000000000000000000000000000000000000000000000000000000002.json.
The JSON document for token ID 2 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
}
You can get the basic functions like transfer, batchtransfer, balanceOf, etc functions directly.
The functionalities can also be extended with openzeppelin other extensions for tokens.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------