quarry_registry/
lib.rs

1//! Registry to help the frontend quickly locate all active quarries.
2#![deny(rustdoc::all)]
3#![allow(rustdoc::missing_doc_code_examples)]
4
5use anchor_lang::prelude::*;
6use quarry_mine::{Quarry, Rewarder};
7use vipers::prelude::*;
8
9mod account_validators;
10
11/// declare_id!("QREGBnEj9Sa5uR91AV8u3FxThgP5ZCvdZUW2bHAkfNc");
12declare_id!("AbphG2hDqjUqAWmEuRTxi3eJvZGvxEKTaUGUt1WFwHjW");
13
14#[cfg(not(feature = "no-entrypoint"))]
15solana_security_txt::security_txt! {
16    name: "Quarry Registry",
17    project_url: "https://quarry.so",
18    contacts: "email:team@quarry.so",
19    policy: "https://github.com/QuarryProtocol/quarry/blob/master/SECURITY.md",
20
21    source_code: "https://github.com/QuarryProtocol/quarry",
22    auditors: "Quantstamp"
23}
24
25/// Registry to help frontends quickly locate all active quarries.
26#[program]
27pub mod quarry_registry {
28
29    use super::*;
30
31    /// Provisions a new registry for a [Rewarder].
32    ///
33    /// # Arguments
34    ///
35    /// * `max_quarries` - The maximum number of quarries that can be held in the registry.
36    /// * `bump` - Bump seed.
37    pub fn new_registry(ctx: Context<NewRegistry>, max_quarries: u16, _bump: u8) -> Result<()> {
38        ctx.accounts.validate()?;
39        let registry = &mut ctx.accounts.registry;
40        registry.bump = unwrap_bump!(ctx, "registry");
41        registry.rewarder = ctx.accounts.rewarder.key();
42        registry
43            .tokens
44            .resize(max_quarries as usize, Pubkey::default());
45        Ok(())
46    }
47
48    /// Synchronizes a [Quarry]'s token mint with the registry of its [Rewarder].
49    pub fn sync_quarry(ctx: Context<SyncQuarry>) -> Result<()> {
50        ctx.accounts.validate()?;
51        let quarry = &ctx.accounts.quarry;
52        let registry = &mut ctx.accounts.registry;
53        registry.tokens[quarry.index as usize] = quarry.token_mint_key;
54        Ok(())
55    }
56}
57
58/// Accounts for [quarry_registry::new_registry].
59#[derive(Accounts)]
60#[instruction(max_quarries: u16)]
61pub struct NewRegistry<'info> {
62    /// [Rewarder].
63    pub rewarder: Account<'info, Rewarder>,
64
65    /// [Rewarder] of mines.
66    #[account(
67        init,
68        seeds = [
69            b"QuarryRegistry".as_ref(),
70            rewarder.key().to_bytes().as_ref()
71        ],
72        bump,
73        payer = payer,
74        space = (8 + 1 + 32 + 32 * max_quarries + 100) as usize
75    )]
76    pub registry: Account<'info, Registry>,
77
78    /// Payer of the [Registry] initialization.
79    #[account(mut)]
80    pub payer: Signer<'info>,
81
82    /// System program.
83    pub system_program: Program<'info, System>,
84}
85
86/// Accounts for [quarry_registry::sync_quarry].
87#[derive(Accounts)]
88pub struct SyncQuarry<'info> {
89    /// [Quarry] to sync.
90    pub quarry: Account<'info, Quarry>,
91    /// [Registry] to write to.
92    #[account(mut)]
93    pub registry: Account<'info, Registry>,
94}
95
96/// The [Registry] of all token mints associated with a [Rewarder].
97#[account]
98#[derive(Default, Debug)]
99pub struct Registry {
100    /// Bump seed
101    pub bump: u8,
102    /// Rewarder
103    pub rewarder: Pubkey,
104    /// Tokens
105    pub tokens: Vec<Pubkey>,
106}
107
108impl Registry {
109    /// Number of bytes a [Registry] takes up when serialized.
110    pub fn byte_length(max_quarries: u16) -> usize {
111        (1 + 32 + 4 + 32 * max_quarries) as usize
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use anchor_lang::system_program;
118
119    use super::*;
120
121    #[test]
122    fn test_registry_len() {
123        let registry = Registry {
124            tokens: vec![system_program::ID, system_program::ID],
125            ..Default::default()
126        };
127        assert_eq!(
128            registry.try_to_vec().unwrap().len(),
129            Registry::byte_length(2)
130        );
131    }
132}