lib.rs 9.4 KB

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