AlmostEfficient / full-stack-solana-dev

Anchor + Rust + React Solana app from scratch. Video walkthrough below.
https://youtu.be/vUHF1X48zM4
Creative Commons Zero v1.0 Universal
28 stars 17 forks source link

Error: Invalid arguments: counter not provided even after deleting target folder. #2

Closed gameuser1982 closed 5 months ago

gameuser1982 commented 5 months ago

I am using Anchor 0.29.0

I keep getting Error: Invalid arguments: counter not provided even after I delete the target folder, run "anchor keys sync", and finally run "anchor test --skip-local-validator".

What is the work around?

Here is the full error:

candid@DESKTOP-5D7VSM9:~/counter$ anchor test --skip-local-validator

warning: virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2021 which implies `resolver = "2"`
note: to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest
note: to use the edition 2021 resolver, specify `workspace.resolver = "2"` in the workspace root's manifest
note: for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
   Compiling proc-macro2 v1.0.82
   Compiling unicode-ident v1.0.12
   Compiling version_check v0.9.4
   Compiling serde v1.0.202
   Compiling typenum v1.17.0
   Compiling syn v1.0.109
   Compiling thiserror v1.0.60
   Compiling serde_json v1.0.117
   Compiling semver v1.0.23
   Compiling subtle v2.5.0
   Compiling anyhow v1.0.83
   Compiling unicode-segmentation v1.11.0
   Compiling libc v0.2.154
   Compiling cpufeatures v0.2.12
   Compiling ryu v1.0.18
   Compiling cfg-if v1.0.0
   Compiling itoa v1.0.11
   Compiling heck v0.3.3
   Compiling generic-array v0.14.7
   Compiling rustc_version v0.4.0
   Compiling quote v1.0.36
   Compiling syn v2.0.63
   Compiling bs58 v0.5.1
   Compiling once_cell v1.19.0
   Compiling proc-macro-error-attr v1.0.4
   Compiling autocfg v1.3.0
   Compiling proc-macro-error v1.0.4
   Compiling hashbrown v0.14.5
   Compiling equivalent v1.0.1
   Compiling ahash v0.7.8
   Compiling ahash v0.8.11
   Compiling toml_datetime v0.6.6
   Compiling feature-probe v0.1.1
   Compiling winnow v0.5.40
   Compiling bv v0.11.1
   Compiling indexmap v2.2.6
   Compiling solana-frozen-abi-macro v1.18.13
   Compiling cfg_aliases v0.1.1
   Compiling zerocopy v0.7.34
   Compiling rustversion v1.0.17
   Compiling jobserver v0.1.31
   Compiling cc v1.0.97
   Compiling borsh v1.5.0
   Compiling memoffset v0.9.1
   Compiling num-traits v0.2.19
   Compiling solana-frozen-abi v1.18.13
   Compiling hashbrown v0.11.2
   Compiling hashbrown v0.13.2
   Compiling bs58 v0.4.0
   Compiling toml_edit v0.21.1
   Compiling lazy_static v1.4.0
   Compiling keccak v0.1.5
   Compiling either v1.11.0
   Compiling log v0.4.21
   Compiling arrayvec v0.7.4
   Compiling constant_time_eq v0.3.0
   Compiling arrayref v0.3.7
   Compiling itertools v0.10.5
   Compiling getrandom v0.2.15
   Compiling base64 v0.13.1
   Compiling borsh-derive-internal v0.10.3
   Compiling proc-macro-crate v3.1.0
   Compiling borsh-schema-derive-internal v0.10.3
   Compiling borsh-derive-internal v0.9.3
   Compiling borsh-schema-derive-internal v0.9.3
   Compiling blake3 v1.5.1
   Compiling solana-program v1.18.13
   Compiling serde_derive v1.0.202
   Compiling thiserror-impl v1.0.60
   Compiling syn_derive v0.1.8
   Compiling bytemuck_derive v1.6.0
   Compiling solana-sdk-macro v1.18.13
   Compiling num-derive v0.4.2
   Compiling anchor-derive-space v0.29.0
   Compiling borsh-derive v1.5.0
   Compiling bytemuck v1.16.0
   Compiling serde_bytes v0.11.14
   Compiling bincode v1.3.3
   Compiling toml v0.5.11
   Compiling crypto-common v0.1.6
   Compiling block-buffer v0.10.4
   Compiling digest v0.10.7
   Compiling sha2 v0.10.8
   Compiling sha3 v0.10.8
   Compiling anchor-syn v0.29.0
   Compiling proc-macro-crate v0.1.5
   Compiling borsh-derive v0.10.3
   Compiling borsh-derive v0.9.3
   Compiling borsh v0.10.3
   Compiling borsh v0.9.3
   Compiling anchor-attribute-constant v0.29.0
   Compiling anchor-attribute-program v0.29.0
   Compiling anchor-attribute-error v0.29.0
   Compiling anchor-derive-accounts v0.29.0
   Compiling anchor-attribute-access-control v0.29.0
   Compiling anchor-derive-serde v0.29.0
   Compiling anchor-attribute-event v0.29.0
   Compiling anchor-attribute-account v0.29.0
   Compiling anchor-lang v0.29.0
   Compiling counter v0.1.0 (/home/candid/counter/programs/counter)
    Finished release [optimized] target(s) in 5m 05s
Deploying cluster: http://localhost:8899
Upgrade authority: /home/candid/.config/solana/id.json
Deploying program "counter"...
Program path: /home/candid/counter/target/deploy/counter.so...
Program Id: 4U4Fiq1qRKcLBC7caitSKX8fdhA7na8bFg6cQi1JmNPW

Deploy success

Found a 'test' script in the Anchor.toml. Running it as a test suite!

Running test suite: "/home/candid/counter/Anchor.toml"

yarn run v1.22.22
warning package.json: No license field
$ /home/candid/counter/node_modules/.bin/ts-mocha -p ./tsconfig.json -t 1000000 'tests/**/*.ts'

  counter
Error: Invalid arguments: counter not provided.
    at /home/candid/counter/node_modules/@coral-xyz/anchor/src/program/common.ts:50:15
    at Array.forEach (<anonymous>)
    at validateAccounts (/home/candid/counter/node_modules/@coral-xyz/anchor/src/program/common.ts:45:14)
    at ix (/home/candid/counter/node_modules/@coral-xyz/anchor/src/program/namespace/instruction.ts:44:23)
    at txFn (/home/candid/counter/node_modules/@coral-xyz/anchor/src/program/namespace/transaction.ts:24:14)
    at MethodsBuilder.rpc [as _rpcFn] (/home/candid/counter/node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:21:18)
    at MethodsBuilder.rpc (/home/candid/counter/node_modules/@coral-xyz/anchor/src/program/namespace/methods.ts:212:17)
    ✔ Is initialized! (80ms)
    1) Increment

  1 passing (99ms)
  1 failing

  1) counter
       Increment:
     Error: Invalid arguments: counter not provided.
      at /home/candid/counter/node_modules/@coral-xyz/anchor/src/program/common.ts:50:15
      at Array.forEach (<anonymous>)
      at validateAccounts (node_modules/@coral-xyz/anchor/src/program/common.ts:45:14)
      at ix (node_modules/@coral-xyz/anchor/src/program/namespace/instruction.ts:44:23)
      at txFn (node_modules/@coral-xyz/anchor/src/program/namespace/transaction.ts:24:14)
      at MethodsBuilder.rpc [as _rpcFn] (node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:21:18)
      at MethodsBuilder.rpc (node_modules/@coral-xyz/anchor/src/program/namespace/methods.ts:212:17)

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
gameuser1982 commented 5 months ago

I solved the issue:

I changed the original counter.ts:

import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Counter } from "../target/types/counter";
import { PublicKey } from "@solana/web3.js";

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

  const program = anchor.workspace.Counter as Program<Counter>;
  const wallet = provider.wallet as anchor.Wallet;
  const connection = provider.connection;

  const [counterPDA] = PublicKey.findProgramAddressSync(
    [Buffer.from("counter")],
    program.programId
  );

  it("Is initialized!", async () => {
    try {
      const txSig = await program.methods.initialize().rpc();

      const accountData = await program.account.counter.fetch(counterPDA);
      console.log(`Transaction Signature: ${txSig}`);
      console.log(`Count: ${accountData.count}`);
    } catch (error) {
      // If PDA Account already created, then we expect an error
      console.log(error);
    }
  });

  it("Increment", async () => {
    const transactionSignature = await program.methods.increment().rpc();

    const accountData = await program.account.counter.fetch(counterPDA);

    console.log(`Transaction Signature: ${transactionSignature}`);
    console.log(`Count: ${accountData.count}`);
  });
});

to the following:

import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Counter } from "../target/types/counter";
import { PublicKey, SystemProgram } from "@solana/web3.js";

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

  const program = anchor.workspace.Counter as Program<Counter>;

  it("Is initialized!", async () => {
    const [counterPDA, counterBump] = await PublicKey.findProgramAddressSync(
      [Buffer.from("counter")],
      program.programId
    );

    const txSig = await program.methods.initialize()
      .accounts({
        user: provider.wallet.publicKey,
        counter: counterPDA,
        systemProgram: SystemProgram.programId,
      })
      .signers([]) // If additional signers are needed, they would go here.
      .rpc();

    console.log(`Transaction Signature: ${txSig}`);

    const accountData = await program.account.counter.fetch(counterPDA);
    console.log(`Count: ${accountData.count}`);
    console.log(`Counter Bump: ${accountData.bump}`);
  });

  it("Increment", async () => {
    const [counterPDA, counterBump] = await PublicKey.findProgramAddressSync(
      [Buffer.from("counter")],
      program.programId
    );

    const txSig = await program.methods.increment()
      .accounts({
        counter: counterPDA,
      })
      .rpc();

    console.log(`Transaction Signature: ${txSig}`);

    const accountData = await program.account.counter.fetch(counterPDA);
    console.log(`Count: ${accountData.count}`);
  });
});

I still don't get why the original code seemed to work for Mr Raza @AlmostEfficient. I am running node v20.12.2 and Solana 1.18.13 so my Node version is slightly newer while my Solana version is slightly older than what was used in Mr Raza's video.

A lot of the criticism regarding Solana posted in this blog seem valid:

https://devlubo.com/blog/why-solana-so-incredibly-stupid-work-rant

I noticed that code from just 2 years ago is completely obsolete and you need to do things completely differently and everything is poorly documented by Solana Labs / Solana Foundation. Even Mr Raza's video from a month ago is obsolete now because he uses Anchor 0.29.0 while the latest version of Anchor is 0.30.0 and it does integration between frontend and blockchain very differently than 0.29.0.

Solana needs to get their shxt together. It's a nightmare to work with.

Even though I found a solution, I am keeping this open in case anyone else needs help.

AlmostEfficient commented 5 months ago

Yo, this is happening cuz the guide is missing a tiny part - you're supposed to enable seeds as a feature in anchor.toml.

''' [features] seeds = true '''

Please view line 733 in this commit

https://github.com/AlmostEfficient/full-stack-solana-dev/commit/f14266dba73cfdbf61c81f3816d41d73a1ecb75d

I've opened a PR to have it updated in the official Solana guide too, just waiting on a merge.

Solana moves fast and things break a lot. Teams are aware and they're working on removing these pains. This particular instance was a mistake on my part. Thanks for your patience <3

gameuser1982 commented 5 months ago

All good man! Keep those great videos coming! I love the projects you're coming up with to inspire new solana devs!