Rust E0599: no method named key found for enum Option in Anchor with Metaplex Collection

Rust E0599: no method named key found for enum Option in Anchor with Metaplex Collection

Manage alerts

Loading saved threads...

ttyrqa · External communityPost link
External question — Solana Stack Exchange Author: ttyrqa Original post: https://solana.stackexchange.com/questions/21542 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I'm hitting a persistent E0599: no method named key found for enum Option error in an Anchor program when creating a Metaplex NFT collection. The error occurs in the #[derive(Accounts)] macro for the Initialize struct, which sets up the program state and a collection mint/metadata. My original code without collection creation compiled fine, but adding Metaplex logic triggers this error. I suspect it’s related to Sysvar<Rent> or AccountInfo , but I can’t resolve it. Error error[E0599]: no method named `key` found for enum `Option` in the current scope --> programs/nft-mint-anchor/src/lib.rs:468:10 | 468 | #[derive(Accounts)] | ^^^^^^^^ method not found in `Option<_>` | = note: this error originates in the derive macro `Accounts` Code Example use anchor_lang::prelude::*; use anchor_spl::token::{Mint, Token}; use mpl_token_metadata::{ instructions::{CreateMetadataAccountV3, CreateMetadataAccountV3InstructionArgs}, types::{Creator, DataV2}, }; use anchor_lang::solana_program::{system_instruction, program::invoke}; declare_id!("4vyPCHx2CiCeFDR6FCWHnbgSwYEwU8zdMos6w111kG2"); #[program] pub mod nft_mint_anchor { use super::*; pub fn initialize(ctx: Context<Initialize>) -> Result<()> { let state = &mut ctx.accounts.state; state.collection_mint = ctx.accounts.collection_mint.key(); // Initialize collection mint invoke( &system_instruction::create_account( &ctx.accounts.payer.key(), &ctx.accounts.collection_mint.key(), Rent::get()?.minimum_balance(82), 82, &Token::id(), ), &[ ctx.accounts.payer.to_account_info(), ctx.accounts.collection_mint.to_account_info(), ctx.accounts.system_program.to_account_info(), ], )?; invoke( &anchor_spl::token::spl_token::instruction::initialize_mint2( &Token::id(), &ctx.accounts.collection_mint.key(), &ctx.accounts.authority.key(), None, 0, )?, &[ctx.accounts.collection_mint.to_account_info()], )?; // Create collection metadata let creators = vec![Creator { address: ctx.accounts.authority.key(), verified: true, share: 100, }]; let metadata_ix = CreateMetadataAccountV3 { metadata: ctx.accounts.collection_metadata.key(), mint: ctx.accounts.collection_mint.key(), mint_authority: ctx.accounts.authority.key(), payer: ctx.accounts.payer.key(), update_authority: (ctx.accounts.authority.key(), true), system_program: ctx.accounts.system_program.key(), rent: Some(ctx.accounts.rent.key()), }.instruction(CreateMetadataAccountV3InstructionArgs { data: DataV2 { name: "Collection".to_string(), symbol: "NFT".to_string(), uri: "https://example.com/collection.json".to_string(), seller_fee_basis_points: 500, creators: Some(creators), collection: None, uses: None, }, is_mutable: true, collection_details: None, }); invoke( &metadata_ix, &[ ctx.accounts.collection_metadata.to_account_info(), ctx.accounts.collection_mint.to_account_info(), ctx.accounts.authority.to_account_info(), ctx.accounts.payer.to_account_info(), ctx.accounts.system_program.to_account_info(), ctx.accounts.rent.to_account_info(), ctx.accounts.token_metadata_program.to_account_info(), ], )?; Ok(()) } } #[derive(Accounts)] pub struct Initialize<'info> { #[account( init, payer = payer, space = 8 + 1 + 8 + 8 + 8 + 8 + (4 + 256) + 32 + 8 + 1 + 32 + 2 + 32, seeds = [b"state"], bump )] pub state: Account<'info, ProgramState>, #[account(mut)] pub payer: Signer<'info>, pub authority: Signer<'info>, #[account( init, payer = payer, mint::decimals = 0, mint::authority = authority, mint::freeze_authority = None )] pub collection_mint: Account<'info, Mint>, #[account(mut)] pub collection_metadata: AccountInfo<'info>, pub system_program: Program<'info, System>, pub token_program: Program<'info, Token>, pub rent: Sysvar<'info, Rent>, #[account(address = mpl_token_metadata::ID)] pub token_metadata_program: AccountInfo<'info>, } #[account] pub struct ProgramState { pub is_minting_enabled: bool, pub total_minted: u64, pub max_initial_supply: u64, pub max_total_supply: u64, pub price: u64, pub metadata_base_uri: String, pub developer: Pubkey, pub mints_per_wallet: u64, pub royalties_enabled: bool, pub royalties_recipient: Pubkey, pub royalties_percentage: u16, pub collection_mint: Pubkey, } Background My original Anchor program initialized a state without Metaplex collection logic and compiled successfully. Adding collection creation (mint and metadata via CreateMetadataAccountV3) in the initialize instruction caused this error. I want to create an NFT collection in one transaction, following Metaplex standards ( https://developers.metaplex.com/token-metadata ). Tech Stack Rust: anchor-lang = { version = "0.31.0", features = ["init-if-needed"] } anchor-spl = "0.31.0" mpl-token-metadata = { version = "5.1.0", features = ["no-entrypoint"] } Client: { "dependencies": { "@coral-xyz/anchor": "^0.31.0", "@solana/spl-token": "^0.4.13", "@solana/web3.js": "^1.98.0" } } Anchor CLI: 0.31.0 What I’ve Tried Changed collection_metadata to AccountInfo or UncheckedAccount with/without #[account(mut)] . Moved PDA checks to runtime with require_eq! . Removed rent: Sysvar<Rent> temporarily. Split collection logic into a separate instruction, but the error persisted. Downgraded mpl-token-metadata to 4.0.0. Reverted to my original Initialize struct (no Metaplex), which compiles fine. Suspected Cause Bug in anchor-lang 0.31.0 Accounts macro with Sysvar<Rent> or AccountInfo . Incompatibility between mpl-token-metadata 5.1.0 and anchor-lang 0.31.0. Incorrect Accounts setup, though it aligns with Metaplex docs. Questions Why does the macro see a field as Option when none are Option ? Are there issues with anchor-lang 0.31.0 or mpl-token-metadata 5.1.0? How can I create a Metaplex collection in initialize without this error? Is Sysvar<Rent> causing the issue, and how should it be handled? Context Goal: Initialize state and collection in one transaction. Client: Uses @coral-xyz/anchor 0.31.0 in initialize.j s. Original code (no collection in initialize ) worked perfectly. Any help or workarounds would be appreciated!
Quote
Report
Jimii · External communityPost link
External answer — Solana Stack Exchange Author: Jimii Original post: https://solana.stackexchange.com/a/21557 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. The issue is that you are trying to set None in the spl mint constraint that is resulting in the error you are encountering To revoke the freeze_authority remove the constraint and anchor will not initliaze your mint account with a freeze authority #[account(init, payer = payer, mint::decimals = 0, mint::authority = authority)] pub collection_mint: Account<'info, Mint>, ref: https://github.com/solana-foundation/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/syn/src/codegen/accounts/constraints.rs#L1303
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Solana Stack Exchange Author: ttyrqa Source score (net votes, not local likes): 1 Original post: https://solana.stackexchange.com/questions/21542 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I'm hitting a persistent E0599: no method named key found for enum Option error in an Anchor program when creating a Metaplex NFT collection. The error occurs in the #[derive(Accounts)] macro for the Initialize struct, which sets up the program state and a collection mint/metadata. My original code without collection creation compiled fine, but adding Metaplex logic triggers this error. I suspect it’s related to Sysvar<Rent> or AccountInfo , but I can’t resolve it. Error error[E0599]: no method named `key` found for enum `Option` in the current scope --> programs/nft-mint-anchor/src/lib.rs:468:10 | 468 | #[derive(Accounts)] | ^^^^^^^^ method not found in `Option<_>` | = note: this error originates in the derive macro `Accounts` Code Example use anchor_lang::prelude::*; use anchor_spl::token::{Mint, Token}; use mpl_token_metadata::{ instructions::{CreateMetadataAccountV3, CreateMetadataAccountV3InstructionArgs}, types::{Creator, DataV2}, }; use anchor_lang::solana_program::{system_instruction, program::invoke}; declare_id!("4vyPCHx2CiCeFDR6FCWHnbgSwYEwU8zdMos6w111kG2"); #[program] pub mod nft_mint_anchor { use super::*; pub fn initialize(ctx: Context<Initialize>) -> Result<()> { let state = &mut ctx.accounts.state; state.collection_mint = ctx.accounts.collection_mint.key(); // Initialize collection mint invoke( &system_instruction::create_account( &ctx.accounts.payer.key(), &ctx.accounts.collection_mint.key(), Rent::get()?.minimum_balance(82), 82, &Token::id(), ), &[ ctx.accounts.payer.to_account_info(), ctx.accounts.collection_mint.to_account_info(), ctx.accounts.system_program.to_account_info(), ], )?; invoke( &anchor_spl::token::spl_token::instruction::initialize_mint2( &Token::id(), &ctx.accounts.collection_mint.key(), &ctx.accounts.authority.key(), None, 0, )?, &[ctx.accounts.collection_mint.to_account_info()], )?; // Create collection metadata let creators = vec![Creator { address: ctx.accounts.authority.key(), verified: true, share: 100, }]; let metadata_ix = CreateMetadataAccountV3 { metadata: ctx.accounts.collection_metadata.key(), mint: ctx.accounts.collection_mint.key(), mint_authority: ctx.accounts.authority.key(), payer: ctx.accounts.payer.key(), update_authority: (ctx.accounts.authority.key(), true), system_program: ctx.accounts.system_program.key(), rent: Some(ctx.accounts.rent.key()), }.instruction(CreateMetadataAccountV3InstructionArgs { data: DataV2 { name: "Collection".to_string(), symbol: "NFT".to_string(), uri: "https://example.com/collection.json".to_string(), seller_fee_basis_points: 500, creators: Some(creators), collection: None, uses: None, }, is_mutable: true, collection_details: None, }); invoke( &metadata_ix, &[ ctx.accounts.collection_metadata.to_account_info(), ctx.accounts.collection_mint.to_account_info(), ctx.accounts.authority.to_account_info(), ctx.accounts.payer.to_account_info(), ctx.accounts.system_program.to_account_info(), ctx.accounts.rent.to_account_info(), ctx.accounts.token_metadata_program.to_account_info(), ], )?; Ok(()) } } #[derive(Accounts)] pub struct Initialize<'info> { #[account( init, payer = payer, space = 8 + 1 + 8 + 8 + 8 + 8 + (4 + 256) + 32 + 8 + 1 + 32 + 2 + 32, seeds = [b"state"], bump )] pub state: Account<'info, ProgramState>, #[account(mut)] pub payer: Signer<'info>, pub authority: Signer<'info>, #[account( init, payer = payer, mint::decimals = 0, mint::authority = authority, mint::freeze_authority = None )] pub collection_mint: Account<'info, Mint>, #[account(mut)] pub collection_metadata: AccountInfo<'info>, pub system_program: Program<'info, System>, pub token_program: Program<'info, Token>, pub rent: Sysvar<'info, Rent>, #[account(address = mpl_token_metadata::ID)] pub token_metadata_program: AccountInfo<'info>, } #[account] pub struct ProgramState { pub is_minting_enabled: bool, pub total_minted: u64, pub max_initial_supply: u64, pub max_total_supply: u64, pub price: u64, pub metadata_base_uri: String, pub developer: Pubkey, pub mints_per_wallet: u64, pub royalties_enabled: bool, pub royalties_recipient: Pubkey, pub royalties_percentage: u16, pub collection_mint: Pubkey, } Background My original Anchor program initialized a state without Metaplex collection logic and compiled successfully. Adding collection creation (mint and metadata via CreateMetadataAccountV3) in the initialize instruction caused this error. I want to create an NFT collection in one transaction, following Metaplex standards ( https://developers.metaplex.com/token-metadata ). Tech Stack Rust: anchor-lang = { version = "0.31.0", features = ["init-if-needed"] } anchor-spl = "0.31.0" mpl-token-metadata = { version = "5.1.0", features = ["no-entrypoint"] } Client: { "dependencies": { "@coral-xyz/anchor": "^0.31.0", "@solana/spl-token": "^0.4.13", "@solana/web3.js": "^1.98.0" } } Anchor CLI: 0.31.0 What I’ve Tried Changed collection_metadata to AccountInfo or UncheckedAccount with/without #[account(mut)] . Moved PDA checks to runtime with require_eq! . Removed rent: Sysvar<Rent> temporarily. Split collection logic into a separate instruction, but the error persisted. Downgraded mpl-token-metadata to 4.0.0. Reverted to my original Initialize struct (no Metaplex), which compiles fine. Suspected Cause Bug in anchor-lang 0.31.0 Accounts macro with Sysvar<Rent> or AccountInfo . Incompatibility between mpl-token-metadata 5.1.0 and anchor-lang 0.31.0. Incorrect Accounts setup, though it aligns with Metaplex docs. Questions Why does the macro see a field as Option when none are Option ? Are there issues with anchor-lang 0.31.0 or mpl-token-metadata 5.1.0? How can I create a Metaplex collection in initialize without this error? Is Sysvar<Rent> causing the issue, and how should it be handled? Context Goal: Initialize state and collection in one transaction. Client: Uses @coral-xyz/anchor 0.31.0 in initialize.j s. Original code (no collection in initialize ) worked perfectly. Any help or workarounds would be appreciated!

Cancel quote

Checking account access…