Skip to content

Commit dd9a0aa

Browse files
committed
refactor: to light sdk v2 cargo generate works, not compiled yet
1 parent 1c8c00d commit dd9a0aa

File tree

4 files changed

+261
-226
lines changed

4 files changed

+261
-226
lines changed

cargo-generate.toml

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
[template]
22
cargo_generate_version = ">=0.18.2"
3-
ignore = [ "circuits/program_name/cargo-generate.toml", "programs_psp/program_name/cargo-generate.toml" ]
3+
ignore = [
4+
"circuits/program_name/cargo-generate.toml",
5+
"programs_psp/program_name/cargo-generate.toml",
6+
]
47

58

69
[placeholders.rust-name]
@@ -40,13 +43,9 @@ prompt = "light macros version"
4043
type = "string"
4144
prompt = "light sdk version"
4245

43-
[placeholders.light-sdk-macros-version]
46+
[placeholders.light-compressed-account-version]
4447
type = "string"
45-
prompt = "light sdk macros version"
46-
47-
[placeholders.light-utils-version]
48-
type = "string"
49-
prompt = "Light utils version"
48+
prompt = "light compressed account version"
5049

5150
[placeholders.light-verifier-version]
5251
type = "string"
@@ -70,4 +69,4 @@ prompt = "solana program test version"
7069

7170
[placeholders.tokio-version]
7271
type = "string"
73-
prompt = "tokio version"
72+
prompt = "tokio version"

programs/{{rust-name}}/Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ idl-build = ["anchor-lang/idl-build", "light-sdk/idl-build"]
2020

2121
[dependencies]
2222
anchor-lang = "{{anchor-version}}"
23-
light-hasher = {version="{{light-hasher-version}}", features = ["solana"] }
23+
light-hasher = { version = "{{light-hasher-version}}", features = ["solana"] }
2424
light-macros = "{{light-macros-version}}"
2525
light-sdk = "{{light-sdk-version}}"
26-
light-sdk-macros = "{{light-sdk-macros-version}}"
27-
light-utils = "{{light-utils-version}}"
2826
light-verifier = "{{light-verifier-version}}"
27+
light-compressed-account = "{{light-compressed-account-version}}"
2928

3029
[target.'cfg(not(target_os = "solana"))'.dependencies]
3130
solana-sdk = "{{solana-sdk-version}}"
3231

3332
[dev-dependencies]
34-
light-client ="{{light-client-version}}"
33+
light-client = "{{light-client-version}}"
3534
light-test-utils = "{{light-test-utils-version}}"
3635
solana-program-test = "{{solana-program-test-version}}"
3736
tokio = "{{tokio-version}}"

programs/{{rust-name}}/src/lib.rs

+134-67
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,170 @@
11
use anchor_lang::prelude::*;
22
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,
58
};
69

710
declare_id!("{{program-id}}");
811

9-
#[light_program]
1012
#[program]
1113
pub mod {{rust-name-snake-case}} {
14+
use light_sdk::{
15+
address::v1::derive_address,
16+
cpi::accounts::CompressionCpiAccounts,
17+
NewAddressParamsPacked,
18+
};
19+
1220
use super::*;
1321

1422
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,
1626
) -> 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)?;
1972

2073
Ok(())
2174
}
2275

2376
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,
2581
) -> 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)?;
27112

28113
Ok(())
29114
}
30115

31116
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,
33121
) -> 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+
34153
Ok(())
35154
}
36155
}
37156

38-
39-
#[light_account]
40-
#[derive(Clone, Debug, Default)]
157+
#[derive(
158+
Clone, Debug, Default, AnchorDeserialize, AnchorSerialize, LightDiscriminator, LightHasher,
159+
)]
41160
pub struct CounterCompressedAccount {
42-
#[truncate]
161+
#[hash]
43162
pub owner: Pubkey,
44163
pub counter: u64,
45164
}
46165

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> {
88168
#[account(mut)]
89-
#[fee_payer]
90169
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>,
103170
}

0 commit comments

Comments
 (0)