quarry_mine/instructions/
create_quarry_v2.rs

1use crate::*;
2
3pub fn handler(ctx: Context<CreateQuarryV2>) -> Result<()> {
4    let rewarder = &mut ctx.accounts.auth.rewarder;
5    // Update rewarder's quarry stats
6    let index = rewarder.num_quarries;
7    rewarder.num_quarries = unwrap_int!(rewarder.num_quarries.checked_add(1));
8
9    let quarry = &mut ctx.accounts.quarry;
10    quarry.bump = unwrap_bump!(ctx, "quarry");
11
12    // Set quarry params
13    quarry.index = index;
14    quarry.famine_ts = i64::MAX;
15    quarry.rewarder = rewarder.key();
16    quarry.annual_rewards_rate = 0;
17    quarry.rewards_share = 0;
18    quarry.token_mint_decimals = ctx.accounts.token_mint.decimals;
19    quarry.token_mint_key = ctx.accounts.token_mint.key();
20
21    let current_ts = Clock::get()?.unix_timestamp;
22    emit!(QuarryCreateEvent {
23        token_mint: quarry.token_mint_key,
24        timestamp: current_ts,
25    });
26
27    Ok(())
28}
29
30/// Accounts for [quarry_mine::create_quarry_v2].
31#[derive(Accounts)]
32pub struct CreateQuarryV2<'info> {
33    /// [Quarry].
34    #[account(
35        init,
36        seeds = [
37            b"Quarry".as_ref(),
38            auth.rewarder.key().to_bytes().as_ref(),
39            token_mint.key().to_bytes().as_ref()
40        ],
41        bump,
42        payer = payer,
43        space = 8 + Quarry::LEN
44    )]
45    pub quarry: Account<'info, Quarry>,
46
47    /// [Rewarder] authority.
48    pub auth: MutableRewarderWithAuthority<'info>,
49
50    /// [Mint] of the token to create a [Quarry] for.
51    pub token_mint: Account<'info, Mint>,
52
53    /// Payer of [Quarry] creation.
54    #[account(mut)]
55    pub payer: Signer<'info>,
56
57    /// System program.
58    pub system_program: Program<'info, System>,
59}
60
61impl<'info> Validate<'info> for CreateQuarryV2<'info> {
62    fn validate(&self) -> Result<()> {
63        self.auth.validate()?;
64        invariant!(!self.auth.rewarder.is_paused, Paused);
65        Ok(())
66    }
67}
68
69/// Emitted when a new quarry is created.
70#[event]
71pub struct QuarryCreateEvent {
72    /// [Mint] of the [Quarry] token.
73    pub token_mint: Pubkey,
74    /// When the event took place.
75    pub timestamp: i64,
76}