sablier_network_program/instructions/
delegation_create.rs

1use {
2    crate::{constants::*, state::*},
3    anchor_lang::prelude::*,
4    anchor_spl::{
5        associated_token::AssociatedToken,
6        token::{Mint, Token, TokenAccount},
7    },
8};
9
10#[derive(Accounts)]
11pub struct DelegationCreate<'info> {
12    #[account(mut)]
13    pub authority: Signer<'info>,
14
15    #[account(address = Config::pubkey())]
16    pub config: AccountLoader<'info, Config>,
17
18    #[account(
19        init,
20        seeds = [
21            SEED_DELEGATION,
22            worker.key().as_ref(),
23            worker.total_delegations.to_be_bytes().as_ref(),
24        ],
25        bump,
26        payer = authority,
27        space = 8 + Delegation::INIT_SPACE,
28    )]
29    pub delegation: Account<'info, Delegation>,
30
31    #[account(
32        init,
33        payer = authority,
34        associated_token::authority = delegation,
35        associated_token::mint = mint,
36    )]
37    pub delegation_tokens: Account<'info, TokenAccount>,
38
39    #[account(address = config.load()?.mint)]
40    pub mint: Account<'info, Mint>,
41
42    #[account(
43        mut,
44        seeds = [
45            SEED_WORKER,
46            worker.id.to_be_bytes().as_ref(),
47        ],
48        bump
49    )]
50    pub worker: Account<'info, Worker>,
51
52    pub token_program: Program<'info, Token>,
53
54    pub associated_token_program: Program<'info, AssociatedToken>,
55
56    pub system_program: Program<'info, System>,
57}
58
59pub fn handler(ctx: Context<DelegationCreate>) -> Result<()> {
60    // Get accounts
61    let authority = &ctx.accounts.authority;
62    let delegation = &mut ctx.accounts.delegation;
63    let worker = &mut ctx.accounts.worker;
64
65    // Initialize the delegation account.
66    delegation.init(authority.key(), worker.total_delegations, worker.key())?;
67
68    // Increment the worker's total delegations counter.
69    worker.total_delegations += 1;
70
71    Ok(())
72}