|
| 1 | +use anchor_lang::prelude::*; |
| 2 | +use anchor_spl::token_interface::{ |
| 3 | + close_account, transfer_checked, CloseAccount, Mint, TokenAccount, TokenInterface, |
| 4 | + TransferChecked, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::error::HackathonError; |
| 8 | +use crate::state::{Hackathon, Prize}; |
| 9 | + |
| 10 | +// Cancel an unpaid prize: drain the vault to `refund_token_account`, close |
| 11 | +// the vault, and lock the prize so `pay_winner` can no longer run. Useful |
| 12 | +// when a prize is funded but never claimed, or when the committee wants to |
| 13 | +// reclaim surplus tokens left in a vault after `pay_winner` paid the exact |
| 14 | +// `prize.amount`. |
| 15 | +#[derive(Accounts)] |
| 16 | +#[instruction(prize_index: u8)] |
| 17 | +pub struct CancelPrize<'info> { |
| 18 | + // Hackathon admin. Must match `hackathon.authority`. |
| 19 | + pub authority: Signer<'info>, |
| 20 | + |
| 21 | + // Where the vault's reclaimed rent lamports go. Separate from `authority` |
| 22 | + // so a Squads vault PDA (which cannot directly receive non-account |
| 23 | + // lamports in this context) can still authorise the cancellation while a |
| 24 | + // human keypair takes the rent refund. |
| 25 | + #[account(mut)] |
| 26 | + pub rent_destination: SystemAccount<'info>, |
| 27 | + |
| 28 | + #[account( |
| 29 | + has_one = authority, |
| 30 | + seeds = [b"hackathon", authority.key().as_ref(), super::name_seed(&hackathon.name).as_ref()], |
| 31 | + bump = hackathon.bump, |
| 32 | + )] |
| 33 | + pub hackathon: Account<'info, Hackathon>, |
| 34 | + |
| 35 | + #[account( |
| 36 | + mut, |
| 37 | + seeds = [b"prize", hackathon.key().as_ref(), &[prize_index]], |
| 38 | + bump = prize.bump, |
| 39 | + has_one = mint, |
| 40 | + constraint = prize.hackathon == hackathon.key(), |
| 41 | + )] |
| 42 | + pub prize: Account<'info, Prize>, |
| 43 | + |
| 44 | + pub mint: InterfaceAccount<'info, Mint>, |
| 45 | + |
| 46 | + #[account( |
| 47 | + mut, |
| 48 | + associated_token::mint = mint, |
| 49 | + associated_token::authority = prize, |
| 50 | + associated_token::token_program = token_program, |
| 51 | + )] |
| 52 | + pub vault: InterfaceAccount<'info, TokenAccount>, |
| 53 | + |
| 54 | + // Token account that receives any tokens currently held in the vault. |
| 55 | + // Must match `mint` but otherwise unconstrained — the committee picks |
| 56 | + // where to send the refund. |
| 57 | + #[account( |
| 58 | + mut, |
| 59 | + token::mint = mint, |
| 60 | + token::token_program = token_program, |
| 61 | + )] |
| 62 | + pub refund_token_account: InterfaceAccount<'info, TokenAccount>, |
| 63 | + |
| 64 | + pub token_program: Interface<'info, TokenInterface>, |
| 65 | +} |
| 66 | + |
| 67 | +pub fn handle_cancel_prize(context: Context<CancelPrize>, _prize_index: u8) -> Result<()> { |
| 68 | + let prize = &mut context.accounts.prize; |
| 69 | + require!(!prize.paid, HackathonError::AlreadyPaid); |
| 70 | + require!(!prize.cancelled, HackathonError::Cancelled); |
| 71 | + |
| 72 | + let hackathon_key = context.accounts.hackathon.key(); |
| 73 | + let prize_index_byte = [prize.index]; |
| 74 | + let bump = [prize.bump]; |
| 75 | + let seeds = &[ |
| 76 | + b"prize".as_ref(), |
| 77 | + hackathon_key.as_ref(), |
| 78 | + prize_index_byte.as_ref(), |
| 79 | + bump.as_ref(), |
| 80 | + ]; |
| 81 | + let signer_seeds = [&seeds[..]]; |
| 82 | + |
| 83 | + // Drain whatever is in the vault back to the refund target. This may be |
| 84 | + // zero (vault never funded) or more than `prize.amount` (vault was |
| 85 | + // over-funded); either is fine. |
| 86 | + let vault_amount = context.accounts.vault.amount; |
| 87 | + if vault_amount > 0 { |
| 88 | + let transfer_accounts = TransferChecked { |
| 89 | + from: context.accounts.vault.to_account_info(), |
| 90 | + mint: context.accounts.mint.to_account_info(), |
| 91 | + to: context.accounts.refund_token_account.to_account_info(), |
| 92 | + authority: prize.to_account_info(), |
| 93 | + }; |
| 94 | + let cpi_context = CpiContext::new_with_signer( |
| 95 | + context.accounts.token_program.key(), |
| 96 | + transfer_accounts, |
| 97 | + &signer_seeds, |
| 98 | + ); |
| 99 | + transfer_checked(cpi_context, vault_amount, context.accounts.mint.decimals)?; |
| 100 | + } |
| 101 | + |
| 102 | + // Close the vault so its rent comes back. Prize account itself stays |
| 103 | + // open: it's an immutable record that this prize was cancelled. |
| 104 | + let close_accounts = CloseAccount { |
| 105 | + account: context.accounts.vault.to_account_info(), |
| 106 | + destination: context.accounts.rent_destination.to_account_info(), |
| 107 | + authority: prize.to_account_info(), |
| 108 | + }; |
| 109 | + let cpi_context = CpiContext::new_with_signer( |
| 110 | + context.accounts.token_program.key(), |
| 111 | + close_accounts, |
| 112 | + &signer_seeds, |
| 113 | + ); |
| 114 | + close_account(cpi_context)?; |
| 115 | + |
| 116 | + prize.cancelled = true; |
| 117 | + Ok(()) |
| 118 | +} |
0 commit comments