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