Skip to main content

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.
Throws:
  • InvalidMultiTokenIdError if value is negative, exceeds max, or invalid

Conversions

toNumber(tokenId: MultiTokenId): number

Convert to number (unsafe for large values).
Throws:
  • RangeError if value exceeds Number.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).
By convention:
  • 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 type
  • balanceOfBatch(address[], uint256[]): Returns balances for multiple accounts/tokens
  • safeTransferFrom(address, address, uint256, uint256, bytes): Transfers tokens
  • safeBatchTransferFrom(address, address, uint256[], uint256[], bytes): Batch transfer
  • setApprovalForAll(address, bool): Approves operator for all tokens
  • isApprovedForAll(address, address): Checks if operator is approved
  • uri(uint256): Returns metadata URI for token type
Specification: EIP-1155

Token ID Patterns

Fungible Tokens

Fungible token IDs typically start from 1 and increment:
Each address can own any amount of these tokens.

Non-Fungible Tokens

Non-fungible token IDs use the upper 128 bits:
Each NFT typically has amount = 1 and a single owner.

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