ore_pool_api/
loaders.rs

1use steel::*;
2
3use crate::state::{Member, Pool, Share};
4
5/// Errors if:
6/// - Owner is not pool program.
7/// - Data is empty.
8/// - Data cannot be parsed to a member account.
9/// - Member authority is not expected value.
10/// - Expected to be writable, but is not.
11pub fn load_member(
12    info: &AccountInfo<'_>,
13    authority: &Pubkey,
14    pool: &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 member_data = info.data.borrow();
26    let member = Member::try_from_bytes(&member_data)?;
27
28    if member.authority.ne(authority) {
29        return Err(ProgramError::InvalidAccountData);
30    }
31
32    if member.pool.ne(pool) {
33        return Err(ProgramError::InvalidAccountData);
34    }
35
36    if is_writable && !info.is_writable {
37        return Err(ProgramError::InvalidAccountData);
38    }
39
40    Ok(())
41}
42
43/// Errors if:
44/// - Owner is not pool program.
45/// - Data is empty.
46/// - Account discriminator does not match expected value.
47/// - Expected to be writable, but is not.
48pub fn load_any_member(
49    info: &AccountInfo<'_>,
50    pool: &Pubkey,
51    is_writable: bool,
52) -> Result<(), ProgramError> {
53    if info.owner.ne(&crate::id()) {
54        return Err(ProgramError::InvalidAccountOwner);
55    }
56
57    if info.data_is_empty() {
58        return Err(ProgramError::UninitializedAccount);
59    }
60
61    let member_data = info.data.borrow();
62    let member = Member::try_from_bytes(&member_data)?;
63
64    if member.pool.ne(pool) {
65        return Err(ProgramError::InvalidAccountData);
66    }
67
68    if is_writable && !info.is_writable {
69        return Err(ProgramError::InvalidAccountData);
70    }
71
72    Ok(())
73}
74
75/// Errors if:
76/// - Owner is not pool program.
77/// - Data is empty.
78/// - Data cannot be deserialized into a pool account.
79/// - Pool authority is not expected value.
80/// - Expected to be writable, but is not.
81pub fn load_pool(
82    info: &AccountInfo<'_>,
83    authority: &Pubkey,
84    is_writable: bool,
85) -> Result<(), ProgramError> {
86    if info.owner.ne(&crate::id()) {
87        return Err(ProgramError::InvalidAccountOwner);
88    }
89
90    if info.data_is_empty() {
91        return Err(ProgramError::UninitializedAccount);
92    }
93
94    let pool_data = info.data.borrow();
95    let pool = Pool::try_from_bytes(&pool_data)?;
96
97    if pool.authority.ne(authority) {
98        return Err(ProgramError::InvalidAccountData);
99    }
100
101    if is_writable && !info.is_writable {
102        return Err(ProgramError::InvalidAccountData);
103    }
104
105    Ok(())
106}
107
108/// Errors if:
109/// - Owner is not pool program.
110/// - Data is empty.
111/// - Account discriminator does not match expected value.
112/// - Expected to be writable, but is not.
113pub fn load_any_pool(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
114    if info.owner.ne(&crate::id()) {
115        return Err(ProgramError::InvalidAccountOwner);
116    }
117
118    if info.data_is_empty() {
119        return Err(ProgramError::UninitializedAccount);
120    }
121
122    if info.data.borrow()[0].ne(&(Pool::discriminator())) {
123        return Err(solana_program::program_error::ProgramError::InvalidAccountData);
124    }
125
126    if is_writable && !info.is_writable {
127        return Err(ProgramError::InvalidAccountData);
128    }
129
130    Ok(())
131}
132
133/// Errors if:
134/// - Owner is not pool program.
135/// - Data is empty.
136/// - Data cannot be deserialized into a share account.
137/// - Share authority is not expected value.
138/// - Share mint account is not the expected value.
139/// - Expected to be writable, but is not.
140pub fn load_share(
141    info: &AccountInfo<'_>,
142    authority: &Pubkey,
143    pool: &Pubkey,
144    mint: &Pubkey,
145    is_writable: bool,
146) -> Result<(), ProgramError> {
147    if info.owner.ne(&crate::id()) {
148        return Err(ProgramError::InvalidAccountOwner);
149    }
150
151    if info.data_is_empty() {
152        return Err(ProgramError::UninitializedAccount);
153    }
154
155    let share_data = info.data.borrow();
156    let share = Share::try_from_bytes(&share_data)?;
157
158    if share.authority.ne(authority) {
159        return Err(ProgramError::InvalidAccountData);
160    }
161
162    if share.pool.ne(pool) {
163        return Err(ProgramError::InvalidAccountData);
164    }
165
166    if share.mint.ne(mint) {
167        return Err(ProgramError::InvalidAccountData);
168    }
169
170    if is_writable && !info.is_writable {
171        return Err(ProgramError::InvalidAccountData);
172    }
173
174    Ok(())
175}