sablier_network_program/instructions/
unstake_create.rs

1use {
2    crate::{constants::*, errors::*, state::*},
3    anchor_lang::prelude::*,
4};
5
6#[derive(Accounts)]
7#[instruction(amount: u64)]
8pub struct UnstakeCreate<'info> {
9    #[account(mut)]
10    pub authority: Signer<'info>,
11
12    #[account(
13        seeds = [
14            SEED_DELEGATION,
15            delegation.worker.as_ref(),
16            delegation.id.to_be_bytes().as_ref(),
17        ],
18        bump,
19        has_one = authority,
20        has_one = worker,
21    )]
22    pub delegation: Account<'info, Delegation>,
23
24    #[account(
25        mut,
26        seeds = [SEED_REGISTRY],
27        bump,
28        constraint = !registry.locked @ SablierError::RegistryLocked
29    )]
30    pub registry: Account<'info, Registry>,
31
32    pub system_program: Program<'info, System>,
33
34    #[account(
35        init,
36        seeds = [
37            SEED_UNSTAKE,
38            registry.total_unstakes.to_be_bytes().as_ref(),
39        ],
40        bump,
41        payer = authority,
42        space = 8 + Unstake::INIT_SPACE,
43    )]
44    pub unstake: Account<'info, Unstake>,
45
46    #[account(address = worker.pubkey())]
47    pub worker: Account<'info, Worker>,
48}
49
50pub fn handler(ctx: Context<UnstakeCreate>, amount: u64) -> Result<()> {
51    // Get accounts.
52    let authority = &ctx.accounts.authority;
53    let delegation = &ctx.accounts.delegation;
54    let registry = &mut ctx.accounts.registry;
55    let unstake = &mut ctx.accounts.unstake;
56    let worker = &ctx.accounts.worker;
57
58    // Validate the request is valid.
59    require!(
60        amount <= delegation.stake_amount,
61        SablierError::InvalidUnstakeAmount
62    );
63
64    // Initialize the unstake account.
65    unstake.init(
66        amount,
67        authority.key(),
68        delegation.key(),
69        registry.total_unstakes,
70        worker.key(),
71    )?;
72
73    // Increment the registry's unstake counter.
74    registry.total_unstakes += 1;
75
76    Ok(())
77}