lib.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. use bolt_lang::*;
  2. declare_id!("FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA");
  3. #[system]
  4. #[program]
  5. pub mod system_simple_movement {
  6. use super::*;
  7. pub fn execute(ctx: Context<Component>, args_p: Vec<u8>) -> Result<Position> {
  8. let args = parse_args::<Args>(&args_p);
  9. let mut position = Position::from_account_info(&ctx.accounts.position)?;
  10. // Compute the new position based on the direction
  11. let (dx, dy) = match args.direction {
  12. Direction::Left => (-1, 0),
  13. Direction::Right => (1, 0),
  14. Direction::Up => (0, 1),
  15. Direction::Down => (0, -1),
  16. };
  17. position.x += dx;
  18. position.y += dy;
  19. Ok(position)
  20. }
  21. }
  22. // Define the Account to parse from the component
  23. #[derive(Accounts)]
  24. pub struct Component<'info> {
  25. /// CHECK: check that the component is the expected account
  26. pub position: AccountInfo<'info>,
  27. }
  28. #[component_deserialize]
  29. pub struct Position {
  30. pub x: i64,
  31. pub y: i64,
  32. pub z: i64,
  33. }
  34. // Define the structs to deserialize the arguments
  35. #[derive(BoltSerialize, BoltDeserialize)]
  36. struct Args {
  37. direction: Direction,
  38. }
  39. #[derive(BoltSerialize, BoltDeserialize)]
  40. pub enum Direction {
  41. Left,
  42. Right,
  43. Up,
  44. Down,
  45. }