References
Account Constraints
Minimal reference examples for Anchor account constraints.
Instruction Attribute
Attribute | Example | Description |
---|---|---|
| You can access the instruction’s arguments with the #[instruction(..)] attribute. You have to list them in the same order as in the instruction but you can omit all arguments after the last one you need. |
Normal Constraints
Attribute | Example | Description |
---|---|---|
| Checks the given account signed the transaction. Custom errors are supported via @. Consider using the Signer type if you would only have this constraint on the account. | |
| Checks the given account is mutable. Makes anchor persist any state changes. Custom errors are supported via @. | |
| Creates the account via a CPI to the system program and initializes it (sets its account discriminator). | |
| Exact same functionality as the init constraint but only runs if the account does not exist yet. This feature should be used with care and is therefore behind a feature flag. You can enable it by importing anchor-lang with the init-if-needed cargo feature. When using init_if_needed, you need to make sure you properly protect yourself against re-initialization attacks. | |
| Checks that given account is a PDA derived from the currently executing program, the seeds, and if provided, the bump. If not provided, anchor uses the canonical bump. Add seeds::program = <expr> to derive the PDA from a different program than the currently executing one. | |
| Checks the target_account field on the account matches the key of the target_account field in the Accounts struct. Custom errors are supported via @. | |
| Checks the account key matches the pubkey. Custom errors are supported via @. | |
| Checks the account owner matches expr. Custom errors are supported via @. | |
| Checks the account is executable (i.e. the account is a program). You may want to use the Program type instead. | |
| Github Solpg | Enforces rent exemption with = enforce. Skips rent exemption check that would normally be done through other constraints with = skip, e.g. when used with the zero constraint |
| Checks the account discriminator is zero. Use this constraint if you want to create an account in a previous instruction and then initialize it in your instruction instead of using init. This is necessary for accounts that are larger than 10 Kibibyte because those accounts cannot be created via a CPI (which is what init would do). | |
| Closes the account by:
Requires mut to exist on the account. | |
| Constraint that checks whether the given expression evaluates to true. Use this when no other constraint fits your use case. | |
| Used to realloc program account space at the beginning of an instruction. |
SPL Constraints
Attribute | Example | Description |
---|---|---|
| Can be used as a check or with init to create a token account with the given mint address and authority. When used as a check, it's possible to only specify a subset of the constraints. | |
| Can be used as a check or with init to create a mint account with the given mint decimals and mint authority. The freeze authority is optional when used with init. When used as a check, it's possible to only specify a subset of the constraints. | |
| Can be used as a standalone as a check or with init to create an associated token account with the given mint address and authority. | |
| The token_program can optionally be overridden. |