lib.rs 24 KB

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