Skip to content

refactor: to light sdk v2 cargo #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[workspace]
members = [
"programs/*"
]
members = ["programs/*"]
resolver = "2"

[profile.release]
Expand All @@ -12,23 +10,3 @@ codegen-units = 1
opt-level = 3
incremental = false
codegen-units = 1

[patch.crates-io]
"solana-account-decoder" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-accounts-db" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-banks-client" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-banks-interface" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-banks-server" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-program" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-program-test" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-program-runtime" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-rpc-client" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-rpc-client-api" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-runtime" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-sdk" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-sdk-macro" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-client" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-zk-token-sdk" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-frozen-abi" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-frozen-abi-macro" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-transaction-status" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
27 changes: 9 additions & 18 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[template]
cargo_generate_version = ">=0.18.2"
ignore = [ "circuits/program_name/cargo-generate.toml", "programs_psp/program_name/cargo-generate.toml" ]
ignore = [
"circuits/program_name/cargo-generate.toml",
"programs_psp/program_name/cargo-generate.toml",
]


[placeholders.rust-name]
Expand Down Expand Up @@ -32,21 +35,13 @@ prompt = "Borsh version"
type = "string"
prompt = "light hasher version"

[placeholders.light-macros-version]
type = "string"
prompt = "light macros version"

[placeholders.light-sdk-version]
type = "string"
prompt = "light sdk version"

[placeholders.light-sdk-macros-version]
type = "string"
prompt = "light sdk macros version"

[placeholders.light-utils-version]
[placeholders.light-compressed-account-version]
type = "string"
prompt = "Light utils version"
prompt = "light compressed account version"

[placeholders.light-verifier-version]
type = "string"
Expand All @@ -60,14 +55,10 @@ prompt = "solana sdk version"
type = "string"
prompt = "light client version"

[placeholders.light-test-utils-version]
type = "string"
prompt = "light test utils version"

[placeholders.solana-program-test-version]
[placeholders.light-program-test-version]
type = "string"
prompt = "solana program test version"
prompt = "light program test version"

[placeholders.tokio-version]
type = "string"
prompt = "tokio version"
prompt = "tokio version"
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"license": "ISC",
"license": "ISC",
"scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
},
"dependencies": {
"@coral-xyz/anchor": "^0.30.1"
"@coral-xyz/anchor": "^0.31.1"
},
"devDependencies": {
"chai": "^4.3.4",
Expand Down
22 changes: 10 additions & 12 deletions programs/{{rust-name}}/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "{{rust-name}}"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"
rust-version = "1.75.0"

[lib]
crate-type = ["cdylib", "lib"]
Expand All @@ -15,23 +16,20 @@ no-log-ix-name = []
cpi = ["no-entrypoint"]
default = ["idl-build"]
test-sbf = []
bench-sbf = []
idl-build = ["anchor-lang/idl-build", "light-sdk/idl-build"]

[dependencies]
anchor-lang = "{{anchor-version}}"
light-hasher = {version="{{light-hasher-version}}", features = ["solana"] }
light-macros = "{{light-macros-version}}"
light-sdk = "{{light-sdk-version}}"
light-sdk-macros = "{{light-sdk-macros-version}}"
light-utils = "{{light-utils-version}}"
light-verifier = "{{light-verifier-version}}"
light-hasher = { version = "{{light-hasher-version}}", features = ["solana"] }
light-sdk = { version = "{{light-sdk-version}}", features = [
"anchor",
"idl-build",
] }

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

[dev-dependencies]
light-client ="{{light-client-version}}"
light-test-utils = "{{light-test-utils-version}}"
solana-program-test = "{{solana-program-test-version}}"
light-client = "{{light-client-version}}"
light-program-test = "{{light-program-test-version}}"
tokio = "{{tokio-version}}"
solana-sdk = "{{solana-sdk-version}}"
serial_test = "3.2.0"
186 changes: 119 additions & 67 deletions programs/{{rust-name}}/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,103 +1,155 @@
use anchor_lang::prelude::*;
use light_sdk::{
compressed_account::LightAccount, light_account, light_accounts, light_program,
merkle_context::PackedAddressMerkleContext,
account::LightAccount,
address::v1::derive_address,
cpi::{CpiAccounts, CpiInputs},
instruction::{
account_meta::CompressedAccountMeta, merkle_context::PackedAddressMerkleContext,
},
LightDiscriminator, LightHasher, NewAddressParamsPacked, ValidityProof,
};

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

#[light_program]
#[program]
pub mod {{rust-name-snake-case}} {

use super::*;

pub fn create<'info>(
ctx: LightContext<'_, '_, '_, 'info, Create<'info>>,
ctx: Context<'_, '_, '_, 'info, GenericAnchorAccounts<'info>>,
proof: ValidityProof,
address_merkle_context: PackedAddressMerkleContext,
output_merkle_tree_index: u8,
) -> Result<()> {
ctx.light_accounts.counter.owner = ctx.accounts.signer.key();
ctx.light_accounts.counter.counter = 0;
let program_id = crate::ID.into();
let light_cpi_accounts = CpiAccounts::new(
ctx.accounts.signer.as_ref(),
ctx.remaining_accounts,
crate::ID,
)
.map_err(ProgramError::from)?;

let (address, address_seed) = derive_address(
&[b"counter", ctx.accounts.signer.key().as_ref()],
&light_cpi_accounts.tree_accounts()
[address_merkle_context.address_merkle_tree_pubkey_index as usize]
.key(),
&crate::ID,
);

let new_address_params = NewAddressParamsPacked {
seed: address_seed,
address_queue_account_index: address_merkle_context.address_queue_pubkey_index,
address_merkle_tree_root_index: address_merkle_context.root_index,
address_merkle_tree_account_index: address_merkle_context
.address_merkle_tree_pubkey_index,
};

let mut counter = LightAccount::<'_, CounterCompressedAccount>::new_init(
&program_id,
Some(address),
output_merkle_tree_index,
);

counter.owner = ctx.accounts.signer.key();

let cpi = CpiInputs::new_with_address(
proof,
vec![counter.to_account_info().map_err(ProgramError::from)?],
vec![new_address_params],
);
cpi.invoke_light_system_program(light_cpi_accounts)
.map_err(ProgramError::from)?;

Ok(())
}

pub fn increment<'info>(
ctx: LightContext<'_, '_, '_, 'info, Increment<'info>>,
ctx: Context<'_, '_, '_, 'info, GenericAnchorAccounts<'info>>,
proof: ValidityProof,
counter_value: u64,
account_meta: CompressedAccountMeta,
) -> Result<()> {
ctx.light_accounts.counter.counter += 1;
let program_id = crate::ID.into();
let mut counter = LightAccount::<'_, CounterCompressedAccount>::new_mut(
&program_id,
&account_meta,
CounterCompressedAccount {
owner: ctx.accounts.signer.key(),
counter: counter_value,
},
)
.map_err(ProgramError::from)?;

counter.counter += 1;

let light_cpi_accounts = CpiAccounts::new(
ctx.accounts.signer.as_ref(),
ctx.remaining_accounts,
crate::ID,
)
.map_err(ProgramError::from)?;

let cpi = CpiInputs::new(
proof,
vec![counter.to_account_info().map_err(ProgramError::from)?],
);

cpi.invoke_light_system_program(light_cpi_accounts)
.map_err(ProgramError::from)?;

Ok(())
}

pub fn delete<'info>(
ctx: LightContext<'_, '_, '_, 'info, Delete<'info>>,
ctx: Context<'_, '_, '_, 'info, GenericAnchorAccounts<'info>>,
proof: ValidityProof,
counter_value: u64,
account_meta: CompressedAccountMeta,
) -> Result<()> {
let program_id = crate::ID.into();

let counter = LightAccount::<'_, CounterCompressedAccount>::new_close(
&program_id,
&account_meta,
CounterCompressedAccount {
owner: ctx.accounts.signer.key(),
counter: counter_value,
},
)
.map_err(ProgramError::from)?;

let light_cpi_accounts = CpiAccounts::new(
ctx.accounts.signer.as_ref(),
ctx.remaining_accounts,
crate::ID,
)
.map_err(ProgramError::from)?;

let cpi = CpiInputs::new(
proof,
vec![counter.to_account_info().map_err(ProgramError::from)?],
);

cpi.invoke_light_system_program(light_cpi_accounts)
.map_err(ProgramError::from)?;

Ok(())
}
}


#[light_account]
#[derive(Clone, Debug, Default)]
#[derive(
Clone, Debug, Default, AnchorDeserialize, AnchorSerialize, LightDiscriminator, LightHasher,
)]
pub struct CounterCompressedAccount {
#[truncate]
#[hash]
pub owner: Pubkey,
pub counter: u64,
}

#[error_code]
pub enum CustomError {
#[msg("No authority to perform this action")]
Unauthorized,
}

#[light_accounts]
pub struct Create<'info> {
#[account(mut)]
#[fee_payer]
pub signer: Signer<'info>,
#[self_program]
pub self_program: Program<'info, crate::program::{{rust-name-camel-case}}>,
/// CHECK: Checked in light-system-program.
#[authority]
pub cpi_signer: AccountInfo<'info>,
#[light_account(init, seeds = [b"counter", signer.key().as_ref()])]
pub counter: LightAccount<CounterCompressedAccount>,
}

#[light_accounts]
pub struct Increment<'info> {
#[account(mut)]
#[fee_payer]
pub signer: Signer<'info>,
#[self_program]
pub self_program: Program<'info, crate::program::{{rust-name-camel-case}}>,
/// CHECK: Checked in light-system-program.
#[authority]
pub cpi_signer: AccountInfo<'info>,

#[light_account(
mut,
seeds = [b"counter", signer.key().as_ref()],
constraint = counter.owner == signer.key() @ CustomError::Unauthorized
)]
pub counter: LightAccount<CounterCompressedAccount>,
}

#[light_accounts]
pub struct Delete<'info> {
#[derive(Accounts)]
pub struct GenericAnchorAccounts<'info> {
#[account(mut)]
#[fee_payer]
pub signer: Signer<'info>,
#[self_program]
pub self_program: Program<'info, crate::program::{{rust-name-camel-case}}>,
/// CHECK: Checked in light-system-program.
#[authority]
pub cpi_signer: AccountInfo<'info>,

#[light_account(
close,
seeds = [b"counter", signer.key().as_ref()],
constraint = counter.owner == signer.key() @ CustomError::Unauthorized
)]
pub counter: LightAccount<CounterCompressedAccount>,
}
Loading