Skip to content

Commit 29e23ef

Browse files
committed
stash
1 parent da70d1c commit 29e23ef

File tree

16 files changed

+642
-61
lines changed

16 files changed

+642
-61
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

program-libs/account-checks/src/account_info/account_info_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::error::AccountError;
44

55
/// Trait to abstract over different AccountInfo implementations (pinocchio vs solana)
66
pub trait AccountInfoTrait {
7-
type Pubkey: Copy + Clone;
7+
type Pubkey: Copy + Clone + std::fmt::Debug;
88
type DataRef<'a>: Deref<Target = [u8]>
99
where
1010
Self: 'a;

program-tests/create-address-test-program/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ pub mod system_cpi_test {
7777
} else {
7878
use light_sdk::cpi::CpiAccounts;
7979
let cpi_accounts =
80-
CpiAccounts::new_with_config(&fee_payer, ctx.remaining_accounts, config);
80+
CpiAccounts::try_new_with_config(&fee_payer, ctx.remaining_accounts, config)
81+
.unwrap();
8182
let account_infos = cpi_accounts.to_account_infos();
8283

8384
let account_metas =

program-tests/sdk-token-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ light-sdk = { workspace = true }
2626
light-sdk-types = { workspace = true }
2727
light-compressed-account = { workspace = true }
2828
arrayvec = { workspace = true }
29+
light-batched-merkle-tree = { workspace = true }
2930

3031
[dev-dependencies]
3132
light-program-test = { workspace = true, features = ["devenv"] }

program-tests/sdk-token-test/src/lib.rs

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ mod process_compress_tokens;
1010
mod process_create_compressed_account;
1111
mod process_decompress_tokens;
1212
mod process_transfer_tokens;
13+
mod process_update_depost;
1314

15+
use light_sdk::{cpi::CpiAccounts, instruction::account_meta::CompressedAccountMeta};
1416
use process_batch_compress_tokens::process_batch_compress_tokens;
1517
use process_compress_tokens::process_compress_tokens;
1618
use process_create_compressed_account::process_create_compressed_account;
@@ -26,10 +28,14 @@ pub const LIGHT_CPI_SIGNER: CpiSigner =
2628

2729
#[program]
2830
pub mod sdk_token_test {
29-
use light_sdk::cpi::CpiAccounts;
31+
use anchor_lang::solana_program::pubkey;
32+
use light_sdk::address::v1::derive_address;
3033
use light_sdk_types::CpiAccountsConfig;
3134

32-
use crate::process_create_compressed_account::deposit_tokens;
35+
use crate::{
36+
process_create_compressed_account::deposit_tokens,
37+
process_update_depost::{deposit_additional_tokens, process_update_escrow_pda},
38+
};
3339

3440
use super::*;
3541

@@ -88,8 +94,8 @@ pub mod sdk_token_test {
8894
deposit_amount: u64,
8995
token_metas: Vec<TokenAccountMeta>,
9096
mint: Pubkey,
91-
recipient: Pubkey,
9297
system_accounts_start_offset: u8,
98+
recipient_bump: u8,
9399
) -> Result<()> {
94100
// It makes sense to parse accounts once.
95101
let config = CpiAccountsConfig {
@@ -100,30 +106,124 @@ pub mod sdk_token_test {
100106
sol_pool_pda: false,
101107
sol_compression_recipient: false,
102108
};
103-
let (token_account_infos, system_account_infos) = ctx
109+
let (_, system_account_infos) = ctx
104110
.remaining_accounts
105111
.split_at(system_accounts_start_offset as usize);
106112
// Could add with pre account infos Option<u8>
107-
let light_cpi_accounts = CpiAccounts::new_with_config(
113+
let light_cpi_accounts = CpiAccounts::try_new_with_config(
108114
ctx.accounts.signer.as_ref(),
109115
system_account_infos,
110116
config,
117+
)
118+
.unwrap();
119+
let (address, address_seed) = derive_address(
120+
&[
121+
b"escrow",
122+
light_cpi_accounts.fee_payer().key.to_bytes().as_ref(),
123+
],
124+
&address_tree_info
125+
.get_tree_pubkey(&light_cpi_accounts)
126+
.map_err(|_| ErrorCode::AccountNotEnoughKeys)?,
127+
&crate::ID,
111128
);
129+
msg!("seeds: {:?}", b"escrow");
130+
msg!("seeds: {:?}", address);
131+
msg!("recipient_bump: {:?}", recipient_bump);
132+
let recipient = Pubkey::create_program_address(
133+
&[b"escrow", &address, &[recipient_bump]],
134+
ctx.program_id,
135+
)
136+
.unwrap();
112137
deposit_tokens(
113138
&light_cpi_accounts,
114139
token_metas,
115140
output_tree_index,
116141
mint,
117142
recipient,
118143
deposit_amount,
119-
token_account_infos,
144+
ctx.remaining_accounts,
120145
)?;
146+
let new_address_params = address_tree_info.into_new_address_params_packed(address_seed);
147+
121148
process_create_compressed_account(
122149
light_cpi_accounts,
123150
proof,
124-
address_tree_info,
125151
output_tree_index,
126152
deposit_amount,
153+
address,
154+
new_address_params,
155+
)
156+
}
157+
158+
pub fn update_deposit<'info>(
159+
ctx: Context<'_, '_, '_, 'info, GenericWithAuthority<'info>>,
160+
proof: LightValidityProof,
161+
output_tree_index: u8,
162+
output_tree_queue_index: u8,
163+
deposit_amount: u64,
164+
depositing_token_metas: Vec<TokenAccountMeta>,
165+
mint: Pubkey,
166+
escrowed_token_meta: TokenAccountMeta,
167+
system_accounts_start_offset: u8,
168+
account_meta: CompressedAccountMeta,
169+
existing_amount: u64,
170+
recipient_bump: u8,
171+
) -> Result<()> {
172+
// It makes sense to parse accounts once.
173+
let config = CpiAccountsConfig {
174+
cpi_signer: crate::LIGHT_CPI_SIGNER,
175+
// TODO: add sanity check that account is a cpi context account.
176+
cpi_context: true,
177+
// TODO: add sanity check that account is a sol_pool_pda account.
178+
sol_pool_pda: false,
179+
sol_compression_recipient: false,
180+
};
181+
msg!(
182+
"crate::LIGHT_CPI_SIGNER, {:?}",
183+
anchor_lang::prelude::Pubkey::new_from_array(crate::LIGHT_CPI_SIGNER.cpi_signer)
184+
);
185+
msg!(
186+
"system_accounts_start_offset {}",
187+
system_accounts_start_offset
188+
);
189+
let (token_account_infos, system_account_infos) = ctx
190+
.remaining_accounts
191+
.split_at(system_accounts_start_offset as usize);
192+
msg!("token_account_infos: {:?}", token_account_infos);
193+
msg!("system_account_infos: {:?}", system_account_infos);
194+
// TODO: figure out why the offsets are wrong.
195+
// Could add with pre account infos Option<u8>
196+
let light_cpi_accounts = CpiAccounts::try_new_with_config(
197+
ctx.accounts.signer.as_ref(),
198+
system_account_infos,
199+
config,
200+
)
201+
.unwrap();
202+
msg!(
203+
"light_cpi_accounts {:?}",
204+
light_cpi_accounts.authority().unwrap()
205+
);
206+
let recipient = ctx.accounts.authority.key();
207+
deposit_additional_tokens(
208+
&light_cpi_accounts,
209+
depositing_token_metas,
210+
escrowed_token_meta,
211+
output_tree_index,
212+
output_tree_queue_index,
213+
mint,
214+
recipient,
215+
recipient_bump,
216+
deposit_amount,
217+
account_meta.address,
218+
ctx.remaining_accounts,
219+
ctx.accounts.authority.to_account_info(),
220+
)?;
221+
process_update_escrow_pda(
222+
light_cpi_accounts,
223+
account_meta,
224+
proof,
225+
existing_amount,
226+
deposit_amount,
127227
)
128228
}
129229
}
@@ -134,3 +234,11 @@ pub struct Generic<'info> {
134234
#[account(mut)]
135235
pub signer: Signer<'info>,
136236
}
237+
238+
#[derive(Accounts)]
239+
pub struct GenericWithAuthority<'info> {
240+
// fee payer and authority are the same
241+
#[account(mut)]
242+
pub signer: Signer<'info>,
243+
pub authority: AccountInfo<'info>,
244+
}

program-tests/sdk-token-test/src/process_create_compressed_account.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use light_compressed_token_sdk::{
1111
};
1212
use light_sdk::{
1313
account::LightAccount,
14-
address::v1::derive_address,
14+
address::{v1::derive_address, NewAddressParams},
1515
cpi::{CpiAccounts, CpiInputs},
1616
instruction::{PackedAddressTreeInfo, ValidityProof},
1717
light_account_checks::AccountInfoTrait,
@@ -29,19 +29,11 @@ pub struct MyTokenCompressedAccount {
2929
pub fn process_create_compressed_account(
3030
cpi_accounts: CpiAccounts,
3131
proof: ValidityProof,
32-
address_tree_info: PackedAddressTreeInfo,
3332
output_tree_index: u8,
3433
amount: u64,
34+
address: [u8; 32],
35+
new_address_params: light_sdk::address::PackedNewAddressParams,
3536
) -> Result<()> {
36-
let (address, address_seed) = derive_address(
37-
&[b"deposit", cpi_accounts.fee_payer().key.to_bytes().as_ref()],
38-
&address_tree_info
39-
.get_tree_pubkey(&cpi_accounts)
40-
.map_err(|_| ErrorCode::AccountNotEnoughKeys)?,
41-
&crate::ID,
42-
);
43-
let new_address_params = address_tree_info.into_new_address_params_packed(address_seed);
44-
4537
let mut my_compressed_account = LightAccount::<'_, MyTokenCompressedAccount>::new_init(
4638
&crate::ID,
4739
Some(address),
@@ -82,7 +74,7 @@ pub fn deposit_tokens<'info>(
8274
mint: Pubkey,
8375
recipient: Pubkey,
8476
amount: u64,
85-
token_account_infos: &[AccountInfo<'info>],
77+
remaining_accounts: &[AccountInfo<'info>],
8678
) -> Result<()> {
8779
let sender_account = CTokenAccount::new(
8880
mint,
@@ -94,14 +86,14 @@ pub fn deposit_tokens<'info>(
9486
// We need to be careful what accounts we pass.
9587
// Big accounts cost many CU.
9688
// TODO: replace
97-
let tree_account_infos =
98-
filter_packed_accounts(&[&sender_account], cpi_accounts.tree_accounts().unwrap());
89+
let tree_account_infos = cpi_accounts.tree_accounts().unwrap();
90+
let tree_account_len = tree_account_infos.len();
91+
// skip cpi context account and omit the address tree and queue accounts.
92+
let tree_account_infos = &tree_account_infos[1..tree_account_len - 2];
9993
let tree_pubkeys = tree_account_infos
10094
.iter()
10195
.map(|x| x.pubkey())
10296
.collect::<Vec<Pubkey>>();
103-
// msg!("tree_pubkeys {:?}", tree_pubkeys);
104-
let cpi_context = cpi_accounts.cpi_context().unwrap();
10597
let cpi_context_pubkey = *cpi_accounts.cpi_context().unwrap().key;
10698
// msg!("cpi_context_pubkey {:?}", cpi_context_pubkey);
10799
let transfer_inputs = TransferInputs {
@@ -133,16 +125,22 @@ pub fn deposit_tokens<'info>(
133125
msg!("create_account_infos");
134126
sol_log_compute_units();
135127
// TODO: initialize from CpiAccounts, use with_compressed_pda() offchain.
136-
let account_infos: TransferAccountInfos<'_, 'info, MAX_ACCOUNT_INFOS> = TransferAccountInfos {
137-
fee_payer: cpi_accounts.fee_payer(),
138-
authority: cpi_accounts.fee_payer(),
139-
packed_accounts: tree_account_infos.as_slice(),
140-
ctoken_accounts: token_account_infos,
141-
cpi_context: Some(cpi_context),
142-
};
143-
let account_infos = account_infos.into_account_infos();
128+
// let account_infos: TransferAccountInfos<'_, 'info, MAX_ACCOUNT_INFOS> = TransferAccountInfos {
129+
// fee_payer: cpi_accounts.fee_payer(),
130+
// authority: cpi_accounts.fee_payer(),
131+
// packed_accounts: tree_account_infos.as_slice(),
132+
// ctoken_accounts: token_account_infos,
133+
// cpi_context: Some(cpi_context),
134+
// };
135+
// let account_infos = account_infos.into_account_infos();
136+
// We can remove the address Merkle tree accounts.
137+
let len = remaining_accounts.len() - 2;
144138
// into_account_infos_checked() can be used for debugging but doubles CU cost to 1.5k CU
145-
139+
let account_infos = [
140+
&[cpi_accounts.fee_payer().clone()][..],
141+
&remaining_accounts[..len],
142+
]
143+
.concat();
146144
sol_log_compute_units();
147145

148146
sol_log_compute_units();

0 commit comments

Comments
 (0)