API Reference

Address: 0x0000000000000000000000000000000000001001

Complete API documentation for the BitBadges tokenization precompile. All methods use a JSON-based interface where each method accepts a single string calldata msgJson parameter.

Interface

interface ITokenizationPrecompile {
    // Types
    struct MessageInput {
        string messageType;  // e.g., "createCollection", "transferTokens"
        string msgJson;      // JSON matching the protobuf format
    }
    
    // Transaction methods
    function transferTokens(string calldata msgJson) external returns (bool);
    function createCollection(string calldata msgJson) external returns (uint256);
    function updateCollection(string calldata msgJson) external returns (uint256);
    function deleteCollection(string calldata msgJson) external returns (bool);
    function createDynamicStore(string calldata msgJson) external returns (uint256);
    function updateDynamicStore(string calldata msgJson) external returns (bool);
    function deleteDynamicStore(string calldata msgJson) external returns (bool);
    function setDynamicStoreValue(string calldata msgJson) external returns (bool);
    function setIncomingApproval(string calldata msgJson) external returns (bool);
    function setOutgoingApproval(string calldata msgJson) external returns (bool);
    function deleteIncomingApproval(string calldata msgJson) external returns (bool);
    function deleteOutgoingApproval(string calldata msgJson) external returns (bool);
    function createAddressLists(string calldata msgJson) external returns (bool);
    function castVote(string calldata msgJson) external returns (bool);
    function executeMultiple(MessageInput[] calldata messages) external returns (bool success, bytes[] memory results);
    
    // Query methods (all return ABI-encoded structs; define matching struct types in your contract)
    function getCollection(string calldata msgJson) external view returns (TokenCollection memory);
    function getBalance(string calldata msgJson) external view returns (UserBalanceStore memory);
    function getBalanceAmount(string calldata msgJson) external view returns (uint256);
    function getTotalSupply(string calldata msgJson) external view returns (uint256);
    function getDynamicStore(string calldata msgJson) external view returns (DynamicStore memory);
    function getDynamicStoreValue(string calldata msgJson) external view returns (DynamicStoreValueResult memory);
    function getAddressList(string calldata msgJson) external view returns (AddressList memory);
    function getCollectionStats(string calldata msgJson) external view returns (CollectionStats memory);

    // Utility helper methods (pure functions)
    function convertEvmAddressToBech32(address evmAddress) external pure returns (string memory);
    function convertBech32ToEvmAddress(string calldata bech32Address) external pure returns (address);
    function rangeContains(uint256 start, uint256 end, uint256 value) external pure returns (bool);
    function rangesOverlap(uint256 start1, uint256 end1, uint256 start2, uint256 end2) external pure returns (bool);
    function searchInRanges(string calldata rangesJson, uint256 value) external pure returns (bool);
    function getBalanceForIdAndTime(string calldata balancesJson, uint256 tokenId, uint256 time) external pure returns (uint256);
    function getReservedListId(address addr) external pure returns (string memory);
}

Transaction Methods

transferTokens

Transfer tokens from the caller to one or more recipients.

Signature:

JSON Format:

Helper Function:

Example:


createCollection

Create a new token collection.

Signature:

JSON Format:

Helper Function:

Example:


createDynamicStore

Create a dynamic boolean store (e.g., KYC registry).

Signature:

JSON Format:

Helper Function:

Example:


setDynamicStoreValue

Set a value in a dynamic store for an address.

Signature:

JSON Format:

Helper Function:

Example:


executeMultiple

Execute multiple messages sequentially in a single atomic transaction. This is particularly useful for workflows like "Create Collection + Transfer Tokens".

Signature:

MessageInput Structure:

Supported Message Types: All transaction methods are supported. See README for the complete list.

Example:

Key Features:

  • Atomic Execution: All messages succeed or all fail (no partial execution)

  • Sequential Order: Messages execute in the order provided

  • Auto-Prev Support: Use collectionId: "0" in JSON to reference the collection created in a previous message

  • Result Decoding: Results are returned as bytes[] - decode based on message type:

    • Boolean results: abi.decode(results[i], (bool))

    • Uint256 results: abi.decode(results[i], (uint256))

Error Handling: If any message fails, the entire transaction is rolled back and an error is returned with context about which message failed (index and type).

Gas Considerations:

  • Base gas: 10,000

  • Per message overhead: 1,000 gas units per message

  • Total gas = base + (per message Γ— count) + sum of individual message gas costs


deleteIncomingApproval / deleteOutgoingApproval

Delete an approval by ID.

Signature:

JSON Format:

Helper Functions:


deleteCollection

Delete a collection (only creator can delete).

Signature:

JSON Format:

Helper Function:


Query Methods

getCollection

Get collection details by ID.

Signature:

JSON Format:

Helper Function:

Example:


getBalanceAmount

Get the exact balance amount for a specific (tokenId, ownershipTime) combination. Returns uint256 directly.

This function queries a single token ID and single ownership time, returning the exact balance amount the user owns. For querying multiple token IDs or time ranges, use getBalance instead and process the full balance store off-chain.

Signature:

JSON Format:

Field
Type
Description

collectionId

string

Collection ID (uint as string)

address

string

User address (bech32 or 0x hex)

tokenId

string

Single token ID to query (uint as string)

ownershipTime

string

Single ownership time to query (uint as string, typically ms timestamp)

Example:

Note: For complex balance queries involving ranges, use getBalance to retrieve the full UserBalanceStore and calculate amounts off-chain.


getTotalSupply

Get the total supply for a specific (tokenId, ownershipTime) combination. Returns uint256 directly.

This function queries the total minted supply for a single token ID and ownership time.

Signature:

JSON Format:

Field
Type
Description

collectionId

string

Collection ID (uint as string)

tokenId

string

Single token ID to query (uint as string)

ownershipTime

string

Single ownership time to query (uint as string, typically ms timestamp)

Example:


getDynamicStoreValue

Get a value from a dynamic store. Returns a struct matching the store’s value type.

Signature:

JSON Format:

Helper Function:

Example:


getAddressList

Get an address list by ID.

Signature:

JSON Format:

Helper Function:


getCollectionStats

Get collection statistics including holder count and circulating supply.

Signature:

JSON Format:

Field
Type
Description

collectionId

string

Collection ID (uint as string)

Response structure: The returned CollectionStats struct includes holderCount (uint256) and balances (array of Balance structs with amount, tokenIds, ownershipTimes).

Example:


Helper Library Reference

Range Helpers

uintRangeToJson(uint256 start, uint256 end) β†’ string

Convert a single range to JSON array format.

uintRangeArrayToJson(uint256[] memory starts, uint256[] memory ends) β†’ string

Convert multiple ranges to JSON array format.

Collection Helpers

collectionMetadataToJson(string memory uri, string memory customData) β†’ string

Build collection metadata JSON.

simpleUserBalanceStoreToJson(bool outgoing, bool incoming, bool allIncoming) β†’ string

Build simple default balances JSON with auto-approve flags.

stringArrayToJson(string[] memory strings) β†’ string

Convert string array to JSON array.

Utility Helpers

uintToString(uint256 value) β†’ string

Convert uint256 to string.

Struct Return Types

All query methods (except getBalanceAmount and getTotalSupply, which return uint256) return ABI-encoded structs. Define struct types in your contract that match the precompile’s response layout and use an interface that declares those return types.

Example:

How it works:

  • Data is packed as ABI-encoded structs matching Solidity’s decoding expectations

  • All numeric types are converted to uint256 for Solidity compatibility

  • Nested structures (e.g. Balance[] within CollectionStats) are supported

Supported struct return types:

  • TokenCollection – full collection data

  • UserBalanceStore – user balance and approval data

  • AddressList – address list data

  • CollectionStats – holder count and circulating supply

  • DynamicStore – dynamic store configuration

  • DynamicStoreValueResult – dynamic store value

  • Other query response types as defined by the precompile


JSON Format Reference

All JSON must match the protobuf JSON format:

  • Numbers: Always strings (e.g., "123" not 123)

  • Booleans: true or false (not strings)

  • Arrays: Standard JSON array format

  • Objects: Standard JSON object format

  • Addresses: Hex format with 0x prefix (e.g., "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb")

Error Handling

Invalid JSON or missing required fields will cause the transaction to revert. See Error Handling for complete error codes and handling strategies.

Security Notes

  1. The creator field is automatically set from msg.sender and cannot be specified

  2. All addresses are validated before processing

  3. JSON is validated against protobuf schema

  4. Invalid operations revert with clear error messages

Utility Helper Methods

The precompile includes utility helper methods for common operations. These are pure functions that don't require state access.

Address Conversion

convertEvmAddressToBech32(address evmAddress) β†’ string

Convert an EVM address (0x...) to a BitBadges bech32 address (bb1...).

convertBech32ToEvmAddress(string bech32Address) β†’ address

Convert a bech32 address (bb1...) to an EVM address (0x...).

Range Utilities

These methods help work with UintRange structures used for token IDs and ownership times.

rangeContains(uint256 start, uint256 end, uint256 value) β†’ bool

Check if a value is within a range (inclusive).

rangesOverlap(uint256 start1, uint256 end1, uint256 start2, uint256 end2) β†’ bool

Check if two ranges overlap.

searchInRanges(string rangesJson, uint256 value) β†’ bool

Search if a value exists in any range within a JSON-encoded array.

Balance Utilities

getBalanceForIdAndTime(string balancesJson, uint256 tokenId, uint256 time) β†’ uint256

Get the balance amount for a specific token ID and time from a JSON-encoded balances array. This is useful for processing balance data returned from getBalance queries.

List ID Utilities

getReservedListId(address addr) β†’ string

Get the reserved list ID for a specific address. Reserved list IDs are automatically created for each address and are the bech32 representation of the address.

Note: To check if a list ID represents "All" addresses, simply compare the string: keccak256(bytes(listId)) == keccak256(bytes("All")).


See Also

Last updated