Skip to main content

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

Set commit_frequency_ms based on your use case:
  • Gaming: 10-30 seconds for responsive state updates
  • Trading: 5-10 seconds for more frequent commits
  • High-value operations: Lower values for more frequent base layer syncs

Complete Flow Example

SPL Token Integration

To use this pattern with real SPL tokens:

What Makes This Advanced?

This example demonstrates:
  1. Custom Delegation Parameters: Fine-grained control over commit frequency and validator selection
  2. Multi-Account Operations: Transferring between multiple delegated accounts
  3. Init-If-Needed Pattern: Automatically creating receiver accounts during transfers
  4. Error Handling: Proper balance validation with custom errors
  5. PDA Management: Using PDAs for user-specific balance accounts

Use Cases

  • 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

Important:
  • Always validate account ownership before transfers
  • Check sufficient balance before debiting
  • Use proper PDA derivation for user accounts
  • Set appropriate commit frequencies for your use case
  • Monitor delegated account states

Adapting for Real Tokens

The key differences when using SPL tokens:
  1. Account Type: TokenAccount instead of custom Balance
  2. Authority: Token account authority, not PDA seeds
  3. Transfer Logic: Use SPL token CPI instead of direct balance updates
  4. Mint Validation: Ensure all accounts use the same mint
Example SPL transfer in ER:
This transfer executes in the ER with the same ultra-low latency as the balance example!

Next Steps