lib.rs 1.2 KB

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