|
| 1 | +use anchor_lang::prelude::*; |
| 2 | +use light_compressed_account::instruction_data::cpi_context::CompressedCpiContext; |
| 3 | +use light_compressed_token_sdk::{ |
| 4 | + account::CTokenAccount, |
| 5 | + instructions::transfer::instruction::{TransferConfig, TransferInputs}, |
| 6 | + TokenAccountMeta, |
| 7 | +}; |
| 8 | +use light_sdk::{ |
| 9 | + account::LightAccount, |
| 10 | + address::v1::derive_address, |
| 11 | + cpi::{CpiAccounts, CpiInputs}, |
| 12 | + instruction::{PackedAddressTreeInfo, ValidityProof}, |
| 13 | + LightDiscriminator, LightHasher, |
| 14 | +}; |
| 15 | + |
| 16 | +#[event] |
| 17 | +#[derive(Clone, Debug, Default, LightHasher, LightDiscriminator)] |
| 18 | +pub struct MyTokenCompressedAccount { |
| 19 | + pub amount: u64, |
| 20 | + #[hash] |
| 21 | + pub owner: Pubkey, |
| 22 | +} |
| 23 | + |
| 24 | +pub fn process_create_compressed_account<'info>( |
| 25 | + cpi_accounts: CpiAccounts, |
| 26 | + proof: ValidityProof, |
| 27 | + address_tree_info: PackedAddressTreeInfo, |
| 28 | + output_tree_index: u8, |
| 29 | + amount: u64, |
| 30 | +) -> Result<()> { |
| 31 | + let (address, address_seed) = derive_address( |
| 32 | + &[ |
| 33 | + b"deposit", |
| 34 | + cpi_accounts.fee_payer().key().to_bytes().as_ref(), |
| 35 | + ], |
| 36 | + &address_tree_info |
| 37 | + .get_tree_pubkey(&cpi_accounts) |
| 38 | + .map_err(|_| ErrorCode::AccountNotEnoughKeys)?, |
| 39 | + &crate::ID, |
| 40 | + ); |
| 41 | + let new_address_params = address_tree_info.into_new_address_params_packed(address_seed); |
| 42 | + |
| 43 | + let mut my_compressed_account = LightAccount::<'_, MyTokenCompressedAccount>::new_init( |
| 44 | + &crate::ID, |
| 45 | + Some(address), |
| 46 | + output_tree_index, |
| 47 | + ); |
| 48 | + |
| 49 | + my_compressed_account.amount = amount; |
| 50 | + my_compressed_account.owner = cpi_accounts.fee_payer().key(); |
| 51 | + |
| 52 | + let cpi_inputs = CpiInputs { |
| 53 | + proof, |
| 54 | + account_infos: Some(vec![my_compressed_account |
| 55 | + .to_account_info() |
| 56 | + .map_err(ProgramError::from)?]), |
| 57 | + new_addresses: Some(vec![new_address_params]), |
| 58 | + cpi_context: Some(CompressedCpiContext { |
| 59 | + set_context: false, |
| 60 | + first_set_context: false, |
| 61 | + cpi_context_account_index: (cpi_accounts.system_accounts_len() - 1) as u8, // TODO: confirm |
| 62 | + }), |
| 63 | + ..Default::default() |
| 64 | + }; |
| 65 | + |
| 66 | + cpi_inputs |
| 67 | + .invoke_light_system_program(cpi_accounts) |
| 68 | + .map_err(ProgramError::from)?; |
| 69 | + |
| 70 | + Ok(()) |
| 71 | +} |
| 72 | + |
| 73 | +pub fn deposit_tokens( |
| 74 | + cpi_accounts: &CpiAccounts, |
| 75 | + token_metas: Vec<TokenAccountMeta>, |
| 76 | + output_tree_index: u8, |
| 77 | + mint: Pubkey, |
| 78 | + recipient: Pubkey, |
| 79 | + amount: u64, |
| 80 | +) -> Result<()> { |
| 81 | + let sender_account = CTokenAccount::new( |
| 82 | + mint, |
| 83 | + *cpi_accounts.fee_payer().key, |
| 84 | + token_metas, |
| 85 | + output_tree_index, |
| 86 | + ); |
| 87 | + let transfer_inputs = TransferInputs { |
| 88 | + fee_payer: *cpi_accounts.fee_payer().key, |
| 89 | + sender_account, |
| 90 | + // No validity proof necessary we are just storing things in the cpi context. |
| 91 | + validity_proof: None.into(), |
| 92 | + recipient, |
| 93 | + tree_pubkeys: cpi_accounts.tree_pubkeys().unwrap(), |
| 94 | + config: Some(TransferConfig { |
| 95 | + cpi_context: Some(CompressedCpiContext { |
| 96 | + set_context: true, |
| 97 | + first_set_context: true, |
| 98 | + cpi_context_account_index: (cpi_accounts.system_accounts_len() - 1) as u8, |
| 99 | + }), |
| 100 | + ..Default::default() |
| 101 | + }), |
| 102 | + amount, |
| 103 | + }; |
| 104 | + let instruction = |
| 105 | + light_compressed_token_sdk::instructions::transfer::instruction::transfer(transfer_inputs) |
| 106 | + .unwrap(); |
| 107 | + // We can use the property that account infos don't have to be in order if you use |
| 108 | + // solana program invoke. |
| 109 | + let mut account_infos = cpi_accounts.account_infos().to_vec(); |
| 110 | + account_infos.push(cpi_accounts.fee_payer().clone()); |
| 111 | + anchor_lang::solana_program::program::invoke(&instruction, account_infos.as_slice())?; |
| 112 | + |
| 113 | + Ok(()) |
| 114 | +} |
| 115 | + |
| 116 | +// For pinocchio we will need to build the accounts in oder |
| 117 | +// The easiest is probably just pass the accounts multiple times since deserialization is zero copy. |
| 118 | +// pub struct TransferInstruction<'info> { |
| 119 | +// pub fee_payer: AccountInfo<'info>, |
| 120 | +// pub authority: AccountInfo<'info>, |
| 121 | +// pub cpi_authority_pda: AccountInfo<'info>, |
| 122 | +// pub light_system_program: AccountInfo<'info>, |
| 123 | +// pub registered_program_pda: AccountInfo<'info>, |
| 124 | +// /// CHECK: (account compression program) when emitting event. |
| 125 | +// pub noop_program: AccountInfo<'info>, |
| 126 | +// /// CHECK: (different program) is used to cpi account compression program from light system program. |
| 127 | +// pub account_compression_authority: AccountInfo<'info>, |
| 128 | +// pub account_compression_program: AccountInfo<'info>, |
| 129 | +// /// CHECK:(system program) used to derive cpi_authority_pda and check that |
| 130 | +// /// this program is the signer of the cpi. |
| 131 | +// pub self_program: AccountInfo<'info>, |
| 132 | +// pub token_pool_pda: Option<AccountInfo<'info>>, |
| 133 | +// pub compress_or_decompress_token_account: Option<InterfaceAccount<'info>>, |
| 134 | +// pub token_program: Option<AccountInfo<'info>>, |
| 135 | +// pub system_program: AccountInfo<'info>, |
| 136 | +// } |
0 commit comments