solana_instruction/account_meta.rs
1use solana_pubkey::Pubkey;
2
3/// Describes a single account read or written by a program during instruction
4/// execution.
5///
6/// When constructing an [`Instruction`], a list of all accounts that may be
7/// read or written during the execution of that instruction must be supplied.
8/// Any account that may be mutated by the program during execution, either its
9/// data or metadata such as held lamports, must be writable.
10///
11/// Note that because the Solana runtime schedules parallel transaction
12/// execution around which accounts are writable, care should be taken that only
13/// accounts which actually may be mutated are specified as writable. As the
14/// default [`AccountMeta::new`] constructor creates writable accounts, this is
15/// a minor hazard: use [`AccountMeta::new_readonly`] to specify that an account
16/// is not writable.
17///
18/// [`Instruction`]: crate::Instruction
19#[repr(C)]
20#[cfg_attr(
21 feature = "serde",
22 derive(serde_derive::Serialize, serde_derive::Deserialize)
23)]
24#[derive(Debug, Default, PartialEq, Eq, Clone)]
25pub struct AccountMeta {
26 /// An account's public key.
27 pub pubkey: Pubkey,
28 /// True if an `Instruction` requires a `Transaction` signature matching `pubkey`.
29 pub is_signer: bool,
30 /// True if the account data or metadata may be mutated during program execution.
31 pub is_writable: bool,
32}
33
34impl AccountMeta {
35 /// Construct metadata for a writable account.
36 ///
37 /// # Examples
38 ///
39 /// ```
40 /// # use solana_pubkey::Pubkey;
41 /// # use solana_instruction::{AccountMeta, Instruction};
42 /// # use borsh::{BorshSerialize, BorshDeserialize};
43 /// #
44 /// # #[derive(BorshSerialize, BorshDeserialize)]
45 /// # #[borsh(crate = "borsh")]
46 /// # pub struct MyInstruction;
47 /// #
48 /// # let instruction = MyInstruction;
49 /// # let from = Pubkey::new_unique();
50 /// # let to = Pubkey::new_unique();
51 /// # let program_id = Pubkey::new_unique();
52 /// let instr = Instruction::new_with_borsh(
53 /// program_id,
54 /// &instruction,
55 /// vec![
56 /// AccountMeta::new(from, true),
57 /// AccountMeta::new(to, false),
58 /// ],
59 /// );
60 /// ```
61 pub fn new(pubkey: Pubkey, is_signer: bool) -> Self {
62 Self {
63 pubkey,
64 is_signer,
65 is_writable: true,
66 }
67 }
68
69 /// Construct metadata for a read-only account.
70 ///
71 /// # Examples
72 ///
73 /// ```
74 /// # use solana_pubkey::Pubkey;
75 /// # use solana_instruction::{AccountMeta, Instruction};
76 /// # use borsh::{BorshSerialize, BorshDeserialize};
77 /// #
78 /// # #[derive(BorshSerialize, BorshDeserialize)]
79 /// # #[borsh(crate = "borsh")]
80 /// # pub struct MyInstruction;
81 /// #
82 /// # let instruction = MyInstruction;
83 /// # let from = Pubkey::new_unique();
84 /// # let to = Pubkey::new_unique();
85 /// # let from_account_storage = Pubkey::new_unique();
86 /// # let program_id = Pubkey::new_unique();
87 /// let instr = Instruction::new_with_borsh(
88 /// program_id,
89 /// &instruction,
90 /// vec![
91 /// AccountMeta::new(from, true),
92 /// AccountMeta::new(to, false),
93 /// AccountMeta::new_readonly(from_account_storage, false),
94 /// ],
95 /// );
96 /// ```
97 pub fn new_readonly(pubkey: Pubkey, is_signer: bool) -> Self {
98 Self {
99 pubkey,
100 is_signer,
101 is_writable: false,
102 }
103 }
104}