Anchor编译报错:Mint/TokenAccount无__anchor_private_insert_idl_defined关联项
问题:Anchor Solana程序编译E0599错误排查
编译错误信息
error[E0599]: no associated item named `__anchor_private_insert_idl_defined` found for struct `anchor_spl::token::Mint` in the current scope --> programs/sol404/src/instructions/createmint.rs:153:10 | 153 | #[derive(Accounts)] | ^^^^^^^^ associated item not found in `Mint` | = note: this error originates in the derive macro `Accounts` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0599]: no associated item named `__anchor_private_insert_idl_defined` found for struct `anchor_spl::token::TokenAccount` in the current scope --> programs/sol404/src/instructions/createmint.rs:153:10 | 153 | #[derive(Accounts)] | ^^^^^^^^ associated item not found in `TokenAccount` | = note: this error originates in the derive macro `Accounts` (in Nightly builds, run with -Z macro-backtrace for more info)
相关程序代码
#[derive(Accounts)] pub struct CreateMint<'info> { // Use ADMIN_PUBKEY as constraint, only the specified admin can invoke this instruction #[account( mut, address = ADMIN_PUBKEY )] pub admin: Signer<'info> // The PDA is both the address of the mint account and the mint authority #[account( init, seeds = [b"jelly"], bump, payer = admin, mint::decimals = 9, mint::authority = jelly_token_mint, )] pub jelly_token_mint: Account<'info, Mint>, ///CHECK: Using "address" constraint to validate metadata account address, this account is created via CPI in the instruction #[account( mut, address = MetadataAccount::find_pda(&jelly_token_mint.key()).0, )] pub jelly_metadata_account: UncheckedAccount<'info>, #[account( init, payer = admin, associated_token::mint = jelly_token_mint, associated_token::authority = admin, )] pub jelly_token_account: Account<'info, TokenAccount>, pub associated_token_program: Program<'info, AssociatedToken>, pub token_program: Program<'info, Token>, pub token_metadata_program: Program<'info, Metadata>, pub system_program: Program<'info, System>, pub rent: Sysvar<'info, Rent>, }
Cargo.toml依赖配置
[package] name = "sol404" version = "0.1.0" description = "Created with Anchor" edition = "2021" [lib] crate-type = ["cdylib", "lib"] name = "sol404" [features] default = [] cpi = ["no-entrypoint"] no-entrypoint = [] no-idl = [] no-log-ix-name = [] idl-build = ["anchor-lang/idl-build"] [dependencies] anchor-lang = {version = "0.29.0", features = ["init-if-needed"]} anchor-spl = {version = "0.29.0", features = ["metadata"]} mpl-token-metadata = "4.1.2" solana-program = "1.18.11"
解决方案
1. 替换账户类型声明
错误核心原因是直接用Account包裹Mint/TokenAccount结构体,未使用Anchor官方提供的包装类型。Anchor的#[derive(Accounts)]宏需要这些包装类型来自动生成IDL相关关联项。
修改后的CreateMint结构体:
#[derive(Accounts)] pub struct CreateMint<'info> { #[account( mut, address = ADMIN_PUBKEY )] pub admin: Signer<'info>, // 补上缺失的逗号 #[account( init, seeds = [b"jelly"], bump, payer = admin, mint::decimals = 9, mint::authority = jelly_token_mint, )] pub jelly_token_mint: anchor_spl::token::Mint<'info>, ///CHECK: Using "address" constraint to validate metadata account address, this account is created via CPI in the instruction #[account( mut, address = MetadataAccount::find_pda(&jelly_token_mint.key()).0, )] pub jelly_metadata_account: UncheckedAccount<'info>, #[account( init, payer = admin, associated_token::mint = jelly_token_mint, associated_token::authority = admin, )] pub jelly_token_account: anchor_spl::token::TokenAccount<'info>, pub associated_token_program: Program<'info, AssociatedToken>, pub token_program: Program<'info, Token>, pub token_metadata_program: Program<'info, Metadata>, pub system_program: Program<'info, System>, pub rent: Sysvar<'info, Rent>, }
2. 对齐依赖版本
Anchor 0.29.0依赖的solana-program版本为1.16.24,当前使用的1.18.11版本过高,会导致宏生成代码与底层结构体定义不兼容。修改Cargo.toml中的solana-program版本:
solana-program = "1.16.24"
原因说明
__anchor_private_insert_idl_defined是Anchor生成IDL时自动注入的关联项,仅当使用Anchor官方包装的anchor_spl::token::Mint/TokenAccount类型时,才会自动实现该关联项。- 依赖版本不匹配会破坏宏与结构体的兼容性,引发此类找不到关联项的错误。
内容的提问来源于stack exchange,提问作者papi
相关产品推荐
相关产品推荐

