add.rs 585 B

12345678910111213141516171819202122
  1. use realloc_api::prelude::*;
  2. use steel::*;
  3. pub fn process_add(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
  4. // Parse args.
  5. let args = Add::try_from_bytes(data)?;
  6. let amount = u64::from_le_bytes(args.amount);
  7. // Load accounts.
  8. let [signer_info, counter_info] = accounts else {
  9. return Err(ProgramError::NotEnoughAccountKeys);
  10. };
  11. signer_info.is_signer()?;
  12. let counter = counter_info
  13. .as_account_mut::<Counter>(&realloc_api::ID)?
  14. .assert_mut(|c| c.value < 100)?;
  15. // Update state
  16. counter.value += amount;
  17. Ok(())
  18. }