メインコンテンツへスキップ

クイックアクセス

基本的なカウンター例はこちらです。

GitHub

Anchor 実装

GitHub

React 実装


ステップバイステップガイド

プログラムを構築し、MagicBlock の Delegation Program DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh を使って delegation hooks を追加しアップグレードします。
1

プログラムを書く

いつも通りに Solana プログラムを書きます。
2

プログラムに delegation と undelegation hooks を追加する

CPI hooks を追加し、状態アカウントを Ephemeral Rollup セッション経由で delegate、commit、undelegate できるようにします。

これらの公開バリデータは開発用として利用できます。委任命令には、 対象となる ER バリデータを必ず追加してください。

メインネット
  • アジア (as.magicblock.app): MAS1Dt9qreoRMQ14YQuhg8UTZMMzDdKhmkZMECCzk57
  • EU (eu.magicblock.app): MEUGGrYPxKk17hCr7wpT6s8dtNokZj5U2L57vjYMS8e
  • 米国 (us.magicblock.app): MUS3hc9TCw4cGC12vHNoYcCGzJG1txjgQLZWVoeNHNd
  • TEE (mainnet-tee.magicblock.app): MTEWGuqxUpYZGFJQcp8tLN7x5v9BSeoFHYWQQ3n3xzo
Devnet
  • アジア (devnet-as.magicblock.app): MAS1Dt9qreoRMQ14YQuhg8UTZMMzDdKhmkZMECCzk57
  • EU (devnet-eu.magicblock.app): MEUGGrYPxKk17hCr7wpT6s8dtNokZj5U2L57vjYMS8e
  • 米国 (devnet-us.magicblock.app): MUS3hc9TCw4cGC12vHNoYcCGzJG1txjgQLZWVoeNHNd
  • TEE (devnet-tee.magicblock.app): FnE6VJT5QNZdedZPnCoLsARgBwoE6DeJNjBs2H1gySXA
ローカルネット
  • ローカル ER (localhost:7799): mAGicPQYBMvcYveUZA5F5UNNwyHvfYh5xkLS2Fr1mev
3

プログラムを Solana にデプロイする。

Anchor または Solana CLI を使ってプログラムを Solana に直接デプロイします。
4

delegation とリアルタイム高速取引の実行準備

SVM RPC 仕様に準拠したトランザクションを、変更なしでオンチェーン・オフチェーンの両方から送信します。

Counter 例

Counter GIF 以下のソフトウェアパッケージが必要になる場合があります。ほかのバージョンでも互換性がある可能性があります。
ソフトウェアバージョンインストールガイド
Solana2.3.13Solana をインストール
Rust1.85.0Rust をインストール
Anchor0.32.1Anchor をインストール
Node24.10.0Node をインストール

コードスニペット

このプログラムは 2 つの主要 instruction を実装しています。
  1. initialize: カウンターを 0 に設定する
  2. increment: カウンターを 1 増やす
また、カウンターの delegation と undelegation 用に次の instruction も実装しています。
  1. Delegate: カウンターを Base Layer から ER に委任する(Base Layer で呼ばれる)
  2. CommitAndUndelegate: カウンターを ER から Base Layer に同期するようスケジュールし、ER 上で undelegate する(ER で呼ばれる)
  3. Commit: カウンターを ER から Base Layer に同期するようスケジュールする(ER で呼ばれる)
  4. Undelegate:
    • カウンターの同期と undelegation をスケジュールする(ER で呼ばれる)
    • #[ephemeral] によって注入された callback instruction で undelegation が実行される(validator CPI を通じて Base Layer で呼ばれる)
The undelegation callback discriminator [196, 28, 41, 206, 48, 37, 51, 167] and its instruction processor must be specified in your program. This instruction triggered by Delegation Program reverts account ownership on the Base Layer after calling undelegation on ER.With [#ephemeral] Anchor macro from MagicBlock’s Ephemeral Rollup SDK, the undelegation callback discriminator and processor are injected into your program.
#[ephemeral]
#[program]
pub mod anchor_counter {
    use super::*;

    /// Initialize the counter.
    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        let counter = &mut ctx.accounts.counter;
        counter.count = 0;
        Ok(())
    }

    /// Increment the counter.
    pub fn increment(ctx: Context<Increment>) -> Result<()> {
        let counter = &mut ctx.accounts.counter;
        counter.count += 1;
        Ok(())
    }

    /// Delegate the account to the delegation program
    /// Set specific validator based on ER, see https://docs.magicblock.gg/pages/get-started/how-integrate-your-program/local-setup
    pub fn delegate(ctx: Context<DelegateInput>) -> Result<()> {
    // ...
    }

    /// Increment the counter and manually commit the account in the Ephemeral Rollup session.
    pub fn increment_and_commit(ctx: Context<IncrementAndCommit>) -> Result<()> {
    // ...
    }

    /// Undelegate the account from the delegation program
    pub fn undelegate(ctx: Context<IncrementAndCommit>) -> Result<()> {
    // ...
    }
}

/// Context for initializing counter
#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(init_if_needed, payer = user, space = 8 + 8, seeds = [TEST_PDA_SEED], bump)]
    pub counter: Account<'info, Counter>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}


/// Context for incrementing counter
#[derive(Accounts)]
pub struct Increment<'info> {
    #[account(mut, seeds = [TEST_PDA_SEED], bump)]
    pub counter: Account<'info, Counter>,
}

/// Counter struct
#[account]
pub struct Counter {
    pub count: u64,
}

/// Other context for delegation


特別なことはなく、単純な Anchor カウンタープログラムです。違いは、undelegation 用の ephemeral macro と、delegation program とやり取りするロジックを注入する delegate macro を追加していることです。⬆️ Back to Top

高度なコードスニペット

委任済み PDA をリサイズする際には:
  • 新しいアカウントサイズでも rent-exempt を保てるだけの lamports が PDA に必要です。
  • 追加 lamports が必要な場合、差額を提供するために payer account も委任済みである必要があります
  • PDA はプログラム所有である必要があり、lamports 転送に必要な signer をトランザクションに含める必要があります。
  • system_instruction::allocate を使用します。
#[account]
pub struct Counter {
    pub count: u64,
    pub extra_data: Vec<u8>,
}

#[derive(Accounts)]
pub struct ResizeCounter<'info> {
    #[account(mut)]
    pub counter: Account<'info, Counter>,
    #[account(mut)]
    pub payer: Signer<'info>,
    pub system_program: Program<'info, System>,
}

    // Resize the counter (e.g., to store more extra_data)
    pub fn resize_counter(ctx: Context<ResizeCounter>, new_size: usize) -> Result<()> {
        let account_to_resize = &mut ctx.accounts.counter.to_account_info();
        let payer = &mut ctx.accounts.payer.to_account_info();

        // Calculate rent-exemption for the new size
        let rent = Rent::get()?;
        let min_balance = rent.minimum_balance(new_size);

        // Top up lamports if needed
        let current_lamports = **account_to_resize.lamports.borrow();
        if current_lamports < min_balance {
            let to_transfer = min_balance - current_lamports;
            **payer.try_borrow_mut_lamports()? -= to_transfer;
            **account_to_resize.try_borrow_mut_lamports()? += to_transfer;
        }

        // Resize account
        account_to_resize.resize(new_size)?;

        Ok(())
    }
⬆️ Back to Top

クイックアクセス

Private ER、Rust Native 実装、ローカル開発についてさらに学びましょう。

Private Ephemeral Rollups (PER)

クイックスタート

Rust Native

クイックスタート

Guide

ローカル開発

Solana エクスプローラー

Solana 上のトランザクションとアカウントの情報を確認できます。

Solana エクスプローラー

公式 Solana エクスプローラー

Solscan

Solana ブロックチェーンを探索する

Solana RPC プロバイダー

既存の RPC プロバイダーを通じてトランザクションやリクエストを送信します。

Solana

Free Public Nodes

Helius

Free Shared Nodes

Triton

Dedicated High-Performance Nodes

Solana バリデータダッシュボード

Solana バリデータインフラのリアルタイム更新を確認できます。

Solana Beach

Get Validator Insights

Validators App

Discover Validator Metrics

サーバーステータス

Solana と MagicBlock のサーバーステータスを確認しましょう。

Solana Status

Subscribe to Solana Server Updates

MagicBlock Status

Subscribe to MagicBlock Server Status

MagicBlock 製品

エフェメラルロールアップ(ER)

Solana 上でリアルタイムかつ手数料ゼロのトランザクションを安全に実行します。

プライベート・エフェメラルロールアップ(PER)

プライバシー保護計算で機密データを守ります。

プライベート決済 API

Solana 上で安全かつコンプライアンスに配慮したプライベート送金を実現します。

検証可能ランダム関数(VRF)

証明可能に公平なランダム性をオンチェーンで直接生成します。

価格オラクル

取引や DeFi 向けの低遅延オンチェーン価格フィードにアクセスできます。