Skip to main content

Overview

This guide demonstrates how to implement cranks using MagicBlock’s Ephemeral Rollups (ER) with the Anchor framework. The implementation follows this flow:
  1. Initialize counter on Solana base layer
  2. Delegate counter account to Ephemeral Rollup for faster execution
  3. Schedule a crank task that automatically increments the counter
  4. Execute the crank automatically at specified intervals
  5. Undelegate the account back to Solana base layer when done

Execution Flow

This guide describes the scheduler in validator 1.1.1, which the mainnet ERs and devnet-eu/devnet-as run. devnet-us runs validator 2.0.0, where cranks run through Hydra and ScheduleTask rejects any signer account in the scheduled instruction.
On validator 1.1.1, task IDs are shared by every program on the validator. If another program already holds your task_id, your ScheduleTask transaction still succeeds, but the scheduler rejects the task afterwards and it never runs. Don’t hardcode task_id: 1; derive an ID that is unique to your program and account. On validator 2.0, IDs are scoped to the scheduling authority.

Core Components

Dependencies

Besides anchor-lang, the snippets below use these crates and imports:

Scheduled Function

Let’s take for example that you wanted to schedule the following simple increment instruction for a counter.
A scheduled instruction is an ordinary instruction: anyone can also call increment directly. That is fine for this counter, but don’t schedule an instruction that moves funds or changes privileged state unless the instruction itself checks who is allowed to call it.

Schedule Increment Function

The core crank scheduling logic:
Key Points:
  • Creates an instruction to increment the counter
  • Serializes it into a ScheduleTask instruction for MagicBlock
  • Uses CPI (Cross-Program Invocation) to call the MagicBlock program
  • The MagicBlock program handles the scheduling and execution

Schedule Arguments

ScheduleIncrement Context

Important: Uses AccountInfo instead of Account<Counter> to avoid Anchor re-serializing stale data after CPI calls.