processor.rs 1.7 KB

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