メインコンテンツへスキップ
このガイドでは、counter をインクリメントするシンプルな Anchor プログラムの書き方を順を追って説明します。Solana 上にデプロイし、React クライアントから対話する方法も学びます。

ソフトウェアパッケージ

このプログラムは以下のソフトウェアパッケージで開発・テストされています。他のバージョンでも互換性がある場合があります。
ソフトウェアバージョンインストールガイド
Solana3.1.9Install Solana
Rust1.89.0Install Rust
Anchor1.0.2Install Anchor

ソースコードへのクイックアクセス

すぐにコードを確認したい場合:

ソースコード:Anchor Counter プログラム

ライブサンプルアプリ

Anchor プログラムを書く

counter プログラムの主要な構成要素を順に見ていきましょう:

コア機能

このプログラムは 2 つの主要な instruction を実装します:
  1. initialize:counter を 0 にセット
  2. increment:counter を 1 だけ加算
プログラムの中核構造は以下のとおりです:
#[ephemeral]
#[program]
pub mod public_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(())
    }

    // ... Additional instructions will be added here
}
特別な点はなく、counter をインクリメントするだけのシンプルな Anchor プログラムです。唯一の違いは、委任プログラムと連携するための便利なロジックを注入する delegate マクロを追加していることです。

Counter PDA を委任する

counter PDA を委任し、Ephemeral Rollup セッション内で書き込み可能にするには、内部的に delegate_account 関数を呼び出す instruction を追加する必要があります。delegate_account は委任プログラムへ CPI を行い、検証が通るとそのアカウントの所有権を取得します。 このステップが完了すると、ephemeral validator が counter PDA に対するトランザクションの処理を開始し、委任プログラムを通じて状態差分を提案できるようになります。

トランザクション(Base Layer):Delegate

Solana Explorer でトランザクション詳細を確認
/// Add delegate function to the context
#[delegate]
#[derive(Accounts)]
pub struct DelegateInput<'info> {
    pub payer: Signer<'info>,
    /// CHECK The pda to delegate
    #[account(mut, del)]
    pub pda: AccountInfo<'info>,
}
/// Delegate the account to the delegation program
/// Set specific validator based on ER, see https://docs.magicblock.gg/pages/ephemeral-rollups-ers/how-to-guide/local-development
pub fn delegate(ctx: Context<DelegateInput>) -> Result<()> {
    ctx.accounts.delegate_pda(
        &ctx.accounts.payer,
        &[COUNTER_SEED],
        DelegateConfig {
            // Optionally set a specific validator from the first remaining account
            validator: ctx.remaining_accounts.first().map(|acc| acc.key()),
            ..Default::default()
        },
    )?;
    Ok(())
}

PDA が委任されている間に commit する

ephemeral ランタイムでは、PDA が委任されている間にその状態を commit できます。これは commit intent を持つ MagicIntentBundleBuilder を構築して呼び出すことで実現します。

トランザクション(ER):Commit

Solana Explorer でトランザクション詳細を確認

トランザクション(Base Layer):Commit

Solana Explorer でトランザクション詳細を確認
use ephemeral_rollups_sdk::ephem::MagicIntentBundleBuilder;

/// Increment the counter and manually commit the account in the Ephemeral Rollup session.
pub fn increment_and_commit(ctx: Context<IncrementAndCommit>) -> Result<()> {
    let counter = &mut ctx.accounts.counter;
    counter.count += 1;
    // Serialize the Anchor account before the CPI sees it
    counter.exit(&crate::ID)?;
    MagicIntentBundleBuilder::new(
        ctx.accounts.payer.to_account_info(),
        ctx.accounts.magic_context.to_account_info(),
        ctx.accounts.magic_program.to_account_info(),
    )
    .commit(&[ctx.accounts.counter.to_account_info()])
    .build_and_invoke()?;
    Ok(())
}

PDA の委任を解除する

PDA の委任解除は、commit_and_undelegate intent を持つ MagicIntentBundleBuilder を構築することで行います。 これにより最新状態がコミットされ、PDA の所有権が所有者プログラムへ戻されます。

トランザクション(ER):Undelegate

Solana Explorer でトランザクション詳細を確認

トランザクション(Base Layer):Undelegate

Solana Explorer でトランザクション詳細を確認
use ephemeral_rollups_sdk::ephem::MagicIntentBundleBuilder;

/// Undelegate the account from the delegation program
pub fn undelegate(ctx: Context<IncrementAndCommit>) -> Result<()> {
    MagicIntentBundleBuilder::new(
        ctx.accounts.payer.to_account_info(),
        ctx.accounts.magic_context.to_account_info(),
        ctx.accounts.magic_program.to_account_info(),
    )
    .commit_and_undelegate(&[ctx.accounts.counter.to_account_info()])
    .build_and_invoke()?;
    Ok(())
}

React クライアントの接続

React クライアントは Anchor プログラムと対話するためのシンプルなインターフェースです。 Anchor バインディングを使ってプログラムと通信し、MagicBlock SDK で Ephemeral Rollup セッションとやり取りします。ソースはプログラムと同じ場所、anchor-counter/app にあります。

ライブサンプルアプリ

iframe は一部のウォレット(例:Backpack)でのみ動作します。代わりに以下のデプロイ済みデモを試してください:https://counter-example.magicblock.app/

Ephemeral エンドポイントの設定

Ephemeral Rollup セッションと対話するには、適切なエンドポイントを設定する必要があります:
  • devnet では、以下の ephemeral エンドポイントを使用してください: https://devnet.magicblock.app
  • mainnet については、適切なエンドポイントを得るために MagicBlock チームへお問い合わせください。
  • localhost の場合は、ephemeral validator をダウンロード・インストールし、適切な環境変数を指定してローカルで実行してください。
開発環境または本番環境に応じて、クライアントの設定を正しいエンドポイントに更新してください。
これらの公開 RPC エンドポイントは現在無料で、開発用途に利用できます:
Magic Router Devnet: https://devnet-router.magicblock.app
Solana Devnet: https://api.devnet.solana.com
ER Devnet: https://devnet.magicblock.app
TEE Devnet: https://devnet-tee.magicblock.app/
詳細は こちら をご覧ください。

ソースコード:Anchor Counter プログラム

ライブサンプルアプリ