A Solana program demonstrating ephemeral rollups using MagicBlock's infrastructure. This counter application showcases how to delegate computation to ephemeral rollups for faster, cheaper transactions while maintaining the security of the Solana base layer.
Live Program: Explorer Link
The Realtime Counter is a simple increment counter that can operate on both Solana's base layer and ephemeral rollups. It demonstrates key concepts like:
- Delegation: Moving program state to ephemeral rollups
- Fast Execution: Performing rapid state updates on rollups
- Commitment: Syncing state back to the base layer
- Undelegation: Returning full control to the base layer
- Initialize a counter with a starting value of 0
- Increment counter (resets to 0 after reaching 1000)
- Delegate counter to ephemeral rollups for faster execution
- Commit state changes back to Solana base layer
- Atomic operations for increment + commit/undelegate
Counter
pub struct Counter {
pub count: u64,
}- Initialize - Creates a new counter PDA
- Increment - Increases counter by 1 (resets at 1000)
- Delegate - Moves counter to ephemeral rollup
- ommit - Syncs rollup state to base layer
- Undelegate - Returns counter to base layer
- IncrementAndCommit - Atomic increment + commit
- IncrementAndUndelegate - Atomic increment + undelegate
- Rust 1.70+
- Solana CLI 1.16+
- Anchor CLI 0.31+
- Node.js 16+
- Clone the repository
git clone https://github.com/priyanshpatel18/realtime-counter
cd realtime-counter- Install dependencies
yarn install- Build the program
anchor build- Deploy the program to devnet
anchor deploy --provider.cluster devnet- Run Test Suite
anchor test --skip-deploy --skip-build --skip-local-validator- Install the Local Validator
npm install -g @magicblock-labs/ephemeral-validator- Start the Local Validator
ACCOUNTS_REMOTE=https://rpc.magicblock.app/devnet ACCOUNTS_LIFECYCLE=ephemeral ephemeral-validatorACCOUNTS_REMOTE point to the reference RPC endpoint, and ACCOUNTS_LIFECYCLE should be set to ephemeral.
- Run the Tests with the Local Validator
PROVIDER_ENDPOINT=http://localhost:8899 WS_ENDPOINT=ws://localhost:8900 anchor test --skip-build --skip-deploy --skip-local-validator- Initialize Counter
await program.methods
.initialize()
.accounts({
owner: owner.publicKey,
counter: counterPda,
})
.rpc();- Delegate to Ephemeral Rollup
await program.methods
.delegate()
.accounts({
owner: owner.publicKey,
pda: counterPda
})
.rpc();- Increment on Rollup (Fast & Cheap)
await program.methods
.increment()
.accounts({
owner: owner.publicKey,
counter: counterPda,
})
.rpc();- Commit to Base Layer
await program.methods
.commit()
.accounts({
owner: owner.publicKey,
counter: counterPda,
})
.rpc();For more efficient operations, use atomic methods:
// Increment and commit in one transaction
await program.methods
.incrementAndCommit()
.accounts({
owner: owner.publicKey,
counter: counterPda
})
.rpc();
// Increment and undelegate in one transaction
await program.methods
.incrementAndUndelegate()
.accounts({
owner: owner.publicKey,
counter: counterPda
})
.rpc();- Speed: Rollup transactions are significantly faster
- Cost: Lower transaction fees on rollups
- Scalability: Handle high-frequency operations
- Security: Final state secured by Solana base layer
- anchor-lang: Solana program development framework
- ephemeral-rollups-sdk: MagicBlock SDK for rollup operations
- @coral-xyz/anchor: TypeScript client for Anchor programs
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is released under the MIT License.
For questions and support:
- Join the MagicBlock Discord
- Open an issue on GitHub
- Check the documentation links above