lib.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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) -> anchor_lang::Result<()> {
  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;, space = &lt;num_bytes&gt;)]</code>
  91. /// </td>
  92. /// <td>
  93. /// Creates the account via a CPI to the system program and
  94. /// initializes it (sets its account discriminator).<br>
  95. /// Marks the account as mutable and is mutually exclusive with <code>mut</code>.<br>
  96. /// Makes the account rent exempt unless skipped with <code>rent_exempt = skip</code>.<br><br>
  97. /// Use <code>#[account(zero)]</code> for accounts larger than 10 Kibibyte.<br><br>
  98. /// <code>init</code> has to be used with additional constraints:
  99. /// <ul>
  100. /// <li>
  101. /// Requires the <code>payer</code> constraint to also be on the account.
  102. /// The <code>payer</code> account pays for the
  103. /// account creation.
  104. /// </li>
  105. /// <li>
  106. /// Requires the system program to exist on the struct
  107. /// and be called <code>system_program</code>.
  108. /// </li>
  109. /// <li>
  110. /// Requires that the <code>space</code> constraint is specified.
  111. /// When using the <code>space</code> constraint, one must remember to add 8 to it
  112. /// which is the size of the account discriminator. This only has to be done
  113. /// for accounts owned by anchor programs.<br>
  114. /// The given space 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 allocate sufficient space for all items that may
  116. /// be added to the data structure because account size is fixed.
  117. /// Check out the <a href = "https://book.anchor-lang.com/anchor_references/space.html" target = "_blank" rel = "noopener noreferrer">space reference</a>
  118. /// and the <a href = "https://borsh.io/" target = "_blank" rel = "noopener noreferrer">borsh library</a>
  119. /// (which anchor uses under the hood for serialization) specification to learn how much
  120. /// space different data structures require.
  121. /// </li>
  122. /// <br>
  123. /// Example:
  124. /// <pre>
  125. /// #[account]
  126. /// pub struct MyData {
  127. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data: u64
  128. /// }&#10;
  129. /// #[derive(Accounts)]
  130. /// pub struct Initialize<'info> {
  131. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(init, payer = payer, space = 8 + 8)]
  132. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data_account_two: Account<'info, MyData>,
  133. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(mut)]
  134. /// &nbsp;&nbsp;&nbsp;&nbsp;pub payer: Signer<'info>,
  135. /// &nbsp;&nbsp;&nbsp;&nbsp;pub system_program: Program<'info, System>,
  136. /// }
  137. /// </pre>
  138. /// </ul>
  139. /// <code>init</code> can be combined with other constraints (at the same time):
  140. /// <ul>
  141. /// <li>
  142. /// By default <code>init</code> sets the owner field of the created account to the
  143. /// currently executing program. Add the <code>owner</code> constraint to specify a
  144. /// different program owner.
  145. /// </li>
  146. /// <li>
  147. /// Use the <code>seeds</code> constraint together with <code>bump</code>to create PDAs.<br>
  148. /// <code>init</code> uses <code>find_program_address</code> to calculate the pda so the
  149. /// bump value can be left empty.<br>
  150. /// However, if you want to use the bump in your instruction,
  151. /// you can pass it in as instruction data and set the bump value like shown in the example,
  152. /// using the <code>instruction_data</code> attribute.
  153. /// Anchor will then check that the bump returned by <code>find_program_address</code> equals
  154. /// the bump in the instruction data.<br>
  155. /// <code>seeds::program</code> cannot be used together with init because the creation of an
  156. /// account requires its signature which for PDAs only the currently executing program can provide.
  157. /// </li>
  158. /// </ul>
  159. /// Example:
  160. /// <pre>
  161. /// #[derive(Accounts)]
  162. /// #[instruction(bump: u8)]
  163. /// pub struct Initialize<'info> {
  164. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(
  165. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init, payer = payer, space = 8 + 8
  166. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seeds = [b"example_seed"], bump = bump
  167. /// &nbsp;&nbsp;&nbsp;&nbsp;)]
  168. /// &nbsp;&nbsp;&nbsp;&nbsp;pub pda_data_account: Account<'info, MyData>,
  169. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(
  170. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init, payer = payer,
  171. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;space = 8 + 8, owner = other_program.key()
  172. /// &nbsp;&nbsp;&nbsp;&nbsp;)]
  173. /// &nbsp;&nbsp;&nbsp;&nbsp;pub account_for_other_program: AccountInfo<'info>,
  174. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(
  175. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init, payer = payer, space = 8 + 8,
  176. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner = other_program.key(),
  177. /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seeds = [b"other_seed"], bump
  178. /// &nbsp;&nbsp;&nbsp;&nbsp;)]
  179. /// &nbsp;&nbsp;&nbsp;&nbsp;pub pda_for_other_program: AccountInfo<'info>,
  180. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(mut)]
  181. /// &nbsp;&nbsp;&nbsp;&nbsp;pub payer: Signer<'info>,
  182. /// &nbsp;&nbsp;&nbsp;&nbsp;pub system_program: Program<'info, System>,
  183. /// &nbsp;&nbsp;&nbsp;&nbsp;pub other_program: Program<'info, OtherProgram>
  184. /// }
  185. /// </pre>
  186. /// </td>
  187. /// </tr>
  188. /// <tr>
  189. /// <td>
  190. /// <code>#[account(init_if_needed, payer = &lt;target_account&gt;)]</code><br><br>
  191. /// <code>#[account(init_if_needed, payer = &lt;target_account&gt;, space = &lt;num_bytes&gt;)]</code>
  192. /// </td>
  193. /// <td>
  194. /// Exact same functionality as the <code>init</code> constraint but only runs if the account does not exist yet.<br>
  195. /// If the account does exist, it still checks whether the given init constraints are correct,
  196. /// e.g. that the account has the expected amount of space and, if it's a PDA, the correct seeds etc.<br><br>
  197. /// This feature should be used with care and is therefore behind a feature flag.
  198. /// You can enable it by importing <code>anchor-lang</code> with the <code>init-if-needed</code> cargo feature.<br>
  199. /// When using <code>init_if_needed</code>, you need to make sure you properly protect yourself
  200. /// against re-initialization attacks. You need to include checks in your code that check
  201. /// that the initialized account cannot be reset to its initial settings after the first time it was
  202. /// initialized (unless that it what you want).<br>
  203. /// Because of the possibility of re-initialization attacks and the general guideline that instructions
  204. /// should avoid having multiple execution flows (which is important so they remain easy to understand),
  205. /// consider breaking up your instruction into two instructions - one for initializing and one for using
  206. /// the account - unless you have a good reason not to do so.
  207. /// <br><br>
  208. /// Example:
  209. /// <pre>
  210. /// #[account]
  211. /// #[derive(Default)]
  212. /// pub struct MyData {
  213. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data: u64
  214. /// }&#10;
  215. /// #[account]
  216. /// pub struct OtherData {
  217. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data: u64
  218. /// }&#10;
  219. /// #[derive(Accounts)]
  220. /// pub struct Initialize<'info> {
  221. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(init_if_needed, payer = payer)]
  222. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data_account: Account<'info, MyData>,
  223. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(init_if_needed, payer = payer, space = 8 + 8)]
  224. /// &nbsp;&nbsp;&nbsp;&nbsp;pub data_account_two: Account<'info, OtherData>,
  225. /// &nbsp;&nbsp;&nbsp;&nbsp;#[account(mut)]
  226. /// &nbsp;&nbsp;&nbsp;&nbsp;pub payer: Signer<'info>,
  227. /// &nbsp;&nbsp;&nbsp;&nbsp;pub system_program: Program<'info, System>
  228. /// }
  229. /// </pre>
  230. /// </td>
  231. /// </tr>
  232. /// <tr>
  233. /// <td>
  234. /// <code>#[account(seeds = &lt;seeds&gt;, bump)]</code><br><br>
  235. /// <code>#[account(seeds = &lt;seeds&gt;, bump, seeds::program = &lt;expr&gt;)]<br><br>
  236. /// <code>#[account(seeds = &lt;seeds&gt;, bump = &lt;expr&gt;)]</code><br><br>
  237. /// <code>#[account(seeds = &lt;seeds&gt;, bump = &lt;expr&gt;, seeds::program = &lt;expr&gt;)]</code><br><br>
  238. /// </td>
  239. /// <td>
  240. /// Checks that given account is a PDA derived from the currently executing program,
  241. /// the seeds, and if provided, the bump. If not provided, anchor uses the canonical
  242. /// bump. <br>
  243. /// Add <code>seeds::program = &lt;expr&gt;</code> to derive the PDA from a different
  244. /// program than the currently executing one.<br>
  245. /// This constraint behaves slightly differently when used with <code>init</code>.
  246. /// See its description.
  247. /// <br><br>
  248. /// Example:
  249. /// <pre><code>
  250. /// #[derive(Accounts)]
  251. /// #[instruction(first_bump: u8, second_bump: u8)]
  252. /// pub struct Example {
  253. /// #[account(seeds = [b"example_seed"], bump)]
  254. /// pub canonical_pda: AccountInfo<'info>,
  255. /// #[account(
  256. /// seeds = [b"example_seed"],
  257. /// bump,
  258. /// seeds::program = other_program.key()
  259. /// )]
  260. /// pub canonical_pda_two: AccountInfo<'info>,
  261. /// #[account(seeds = [b"other_seed"], bump = first_bump)]
  262. /// pub arbitrary_pda: AccountInfo<'info>
  263. /// #[account(
  264. /// seeds = [b"other_seed"],
  265. /// bump = second_bump,
  266. /// seeds::program = other_program.key()
  267. /// )]
  268. /// pub arbitrary_pda_two: AccountInfo<'info>,
  269. /// pub other_program: Program<'info, OtherProgram>
  270. /// }
  271. /// </code></pre>
  272. /// </td>
  273. /// </tr>
  274. /// <tr>
  275. /// <td>
  276. /// <code>#[account(has_one = &lt;target_account&gt;)]</code><br><br>
  277. /// <code>#[account(has_one = &lt;target_account&gt; @ &lt;custom_error&gt;)]</code>
  278. /// </td>
  279. /// <td>
  280. /// Checks the <code>target_account</code> field on the account matches the
  281. /// key of the <code>target_account</code> field in the Accounts struct.<br>
  282. /// Custom errors are supported via <code>@</code>.<br><br>
  283. /// Example:
  284. /// <pre><code>
  285. /// #[account(mut, has_one = authority)]
  286. /// pub data: Account<'info, MyData>,
  287. /// pub authority: Signer<'info>
  288. /// </code></pre>
  289. /// In this example <code>has_one</code> checks that <code>data.authority = authority.key()</code>
  290. /// </td>
  291. /// </tr>
  292. /// <tr>
  293. /// <td>
  294. /// <code>#[account(address = &lt;expr&gt;)]</code><br><br>
  295. /// <code>#[account(address = &lt;expr&gt; @ &lt;custom_error&gt;)]</code>
  296. /// </td>
  297. /// <td>
  298. /// Checks the account key matches the pubkey.<br>
  299. /// Custom errors are supported via <code>@</code>.<br><br>
  300. /// Example:
  301. /// <pre><code>
  302. /// #[account(address = crate::ID)]
  303. /// pub data: Account<'info, MyData>,
  304. /// #[account(address = crate::ID @ MyError::MyErrorCode)]
  305. /// pub data_two: Account<'info, MyData>
  306. /// </code></pre>
  307. /// </td>
  308. /// </tr>
  309. /// <tr>
  310. /// <td>
  311. /// <code>#[account(owner = &lt;expr&gt;)]</code><br><br>
  312. /// <code>#[account(owner = &lt;expr&gt; @ &lt;custom_error&gt;)]</code>
  313. /// </td>
  314. /// <td>
  315. /// Checks the account owner matches <code>expr</code>.<br>
  316. /// Custom errors are supported via <code>@</code>.<br><br>
  317. /// Example:
  318. /// <pre><code>
  319. /// #[account(owner = Token::ID @ MyError::MyErrorCode)]
  320. /// pub data: Account<'info, MyData>,
  321. /// #[account(owner = token_program.key())]
  322. /// pub data_two: Account<'info, MyData>,
  323. /// pub token_program: Program<'info, Token>
  324. /// </code></pre>
  325. /// </td>
  326. /// </tr>
  327. /// <tr>
  328. /// <td>
  329. /// <code>#[account(executable)]</code>
  330. /// </td>
  331. /// <td>
  332. /// Checks the account is executable (i.e. the account is a program).<br>
  333. /// You may want to use the <code>Program</code> type instead.<br><br>
  334. /// Example:
  335. /// <pre><code>
  336. /// #[account(executable)]
  337. /// pub my_program: AccountInfo<'info>
  338. /// </code></pre>
  339. /// </td>
  340. /// </tr>
  341. /// <tr>
  342. /// <td>
  343. /// <code>#[account(rent_exempt = skip)]</code><br><br>
  344. /// <code>#[account(rent_exempt = enforce)]</code>
  345. /// </td>
  346. /// <td>
  347. /// Enforces rent exemption with <code>= enforce</code>.<br>
  348. /// Skips rent exemption check that would normally be done
  349. /// through other constraints with <code>= skip</code>,
  350. /// e.g. when used with the <code>zero</code> constraint<br><br>
  351. /// Example:
  352. /// <pre><code>
  353. /// #[account(zero, rent_exempt = skip)]
  354. /// pub skipped_account: Account<'info, MyData>,
  355. /// #[account(rent_exempt = enforce)]
  356. /// pub enforced_account: AccountInfo<'info>
  357. /// </code></pre>
  358. /// </td>
  359. /// </tr>
  360. /// <tr>
  361. /// <td>
  362. /// <code>#[account(zero)]</code>
  363. /// </td>
  364. /// <td>
  365. /// Checks the account discriminator is zero.<br>
  366. /// Enforces rent exemption unless skipped with <code>rent_exempt = skip</code>.<br><br>
  367. /// Use this constraint if you want to create an account in a previous instruction
  368. /// and then initialize it in your instruction instead of using <code>init</code>.
  369. /// This is necessary for accounts that are larger than 10 Kibibyte because those
  370. /// accounts cannot be created via a CPI (which is what <code>init</code> would do).<br><br>
  371. /// Anchor adds internal data to the account when using <code>zero</code> just like it
  372. /// does with <code>init</code> which is why <code>zero</code> implies <code>mut</code>.
  373. /// <br><br>
  374. /// Example:
  375. /// <pre><code>
  376. /// #[account(zero)]
  377. /// pub my_account: Account<'info, MyData>
  378. /// </code></pre>
  379. /// </td>
  380. /// </tr>
  381. /// <tr>
  382. /// <td>
  383. /// <code>#[account(close = &lt;target_account&gt;)]</code>
  384. /// </td>
  385. /// <td>
  386. /// Marks the account as closed at the end of the instruction’s execution
  387. /// (sets its discriminator to the <code>CLOSED_ACCOUNT_DISCRIMINATOR</code>)
  388. /// and sends its lamports to the specified account.<br>
  389. /// Setting the discriminator to a special variant
  390. /// makes account revival attacks (where a subsequent instruction
  391. /// adds the rent exemption lamports again) impossible.<br>
  392. /// Requires <code>mut</code> to exist on the account.
  393. /// <br><br>
  394. /// Example:
  395. /// <pre><code>
  396. /// #[account(mut, close = receiver)]
  397. /// pub data_account: Account<'info, MyData>,
  398. /// #[account(mut)]
  399. /// pub receiver: SystemAccount<'info>
  400. /// </code></pre>
  401. /// </td>
  402. /// </tr>
  403. /// <tr>
  404. /// <td>
  405. /// <code>#[account(constraint = &lt;expr&gt;)]</code><br><br><code>#[account(constraint = &lt;expr&gt; @ &lt;custom_error&gt;)]</code>
  406. /// </td>
  407. /// <td>
  408. /// Constraint that checks whether the given expression evaluates to true.<br>
  409. /// Use this when no other constraint fits your use case.
  410. /// <br><br>
  411. /// Example:
  412. /// <pre><code>
  413. /// #[account(constraint = one.keys[0].age == two.apple.age)]
  414. /// pub one: Account<'info, MyData>,
  415. /// pub two: Account<'info, OtherData>
  416. /// </code></pre>
  417. /// </td>
  418. /// </tr>
  419. /// </tbody>
  420. /// </table>
  421. ///
  422. /// # SPL Constraints
  423. ///
  424. /// Anchor provides constraints that make verifying SPL accounts easier.
  425. ///
  426. /// <table>
  427. /// <thead>
  428. /// <tr>
  429. /// <th>Attribute</th>
  430. /// <th>Description</th>
  431. /// </tr>
  432. /// </thead>
  433. /// <tbody>
  434. /// <tr>
  435. /// <td>
  436. /// <code>#[account(token::mint = &lt;target_account&gt;, token::authority = &lt;target_account&gt;)]</code>
  437. /// </td>
  438. /// <td>
  439. /// Can be used as a check or with <code>init</code> to create a token
  440. /// account with the given mint address and authority.<br>
  441. /// When used as a check, it's possible to only specify a subset of the constraints.
  442. /// <br><br>
  443. /// Example:
  444. /// <pre>
  445. /// use anchor_spl::{mint, token::{TokenAccount, Mint, Token}};
  446. /// ...&#10;
  447. /// #[account(
  448. /// init,
  449. /// payer = payer,
  450. /// token::mint = mint,
  451. /// token::authority = payer,
  452. /// )]
  453. /// pub token: Account<'info, TokenAccount>,
  454. /// #[account(address = mint::USDC)]
  455. /// pub mint: Account<'info, Mint>,
  456. /// #[account(mut)]
  457. /// pub payer: Signer<'info>,
  458. /// pub token_program: Program<'info, Token>,
  459. /// pub system_program: Program<'info, System>
  460. /// </pre>
  461. /// </td>
  462. /// </tr>
  463. /// <tr>
  464. /// <td>
  465. /// <code>#[account(mint::authority = &lt;target_account&gt;, mint::decimals = &lt;expr&gt;)]</code>
  466. /// <br><br>
  467. /// <code>#[account(mint::authority = &lt;target_account&gt;, mint::decimals = &lt;expr&gt;, mint::freeze_authority = &lt;target_account&gt;)]</code>
  468. /// </td>
  469. /// <td>
  470. /// Can be used as a check or with <code>init</code> to create a mint
  471. /// account with the given mint decimals and mint authority.<br>
  472. /// The freeze authority is optional when used with <code>init</code>.<br>
  473. /// When used as a check, it's possible to only specify a subset of the constraints.
  474. /// <br><br>
  475. /// Example:
  476. /// <pre>
  477. /// use anchor_spl::token::{Mint, Token};
  478. /// ...&#10;
  479. /// #[account(
  480. /// init,
  481. /// payer = payer,
  482. /// mint::decimals = 9,
  483. /// mint::authority = payer,
  484. /// )]
  485. /// pub mint_one: Account<'info, Mint>,
  486. /// #[account(
  487. /// init,
  488. /// payer = payer,
  489. /// mint::decimals = 9,
  490. /// mint::authority = payer,
  491. /// mint::freeze_authority = payer
  492. /// )]
  493. /// pub mint_two: Account<'info, Mint>,
  494. /// #[account(mut)]
  495. /// pub payer: Signer<'info>,
  496. /// pub token_program: Program<'info, Token>,
  497. /// pub system_program: Program<'info, System>
  498. /// </pre>
  499. /// </td>
  500. /// </tr>
  501. /// <tr>
  502. /// <td>
  503. /// <code>#[account(associated_token::mint = &lt;target_account&gt;, associated_token::authority = &lt;target_account&gt;)]</code>
  504. /// </td>
  505. /// <td>
  506. /// Can be used as a standalone as a check or with <code>init</code> to create an associated token
  507. /// account with the given mint address and authority.
  508. /// <br><br>
  509. /// Example:
  510. /// <pre>
  511. /// use anchor_spl::{
  512. /// associated_token::AssociatedToken,
  513. /// mint,
  514. /// token::{TokenAccount, Mint, Token}
  515. /// };
  516. /// ...&#10;
  517. /// #[account(
  518. /// init,
  519. /// payer = payer,
  520. /// associated_token::mint = mint,
  521. /// associated_token::authority = payer,
  522. /// )]
  523. /// pub token: Account<'info, TokenAccount>,
  524. /// #[account(
  525. /// associated_token::mint = mint,
  526. /// associated_token::authority = payer,
  527. /// )]
  528. /// pub second_token: Account<'info, TokenAccount>,
  529. /// #[account(address = mint::USDC)]
  530. /// pub mint: Account<'info, Mint>,
  531. /// #[account(mut)]
  532. /// pub payer: Signer<'info>,
  533. /// pub token_program: Program<'info, Token>,
  534. /// pub associated_token_program: Program<'info, AssociatedToken>,
  535. /// pub system_program: Program<'info, System>
  536. /// </pre>
  537. /// </td>
  538. /// </tr>
  539. /// <tbody>
  540. /// </table>
  541. #[proc_macro_derive(Accounts, attributes(account, instruction))]
  542. pub fn derive_anchor_deserialize(item: TokenStream) -> TokenStream {
  543. parse_macro_input!(item as anchor_syn::AccountsStruct)
  544. .to_token_stream()
  545. .into()
  546. }