-
Notifications
You must be signed in to change notification settings - Fork 110
/
util.rs
49 lines (43 loc) · 1.57 KB
/
util.rs
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
use anchor_lang::{
prelude::{AccountInfo, Pubkey, Signer, *},
ToAccountInfo,
};
use anchor_spl::token::TokenAccount;
use solana_program::program_option::COption;
use std::convert::TryFrom;
use crate::errors::ErrorCode;
pub fn verify_position_bundle_authority<'info>(
position_bundle_token_account: &TokenAccount,
position_bundle_authority: &Signer<'info>,
) -> Result<()> {
// use same logic
verify_position_authority(position_bundle_token_account, position_bundle_authority)
}
pub fn verify_position_authority<'info>(
position_token_account: &TokenAccount,
position_authority: &Signer<'info>,
) -> Result<()> {
// Check token authority using validate_owner method...
match position_token_account.delegate {
COption::Some(ref delegate) if position_authority.key == delegate => {
validate_owner(delegate, &position_authority.to_account_info())?;
if position_token_account.delegated_amount != 1 {
return Err(ErrorCode::InvalidPositionTokenAmount.into());
}
}
_ => validate_owner(
&position_token_account.owner,
&position_authority.to_account_info(),
)?,
};
Ok(())
}
fn validate_owner(expected_owner: &Pubkey, owner_account_info: &AccountInfo) -> Result<()> {
if expected_owner != owner_account_info.key || !owner_account_info.is_signer {
return Err(ErrorCode::MissingOrInvalidDelegate.into());
}
Ok(())
}
pub fn to_timestamp_u64(t: i64) -> Result<u64> {
u64::try_from(t).or(Err(ErrorCode::InvalidTimestampConversion.into()))
}