Skip to main content
A simple counter program demonstrating how to use the Anchor framework with Ephemeral Rollups. This example shows how to delegate accounts, execute low-latency transactions, and commit state back to the base layer using Anchor’s ergonomic macros.

What You’ll Learn

  • How to use Anchor’s #[ephemeral], #[delegate], and #[commit] macros
  • How to delegate a PDA to Ephemeral Rollups
  • How to execute transactions with low latency on delegated accounts
  • How to commit state changes back to Solana
  • How to undelegate accounts from Ephemeral Rollups

Program Structure

The Anchor counter program includes the following instructions:
  • initialize - Initialize the counter PDA to 0
  • increment - Increment the counter by 1 (with rollover at 1000)
  • delegate - Delegate the counter account to the delegation program
  • commit - Manually commit the account state to Solana
  • undelegate - Commit and undelegate the account
  • increment_and_commit - Increment and commit in one transaction
  • increment_and_undelegate - Increment and undelegate in one transaction

Software Requirements

Ensure you have the following software packages installed before building the program.

Build and Test

1

Install dependencies

2

Run tests (skip build and deploy)

The test script automatically detects the cluster from Anchor.toml and handles Ephemeral Rollup setup for localnet:
3

Build, deploy and test (optional)

To build, deploy and run tests with a new program:

Program Implementation

Delegation Macro

The program uses Anchor’s special macros to enable Ephemeral Rollups integration:
The #[ephemeral] macro marks the program as compatible with Ephemeral Rollups, enabling special delegation features.

Delegate Instruction

The delegate instruction uses the #[delegate] macro on the context struct:
The #[delegate] macro automatically adds the required delegation accounts to the context, and the #[account(mut, del)] attribute marks which account to delegate.

Increment Instruction

The core increment logic is simple:

Commit and Undelegate

The #[commit] macro simplifies committing state changes:
The #[commit] macro automatically adds the magic_program and magic_context accounts required for committing state.

TypeScript Client Usage

Initialize and Increment on Solana

Delegate to Ephemeral Rollups

Execute on Ephemeral Rollups

Key Features

Ergonomic Macros

Use #[ephemeral], #[delegate], and #[commit] macros to simplify Ephemeral Rollups integration

Automatic Account Injection

Delegation and commit accounts are automatically added to instruction contexts

Type Safety

Anchor’s type-safe framework ensures correct account structures and validation

Flexible Commits

Commit state manually or combine with other operations in a single instruction

Source Code

View the complete source code on GitHub: anchor-counter on GitHub