Skip to content

Solana counter demo using MagicBlock ephemeral rollups - 8x faster transactions with base layer security. Built with Anchor & TypeScript.

License

Notifications You must be signed in to change notification settings

priyanshpatel18/realtime-counter

Repository files navigation

Realtime Counter

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

Overview

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

Features

  • 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

Program Structure

States

Counter

pub struct Counter {
    pub count: u64,
}

Instructions

  1. Initialize - Creates a new counter PDA
  2. Increment - Increases counter by 1 (resets at 1000)
  3. Delegate - Moves counter to ephemeral rollup
  4. ommit - Syncs rollup state to base layer
  5. Undelegate - Returns counter to base layer
  6. IncrementAndCommit - Atomic increment + commit
  7. IncrementAndUndelegate - Atomic increment + undelegate

Getting Started

Prerequisites

  1. Rust 1.70+
  2. Solana CLI 1.16+
  3. Anchor CLI 0.31+
  4. Node.js 16+

Installation

  1. Clone the repository
git clone https://github.com/priyanshpatel18/realtime-counter
cd realtime-counter
  1. Install dependencies
yarn install
  1. Build the program
anchor build
  1. Deploy the program to devnet
anchor deploy --provider.cluster devnet
  1. Run Test Suite
anchor test --skip-deploy --skip-build --skip-local-validator

Running Tests on Devnet with a Local Ephemeral Rollup

  1. Install the Local Validator
npm install -g @magicblock-labs/ephemeral-validator
  1. Start the Local Validator
ACCOUNTS_REMOTE=https://rpc.magicblock.app/devnet ACCOUNTS_LIFECYCLE=ephemeral ephemeral-validator

ACCOUNTS_REMOTE point to the reference RPC endpoint, and ACCOUNTS_LIFECYCLE should be set to ephemeral.

  1. 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

Usage Examples

Basic Workflow

  1. Initialize Counter
await program.methods
  .initialize()
  .accounts({
    owner: owner.publicKey,
    counter: counterPda,
  })
  .rpc();
  1. Delegate to Ephemeral Rollup
await program.methods
  .delegate()
  .accounts({
    owner: owner.publicKey,
    pda: counterPda
  })
  .rpc();
  1. Increment on Rollup (Fast & Cheap)
await program.methods
  .increment()
  .accounts({
    owner: owner.publicKey,
    counter: counterPda,
  })
  .rpc();
  1. Commit to Base Layer
await program.methods
  .commit()
  .accounts({
    owner: owner.publicKey,
    counter: counterPda,
  })
  .rpc();

Atomic Operations

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();

Benefits

  • 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

Key Dependencies

  • anchor-lang: Solana program development framework
  • ephemeral-rollups-sdk: MagicBlock SDK for rollup operations
  • @coral-xyz/anchor: TypeScript client for Anchor programs

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Submit a pull request

License

This project is released under the MIT License.

Resources

Support

For questions and support:

About

Solana counter demo using MagicBlock ephemeral rollups - 8x faster transactions with base layer security. Built with Anchor & TypeScript.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published