lib.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. use anchor_lang::prelude::*;
  2. declare_id!("Externa111111111111111111111111111111111111");
  3. #[program]
  4. pub mod external {
  5. use super::*;
  6. pub fn init(_ctx: Context<Init>) -> Result<()> {
  7. Ok(())
  8. }
  9. pub fn update(ctx: Context<Update>, value: u32) -> Result<()> {
  10. ctx.accounts.my_account.field = value;
  11. Ok(())
  12. }
  13. pub fn update_composite(ctx: Context<UpdateComposite>, value: u32) -> Result<()> {
  14. ctx.accounts.update.my_account.field = value;
  15. Ok(())
  16. }
  17. // Compilation test for whether a defined type (an account in this case) can be used in `cpi` client.
  18. pub fn test_compilation_defined_type_param(
  19. _ctx: Context<TestCompilation>,
  20. _my_account: MyAccount,
  21. ) -> Result<()> {
  22. Ok(())
  23. }
  24. // Compilation test for whether a custom return type can be specified in `cpi` client
  25. pub fn test_compilation_return_type(_ctx: Context<TestCompilation>) -> Result<bool> {
  26. Ok(true)
  27. }
  28. }
  29. #[derive(Accounts)]
  30. pub struct TestCompilation<'info> {
  31. pub signer: Signer<'info>,
  32. }
  33. #[derive(Accounts)]
  34. pub struct Init<'info> {
  35. #[account(mut)]
  36. pub authority: Signer<'info>,
  37. #[account(
  38. init,
  39. payer = authority,
  40. space = 8 + 4,
  41. seeds = [authority.key.as_ref()],
  42. bump
  43. )]
  44. pub my_account: Account<'info, MyAccount>,
  45. pub system_program: Program<'info, System>,
  46. }
  47. #[derive(Accounts)]
  48. pub struct Update<'info> {
  49. pub authority: Signer<'info>,
  50. #[account(mut, seeds = [authority.key.as_ref()], bump)]
  51. pub my_account: Account<'info, MyAccount>,
  52. }
  53. #[derive(Accounts)]
  54. pub struct UpdateComposite<'info> {
  55. pub update: Update<'info>,
  56. }
  57. #[account]
  58. pub struct MyAccount {
  59. pub field: u32,
  60. }
  61. #[event]
  62. pub struct MyEvent {
  63. pub value: u32,
  64. }