1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
pubkey::Pubkey,
};
use crate::error::{AoError, AoResult};
#[cfg(not(debug_assertions))]
#[inline(always)]
unsafe fn invariant(check: bool) {
if check {
std::hint::unreachable_unchecked();
}
}
pub fn check_account_key(account: &AccountInfo, key: &Pubkey) -> ProgramResult {
if account.key != key {
return Err(ProgramError::InvalidArgument);
}
Ok(())
}
pub fn check_account_owner(account: &AccountInfo, owner: &Pubkey) -> ProgramResult {
if account.owner != owner {
return Err(ProgramError::InvalidArgument);
}
Ok(())
}
pub fn check_signer(account: &AccountInfo) -> ProgramResult {
if !(account.is_signer) {
return Err(ProgramError::MissingRequiredSignature);
}
Ok(())
}
pub fn check_unitialized(account: &AccountInfo) -> AoResult {
if account.data.borrow()[0] != 0 {
return Err(AoError::AlreadyInitialized);
}
Ok(())
}
pub(crate) fn fp32_div(a: u64, b_fp32: u64) -> u64 {
(((a as u128) << 32) / (b_fp32 as u128)) as u64
}
pub(crate) fn fp32_mul(a: u64, b_fp32: u64) -> u64 {
(((a as u128) * (b_fp32 as u128)) >> 32) as u64
}