Erro while testing Anchor programm

592 Views Asked by At

Trying to test my program, using example from Anchor book(https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-2). But have a problem, when run anchor test. A program deployed on local host.

1) solana_anchor_count
       Creates a counter:
     Error: failed to send transaction: invalid transaction: Transaction failed to sanitize accounts offsets correctly
      at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:4068:13)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:4030:20)
      at sendAndConfirmRawTransaction (node_modules/@solana/web3.js/src/util/send-and-confirm-raw-transaction.ts:27:21)
      at Provider.send (node_modules/@project-serum/anchor/src/provider.ts:118:18)
      at Object.rpc [as create] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:25:23)

...
error Command failed with exit code 2.

Program code

use anchor_lang::prelude::*;

declare_id!("AtVwEAsPzCcvFqXh5M6L4ZsYmKQgsRERuFnZo2g3PY8A");

#[program]
pub mod solana_anchor_count {
    use super::*;
    pub fn create(ctx: Context<Create>, authority: Pubkey) -> Result<()> {
        let counter = &mut ctx.accounts.counter;
        counter.authority = authority;
        counter.count = 0;
        Ok(())
    }

    pub fn increment(ctx: Context<Increment>) -> Result<()> {
        let counter = &mut ctx.accounts.counter;
        counter.count += 1;
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Create<'info> {
    #[account(init, payer = user, space = 8 + 40)]
    pub counter: Account<'info, Counter>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Increment<'info> {
    #[account(mut, has_one = authority)]
    pub counter: Account<'info, Counter>,
    pub authority: Signer<'info>,
}

#[account]
pub struct Counter {
    pub authority: Pubkey,
    pub count: u64,
}

tests code(typescript)

import * as anchor from "@project-serum/anchor";
const assert = require("assert");
const { SystemProgram } = anchor.web3;


describe("solana_anchor_count", () => {
  // Configure the client to use the local cluster.
  anchor.setProvider(anchor.Provider.local());
  const provider = anchor.Provider.local();

  const program = anchor.workspace.SolanaAnchorCount;

  const counter = anchor.web3.Keypair.generate();

  it("Creates a counter", async () => {
    await program.rpc.create(provider.wallet.publicKey, {
      accounts: {
        counter: counter.publicKey,
        user: provider.wallet.publicKey,
        systemProgram: SystemProgram.programId,
      },
      signers: [counter],
    });

    let counterAccount = await program.account.counter.fetch(counter.publicKey);

    assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
    assert.ok(counterAccount.count.toNumber() === 0);
  });

  it("Updates a counter", async () => {
    await program.rpc.increment({
      accounts: {
        counter: counter.publicKey,
        authority: provider.wallet.publicKey,
      },
    });

    const counterAccount = await program.account.counter.fetch(
      counter.publicKey
    );

    assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
    assert.ok(counterAccount.count.toNumber() == 1);
  });
});

I'm new in Anchor and can't fined out what should i fix here.

0

There are 0 best solutions below