ore_pool_api/
loaders.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use steel::*;

use crate::state::{Member, Pool, Share};

/// Errors if:
/// - Owner is not pool program.
/// - Data is empty.
/// - Data cannot be parsed to a member account.
/// - Member authority is not expected value.
/// - Expected to be writable, but is not.
pub fn load_member(
    info: &AccountInfo<'_>,
    authority: &Pubkey,
    pool: &Pubkey,
    is_writable: bool,
) -> Result<(), ProgramError> {
    if info.owner.ne(&crate::id()) {
        return Err(ProgramError::InvalidAccountOwner);
    }

    if info.data_is_empty() {
        return Err(ProgramError::UninitializedAccount);
    }

    let member_data = info.data.borrow();
    let member = Member::try_from_bytes(&member_data)?;

    if member.authority.ne(authority) {
        return Err(ProgramError::InvalidAccountData);
    }

    if member.pool.ne(pool) {
        return Err(ProgramError::InvalidAccountData);
    }

    if is_writable && !info.is_writable {
        return Err(ProgramError::InvalidAccountData);
    }

    Ok(())
}

/// Errors if:
/// - Owner is not pool program.
/// - Data is empty.
/// - Account discriminator does not match expected value.
/// - Expected to be writable, but is not.
pub fn load_any_member(
    info: &AccountInfo<'_>,
    pool: &Pubkey,
    is_writable: bool,
) -> Result<(), ProgramError> {
    if info.owner.ne(&crate::id()) {
        return Err(ProgramError::InvalidAccountOwner);
    }

    if info.data_is_empty() {
        return Err(ProgramError::UninitializedAccount);
    }

    let member_data = info.data.borrow();
    let member = Member::try_from_bytes(&member_data)?;

    if member.pool.ne(pool) {
        return Err(ProgramError::InvalidAccountData);
    }

    if is_writable && !info.is_writable {
        return Err(ProgramError::InvalidAccountData);
    }

    Ok(())
}

/// Errors if:
/// - Owner is not pool program.
/// - Data is empty.
/// - Data cannot be deserialized into a pool account.
/// - Pool authority is not expected value.
/// - Expected to be writable, but is not.
pub fn load_pool(
    info: &AccountInfo<'_>,
    authority: &Pubkey,
    is_writable: bool,
) -> Result<(), ProgramError> {
    if info.owner.ne(&crate::id()) {
        return Err(ProgramError::InvalidAccountOwner);
    }

    if info.data_is_empty() {
        return Err(ProgramError::UninitializedAccount);
    }

    let pool_data = info.data.borrow();
    let pool = Pool::try_from_bytes(&pool_data)?;

    if pool.authority.ne(authority) {
        return Err(ProgramError::InvalidAccountData);
    }

    if is_writable && !info.is_writable {
        return Err(ProgramError::InvalidAccountData);
    }

    Ok(())
}

/// Errors if:
/// - Owner is not pool program.
/// - Data is empty.
/// - Account discriminator does not match expected value.
/// - Expected to be writable, but is not.
pub fn load_any_pool(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
    if info.owner.ne(&crate::id()) {
        return Err(ProgramError::InvalidAccountOwner);
    }

    if info.data_is_empty() {
        return Err(ProgramError::UninitializedAccount);
    }

    if info.data.borrow()[0].ne(&(Pool::discriminator())) {
        return Err(solana_program::program_error::ProgramError::InvalidAccountData);
    }

    if is_writable && !info.is_writable {
        return Err(ProgramError::InvalidAccountData);
    }

    Ok(())
}

/// Errors if:
/// - Owner is not pool program.
/// - Data is empty.
/// - Data cannot be deserialized into a share account.
/// - Share authority is not expected value.
/// - Share mint account is not the expected value.
/// - Expected to be writable, but is not.
pub fn load_share(
    info: &AccountInfo<'_>,
    authority: &Pubkey,
    pool: &Pubkey,
    mint: &Pubkey,
    is_writable: bool,
) -> Result<(), ProgramError> {
    if info.owner.ne(&crate::id()) {
        return Err(ProgramError::InvalidAccountOwner);
    }

    if info.data_is_empty() {
        return Err(ProgramError::UninitializedAccount);
    }

    let share_data = info.data.borrow();
    let share = Share::try_from_bytes(&share_data)?;

    if share.authority.ne(authority) {
        return Err(ProgramError::InvalidAccountData);
    }

    if share.pool.ne(pool) {
        return Err(ProgramError::InvalidAccountData);
    }

    if share.mint.ne(mint) {
        return Err(ProgramError::InvalidAccountData);
    }

    if is_writable && !info.is_writable {
        return Err(ProgramError::InvalidAccountData);
    }

    Ok(())
}