Skip to main content

quarry_mint_wrapper/
lib.rs

1//! Proxy program for interacting with the token mint.
2#![deny(rustdoc::all)]
3#![allow(rustdoc::missing_doc_code_examples)]
4#![allow(deprecated)]
5
6#[macro_use]
7mod macros;
8
9use anchor_lang::prelude::*;
10use anchor_spl::token::Token;
11use anchor_spl::token::{self, Mint, TokenAccount};
12use vipers::prelude::*;
13
14mod account_validators;
15mod instructions;
16mod state;
17
18use instructions::*;
19pub use state::*;
20
21
22/// declare_id!("QMWoBmAyJLAsA1Lh9ugMTw2gciTihncciphzdNzdZYV");
23declare_id!("HpgnS49YGjR6QqHqH1w8bqWthYW64Sp3QRovfTmmPDUm");
24
25#[cfg(not(feature = "no-entrypoint"))]
26solana_security_txt::security_txt! {
27    name: "Quarry Mint Wrapper",
28    project_url: "https://quarry.so",
29    contacts: "email:team@quarry.so",
30    policy: "https://github.com/QuarryProtocol/quarry/blob/master/SECURITY.md",
31
32    source_code: "https://github.com/QuarryProtocol/quarry",
33    auditors: "Quantstamp"
34}
35
36#[program]
37pub mod quarry_mint_wrapper {
38    use super::*;
39
40    // --------------------------------
41    // [MintWrapper] instructions
42    // --------------------------------
43
44    /// Creates a new [MintWrapper].
45    #[deprecated(since = "5.0.0", note = "Use `new_wrapper_v2` instead.")]
46    #[access_control(ctx.accounts.validate())]
47    pub fn new_wrapper(ctx: Context<NewWrapper>, _bump: u8, hard_cap: u64) -> Result<()> {
48        instructions::new_wrapper::handler(ctx, hard_cap)
49    }
50
51    /// Creates a new [MintWrapper].
52    ///
53    /// The V2 variant removes the need for supplying the bump.
54    #[access_control(ctx.accounts.validate())]
55    pub fn new_wrapper_v2(ctx: Context<NewWrapper>, hard_cap: u64) -> Result<()> {
56        instructions::new_wrapper::handler(ctx, hard_cap)
57    }
58
59    /// Transfers admin to another account.
60    #[access_control(ctx.accounts.validate())]
61    pub fn transfer_admin(ctx: Context<TransferAdmin>) -> Result<()> {
62        let mint_wrapper = &mut ctx.accounts.mint_wrapper;
63        mint_wrapper.pending_admin = ctx.accounts.next_admin.key();
64
65        emit!(MintWrapperAdminProposeEvent {
66            mint_wrapper: mint_wrapper.key(),
67            current_admin: mint_wrapper.admin,
68            pending_admin: mint_wrapper.pending_admin,
69        });
70        Ok(())
71    }
72
73    /// Accepts the new admin.
74    #[access_control(ctx.accounts.validate())]
75    pub fn accept_admin(ctx: Context<AcceptAdmin>) -> Result<()> {
76        let mint_wrapper = &mut ctx.accounts.mint_wrapper;
77        let previous_admin = mint_wrapper.admin;
78        mint_wrapper.admin = ctx.accounts.pending_admin.key();
79        mint_wrapper.pending_admin = Pubkey::default();
80
81        emit!(MintWrapperAdminUpdateEvent {
82            mint_wrapper: mint_wrapper.key(),
83            previous_admin,
84            admin: mint_wrapper.admin,
85        });
86        Ok(())
87    }
88
89    // --------------------------------
90    // [Minter] instructions
91    // --------------------------------
92
93    /// Creates a new [Minter].
94    #[deprecated(since = "5.0.0", note = "Use `new_minter_v2` instead.")]
95    #[access_control(ctx.accounts.validate())]
96    pub fn new_minter(ctx: Context<NewMinter>, _bump: u8) -> Result<()> {
97        instructions::new_minter::handler(ctx)
98    }
99
100    /// Creates a new [Minter].
101    ///
102    /// The V2 variant removes the need for supplying the bump.
103    #[access_control(ctx.accounts.validate())]
104    pub fn new_minter_v2(ctx: Context<NewMinter>) -> Result<()> {
105        instructions::new_minter::handler(ctx)
106    }
107
108    /// Updates a [Minter]'s allowance.
109    #[access_control(ctx.accounts.validate())]
110    pub fn minter_update(ctx: Context<MinterUpdate>, allowance: u64) -> Result<()> {
111        let minter = &mut ctx.accounts.minter;
112        let previous_allowance = minter.allowance;
113        minter.allowance = allowance;
114
115        let mint_wrapper = &mut ctx.accounts.auth.mint_wrapper;
116        mint_wrapper.total_allowance = unwrap_int!(mint_wrapper
117            .total_allowance
118            .checked_add(allowance)
119            .and_then(|v| v.checked_sub(previous_allowance)));
120
121        emit!(MinterAllowanceUpdateEvent {
122            mint_wrapper: minter.mint_wrapper,
123            minter: minter.key(),
124            previous_allowance,
125            allowance: minter.allowance,
126        });
127        Ok(())
128    }
129
130    /// Performs a mint.
131    #[access_control(ctx.accounts.validate())]
132    pub fn perform_mint(ctx: Context<PerformMint>, amount: u64) -> Result<()> {
133        let mint_wrapper = &ctx.accounts.mint_wrapper;
134        let minter = &mut ctx.accounts.minter;
135        invariant!(minter.allowance >= amount, MinterAllowanceExceeded);
136
137        let new_supply = unwrap_int!(ctx.accounts.token_mint.supply.checked_add(amount));
138        invariant!(new_supply <= mint_wrapper.hard_cap, HardcapExceeded);
139
140        minter.allowance = unwrap_int!(minter.allowance.checked_sub(amount));
141        minter.total_minted = unwrap_int!(minter.total_minted.checked_add(amount));
142
143        let seeds = gen_wrapper_signer_seeds!(mint_wrapper);
144        let proxy_signer = &[&seeds[..]];
145        let cpi_ctx = CpiContext::new_with_signer(
146            ctx.accounts.token_program.to_account_info(),
147            token::MintTo {
148                mint: ctx.accounts.token_mint.to_account_info(),
149                to: ctx.accounts.destination.to_account_info(),
150                authority: ctx.accounts.mint_wrapper.to_account_info(),
151            },
152            proxy_signer,
153        );
154        token::mint_to(cpi_ctx, amount)?;
155
156        let mint_wrapper = &mut ctx.accounts.mint_wrapper;
157        mint_wrapper.total_allowance =
158            unwrap_int!(mint_wrapper.total_allowance.checked_sub(amount));
159        mint_wrapper.total_minted = unwrap_int!(mint_wrapper.total_minted.checked_add(amount));
160
161        // extra sanity checks
162        ctx.accounts.token_mint.reload()?;
163        invariant!(new_supply == ctx.accounts.token_mint.supply, Unauthorized);
164
165        emit!(MinterMintEvent {
166            mint_wrapper: mint_wrapper.key(),
167            minter: minter.key(),
168            amount,
169            destination: ctx.accounts.destination.key(),
170        });
171        Ok(())
172    }
173}
174
175// --------------------------------
176// Instructions
177// --------------------------------
178
179/// Updates a minter.
180#[derive(Accounts)]
181pub struct MinterUpdate<'info> {
182    /// Owner of the [MintWrapper].
183    pub auth: OnlyAdmin<'info>,
184    /// Information about the minter.
185    #[account(mut)]
186    pub minter: Account<'info, Minter>,
187}
188
189#[derive(Accounts)]
190pub struct TransferAdmin<'info> {
191    /// The [MintWrapper].
192    #[account(mut)]
193    pub mint_wrapper: Account<'info, MintWrapper>,
194
195    /// The previous admin.
196    pub admin: Signer<'info>,
197
198    /// The next admin.
199    /// CHECK: OK
200    pub next_admin: UncheckedAccount<'info>,
201}
202
203#[derive(Accounts)]
204pub struct AcceptAdmin<'info> {
205    /// The mint wrapper.
206    #[account(mut)]
207    pub mint_wrapper: Account<'info, MintWrapper>,
208
209    /// The new admin.
210    pub pending_admin: Signer<'info>,
211}
212
213/// Accounts for the perform_mint instruction.
214#[derive(Accounts, Clone)]
215pub struct PerformMint<'info> {
216    /// [MintWrapper].
217    #[account(mut)]
218    pub mint_wrapper: Account<'info, MintWrapper>,
219
220    /// [Minter]'s authority.
221    pub minter_authority: Signer<'info>,
222
223    /// Token [Mint].
224    #[account(mut)]
225    pub token_mint: Account<'info, Mint>,
226
227    /// Destination [TokenAccount] for minted tokens.
228    #[account(mut)]
229    pub destination: Account<'info, TokenAccount>,
230
231    /// [Minter] information.
232    #[account(mut)]
233    pub minter: Account<'info, Minter>,
234
235    /// SPL Token program.
236    pub token_program: Program<'info, Token>,
237}
238
239// --------------------------------
240// Account structs
241// --------------------------------
242
243/// Only an admin is allowed to use instructions containing this struct.
244#[derive(Accounts)]
245pub struct OnlyAdmin<'info> {
246    /// The [MintWrapper].
247    #[account(mut, has_one = admin @ ErrorCode::Unauthorized)]
248    pub mint_wrapper: Account<'info, MintWrapper>,
249    /// [MintWrapper::admin].
250    pub admin: Signer<'info>,
251}
252
253// --------------------------------
254// Events
255// --------------------------------
256
257/// Emitted when a [MintWrapper] is created.
258#[event]
259pub struct NewMintWrapperEvent {
260    /// The [MintWrapper].
261    #[index]
262    pub mint_wrapper: Pubkey,
263
264    /// Hard cap.
265    pub hard_cap: u64,
266    /// The admin.
267    pub admin: Pubkey,
268    /// The [Mint] of the token.
269    pub token_mint: Pubkey,
270}
271
272/// Emitted when a [MintWrapper]'s admin is proposed.
273#[event]
274pub struct MintWrapperAdminProposeEvent {
275    /// The [MintWrapper].
276    #[index]
277    pub mint_wrapper: Pubkey,
278
279    /// The [MintWrapper]'s current admin.
280    pub current_admin: Pubkey,
281    /// The [MintWrapper]'s pending admin.
282    pub pending_admin: Pubkey,
283}
284
285/// Emitted when a [MintWrapper]'s admin is transferred.
286#[event]
287pub struct MintWrapperAdminUpdateEvent {
288    /// The [MintWrapper].
289    #[index]
290    pub mint_wrapper: Pubkey,
291
292    /// The [MintWrapper]'s previous admin.
293    pub previous_admin: Pubkey,
294    /// The [MintWrapper]'s new admin.
295    pub admin: Pubkey,
296}
297
298/// Emitted when a [Minter] is created.
299#[event]
300pub struct NewMinterEvent {
301    /// The [MintWrapper].
302    #[index]
303    pub mint_wrapper: Pubkey,
304    /// The [Minter].
305    #[index]
306    pub minter: Pubkey,
307
308    /// The [Minter]'s index.
309    pub index: u64,
310    /// The [Minter]'s authority.
311    pub minter_authority: Pubkey,
312}
313
314/// Emitted when a [Minter]'s allowance is updated.
315#[event]
316pub struct MinterAllowanceUpdateEvent {
317    /// The [MintWrapper].
318    #[index]
319    pub mint_wrapper: Pubkey,
320    /// The [Minter].
321    #[index]
322    pub minter: Pubkey,
323
324    /// The [Minter]'s previous allowance.
325    pub previous_allowance: u64,
326    /// The [Minter]'s new allowance.
327    pub allowance: u64,
328}
329
330/// Emitted when a [Minter] performs a mint.
331#[event]
332pub struct MinterMintEvent {
333    /// The [MintWrapper].
334    #[index]
335    pub mint_wrapper: Pubkey,
336    /// The [Minter].
337    #[index]
338    pub minter: Pubkey,
339
340    /// Amount minted.
341    pub amount: u64,
342    /// Mint destination.
343    pub destination: Pubkey,
344}
345
346/// Error Codes
347#[error_code]
348pub enum ErrorCode {
349    #[msg("You are not authorized to perform this action.")]
350    Unauthorized,
351    #[msg("Cannot mint over hard cap.")]
352    HardcapExceeded,
353    #[msg("Minter allowance exceeded.")]
354    MinterAllowanceExceeded,
355}