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();