context.rs 17 KB

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