Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Initialize stamps the version and authority

Source: record/tests/record.rs L46

  • Given: a fresh, uninitialized record account with 16 bytes of payload space
  • When: payer initializes the record, nominating alice as authority
  • Then: version = 1 ✓
  • Then: authority = 2rj1EEgg32bupe12mMYnPFaKQCPPyYBhKYmDFZ8cXxpT ✓
  • Then: payload = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ✓

Result: ✓ success — 707 CU · claims 3/3 ✓

sequenceDiagram
    participant p0 as payer
    participant p1 as Record
    p0->>p1: initialize
    activate p1
    p1-->>p0: ✓ 707cu
    deactivate p1

Authority

flowchart LR
    Record["Record"]:::program
    1117[("1117…")]:::state
    Record -->|writes| 1117
    classDef program fill:#dae8fc,stroke:#6c8ebf;
    classDef signer fill:#d5e8d4,stroke:#82b366;
    classDef state fill:#ffe6cc,stroke:#d79b00;

Ownership

flowchart LR
    Record["Record"]:::program
    1117[("1117…")]:::state
    Record -->|owns| 1117
    classDef program fill:#dae8fc,stroke:#6c8ebf;
    classDef signer fill:#d5e8d4,stroke:#82b366;
    classDef state fill:#ffe6cc,stroke:#d79b00;

Accounts

AccountSignerWritableOwner
1117…·Record
alice··system
Call tree and logs
Initialize stamps the version and authority
payer (707cu)
└─ Record::initialize ✓ 707cu
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 invoke [1]
Program log: RecordInstruction::Initialize
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 consumed 707 of 200000 compute units
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 success

Integration

What an integrator writes: the derivations and the instruction, byte-identical to the transaction this page witnessed. Addresses are this scenario’s; supply your own.

#![allow(unused)]
fn main() {
use solana_instruction::{AccountMeta, Instruction};
use solana_pubkey::{pubkey, Pubkey};

// --- initialize ---
let program_id: Pubkey = pubkey!("recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5");
let record_account: Pubkey = pubkey!("1117mWrzzrZr312ebPDHu8tbfMwFNvCvMbr6WepCNG"); // supply your own
let authority: Pubkey = pubkey!("2rj1EEgg32bupe12mMYnPFaKQCPPyYBhKYmDFZ8cXxpT"); // alice: this scenario's value; supply your own
let ix = Instruction {
    program_id,
    accounts: vec![
        AccountMeta::new(record_account, false), // recordAccount
        AccountMeta::new_readonly(authority, false), // authority
    ],
    // discriminator ++ borsh()
    data: vec![0x00],
};
}

The same call through the Codama-generated TypeScript client (the async builder derives every PDA the resolver derived here, so only the free inputs appear):

import { address } from "@solana/kit";
import { getInitializeInstructionAsync } from "your-generated-client";

const ix = await getInitializeInstructionAsync({
  recordAccount: address("1117mWrzzrZr312ebPDHu8tbfMwFNvCvMbr6WepCNG"),
  authority: address("2rj1EEgg32bupe12mMYnPFaKQCPPyYBhKYmDFZ8cXxpT"), // alice
});
How this page verifies itself: the test that ran it

The narrative above is this test; the page and the code cannot drift.

#![allow(unused)]
fn main() {
#[test]
fn initialize_stamps_version_and_authority() {
    let mut story = Story::load(SO, IDL);

    story.given("a fresh, uninitialized record account with 16 bytes of payload space");
    let payer = story.cast("payer");
    let alice = story.cast("alice");
    let record = story.stage_record(WRITABLE_START + 16);

    let version = story.track("version", move |s| s.read_record(&record).0);
    let authority = story.track("authority", move |s| s.read_record(&record).1);
    let payload = story.track("payload", move |s| s.read_record(&record).2);

    let out = story
        .when(
            "payer initializes the record, nominating alice as authority",
            initialize()
                .record_account(record)
                .authority(alice.pubkey()),
            &[&payer],
        )
        .expect_success();

    story.then_eq(&version, 1);
    story.then_eq(&authority, alice.pubkey());
    story.then_eq(&payload, vec![0u8; 16]);
    emit_scenario(&story, &out, "Initialize stamps the version and authority");
}
}