lib.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //! Misc example is a catchall program for testing unrelated features.
  2. //! It's not too instructive/coherent by itself, so please see other examples.
  3. use anchor_lang::prelude::*;
  4. use misc2::misc2::MyState as Misc2State;
  5. use misc2::Auth;
  6. #[program]
  7. pub mod misc {
  8. use super::*;
  9. pub const SIZE: u64 = 99;
  10. #[state(SIZE)]
  11. pub struct MyState {
  12. pub v: Vec<u8>,
  13. }
  14. impl MyState {
  15. pub fn new(_ctx: Context<Ctor>) -> Result<Self, ProgramError> {
  16. Ok(Self { v: vec![] })
  17. }
  18. pub fn remaining_accounts(&mut self, ctx: Context<RemainingAccounts>) -> ProgramResult {
  19. if ctx.remaining_accounts.len() != 1 {
  20. return Err(ProgramError::Custom(1)); // Arbitrary error.
  21. }
  22. Ok(())
  23. }
  24. }
  25. pub fn initialize(ctx: Context<Initialize>, udata: u128, idata: i128) -> ProgramResult {
  26. ctx.accounts.data.udata = udata;
  27. ctx.accounts.data.idata = idata;
  28. Ok(())
  29. }
  30. pub fn test_owner(_ctx: Context<TestOwner>) -> ProgramResult {
  31. Ok(())
  32. }
  33. pub fn test_executable(_ctx: Context<TestExecutable>) -> ProgramResult {
  34. Ok(())
  35. }
  36. pub fn test_state_cpi(ctx: Context<TestStateCpi>, data: u64) -> ProgramResult {
  37. let cpi_program = ctx.accounts.misc2_program.clone();
  38. let cpi_accounts = Auth {
  39. authority: ctx.accounts.authority.clone(),
  40. };
  41. let ctx = ctx.accounts.cpi_state.context(cpi_program, cpi_accounts);
  42. misc2::cpi::state::set_data(ctx, data)
  43. }
  44. pub fn test_init_associated_account(
  45. ctx: Context<TestInitAssociatedAccount>,
  46. data: u64,
  47. ) -> ProgramResult {
  48. ctx.accounts.my_account.data = data;
  49. Ok(())
  50. }
  51. pub fn test_associated_account(
  52. ctx: Context<TestAssociatedAccount>,
  53. data: u64,
  54. ) -> ProgramResult {
  55. ctx.accounts.my_account.data = data;
  56. Ok(())
  57. }
  58. pub fn test_u16(ctx: Context<TestU16>, data: u16) -> ProgramResult {
  59. ctx.accounts.my_account.data = data;
  60. Ok(())
  61. }
  62. pub fn test_simulate(_ctx: Context<TestSimulate>, data: u32) -> ProgramResult {
  63. emit!(E1 { data });
  64. emit!(E2 { data: 1234 });
  65. emit!(E3 { data: 9 });
  66. Ok(())
  67. }
  68. pub fn test_i8(ctx: Context<TestI8>, data: i8) -> ProgramResult {
  69. ctx.accounts.data.data = data;
  70. Ok(())
  71. }
  72. pub fn test_i16(ctx: Context<TestI16>, data: i16) -> ProgramResult {
  73. ctx.accounts.data.data = data;
  74. Ok(())
  75. }
  76. pub fn test_close(_ctx: Context<TestClose>) -> ProgramResult {
  77. Ok(())
  78. }
  79. pub fn test_instruction_constraint(
  80. _ctx: Context<TestInstructionConstraint>,
  81. _nonce: u8,
  82. ) -> ProgramResult {
  83. Ok(())
  84. }
  85. pub fn test_pda_init(
  86. ctx: Context<TestPdaInit>,
  87. _domain: String,
  88. _seed: Vec<u8>,
  89. _bump: u8,
  90. ) -> ProgramResult {
  91. ctx.accounts.my_pda.data = 6;
  92. Ok(())
  93. }
  94. pub fn test_pda_init_zero_copy(ctx: Context<TestPdaInitZeroCopy>, bump: u8) -> ProgramResult {
  95. let mut acc = ctx.accounts.my_pda.load_init()?;
  96. acc.data = 9;
  97. acc.bump = bump;
  98. Ok(())
  99. }
  100. pub fn test_pda_mut_zero_copy(ctx: Context<TestPdaMutZeroCopy>) -> ProgramResult {
  101. let mut acc = ctx.accounts.my_pda.load_mut()?;
  102. acc.data = 1234;
  103. Ok(())
  104. }
  105. }
  106. #[derive(Accounts)]
  107. #[instruction(nonce: u8)]
  108. pub struct TestInstructionConstraint<'info> {
  109. #[account(seeds = [b"my-seed", my_account.key.as_ref(), &[nonce]])]
  110. pub my_pda: AccountInfo<'info>,
  111. pub my_account: AccountInfo<'info>,
  112. }
  113. #[derive(Accounts)]
  114. #[instruction(domain: String, seed: Vec<u8>, bump: u8)]
  115. pub struct TestPdaInit<'info> {
  116. #[account(
  117. init,
  118. seeds = [b"my-seed", domain.as_bytes(), foo.key.as_ref(), &seed, &[bump]],
  119. payer = my_payer,
  120. )]
  121. my_pda: ProgramAccount<'info, DataU16>,
  122. my_payer: AccountInfo<'info>,
  123. foo: AccountInfo<'info>,
  124. rent: Sysvar<'info, Rent>,
  125. system_program: AccountInfo<'info>,
  126. }
  127. #[derive(Accounts)]
  128. #[instruction(bump: u8)]
  129. pub struct TestPdaInitZeroCopy<'info> {
  130. #[account(init, seeds = [b"my-seed".as_ref(), &[bump]], payer = my_payer)]
  131. my_pda: Loader<'info, DataZeroCopy>,
  132. my_payer: AccountInfo<'info>,
  133. rent: Sysvar<'info, Rent>,
  134. system_program: AccountInfo<'info>,
  135. }
  136. #[derive(Accounts)]
  137. pub struct TestPdaMutZeroCopy<'info> {
  138. #[account(mut, seeds = [b"my-seed".as_ref(), &[my_pda.load()?.bump]])]
  139. my_pda: Loader<'info, DataZeroCopy>,
  140. my_payer: AccountInfo<'info>,
  141. }
  142. #[derive(Accounts)]
  143. pub struct Ctor {}
  144. #[derive(Accounts)]
  145. pub struct RemainingAccounts {}
  146. #[derive(Accounts)]
  147. pub struct Initialize<'info> {
  148. #[account(init)]
  149. data: ProgramAccount<'info, Data>,
  150. rent: Sysvar<'info, Rent>,
  151. }
  152. #[derive(Accounts)]
  153. pub struct TestOwner<'info> {
  154. #[account(owner = misc)]
  155. data: AccountInfo<'info>,
  156. misc: AccountInfo<'info>,
  157. }
  158. #[derive(Accounts)]
  159. pub struct TestExecutable<'info> {
  160. #[account(executable)]
  161. program: AccountInfo<'info>,
  162. }
  163. #[derive(Accounts)]
  164. pub struct TestStateCpi<'info> {
  165. #[account(signer)]
  166. authority: AccountInfo<'info>,
  167. #[account(mut, state = misc2_program)]
  168. cpi_state: CpiState<'info, Misc2State>,
  169. #[account(executable)]
  170. misc2_program: AccountInfo<'info>,
  171. }
  172. #[derive(Accounts)]
  173. pub struct TestClose<'info> {
  174. #[account(mut, close = sol_dest)]
  175. data: ProgramAccount<'info, Data>,
  176. sol_dest: AccountInfo<'info>,
  177. }
  178. // `my_account` is the associated token account being created.
  179. // `authority` must be a `mut` and `signer` since it will pay for the creation
  180. // of the associated token account. `state` is used as an association, i.e., one
  181. // can *optionally* identify targets to be used as seeds for the program
  182. // derived address by using `with` (and it doesn't have to be a state account).
  183. // For example, the SPL token program uses a `Mint` account. Lastly,
  184. // `rent` and `system_program` are *required* by convention, since the
  185. // accounts are needed when creating the associated program address within
  186. // the program.
  187. #[derive(Accounts)]
  188. pub struct TestInitAssociatedAccount<'info> {
  189. #[account(init, associated = authority, with = state, with = data)]
  190. my_account: ProgramAccount<'info, TestData>,
  191. #[account(mut, signer)]
  192. authority: AccountInfo<'info>,
  193. state: ProgramState<'info, MyState>,
  194. data: ProgramAccount<'info, Data>,
  195. rent: Sysvar<'info, Rent>,
  196. system_program: AccountInfo<'info>,
  197. }
  198. #[derive(Accounts)]
  199. pub struct TestAssociatedAccount<'info> {
  200. #[account(mut, associated = authority, with = state, with = data)]
  201. my_account: ProgramAccount<'info, TestData>,
  202. #[account(mut, signer)]
  203. authority: AccountInfo<'info>,
  204. state: ProgramState<'info, MyState>,
  205. data: ProgramAccount<'info, Data>,
  206. }
  207. #[derive(Accounts)]
  208. pub struct TestU16<'info> {
  209. #[account(init)]
  210. my_account: ProgramAccount<'info, DataU16>,
  211. rent: Sysvar<'info, Rent>,
  212. }
  213. #[derive(Accounts)]
  214. pub struct TestI16<'info> {
  215. #[account(init)]
  216. data: ProgramAccount<'info, DataI16>,
  217. rent: Sysvar<'info, Rent>,
  218. }
  219. #[derive(Accounts)]
  220. pub struct TestSimulate {}
  221. #[derive(Accounts)]
  222. pub struct TestI8<'info> {
  223. #[account(init)]
  224. data: ProgramAccount<'info, DataI8>,
  225. rent: Sysvar<'info, Rent>,
  226. }
  227. #[associated]
  228. #[derive(Default)]
  229. pub struct TestData {
  230. data: u64,
  231. }
  232. #[account]
  233. pub struct Data {
  234. udata: u128,
  235. idata: i128,
  236. }
  237. #[account]
  238. #[derive(Default)]
  239. pub struct DataU16 {
  240. data: u16,
  241. }
  242. #[account]
  243. pub struct DataI8 {
  244. data: i8,
  245. }
  246. #[account]
  247. pub struct DataI16 {
  248. data: i16,
  249. }
  250. #[account(zero_copy)]
  251. #[derive(Default)]
  252. pub struct DataZeroCopy {
  253. data: u16,
  254. bump: u8,
  255. }
  256. #[event]
  257. pub struct E1 {
  258. data: u32,
  259. }
  260. #[event]
  261. pub struct E2 {
  262. data: u32,
  263. }
  264. #[event]
  265. pub struct E3 {
  266. data: u32,
  267. }