lib.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. use anchor_lang::prelude::*;
  2. declare_id!("DuT6R8tQGYa8ACYXyudFJtxDppSALLcmK39b7918jeSC");
  3. #[program]
  4. pub mod basic_5 {
  5. use super::*;
  6. pub fn create(ctx: Context<Create>) -> Result<()> {
  7. let action_state = &mut ctx.accounts.action_state;
  8. // * - means dereferencing
  9. action_state.user = *ctx.accounts.user.key;
  10. // Lets initialize the state
  11. action_state.action = 0;
  12. Ok(())
  13. }
  14. pub fn walk(ctx: Context<Walk>) -> Result<()> {
  15. let action_state = &mut ctx.accounts.action_state;
  16. // Lets change the robot action state to "walk"
  17. action_state.action = 1;
  18. Ok(())
  19. }
  20. pub fn run(ctx: Context<Run>) -> Result<()> {
  21. let action_state = &mut ctx.accounts.action_state;
  22. // Lets change the robot action state to "run"
  23. action_state.action = 2;
  24. Ok(())
  25. }
  26. pub fn jump(ctx: Context<Jump>) -> Result<()> {
  27. let action_state = &mut ctx.accounts.action_state;
  28. // Lets change the robot action state to "jump"
  29. action_state.action = 3;
  30. Ok(())
  31. }
  32. pub fn reset(ctx: Context<Reset>) -> Result<()> {
  33. let action_state = &mut ctx.accounts.action_state;
  34. // Lets reset the robot action states
  35. action_state.action = 0;
  36. Ok(())
  37. }
  38. }
  39. #[derive(Accounts)]
  40. pub struct Create<'info> {
  41. // init means to create action_state account
  42. // bump to use unique address for action_state account
  43. #[account(
  44. init,
  45. payer = user,
  46. space = 8 + ActionState::INIT_SPACE,
  47. seeds = [b"action-state", user.key().as_ref()],
  48. bump
  49. )]
  50. pub action_state: Account<'info, ActionState>,
  51. // mut makes it changeble (mutable)
  52. #[account(mut)]
  53. pub user: Signer<'info>,
  54. pub system_program: Program<'info, System>,
  55. }
  56. #[derive(Accounts)]
  57. pub struct Walk<'info> {
  58. // Only the user on account action_state, should be able to change state
  59. #[account(mut, has_one = user)]
  60. pub action_state: Account<'info, ActionState>,
  61. // mut makes it changeble (mutable)
  62. #[account(mut)]
  63. pub user: Signer<'info>,
  64. }
  65. #[derive(Accounts)]
  66. pub struct Run<'info> {
  67. // Only the user on account action_state, should be able to change state
  68. #[account(mut, has_one = user)]
  69. pub action_state: Account<'info, ActionState>,
  70. // mut makes it changeble (mutable)
  71. #[account(mut)]
  72. pub user: Signer<'info>,
  73. }
  74. #[derive(Accounts)]
  75. pub struct Jump<'info> {
  76. // Only the user on account action_state, should be able to change state
  77. #[account(mut, has_one = user)]
  78. pub action_state: Account<'info, ActionState>,
  79. // mut makes it changeble (mutable)
  80. #[account(mut)]
  81. pub user: Signer<'info>,
  82. }
  83. #[derive(Accounts)]
  84. pub struct Reset<'info> {
  85. // Only the user on account action_state, should be able to change state
  86. #[account(mut, has_one = user)]
  87. pub action_state: Account<'info, ActionState>,
  88. // mut makes it changeble (mutable)
  89. #[account(mut)]
  90. pub user: Signer<'info>,
  91. }
  92. #[account]
  93. #[derive(InitSpace)]
  94. pub struct ActionState {
  95. pub user: Pubkey,
  96. pub action: u8,
  97. }