lib.rs 11 KB

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