Skip to content

Commit 6a2dd12

Browse files
committed
update deposit works
1 parent 29e23ef commit 6a2dd12

File tree

2 files changed

+50
-53
lines changed

2 files changed

+50
-53
lines changed

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,10 @@ pub mod sdk_token_test {
178178
sol_pool_pda: false,
179179
sol_compression_recipient: false,
180180
};
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
181+
182+
let (_token_account_infos, system_account_infos) = ctx
190183
.remaining_accounts
191184
.split_at(system_accounts_start_offset as usize);
192-
msg!("token_account_infos: {:?}", token_account_infos);
193-
msg!("system_account_infos: {:?}", system_account_infos);
194185
// TODO: figure out why the offsets are wrong.
195186
// Could add with pre account infos Option<u8>
196187
let light_cpi_accounts = CpiAccounts::try_new_with_config(
@@ -199,13 +190,10 @@ pub mod sdk_token_test {
199190
config,
200191
)
201192
.unwrap();
202-
msg!(
203-
"light_cpi_accounts {:?}",
204-
light_cpi_accounts.authority().unwrap()
205-
);
193+
206194
let recipient = ctx.accounts.authority.key();
207195
deposit_additional_tokens(
208-
&light_cpi_accounts,
196+
light_cpi_accounts,
209197
depositing_token_metas,
210198
escrowed_token_meta,
211199
output_tree_index,
@@ -217,14 +205,11 @@ pub mod sdk_token_test {
217205
account_meta.address,
218206
ctx.remaining_accounts,
219207
ctx.accounts.authority.to_account_info(),
220-
)?;
221-
process_update_escrow_pda(
222-
light_cpi_accounts,
208+
existing_amount,
223209
account_meta,
224210
proof,
225-
existing_amount,
226-
deposit_amount,
227-
)
211+
)?;
212+
Ok(())
228213
}
229214
}
230215

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

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn process_update_escrow_pda(
6868
}
6969

7070
pub fn deposit_additional_tokens<'info>(
71-
cpi_accounts: &CpiAccounts<'_, 'info>,
71+
cpi_accounts: CpiAccounts<'_, 'info>,
7272
depositing_token_metas: Vec<TokenAccountMeta>,
7373
escrowed_token_meta: TokenAccountMeta,
7474
output_tree_index: u8,
@@ -80,15 +80,22 @@ pub fn deposit_additional_tokens<'info>(
8080
address: [u8; 32],
8181
remaining_accounts: &[AccountInfo<'info>],
8282
authority: AccountInfo<'info>,
83+
existing_amount: u64,
84+
account_meta: CompressedAccountMeta,
85+
proof: ValidityProof,
8386
) -> Result<()> {
84-
let tree_pubkeys = cpi_accounts.tree_pubkeys().unwrap();
85-
msg!("tree_pubkeys: {:?}", tree_pubkeys);
86-
msg!("output_tree_queue_index {:?}", output_tree_queue_index);
87-
msg!("output_tree_index {:?}", output_tree_index);
8887
// We want to keep only one escrow compressed token account
8988
// But ctoken transfers can only have one signer -> we cannot from 2 signers at the same time
9089
// 1. transfer depositing token to recipient pda -> escrow token account 2
91-
// 2. merge escrow token account 2 into escrow token account
90+
// 2. update escrow pda balance
91+
// 3. merge escrow token account 2 into escrow token account
92+
// Note:
93+
// - if the escrow pda only stores the amount and the owner we can omit the escrow pda.
94+
// - the escrowed token accounts are owned by a pda derived from the owner
95+
// that is sufficient to verify ownership.
96+
// - no escrow pda will simplify the transaction, for no cpi context account is required
97+
98+
// 1.transfer depositing token to recipient pda -> escrow token account 2
9299
let escrow_token_account_meta_2 = {
93100
let sender_account = CTokenAccount::new(
94101
mint,
@@ -104,9 +111,9 @@ pub fn deposit_additional_tokens<'info>(
104111
)
105112
.unwrap();
106113
// SAFETY: state trees are height 32
107-
let leaf_index = output_queue.batch_metadata.next_index as u32;
114+
let leaf_index = output_queue.batch_metadata.next_index as u32 + 1;
108115

109-
let new_input = TokenAccountMeta {
116+
let escrow_token_account_meta_2 = TokenAccountMeta {
110117
amount,
111118
delegate_index: None,
112119
lamports: None,
@@ -126,7 +133,6 @@ pub fn deposit_additional_tokens<'info>(
126133
.iter()
127134
.map(|x| x.pubkey())
128135
.collect::<Vec<Pubkey>>();
129-
msg!("tree_pubkeys {:?}", tree_pubkeys);
130136
let cpi_context_pubkey = *cpi_accounts.cpi_context().unwrap().key;
131137
let transfer_inputs = TransferInputs {
132138
fee_payer: *cpi_accounts.fee_payer().key,
@@ -166,41 +172,55 @@ pub fn deposit_additional_tokens<'info>(
166172
&[&seeds],
167173
)?;
168174
sol_log_compute_units();
169-
new_input
175+
escrow_token_account_meta_2
170176
};
177+
let tree_account_infos = cpi_accounts.tree_accounts().unwrap()[1..].to_vec();
178+
let fee_payer = cpi_accounts.fee_payer().clone();
179+
180+
// 2. Update escrow pda balance
181+
// - settle tx 1 in the same instruction with the cpi context account
182+
process_update_escrow_pda(cpi_accounts, account_meta, proof, existing_amount, amount)?;
171183

184+
// 3. Merge the newly escrowed tokens into the existing escrow account.
172185
{
173-
msg!("recipient {}", recipient);
174-
msg!("escrowed_token_meta {:?}", escrowed_token_meta);
186+
// We remove the cpi context account -> we decrement all packed account indices by 1.
187+
let mut output_tree_queue_index = output_tree_queue_index;
188+
output_tree_queue_index -= 1;
189+
let mut escrowed_token_meta = escrowed_token_meta;
190+
escrowed_token_meta
191+
.packed_tree_info
192+
.merkle_tree_pubkey_index -= 1;
193+
escrowed_token_meta.packed_tree_info.queue_pubkey_index -= 1;
194+
let mut escrow_token_account_meta_2 = escrow_token_account_meta_2;
195+
escrow_token_account_meta_2
196+
.packed_tree_info
197+
.merkle_tree_pubkey_index -= 1;
198+
escrow_token_account_meta_2
199+
.packed_tree_info
200+
.queue_pubkey_index -= 1;
175201
let escrow_account = CTokenAccount::new(
176202
mint,
177203
recipient,
178204
vec![escrowed_token_meta, escrow_token_account_meta_2],
179205
output_tree_queue_index,
180206
);
207+
181208
let total_escrowed_amount = escrow_account.amount;
182209

183-
let tree_account_infos = cpi_accounts.tree_accounts().unwrap();
184-
let tree_account_infos = &tree_account_infos[1..];
185210
let tree_pubkeys = tree_account_infos
186211
.iter()
187212
.map(|x| x.pubkey())
188213
.collect::<Vec<Pubkey>>();
189-
let cpi_context_pubkey = *cpi_accounts.cpi_context().unwrap().key;
190214
let transfer_inputs = TransferInputs {
191-
fee_payer: *cpi_accounts.fee_payer().key,
215+
fee_payer: *fee_payer.key,
192216
sender_account: escrow_account,
193217
// No validity proof necessary we are just storing state in the cpi context.
194218
validity_proof: None.into(),
195219
recipient,
196220
tree_pubkeys,
197221
config: Some(TransferConfig {
198-
cpi_context: Some(CompressedCpiContext {
199-
set_context: true,
200-
first_set_context: true,
201-
cpi_context_account_index: 0, // TODO: replace with Pubkey (maybe not because it is in tree pubkeys 1 in this case)
202-
}),
203-
cpi_context_pubkey: Some(cpi_context_pubkey),
222+
cpi_context: None,
223+
cpi_context_pubkey: None,
204224
..Default::default()
205225
}),
206226
amount: total_escrowed_amount,
@@ -211,16 +231,8 @@ pub fn deposit_additional_tokens<'info>(
211231
)
212232
.unwrap();
213233

214-
let account_infos = [
215-
&[cpi_accounts.fee_payer().clone(), authority][..],
216-
remaining_accounts,
217-
]
218-
.concat();
219-
sol_log_compute_units();
234+
let account_infos = [&[fee_payer, authority][..], remaining_accounts].concat();
220235

221-
sol_log_compute_units();
222-
msg!("invoke");
223-
sol_log_compute_units();
224236
let seeds = [&b"escrow"[..], &address, &[recipient_bump]];
225237
anchor_lang::solana_program::program::invoke_signed(
226238
&instruction,

0 commit comments

Comments
 (0)