Loading...
One contract holding many token types — fungible (like currency) and non-fungible (like swords). Used for in-game assets.
ERC-20 is one token type per contract. ERC-721 is one NFT collection per contract. If your game has 500 item types (swords, potions, shields, currencies), deploying 500 contracts is absurd.
ERC-1155 solves this: one contract, many token IDs, each with a fungible balance.
solidity// Token ID 1 = gold coins (fungible, millions in circulation) // Token ID 2 = legendary sword (NFT, supply = 1) // Token ID 3 = health potion (fungible, stackable)
solidityimport "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract GameItems is ERC1155, Ownable { uint256 public constant GOLD = 0; uint256 public constant SWORD = 1; uint256 public constant SHIELD = 2; constructor() ERC1155("https://game.example/{id}.json") Ownable(msg.sender) { _mint(msg.sender, GOLD, 10**24, ""); _mint(msg.sender, SWORD, 100, ""); _mint(msg.sender, SHIELD, 50, ""); } }
ERC-1155 supports moving multiple token types in one tx via safeBatchTransferFrom. Gas-efficient for games where a trade involves multiple items.
ERC1155("https://.../{id}.json") — the {id} placeholder is a 64-char hex-padded token ID. Example: https://game.example/0000...0001.json for token ID 1.
Write CardDeck using OZ's ERC1155. Define three constants: SPADES=0, HEARTS=1, CLUBS=2. In the constructor, mint 52 of each to the deployer.