solana_json/
instruction.rs

1use borsh::{BorshDeserialize, BorshSerialize};
2use shank::ShankInstruction;
3use solana_program::{
4    instruction::{AccountMeta, Instruction},
5    pubkey::Pubkey,
6    system_program,
7};
8
9#[repr(C)]
10#[derive(PartialEq, Eq, Debug, Clone, BorshSerialize, BorshDeserialize)]
11pub struct SetValueArgs {
12    pub value: String,
13}
14
15#[repr(C)]
16#[repr(C)]
17#[derive(PartialEq, Eq, Debug, Clone, BorshSerialize, BorshDeserialize)]
18pub struct AppendValueArgs {
19    pub value: String,
20}
21
22#[repr(C)]
23#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Debug, Clone)]
24pub struct AddAuthorityArgs {
25    pub new_authority: Pubkey,
26}
27
28#[repr(C)]
29#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Debug, Clone)]
30pub struct RemoveAuthorityArgs {
31    pub authority: Pubkey,
32}
33
34#[derive(Debug, Clone, ShankInstruction, BorshSerialize, BorshDeserialize)]
35#[rustfmt::skip]
36pub enum OnchainMetadataInstructions {
37    /// Description of this instruction
38    #[account(0, writable, signer, name="json_account", desc = "The account to store the metadata in.")]
39    #[account(1, writable, name="json_metadata_account", desc = "The account to store the json account's metadata in.")]
40    #[account(2, writable, signer, name="payer", desc="The account that will pay for the transaction and rent.")]
41    #[account(3, name="system_program", desc = "System program")]
42    Initialize,
43
44    /// Description of this instruction
45    #[account(0, writable, name="json_account", desc = "The account to store the metadata in.")]
46    #[account(1, writable, name="json_metadata_account", desc = "The account to store the json account's metadata in.")]
47    #[account(2, writable, signer, name="payer", desc="The account that will pay for the transaction and rent.")]
48    #[account(3, name="system_program", desc = "System program")]
49    Close,
50
51    /// Description of this instruction
52    #[account(0, writable, name="json_account", desc = "The account to store the metadata in.")]
53    #[account(1, writable, name="json_metadata_account", desc = "The account to store the json account's metadata in.")]
54    #[account(2, writable, signer, name="payer", desc="The account that will pay for the transaction and rent.")]
55    #[account(3, name="system_program", desc = "System program")]
56    SetValue(SetValueArgs),
57
58    /// Description of this instruction
59    #[account(0, writable, name="json_account", desc = "The account to store the metadata in.")]
60    #[account(1, writable, name="json_metadata_account", desc = "The account to store the json account's metadata in.")]
61    #[account(2, writable, signer, name="payer", desc="The account that will pay for the transaction and rent.")]
62    #[account(3, name="system_program", desc = "System program")]
63    AppendValue(AppendValueArgs),
64
65    #[account(0, writable, name="json_metadata_account", desc = "The account to store the metadata's metadata in.")]
66    #[account(1, writable, signer, name="payer", desc="The account that will pay for the transaction and rent.")]
67    #[account(2, name="system_program", desc = "System program")]
68    AddAuthority(AddAuthorityArgs),
69
70    #[account(0, writable, name="json_metadata_account", desc = "The account to store the metadata's metadata in.")]
71    #[account(1, writable, signer, name="payer", desc="The account that will pay for the transaction and rent.")]
72    #[account(2, name="system_program", desc = "System program")]
73    RemoveAuthority(RemoveAuthorityArgs),
74}
75
76pub fn initialize(
77    _program_id: Pubkey,
78    json_account: Pubkey,
79    json_metadata_account: Pubkey,
80    payer: Pubkey,
81) -> Instruction {
82    Instruction {
83        program_id: crate::ID,
84        accounts: vec![
85            AccountMeta::new(json_account, true),
86            AccountMeta::new(json_metadata_account, false),
87            AccountMeta::new(payer, true),
88            AccountMeta::new_readonly(system_program::ID, false),
89        ],
90        data: OnchainMetadataInstructions::Initialize
91            .try_to_vec()
92            .unwrap(),
93    }
94}
95
96pub fn close(
97    _program_id: Pubkey,
98    json_account: Pubkey,
99    json_metadata_account: Pubkey,
100    payer: Pubkey,
101) -> Instruction {
102    Instruction {
103        program_id: crate::ID,
104        accounts: vec![
105            AccountMeta::new(json_account, false),
106            AccountMeta::new(json_metadata_account, false),
107            AccountMeta::new(payer, true),
108            AccountMeta::new_readonly(system_program::ID, false),
109        ],
110        data: OnchainMetadataInstructions::Close.try_to_vec().unwrap(),
111    }
112}
113
114pub fn set_value(
115    _program_id: Pubkey,
116    json_account: Pubkey,
117    json_metadata_account: Pubkey,
118    payer: Pubkey,
119    args: SetValueArgs,
120) -> Instruction {
121    Instruction {
122        program_id: crate::ID,
123        accounts: vec![
124            AccountMeta::new(json_account, false),
125            AccountMeta::new(json_metadata_account, false),
126            AccountMeta::new(payer, true),
127            AccountMeta::new_readonly(system_program::ID, false),
128        ],
129        data: OnchainMetadataInstructions::SetValue(args)
130            .try_to_vec()
131            .unwrap(),
132    }
133}
134
135pub fn append_value(
136    _program_id: Pubkey,
137    json_account: Pubkey,
138    json_metadata_account: Pubkey,
139    payer: Pubkey,
140    args: AppendValueArgs,
141) -> Instruction {
142    Instruction {
143        program_id: crate::ID,
144        accounts: vec![
145            AccountMeta::new(json_account, false),
146            AccountMeta::new(json_metadata_account, false),
147            AccountMeta::new(payer, true),
148            AccountMeta::new_readonly(system_program::ID, false),
149        ],
150        data: OnchainMetadataInstructions::AppendValue(args)
151            .try_to_vec()
152            .unwrap(),
153    }
154}
155
156pub fn add_authority(
157    _program_id: Pubkey,
158    json_metadata_account: Pubkey,
159    payer: Pubkey,
160    args: AddAuthorityArgs,
161) -> Instruction {
162    Instruction {
163        program_id: crate::ID,
164        accounts: vec![
165            AccountMeta::new(json_metadata_account, false),
166            AccountMeta::new(payer, true),
167            AccountMeta::new_readonly(system_program::ID, false),
168        ],
169        data: OnchainMetadataInstructions::AddAuthority(args)
170            .try_to_vec()
171            .unwrap(),
172    }
173}
174
175pub fn remove_authority(
176    _program_id: Pubkey,
177    json_metadata_account: Pubkey,
178    payer: Pubkey,
179    args: RemoveAuthorityArgs,
180) -> Instruction {
181    Instruction {
182        program_id: crate::ID,
183        accounts: vec![
184            AccountMeta::new(json_metadata_account, false),
185            AccountMeta::new(payer, true),
186            AccountMeta::new_readonly(system_program::ID, false),
187        ],
188        data: OnchainMetadataInstructions::RemoveAuthority(args)
189            .try_to_vec()
190            .unwrap(),
191    }
192}