Magic Actions
Magic Actions enable you to execute automatic on-chain handlers when committing accounts from Ephemeral Rollups back to the base layer. This powerful pattern allows you to perform base chain operations (like updating global state) automatically whenever ER state is committed.What Are Magic Actions?
Magic Actions are instruction handlers that execute automatically on the base chain when you commit delegated accounts from an Ephemeral Rollup. They enable:- Updating global leaderboards when player scores are committed
- Triggering reward distributions based on ER activity
- Synchronizing cross-program state
- Executing base-layer-only operations (like SPL token transfers) on commit
Magic Actions execute on the base layer (Solana mainnet/devnet) using funds from an escrow account, allowing automated base chain interactions without user signatures.
How It Works
Required Imports
Account Context Annotations
Managing Escrow Accounts
- Create & Fund
- Close Escrow
What Makes This Advanced?
Magic Actions represent an advanced pattern because they:- Bridge ER and Base Layer: Automatically execute base chain logic when ER state is committed
- Gasless Automation: Use escrow accounts to pay for base layer fees without user interaction
- Cross-Program Coordination: Update global state (like leaderboards) based on individual player actions in ERs
- Composability: Chain multiple actions together in a single commit operation
Use Cases
- Global Leaderboards: Update rankings when player scores are committed from game ERs
- Tournament Systems: Trigger prize distributions when tournament state is finalized
- Cross-Chain State: Synchronize ER state with base layer contracts
- Token Operations: Execute SPL token transfers on the base layer when ER conditions are met
Example: Counter with Leaderboard
programs/magic-actions/src/lib.rs
Next Steps
- Learn about Cranks for scheduled automated execution
- Explore Session Keys for gasless user transactions
- Check the full source code