Skip to main content

Step-By-Step Guide

Build valid transactions that calls your program instructions for delegation and undelegation. The complete test for this project can be found in the Typescript Test Script.
1

Import SDK and initialize Connection

Set up connection and accounts
2

Delegate PDA on Base Layer

Test CPI hook to delegate state account on Base Layer
3

Commit PDA on ER

Test CPI hook to commit state account on ER
4

Undelegate PDA on ER

Test CPI hook to undelegate state account on ER

Quick Access

If you prefer to dive straight into the code:

Code Snippets

  • Connection
  • Delegate
  • Commit
  • Undelegate

Import SDK and create Connection

Import relevant libraries @magicblock-labs/ephemeral-rollups-sdk or @magicblock-labs/ephemeral-rollups-kit. Initialize connection before testing and sending transactions.
yarn add @magicblock-labs/ephemeral-rollups-kit@latest
These public RPC endpoints are currently free and supported for development:
Magic Router Devnet: https://devnet-router.magicblock.app
Solana Devnet: https://api.devnet.solana.com
ER Devnet: https://devnet.magicblock.app
TEE Devnet: https://tee.magicblock.app/
Find out more details here .
import {
  Instruction,
  getAddressEncoder,
  getProgramDerivedAddress,
  AccountRole,
  createKeyPairFromBytes,
  getAddressFromPublicKey,
  address,
  createTransactionMessage,
  appendTransactionMessageInstructions,
  pipe,
  setTransactionMessageFeePayer,
} from "@solana/kit";

import {
  Connection,
  DELEGATION_PROGRAM_ID,
  delegationRecordPdaFromDelegatedAccount,
  delegationMetadataPdaFromDelegatedAccount,
  delegateBufferPdaFromDelegatedAccountAndOwnerProgram,
  MAGIC_CONTEXT_ID,
  MAGIC_PROGRAM_ID,
} from "@magicblock-labs/ephemeral-rollups-kit";

// Set up a base and ephemeral connection (alternatively use router, see Magic Router)
const connection = await Connection.create(
  process.env.PROVIDER_ENDPOINT || "https://api.devnet.solana.com",
  process.env.WS_ENDPOINT || "wss://api.devnet.solana.com"
);
const ephemeralConnection = await Connection.create(
  process.env.EPHEMERAL_PROVIDER_ENDPOINT || "https://devnet-as.magicblock.app",
  process.env.EPHEMERAL_WS_ENDPOINT || "wss://devnet-as.magicblock.app"
);

// Prepare user
const userKeypair = await initializeSolSignerKeypair();
const userPubkey = await getAddressFromPublicKey(userKeypair.publicKey);

// Get PDA
const addressEncoder = getAddressEncoder();
const [counterPda, bump] = await getProgramDerivedAddress({
  programAddress: PROGRAM_ID,
  seeds: [Buffer.from("counter_account"), addressEncoder.encode(userPubkey)],
});
⬆️ Back to Top

Solana Explorer

Get insights about your transactions and accounts on Solana:

Solana RPC Providers

Send transactions and requests through existing RPC providers:

Solana Validator Dashboard

Find real-time updates on Solana’s validator infrastructure:

Server Status

Subscribe to Solana’s and MagicBlock’s server status: