Links

XRC1155

Introduction

The XRC1155 token standard combines the best of the previous two standards, XRC20 and XRC721, to create a fungibility agnostic and gas-efficient token contract.

Multi-Token Standard

The XRC1155 standard uses a single smart contract to represent multiple tokens. For this reason, its functionbalanceOfdiffers from that in an XRC20 contract. It also consists of an additional id argument for the identifier of the token.
XRC721's balanceOf function queries the total number of different kinds of tokens an account possesses, not the total number of individual tokens of each kind. According to the XRC1155 standard, accounts have a distinct balance for every token ID, and NFTs are implemented simply by minting a single one of them.
It leads to significant gas savings for projects that require multiple tokens. Rather than deploy a new token for every new token type, a single XRC1155 contains the entire system, reducing deployment complexity and costs.

Batch Operations

Since, in the case of XRC1155, all states are contained in a single contract, it is possible to efficiently perform operations over multiple tokens in a single transaction. The standard offers two functions, balanceOfBatch and safeBatchTransferFrom, making it possible to query multiple balances and transfer multiple tokens in a simple and less gas-intensive manner.

Construct an XRC1155 Token Contract

Say, for example, that the XRC1155 standard will be used to track multiple one-of-a-kind items, all with their unique attributes, in a hypothetical game. The items are minted and then transferred to the players, where players can keep or transfer their tokens.
The contract for tokenized items might look like this:
contract GameItems is XRC1155 {
uint256 public constant DIAMOND = 0;
uint256 public constant GOLD = 1;
uint256 public constant HAMMER = 2;
uint256 public constant SWORD = 3;
uint256 public constant SHIELD = 4;
​
constructor() public XRC1155{
_mint(msg.sender, DIAMOND, 10**18, "");
_mint(msg.sender, GOLD, 10**27, "");
_mint(msg.sender, HAMMER, 1, "");
_mint(msg.sender, SWORD, 10**9, "");
_mint(msg.sender, SHIELD, 10**9, "");
}
}
Note that Diamond is a fungible token, while Hammer is a non-fungible token, as only one of them was minted.
The XRC1155 contract has the optional extension IXRC1155MetadataURI. The URI function is used to retrieve the metadata uri.
Unlike XRC20, XRC1155 lacks the decimals field since every token is unique and cannot be partitioned.
To query the deployer's balance, run the following code:
> gameItems.balanceOf(deployerAddress,3)
1000000000
To transfer items to player accounts, run the following code:
> gameItems.safeTransferFrom(deployerAddress, playerAddress, 2, 1, "0x0")
> gameItems.balanceOf(playerAddress, 2)
1
> gameItems.balanceOf(deployerAddress, 2)
0
To transfer items in batches to player accounts and get the balance of batches, run the following code:
> gameItems.safeBatchTransferFrom(deployerAddress, playerAddress, [0,1,3,4], [50,100,1,1], "0x0")
> gameItems.balanceOfBatch([playerAddress,playerAddress,playerAddress,playerAddress,playerAddress], [0,1,2,3,4])
[50,100,1,1,1]
Last modified 1yr ago