Consensus Basics
8 examples to get you started with Consensus on Agave 4.1.1 - 5 basic and 3 intermediate.
Prerequisites
solana config set --url devnet
npm install @solana/kit@7.0.0Basic Examples
1. Inspect via RPC
import { createSolanaRpc } from "@solana/kit";
const rpc = createSolanaRpc("https://api.devnet.solana.com");
const slot = await rpc.getSlot().send();
console.log(slot);- Confirms RPC connectivity and cluster reachability
- Use devnet before mainnet-beta integration
2. CLI Verification
solana slot
solana balance- Solana CLI 3.0.10 shares config with Anchor provider
- Match
solana config getURL with client RPC
3. Read On-Chain Logs
solana confirm -v <SIGNATURE>- Logs expose program
msg!output and CU consumed - Decode errors before retrying transactions
4. Simulate First
const sim = await rpc.simulateTransaction(encodedTx, { sigVerify: false }).send();
console.log(sim.value.err, sim.value.logs);- Preflight catches most failures without landing fees
- Essential during Anchor 0.32.1 program development
5. Anchor Program Hook
use anchor_lang::prelude::*;
#[program]
pub mod example {
use super::*;
pub fn touch(_ctx: Context<Touch>) -> Result<()> { Ok(()) }
}
#[derive(Accounts)]
pub struct Touch {}- Programs execute in Sealevel with explicit account lists
- Stateless logic + separate data accounts
Intermediate Examples
6. Build Version 0 Transaction
import { createTransactionMessage, pipe } from "@solana/kit";
const msg = pipe(createTransactionMessage({ version: 0 }), (m) => m);- v0 transactions support address lookup tables
- Prefer @solana/kit 7.0.0 transaction builders
7. Set Compute Budget
# Client adds ComputeBudgetProgram instructions for CU limit and price- Request enough CU under load
- Priority fees use microlamports per CU
8. Test Locally
npx surfpool@0.12.0 run anchor test- Surfpool provides Agave-compatible local validation
- LiteSVM 0.6.x suits lightweight unit tests
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.