Token Transfer
The Token Transfer example demonstrates how to work with custom token accounts in Ephemeral Rollups. While this example uses a simple balance account for clarity, the same patterns apply to SPL tokens, allowing you to build high-frequency token operations with ultra-low latency.Overview
This example shows:- Creating and delegating balance accounts
- Transferring tokens between accounts in ERs
- Configurable delegation parameters
- Committing and undelegating accounts
While this example uses a simple
Balance account, the same delegation patterns work with SPL Token Accounts for real token transfers.How It Works
Program Annotations
The program uses the#[ephemeral] macro to enable ER support:
programs/dummy-transfer/src/lib.rs
Delegation Configuration
Complete Flow Example
SPL Token Integration
To use this pattern with real SPL tokens:What Makes This Advanced?
This example demonstrates:- Custom Delegation Parameters: Fine-grained control over commit frequency and validator selection
- Multi-Account Operations: Transferring between multiple delegated accounts
- Init-If-Needed Pattern: Automatically creating receiver accounts during transfers
- Error Handling: Proper balance validation with custom errors
- PDA Management: Using PDAs for user-specific balance accounts
Use Cases
- Gaming
- DeFi
- In-game currency transfers
- Rapid item trading between players
- Reward distributions
- Marketplace transactions
Performance Benefits
Compared to base layer token transfers:- Latency: ~400ms → ~10ms (40x faster)
- Cost: ~0.000005 SOL → negligible in ER
- Throughput: Thousands of transfers per second
- Batching: Execute 100s of transfers before committing
Testing Locally
Testing on Devnet
To run tests on devnet:Make sure you have devnet SOL in your wallet before running devnet tests.
Security Considerations
Adapting for Real Tokens
The key differences when using SPL tokens:- Account Type:
TokenAccountinstead of customBalance - Authority: Token account authority, not PDA seeds
- Transfer Logic: Use SPL token CPI instead of direct balance updates
- Mint Validation: Ensure all accounts use the same mint
Next Steps
- Explore Session Keys for gasless token transfers
- Learn about Magic Actions to trigger base layer logic on commit
- Check out SPL Token documentation for working with real tokens
- Review the full source code