MultiTokenId
MultiTokenId is a branded bigint type representing ERC-1155 multi-token identifiers that can represent both fungible and non-fungible tokens.
Overview
ERC-1155 is a multi-token standard that supports multiple token types in a single contract. Each token type has a unique ID, and can be either fungible (like ERC-20) or non-fungible (like ERC-721).Key Features
- Type-safe: Branded bigint prevents mixing with raw numbers
- Dual nature: Supports both fungible and non-fungible tokens
- Range validation: 0 to 2^256-1
- Fungibility detection: Utilities to check token type
- ERC-1155 interface selectors: Built-in function selectors
Installation
Basic Usage
API Reference
Constructor
from(value: bigint | number | string): MultiTokenId
Create MultiTokenId from bigint, number, or hex/decimal string.
InvalidMultiTokenIdErrorif value is negative, exceeds max, or invalid
Conversions
toNumber(tokenId: MultiTokenId): number
Convert to number (unsafe for large values).
RangeErrorif value exceedsNumber.MAX_SAFE_INTEGER
toBigInt(tokenId: MultiTokenId): bigint
Convert to bigint.
toHex(tokenId: MultiTokenId): string
Convert to hex string with 0x prefix.
Fungibility Checks
isValidFungible(tokenId: MultiTokenId): boolean
Check if token ID is valid for fungible tokens (> 0 and < 2^128).
isValidNonFungible(tokenId: MultiTokenId): boolean
Check if token ID is valid for non-fungible tokens (>= 2^128).
Comparison
equals(a: MultiTokenId, b: MultiTokenId): boolean
Check if two token IDs are equal.
compare(a: MultiTokenId, b: MultiTokenId): number
Compare two token IDs. Returns -1 if a < b, 0 if equal, 1 if a > b.
Constants
constants.MAX
Maximum MultiTokenId value (2^256 - 1).
constants.MIN
Minimum MultiTokenId value (0).
constants.FUNGIBLE_THRESHOLD
Threshold for fungible vs non-fungible tokens (2^128).
- Token IDs < 2^128: Fungible tokens (multiple owners can have amounts)
- Token IDs >= 2^128: Non-fungible tokens (single owner, amount = 1)
ERC-1155 Interface
ERC1155_SELECTORS
ERC-1155 function selectors for ABI encoding.
ERC-1155 Standard
ERC-1155 defines a multi-token standard. Key functions:balanceOf(address, uint256): Returns balance of token typebalanceOfBatch(address[], uint256[]): Returns balances for multiple accounts/tokenssafeTransferFrom(address, address, uint256, uint256, bytes): Transfers tokenssafeBatchTransferFrom(address, address, uint256[], uint256[], bytes): Batch transfersetApprovalForAll(address, bool): Approves operator for all tokensisApprovedForAll(address, address): Checks if operator is approveduri(uint256): Returns metadata URI for token type
Token ID Patterns
Fungible Tokens
Fungible token IDs typically start from 1 and increment:Non-Fungible Tokens
Non-fungible token IDs use the upper 128 bits:Hybrid Collections
Mix both types in one contract:Examples
Query Balance
Batch Query
Transfer Tokens
Check Token Type
List All Tokens
Error Handling
Type Safety
MultiTokenId is a branded type that prevents accidental mixing with raw bigints:Use Cases
Gaming
Collectibles
See Also
- TokenBalance - ERC-20 token balances
- TokenId - ERC-721 NFT token IDs
- Uint256 - Generic unsigned 256-bit integers
- Address - Ethereum addresses

