Tokenization Precompile
Quick Start
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./interfaces/ITokenizationPrecompile.sol";
import "./libraries/TokenizationJSONHelpers.sol";
contract MyTokenContract {
ITokenizationPrecompile constant TOKENIZATION =
ITokenizationPrecompile(0x0000000000000000000000000000000000001001);
// Transfer tokens using JSON helper
function transfer(
uint256 collectionId,
address to,
uint256 amount,
uint256 tokenId
) external returns (bool) {
address[] memory recipients = new address[](1);
recipients[0] = to;
// Build JSON using helpers
string memory tokenIdsJson = TokenizationJSONHelpers.uintRangeToJson(tokenId, tokenId);
string memory ownershipTimesJson = TokenizationJSONHelpers.uintRangeToJson(1, TokenizationJSONHelpers.FOREVER);
string memory transferJson = TokenizationJSONHelpers.transferTokensJSON(
collectionId,
recipients,
amount,
tokenIdsJson,
ownershipTimesJson
);
return TOKENIZATION.transferTokens(transferJson);
}
// Query balance using JSON helper
function balanceOf(
uint256 collectionId,
address user
) external view returns (uint256) {
string memory tokenIdsJson = TokenizationJSONHelpers.uintRangeToJson(1, TokenizationJSONHelpers.FOREVER);
string memory ownershipTimesJson = TokenizationJSONHelpers.uintRangeToJson(1, TokenizationJSONHelpers.FOREVER);
string memory balanceJson = TokenizationJSONHelpers.getBalanceAmountJSON(
collectionId,
user,
tokenIdsJson,
ownershipTimesJson
);
return TOKENIZATION.getBalanceAmount(balanceJson);
}
}Why JSON?
Core Concepts
1. All Methods Accept JSON Strings
2. Use Helper Libraries
3. UintRange Arrays
Common Patterns
Pattern 1: Simple Token Transfer
Pattern 2: Time-Bound Transfer
Pattern 3: Create Dynamic Store (KYC Registry)
Pattern 4: Create Collection
Pattern 5: Multi-Message Execution (Create Collection + Transfer Tokens)
Available Methods
Transaction Methods
Multi-Message Execution
Query Methods
Struct Return Types
Helper Library Functions
Transfer Helpers
Collection Helpers
Dynamic Store Helpers
Query Helpers
Utility Helpers
Precompile Utility Methods
Multi-Message Helpers
Return Value Handling
Direct Returns (uint256)
Struct Returns (Query Methods)
Security Considerations
Best Practices
1. Always Use Helper Functions
2. Cache Complex JSON
3. Validate Inputs Before Building JSON
4. Handle Errors Gracefully
5. Multi-Message Execution Best Practices
Examples
Next Steps
Resources
Last updated