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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Account validators

use anchor_lang::prelude::*;
use vipers::validate::Validate;
use vipers::{assert_ata, assert_keys, invariant};

use crate::ClaimRewards;
use crate::WithdrawTokens;
use crate::{InitMergeMiner, QuarryStakePrimary};
use crate::{InitMiner, QuarryStake};
use crate::{NewPool, QuarryStakeReplica};
use anchor_lang::Key;

/// --------------------------------
/// Instruction account structs
/// --------------------------------

impl<'info> Validate<'info> for NewPool<'info> {
    fn validate(&self) -> ProgramResult {
        // replica_mint checks are now redundant
        // since it is now an associated mint
        assert_keys!(
            self.replica_mint.mint_authority.unwrap(),
            self.pool,
            "replica_mint.mint_authority"
        );
        invariant!(
            self.replica_mint.freeze_authority.is_none(),
            "cannot have freeze authority"
        );
        invariant!(
            self.replica_mint.decimals == self.primary_mint.decimals,
            "decimals mismatch"
        );
        invariant!(
            self.replica_mint.supply == 0,
            "replica must have zero supply"
        );

        Ok(())
    }
}

impl<'info> Validate<'info> for InitMergeMiner<'info> {
    fn validate(&self) -> ProgramResult {
        Ok(())
    }
}

impl<'info> Validate<'info> for InitMiner<'info> {
    fn validate(&self) -> ProgramResult {
        require!(
            self.quarry.token_mint_key == self.pool.primary_mint
                || self.quarry.token_mint_key == self.pool.replica_mint,
            InvalidMiner
        );

        assert_keys!(self.mm.pool, self.pool, "mm.pool");
        assert_keys!(
            self.quarry.rewarder_key,
            *self.rewarder,
            "quarry.rewarder_key"
        );
        assert_ata!(
            self.miner_vault,
            self.miner,
            self.quarry.token_mint_key,
            "miner_vault"
        );

        Ok(())
    }
}

impl<'info> Validate<'info> for WithdrawTokens<'info> {
    fn validate(&self) -> ProgramResult {
        let withdraw_mint = self.mm_token_account.mint;

        assert_keys!(self.withdraw_mint, withdraw_mint, "withdraw_mint");
        // cannot withdraw a replica mint.
        require!(
            self.withdraw_mint.key() != self.pool.replica_mint,
            CannotWithdrawReplicaMint
        );

        if withdraw_mint == self.pool.primary_mint {
            // should be no replica balance if withdrawing primary
            require!(self.mm.replica_balance == 0, OutstandingReplicaTokens);
        }

        assert_keys!(self.owner, self.mm.owner, "owner");
        assert_keys!(self.pool, self.mm.pool, "pool");

        assert_ata!(
            self.mm_token_account,
            self.mm,
            withdraw_mint,
            "mm_token_account"
        );
        assert_keys!(
            self.token_destination.mint,
            withdraw_mint,
            "token_destination.mint"
        );

        Ok(())
    }
}

impl<'info> Validate<'info> for ClaimRewards<'info> {
    fn validate(&self) -> ProgramResult {
        self.stake.validate()?;

        assert_keys!(
            self.minter.mint_wrapper,
            *self.mint_wrapper,
            "minter.mint_wrapper"
        );
        assert_keys!(
            self.minter.minter_authority,
            *self.stake.rewarder,
            "minter.minter_authority"
        );
        assert_keys!(
            *self.rewards_token_mint,
            self.mint_wrapper.token_mint,
            "mint_wrapper.token_mint"
        );
        assert_ata!(
            *self.rewards_token_account,
            self.stake.mm,
            *self.rewards_token_mint,
            "rewards_token_account"
        );
        assert_keys!(
            self.claim_fee_token_account.mint,
            *self.rewards_token_mint,
            "claim_fee_token_account.mint"
        );
        assert_keys!(
            self.stake_token_account.mint,
            self.stake.quarry.token_mint_key,
            "stake_token_account.mint"
        );

        Ok(())
    }
}

/// --------------------------------
/// Context Structs
/// --------------------------------

impl<'info> Validate<'info> for QuarryStakePrimary<'info> {
    fn validate(&self) -> ProgramResult {
        self.stake.validate()?;

        // For primary staking:
        // - quarry is a quarry, staking the `primary_mint`

        assert_keys!(self.mm_owner, self.stake.mm.owner, "mm_owner");
        assert_keys!(
            self.stake.quarry.token_mint_key,
            self.stake.pool.primary_mint,
            "stake.quarry.token_mint_key"
        );
        assert_ata!(
            self.mm_primary_token_account,
            self.stake.mm,
            self.stake.pool.primary_mint,
            "mm_primary_token_account"
        );

        Ok(())
    }
}

impl<'info> Validate<'info> for QuarryStakeReplica<'info> {
    fn validate(&self) -> ProgramResult {
        self.stake.validate()?;

        // For replica staking:
        // - rewarder is any rewarder
        // - quarry is a quarry staking the `replica_mint`
        assert_keys!(self.mm_owner, self.stake.mm.owner, "mm_owner");

        assert_keys!(
            self.stake.pool.replica_mint,
            self.replica_mint,
            "stake.pool.replica_mint"
        );
        assert_keys!(
            self.stake.quarry.token_mint_key,
            self.replica_mint,
            "stake.quarry.token_mint_key"
        );
        assert_ata!(
            self.replica_mint_token_account,
            self.stake.mm,
            self.replica_mint,
            "replica_mint_token_account"
        );

        Ok(())
    }
}

impl<'info> Validate<'info> for QuarryStake<'info> {
    fn validate(&self) -> ProgramResult {
        assert_keys!(self.mm.pool, self.pool, "mm.pool");

        assert_keys!(*self.rewarder, self.quarry.rewarder_key, "rewarder");
        assert_keys!(*self.quarry, self.miner.quarry_key, "quarry");
        assert_keys!(self.miner.authority, self.mm, "miner.authority");
        assert_keys!(self.miner_vault, self.miner.token_vault_key, "miner_vault");
        assert_ata!(
            self.miner_vault,
            *self.miner,
            self.quarry.token_mint_key,
            "miner_vault"
        );

        Ok(())
    }
}