Documentation Index
Fetch the complete documentation index at: https://compact-by-example.org/llms.txt
Use this file to discover all available pages before exploring further.
The Contract
View Contract Code
View Contract Code
How It Works
This ERC1155 implementation enables multiple token types in a single contract. For detailed explanations of core concepts, see the linked basics tutorials.Initialization Protection
Uses the same initialization guard pattern as ERC20/ERC721 to ensureinitialize() is called once to set the base URI.
Nested Map Balance Structure
Key Data Structure:- Outer Map: Token ID → Inner Map
- Inner Map: Account → Balance
Mixed Token Types
Fungible:_mint(alice, 1, 1000) - 1000 copies of token ID 1 (Gold Coins)Non-Fungible:
_mint(bob, 100, 1) - 1 unique token ID 100 (Legendary Sword)Semi-Fungible:
_mint(carol, 50, 10) - 10 limited copies of token ID 50 (Shield)
All coexist in the same contract. Organization tip: Use ID ranges (1-999: currency, 1000-9999: items, 10000+: legendaries).
ERC1155-Specific Features
Operator-Only Approval:- Only
setApprovalForAll()(no single-token approval like ERC721) - Operator can transfer ANY token type you own (all-or-nothing)
- Simpler but less granular than ERC721
- Transfer partial amounts:
safeTransferFrom(alice, bob, 1, 500)- Transfer 500 of token ID 1 - Transfer singles:
safeTransferFrom(alice, bob, 100, 1)- Transfer unique NFT - Authorization: owner or approved operator only
- Each token ID has one URI describing that token type (not instance)
- Example: All “Gold Coin” tokens (ID 1) share the same metadata
- Unlike ERC721 where each token is unique with its own metadata
For minting/burning patterns, see Minting tutorial and Burning tutorial
Helper Functions
The contract uses utility functions for address validation and operations:- isKeyOrAddressZero: Validates addresses by checking against default values. Used to prevent minting/transferring to invalid addresses.
- isContractAddress: Determines if an address is a ContractAddress type. Used to prevent unsafe transfers to contracts until contract-to-contract calls are supported.
- burnAddress: Returns the burn address for minting (from burn) and burning (to burn) operations.
In production OpenZeppelin contracts, these utilities are imported from the
Utils module. The implementations shown here match the OpenZeppelin Compact
Contracts v0.0.1-alpha.0 specification.Try It Yourself
Build and Deploy
Build and Deploy
1. Create project structure:2. Save the contract:Create 4. Mint different token types:5. Compile:
contracts/game-items.compact with the code above.3. Define your token types:What’s Next
ERC20 Token
Create fungible tokens
ERC721 NFT
Build non-fungible tokens