1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use {
    crate::{constants::*, errors::*, state::*},
    anchor_lang::prelude::*,
};

#[derive(Accounts)]
#[instruction(amount: u64)]
pub struct UnstakeCreate<'info> {
    #[account(mut)]
    pub authority: Signer<'info>,

    #[account(
        seeds = [
            SEED_DELEGATION,
            delegation.worker.as_ref(),
            delegation.id.to_be_bytes().as_ref(),
        ],
        bump,
        has_one = authority,
        has_one = worker,
    )]
    pub delegation: Account<'info, Delegation>,

    #[account(
        mut,
        seeds = [SEED_REGISTRY],
        bump,
        constraint = !registry.locked @ SablierError::RegistryLocked
    )]
    pub registry: Account<'info, Registry>,

    pub system_program: Program<'info, System>,

    #[account(
        init,
        seeds = [
            SEED_UNSTAKE,
            registry.total_unstakes.to_be_bytes().as_ref(),
        ],
        bump,
        payer = authority,
        space = 8 + Unstake::INIT_SPACE,
    )]
    pub unstake: Account<'info, Unstake>,

    #[account(address = worker.pubkey())]
    pub worker: Account<'info, Worker>,
}

pub fn handler(ctx: Context<UnstakeCreate>, amount: u64) -> Result<()> {
    // Get accounts.
    let authority = &ctx.accounts.authority;
    let delegation = &ctx.accounts.delegation;
    let registry = &mut ctx.accounts.registry;
    let unstake = &mut ctx.accounts.unstake;
    let worker = &ctx.accounts.worker;

    // Validate the request is valid.
    require!(
        amount <= delegation.stake_amount,
        SablierError::InvalidUnstakeAmount
    );

    // Initialize the unstake account.
    unstake.init(
        amount,
        authority.key(),
        delegation.key(),
        registry.total_unstakes,
        worker.key(),
    )?;

    // Increment the registry's unstake counter.
    registry.total_unstakes += 1;

    Ok(())
}