lib.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use bolt_lang::*;
  2. declare_id!("FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA");
  3. #[system]
  4. pub mod system_simple_movement {
  5. pub fn execute(ctx: Context<Components>, args_p: Vec<u8>) -> Result<Components> {
  6. let args = parse_args::<Args>(&args_p);
  7. // Compute the new position based on the direction
  8. let (dx, dy) = match args.direction {
  9. Direction::Left => (-1, 0),
  10. Direction::Right => (1, 0),
  11. Direction::Up => (0, 1),
  12. Direction::Down => (0, -1),
  13. };
  14. ctx.accounts.position.x += dx;
  15. ctx.accounts.position.y += dy;
  16. Ok(ctx.accounts)
  17. }
  18. #[system_input]
  19. pub struct Components {
  20. #[component_id("Fn1JzzEdyb55fsyduWS94mYHizGhJZuhvjX6DVvrmGbQ")]
  21. pub position: Position,
  22. }
  23. // Define the structs to deserialize the arguments
  24. #[derive(serde::Serialize, serde::Deserialize)]
  25. struct Args {
  26. direction: Direction,
  27. }
  28. #[derive(serde::Serialize, serde::Deserialize)]
  29. pub enum Direction {
  30. Left,
  31. Right,
  32. Up,
  33. Down,
  34. }
  35. }