The ERC-1155 standard lets you create and manage many types of tokens in one smart contract. This saves time and money compared to making separate contracts for each token.
0
0
ERC-1155 multi-token standard in Blockchain / Solidity
Introduction
You want to create a game with many items like swords, shields, and potions all in one contract.
You need to manage both collectible tokens and currency tokens together.
You want to save blockchain fees by batching multiple token transfers in one transaction.
You want to track ownership of different digital assets efficiently.
You want to build a marketplace that handles many token types easily.
Syntax
Blockchain / Solidity
contract MyToken is ERC1155 {
constructor(string memory uri) ERC1155(uri) {
// Initialize tokens here
}
function mint(address to, uint256 id, uint256 amount, bytes memory data) public {
_mint(to, id, amount, data);
}
function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public {
_mintBatch(to, ids, amounts, data);
}
}The id represents the token type (like sword or shield).
The amount is how many tokens of that type you want to create or transfer.
Examples
This example creates a sword token with ID 1 and mints one to a player.
Blockchain / Solidity
contract GameItems is ERC1155 { constructor() ERC1155("https://game.example/api/item/{id}.json") {} function mintSword(address player) public { _mint(player, 1, 1, ""); // Mint 1 sword (id=1) } }
This mints 10 swords and 5 shields to the player in one transaction.
Blockchain / Solidity
function mintBatchItems(address player) public {
uint256[] memory ids = new uint256[](2);
ids[0] = 1; // sword
ids[1] = 2; // shield
uint256[] memory amounts = new uint256[](2);
amounts[0] = 10;
amounts[1] = 5;
_mintBatch(player, ids, amounts, "");
}Sample Program
This contract creates two token types: GOLD and SILVER. It mints 1000 GOLD and 5000 SILVER tokens to the contract creator.
Blockchain / Solidity
pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; contract SimpleMultiToken is ERC1155 { uint256 public constant GOLD = 0; uint256 public constant SILVER = 1; constructor() ERC1155("https://token-cdn-domain/{id}.json") { _mint(msg.sender, GOLD, 1000, ""); _mint(msg.sender, SILVER, 5000, ""); } }
OutputSuccess
Important Notes
ERC-1155 supports batch transfers which reduce transaction costs.
Each token type has its own ID, so you can track many assets in one contract.
Use OpenZeppelin's ERC1155 implementation to avoid security issues.
Summary
ERC-1155 lets you manage many token types in one smart contract.
It saves gas by batching token transfers and minting.
Useful for games, collectibles, and multi-asset platforms.