Skip to content
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

Add Macros support on Accounts struct (Currently IDL generator doesn't support them) [Feature request] #2431

Closed
AnderUstarroz opened this issue Mar 11, 2023 · 6 comments
Labels
idl related to the IDL, either program or client side

Comments

@AnderUstarroz
Copy link

AnderUstarroz commented Mar 11, 2023

Problem

Anchor IDL generator doesn't seem to support macros on Accounts structs.

Why is this important

Supporting macros on Accounts structs would facilitate the integration of 3rd party packages by abstracting away the complexity and reducing a lot the boilerplate. This is easier to understand with an example.

Example: Analytics package

Imagine we want to create an Analytics package which stores data on every instruction. We would have to repeat this boilerplate on every single instruction:

#[derive(Accounts)]
pub struct DoSomething<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,
    #[account(mut, seeds = [b"some_account".as_ref(), some_account.key().as_ref()],  bump = some_account.bump)],
    pub some_account:  Box<Account<'info, MyAccount>>,
    #[account(
        mut,
        seeds = [b"analytics-app".as_ref(), analytics_app.key().as_ref()], 
        bump = analytics_app.bump
    )]
    pub analytics_app:  Box<Account<'info, AnalyticsApp>>,
    #[account(
        mut,
        seeds = [b"analytics-owner".as_ref(), analytics_owner.key().as_ref()], 
        bump = analytics_owner.bump
    )]
    pub analytics_owner:  Box<Account<'info, AnalyticsOwner>>,
    pub analytics_program: Program<'info, Analytics>,
}

But using a simple Macro like syntax #[analytics_accounts], all the analytics accounts could be added during compilation time making for a much succinct and neat integrations:

#[analytics_accounts] <--- All analytics accounts are added on compilation time. 
#[derive(Accounts)]
pub struct DoSomething<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,
    #[account(mut, seeds = [b"some_account".as_ref(), some_account.key().as_ref()],  bump = some_account.bump)],
    pub some_account:  Box<Account<'info, MyAccount>>,
}

Benefits

Analytics, Authentication, Logs, Audits systems are just some examples of potential packages which would benefit from a "more mature" IDL generator in Anchor. I think this will add flexibility, increase code quality and facilitate the integration of packages by abstracting away complexity and avoiding having to write so much redundant code.

@AnderUstarroz AnderUstarroz changed the title Add Macros support on Accounts struct (Currently IDL generator doesn't seem to support them) Add Macros support on Accounts struct (Currently IDL generator doesn't support them) [Feature request] Mar 11, 2023
@buffalojoec
Copy link
Contributor

Are you saying that if you write the macro in your example, and it creates the tokens to add all of the accounts to your context, they don't make it into the IDL?

Any chance you have this code somewhere that you can demonstrate and show the macro & IDL?

@Aursen
Copy link
Contributor

Aursen commented Apr 10, 2023

Maybe it can be good to have a new macro to import derive account structure

@AnderUstarroz
Copy link
Author

AnderUstarroz commented Apr 10, 2023

Are you saying that if you write the macro in your example, and it creates the tokens to add all of the accounts to your context, they don't make it into the IDL?

Any chance you have this code somewhere that you can demonstrate and show the macro & IDL?

Correct this is a real example @realbuffalojoe :

#[sol_cerberus_accounts]
#[derive(Accounts)]
pub struct Add<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,
    #[account(
        mut,
        seeds = [b"sol-cerberus-demo".as_ref(), demo.sol_cerberus_app.key().as_ref()], 
        bump = demo.bump
    )]
    pub demo: Box<Account<'info, Demo>>,
}

Where the #[sol_cerberus_accounts] macro adds the accounts:

  • sol_cerberus_app
  • sol_cerberus_rule
  • sol_cerberus_role
  • sol_cerberus_token_acc
  • sol_cerberus_metadata
  • sol_cerberus

Nonetheless the IDL generates just the signer and demo accounts, forcing me to bypass this issue using the following workaround:

#[sol_cerberus_accounts]
#[derive(Accounts)]
pub struct Add<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,
    #[account(
        mut,
        seeds = [b"sol-cerberus-demo".as_ref(), demo.sol_cerberus_app.key().as_ref()], 
        bump = demo.bump
    )]
    pub demo: Box<Account<'info, Demo>>,
    /// CHECK: Added to avoid the IDL generation issue
    pub sol_cerberus_app: UncheckedAccount<'info>,
    /// CHECK: Added to avoid the IDL generation issue
    pub sol_cerberus_rule: Option<UncheckedAccount<'info>>,
    /// CHECK: Added to avoid the IDL generation issue
    pub sol_cerberus_role: Option<UncheckedAccount<'info>>,
    /// CHECK: Added to avoid the IDL generation issue
    pub sol_cerberus_token_acc: Option<UncheckedAccount<'info>>,
    /// CHECK: Added to avoid the IDL generation issue
    pub sol_cerberus_metadata: Option<UncheckedAccount<'info>>,
    pub sol_cerberus: Program<'info, SolCerberus>,
}

which kind of defeats the whole point of reducing boilerplate :(

@peterschwarzdev
Copy link

peterschwarzdev commented May 23, 2023

Any update on this? the very same lack of flexibility is preventing us for building packages for anchor. Any plans on addressing this issue for Anchor v0.28.0?

@Aursen
Copy link
Contributor

Aursen commented May 24, 2023

I think that for the moment it will be blocked. The problem comes from a design issue to generate the idl. I think we will need this PR to solve the case: (#2011)

@acheroncrypto acheroncrypto mentioned this issue Feb 25, 2024
@acheroncrypto acheroncrypto added the idl related to the IDL, either program or client side label Mar 10, 2024
@acheroncrypto
Copy link
Collaborator

Added in #2824.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idl related to the IDL, either program or client side
Projects
None yet
Development

No branches or pull requests

5 participants