lib.rs 11 KB

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