processor.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. use borsh::{BorshDeserialize, BorshSerialize};
  2. use solana_program::{
  3. account_info::AccountInfo,
  4. entrypoint,
  5. entrypoint::ProgramResult,
  6. msg,
  7. program_error::ProgramError,
  8. pubkey::Pubkey,
  9. };
  10. use crate::instructions::{
  11. get_on_ride,
  12. play_game,
  13. eat_food,
  14. };
  15. // For processing everything at the entrypoint
  16. entrypoint!(process_instruction);
  17. #[derive(BorshSerialize, BorshDeserialize, Debug)]
  18. pub struct CarnivalInstructionData {
  19. pub name: String,
  20. pub height: u32,
  21. pub ticket_count: u32,
  22. pub attraction: String,
  23. pub attraction_name: String,
  24. }
  25. pub fn process_instruction(
  26. _program_id: &Pubkey,
  27. _accounts: &[AccountInfo],
  28. instruction_data: &[u8],
  29. ) -> ProgramResult {
  30. let ix_data_object = CarnivalInstructionData::try_from_slice(&instruction_data)?;
  31. msg!("Welcome to the carnival, {}!", ix_data_object.name);
  32. match ix_data_object.attraction.as_str() {
  33. "ride" => get_on_ride::get_on_ride(get_on_ride::GetOnRideInstructionData {
  34. rider_name: ix_data_object.name,
  35. rider_height: ix_data_object.height,
  36. rider_ticket_count: ix_data_object.ticket_count,
  37. ride: ix_data_object.attraction_name,
  38. }),
  39. "game" => play_game::play_game(play_game::PlayGameInstructionData {
  40. gamer_name: ix_data_object.name,
  41. gamer_ticket_count: ix_data_object.ticket_count,
  42. game: ix_data_object.attraction_name,
  43. }),
  44. "food" => eat_food::eat_food(eat_food::EatFoodInstructionData {
  45. eater_name: ix_data_object.name,
  46. eater_ticket_count: ix_data_object.ticket_count,
  47. food_stand: ix_data_object.attraction_name,
  48. }),
  49. _ => Err(ProgramError::InvalidInstructionData),
  50. }
  51. }