miclockwork_network_program/instructions/
delegation_deposit.rs

1use {
2    crate::state::*,
3    anchor_lang::prelude::*,
4    anchor_spl::token::{transfer, Token, TokenAccount, Transfer},
5};
6
7#[derive(Accounts)]
8#[instruction(amount: u64)]
9pub struct DelegationDeposit<'info> {
10    #[account(mut)]
11    pub authority: Signer<'info>,
12
13    #[account(
14        mut,
15        associated_token::authority = authority,
16        associated_token::mint = config.mint,
17    )]
18    pub authority_tokens: Account<'info, TokenAccount>,
19
20    #[account(address = Config::pubkey())]
21    pub config: Account<'info, Config>,
22
23    #[account(
24        mut,
25        seeds = [
26            SEED_DELEGATION,
27            delegation.worker.as_ref(),
28            delegation.id.to_be_bytes().as_ref(),
29        ],
30        bump,
31        has_one = authority,
32    )]
33    pub delegation: Account<'info, Delegation>,
34
35    #[account(
36        mut,
37        associated_token::authority = delegation,
38        associated_token::mint = config.mint,
39    )]
40    pub delegation_tokens: Account<'info, TokenAccount>,
41
42    #[account(address = anchor_spl::token::ID)]
43    pub token_program: Program<'info, Token>,
44}
45
46pub fn handler(ctx: Context<DelegationDeposit>, amount: u64) -> Result<()> {
47    // Get accounts.
48    let authority = &ctx.accounts.authority;
49    let authority_tokens = &ctx.accounts.authority_tokens;
50    let delegation_tokens = &ctx.accounts.delegation_tokens;
51    let token_program = &ctx.accounts.token_program;
52
53    // Transfer tokens from authority tokens to delegation
54    transfer(
55        CpiContext::new(
56            token_program.to_account_info(),
57            Transfer {
58                from: authority_tokens.to_account_info(),
59                to: delegation_tokens.to_account_info(),
60                authority: authority.to_account_info(),
61            },
62        ),
63        amount,
64    )?;
65
66    Ok(())
67}