lib.rs 1.2 KB

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