1use solana_program::program_error::ProgramError;
2use solana_program::program::invoke;
3use solana_program::sysvar::clock::Clock;
4use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
5use steel::*;
6use crate::state::Config;
7
8#[macro_export]
14macro_rules! extract_accounts {
15 ($iter:expr, [$($var:ident),*]) => {
16 $(
17 let $var = $iter.next().unwrap();
18 )*
19 };
20}
21
22pub fn create_or_validate_wrapped_sol_ata<'a>(
24 ata_info: &AccountInfo<'a>,
25 owner_info: &AccountInfo<'a>,
26 mint_info: &AccountInfo<'a>,
27 payer_info: &AccountInfo<'a>,
28 system_program: &AccountInfo<'a>,
29 token_program: &AccountInfo<'a>,
30 ata_program: &AccountInfo<'a>,
31 _log_message: Option<&str>,
32) -> Result<(), ProgramError> {
33 ata_info.is_writable()?;
34
35 let is_empty = ata_info.data_is_empty();
39 let is_owned_by_token_program = !is_empty && ata_info.has_owner(token_program.key).is_ok();
41
42 if is_owned_by_token_program {
43 ata_info.as_associated_token_account(owner_info.key, mint_info.key).map(|_| ())
46 } else if is_empty {
47 let create_ix = create_associated_token_account_idempotent(
49 payer_info.key,
50 owner_info.key,
51 mint_info.key,
52 token_program.key,
53 );
54
55 invoke(
56 &create_ix,
57 &[
58 payer_info.clone(),
59 ata_info.clone(),
60 owner_info.clone(),
61 mint_info.clone(),
62 system_program.clone(),
63 token_program.clone(),
64 ata_program.clone(),
65 ],
66 )?;
67
68 ata_info.as_associated_token_account(owner_info.key, mint_info.key)?;
70 Ok(())
71 } else {
72 ata_info.as_associated_token_account(owner_info.key, mint_info.key).map(|_| ())
75 }
76}
77
78pub fn is_premine_active(config: &Config, clock: &Clock) -> bool {
86 config.tge_timestamp > 0 && clock.unix_timestamp < config.tge_timestamp
87}