-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathaccess_control.rs
38 lines (36 loc) · 1.03 KB
/
access_control.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
use crate::error::ErrorCode;
use anchor_lang::prelude::*;
use anchor_spl::token::TokenAccount;
/// Checks whether the transaction time has not crossed the deadline
///
/// # Arguments
///
/// * `deadline` - The deadline specified by a user
///
pub fn check_deadline(deadline: i64) -> Result<()> {
require!(
Clock::get()?.unix_timestamp <= deadline,
ErrorCode::TransactionTooOld
);
Ok(())
}
/// Ensures that the signer is the owner or a delgated authority for the position NFT
///
/// # Arguments
///
/// * `signer` - The signer address
/// * `token_account` - The token account holding the position NFT
///
pub fn is_authorized_for_token<'info>(
signer: &Signer<'info>,
token_account: &Box<Account<'info, TokenAccount>>,
) -> Result<()> {
require!(
token_account.amount == 1
&& (token_account.owner == signer.key()
|| (token_account.delegate.contains(&signer.key())
&& token_account.delegated_amount > 0)),
ErrorCode::NotApproved
);
Ok(())
}