lib.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 account::*;
  4. use anchor_lang::prelude::*;
  5. use context::*;
  6. use event::*;
  7. mod account;
  8. mod context;
  9. mod event;
  10. declare_id!("3TEqcc8xhrhdspwbvoamUJe2borm4Nr72JxL66k6rgrh");
  11. #[program]
  12. pub mod misc {
  13. use super::*;
  14. pub fn initialize(ctx: Context<Initialize>, udata: u128, idata: i128) -> Result<()> {
  15. ctx.accounts.data.udata = udata;
  16. ctx.accounts.data.idata = idata;
  17. Ok(())
  18. }
  19. pub fn initialize_no_rent_exempt(_ctx: Context<InitializeNoRentExempt>) -> Result<()> {
  20. Ok(())
  21. }
  22. pub fn initialize_skip_rent_exempt(_ctx: Context<InitializeSkipRentExempt>) -> Result<()> {
  23. Ok(())
  24. }
  25. pub fn test_owner(_ctx: Context<TestOwner>) -> Result<()> {
  26. Ok(())
  27. }
  28. pub fn test_executable(_ctx: Context<TestExecutable>) -> Result<()> {
  29. Ok(())
  30. }
  31. pub fn test_simulate(_ctx: Context<TestSimulate>, data: u32) -> Result<()> {
  32. emit!(E1 { data });
  33. emit!(E2 { data: 1234 });
  34. emit!(E3 { data: 9 });
  35. emit!(E5 {
  36. data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  37. });
  38. emit!(E6 {
  39. data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  40. });
  41. Ok(())
  42. }
  43. pub fn test_const_array_size(ctx: Context<TestConstArraySize>, data: u8) -> Result<()> {
  44. ctx.accounts.data.data[0] = data;
  45. Ok(())
  46. }
  47. pub fn test_const_ix_data_size(
  48. ctx: Context<TestConstIxDataSize>,
  49. data: [u8; MAX_SIZE],
  50. ) -> Result<()> {
  51. ctx.accounts.data.data = data;
  52. Ok(())
  53. }
  54. pub fn test_close(_ctx: Context<TestClose>) -> Result<()> {
  55. Ok(())
  56. }
  57. pub fn test_close_twice(ctx: Context<TestCloseTwice>) -> Result<()> {
  58. let data_account = &ctx.accounts.data;
  59. let sol_dest_info = ctx.accounts.sol_dest.to_account_info();
  60. data_account.close(sol_dest_info)?;
  61. let data_account_info: &AccountInfo = data_account.as_ref();
  62. require_keys_eq!(*data_account_info.owner, System::id());
  63. Ok(())
  64. }
  65. pub fn test_close_mut(ctx: Context<TestCloseMut>) -> Result<()> {
  66. let data_account = &ctx.accounts.data;
  67. let sol_dest_info = ctx.accounts.sol_dest.to_account_info();
  68. data_account.close(sol_dest_info)?;
  69. let data_account_info: &AccountInfo = data_account.as_ref();
  70. require_keys_eq!(*data_account_info.owner, System::id());
  71. Ok(())
  72. }
  73. pub fn test_instruction_constraint(
  74. _ctx: Context<TestInstructionConstraint>,
  75. _nonce: u8,
  76. ) -> Result<()> {
  77. Ok(())
  78. }
  79. pub fn test_pda_init(
  80. ctx: Context<TestPdaInit>,
  81. _domain: String,
  82. _seed: Vec<u8>,
  83. _bump: u8,
  84. ) -> Result<()> {
  85. ctx.accounts.my_pda.data = 6;
  86. Ok(())
  87. }
  88. pub fn test_pda_init_zero_copy(ctx: Context<TestPdaInitZeroCopy>) -> Result<()> {
  89. let mut acc = ctx.accounts.my_pda.load_init()?;
  90. acc.data = 9;
  91. acc.bump = ctx.bumps.my_pda;
  92. Ok(())
  93. }
  94. pub fn test_pda_mut_zero_copy(ctx: Context<TestPdaMutZeroCopy>) -> Result<()> {
  95. let mut acc = ctx.accounts.my_pda.load_mut()?;
  96. acc.data = 1234;
  97. Ok(())
  98. }
  99. pub fn test_token_seeds_init(_ctx: Context<TestTokenSeedsInit>) -> Result<()> {
  100. Ok(())
  101. }
  102. pub fn default<'info>(
  103. _program_id: &Pubkey,
  104. _accounts: &[AccountInfo<'info>],
  105. _data: &[u8],
  106. ) -> Result<()> {
  107. Err(ProgramError::Custom(1234).into())
  108. }
  109. pub fn test_init(ctx: Context<TestInit>) -> Result<()> {
  110. ctx.accounts.data.data = 3;
  111. Ok(())
  112. }
  113. pub fn test_init_zero_copy(ctx: Context<TestInitZeroCopy>) -> Result<()> {
  114. let mut data = ctx.accounts.data.load_init()?;
  115. data.data = 10;
  116. data.bump = 2;
  117. Ok(())
  118. }
  119. pub fn test_init_mint(ctx: Context<TestInitMint>) -> Result<()> {
  120. assert!(ctx.accounts.mint.decimals == 6);
  121. Ok(())
  122. }
  123. pub fn test_init_mint_with_token_program(
  124. _ctx: Context<TestInitMintWithTokenProgram>,
  125. ) -> Result<()> {
  126. Ok(())
  127. }
  128. pub fn test_init_token(ctx: Context<TestInitToken>) -> Result<()> {
  129. assert!(ctx.accounts.token.mint == ctx.accounts.mint.key());
  130. Ok(())
  131. }
  132. pub fn test_init_token_with_token_program(
  133. _ctx: Context<TestInitTokenWithTokenProgram>,
  134. ) -> Result<()> {
  135. Ok(())
  136. }
  137. pub fn test_composite_payer(ctx: Context<TestCompositePayer>) -> Result<()> {
  138. ctx.accounts.composite.data.data = 1;
  139. ctx.accounts.data.udata = 2;
  140. ctx.accounts.data.idata = 3;
  141. Ok(())
  142. }
  143. pub fn test_init_associated_token(ctx: Context<TestInitAssociatedToken>) -> Result<()> {
  144. assert!(ctx.accounts.token.mint == ctx.accounts.mint.key());
  145. Ok(())
  146. }
  147. pub fn test_init_associated_token_with_token_program(
  148. _ctx: Context<TestInitAssociatedTokenWithTokenProgram>,
  149. ) -> Result<()> {
  150. Ok(())
  151. }
  152. pub fn test_validate_associated_token(
  153. _ctx: Context<TestValidateAssociatedToken>,
  154. ) -> Result<()> {
  155. Ok(())
  156. }
  157. pub fn test_fetch_all(ctx: Context<TestFetchAll>, filterable: Pubkey) -> Result<()> {
  158. ctx.accounts.data.authority = ctx.accounts.authority.key();
  159. ctx.accounts.data.filterable = filterable;
  160. Ok(())
  161. }
  162. pub fn test_init_with_empty_seeds(_ctx: Context<TestInitWithEmptySeeds>) -> Result<()> {
  163. Ok(())
  164. }
  165. pub fn test_empty_seeds_constraint(_ctx: Context<TestEmptySeedsConstraint>) -> Result<()> {
  166. Ok(())
  167. }
  168. pub fn test_init_if_needed(ctx: Context<TestInitIfNeeded>, data: u16) -> Result<()> {
  169. ctx.accounts.data.data = data;
  170. Ok(())
  171. }
  172. pub fn test_init_if_needed_checks_owner(
  173. _ctx: Context<TestInitIfNeededChecksOwner>,
  174. ) -> Result<()> {
  175. Ok(())
  176. }
  177. pub fn test_init_if_needed_checks_seeds(
  178. _ctx: Context<TestInitIfNeededChecksSeeds>,
  179. _seed_data: String,
  180. ) -> Result<()> {
  181. Ok(())
  182. }
  183. pub fn test_init_mint_if_needed(
  184. _ctx: Context<TestInitMintIfNeeded>,
  185. _decimals: u8,
  186. ) -> Result<()> {
  187. Ok(())
  188. }
  189. pub fn test_init_mint_if_needed_with_token_program(
  190. _ctx: Context<TestInitMintIfNeededWithTokenProgram>,
  191. ) -> Result<()> {
  192. Ok(())
  193. }
  194. pub fn test_init_token_if_needed(_ctx: Context<TestInitTokenIfNeeded>) -> Result<()> {
  195. Ok(())
  196. }
  197. pub fn test_init_token_if_needed_with_token_program(
  198. _ctx: Context<TestInitTokenIfNeededWithTokenProgram>,
  199. ) -> Result<()> {
  200. Ok(())
  201. }
  202. pub fn test_init_associated_token_if_needed(
  203. _ctx: Context<TestInitAssociatedTokenIfNeeded>,
  204. ) -> Result<()> {
  205. Ok(())
  206. }
  207. pub fn test_init_associated_token_if_needed_with_token_program(
  208. _ctx: Context<TestInitAssociatedTokenIfNeededWithTokenProgram>,
  209. ) -> Result<()> {
  210. Ok(())
  211. }
  212. pub fn init_with_space(_ctx: Context<InitWithSpace>, _data: u16) -> Result<()> {
  213. Ok(())
  214. }
  215. pub fn test_multidimensional_array(
  216. ctx: Context<TestMultidimensionalArray>,
  217. data: [[u8; 10]; 10],
  218. ) -> Result<()> {
  219. ctx.accounts.data.data = data;
  220. Ok(())
  221. }
  222. pub fn test_multidimensional_array_const_sizes(
  223. ctx: Context<TestMultidimensionalArrayConstSizes>,
  224. data: [[u8; 11]; 10],
  225. ) -> Result<()> {
  226. ctx.accounts.data.data = data;
  227. Ok(())
  228. }
  229. pub fn test_no_rent_exempt(_ctx: Context<NoRentExempt>) -> Result<()> {
  230. Ok(())
  231. }
  232. pub fn test_enforce_rent_exempt(_ctx: Context<EnforceRentExempt>) -> Result<()> {
  233. Ok(())
  234. }
  235. pub fn init_decrease_lamports(ctx: Context<InitDecreaseLamports>) -> Result<()> {
  236. **ctx.accounts.data.try_borrow_mut_lamports()? -= 1;
  237. **ctx.accounts.user.try_borrow_mut_lamports()? += 1;
  238. Ok(())
  239. }
  240. pub fn init_if_needed_checks_rent_exemption(
  241. _ctx: Context<InitIfNeededChecksRentExemption>,
  242. ) -> Result<()> {
  243. Ok(())
  244. }
  245. pub fn test_program_id_constraint(
  246. _ctx: Context<TestProgramIdConstraint>,
  247. _bump: u8,
  248. _second_bump: u8,
  249. ) -> Result<()> {
  250. Ok(())
  251. }
  252. pub fn test_program_id_constraint_find_pda(
  253. _ctx: Context<TestProgramIdConstraintUsingFindPda>,
  254. ) -> Result<()> {
  255. Ok(())
  256. }
  257. pub fn test_token_constraint(_ctx: Context<TestConstraintToken>) -> Result<()> {
  258. Ok(())
  259. }
  260. pub fn test_token_auth_constraint(_ctx: Context<TestAuthorityConstraint>) -> Result<()> {
  261. Ok(())
  262. }
  263. pub fn test_only_auth_constraint(_ctx: Context<TestOnlyAuthorityConstraint>) -> Result<()> {
  264. Ok(())
  265. }
  266. pub fn test_only_mint_constraint(_ctx: Context<TestOnlyMintConstraint>) -> Result<()> {
  267. Ok(())
  268. }
  269. pub fn test_only_token_program_constraint(
  270. _ctx: Context<TestOnlyTokenProgramConstraint>,
  271. ) -> Result<()> {
  272. Ok(())
  273. }
  274. pub fn test_mint_constraint(_ctx: Context<TestMintConstraint>, _decimals: u8) -> Result<()> {
  275. Ok(())
  276. }
  277. pub fn test_mint_only_decimals_constraint(
  278. _ctx: Context<TestMintOnlyDecimalsConstraint>,
  279. _decimals: u8,
  280. ) -> Result<()> {
  281. Ok(())
  282. }
  283. pub fn test_mint_only_auth_constraint(
  284. _ctx: Context<TestMintAuthorityConstraint>,
  285. ) -> Result<()> {
  286. Ok(())
  287. }
  288. pub fn test_mint_only_one_auth_constraint(
  289. _ctx: Context<TestMintOneAuthorityConstraint>,
  290. ) -> Result<()> {
  291. Ok(())
  292. }
  293. pub fn test_mint_miss_mint_auth_constraint(
  294. _ctx: Context<TestMintMissMintAuthConstraint>,
  295. _decimals: u8,
  296. ) -> Result<()> {
  297. Ok(())
  298. }
  299. pub fn test_mint_only_token_program_constraint(
  300. _ctx: Context<TestMintOnlyTokenProgramConstraint>,
  301. ) -> Result<()> {
  302. Ok(())
  303. }
  304. pub fn test_associated_constraint(_ctx: Context<TestAssociatedToken>) -> Result<()> {
  305. Ok(())
  306. }
  307. pub fn test_associated_token_with_token_program_constraint(
  308. _ctx: Context<TestAssociatedTokenWithTokenProgramConstraint>,
  309. ) -> Result<()> {
  310. Ok(())
  311. }
  312. #[allow(unused_variables)]
  313. pub fn test_used_identifiers(
  314. _ctx: Context<TestUsedIdentifiers>,
  315. program_id: u8,
  316. accounts: u8,
  317. ix_data: u8,
  318. remaining_accounts: u8,
  319. ) -> Result<()> {
  320. Ok(())
  321. }
  322. pub fn test_init_many_associated_token_accounts(
  323. _ctx: Context<InitManyAssociatedTokenAccounts>,
  324. ) -> Result<()> {
  325. Ok(())
  326. }
  327. /// Compilation test for https://github.com/coral-xyz/anchor/issues/3074
  328. pub fn test_boxed_owner_constraint(_ctx: Context<TestBoxedOwnerConstraint>) -> Result<()> {
  329. Ok(())
  330. }
  331. #[cfg(feature = "my-feature")]
  332. pub fn only_my_feature(_ctx: Context<Empty>) -> Result<()> {
  333. Ok(())
  334. }
  335. pub fn test_multiple_zero_constraint(_ctx: Context<TestMultipleZeroConstraint>) -> Result<()> {
  336. Ok(())
  337. }
  338. pub fn test_init_and_zero(_ctx: Context<TestInitAndZero>) -> Result<()> {
  339. Ok(())
  340. }
  341. }