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
use crate::*;
pub fn handler(ctx: Context<ClaimRewards>) -> Result<()> {
let miner = &mut ctx.accounts.claim.miner;
let now = Clock::get()?.unix_timestamp;
let quarry = &mut ctx.accounts.claim.quarry;
quarry.update_rewards_and_miner(miner, &ctx.accounts.claim.rewarder, now)?;
ctx.accounts.calculate_and_claim_rewards()?;
Ok(())
}
impl<'info> ClaimRewards<'info> {
pub fn calculate_and_claim_rewards(&mut self) -> Result<()> {
let miner = &mut self.claim.miner;
let amount_claimable = miner.rewards_earned;
if amount_claimable == 0 {
return Ok(());
}
let max_claim_fee_millibps = self.claim.rewarder.max_claim_fee_millibps;
invariant!(
max_claim_fee_millibps < MAX_BPS * DEFAULT_CLAIM_FEE_MILLIBPS,
InvalidMaxClaimFee
);
let max_claim_fee = unwrap_int!(::u128::mul_div_u64(
amount_claimable,
max_claim_fee_millibps,
MAX_BPS * DEFAULT_CLAIM_FEE_MILLIBPS
));
let amount_claimable_minus_fees = unwrap_int!(amount_claimable.checked_sub(max_claim_fee));
miner.rewards_earned = 0;
self.mint_claimed_tokens(amount_claimable_minus_fees)?;
self.mint_fees(max_claim_fee)?;
let now = Clock::get()?.unix_timestamp;
emit!(ClaimEvent {
authority: self.claim.authority.key(),
staked_token: self.claim.quarry.token_mint_key,
timestamp: now,
rewards_token: self.rewards_token_mint.key(),
amount: amount_claimable_minus_fees,
fees: max_claim_fee,
});
Ok(())
}
fn mint_claimed_tokens(&self, amount_claimable_minus_fees: u64) -> Result<()> {
self.perform_mint(&self.rewards_token_account, amount_claimable_minus_fees)
}
fn mint_fees(&self, claim_fee: u64) -> Result<()> {
self.perform_mint(&self.claim_fee_token_account, claim_fee)
}
fn create_perform_mint_accounts(
&self,
destination: &Account<'info, TokenAccount>,
) -> quarry_mint_wrapper::cpi::accounts::PerformMint<'info> {
quarry_mint_wrapper::cpi::accounts::PerformMint {
mint_wrapper: self.mint_wrapper.to_account_info(),
minter_authority: self.claim.rewarder.to_account_info(),
token_mint: self.rewards_token_mint.to_account_info(),
destination: destination.to_account_info(),
minter: self.minter.to_account_info(),
token_program: self.claim.token_program.to_account_info(),
}
}
fn perform_mint(&self, destination: &Account<'info, TokenAccount>, amount: u64) -> Result<()> {
let claim_mint_accounts = self.create_perform_mint_accounts(destination);
let seeds = gen_rewarder_signer_seeds!(self.claim.rewarder);
let signer_seeds = &[&seeds[..]];
quarry_mint_wrapper::cpi::perform_mint(
CpiContext::new_with_signer(
self.mint_wrapper_program.to_account_info(),
claim_mint_accounts,
signer_seeds,
),
amount,
)
}
}
#[derive(Accounts)]
pub struct ClaimRewards<'info> {
#[account(mut)]
pub mint_wrapper: Box<Account<'info, quarry_mint_wrapper::MintWrapper>>,
pub mint_wrapper_program: Program<'info, quarry_mint_wrapper::program::QuarryMintWrapper>,
#[account(mut)]
pub minter: Box<Account<'info, quarry_mint_wrapper::Minter>>,
#[account(mut)]
pub rewards_token_mint: Box<Account<'info, Mint>>,
#[account(mut)]
pub rewards_token_account: Box<Account<'info, TokenAccount>>,
#[account(mut)]
pub claim_fee_token_account: Box<Account<'info, TokenAccount>>,
pub claim: UserClaim<'info>,
}
#[derive(Accounts, Clone)]
pub struct UserClaim<'info> {
pub authority: Signer<'info>,
#[account(mut)]
pub miner: Account<'info, Miner>,
#[account(mut)]
pub quarry: Account<'info, Quarry>,
pub unused_miner_vault: UncheckedAccount<'info>,
pub unused_token_account: UncheckedAccount<'info>,
pub token_program: Program<'info, Token>,
pub rewarder: Account<'info, Rewarder>,
}
impl<'info> Validate<'info> for ClaimRewards<'info> {
fn validate(&self) -> Result<()> {
self.claim.validate()?;
self.claim.rewarder.assert_not_paused()?;
assert_keys_eq!(self.mint_wrapper, self.claim.rewarder.mint_wrapper);
assert_keys_eq!(self.mint_wrapper.token_mint, self.rewards_token_mint);
assert_keys_eq!(self.minter.mint_wrapper, self.mint_wrapper);
assert_keys_eq!(self.minter.minter_authority, self.claim.rewarder);
assert_keys_eq!(
self.rewards_token_mint,
self.claim.rewarder.rewards_token_mint
);
assert_keys_eq!(
self.rewards_token_mint.mint_authority.unwrap(),
self.mint_wrapper
);
assert_keys_eq!(self.rewards_token_account.mint, self.rewards_token_mint);
assert_keys_eq!(
self.claim_fee_token_account,
self.claim.rewarder.claim_fee_token_account
);
assert_keys_eq!(self.claim_fee_token_account.mint, self.rewards_token_mint);
Ok(())
}
}
impl<'info> Validate<'info> for UserClaim<'info> {
fn validate(&self) -> Result<()> {
invariant!(!self.rewarder.is_paused, Paused);
invariant!(self.authority.is_signer, Unauthorized);
assert_keys_eq!(self.authority, self.miner.authority);
assert_keys_eq!(self.miner.quarry, self.quarry);
assert_keys_eq!(self.quarry.rewarder, self.rewarder);
Ok(())
}
}