3

I wrote an anchor program which initializes a counter account and sets its inital value to instruction_data.

pub fn initialize(ctx: Context<Initialize>, instruction_data: u64) -> Result<()> {
        let counter = &mut ctx.accounts.counter;
        counter.count = instruction_data;
        msg!("Counter account created");
        msg! {"Current count: { }", counter.count};
        Ok(())
    }

How can I pass this instruction_data in my test function?

  it("Is initialized!", async () => {
    // Add your test here.
    const tx = await program.methods
      .initialize( 12 )
      .accounts({
        counter: counter.publicKey,
      })
      .signers([counter])
      .rpc();
    console.log("Your transaction signature", tx);
    const account = await program.account["counter"].fetch(counter.publicKey);
    console.log("Count: ", account.count.toNumber());
    expect(account.count.toNumber()).to.eq(0);
  });

I don't know in what format I have to pass the instruction_data in the initialize function.

1 Answer 1

1

I figured it out. I need to convert the data to BN.

  it("Is initialized!", async () => {
    // Add your test here.
    const tx = await program.methods
      .initialize( new anchor.BN(12) )
      .accounts({
        counter: counter.publicKey,
      })
      .signers([counter])
      .rpc();
    console.log("Your transaction signature", tx);
    const account = await program.account["counter"].fetch(counter.publicKey);
    console.log("Count: ", account.count.toNumber());
    expect(account.count.toNumber()).to.eq(0);
  });

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.