Why Fetch Blockhash for Accounts?
In MagicBlock’s architecture, accounts can exist in two execution environments:- Solana Mainnet: The base layer for persistent, composable state
- Ephemeral Rollups: High-performance execution layers for delegated accounts
getBlockhashForAccounts
method to:
- Analyze Account Delegation: Check where your transaction’s writable accounts are currently delegated
- Fetch Correct Blockhash: Get the appropriate blockhash from either the Ephemeral Rollup or Solana mainnet
- Ensure Transaction Validity: Prevent transaction failures due to blockhash mismatches
Using a standard Solana
getLatestBlockhash()
call may result in transaction failures if your accounts are delegated to an Ephemeral Rollup, as the blockhash progression differs between layers.Why Get the Nearest Validator?
ThegetClosestValidator
function optimizes transaction performance by:
- Reducing Latency: Connecting to geographically closer validators for faster transaction confirmation
- Load Balancing: Distributing transactions across available validators in the network
- Network Optimization: Ensuring optimal routing through the MagicBlock infrastructure
getClosestValidator
to automatically select the best one for your users. Here is an example of a contract and frontend following this flow.
Complete Delegation Example
Smart Contract Implementation
First, let’s look at the Rust contract that handles account delegation:Frontend Implementation
Now let’s see how to call this from the frontend using the Magic Router SDK:Key Benefits of This Approach
Automatic Validator Selection
Automatic Validator Selection
Using
getClosestValidator()
ensures optimal performance by automatically selecting the geographically closest and least loaded validator for your users.Batch Delegation
Batch Delegation
The example shows how to delegate multiple accounts in a single transaction using
postInstructions()
, reducing transaction costs and setup time.Flexible Commit Frequency
Flexible Commit Frequency
The
commitFrequencyMs
parameter allows you to balance between performance (lower frequency) and data persistence guarantees (higher frequency).After delegation, all subsequent transactions involving these accounts will automatically route to the Ephemeral Rollup, providing sub-second confirmation times and higher throughput for your application.