lib.rs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. extern crate proc_macro;
  2. use proc_macro::TokenStream;
  3. use quote::ToTokens;
  4. use syn::parse_macro_input;
  5. /// Implements an [`Accounts`](./trait.Accounts.html) deserializer on the given
  6. /// struct. Can provide further functionality through the use of attributes.
  7. ///
  8. /// # Table of Contents
  9. /// - [Instruction Attribute](#instruction-attribute)
  10. /// - [Constraints](#constraints)
  11. ///
  12. /// # Instruction Attribute
  13. ///
  14. /// You can access the instruction's arguments with the
  15. /// `#[instruction(..)]` attribute. You have to list them
  16. /// in the same order as in the instruction but you can
  17. /// omit all arguments after the last one you need.
  18. ///
  19. /// # Example
  20. ///
  21. /// ```ignore
  22. /// ...
  23. /// pub fn initialize(ctx: Context<Create>, bump: u8, authority: Pubkey, data: u64) -> ProgramResult {
  24. /// ...
  25. /// Ok(())
  26. /// }
  27. /// ...
  28. /// #[derive(Accounts)]
  29. /// #[instruction(bump: u8)]
  30. /// pub struct Initialize<'info> {
  31. /// ...
  32. /// }
  33. /// ```
  34. ///
  35. /// # Constraints
  36. ///
  37. /// There are different types of constraints that can be applied with the `#[account(..)]` attribute.
  38. ///
  39. /// Attributes may reference other data structures. When `<expr>` is used in the tables below, an arbitrary expression
  40. /// may be passed in as long as it evaluates to a value of the expected type, e.g. `owner = token_program.key()`. If `target_account`
  41. /// used, the `target_account` must exist in the struct and the `.key()` is implicit, e.g. `payer = authority`.
  42. ///
  43. /// - [Normal Constraints](#normal-constraints)
  44. /// - [SPL Constraints](#spl-constraints)
  45. /// # Normal Constraints
  46. /// <table>
  47. /// <thead>
  48. /// <tr>
  49. /// <th>Attribute</th>
  50. /// <th>Description</th>
  51. /// </tr>
  52. /// </thead>
  53. /// <tbody>
  54. /// <tr>
  55. /// <td>
  56. /// <code>#[account(signer)]</code> <br><br><code>#[account(signer @ &lt;custom_error&gt;)]</code>
  57. /// </td>
  58. /// <td>
  59. /// Checks the given account signed the transaction.<br>
  60. /// Custom errors are supported via <code>@</code>.<br>
  61. /// Consider using the <code>Signer</code> type if you would only have this constraint on the account.<br><br>
  62. /// Example:
  63. /// <pre><code>
  64. /// #[account(signer)]
  65. /// pub authority: AccountInfo<'info>,
  66. /// #[account(signer @ MyError::MyErrorCode)]
  67. /// pub payer: AccountInfo<'info>
  68. /// </code></pre>
  69. /// </td>
  70. /// </tr>
  71. /// <tr>
  72. /// <td>
  73. /// <code>#[account(mut)]</code> <br><br><code>#[account(mut @ &lt;custom_error&gt;)]</code>
  74. /// </td>
  75. /// <td>
  76. /// Checks the given account is mutable.<br>
  77. /// Makes anchor persist any state changes.<br>
  78. /// Custom errors are supported via <code>@</code>.<br><br>
  79. /// Example:
  80. /// <pre><code>
  81. /// #[account(mut)]
  82. /// pub data_account: Account<'info, MyData>,
  83. /// #[account(mut @ MyError::MyErrorCode)]
  84. /// pub data_account_two: Account<'info, MyData>
  85. /// </code></pre>
  86. /// </td>
  87. /// </tr>
  88. /// <tr>
  89. /// <td>
  90. /// <code>#[account(init, payer = &lt;target_account&gt;)]</code><br><br>
  91. /// <code>#[account(init, payer = &lt;target_account&gt;, space = &lt;num_bytes&gt;)]</code>
  92. /// </td>
  93. /// <td>
  94. /// Creates the account via a CPI to the system program and
  95. /// initializes it (sets its account discriminator).<br>
  96. /// Marks the account as mutable and is mutually exclusive with <code>mut</code>.<br>
  97. /// Makes the account rent exempt unless skipped with `rent_exempt = skip`.<br>
  98. /// <ul>
  99. /// <li>
  100. /// Requires the <code>payer</code> constraint to also be on the account.
  101. /// The <code>payer</code> account pays for the
  102. /// account creation.
  103. /// </li>
  104. /// <li>
  105. /// Requires the system program to exist on the struct
  106. /// and be called <code>system_program</code>.
  107. /// </li>
  108. /// <li>
  109. /// Requires that the <code>space</code> constraint is specified
  110. /// or, if creating an <code>Account</code> type, the <code>T</code> of <code>Account</code>
  111. /// to implement the rust std <code>Default</code> trait.<br>
  112. /// When using the <code>space</code> constraint, one must remember to add 8 to it
  113. /// which is the size of the account discriminator.<br>
  114. /// The given number is the size of the account in bytes, so accounts that hold
  115. /// a variable number of items such as a <code>Vec</code> should use the <code>space</code>
  116. /// constraint instead of using the <code>Default</code> trait and allocate sufficient space for all items that may
  117. /// be added to the data structure because account size is fixed. Check out the <a href = "https://borsh.io/" target = "_blank" rel = "noopener noreferrer">borsh library</a>
  118. /// (which anchor uses under the hood for serialization) specification to learn how much
  119. /// space different data structures require.
  120. /// </li>
  121. /// <br>
  122. /// Example:
  123. /// <pre>
  124. /// #[account]
  125. /// #[derive(Default)]
  126. /// pub struct MyData {
  127. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data: u64
  128. /// }&#10;
  129. /// #[account]
  130. /// pub struct OtherData {
  131. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data: u64
  132. /// }&#10;
  133. /// #[derive(Accounts)]
  134. /// pub struct Initialize<'info> {
  135. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(init, payer = payer)]
  136. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data_account: Account<'info, MyData>,
  137. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(init, payer = payer, space = 8 + 8)]
  138. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data_account_two: Account<'info, OtherData>,
  139. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(mut)]
  140. /// &nbsp;&nbsp;&nbsp;&nbsp;pub payer: Signer<'info>,
  141. /// &nbsp;&nbsp;&nbsp;&nbsp;pub system_program: Program<'info, System>,
  142. /// }
  143. /// </pre>
  144. /// </ul>
  145. /// <code>init</code> can be combined with other constraints (at the same time):
  146. /// <ul>
  147. /// <li>
  148. /// By default <code>init</code> sets the owner field of the created account to the
  149. /// currently executing program. Add the <code>owner</code> constraint to specify a
  150. /// different program owner.
  151. /// </li>
  152. /// <li>
  153. /// Use the <code>seeds</code> constraint together with <code>bump</code>to create PDAs.<br>
  154. /// <code>init</code> uses <code>find_program_address</code> to calculate the pda so the
  155. /// bump value can be left empty.<br>
  156. /// However, if you want to use the bump in your instruction,
  157. /// you can pass it in as instruction data and set the bump value like shown in the example,
  158. /// using the <code>instruction_data</code> attribute.
  159. /// Anchor will then check that the bump returned by <code>find_program_address</code> equals
  160. /// the bump in the instruction data.<br>
  161. /// <code>seeds::program</code> cannot be used together with init because the creation of an
  162. /// account requires its signature which for PDAs only the currently executing program can provide.
  163. /// </li>
  164. /// </ul>
  165. /// Example:
  166. /// <pre>
  167. /// #[derive(Accounts)]
  168. /// #[instruction(bump: u8)]
  169. /// pub struct Initialize<'info> {
  170. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(
  171. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init, payer = payer,
  172. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seeds = [b"example_seed".as_ref()], bump = bump
  173. /// &nbsp;&nbsp;&nbsp;&nbsp;)]
  174. /// &nbsp;&nbsp;&nbsp;&nbsp;pub pda_data_account: Account<'info, MyData>,
  175. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(
  176. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init, payer = payer,
  177. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;space = 8 + 8, owner = other_program.key()
  178. /// &nbsp;&nbsp;&nbsp;&nbsp;)]
  179. /// &nbsp;&nbsp;&nbsp;&nbsp;pub account_for_other_program: AccountInfo<'info>,
  180. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(
  181. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init,payer = payer, space = 8 + 8,
  182. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner = other_program.key(),
  183. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seeds = [b"other_seed".as_ref()], bump
  184. /// &nbsp;&nbsp;&nbsp;&nbsp;)]
  185. /// &nbsp;&nbsp;&nbsp;&nbsp;pub pda_for_other_program: AccountInfo<'info>,
  186. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(mut)]
  187. /// &nbsp;&nbsp;&nbsp;&nbsp;pub payer: Signer<'info>,
  188. /// &nbsp;&nbsp;&nbsp;&nbsp;pub system_program: Program<'info, System>,
  189. /// &nbsp;&nbsp;&nbsp;&nbsp;pub other_program: Program<'info, OtherProgram>
  190. /// }
  191. /// </pre>
  192. /// </td>
  193. /// </tr>
  194. /// <tr>
  195. /// <td>
  196. /// <code>#[account(init_if_needed, payer = &lt;target_account&gt;)]</code><br><br>
  197. /// <code>#[account(init_if_needed, payer = &lt;target_account&gt;, space = &lt;num_bytes&gt;)]</code>
  198. /// </td>
  199. /// <td>
  200. /// Exact same functionality as the <code>init</code> constraint but only runs if the account does not exist yet.<br>
  201. /// If it does exist, it still checks whether the given init constraints are correct,
  202. /// e.g. that the account has the expected amount of space and, if it's a PDA, the correct seeds etc.
  203. /// <br><br>
  204. /// Example:
  205. /// <pre>
  206. /// #[account]
  207. /// #[derive(Default)]
  208. /// pub struct MyData {
  209. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data: u64
  210. /// }&#10;
  211. /// #[account]
  212. /// pub struct OtherData {
  213. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data: u64
  214. /// }&#10;
  215. /// #[derive(Accounts)]
  216. /// pub struct Initialize<'info> {
  217. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(init_if_needed, payer = payer)]
  218. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data_account: Account<'info, MyData>,
  219. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(init_if_needed, payer = payer, space = 8 + 8)]
  220. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data_account_two: Account<'info, OtherData>,
  221. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(mut)]
  222. /// &nbsp;&nbsp;&nbsp;&nbsp;pub payer: Signer<'info>,
  223. /// &nbsp;&nbsp;&nbsp;&nbsp;pub system_program: Program<'info, System>
  224. /// }
  225. /// </pre>
  226. /// </td>
  227. /// </tr>
  228. /// <tr>
  229. /// <td>
  230. /// <code>#[account(seeds = &lt;seeds&gt;, bump)]</code><br><br>
  231. /// <code>#[account(seeds = &lt;seeds&gt;, bump, seeds::program = &lt;expr&gt;)]<br><br>
  232. /// <code>#[account(seeds = &lt;seeds&gt;, bump = &lt;expr&gt;)]</code><br><br>
  233. /// <code>#[account(seeds = &lt;seeds&gt;, bump = &lt;expr&gt;, seeds::program = &lt;expr&gt;)]</code><br><br>
  234. /// </td>
  235. /// <td>
  236. /// Checks that given account is a PDA derived from the currently executing program,
  237. /// the seeds, and if provided, the bump. If not provided, anchor uses the canonical
  238. /// bump. <br>
  239. /// Add <code>seeds::program = &lt;expr&gt;</code> to derive the PDA from a different
  240. /// program than the currently executing one.<br>
  241. /// This constraint behaves slightly differently when used with <code>init</code>.
  242. /// See its description.
  243. /// <br><br>
  244. /// Example:
  245. /// <pre><code>
  246. /// #[derive(Accounts)]
  247. /// #[instruction(first_bump: u8, second_bump: u8)]
  248. /// pub struct Example {
  249. /// #[account(seeds = [b"example_seed], bump)]
  250. /// pub canonical_pda: AccountInfo<'info>,
  251. /// #[account(
  252. /// seeds = [b"example_seed],
  253. /// bump,
  254. /// seeds::program = other_program.key()
  255. /// )]
  256. /// pub canonical_pda_two: AccountInfo<'info>,
  257. /// #[account(seeds = [b"other_seed], bump = first_bump)]
  258. /// pub arbitrary_pda: AccountInfo<'info>
  259. /// #[account(
  260. /// seeds = [b"other_seed],
  261. /// bump = second_bump,
  262. /// seeds::program = other_program.key()
  263. /// )]
  264. /// pub arbitrary_pda_two: AccountInfo<'info>,
  265. /// pub other_program: Program<'info, OtherProgram>
  266. /// }
  267. /// </code></pre>
  268. /// </td>
  269. /// </tr>
  270. /// <tr>
  271. /// <td>
  272. /// <code>#[account(has_one = &lt;target_account&gt;)]</code><br><br>
  273. /// <code>#[account(has_one = &lt;target_account&gt; @ &lt;custom_error&gt;)]</code>
  274. /// </td>
  275. /// <td>
  276. /// Checks the <code>target_account</code> field on the account matches the
  277. /// key of the <code>target_account</code> field in the Accounts struct.<br>
  278. /// Custom errors are supported via <code>@</code>.<br><br>
  279. /// Example:
  280. /// <pre><code>
  281. /// #[account(mut, has_one = authority)]
  282. /// pub data: Account<'info, MyData>,
  283. /// pub authority: Signer<'info>
  284. /// </code></pre>
  285. /// In this example <code>has_one</code> checks that <code>data.authority = authority.key()</code>
  286. /// </td>
  287. /// </tr>
  288. /// <tr>
  289. /// <td>
  290. /// <code>#[account(address = &lt;expr&gt;)]</code><br><br>
  291. /// <code>#[account(address = &lt;expr&gt; @ &lt;custom_error&gt;)]</code>
  292. /// </td>
  293. /// <td>
  294. /// Checks the account key matches the pubkey.<br>
  295. /// Custom errors are supported via <code>@</code>.<br><br>
  296. /// Example:
  297. /// <pre><code>
  298. /// #[account(address = crate::ID)]
  299. /// pub data: Account<'info, MyData>,
  300. /// #[account(address = crate::ID @ MyError::MyErrorCode)]
  301. /// pub data_two: Account<'info, MyData>
  302. /// </code></pre>
  303. /// </td>
  304. /// </tr>
  305. /// <tr>
  306. /// <td>
  307. /// <code>#[account(owner = &lt;expr&gt;)]</code><br><br>
  308. /// <code>#[account(owner = &lt;expr&gt; @ &lt;custom_error&gt;)]</code>
  309. /// </td>
  310. /// <td>
  311. /// Checks the account owner matches <code>expr</code>.<br>
  312. /// Custom errors are supported via <code>@</code>.<br><br>
  313. /// Example:
  314. /// <pre><code>
  315. /// #[account(owner = Token::ID @ MyError::MyErrorCode)]
  316. /// pub data: Account<'info, MyData>,
  317. /// #[account(owner = token_program.key())]
  318. /// pub data_two: Account<'info, MyData>,
  319. /// pub token_program: Program<'info, Token>
  320. /// </code></pre>
  321. /// </td>
  322. /// </tr>
  323. /// <tr>
  324. /// <td>
  325. /// <code>#[account(executable)]</code>
  326. /// </td>
  327. /// <td>
  328. /// Checks the account is executable (i.e. the account is a program).<br>
  329. /// You may want to use the <code>Program</code> type instead.<br><br>
  330. /// Example:
  331. /// <pre><code>
  332. /// #[account(executable)]
  333. /// pub my_program: AccountInfo<'info>
  334. /// </code></pre>
  335. /// </td>
  336. /// </tr>
  337. /// <tr>
  338. /// <td>
  339. /// <code>#[account(rent_exempt = skip)]</code><br><br>
  340. /// <code>#[account(rent_exempt = enforce)]</code>
  341. /// </td>
  342. /// <td>
  343. /// Enforces rent exemption with <code>= enforce</code>.<br>
  344. /// Skips rent exemption check that would normally be done
  345. /// through other constraints with <code>= skip</code>,
  346. /// e.g. when used with the <code>zero</code> constraint<br><br>
  347. /// Example:
  348. /// <pre><code>
  349. /// #[account(zero, rent_exempt = skip)]
  350. /// pub skipped_account: Account<'info, MyData>,
  351. /// #[account(rent_exempt = enforce)]
  352. /// pub enforced_account: AccountInfo<'info>
  353. /// </code></pre>
  354. /// </td>
  355. /// </tr>
  356. /// <tr>
  357. /// <td>
  358. /// <code>#[account(zero)]</code>
  359. /// </td>
  360. /// <td>
  361. /// Checks the account discriminator is zero.<br>
  362. /// Enforces rent exemption unless skipped with <code>rent_exempt = skip</code><br><br>
  363. /// Example:
  364. /// <pre><code>
  365. /// #[account(zero)]
  366. /// pub my_account: Account<'info, MyData>
  367. /// </code></pre>
  368. /// </td>
  369. /// </tr>
  370. /// <tr>
  371. /// <td>
  372. /// <code>#[account(close = &lt;target_account&gt;)]</code>
  373. /// </td>
  374. /// <td>
  375. /// Marks the account as closed at the end of the instruction’s execution
  376. /// (sets its discriminator to the <code>CLOSED_ACCOUNT_DISCRIMINATOR</code>)
  377. /// and sends its lamports to the specified account.<br>
  378. /// Setting the discriminator to a special variant
  379. /// makes account revival attacks (where a subsequent instruction
  380. /// adds the rent exemption lamports again) impossible.<br>
  381. /// Requires <code>mut</code> to exist on the account.
  382. /// <br><br>
  383. /// Example:
  384. /// <pre><code>
  385. /// #[account(mut, close = receiver)]
  386. /// pub data_account: Account<'info, MyData>,
  387. /// #[account(mut)]
  388. /// pub receiver: SystemAccount<'info>
  389. /// </code></pre>
  390. /// </td>
  391. /// </tr>
  392. /// <tr>
  393. /// <td>
  394. /// <code>#[account(constraint = &lt;expr&gt;)]</code><br><br><code>#[account(constraint = &lt;expr&gt; @ &lt;custom_error&gt;)]</code>
  395. /// </td>
  396. /// <td>
  397. /// Constraint that checks whether the given expression evaluates to true.<br>
  398. /// Use this when no other constraint fits your use case.
  399. /// <br><br>
  400. /// Example:
  401. /// <pre><code>
  402. /// #[account(constraint = one.keys[0].age == two.apple.age)]
  403. /// pub one: Account<'info, MyData>,
  404. /// pub two: Account<'info, OtherData>
  405. /// </code></pre>
  406. /// </td>
  407. /// </tr>
  408. /// </tbody>
  409. /// </table>
  410. ///
  411. /// # SPL Constraints
  412. ///
  413. /// Anchor provides constraints that make verifying SPL accounts easier.
  414. ///
  415. /// <table>
  416. /// <thead>
  417. /// <tr>
  418. /// <th>Attribute</th>
  419. /// <th>Description</th>
  420. /// </tr>
  421. /// </thead>
  422. /// <tbody>
  423. /// <tr>
  424. /// <td>
  425. /// <code>#[account(token::mint = &lt;target_account&gt;, token::authority = &lt;target_account&gt;)]</code>
  426. /// </td>
  427. /// <td>
  428. /// Can currently only be used with <code>init</code> to create a token
  429. /// account with the given mint address and authority.
  430. /// <br><br>
  431. /// Example:
  432. /// <pre>
  433. /// use anchor_spl::{mint, token::{TokenAccount, Mint, Token}};
  434. /// ...&#10;
  435. /// #[account(
  436. /// init,
  437. /// payer = payer,
  438. /// token::mint = mint,
  439. /// token::authority = payer,
  440. /// )]
  441. /// pub token: Account<'info, TokenAccount>,
  442. /// #[account(address = mint::USDC)]
  443. /// pub mint: Account<'info, Mint>,
  444. /// #[account(mut)]
  445. /// pub payer: Signer<'info>,
  446. /// pub token_program: Program<'info, Token>,
  447. /// pub system_program: Program<'info, System>
  448. /// </pre>
  449. /// </td>
  450. /// </tr>
  451. /// <tr>
  452. /// <td>
  453. /// <code>#[account(mint::authority = &lt;target_account&gt;, mint::decimals = &lt;expr&gt;)]</code>
  454. /// <br><br>
  455. /// <code>#[account(mint::authority = &lt;target_account&gt;, mint::decimals = &lt;expr&gt;, mint::freeze_authority = &lt;target_account&gt;)]</code>
  456. /// </td>
  457. /// <td>
  458. /// Can currently only be used with <code>init</code> to create a mint
  459. /// account with the given mint decimals and mint authority.<br>
  460. /// The freeze authority is optional.
  461. /// <br><br>
  462. /// Example:
  463. /// <pre>
  464. /// use anchor_spl::token::{Mint, Token};
  465. /// ...&#10;
  466. /// #[account(
  467. /// init,
  468. /// payer = payer,
  469. /// mint::decimals = 9,
  470. /// mint::authority = payer,
  471. /// )]
  472. /// pub mint_one: Account<'info, Mint>,
  473. /// #[account(
  474. /// init,
  475. /// payer = payer,
  476. /// mint::decimals = 9,
  477. /// mint::authority = payer,
  478. /// mint::freeze_authority = payer
  479. /// )]
  480. /// pub mint_two: Account<'info, Mint>,
  481. /// #[account(mut)]
  482. /// pub payer: Signer<'info>,
  483. /// pub token_program: Program<'info, Token>,
  484. /// pub system_program: Program<'info, System>
  485. /// </pre>
  486. /// </td>
  487. /// </tr>
  488. /// <tr>
  489. /// <td>
  490. /// <code>#[account(associated_token::mint = &lt;target_account&gt;, associated_token::authority = &lt;target_account&gt;)]</code>
  491. /// </td>
  492. /// <td>
  493. /// Can be used as a standalone as a check or with <code>init</code> to create an associated token
  494. /// account with the given mint address and authority.
  495. /// <br><br>
  496. /// Example:
  497. /// <pre>
  498. /// use anchor_spl::{
  499. /// associated_token::AssociatedToken,
  500. /// mint,
  501. /// token::{TokenAccount, Mint, Token}
  502. /// };
  503. /// ...&#10;
  504. /// #[account(
  505. /// init,
  506. /// payer = payer,
  507. /// associated_token::mint = mint,
  508. /// associated_token::authority = payer,
  509. /// )]
  510. /// pub token: Account<'info, TokenAccount>,
  511. /// #[account(
  512. /// associated_token::mint = mint,
  513. /// associated_token::authority = payer,
  514. /// )]
  515. /// pub second_token: Account<'info, TokenAccount>,
  516. /// #[account(address = mint::USDC)]
  517. /// pub mint: Account<'info, Mint>,
  518. /// #[account(mut)]
  519. /// pub payer: Signer<'info>,
  520. /// pub token_program: Program<'info, Token>,
  521. /// pub associated_token_program: Program<'info, AssociatedToken>,
  522. /// pub system_program: Program<'info, System>
  523. /// </pre>
  524. /// </td>
  525. /// </tr>
  526. /// <tbody>
  527. /// </table>
  528. #[proc_macro_derive(Accounts, attributes(account, instruction))]
  529. pub fn derive_anchor_deserialize(item: TokenStream) -> TokenStream {
  530. parse_macro_input!(item as anchor_syn::AccountsStruct)
  531. .to_token_stream()
  532. .into()
  533. }