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

Write lands bytes behind the authority

Source: record/tests/record.rs L82

  • Given: a record initialized with alice as authority
  • When: setup: payer initializes the record
  • When: alice writes “hello” at payload offset 0
  • Then: the bytes land at payload offset 0 ✓

Result: ✓ success — 805 CU · claims 1/1 ✓

sequenceDiagram
    participant p0 as alice
    participant p1 as Record
    p0->>p1: write
    activate p1
    p1-->>p0: ✓ 805cu
    deactivate p1

Authority

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

Ownership

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

Accounts

AccountSignerWritableOwner
1119…·Record
alicesystem
Call tree and logs
Write lands bytes behind the authority
alice (805cu)
└─ Record::write ✓ 805cu
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 invoke [1]
Program log: RecordInstruction::Write
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 consumed 805 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};

// --- write ---
let program_id: Pubkey = pubkey!("recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5");
let record_account: Pubkey = pubkey!("1119DWteoLSdjvrT6g6L8C2PfDD2faiTQUpsjY2RiF"); // 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, true), // authority
    ],
    // discriminator ++ borsh(offset: 0, data: …)
    data: vec![0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f],
};
}

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 { getWriteInstructionAsync } from "your-generated-client";

const ix = await getWriteInstructionAsync({
  recordAccount: address("1119DWteoLSdjvrT6g6L8C2PfDD2faiTQUpsjY2RiF"),
  authority: authoritySigner, // alice (a TransactionSigner)
  offset: 0n,
  data: /* … */,
});
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 write_then_read_back() {
    let mut story = Story::load(SO, IDL);

    story.given("a record initialized with alice as authority");
    let payer = story.cast("payer");
    let alice = story.cast("alice");
    let record = story.stage_record(WRITABLE_START + 16);
    story
        .when(
            "setup: payer initializes the record",
            initialize()
                .record_account(record)
                .authority(alice.pubkey()),
            &[&payer],
        )
        .expect_success();

    let out = story
        .when(
            "alice writes \"hello\" at payload offset 0",
            write()
                .record_account(record)
                .authority(&alice)
                .offset(0)
                .data(b"hello".to_vec()),
            &[&alice],
        )
        .expect_success();

    story.then("the bytes land at payload offset 0", move |s| {
        &s.read_record(&record).2[..5] == b"hello"
    });
    emit_scenario(&story, &out, "Write lands bytes behind the authority");
}
}