coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.59k stars 1.32k forks source link

Issue with rpc #2875

Closed ASoldo closed 6 months ago

ASoldo commented 6 months ago

Hi, I'm trying to call initUser method from Frontend (Nuxt3) and create user on deployed solana program using exported IDL. However I keep getting error when trying to call that method.

const crateUserAccount = async () => {
  const connection = new web3.Connection(
    web3.clusterApiUrl("devnet"),
    "confirmed",
  );

  const provider = new anchor.AnchorProvider(
    connection,
    wallet.value as any,
    anchor.AnchorProvider.defaultOptions(),
  );

  const program = new anchor.Program(idl as anchor.Idl, PROGRAM_KEY, provider);

  console.log("Program: Crate User Account", program);

  const [create_user_data] = findProgramAddressSync(
    [
      utf8.encode("user"),
      new web3.PublicKey(
        wallet.value?.adapter.publicKey as web3.PublicKey,
      ).toBuffer(),
    ],
    program.programId,
  );
  console.log("Create user data", create_user_data);

  const prog = await program.methods  // <--- breaks here
    .initUser("Marac", "Avatar")
    .accounts({
      userAccount: create_user_data,
      authority: anchorWallet.value?.publicKey as web3.PublicKey,
      systemProgram: anchor.web3.SystemProgram.programId,
    })
    .rpc();
};

This is the message I get:

Uncaught (in promise) TypeError: this.wallet.signTransaction is not a function
    at AnchorProvider.sendAndConfirm (@project-serum_anchor.js?v=998bb21e:6769:28)
    at async MethodsBuilder.rpc2 [as _rpcFn] (@project-serum_anchor.js?v=998bb21e:9863:16)
    at async crateUserAccount (index.vue:240:16)

Anybody know how to fix this? I don't have any warnings or issues in editor.

acheroncrypto commented 6 months ago

I don't have any warnings or issues in editor.

You're using wallet.value as any for the wallet and getting an error related to the wallet, which likely means that value is not what Anchor expects.

I see this is a frontend code, so you can use useAnchorWallet for this purpose.