|
| 1 | +// Instruction to clawback funds once they have expired |
| 2 | + |
| 3 | +use anchor_lang::{context::Context, prelude::*, Accounts, Key, Result}; |
| 4 | +use anchor_spl::{ |
| 5 | + token, |
| 6 | + token::{Token, TokenAccount}, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::{error::ErrorCode, state::merkle_distributor::MerkleDistributor}; |
| 10 | + |
| 11 | +/// [merkle_distributor::clawback] accounts. |
| 12 | +#[derive(Accounts)] |
| 13 | +pub struct ClawbackNow<'info> { |
| 14 | + /// The [MerkleDistributor]. |
| 15 | + #[account(mut)] |
| 16 | + pub distributor: Account<'info, MerkleDistributor>, |
| 17 | + |
| 18 | + /// Distributor ATA containing the tokens to distribute. |
| 19 | + #[account( |
| 20 | + mut, |
| 21 | + associated_token::mint = distributor.mint, |
| 22 | + associated_token::authority = distributor.key(), |
| 23 | + address = distributor.token_vault |
| 24 | + )] |
| 25 | + pub from: Account<'info, TokenAccount>, |
| 26 | + |
| 27 | + /// The Clawback token account. |
| 28 | + #[account(mut, address = distributor.clawback_receiver)] |
| 29 | + pub to: Account<'info, TokenAccount>, |
| 30 | + |
| 31 | + /// Claimant account |
| 32 | + /// Anyone can claw back the funds |
| 33 | + pub claimant: Signer<'info>, |
| 34 | + |
| 35 | + /// The [System] program. |
| 36 | + pub system_program: Program<'info, System>, |
| 37 | + |
| 38 | + /// SPL [Token] program. |
| 39 | + pub token_program: Program<'info, Token>, |
| 40 | +} |
| 41 | + |
| 42 | +/// Claws back unclaimed tokens by: |
| 43 | +/// 1. Checking that the lockup has expired |
| 44 | +/// 2. Transferring remaining funds from the vault to the clawback receiver |
| 45 | +/// 3. Marking the distributor as clawed back |
| 46 | +/// CHECK: |
| 47 | +/// 1. The distributor has not already been clawed back |
| 48 | +#[allow(clippy::result_large_err)] |
| 49 | +pub fn handle_clawback_now(ctx: Context<ClawbackNow>, amount: u64) -> Result<()> { |
| 50 | + let distributor = &ctx.accounts.distributor; |
| 51 | + |
| 52 | + let seeds = [ |
| 53 | + b"MerkleDistributor".as_ref(), |
| 54 | + &distributor.mint.to_bytes(), |
| 55 | + &distributor.version.to_le_bytes(), |
| 56 | + &[ctx.accounts.distributor.bump], |
| 57 | + ]; |
| 58 | + |
| 59 | + token::transfer( |
| 60 | + CpiContext::new( |
| 61 | + ctx.accounts.token_program.to_account_info(), |
| 62 | + token::Transfer { |
| 63 | + from: ctx.accounts.from.to_account_info(), |
| 64 | + to: ctx.accounts.to.to_account_info(), |
| 65 | + authority: ctx.accounts.distributor.to_account_info(), |
| 66 | + }, |
| 67 | + ) |
| 68 | + .with_signer(&[&seeds[..]]), |
| 69 | + amount, |
| 70 | + )?; |
| 71 | + |
| 72 | + Ok(()) |
| 73 | +} |
0 commit comments