lib.rs 982 B

123456789101112131415161718192021222324252627282930313233
  1. use anchor_lang::prelude::*;
  2. declare_id!("DMATyR7jooijeJ2aJYWiyYPf3eoUouumaaLw1JbG3TYF");
  3. #[program]
  4. pub mod counter_program {
  5. use super::*;
  6. pub fn initialize(ctx: Context<InitializeContext>) -> Result<()> {
  7. ctx.accounts.state.count = 0;
  8. Ok(())
  9. }
  10. pub fn increment(ctx: Context<IncrementContext>) -> Result<()> {
  11. ctx.accounts.state.count = ctx.accounts.state.count + 1;
  12. Ok(())
  13. }
  14. }
  15. #[derive(Accounts)]
  16. pub struct InitializeContext<'info> {
  17. #[account(mut)]
  18. pub user: Signer<'info>,
  19. #[account(init, payer = user, space = 17, seeds = [b"count"], bump)]
  20. pub state: Account<'info, CounterState>,
  21. pub system_program: Program<'info, System>,
  22. }
  23. #[derive(Accounts)]
  24. pub struct IncrementContext<'info> {
  25. #[account(mut, seeds = [b"count"], bump)]
  26. pub state: Account<'info, CounterState>,
  27. pub system_program: Program<'info, System>,
  28. }
  29. #[account]
  30. pub struct CounterState {
  31. pub count: u64,
  32. pub bump: u8,
  33. }