ore_relayer_api/
loaders.rs

1use ore_utils::*;
2use solana_program::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};
3
4pub use crate::state::*;
5
6/// Errors if:
7/// - Owner is not relay program.
8/// - Data is empty.
9/// - Account cannot be parsed to a escrow account.
10/// - Escrow authority is not expected value.
11/// - Expected to be writable, but is not.
12pub fn load_escrow<'a, 'info>(
13    info: &'a AccountInfo<'info>,
14    authority: &Pubkey,
15    is_writable: bool,
16) -> Result<(), ProgramError> {
17    if info.owner.ne(&crate::id()) {
18        return Err(ProgramError::InvalidAccountOwner);
19    }
20
21    if info.data_is_empty() {
22        return Err(ProgramError::UninitializedAccount);
23    }
24
25    let escrow_data = info.data.borrow();
26    let escrow = Escrow::try_from_bytes(&escrow_data)?;
27
28    if escrow.authority.ne(&authority) {
29        return Err(ProgramError::InvalidAccountData);
30    }
31
32    if is_writable && !info.is_writable {
33        return Err(ProgramError::InvalidAccountData);
34    }
35
36    Ok(())
37}
38
39/// Errors if:
40/// - Owner is not relay program.
41/// - Data is empty.
42/// - Account cannot be parsed to a escrow account.
43/// - Expected to be writable, but is not.
44pub fn load_any_escrow<'a, 'info>(
45    info: &'a AccountInfo<'info>,
46    is_writable: bool,
47) -> Result<(), ProgramError> {
48    if info.owner.ne(&crate::id()) {
49        return Err(ProgramError::InvalidAccountOwner);
50    }
51
52    if info.data_is_empty() {
53        return Err(ProgramError::UninitializedAccount);
54    }
55
56    let escrow_data = info.data.borrow();
57    let _ = Escrow::try_from_bytes(&escrow_data)?;
58
59    if is_writable && !info.is_writable {
60        return Err(ProgramError::InvalidAccountData);
61    }
62
63    Ok(())
64}