1use solana_program::program_error::ProgramError;
2use solana_program::program::invoke;
3use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
4use steel::*;
5
6pub fn create_or_validate_wrapped_sol_ata<'a>(
8 ata_info: &AccountInfo<'a>,
9 owner_info: &AccountInfo<'a>,
10 mint_info: &AccountInfo<'a>,
11 payer_info: &AccountInfo<'a>,
12 system_program: &AccountInfo<'a>,
13 token_program: &AccountInfo<'a>,
14 ata_program: &AccountInfo<'a>,
15 _log_message: Option<&str>,
16) -> Result<(), ProgramError> {
17 ata_info.is_writable()?;
18
19 let is_empty = ata_info.data_is_empty();
23 let is_owned_by_token_program = !is_empty && ata_info.has_owner(token_program.key).is_ok();
25
26 if is_owned_by_token_program {
27 ata_info.as_associated_token_account(owner_info.key, mint_info.key).map(|_| ())
30 } else if is_empty {
31 let create_ix = create_associated_token_account_idempotent(
33 payer_info.key,
34 owner_info.key,
35 mint_info.key,
36 token_program.key,
37 );
38
39 invoke(
40 &create_ix,
41 &[
42 payer_info.clone(),
43 ata_info.clone(),
44 owner_info.clone(),
45 mint_info.clone(),
46 system_program.clone(),
47 token_program.clone(),
48 ata_program.clone(),
49 ],
50 )?;
51
52 ata_info.as_associated_token_account(owner_info.key, mint_info.key)?;
54 Ok(())
55 } else {
56 ata_info.as_associated_token_account(owner_info.key, mint_info.key).map(|_| ())
59 }
60}