0

I try to run this function in client, but I got an issue with it.

    try {
      const provider = getProvider();
      const program = new Program(idl, provider);
      const accounts = {
        accounts: {
          campaign: publicKey,
          user: provider.wallet.publicKey,
          systemProgram: SystemProgram.programId
        }
      }
      await program.methods.donate(new BN(0.5) * web3.LAMPORTS_PER_SOL, accounts).rpc();
      console.log("Donated some money to: ", publicKey.toString());
      getCampaign();
    } catch (error) {
      console.log("Error: ", error);
    }
  };```

browser throw a catch error, told me that "Error:  Error: provided too many arguments 0,[object Object],[object Object] to instruction donate expecting: amount". Although, I ensure that I passed right args. Here is **donate** function in Rust.

```pub fn donate(ctx: Context<Donate>, amount: u64) -> ProgramResult {
        let ix = transfer(
            &ctx.accounts.user.key(),
            &ctx.accounts.campaign.key(),
            amount,
        );
        let _ = invoke(
            &ix,
            &[
                ctx.accounts.user.to_account_info(),
                ctx.accounts.campaign.to_account_info(),
            ],
        );
        (&mut ctx.accounts.campaign).amount_donated += amount;
        Ok(())
    }```

and derive account struct.

```#[derive(Accounts)]
pub struct Donate<'info> {
    #[account(mut)]
    pub campaign: Account<'info, Campaign>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}```

1 Answer 1

1

You should convert to this new BN(0.5 * web3.LAMPORTS_PER_SOL)

await program.methods.donate(new BN(0.5 * web3.LAMPORTS_PER_SOL)).accounts({ campaign: publicKey, user: provider.wallet.publicKey, systemProgram: SystemProgram.programId }).rpc();

1
  • Thank you. I resolved it, coral-xyz/anchor @0.3.0 Commented Apr 23 at 4:55

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.