Skip to content

Commit c4bce19

Browse files
authored
Add built-in tokenaccount and mint validations (#6)
1 parent 699d2c0 commit c4bce19

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/validate.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
11
//! Validations for accounts.
22
33
use anchor_lang::prelude::*;
4+
use anchor_spl::token::{Mint, TokenAccount};
45

5-
/// Validator for [Accounts] structs.
6-
pub trait Validate<'info>: Accounts<'info> {
6+
use crate::assert_owner;
7+
8+
/// Validates the contents of a variable. Generally used for [Accounts] structs and struct members.
9+
pub trait Validate<'info> {
710
/// Validates the account struct.
811
fn validate(&self) -> ProgramResult;
912
}
13+
14+
/// Represents an account owned by a program.
15+
pub trait ProgramOwned: AccountDeserialize + Clone {
16+
/// Gets the expected owner of the program owned account.
17+
fn expected_owner(&self) -> &'static Pubkey;
18+
}
19+
20+
impl<'info, T: ProgramOwned> Validate<'info> for CpiAccount<'info, T> {
21+
fn validate(&self) -> ProgramResult {
22+
assert_owner!(*self, *self.expected_owner());
23+
Ok(())
24+
}
25+
}
26+
27+
impl ProgramOwned for TokenAccount {
28+
fn expected_owner(&self) -> &'static Pubkey {
29+
&crate::program_ids::token::ID
30+
}
31+
}
32+
33+
impl ProgramOwned for Mint {
34+
fn expected_owner(&self) -> &'static Pubkey {
35+
&crate::program_ids::token::ID
36+
}
37+
}

0 commit comments

Comments
 (0)