ERC-1155 · Final · ERC · 2018-06-17
ERC-1155: Multi Token Standard
One contract that can hold any number of fungible and non-fungible token types, with batch transfers and mandatory receiver hooks, popular for games and collectibles.
What problem does it solve?
ERC-20 needs one contract per fungible token, and ERC-721 needs one contract per NFT collection. A game with thousands of item types (gold, potions, unique swords) would have to deploy and approve thousands of contracts, and moving several items at once meant several transactions.
ERC-1155 puts many token types in a single contract. Each id is its own token type with its own supply and metadata: an id with a supply of 1 behaves like an NFT, one with a supply of a million behaves like a fungible token. Multiple ids can be moved in one batch transfer.
How it works
Balances are tracked per (owner, id) pair. The core interface (ERC-165 id 0xd9b67a26):
| Function | Purpose |
|---|---|
balanceOf(owner, id) |
Balance of one token type |
balanceOfBatch(owners[], ids[]) |
Many balances in one call |
safeTransferFrom(from, to, id, value, data) |
Move one token type |
safeBatchTransferFrom(from, to, ids[], values[], data) |
Move several types at once |
setApprovalForAll(operator, approved) |
Let an operator manage all your tokens |
isApprovedForAll(owner, operator) |
Check operator status |
Key rules:
- Only safe transfers. If
tois a contract, the token contract must callonERC1155Received(oronERC1155BatchReceived) on it, and the receiver must return the magic value0xf23a6e61(or0xbc197c81) or the transfer reverts. This prevents tokens getting stuck in contracts that cannot handle them. - Events are the source of truth. Every mint, burn and transfer must emit
TransferSingleorTransferBatch(mint usesfrom = 0x0, burn usesto = 0x0), so indexers can rebuild all balances from logs alone. - Metadata by URI. The optional
uri(id)function returns a URI; if it contains{id}, clients replace it with the id as 64 lowercase hex characters, no0x. One string can then serve every token. - Approval is all-or-nothing per operator. There is no per-id
approve.
Sample code
A minimal multi-token using OpenZeppelin, plus a contract that accepts ERC-1155 deposits:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
contract GameItems is ERC1155 {
uint256 public constant GOLD = 0;
uint256 public constant SWORD = 1;
constructor() ERC1155("https://example.com/items/{id}.json") {
_mint(msg.sender, GOLD, 1_000_000, "");
_mint(msg.sender, SWORD, 1, ""); // supply of 1: behaves like an NFT
}
}
contract Vault {
function onERC1155Received(address, address, uint256, uint256, bytes calldata)
external pure returns (bytes4)
{
return this.onERC1155Received.selector; // 0xf23a6e61
}
function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata)
external pure returns (bytes4)
{
return this.onERC1155BatchReceived.selector; // 0xbc197c81
}
}
Reading several balances at once (ethers v6):
import { Contract, JsonRpcProvider } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const items = new Contract(itemsAddress, [
"function balanceOfBatch(address[] owners, uint256[] ids) view returns (uint256[])",
], provider);
const [gold, sword] = await items.balanceOfBatch([player, player], [0n, 1n]);
Things to know
- A real receiver should also implement ERC-165
supportsInterfacefor0x4e2312e0(the receiver interface); OpenZeppelin'sERC1155Holderdoes this for you. - The receiver hook is an external call made during the transfer, so it is a reentrancy point. Follow checks-effects-interactions in code that mints or transfers.
- The standard drops
nameandsymbol; names live in the metadata JSON. - There is no on-chain enumeration of ids or holders. Build that off-chain from events.
- For finer-grained approvals see ERC-1761 (Stagnant). ERC-6909 is a newer, slimmer multi-token interface without mandatory callbacks.