lib.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_token(ctx: Context<TestInitToken>) -> Result<()> {
  145. assert!(
  146. ctx.accounts.token.as_ref().unwrap().mint == ctx.accounts.mint.as_ref().unwrap().key()
  147. );
  148. Ok(())
  149. }
  150. pub fn test_composite_payer(ctx: Context<TestCompositePayer>) -> Result<()> {
  151. ctx.accounts.composite.data.as_mut().unwrap().data = 1;
  152. ctx.accounts.data.as_mut().unwrap().udata = 2;
  153. ctx.accounts.data.as_mut().unwrap().idata = 3;
  154. Ok(())
  155. }
  156. pub fn test_init_associated_token(ctx: Context<TestInitAssociatedToken>) -> Result<()> {
  157. assert!(
  158. ctx.accounts.token.as_ref().unwrap().mint == ctx.accounts.mint.as_ref().unwrap().key()
  159. );
  160. Ok(())
  161. }
  162. pub fn test_validate_associated_token(
  163. _ctx: Context<TestValidateAssociatedToken>,
  164. ) -> Result<()> {
  165. Ok(())
  166. }
  167. pub fn test_fetch_all(ctx: Context<TestFetchAll>, filterable: Pubkey) -> Result<()> {
  168. ctx.accounts.data.as_mut().unwrap().authority =
  169. ctx.accounts.authority.as_ref().unwrap().key();
  170. ctx.accounts.data.as_mut().unwrap().filterable = filterable;
  171. Ok(())
  172. }
  173. pub fn test_init_with_empty_seeds(_ctx: Context<TestInitWithEmptySeeds>) -> Result<()> {
  174. Ok(())
  175. }
  176. pub fn test_empty_seeds_constraint(_ctx: Context<TestEmptySeedsConstraint>) -> Result<()> {
  177. Ok(())
  178. }
  179. pub fn test_init_if_needed(ctx: Context<TestInitIfNeeded>, data: u16) -> Result<()> {
  180. ctx.accounts.data.as_mut().unwrap().data = data;
  181. Ok(())
  182. }
  183. pub fn test_init_if_needed_checks_owner(
  184. _ctx: Context<TestInitIfNeededChecksOwner>,
  185. ) -> Result<()> {
  186. Ok(())
  187. }
  188. pub fn test_init_if_needed_checks_seeds(
  189. _ctx: Context<TestInitIfNeededChecksSeeds>,
  190. _seed_data: String,
  191. ) -> Result<()> {
  192. Ok(())
  193. }
  194. pub fn test_init_mint_if_needed(
  195. _ctx: Context<TestInitMintIfNeeded>,
  196. _decimals: u8,
  197. ) -> Result<()> {
  198. Ok(())
  199. }
  200. pub fn test_init_token_if_needed(_ctx: Context<TestInitTokenIfNeeded>) -> Result<()> {
  201. Ok(())
  202. }
  203. pub fn test_init_associated_token_if_needed(
  204. _ctx: Context<TestInitAssociatedTokenIfNeeded>,
  205. ) -> Result<()> {
  206. Ok(())
  207. }
  208. pub fn init_with_space(_ctx: Context<InitWithSpace>, data: u16) -> Result<()> {
  209. Ok(())
  210. }
  211. pub fn test_multidimensional_array(
  212. ctx: Context<TestMultidimensionalArray>,
  213. data: [[u8; 10]; 10],
  214. ) -> Result<()> {
  215. ctx.accounts.data.as_mut().unwrap().data = data;
  216. Ok(())
  217. }
  218. pub fn test_multidimensional_array_const_sizes(
  219. ctx: Context<TestMultidimensionalArrayConstSizes>,
  220. data: [[u8; 11]; 10],
  221. ) -> Result<()> {
  222. ctx.accounts.data.as_mut().unwrap().data = data;
  223. Ok(())
  224. }
  225. pub fn test_no_rent_exempt(_ctx: Context<NoRentExempt>) -> Result<()> {
  226. Ok(())
  227. }
  228. pub fn test_enforce_rent_exempt(_ctx: Context<EnforceRentExempt>) -> Result<()> {
  229. Ok(())
  230. }
  231. pub fn init_decrease_lamports(ctx: Context<InitDecreaseLamports>) -> Result<()> {
  232. **ctx
  233. .accounts
  234. .data
  235. .as_mut()
  236. .unwrap()
  237. .try_borrow_mut_lamports()? -= 1;
  238. **ctx
  239. .accounts
  240. .user
  241. .as_mut()
  242. .unwrap()
  243. .try_borrow_mut_lamports()? += 1;
  244. Ok(())
  245. }
  246. pub fn init_if_needed_checks_rent_exemption(
  247. _ctx: Context<InitIfNeededChecksRentExemption>,
  248. ) -> Result<()> {
  249. Ok(())
  250. }
  251. pub fn test_program_id_constraint(
  252. _ctx: Context<TestProgramIdConstraint>,
  253. _bump: u8,
  254. _second_bump: u8,
  255. ) -> Result<()> {
  256. Ok(())
  257. }
  258. pub fn test_program_id_constraint_find_pda(
  259. _ctx: Context<TestProgramIdConstraintUsingFindPda>,
  260. ) -> Result<()> {
  261. Ok(())
  262. }
  263. pub fn test_token_constraint(_ctx: Context<TestConstraintToken>) -> Result<()> {
  264. Ok(())
  265. }
  266. pub fn test_token_auth_constraint(_ctx: Context<TestAuthorityConstraint>) -> Result<()> {
  267. Ok(())
  268. }
  269. pub fn test_only_auth_constraint(_ctx: Context<TestOnlyAuthorityConstraint>) -> Result<()> {
  270. Ok(())
  271. }
  272. pub fn test_only_mint_constraint(_ctx: Context<TestOnlyMintConstraint>) -> Result<()> {
  273. Ok(())
  274. }
  275. pub fn test_mint_constraint(_ctx: Context<TestMintConstraint>, _decimals: u8) -> Result<()> {
  276. Ok(())
  277. }
  278. pub fn test_mint_only_decimals_constraint(
  279. _ctx: Context<TestMintOnlyDecimalsConstraint>,
  280. _decimals: u8,
  281. ) -> Result<()> {
  282. Ok(())
  283. }
  284. pub fn test_mint_only_auth_constraint(
  285. _ctx: Context<TestMintAuthorityConstraint>,
  286. ) -> Result<()> {
  287. Ok(())
  288. }
  289. pub fn test_mint_only_one_auth_constraint(
  290. _ctx: Context<TestMintOneAuthorityConstraint>,
  291. ) -> Result<()> {
  292. Ok(())
  293. }
  294. pub fn test_mint_miss_mint_auth_constraint(
  295. _ctx: Context<TestMintMissMintAuthConstraint>,
  296. _decimals: u8,
  297. ) -> Result<()> {
  298. Ok(())
  299. }
  300. pub fn test_associated_constraint(_ctx: Context<TestAssociatedToken>) -> Result<()> {
  301. Ok(())
  302. }
  303. }