I would like to initialize in my Anchor program a new token to mint with extension transfer fee and metadata. It keeps return error InvalidAccountData when invoke initialize_transfer_fee_config, i think there is something wrong with this part
let acc = vec![ ctx.accounts.mint.to_account_info(), ];
My code:
pub fn initialize_token_mint(ctx: Context<InitializeMint>, params: InitTokenParams) -> Result<()> {
msg!("Start set fee for token");
let seeds = &["mint".as_bytes(), &[ctx.bumps.mint]];
let signer = [&seeds[..]];
let acc = vec![
ctx.accounts.mint.to_account_info(),
];
let ix = initialize_transfer_fee_config(
&ctx.accounts.token_program.key(), // SPL Token program ID
&ctx.accounts.mint.key(), // Mint account
Some(&ctx.accounts.user.key()),
Some(&ctx.accounts.user.key()),
params.transfer_fee_basis_points, // Basis points for the transfer fee
params.maximum_fee,// Maximum fee
).unwrap();
invoke(
&ix,
acc.as_slice(),
)?;
msg!("Set init transfer fee successfully");
let account_init_mint_ix = vec![
ctx.accounts.mint.to_account_info(),
ctx.accounts.rent.to_account_info(),
];
invoke(
&initialize_mint(
&ctx.accounts.token_program.key(),
&ctx.accounts.mint.key(),
&ctx.accounts.mint.key(),
Some(&ctx.accounts.mint.key()),
params.decimals,
)?,
account_init_mint_ix.as_slice(),
)?;
msg!("Set init mint fee successfully");
let account_fee_ix = vec![
ctx.accounts.mint.to_account_info(),
ctx.accounts.mint.to_account_info(),
];
invoke(
&set_transfer_fee(
&ctx.accounts.token_program.key(), // SPL Token program ID
&ctx.accounts.mint.key(), // Mint account
&ctx.accounts.mint.key(),
&[&ctx.accounts.user.key()],
params.transfer_fee_basis_points, // Basis points for the transfer fee
params.maximum_fee,// Maximum fee
).unwrap(),
account_fee_ix.as_slice(),
)?;
msg!("Set fee fee successfully");
let account_info = vec![
ctx.accounts.metadata.to_account_info(),
ctx.accounts.mint.to_account_info(),
ctx.accounts.user.to_account_info(),
ctx.accounts.token_metadata_program.to_account_info(),
ctx.accounts.token_program.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.rent.to_account_info(),
];
invoke_signed(
&mpl_instruction::create_metadata_accounts_v3(
ctx.accounts.token_metadata_program.key(),
ctx.accounts.metadata.key(),
ctx.accounts.mint.key(),
ctx.accounts.mint.key(),
ctx.accounts.user.key(),
ctx.accounts.mint.key(),
params.name,
params.symbol,
params.uri,
None,
0,
true,
true,
None,
None,
None,
),
account_info.as_slice(),
&signer,
)?;
msg!("Token metadata created successfully.");
Ok(())
}
#[derive(Accounts)]
#[instruction(
params: InitTokenParams
)]
pub struct InitializeMint<'info> {
/// CHECK:
#[account(
init,
payer = user,
space = 8 + 8,
seeds = ["mint22".as_bytes()],
bump
)]
pub mint: AccountInfo<'info, >,
#[account(mut)]
/// CHECK:
pub metadata: UncheckedAccount<'info>,
/// CHECK: account constraint checked in account trait
#[account(address = MetadataID)]
pub token_metadata_program: UncheckedAccount<'info>,
#[account(mut)]
pub user: Signer<'info>,
pub token_program: Program<'info, Token2022>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(
params: InitTokenParams
)]
pub struct InitializeMint<'info> {
/// CHECK:
#[account(
init,
payer = user,
space = 8 + 8,
seeds = ["mint22".as_bytes()],
bump
)]
pub mint: AccountInfo<'info, >,
#[account(mut)]
/// CHECK:
pub metadata: UncheckedAccount<'info>,
/// CHECK: account constraint checked in account trait
#[account(address = MetadataID)]
pub token_metadata_program: UncheckedAccount<'info>,
#[account(mut)]
pub user: Signer<'info>,
pub token_program: Program<'info, Token2022>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
}
Then I run a script at client side TS, it showed log error:
logs: [
'Program DczPycuxn6dM7nRpojKgZcduMWHiXdaEPvCWCqLED97h invoke [1]',
'Program log: Instruction: InitializeTokenMint',
'Program 11111111111111111111111111111111 invoke [2]',
'Program 11111111111111111111111111111111 success',
'Program log: Start set fee for token',
'Program log: Start signed 1',
'Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb invoke [2]',
'Program log: Error: InvalidAccountData',
'Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb consumed 1310 of 188290 compute units',
'Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb failed: invalid account data for instruction',
'Program DczPycuxn6dM7nRpojKgZcduMWHiXdaEPvCWCqLED97h consumed 13020 of 200000 compute units',
'Program DczPycuxn6dM7nRpojKgZcduMWHiXdaEPvCWCqLED97h failed: invalid account data for instruction'
],
Please help me find the problem. Thank a lot