lib.rs 872 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //! This tests that the `allow-missing-optionals` feature works
  2. use anchor_lang::prelude::*;
  3. declare_id!("ErjUjtqKE5AGWUsjseSJCVLtddM6rhaMbDqmhzraF9h6");
  4. #[program]
  5. mod allow_missing_optionals {
  6. use super::*;
  7. pub fn do_stuff(ctx: Context<DoStuff>) -> Result<()> {
  8. msg!("Doing stuff...");
  9. let optional_2 = &mut ctx.accounts.optional_2;
  10. if let Some(data_account) = optional_2 {
  11. data_account.data = 42;
  12. }
  13. Ok(())
  14. }
  15. }
  16. #[account]
  17. pub struct DataAccount {
  18. pub data: u64,
  19. }
  20. impl DataAccount {
  21. pub const LEN: usize = 8 + 8;
  22. }
  23. #[derive(Accounts)]
  24. pub struct DoStuff<'info> {
  25. #[account(mut)]
  26. pub payer: Signer<'info>,
  27. pub system_program: Option<Program<'info, System>>,
  28. #[account(init, payer = payer, space = DataAccount::LEN)]
  29. pub optional_2: Option<Account<'info, DataAccount>>,
  30. }