1

I am trying to create a token mint with extension in anchor. This works fine until I try to to add an additional extension to the mint. When I call:

let space =
    ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::MetadataPointer]).unwrap();

The size output for the mint is: "Mint Space: 234 lamports: 2519520"

The error is:

Error: InvalidAccountData

So i assume the space calculation for the mint is wrong.

When I try to change it to

let space =
    ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::TokenMetadata]).unwrap();

the error turns into:

"panicked at 'called `Result::unwrap()` on an `Err` value: InvalidArgument', programs/extension_nft/src/instructions/mint_nft.rs:19:91"

Is that because TokenMetadata has variable length?

ExtensionType::TokenMetadata => unreachable!(),

How can I create a Mint with Token meta data in anchor/rust?

Here is the code:

let space = ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::MetadataPointer])
        .unwrap();

let lamports_required = (Rent::get()?).minimum_balance(space);
msg!(
    "Meta data Space: {} lamports: {}",
    space as u64,
    lamports_required
);

system_program::create_account(
    CpiContext::new(
        ctx.accounts.token_program.to_account_info(),
        system_program::CreateAccount {
            from: ctx.accounts.signer.to_account_info(),
            to: ctx.accounts.mint.to_account_info(),
        },
    ),
    lamports_required,
    space as u64,
    &ctx.accounts.token_program.key(),
)?;

system_program::assign(
    CpiContext::new(
        ctx.accounts.token_program.to_account_info(),
        system_program::Assign {
            account_to_assign: ctx.accounts.mint.to_account_info(),
        },
    ),
    &token_2022::ID,
)?;

msg!("Created mint account");

let mint_cpi = CpiContext::new(
    ctx.accounts.token_program.to_account_info(),
    token_2022::InitializeMint2 {
        mint: ctx.accounts.mint.to_account_info(),
    },
);
msg!("Init mint");

token_2022::initialize_mint2(mint_cpi, 0, &ctx.accounts.signer.key(), None).unwrap();

1 Answer 1

1

Ohhh i figured the first part out!

I need to initialize the metadata pointer before initializing the mint:

let instruction = spl_token_2022::extension::metadata_pointer::instruction::initialize(
    &Token2022::id(),
    &ctx.accounts.mint.key(),
    Some(ctx.accounts.signer.key()),
    Some(ctx.accounts.signer.key()),
)
.unwrap();
invoke(
    &instruction,
    &[
        ctx.accounts.mint.to_account_info(),
        ctx.accounts.signer.to_account_info(),
        ctx.accounts.signer.to_account_info(),
    ],
)?;
token_2022::initialize_mint2(mint_cpi, 0, &ctx.accounts.signer.key(), None).unwrap();

The second part of creating the meta data looks like this:

invoke(
    &spl_token_metadata_interface::instruction::initialize(
        &spl_token_2022::id(),
        ctx.accounts.mint.key,
        ctx.accounts.signer.key,
        ctx.accounts.mint.key,
        ctx.accounts.signer.key,
         "Epic Fish".to_string(),
         "EPC".to_string(),
         "https://shdw-drive.genesysgo.net/AzjHvXgqUJortnr5fXDG2aPkp2PfFMvu4Egr57fdiite/PirateCoinMeta".to_string(),

    ),
    &[ctx.accounts.mint.to_account_info().clone(), ctx.accounts.signer.to_account_info().clone()],
)?;

Also the pointer extension needs to be initialized before the mint and the meta data itself after the mint.

For that you also need to add

spl-token-metadata-interface = { version = "0.2.1"}

to your cargo.toml

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.