|
1 | 1 | use anchor_lang::prelude::*;
|
2 | 2 | use light_sdk::{
|
3 |
| - compressed_account::LightAccount, light_account, light_accounts, light_program, |
4 |
| - merkle_context::PackedAddressMerkleContext, |
| 3 | + account::LightAccount, |
| 4 | + cpi::verify::verify_compressed_account_infos, |
| 5 | + error::LightSdkError, |
| 6 | + instruction::{account_meta::CompressedAccountMeta, instruction_data::LightInstructionData}, |
| 7 | + Discriminator, LightDiscriminator, LightHasher, |
5 | 8 | };
|
6 | 9 |
|
7 | 10 | declare_id!("{{program-id}}");
|
8 | 11 |
|
9 |
| -#[light_program] |
10 | 12 | #[program]
|
11 | 13 | pub mod {{rust-name-snake-case}} {
|
| 14 | + use light_sdk::{ |
| 15 | + address::v1::derive_address, |
| 16 | + cpi::accounts::CompressionCpiAccounts, |
| 17 | + NewAddressParamsPacked, |
| 18 | + }; |
| 19 | + |
12 | 20 | use super::*;
|
13 | 21 |
|
14 | 22 | pub fn create<'info>(
|
15 |
| - ctx: LightContext<'_, '_, '_, 'info, Create<'info>>, |
| 23 | + ctx: Context<'_, '_, '_, 'info, GenericAnchorAccounts<'info>>, |
| 24 | + light_ix_data: LightInstructionData, |
| 25 | + output_merkle_tree_index: u8, |
16 | 26 | ) -> Result<()> {
|
17 |
| - ctx.light_accounts.counter.owner = ctx.accounts.signer.key(); |
18 |
| - ctx.light_accounts.counter.counter = 0; |
| 27 | + let program_id = crate::ID.into(); |
| 28 | + let light_cpi_accounts = CompressionCpiAccounts::new( |
| 29 | + ctx.accounts.signer.as_ref(), |
| 30 | + ctx.remaining_accounts, |
| 31 | + crate::ID, |
| 32 | + ) |
| 33 | + .map_err(ProgramError::from)?; |
| 34 | + |
| 35 | + let address_merkle_context = light_ix_data |
| 36 | + .new_addresses |
| 37 | + .ok_or(LightSdkError::ExpectedAddressMerkleContext) |
| 38 | + .map_err(ProgramError::from)?[0]; |
| 39 | + |
| 40 | + let (address, address_seed) = derive_address( |
| 41 | + &[b"counter", ctx.accounts.signer.key().as_ref()], |
| 42 | + &light_cpi_accounts.tree_accounts()[address_merkle_context.address_merkle_tree_pubkey_index as |
| 43 | +usize].key(), |
| 44 | + &crate::ID, |
| 45 | + ); |
| 46 | + |
| 47 | + let new_address_params = NewAddressParamsPacked { |
| 48 | + seed: address_seed, |
| 49 | + address_queue_account_index: address_merkle_context.address_queue_pubkey_index, |
| 50 | + address_merkle_tree_root_index: address_merkle_context.root_index, |
| 51 | + address_merkle_tree_account_index: address_merkle_context.address_merkle_tree_pubkey_index, |
| 52 | + }; |
| 53 | + |
| 54 | + let mut counter = LightAccount::<'_, CounterCompressedAccount>::new_init( |
| 55 | + &program_id, |
| 56 | + Some(address), |
| 57 | + output_merkle_tree_index, |
| 58 | + ); |
| 59 | + |
| 60 | + counter.owner = ctx.accounts.signer.key(); |
| 61 | + |
| 62 | + verify_compressed_account_infos( |
| 63 | + &light_cpi_accounts, |
| 64 | + light_ix_data.proof, |
| 65 | + &[counter.to_account_info().unwrap()], |
| 66 | + Some(vec![new_address_params]), |
| 67 | + None, |
| 68 | + false, |
| 69 | + None, |
| 70 | + ) |
| 71 | + .map_err(ProgramError::from)?; |
19 | 72 |
|
20 | 73 | Ok(())
|
21 | 74 | }
|
22 | 75 |
|
23 | 76 | pub fn increment<'info>(
|
24 |
| - ctx: LightContext<'_, '_, '_, 'info, Increment<'info>>, |
| 77 | + ctx: Context<'_, '_, '_, 'info, GenericAnchorAccounts<'info>>, |
| 78 | + light_ix_data: LightInstructionData, |
| 79 | + counter_value: u64, |
| 80 | + account_meta: CompressedAccountMeta, |
25 | 81 | ) -> Result<()> {
|
26 |
| - ctx.light_accounts.counter.counter += 1; |
| 82 | + let program_id = crate::ID.into(); |
| 83 | + let mut counter = LightAccount::<'_, CounterCompressedAccount>::new_mut( |
| 84 | + &program_id, |
| 85 | + &account_meta, |
| 86 | + CounterCompressedAccount { |
| 87 | + owner: ctx.accounts.signer.key(), |
| 88 | + counter: counter_value, |
| 89 | + }, |
| 90 | + ) |
| 91 | + .map_err(ProgramError::from)?; |
| 92 | + |
| 93 | + counter.counter += 1; |
| 94 | + |
| 95 | + let light_cpi_accounts = CompressionCpiAccounts::new( |
| 96 | + ctx.accounts.signer.as_ref(), |
| 97 | + ctx.remaining_accounts, |
| 98 | + crate::ID, |
| 99 | + ) |
| 100 | + .map_err(ProgramError::from)?; |
| 101 | + |
| 102 | + verify_compressed_account_infos( |
| 103 | + &light_cpi_accounts, |
| 104 | + light_ix_data.proof, |
| 105 | + &[counter.to_account_info().unwrap()], |
| 106 | + None, |
| 107 | + None, |
| 108 | + false, |
| 109 | + None, |
| 110 | + ) |
| 111 | + .map_err(ProgramError::from)?; |
27 | 112 |
|
28 | 113 | Ok(())
|
29 | 114 | }
|
30 | 115 |
|
31 | 116 | pub fn delete<'info>(
|
32 |
| - ctx: LightContext<'_, '_, '_, 'info, Delete<'info>>, |
| 117 | + ctx: Context<'_, '_, '_, 'info, GenericAnchorAccounts<'info>>, |
| 118 | + light_ix_data: LightInstructionData, |
| 119 | + counter_value: u64, |
| 120 | + account_meta: CompressedAccountMeta, |
33 | 121 | ) -> Result<()> {
|
| 122 | + let program_id = crate::ID.into(); |
| 123 | + |
| 124 | + let counter = LightAccount::<'_, CounterCompressedAccount>::new_close( |
| 125 | + &program_id, |
| 126 | + &account_meta, |
| 127 | + CounterCompressedAccount { |
| 128 | + owner: ctx.accounts.signer.key(), |
| 129 | + counter: counter_value, |
| 130 | + }, |
| 131 | + ) |
| 132 | + .map_err(ProgramError::from)?; |
| 133 | + |
| 134 | + let light_cpi_accounts = CompressionCpiAccounts::new( |
| 135 | + ctx.accounts.signer.as_ref(), |
| 136 | + ctx.remaining_accounts, |
| 137 | + crate::ID, |
| 138 | + ) |
| 139 | + .map_err(ProgramError::from)?; |
| 140 | + |
| 141 | + // The true parameter indicates that accounts should be closed |
| 142 | + verify_compressed_account_infos( |
| 143 | + &light_cpi_accounts, |
| 144 | + light_ix_data.proof, |
| 145 | + &[counter.to_account_info().unwrap()], |
| 146 | + None, |
| 147 | + None, |
| 148 | + true, |
| 149 | + None, |
| 150 | + ) |
| 151 | + .map_err(ProgramError::from)?; |
| 152 | + |
34 | 153 | Ok(())
|
35 | 154 | }
|
36 | 155 | }
|
37 | 156 |
|
38 |
| - |
39 |
| -#[light_account] |
40 |
| -#[derive(Clone, Debug, Default)] |
| 157 | +#[derive( |
| 158 | + Clone, Debug, Default, AnchorDeserialize, AnchorSerialize, LightDiscriminator, LightHasher, |
| 159 | +)] |
41 | 160 | pub struct CounterCompressedAccount {
|
42 |
| - #[truncate] |
| 161 | + #[hash] |
43 | 162 | pub owner: Pubkey,
|
44 | 163 | pub counter: u64,
|
45 | 164 | }
|
46 | 165 |
|
47 |
| -#[error_code] |
48 |
| -pub enum CustomError { |
49 |
| - #[msg("No authority to perform this action")] |
50 |
| - Unauthorized, |
51 |
| -} |
52 |
| - |
53 |
| -#[light_accounts] |
54 |
| -pub struct Create<'info> { |
55 |
| - #[account(mut)] |
56 |
| - #[fee_payer] |
57 |
| - pub signer: Signer<'info>, |
58 |
| - #[self_program] |
59 |
| - pub self_program: Program<'info, crate::program::{{rust-name-camel-case}}>, |
60 |
| - /// CHECK: Checked in light-system-program. |
61 |
| - #[authority] |
62 |
| - pub cpi_signer: AccountInfo<'info>, |
63 |
| - #[light_account(init, seeds = [b"counter", signer.key().as_ref()])] |
64 |
| - pub counter: LightAccount<CounterCompressedAccount>, |
65 |
| -} |
66 |
| - |
67 |
| -#[light_accounts] |
68 |
| -pub struct Increment<'info> { |
69 |
| - #[account(mut)] |
70 |
| - #[fee_payer] |
71 |
| - pub signer: Signer<'info>, |
72 |
| - #[self_program] |
73 |
| - pub self_program: Program<'info, crate::program::{{rust-name-camel-case}}>, |
74 |
| - /// CHECK: Checked in light-system-program. |
75 |
| - #[authority] |
76 |
| - pub cpi_signer: AccountInfo<'info>, |
77 |
| - |
78 |
| - #[light_account( |
79 |
| - mut, |
80 |
| - seeds = [b"counter", signer.key().as_ref()], |
81 |
| - constraint = counter.owner == signer.key() @ CustomError::Unauthorized |
82 |
| - )] |
83 |
| - pub counter: LightAccount<CounterCompressedAccount>, |
84 |
| -} |
85 |
| - |
86 |
| -#[light_accounts] |
87 |
| -pub struct Delete<'info> { |
| 166 | +#[derive(Accounts)] |
| 167 | +pub struct GenericAnchorAccounts<'info> { |
88 | 168 | #[account(mut)]
|
89 |
| - #[fee_payer] |
90 | 169 | pub signer: Signer<'info>,
|
91 |
| - #[self_program] |
92 |
| - pub self_program: Program<'info, crate::program::{{rust-name-camel-case}}>, |
93 |
| - /// CHECK: Checked in light-system-program. |
94 |
| - #[authority] |
95 |
| - pub cpi_signer: AccountInfo<'info>, |
96 |
| - |
97 |
| - #[light_account( |
98 |
| - close, |
99 |
| - seeds = [b"counter", signer.key().as_ref()], |
100 |
| - constraint = counter.owner == signer.key() @ CustomError::Unauthorized |
101 |
| - )] |
102 |
| - pub counter: LightAccount<CounterCompressedAccount>, |
103 | 170 | }
|
0 commit comments