Skip to content

Commit 6bf9a2a

Browse files
committed
clawback_now.
1 parent caa1a88 commit 6bf9a2a

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

programs/merkle-distributor/src/instructions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub use remove_new_claim::*;
88
pub use admin_new_claim::*;
99
pub use claim_new_claim::*;
1010
pub use remove_claim::*;
11+
pub use clawback_now::*;
1112
pub mod claim_locked;
1213
pub mod clawback;
1314
pub mod new_claim;
@@ -16,6 +17,7 @@ pub mod admin_new_claim;
1617
pub mod claim_new_claim;
1718
pub mod remove_new_claim;
1819
pub mod remove_claim;
20+
pub mod clawback_now;
1921

2022
pub mod set_admin;
2123
pub mod set_clawback_receiver;

0 commit comments

Comments
 (0)