increment.rs 766 B

12345678910111213141516171819202122232425262728
  1. use counter_api::prelude::*;
  2. use steel::*;
  3. use solana_program::msg;
  4. pub fn process_increment(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
  5. msg!("Processing Increment instruction");
  6. // Parse args.
  7. let args = Increment::try_from_bytes(data)?;
  8. let amount = u64::from_le_bytes(args.amount);
  9. msg!("Parsed amount: {}", amount);
  10. // Load accounts.
  11. let [signer_info, counter_info] = accounts else {
  12. return Err(ProgramError::NotEnoughAccountKeys);
  13. };
  14. signer_info.is_signer()?;
  15. let counter = counter_info
  16. .as_account_mut::<Counter>(&counter_api::ID)?
  17. .assert_mut(|c| c.value < 100)?;
  18. // Update state
  19. counter.value += amount;
  20. msg!("Final amount: {}", counter.value);
  21. Ok(())
  22. }