oil_api/utils.rs
1use solana_program::program_error::ProgramError;
2use steel::*;
3
4/// Creates a wrapped SOL ATA if it doesn't exist, otherwise validates it.
5/// This is a helper function to avoid code duplication in deploy and place_bid.
6///
7/// # Arguments
8/// * `ata_info` - The ATA account info (must be writable)
9/// * `owner_info` - The owner account info (PDA or regular account that owns the ATA)
10/// * `mint_info` - The SOL_MINT account info
11/// * `payer_info` - The account that will pay for ATA creation
12/// * `system_program` - System program account info
13/// * `token_program` - Token program account info
14/// * `ata_program` - Associated token program account info
15/// * `log_message` - Optional log message to emit when ATA is created
16///
17/// # Returns
18/// * `Ok(())` if ATA exists or was created successfully
19/// * `Err(ProgramError)` if validation fails
20pub fn create_or_validate_wrapped_sol_ata<'a>(
21 ata_info: &AccountInfo<'a>,
22 owner_info: &AccountInfo<'a>,
23 mint_info: &AccountInfo<'a>,
24 payer_info: &AccountInfo<'a>,
25 system_program: &AccountInfo<'a>,
26 token_program: &AccountInfo<'a>,
27 ata_program: &AccountInfo<'a>,
28 log_message: Option<&str>,
29) -> Result<(), ProgramError> {
30 ata_info.is_writable()?;
31
32 if ata_info.data_is_empty() {
33 // Create the wrapped SOL ATA if it doesn't exist
34 create_associated_token_account(
35 payer_info,
36 owner_info,
37 ata_info,
38 mint_info,
39 system_program,
40 token_program,
41 ata_program,
42 )?;
43
44 if let Some(msg) = log_message {
45 solana_program::log::sol_log(msg);
46 }
47 } else {
48 // Validate existing ATA
49 ata_info.as_associated_token_account(owner_info.key, mint_info.key)?;
50 }
51
52 Ok(())
53}