lib.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. use anchor_lang::prelude::*;
  2. declare_id!("BZoppwWi6jMnydnUBEJzotgEXHwLr3b3NramJgZtWeF2");
  3. #[program]
  4. pub mod init_if_needed {
  5. use super::*;
  6. // _val only used to make tx different so that it doesn't result
  7. // in dup tx error
  8. pub fn initialize(ctx: Context<Initialize>, _val: u8) -> Result<()> {
  9. ctx.accounts.acc.val = 1000;
  10. Ok(())
  11. }
  12. pub fn second_initialize(ctx: Context<SecondInitialize>, _val: u8) -> Result<()> {
  13. ctx.accounts.acc.other_val = 2000;
  14. Ok(())
  15. }
  16. pub fn close(ctx: Context<Close>) -> Result<()> {
  17. ctx.accounts.acc.val = 5000;
  18. Ok(())
  19. }
  20. }
  21. #[account]
  22. pub struct MyData {
  23. pub val: u64,
  24. }
  25. #[account]
  26. pub struct OtherData {
  27. pub other_val: u64,
  28. }
  29. #[derive(Accounts)]
  30. pub struct Initialize<'info> {
  31. #[account(init_if_needed, payer = payer, space = 8 + 8)]
  32. pub acc: Account<'info, MyData>,
  33. #[account(mut)]
  34. pub payer: Signer<'info>,
  35. pub system_program: Program<'info, System>,
  36. }
  37. #[derive(Accounts)]
  38. pub struct SecondInitialize<'info> {
  39. #[account(init, payer = payer, space = 8 + 8)]
  40. pub acc: Account<'info, OtherData>,
  41. #[account(mut)]
  42. pub payer: Signer<'info>,
  43. pub system_program: Program<'info, System>,
  44. }
  45. #[derive(Accounts)]
  46. pub struct Close<'info> {
  47. #[account(mut, close = receiver)]
  48. pub acc: Account<'info, MyData>,
  49. #[account(mut)]
  50. pub receiver: UncheckedAccount<'info>,
  51. }