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#[cfg_attr(feature = "wincode", derive(wincode::SchemaRead, wincode::SchemaWrite))]
25#[derive(Debug, Default, PartialEq, Eq, Clone)]
26pub struct AccountMeta {
27 /// An account's public key.
28 pub pubkey: Pubkey,
29 /// True if an `Instruction` requires a `Transaction` signature matching `pubkey`.
30 pub is_signer: bool,
31 /// True if the account data or metadata may be mutated during program execution.
32 pub is_writable: bool,
33}
34
35impl AccountMeta {
36 /// Construct metadata for a writable account.
37 ///
38 /// # Examples
39 ///
40 /// ```
41 /// # use solana_pubkey::Pubkey;
42 /// # use solana_instruction::{AccountMeta, Instruction};
43 /// # use borsh::{BorshSerialize, BorshDeserialize};
44 /// #
45 /// # #[derive(BorshSerialize, BorshDeserialize)]
46 /// # #[borsh(crate = "borsh")]
47 /// # pub struct MyInstruction;
48 /// #
49 /// # let instruction = MyInstruction;
50 /// # let from = Pubkey::new_unique();
51 /// # let to = Pubkey::new_unique();
52 /// # let program_id = Pubkey::new_unique();
53 /// let instr = Instruction::new_with_borsh(
54 /// program_id,
55 /// &instruction,
56 /// vec![
57 /// AccountMeta::new(from, true),
58 /// AccountMeta::new(to, false),
59 /// ],
60 /// );
61 /// ```
62 pub fn new(pubkey: Pubkey, is_signer: bool) -> Self {
63 Self {
64 pubkey,
65 is_signer,
66 is_writable: true,
67 }
68 }
69
70 /// Construct metadata for a read-only account.
71 ///
72 /// # Examples
73 ///
74 /// ```
75 /// # use solana_pubkey::Pubkey;
76 /// # use solana_instruction::{AccountMeta, Instruction};
77 /// # use borsh::{BorshSerialize, BorshDeserialize};
78 /// #
79 /// # #[derive(BorshSerialize, BorshDeserialize)]
80 /// # #[borsh(crate = "borsh")]
81 /// # pub struct MyInstruction;
82 /// #
83 /// # let instruction = MyInstruction;
84 /// # let from = Pubkey::new_unique();
85 /// # let to = Pubkey::new_unique();
86 /// # let from_account_storage = Pubkey::new_unique();
87 /// # let program_id = Pubkey::new_unique();
88 /// let instr = Instruction::new_with_borsh(
89 /// program_id,
90 /// &instruction,
91 /// vec![
92 /// AccountMeta::new(from, true),
93 /// AccountMeta::new(to, false),
94 /// AccountMeta::new_readonly(from_account_storage, false),
95 /// ],
96 /// );
97 /// ```
98 pub fn new_readonly(pubkey: Pubkey, is_signer: bool) -> Self {
99 Self {
100 pubkey,
101 is_signer,
102 is_writable: false,
103 }
104 }
105}