lib.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. use anchor_lang::prelude::*;
  2. declare_id!("Externa111111111111111111111111111111111111");
  3. /// Master seed slice
  4. #[constant]
  5. pub const MASTER_SEED: &[u8] = b"master";
  6. #[program]
  7. pub mod external {
  8. use super::*;
  9. pub fn init(_ctx: Context<Init>) -> Result<()> {
  10. Ok(())
  11. }
  12. pub fn update(ctx: Context<Update>, value: u32) -> Result<()> {
  13. ctx.accounts.my_account.field = value;
  14. Ok(())
  15. }
  16. pub fn update_composite(ctx: Context<UpdateComposite>, value: u32) -> Result<()> {
  17. ctx.accounts.update.my_account.field = value;
  18. Ok(())
  19. }
  20. // Test the issue described in https://github.com/coral-xyz/anchor/issues/3274
  21. pub fn update_non_instruction_composite(
  22. ctx: Context<UpdateNonInstructionComposite>,
  23. value: u32,
  24. ) -> Result<()> {
  25. ctx.accounts.non_instruction_update.my_account.field = value;
  26. Ok(())
  27. }
  28. // Compilation test for whether a defined type (an account in this case) can be used in `cpi` client.
  29. pub fn test_compilation_defined_type_param(
  30. _ctx: Context<TestCompilation>,
  31. _my_account: MyAccount,
  32. ) -> Result<()> {
  33. Ok(())
  34. }
  35. // Compilation test for whether a custom return type can be specified in `cpi` client
  36. pub fn test_compilation_return_type(_ctx: Context<TestCompilation>) -> Result<bool> {
  37. Ok(true)
  38. }
  39. }
  40. #[derive(Accounts)]
  41. pub struct TestCompilation<'info> {
  42. pub signer: Signer<'info>,
  43. }
  44. #[derive(Accounts)]
  45. pub struct Init<'info> {
  46. #[account(mut)]
  47. pub authority: Signer<'info>,
  48. #[account(
  49. init,
  50. payer = authority,
  51. space = 8 + 4,
  52. seeds = [authority.key.as_ref()],
  53. bump
  54. )]
  55. pub my_account: Account<'info, MyAccount>,
  56. pub system_program: Program<'info, System>,
  57. }
  58. #[derive(Accounts)]
  59. pub struct Update<'info> {
  60. pub authority: Signer<'info>,
  61. #[account(mut, seeds = [authority.key.as_ref()], bump)]
  62. pub my_account: Account<'info, MyAccount>,
  63. }
  64. #[derive(Accounts)]
  65. pub struct NonInstructionUpdate<'info> {
  66. pub authority: Signer<'info>,
  67. #[account(mut, seeds = [authority.key.as_ref()], bump)]
  68. pub my_account: Account<'info, MyAccount>,
  69. pub program: Program<'info, program::External>,
  70. }
  71. #[derive(Accounts)]
  72. pub struct UpdateComposite<'info> {
  73. pub update: Update<'info>,
  74. }
  75. #[derive(Accounts)]
  76. pub struct UpdateNonInstructionComposite<'info> {
  77. pub non_instruction_update: NonInstructionUpdate<'info>,
  78. }
  79. #[account]
  80. pub struct MyAccount {
  81. pub field: u32,
  82. }
  83. #[event]
  84. pub struct MyEvent {
  85. pub value: u32,
  86. }