Skip to content

Commit 0245f42

Browse files
refactor: extract instruction handlers for basics programs
Move instruction handlers into src/instructions/ directory following Anchor 1.0 conventions for: counter, lever, anchor-realloc, transfer-sol. Each handler is now a separate file with its own Accounts struct, and lib.rs delegates to the instruction modules.
1 parent 4882570 commit 0245f42

16 files changed

Lines changed: 209 additions & 125 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use anchor_lang::prelude::*;
2+
3+
use crate::Counter;
4+
5+
#[derive(Accounts)]
6+
pub struct Increment<'info> {
7+
#[account(mut)]
8+
pub counter: Account<'info, Counter>,
9+
}
10+
11+
pub fn handler(context: Context<Increment>) -> Result<()> {
12+
context.accounts.counter.count = context.accounts.counter.count.checked_add(1).unwrap();
13+
Ok(())
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use anchor_lang::prelude::*;
2+
3+
use crate::Counter;
4+
5+
#[derive(Accounts)]
6+
pub struct InitializeCounter<'info> {
7+
#[account(mut)]
8+
pub payer: Signer<'info>,
9+
10+
#[account(
11+
init,
12+
space = 8 + Counter::INIT_SPACE,
13+
payer = payer
14+
)]
15+
pub counter: Account<'info, Counter>,
16+
pub system_program: Program<'info, System>,
17+
}
18+
19+
pub fn handler(_context: Context<InitializeCounter>) -> Result<()> {
20+
Ok(())
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod increment;
2+
pub mod initialize_counter;
3+
4+
pub use increment::*;
5+
pub use initialize_counter::*;

basics/counter/anchor/programs/counter_anchor/src/lib.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,23 @@
11
use anchor_lang::prelude::*;
22

3+
mod instructions;
4+
use instructions::*;
5+
36
declare_id!("BmDHboaj1kBUoinJKKSRqKfMeRKJqQqEbUj1VgzeQe4A");
47

58
#[program]
69
pub mod counter_anchor {
710
use super::*;
811

9-
pub fn initialize_counter(_context: Context<InitializeCounter>) -> Result<()> {
10-
Ok(())
12+
pub fn initialize_counter(context: Context<InitializeCounter>) -> Result<()> {
13+
instructions::initialize_counter::handler(context)
1114
}
1215

1316
pub fn increment(context: Context<Increment>) -> Result<()> {
14-
context.accounts.counter.count = context.accounts.counter.count.checked_add(1).unwrap();
15-
Ok(())
17+
instructions::increment::handler(context)
1618
}
1719
}
1820

19-
#[derive(Accounts)]
20-
pub struct InitializeCounter<'info> {
21-
#[account(mut)]
22-
pub payer: Signer<'info>,
23-
24-
#[account(
25-
init,
26-
space = 8 + Counter::INIT_SPACE,
27-
payer = payer
28-
)]
29-
pub counter: Account<'info, Counter>,
30-
pub system_program: Program<'info, System>,
31-
}
32-
33-
#[derive(Accounts)]
34-
pub struct Increment<'info> {
35-
#[account(mut)]
36-
pub counter: Account<'info, Counter>,
37-
}
38-
3921
#[account]
4022
#[derive(InitSpace)]
4123
pub struct Counter {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use anchor_lang::prelude::*;
2+
3+
use crate::PowerStatus;
4+
5+
#[derive(Accounts)]
6+
pub struct InitializeLever<'info> {
7+
#[account(init, payer = user, space = PowerStatus::DISCRIMINATOR.len() + PowerStatus::INIT_SPACE)]
8+
pub power: Account<'info, PowerStatus>,
9+
#[account(mut)]
10+
pub user: Signer<'info>,
11+
pub system_program: Program<'info, System>,
12+
}
13+
14+
pub fn handler(_context: Context<InitializeLever>) -> Result<()> {
15+
Ok(())
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod initialize;
2+
pub mod switch_power;
3+
4+
pub use initialize::*;
5+
pub use switch_power::*;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use anchor_lang::prelude::*;
2+
3+
use crate::PowerStatus;
4+
5+
#[derive(Accounts)]
6+
pub struct SetPowerStatus<'info> {
7+
#[account(mut)]
8+
pub power: Account<'info, PowerStatus>,
9+
}
10+
11+
pub fn handler(context: Context<SetPowerStatus>, name: String) -> Result<()> {
12+
let power = &mut context.accounts.power;
13+
power.is_on = !power.is_on;
14+
15+
msg!("{} is pulling the power switch!", &name);
16+
17+
match power.is_on {
18+
true => msg!("The power is now on."),
19+
false => msg!("The power is now off!"),
20+
};
21+
22+
Ok(())
23+
}

basics/cross-program-invocation/anchor/programs/lever/src/lib.rs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,23 @@
11
use anchor_lang::prelude::*;
22

3+
mod instructions;
4+
use instructions::*;
5+
36
declare_id!("E64FVeubGC4NPNF2UBJYX4AkrVowf74fRJD9q6YhwstN");
47

58
#[program]
69
pub mod lever {
710
use super::*;
811

9-
pub fn initialize(_context: Context<InitializeLever>) -> Result<()> {
10-
Ok(())
12+
pub fn initialize(context: Context<InitializeLever>) -> Result<()> {
13+
instructions::initialize::handler(context)
1114
}
1215

1316
pub fn switch_power(context: Context<SetPowerStatus>, name: String) -> Result<()> {
14-
let power = &mut context.accounts.power;
15-
power.is_on = !power.is_on;
16-
17-
msg!("{} is pulling the power switch!", &name);
18-
19-
match power.is_on {
20-
true => msg!("The power is now on."),
21-
false => msg!("The power is now off!"),
22-
};
23-
24-
Ok(())
17+
instructions::switch_power::handler(context, name)
2518
}
2619
}
2720

28-
#[derive(Accounts)]
29-
pub struct InitializeLever<'info> {
30-
#[account(init, payer = user, space = PowerStatus::DISCRIMINATOR.len() + PowerStatus::INIT_SPACE)]
31-
pub power: Account<'info, PowerStatus>,
32-
#[account(mut)]
33-
pub user: Signer<'info>,
34-
pub system_program: Program<'info, System>,
35-
}
36-
37-
#[derive(Accounts)]
38-
pub struct SetPowerStatus<'info> {
39-
#[account(mut)]
40-
pub power: Account<'info, PowerStatus>,
41-
}
42-
4321
#[account]
4422
#[derive(InitSpace)]
4523
pub struct PowerStatus {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use anchor_lang::prelude::*;
2+
3+
use crate::Message;
4+
5+
#[derive(Accounts)]
6+
#[instruction(input: String)]
7+
pub struct Initialize<'info> {
8+
#[account(mut)]
9+
pub payer: Signer<'info>,
10+
11+
#[account(
12+
init,
13+
payer = payer,
14+
space = Message::required_space(input.len()),
15+
)]
16+
pub message_account: Account<'info, Message>,
17+
pub system_program: Program<'info, System>,
18+
}
19+
20+
pub fn handler(context: Context<Initialize>, input: String) -> Result<()> {
21+
context.accounts.message_account.message = input;
22+
Ok(())
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod initialize;
2+
pub mod update;
3+
4+
pub use initialize::*;
5+
pub use update::*;

0 commit comments

Comments
 (0)