Overview
The Token Plugin provides comprehensive token management for ICP applications, supporting both ICP ledger and ICRC-1/ICRC-2 standard tokens. It handles transfers, balance queries, metadata retrieval, and account management with full type safety and error handling.Core Features
ICP Transfers
Send ICP tokens between accounts with automatic fee calculation and validation
ICRC-1 & ICRC-2 Support
Full support for ICRC standard tokens including approvals and allowances
Balance Queries
Check token balances for any account, principal, or canister
Account Management
Create and manage multiple accounts with subaccount support
Transaction Utilities
Format amounts, parse transactions, and handle memos with ease
Metadata Retrieval
Get token metadata including symbol, decimals, and total supply
Quick Start
LangChain Integration
The Token Plugin is integrated with LangChain tools for natural language operations:Available Tools
transfer_icp
Transfer ICP tokens with natural language
get_balance
Check ICP balance for current identity
icrc1_transfer
Transfer ICRC-1 standard tokens
Natural Language Examples
Using with DeFi Agent
Core Methods
Transfer Operations
transfer(to, amount, options?)
Transfer tokens to another account with comprehensive validation and error handling.
to
: Recipient account (Principal, account ID string, or IAccount object)amount
: Amount to transfer (bigint in smallest token units)options
: Transfer options including memo, fee, subaccount, and timestamp
Balance Operations
getBalance(account?)
Get token balance for the current identity or specified account.
bigint
- Balance in smallest token units
Account Management
getAccountId(principal?, subaccount?)
Generate account identifier from principal and optional subaccount.
createAccount(principal, subaccount?)
Create an IAccount object for ICRC operations.
generateSubaccount()
Generate a random 32-byte subaccount.
Utility Functions
formatAmount(amount, decimals?)
Format raw token amounts for display.
parseAmount(amountStr, decimals?)
Parse formatted amounts to raw bigint values.
createMemo(input)
Create transaction memos from various inputs.
Validation
validateAccount(account)
Validate account identifiers and principals.
ICRC Token Operations
ICRC-1 Support
ICRC-2 Support (Approvals)
Error Handling
The Token Plugin provides comprehensive error handling with descriptive messages:Common Error Types
Error Code | Description | Solution |
---|---|---|
INSUFFICIENT_BALANCE | Not enough tokens for transfer + fee | Check balance before transfer |
INVALID_ACCOUNT_ID | Invalid recipient address format | Validate address format |
INVALID_PRINCIPAL | Invalid principal format | Use Principal.fromText() |
TRANSFER_FAILED | Ledger rejected the transfer | Check amount, fee, and account validity |
LEDGER_UNAVAILABLE | Canister temporarily unavailable | Retry after some time |
BAD_FEE | Incorrect fee amount | Use the expected fee from metadata |
Integration with Identity Plugin
The Token Plugin automatically uses the active identity from the Identity Plugin:Advanced Usage
Multi-Token Portfolio Management
Batch Operations
Custom Fee Estimation
Type Definitions
Core Types
ICRC Types
Best Practices
1. Always Validate Inputs
2. Handle Network Conditions
3. Use Appropriate Precision
4. Secure Memo Handling
Examples
Simple Payment System
Multi-Token Wallet
Performance Tips
- Batch Balance Queries: Use
Promise.all()
for multiple balance checks - Cache Metadata: Token metadata rarely changes, consider caching
- Optimize Fee Queries: Get fee once and reuse for multiple transactions
- Use Subaccounts: Create subaccounts for different purposes within the same principal