create_amm.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. use steel::*;
  2. use token_swap_api::prelude::*;
  3. pub fn process_create_amm(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
  4. // Load accounts.
  5. let [payer_info, admin_info, amm_info, system_program] = accounts else {
  6. return Err(ProgramError::NotEnoughAccountKeys);
  7. };
  8. let args = CreateAmm::try_from_bytes(data)?;
  9. let id = args.id;
  10. let fee = args.fee;
  11. // Check fee is valid.
  12. if u16::from_le_bytes(fee) > 10000 {
  13. return Err(TokenSwapError::InvalidFee.into());
  14. }
  15. // check payer is signer of the transaction
  16. payer_info.is_signer()?;
  17. amm_info
  18. .is_empty()?
  19. .is_writable()?
  20. .has_seeds(&[id.as_ref()], &token_swap_api::ID)?;
  21. system_program.is_program(&system_program::ID)?;
  22. // Initialize amm account.
  23. create_account::<Amm>(
  24. amm_info,
  25. system_program,
  26. payer_info,
  27. &token_swap_api::ID,
  28. &[id.as_ref()],
  29. )?;
  30. let amm = amm_info.as_account_mut::<Amm>(&token_swap_api::ID)?;
  31. amm.admin = *admin_info.key;
  32. amm.id = id;
  33. amm.fee = fee;
  34. Ok(())
  35. }