Trying to create a program that has the authority to mint tokens to the wallet signing the transaction. However, one of my tests keeps failing with: "insufficient account keys for instruction"
pda-airdrop
✔ can create airdrop account (444ms)
1) can airdrop token to account
1 passing (604ms)
1 failing
1) pda-airdrop
can airdrop token to account:
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: insufficient account keys for instruction
at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:4480:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:4439:20)
at sendAndConfirmRawTransaction (node_modules/@project-serum/anchor/src/provider.ts:288:21)
at AnchorProvider.sendAndConfirm (node_modules/@project-serum/anchor/src/provider.ts:148:14)
at MethodsBuilder.rpc [as _rpcFn] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:29:16)
Here is my program:
use anchor_lang::prelude::*;
use anchor_spl::{
associated_token::AssociatedToken,
token::{mint_to, Mint, MintTo, Token, TokenAccount},
};
declare_id!("AUrpX9QjAFeKBSBC2acgYHzBUud6xeiR2VvmiJSdoHqk");
#[program]
pub mod pda_airdrop {
use super::*;
pub fn create_airdop(_ctx: Context<CreateAirdrop>) -> Result<()> {
Ok(())
}
pub fn execute_airdrop(ctx: Context<ExecuteAirdrop>, amount: u64) -> Result<()> {
// Find bump for mint account created in create_airdrop
let (_, bump) = Pubkey::find_program_address(
&[],
ctx.program_id
);
// mint amount to associated token account
let token_program = ctx.accounts.token_program.to_account_info();
let mint_to_accounts = MintTo {
mint: ctx.accounts.mint.to_account_info(),
to: ctx.accounts.ata.to_account_info(),
authority: ctx.accounts.mint.to_account_info(),
};
mint_to(
CpiContext::new_with_signer(
token_program,
mint_to_accounts,
&[&[
&[],
&[bump]
]]
),
amount
)?;
Ok(())
}
}
// Instruction: Create Mint Account for Airdrop
#[derive(Accounts)]
pub struct CreateAirdrop<'info> {
#[account(mut)]
pub signer: Signer<'info>,
#[account(
init,
payer = signer,
seeds = [],
bump,
mint::decimals = 0,
mint::authority = mint
)]
pub mint: Account<'info, Mint>,
pub token_program: Program<'info, Token>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
}
// Instruction: Execute Airdrop
#[derive(Accounts)]
pub struct ExecuteAirdrop<'info> {
#[account(mut)]
pub signer: Signer<'info>,
#[account(mut)]
pub mint: Account<'info, Mint>,
#[account(init_if_needed, payer = signer, associated_token::mint = mint, associated_token::authority = signer)]
pub ata: Account<'info, TokenAccount>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub token_program: Program<'info, Token>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
}
And here are the tests I'm running:
import * as anchor from "@project-serum/anchor";
import { Program, web3 } from "@project-serum/anchor";
import { PdaAirdrop } from "../target/types/pda_airdrop";
import { expect } from "chai";
import { getMint, getAssociatedTokenAddress } from "@solana/spl-token";
describe("pda-airdrop", () => {
const program = anchor.workspace.PdaAirdrop as Program<PdaAirdrop>;
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
it("can create airdrop account", async () => {
const [mint, _bump] = await web3.PublicKey.findProgramAddress(
[],
program.programId
);
await program.methods
.createAirdop()
.accounts({
mint,
})
.rpc();
const mintAccount = await getMint(provider.connection, mint);
expect(mintAccount.isInitialized).eql(true);
});
it("can airdrop token to account", async () => {
const [mint, _bump] = await web3.PublicKey.findProgramAddress(
[],
program.programId
);
const ata = await getAssociatedTokenAddress(
mint,
provider.wallet.publicKey
);
const supply = new anchor.BN(200);
await program.methods
.executeAirdrop(supply)
.accounts({
mint,
ata,
})
.rpc();
const mintAccount = await getMint(provider.connection, mint);
expect(mintAccount.supply).equal(supply);
});
});
The dependencies in my Cargo.toml file look like this if that helps:
[dependencies]
anchor-lang = { version = "0.25.0", features = ["init-if-needed"] }
anchor-spl = "0.25.0"
Any idea what I'm missing here?