-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IDL generation through compilation #2011
Conversation
@kklas is attempting to deploy a commit to the 200ms Team on Vercel. A member of the Team first needs to authorize it. |
51bc4f4
to
a2713fb
Compare
What implications (if any) does adding prefixes to type definition names like |
I touched upon this in note 3). It means that current clients wont be able to parse the new IDL successfully but we can make the new clients work with both IDLs easily. Right now the generated IDL won't work with the current TS client until we change it to search for full_name. |
ahh must have missed that part. so the introduction of the path segmented naming via this is a great PR, just trying to think of ways to make it less breaking for existing clients. |
Exactly. Name conflicts don't happen super often but when they do it's nasty so it's a good idea to guarantee unique names.
If I understood your suggestion correctly, this doesn't properly solve name conflicts because in theory you could have There probably isn't a way to introduce unique names and have this be fully backward compatible, but I think it's ok to have a breaking change here since anchor is v0 anyways. I'd rather have it be a breaking change rather than some frankenstein solution, but let's hear what Armani says. Anyways since there are multiple ways to do it my idea was to do this in a separate PR not to complicate this PR more than it is. |
Seems sad to let this much work go to waste but i don't really know if it would be possible to merge. The IDL stuff isn't something I'm greatly familiar with yet |
I will take a look into this again when I finish what I'm currently working on and try to push it through. I think there might be a cleaner solution using schemars which seemingly already implements the functionality of this PR and also supports generics. |
fbe8eeb
to
9c7fb82
Compare
Wooh! Everyone I've talked to really likes this approach over the current IDL approach. So I really hope to be able to see this PR reviewed and merged into anchor |
Yes, I also think this is the right approach. My only concern with it previously was the generics support, but now with this implemented it should be very solid. It needs to be tested on more programs but I wouldn't be surprised it's already more reliable than the current method, esp. w.r.t. generics. There's still a bit of work to be done before it can replace the current IDL approach though, and it will require breaking changes to the IDL. I think that should be done in a separate PR, probably along with other breaking changes to the IDL (I feel like some things need to be consolidated in the IDL format). Even so, this PR will already be useful for some people as it introduces:
What I think is the right way forward:
W.r.t. the review, yea it will be a though review, but I'm happy to hop on a call with whoever will be reviewing this to go through the changes and provide context. But if there's only one commit to review it's this one 5da2b51 which gates the changes to the derive and attribute macros behind the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, thank you so much for this PR, it has been open for a very long time and I'd like to merge it as soon as possible.
I have a few questions and have some changes that I'd like to get your opinion on.
Questions
-
How should the external wrapper types like
TokenAccount
work? Currently it doesn't seem to build the IDL with an SPL account. Should we skip the known types? -
How should a generic accounts struct work?
#[derive(Accounts)]
pub struct GenericsTest<'info, T>
where
T: AccountSerialize + AccountDeserialize + Owner + Clone,
{
pub generic: Account<'info, T>,
}
Changes
- Add
InterfaceAccount
to the account types(we are checking forAccount
andAccountLoader
). - Remove
full_path
and usename
instead because clients can derive the name fromfull_path
anyway. - Use
idl-build
instead ofidl-gen
to make it more clear and also parse command isanchor idl parse
and the build command isanchor idl build
. - Convert
type_args
to camelCase in IDL.
I don't want to change too much, this seems to work mostly great as is and I think this could replace the parsing method after we implement the remaining parts. Please let me know if you are OK with these changes and I can continue what you've started.
Do you mean that the IDL build fails with the compilation error or they're just not included? I think for this PR they should be skipped to keep it minimal and this functionality added later on.
This is an entirely different problem far out of scope of this work. The question you should be asking is whether you want to implement this to make
LGTM, just be careful with old IDL / new IDL parsing compatibility for the clients and how you want to roll this out. People parsing the new IDL with the old client could run into issues. |
The former, IDL build fails because those types don't have
Sounds good.
Sorry I wasn't clear enough, I've asked this because of the tests here anchor/lang/tests/generics_test.rs Lines 11 to 23 in 9a93a2e
I don't think it's being used at all and the current IDL doesn't have a support for it either. I also share your sentiment about the security aspect of it and we should probably remove it.
Thanks, it seems to me that the old clients won't be able to properly use the new IDL regardless of what we do and since people upload their IDLs on-chain, I think we should get rid of any extra fields that can be derived from client side. |
Oh I'm a bit fuzzy on the details here since this was a while ago, but I thought these would be skipped here https://github.com/coral-xyz/anchor/pull/2011/files#diff-b2688db258a9b7f16103c6bd084af5bc13cfd095a651f723a66d29d27acd7f24R656-R658 for Maybe there's a way to add a default implementation of
Yea, not only is it not supported by the current IDL, but I've had even |
wow, great work @kklas ! You put a serious amount of work on this PR. Do you think this could be a potential fix for the issue mentioned here? |
@AnderUstarroz yes, it will fix it |
@kklas I've fixed the conflicts and included some changes/fixes. Some parts are still rough around the edges such as external types, duplication of accounts/types, larger IDL size and client compatibility. I'll work on these issues in subsequent PRs. Is there anything you'd like to change before we merge this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally able to merge this, thank you for making this incredible contribution.
Thanks for continuing the effort! |
commit cec9946 Author: Jesserc <raymondjesse713@gmail.com> Date: Sun Sep 10 20:32:34 2023 +0100 docs: Fix typo and grammar in error documentation commit 4955a92 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Fri Sep 8 10:01:51 2023 +0200 Add byte slice(`&[u8]`) support for `idl-build` (coral-xyz#2622) commit a1e4453 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Sep 5 17:30:53 2023 +0200 cli: Make conflicting account names a compile-time error (coral-xyz#2621) commit b9fa898 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Fri Sep 1 22:42:06 2023 +0200 cli: Fix `anchor account` command panicking outside of workspace (coral-xyz#2620) commit dcf5928 Author: Will <82029448+wjthieme@users.noreply.github.com> Date: Wed Aug 30 19:28:51 2023 +0200 lang: `Box` the inner enums of `anchor_lang::error::Error` (coral-xyz#2600) Co-authored-by: acheron <acheroncrypto@gmail.com> commit 115679e Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Mon Aug 28 14:13:12 2023 +0200 bench: Add benchmarking for stack memory usage (coral-xyz#2617) commit a5e4c02 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sun Aug 27 10:21:17 2023 +0200 ts: Bump packages to `0.28.1-beta.2` (coral-xyz#2616) commit a7205af Author: Will <82029448+wjthieme@users.noreply.github.com> Date: Wed Aug 23 22:10:08 2023 +0200 lang: `associated_token` constraints don't work when setting `token_program` (coral-xyz#2603) Co-authored-by: acheron <acheroncrypto@gmail.com> commit 6f9f7d9 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Aug 19 13:09:09 2023 +0200 tests: Move IDL related tests in `misc` to `idl` (coral-xyz#2606) commit 6eacad4 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Aug 15 23:58:17 2023 +0200 cli: Add program template with multiple files (coral-xyz#2602) commit 454f1dd Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sun Aug 13 16:50:28 2023 +0200 ts: Add support for unnamed(tuple) enum in accounts (coral-xyz#2601) commit b5cf67f Author: Jimii <30603522+jim4067@users.noreply.github.com> Date: Sat Aug 12 23:02:37 2023 +0300 spl: Add `TokenRecordAccount` for pNFTs (coral-xyz#2597) commit 58428f8 Author: Proph3t <metaproph3t@protonmail.com> Date: Fri Aug 11 19:48:28 2023 +0000 Add `setup_tests.sh` for setting up your local machine for tests (coral-xyz#2594) commit 4cf447a Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Aug 8 22:03:31 2023 +0200 bench: Add benchmarking for program binary size (coral-xyz#2591) commit abfdc4e Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Aug 5 23:47:06 2023 +0200 chore: Remove duplicate dependency and and alphabetize features (coral-xyz#2590) commit 2af9cc6 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Wed Aug 2 23:01:05 2023 +0200 cli: Improve converting non-conflicting paths to names in IDL (coral-xyz#2588) commit be8764b Author: Proph3t <metaproph3t@protonmail.com> Date: Tue Aug 1 20:23:48 2023 +0000 Fix typo in account.rs docs (coral-xyz#2587) commit b7b8736 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Mon Jul 31 23:41:12 2023 +0200 spl: Export `mpl-token-metadata` crate (coral-xyz#2583) commit cad868a Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Jul 29 23:49:17 2023 +0200 Make the new IDL features explicit (coral-xyz#2582) commit 8f30f00 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Thu Jul 27 23:29:26 2023 +0200 tests: Improve IDL comparison tests (coral-xyz#2581) commit df3e959 Author: Pierre <Arrowana@users.noreply.github.com> Date: Thu Jul 27 07:56:12 2023 +1000 chore: Use @noble/hashes/sha256 rather than obscure package (coral-xyz#2580) commit 5eb678a Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Jul 25 23:52:26 2023 +0200 ts: Lazy load workspace programs and improve program name accessor (coral-xyz#2579) commit 4604fbe Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Jul 22 16:39:05 2023 +0200 cli: Automatically decide IDL generation method (coral-xyz#2578) commit c548c85 Author: Jean Marchand (Exotic Markets) <jeanno11@orange.fr> Date: Sat Jul 22 12:43:56 2023 +0200 chore: Remove unused crates (coral-xyz#2577) commit 2bb3237 Author: Jean Marchand (Exotic Markets) <jeanno11@orange.fr> Date: Fri Jul 21 11:04:52 2023 +0200 chore: Fix clippy lints (coral-xyz#2576) commit 10eb698 Author: dromaz <92167972+dromaz@users.noreply.github.com> Date: Thu Jul 20 23:58:13 2023 +0200 avm: Add support for the `.anchorversion` file (coral-xyz#2553) Co-authored-by: acheron <acheroncrypto@gmail.com> commit 8309bb3 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Wed Jul 19 18:28:38 2023 +0200 cli: Fix `anchor build --no-docs` (coral-xyz#2575) commit c306463 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Mon Jul 17 22:47:55 2023 +0200 tests: Refactor IDL tests (coral-xyz#2573) commit cf057ac Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sun Jul 16 17:08:25 2023 +0200 client: Fix compilation with Solana `1.14` (coral-xyz#2572) commit 4e5280b Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Fri Jul 14 23:17:23 2023 +0200 cli: Fix workspace inheritence (coral-xyz#2570) commit cfc6d29 Author: Lucas Steuernagel <38472950+LucasSte@users.noreply.github.com> Date: Fri Jul 14 16:29:16 2023 -0300 cli: Bump `solang-parser` version (coral-xyz#2569) commit 472279d Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Thu Jul 13 22:06:12 2023 +0200 cli: Add `--out` and `--out-ts` arguments for `idl build` command (coral-xyz#2566) commit b7e91d4 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Jul 11 19:05:14 2023 +0200 Remove IDL `path` field (coral-xyz#2564) commit 6ef6b79 Author: Krešimir Klas <kklas@users.noreply.github.com> Date: Sat Jul 8 19:59:30 2023 +0200 IDL generation through compilation (coral-xyz#2011) Co-authored-by: acheron <acheroncrypto@gmail.com> commit 0225b7c Author: Noah Prince <83885631+ChewingGlass@users.noreply.github.com> Date: Fri Jul 7 14:30:38 2023 -0700 lang: Allow polymorphic CPI calls matching an interface (coral-xyz#2559) commit 9ff7dfc Author: Jean Marchand (Exotic Markets) <jeanno11@orange.fr> Date: Wed Jul 5 10:05:37 2023 +0200 lang: Support for `const` in the `InitSpace` macro (coral-xyz#2555) commit 401d526 Author: chalda <chalda@marinade.finance> Date: Mon Jul 3 22:56:58 2023 +0200 client: Add `DynSigner` (coral-xyz#2550) commit e55cd3e Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Jul 1 22:59:36 2023 +0200 lang: Add `Lamports` trait (coral-xyz#2552) commit 5624bfe Author: Jean Marchand (Exotic Markets) <jeanno11@orange.fr> Date: Thu Jun 29 13:35:38 2023 +0200 lang: Fix typo in the doc (coral-xyz#2551) commit 29b8a72 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Jun 27 20:03:51 2023 +0200 bench: Show change amount and add change note (coral-xyz#2549) commit 8bdc1b1 Author: Han Yang <dhy1996@live.com.sg> Date: Tue Jun 27 01:04:30 2023 +0800 spl: Only allow spl-token version above 1.1 (coral-xyz#2546)
commit cec9946 Author: Jesserc <raymondjesse713@gmail.com> Date: Sun Sep 10 20:32:34 2023 +0100 docs: Fix typo and grammar in error documentation commit 4955a92 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Fri Sep 8 10:01:51 2023 +0200 Add byte slice(`&[u8]`) support for `idl-build` (coral-xyz#2622) commit a1e4453 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Sep 5 17:30:53 2023 +0200 cli: Make conflicting account names a compile-time error (coral-xyz#2621) commit b9fa898 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Fri Sep 1 22:42:06 2023 +0200 cli: Fix `anchor account` command panicking outside of workspace (coral-xyz#2620) commit dcf5928 Author: Will <82029448+wjthieme@users.noreply.github.com> Date: Wed Aug 30 19:28:51 2023 +0200 lang: `Box` the inner enums of `anchor_lang::error::Error` (coral-xyz#2600) Co-authored-by: acheron <acheroncrypto@gmail.com> commit 115679e Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Mon Aug 28 14:13:12 2023 +0200 bench: Add benchmarking for stack memory usage (coral-xyz#2617) commit a5e4c02 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sun Aug 27 10:21:17 2023 +0200 ts: Bump packages to `0.28.1-beta.2` (coral-xyz#2616) commit a7205af Author: Will <82029448+wjthieme@users.noreply.github.com> Date: Wed Aug 23 22:10:08 2023 +0200 lang: `associated_token` constraints don't work when setting `token_program` (coral-xyz#2603) Co-authored-by: acheron <acheroncrypto@gmail.com> commit 6f9f7d9 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Aug 19 13:09:09 2023 +0200 tests: Move IDL related tests in `misc` to `idl` (coral-xyz#2606) commit 6eacad4 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Aug 15 23:58:17 2023 +0200 cli: Add program template with multiple files (coral-xyz#2602) commit 454f1dd Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sun Aug 13 16:50:28 2023 +0200 ts: Add support for unnamed(tuple) enum in accounts (coral-xyz#2601) commit b5cf67f Author: Jimii <30603522+jim4067@users.noreply.github.com> Date: Sat Aug 12 23:02:37 2023 +0300 spl: Add `TokenRecordAccount` for pNFTs (coral-xyz#2597) commit 58428f8 Author: Proph3t <metaproph3t@protonmail.com> Date: Fri Aug 11 19:48:28 2023 +0000 Add `setup_tests.sh` for setting up your local machine for tests (coral-xyz#2594) commit 4cf447a Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Aug 8 22:03:31 2023 +0200 bench: Add benchmarking for program binary size (coral-xyz#2591) commit abfdc4e Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Aug 5 23:47:06 2023 +0200 chore: Remove duplicate dependency and and alphabetize features (coral-xyz#2590) commit 2af9cc6 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Wed Aug 2 23:01:05 2023 +0200 cli: Improve converting non-conflicting paths to names in IDL (coral-xyz#2588) commit be8764b Author: Proph3t <metaproph3t@protonmail.com> Date: Tue Aug 1 20:23:48 2023 +0000 Fix typo in account.rs docs (coral-xyz#2587) commit b7b8736 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Mon Jul 31 23:41:12 2023 +0200 spl: Export `mpl-token-metadata` crate (coral-xyz#2583) commit cad868a Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Jul 29 23:49:17 2023 +0200 Make the new IDL features explicit (coral-xyz#2582) commit 8f30f00 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Thu Jul 27 23:29:26 2023 +0200 tests: Improve IDL comparison tests (coral-xyz#2581) commit df3e959 Author: Pierre <Arrowana@users.noreply.github.com> Date: Thu Jul 27 07:56:12 2023 +1000 chore: Use @noble/hashes/sha256 rather than obscure package (coral-xyz#2580) commit 5eb678a Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Jul 25 23:52:26 2023 +0200 ts: Lazy load workspace programs and improve program name accessor (coral-xyz#2579) commit 4604fbe Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Jul 22 16:39:05 2023 +0200 cli: Automatically decide IDL generation method (coral-xyz#2578) commit c548c85 Author: Jean Marchand (Exotic Markets) <jeanno11@orange.fr> Date: Sat Jul 22 12:43:56 2023 +0200 chore: Remove unused crates (coral-xyz#2577) commit 2bb3237 Author: Jean Marchand (Exotic Markets) <jeanno11@orange.fr> Date: Fri Jul 21 11:04:52 2023 +0200 chore: Fix clippy lints (coral-xyz#2576) commit 10eb698 Author: dromaz <92167972+dromaz@users.noreply.github.com> Date: Thu Jul 20 23:58:13 2023 +0200 avm: Add support for the `.anchorversion` file (coral-xyz#2553) Co-authored-by: acheron <acheroncrypto@gmail.com> commit 8309bb3 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Wed Jul 19 18:28:38 2023 +0200 cli: Fix `anchor build --no-docs` (coral-xyz#2575) commit c306463 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Mon Jul 17 22:47:55 2023 +0200 tests: Refactor IDL tests (coral-xyz#2573) commit cf057ac Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sun Jul 16 17:08:25 2023 +0200 client: Fix compilation with Solana `1.14` (coral-xyz#2572) commit 4e5280b Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Fri Jul 14 23:17:23 2023 +0200 cli: Fix workspace inheritence (coral-xyz#2570) commit cfc6d29 Author: Lucas Steuernagel <38472950+LucasSte@users.noreply.github.com> Date: Fri Jul 14 16:29:16 2023 -0300 cli: Bump `solang-parser` version (coral-xyz#2569) commit 472279d Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Thu Jul 13 22:06:12 2023 +0200 cli: Add `--out` and `--out-ts` arguments for `idl build` command (coral-xyz#2566) commit b7e91d4 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Jul 11 19:05:14 2023 +0200 Remove IDL `path` field (coral-xyz#2564) commit 6ef6b79 Author: Krešimir Klas <kklas@users.noreply.github.com> Date: Sat Jul 8 19:59:30 2023 +0200 IDL generation through compilation (coral-xyz#2011) Co-authored-by: acheron <acheroncrypto@gmail.com> commit 0225b7c Author: Noah Prince <83885631+ChewingGlass@users.noreply.github.com> Date: Fri Jul 7 14:30:38 2023 -0700 lang: Allow polymorphic CPI calls matching an interface (coral-xyz#2559) commit 9ff7dfc Author: Jean Marchand (Exotic Markets) <jeanno11@orange.fr> Date: Wed Jul 5 10:05:37 2023 +0200 lang: Support for `const` in the `InitSpace` macro (coral-xyz#2555) commit 401d526 Author: chalda <chalda@marinade.finance> Date: Mon Jul 3 22:56:58 2023 +0200 client: Add `DynSigner` (coral-xyz#2550) commit e55cd3e Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Sat Jul 1 22:59:36 2023 +0200 lang: Add `Lamports` trait (coral-xyz#2552) commit 5624bfe Author: Jean Marchand (Exotic Markets) <jeanno11@orange.fr> Date: Thu Jun 29 13:35:38 2023 +0200 lang: Fix typo in the doc (coral-xyz#2551) commit 29b8a72 Author: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue Jun 27 20:03:51 2023 +0200 bench: Show change amount and add change note (coral-xyz#2549) commit 8bdc1b1 Author: Han Yang <dhy1996@live.com.sg> Date: Tue Jun 27 01:04:30 2023 +0800 spl: Only allow spl-token version above 1.1 (coral-xyz#2546)
The following new IDL types were introduced in coral-xyz#2011: * `GenericLenArray` * `Generic` * `DefinedWithTypeArgs` Usage of these types was leading to incompatibility of the IDL with the TypeScript `IdlType`. Fixes: coral-xyz#2687
The following new IDL types were introduced in coral-xyz#2011: * `GenericLenArray` * `Generic` * `DefinedWithTypeArgs` Usage of these types was leading to incompatibility of the IDL with the TypeScript `IdlType`. Fixes: coral-xyz#2687
I've implemented an IDL generation method which instead of parsing the program source files generates the idl generation code by hooking into various anchor derive and attribute macros. This IDL generation code is then executed by running
cargo test
on the program crate. The benefit of this approach is that we're now able to evaluate expressions within the program source which allows for embedding additional information into the IDL.The scope of this PR is to cover the same features that the current (parse) IDL generation has (I haven't implemented any of the eval stuff from #1927), with the addition of supporting external types (discussed #1972).
The change is fairly big so I've seperated it into multiple stand-alone commits to make the review easier. I strongly suggest to merge this via a rebase vs squash for the purposes of git history.
In summary, here is the diff between the IDLs generated by the parse and gen methods: https://www.diffchecker.com/UzVPnS0o
(for the following test program here)
Also, generics on accounts and AnchorSerialize structs are now supported. Here's the IDL it generates https://gist.github.com/kklas/1617d2b636e8d24e6789f3cf99519b1f generated for this program.
Notes
anchor idl build
command. The parsing method is still default when runninganchor build
so this PR doesn't actually introduce any breaking changes.idl-gen
feature flag so there's no idl generation code generated when it's not enabled.The new IDL generation method currently doesn't support generics (i.e. if you have account, event, etc. structs which are generics, the generation will fail with a compile error). There's a way to go around this which I believe would work support for generics in compile based IDL generation #2013I didn't implement "state" stuff because it's going to get deprecatedidl-gen
feature needs to be added to the[features]
section ofCargo.toml
of programs, including all external crates that we import types to be included in the IDL, e.g.:Implementation Details
The basis of the IDL generation is the hook in the AnchorSerialize derive macro (gated behind 'idl-gen' feature). It generates the following methods for each type that derives AnchorSerialize:
__anchor_private_full_path()
- calling this will return the type's full path (implemented using thestd::module_path!
macro)__anchor_private_gen_idl_type()
- this will return theIdlTypeDefinition
for the type when called__anchor_private_insert_idl_defined(defined_types: &mut HashMap<String, IdlTypeDefinition>)
- a helper method which will insert any defined types in the types fields into the provided defined_types map.#[zero_copy] does the same as above and #[account] and #[account(zero_copy)] are covered due to the fact that these attribute macros already add #[zero_copy] and #[derive(AnchorSerialize)] respectively.
#[derive(Accounts)] adds a
__anchor_private_gen_idl_accounts
method which will returnVec<IdlAccountItem>
when called. It also adds any types referenced within Account<...> or AccountLoader<...> into the provided defined_types hash map.#[program] generates a
__anchor_private_print_idl_program
function which is marked as a #[test]. When executed it will collect all the accounts from ctx by calling__anchor_private_gen_idl_accounts
on the ctx type, and__anchor_private_gen_idl_type
on each ix arg. This is how all the defined types are collected recursively. It will print out theIdl
json.#[event], #[error_code], and #[constant] work a bit different in that they also generate an
__anchor_private_print...
function marked as a test to print them out. This is because it's not possible to know which events, etc. are being referenced and we can't collect them recursively in #[program] as mentioned in the note 5).Then the
anchor idl build
command runscargo test __anchor_private_print_idl --features idl-gen -- --show-output --quiet
on the crate and collects the Idl jsons from the stdout printed by the generated test methods. The events, errors, and constants are printed separately and are combined by the command into the final IDL json which is then outputted to stdout.When running
anchor idl build
on the workspace with multiple programs, it will collect all the IDLs and print them in form of an array:[<idl1>, <idl2>]