context.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. use crate::account::*;
  2. use anchor_lang::accounts::loader::Loader;
  3. use anchor_lang::prelude::*;
  4. use anchor_spl::associated_token::AssociatedToken;
  5. use anchor_spl::token::{Mint, Token, TokenAccount};
  6. #[derive(Accounts)]
  7. pub struct TestTokenSeedsInit<'info> {
  8. #[account(
  9. init,
  10. seeds = [b"my-mint-seed".as_ref()],
  11. bump,
  12. payer = authority,
  13. mint::decimals = 6,
  14. mint::authority = authority,
  15. )]
  16. pub mint: Option<Account<'info, Mint>>,
  17. #[account(
  18. init,
  19. seeds = [b"my-token-seed".as_ref()],
  20. bump,
  21. payer = authority,
  22. token::mint = mint,
  23. token::authority = authority,
  24. )]
  25. pub my_pda: Option<Account<'info, TokenAccount>>,
  26. #[account(mut)]
  27. /// CHECK:
  28. pub authority: Option<AccountInfo<'info>>,
  29. pub system_program: Option<Program<'info, System>>,
  30. pub token_program: Option<Program<'info, Token>>,
  31. }
  32. #[derive(Accounts)]
  33. pub struct TestInitAssociatedToken<'info> {
  34. #[account(
  35. init,
  36. associated_token::mint = mint,
  37. payer = payer,
  38. associated_token::authority = payer,
  39. )]
  40. pub token: Option<Account<'info, TokenAccount>>,
  41. pub mint: Option<Account<'info, Mint>>,
  42. #[account(mut)]
  43. pub payer: Option<Signer<'info>>,
  44. pub system_program: Option<Program<'info, System>>,
  45. pub token_program: Option<Program<'info, Token>>,
  46. pub associated_token_program: Option<Program<'info, AssociatedToken>>,
  47. }
  48. #[derive(Accounts)]
  49. pub struct TestValidateAssociatedToken<'info> {
  50. #[account(
  51. associated_token::mint = mint,
  52. associated_token::authority = wallet,
  53. )]
  54. pub token: Option<Account<'info, TokenAccount>>,
  55. pub mint: Option<Account<'info, Mint>>,
  56. /// CHECK:
  57. pub wallet: Option<AccountInfo<'info>>,
  58. }
  59. #[derive(Accounts)]
  60. #[instruction(nonce: u8)]
  61. pub struct TestInstructionConstraint<'info> {
  62. #[account(
  63. seeds = [b"my-seed", my_account.as_ref().unwrap().key.as_ref()],
  64. bump = nonce,
  65. )]
  66. /// CHECK:
  67. pub my_pda: Option<AccountInfo<'info>>,
  68. /// CHECK:
  69. pub my_account: Option<AccountInfo<'info>>,
  70. }
  71. #[derive(Accounts)]
  72. #[instruction(domain: String, seed: Vec<u8>, bump: u8)]
  73. pub struct TestPdaInit<'info> {
  74. #[account(
  75. init,
  76. seeds = [b"my-seed", domain.as_bytes(), foo.as_ref().unwrap().key.as_ref(), &seed],
  77. bump,
  78. payer = my_payer,
  79. space = DataU16::LEN + 8
  80. )]
  81. pub my_pda: Option<Account<'info, DataU16>>,
  82. #[account(mut)]
  83. pub my_payer: Option<Signer<'info>>,
  84. /// CHECK:
  85. pub foo: Option<AccountInfo<'info>>,
  86. pub system_program: Option<Program<'info, System>>,
  87. }
  88. #[derive(Accounts)]
  89. pub struct TestPdaInitZeroCopy<'info> {
  90. #[account(
  91. init,
  92. seeds = [b"my-seed".as_ref()],
  93. bump,
  94. payer = my_payer,
  95. space = DataZeroCopy::LEN + 8
  96. )]
  97. pub my_pda: Option<AccountLoader<'info, DataZeroCopy>>,
  98. #[account(mut)]
  99. pub my_payer: Option<Signer<'info>>,
  100. pub system_program: Option<Program<'info, System>>,
  101. }
  102. #[derive(Accounts)]
  103. pub struct TestPdaMutZeroCopy<'info> {
  104. #[account(
  105. mut,
  106. seeds = [b"my-seed".as_ref()],
  107. bump = my_pda.load()?.bump,
  108. )]
  109. pub my_pda: Option<AccountLoader<'info, DataZeroCopy>>,
  110. /// CHECK:
  111. pub my_payer: Option<AccountInfo<'info>>,
  112. }
  113. #[derive(Accounts)]
  114. pub struct Initialize<'info> {
  115. #[account(zero)]
  116. pub data: Option<Account<'info, Data>>,
  117. }
  118. #[derive(Accounts)]
  119. pub struct InitializeSkipRentExempt<'info> {
  120. #[account(zero, rent_exempt = skip)]
  121. pub data: Option<Account<'info, Data>>,
  122. }
  123. #[derive(Accounts)]
  124. pub struct InitializeNoRentExempt<'info> {
  125. /// CHECK:
  126. pub data: Option<AccountInfo<'info>>,
  127. }
  128. #[derive(Accounts)]
  129. pub struct TestOwner<'info> {
  130. #[account(owner = *misc.key)]
  131. /// CHECK:
  132. pub data: Option<AccountInfo<'info>>,
  133. /// CHECK:
  134. pub misc: AccountInfo<'info>,
  135. }
  136. #[derive(Accounts)]
  137. pub struct TestExecutable<'info> {
  138. #[account(executable)]
  139. /// CHECK:
  140. pub program: Option<AccountInfo<'info>>,
  141. }
  142. #[derive(Accounts)]
  143. pub struct TestClose<'info> {
  144. #[account(mut, close = sol_dest)]
  145. pub data: Option<Account<'info, Data>>,
  146. /// CHECK:
  147. sol_dest: Option<AccountInfo<'info>>,
  148. }
  149. #[derive(Accounts)]
  150. pub struct TestCloseTwice<'info> {
  151. #[account(mut, close = sol_dest)]
  152. pub data: Option<Account<'info, Data>>,
  153. /// CHECK:
  154. pub sol_dest: Option<AccountInfo<'info>>,
  155. }
  156. #[derive(Accounts)]
  157. pub struct TestCloseMut<'info> {
  158. #[account(mut)]
  159. pub data: Option<Account<'info, Data>>,
  160. /// CHECK:
  161. pub sol_dest: Option<AccountInfo<'info>>,
  162. }
  163. #[derive(Accounts)]
  164. pub struct TestU16<'info> {
  165. #[account(zero)]
  166. pub my_account: Option<Account<'info, DataU16>>,
  167. }
  168. #[derive(Accounts)]
  169. pub struct TestI16<'info> {
  170. #[account(zero)]
  171. pub data: Option<Account<'info, DataI16>>,
  172. }
  173. #[derive(Accounts)]
  174. pub struct TestSimulate {}
  175. #[derive(Accounts)]
  176. pub struct TestI8<'info> {
  177. #[account(zero)]
  178. pub data: Option<Account<'info, DataI8>>,
  179. }
  180. #[derive(Accounts)]
  181. pub struct TestCompositePayer<'info> {
  182. pub composite: TestInit<'info>,
  183. #[account(init, payer = payer.as_ref().unwrap(), space = Data::LEN + 8)]
  184. pub data: Option<Account<'info, Data>>,
  185. pub payer: Option<Signer<'info>>,
  186. pub system_program: Option<Program<'info, System>>,
  187. }
  188. #[derive(Accounts)]
  189. pub struct TestInit<'info> {
  190. #[account(init, payer = payer, space = DataI8::LEN + 8)]
  191. pub data: Option<Account<'info, DataI8>>,
  192. #[account(mut)]
  193. pub payer: Option<Signer<'info>>,
  194. pub system_program: Option<Program<'info, System>>,
  195. }
  196. #[derive(Accounts)]
  197. pub struct TestInitZeroCopy<'info> {
  198. #[account(init, payer = payer, space = DataZeroCopy::LEN + 8)]
  199. pub data: Option<Loader<'info, DataZeroCopy>>,
  200. #[account(mut)]
  201. pub payer: Option<Signer<'info>>,
  202. pub system_program: Option<Program<'info, System>>,
  203. }
  204. #[derive(Accounts)]
  205. pub struct TestInitMint<'info> {
  206. #[account(init, mint::decimals = 6, mint::authority = payer, mint::freeze_authority = payer, payer = payer, )]
  207. pub mint: Option<Account<'info, Mint>>,
  208. #[account(mut)]
  209. pub payer: Option<Signer<'info>>,
  210. pub system_program: Option<Program<'info, System>>,
  211. pub token_program: Option<Program<'info, Token>>,
  212. }
  213. #[derive(Accounts)]
  214. pub struct TestInitToken<'info> {
  215. #[account(init, token::mint = mint, token::authority = payer, payer = payer, )]
  216. pub token: Option<Account<'info, TokenAccount>>,
  217. pub mint: Option<Account<'info, Mint>>,
  218. #[account(mut)]
  219. pub payer: Option<Signer<'info>>,
  220. pub system_program: Option<Program<'info, System>>,
  221. pub token_program: Option<Program<'info, Token>>,
  222. }
  223. #[derive(Accounts)]
  224. pub struct TestFetchAll<'info> {
  225. #[account(init, payer = authority, space = DataWithFilter::LEN + 8)]
  226. pub data: Option<Account<'info, DataWithFilter>>,
  227. #[account(mut)]
  228. pub authority: Option<Signer<'info>>,
  229. pub system_program: Option<Program<'info, System>>,
  230. }
  231. #[derive(Accounts)]
  232. pub struct TestInitWithEmptySeeds<'info> {
  233. #[account(init, seeds = [], bump, payer = authority, space = Data::LEN + 8)]
  234. pub pda: Option<Account<'info, Data>>,
  235. #[account(mut)]
  236. pub authority: Option<Signer<'info>>,
  237. pub system_program: Option<Program<'info, System>>,
  238. }
  239. #[derive(Accounts)]
  240. pub struct TestEmptySeedsConstraint<'info> {
  241. #[account(seeds = [], bump)]
  242. /// CHECK:
  243. pub pda: Option<AccountInfo<'info>>,
  244. }
  245. #[derive(Accounts)]
  246. pub struct InitWithSpace<'info> {
  247. #[account(init, payer = payer, space = DataU16::LEN + 8)]
  248. pub data: Option<Account<'info, DataU16>>,
  249. #[account(mut)]
  250. pub payer: Option<Signer<'info>>,
  251. pub system_program: Option<Program<'info, System>>,
  252. }
  253. #[derive(Accounts)]
  254. pub struct TestInitIfNeeded<'info> {
  255. // intentionally using more space (+500) to check whether space is checked when using init_if_needed
  256. #[account(init_if_needed, payer = payer, space = DataU16::LEN + 8 + 500)]
  257. pub data: Option<Account<'info, DataU16>>,
  258. #[account(mut)]
  259. pub payer: Option<Signer<'info>>,
  260. pub system_program: Option<Program<'info, System>>,
  261. }
  262. #[derive(Accounts)]
  263. pub struct TestInitIfNeededChecksOwner<'info> {
  264. #[account(init_if_needed, payer = payer, space = 100, owner = *owner.key, seeds = [b"hello"], bump)]
  265. /// CHECK:
  266. pub data: Option<UncheckedAccount<'info>>,
  267. #[account(mut)]
  268. pub payer: Option<Signer<'info>>,
  269. pub system_program: Option<Program<'info, System>>,
  270. /// CHECK:
  271. pub owner: AccountInfo<'info>,
  272. }
  273. #[derive(Accounts)]
  274. #[instruction(seed_data: String)]
  275. pub struct TestInitIfNeededChecksSeeds<'info> {
  276. #[account(init_if_needed, payer = payer, space = 100, seeds = [seed_data.as_bytes()], bump)]
  277. /// CHECK:
  278. pub data: Option<UncheckedAccount<'info>>,
  279. #[account(mut)]
  280. pub payer: Option<Signer<'info>>,
  281. pub system_program: Option<Program<'info, System>>,
  282. }
  283. #[derive(Accounts)]
  284. #[instruction(decimals: u8)]
  285. pub struct TestInitMintIfNeeded<'info> {
  286. #[account(init_if_needed, mint::decimals = decimals, mint::authority = mint_authority, mint::freeze_authority = freeze_authority, payer = payer)]
  287. pub mint: Option<Account<'info, Mint>>,
  288. #[account(mut)]
  289. pub payer: Option<Signer<'info>>,
  290. pub system_program: Option<Program<'info, System>>,
  291. pub token_program: Option<Program<'info, Token>>,
  292. /// CHECK:
  293. pub mint_authority: Option<AccountInfo<'info>>,
  294. /// CHECK:
  295. pub freeze_authority: Option<AccountInfo<'info>>,
  296. }
  297. #[derive(Accounts)]
  298. pub struct TestInitTokenIfNeeded<'info> {
  299. #[account(init_if_needed, token::mint = mint, token::authority = authority, payer = payer, )]
  300. pub token: Option<Account<'info, TokenAccount>>,
  301. pub mint: Option<Account<'info, Mint>>,
  302. #[account(mut)]
  303. pub payer: Option<Signer<'info>>,
  304. pub system_program: Option<Program<'info, System>>,
  305. pub token_program: Option<Program<'info, Token>>,
  306. /// CHECK:
  307. pub authority: Option<AccountInfo<'info>>,
  308. }
  309. #[derive(Accounts)]
  310. pub struct TestInitAssociatedTokenIfNeeded<'info> {
  311. #[account(
  312. init_if_needed,
  313. payer = payer,
  314. associated_token::mint = mint,
  315. associated_token::authority = authority
  316. )]
  317. pub token: Option<Account<'info, TokenAccount>>,
  318. pub mint: Option<Account<'info, Mint>>,
  319. #[account(mut)]
  320. pub payer: Option<Signer<'info>>,
  321. pub system_program: Option<Program<'info, System>>,
  322. pub token_program: Option<Program<'info, Token>>,
  323. pub associated_token_program: Option<Program<'info, AssociatedToken>>,
  324. /// CHECK:
  325. pub authority: Option<AccountInfo<'info>>,
  326. }
  327. #[derive(Accounts)]
  328. pub struct TestMultidimensionalArray<'info> {
  329. #[account(zero)]
  330. pub data: Option<Account<'info, DataMultidimensionalArray>>,
  331. }
  332. #[derive(Accounts)]
  333. pub struct TestConstArraySize<'info> {
  334. #[account(zero)]
  335. pub data: Option<Account<'info, DataConstArraySize>>,
  336. }
  337. #[derive(Accounts)]
  338. pub struct TestConstIxDataSize<'info> {
  339. #[account(zero)]
  340. pub data: Option<Account<'info, DataConstArraySize>>,
  341. }
  342. #[derive(Accounts)]
  343. pub struct TestMultidimensionalArrayConstSizes<'info> {
  344. #[account(zero)]
  345. pub data: Option<Account<'info, DataMultidimensionalArrayConstSizes>>,
  346. }
  347. #[derive(Accounts)]
  348. pub struct NoRentExempt<'info> {
  349. /// CHECK:
  350. pub data: Option<AccountInfo<'info>>,
  351. }
  352. #[derive(Accounts)]
  353. pub struct EnforceRentExempt<'info> {
  354. #[account(rent_exempt = enforce)]
  355. /// CHECK:
  356. pub data: Option<AccountInfo<'info>>,
  357. }
  358. #[derive(Accounts)]
  359. pub struct InitDecreaseLamports<'info> {
  360. #[account(init, payer = user, space = 1000)]
  361. /// CHECK:
  362. pub data: Option<AccountInfo<'info>>,
  363. #[account(mut)]
  364. pub user: Option<Signer<'info>>,
  365. pub system_program: Option<Program<'info, System>>,
  366. }
  367. #[derive(Accounts)]
  368. pub struct InitIfNeededChecksRentExemption<'info> {
  369. #[account(init_if_needed, payer = user, space = 1000)]
  370. /// CHECK:
  371. pub data: Option<AccountInfo<'info>>,
  372. #[account(mut)]
  373. pub user: Option<Signer<'info>>,
  374. pub system_program: Option<Program<'info, System>>,
  375. }
  376. #[derive(Accounts)]
  377. #[instruction(bump: u8, second_bump: u8)]
  378. pub struct TestProgramIdConstraint<'info> {
  379. // not a real associated token account
  380. // just deriving like this for testing purposes
  381. #[account(seeds = [b"seed"], bump = bump, seeds::program = anchor_spl::associated_token::ID)]
  382. /// CHECK:
  383. first: Option<AccountInfo<'info>>,
  384. #[account(seeds = [b"seed"], bump = second_bump, seeds::program = crate::ID)]
  385. /// CHECK:
  386. second: Option<AccountInfo<'info>>,
  387. }
  388. #[derive(Accounts)]
  389. pub struct TestProgramIdConstraintUsingFindPda<'info> {
  390. // not a real associated token account
  391. // just deriving like this for testing purposes
  392. #[account(seeds = [b"seed"], bump, seeds::program = anchor_spl::associated_token::ID)]
  393. /// CHECK:
  394. first: Option<AccountInfo<'info>>,
  395. #[account(seeds = [b"seed"], bump, seeds::program = crate::ID)]
  396. /// CHECK:
  397. second: Option<AccountInfo<'info>>,
  398. }
  399. #[derive(Accounts)]
  400. pub struct TestUnsafeFieldSafetyErrors<'info> {
  401. #[doc = "test"]
  402. /// CHECK:
  403. pub data: Option<UncheckedAccount<'info>>,
  404. #[account(mut)]
  405. /// CHECK:
  406. pub data_two: Option<UncheckedAccount<'info>>,
  407. #[account(
  408. seeds = [b"my-seed", signer.as_ref().unwrap().key.as_ref()],
  409. bump
  410. )]
  411. /// CHECK:
  412. pub data_three: Option<UncheckedAccount<'info>>,
  413. /// CHECK:
  414. pub data_four: Option<UncheckedAccount<'info>>,
  415. pub signer: Option<Signer<'info>>,
  416. pub system_program: Option<Program<'info, System>>,
  417. }
  418. #[derive(Accounts)]
  419. pub struct TestConstraintToken<'info> {
  420. #[account(
  421. token::mint = mint,
  422. token::authority = payer
  423. )]
  424. pub token: Option<Account<'info, TokenAccount>>,
  425. pub mint: Option<Account<'info, Mint>>,
  426. pub payer: Option<Signer<'info>>,
  427. }
  428. #[derive(Accounts)]
  429. pub struct TestAuthorityConstraint<'info> {
  430. #[account(
  431. token::mint = mint,
  432. token::authority = fake_authority
  433. )]
  434. pub token: Option<Account<'info, TokenAccount>>,
  435. pub mint: Option<Account<'info, Mint>>,
  436. pub fake_authority: Option<AccountInfo<'info>>,
  437. }
  438. #[derive(Accounts)]
  439. pub struct TestOnlyAuthorityConstraint<'info> {
  440. #[account(
  441. token::authority = payer
  442. )]
  443. pub token: Option<Account<'info, TokenAccount>>,
  444. pub mint: Option<Account<'info, Mint>>,
  445. pub payer: Option<Signer<'info>>,
  446. }
  447. #[derive(Accounts)]
  448. pub struct TestOnlyMintConstraint<'info> {
  449. #[account(
  450. token::mint = mint,
  451. )]
  452. pub token: Option<Account<'info, TokenAccount>>,
  453. pub mint: Option<Account<'info, Mint>>,
  454. }
  455. #[derive(Accounts)]
  456. #[instruction(decimals: u8)]
  457. pub struct TestMintConstraint<'info> {
  458. #[account(
  459. mint::decimals = decimals,
  460. mint::authority = mint_authority,
  461. mint::freeze_authority = freeze_authority
  462. )]
  463. pub mint: Option<Account<'info, Mint>>,
  464. pub mint_authority: Option<AccountInfo<'info>>,
  465. pub freeze_authority: Option<AccountInfo<'info>>,
  466. }
  467. #[derive(Accounts)]
  468. #[instruction(decimals: u8)]
  469. pub struct TestMintOnlyDecimalsConstraint<'info> {
  470. #[account(
  471. mint::decimals = decimals,
  472. )]
  473. pub mint: Option<Account<'info, Mint>>,
  474. }
  475. #[derive(Accounts)]
  476. pub struct TestMintAuthorityConstraint<'info> {
  477. #[account(
  478. mint::authority = mint_authority,
  479. mint::freeze_authority = freeze_authority
  480. )]
  481. pub mint: Option<Account<'info, Mint>>,
  482. pub mint_authority: Option<AccountInfo<'info>>,
  483. pub freeze_authority: Option<AccountInfo<'info>>,
  484. }
  485. #[derive(Accounts)]
  486. pub struct TestMintOneAuthorityConstraint<'info> {
  487. #[account(
  488. mint::authority = mint_authority,
  489. )]
  490. pub mint: Option<Account<'info, Mint>>,
  491. pub mint_authority: Option<AccountInfo<'info>>,
  492. }
  493. #[derive(Accounts)]
  494. #[instruction(decimals: u8)]
  495. pub struct TestMintMissMintAuthConstraint<'info> {
  496. #[account(
  497. mint::decimals = decimals,
  498. mint::freeze_authority = freeze_authority,
  499. )]
  500. pub mint: Option<Account<'info, Mint>>,
  501. pub freeze_authority: Option<AccountInfo<'info>>,
  502. }
  503. #[derive(Accounts)]
  504. pub struct TestAssociatedToken<'info> {
  505. #[account(
  506. associated_token::mint = mint,
  507. associated_token::authority = authority,
  508. )]
  509. pub token: Option<Account<'info, TokenAccount>>,
  510. pub mint: Option<Account<'info, Mint>>,
  511. pub authority: Option<AccountInfo<'info>>,
  512. }