Skip to main content

kvault_interface/
util.rs

1//! [`AccountMeta`] builder utilities.
2//!
3//! Shorthand constructors used by the [`instructions`](crate::instructions) module
4//! to build account lists concisely.
5
6use solana_instruction::AccountMeta;
7use solana_pubkey::Pubkey;
8
9/// Build an `AccountMeta` for an optional account.
10///
11/// When `opt` is `None`, the program ID is substituted as a non-writable,
12/// non-signer placeholder (Anchor 0.29 convention for optional accounts).
13pub fn optional_account(
14    program_id: &Pubkey,
15    opt: Option<Pubkey>,
16    is_writable: bool,
17) -> AccountMeta {
18    match opt {
19        Some(key) => AccountMeta {
20            pubkey: key,
21            is_signer: false,
22            is_writable,
23        },
24        None => AccountMeta {
25            pubkey: *program_id,
26            is_signer: false,
27            is_writable: false,
28        },
29    }
30}
31
32/// Non-signer, non-writable account.
33pub fn readonly(pubkey: Pubkey) -> AccountMeta {
34    AccountMeta {
35        pubkey,
36        is_signer: false,
37        is_writable: false,
38    }
39}
40
41/// Non-signer, writable account.
42pub fn writable(pubkey: Pubkey) -> AccountMeta {
43    AccountMeta {
44        pubkey,
45        is_signer: false,
46        is_writable: true,
47    }
48}
49
50/// Signer, non-writable account.
51pub fn signer(pubkey: Pubkey) -> AccountMeta {
52    AccountMeta {
53        pubkey,
54        is_signer: true,
55        is_writable: false,
56    }
57}
58
59/// Signer and writable account.
60pub fn signer_writable(pubkey: Pubkey) -> AccountMeta {
61    AccountMeta {
62        pubkey,
63        is_signer: true,
64        is_writable: true,
65    }
66}