4

I couldn't find anchor docs very clear about this. What is exactly the difference between these two structures:

#[derive(Accounts)]
pub struct Foo<'info> {
    pub bar: Signer<'info>,
}

and

#[derive(Accounts)]
pub struct Foo<'info> {
    #[account]
    pub bar: Signer<'info>,
}

Does this really makes sense to have #[account] when we want no constraint on the given account?

1 Answer 1

6

Short answer: The answer is that there's no difference. In this case, #[account] has no effect in the generated code. The reason for this is that the parsing logic for the #[account] macro in this specific case does nothing because no arguments are passed to it.

Long-winded answer: You can see that here. The call to attr.parse_args_with returns a list of comma-separated arguments for the #[account] macro. In this case, it returns nothing because there are no arguments, hence constraints.add is never called and the parse function generates zero constraints.

Since the resulting ConstraintGroup type is what anchor uses to generate the resulting code and it's "empty" in this case, nothing is generated. You can also see evidence of this in the codegen code here. Since we get back an empty vector, nothing gets generated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.