1
import { Connection, Keypair, PublicKey, clusterApiUrl } from "@solana/web3.js";
import { Program, setProvider, web3, AnchorProvider } from "@coral-xyz/anchor";
import { NodeStringSaver, IDL } from "../target/types/node_string_saver";
import "dotenv/config";
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";

const connection = new Connection(clusterApiUrl("devnet"));
const program = new Program<NodeStringSaver>(
  IDL,
  new PublicKey("LwLEiVLfLitKwkTSo6rScZspxTF1PfeEX8HBp71LCHE"),
  {
    connection,
  }
);
const keyPair = Keypair.fromSecretKey(
  bs58.decode(process.env.PRIVATE_KEY ?? "")
);

const wallet = new NodeWallet(keyPair);

const provider = new AnchorProvider(connection, wallet, {
  commitment: "processed",
});
setProvider(provider);

const start = async () => {
  const myAccount = web3.Keypair.generate();

  await program.methods
    .initialize("Users")
    .accounts({
      stringHolder: myAccount.publicKey,
      signer: provider.wallet.publicKey,
      systemProgram: web3.SystemProgram.programId,
    })
    .signers([myAccount])
    .rpc();
  console.log(await program.account.stringHolder.all());
};

start();

and this is my error Error: This function requires 'Provider.sendAndConfirm' to be implemented.

4 Answers 4

1

You used { connection: Connection } for the 3rd param of Program constructor which is derived from Provider interface. AnchorProvider is also derived from Provider and it has NodeWallet and it has sendAndConfirm function implemented. When you use program.methods().rpc() it tries to use Provider instance which it does not have sendAndConfirm implemented.

Here you get the updated code:

const connection = new Connection(clusterApiUrl("devnet"));
const keyPair = Keypair.fromSecretKey(
  bs58.decode(process.env.PRIVATE_KEY ?? "")
);
const wallet = new NodeWallet(keyPair);
const provider = new AnchorProvider(connection, wallet, {
  commitment: "processed",
});
setProvider(provider);
const program = new Program<NodeStringSaver>(
  IDL,
  new PublicKey("LwLEiVLfLitKwkTSo6rScZspxTF1PfeEX8HBp71LCHE"),
  provider,
);
0

i don't know about this error, but you can still send the transaction manually if you're stucked. Instead of using .rpc(), just create a new Transaction(), add your instruction, blockhash and feepayer, sign it and send it.

If you don't know how to do that, let me know, i will post some code to show you

1
  • Didn't know that thanks for that. Commented Nov 15, 2023 at 6:57
0

This function requires 'Provider.sendAndConfirm' to be implemented happened to me when I wanted to call a Solana Anchor program function from NodeJs or BunJs with my keypair file, no browser wallet.

Thanks to above answer, I got it working:

    const wallet = new NodeWallet(sender_keypair);
    const provider = new AnchorProvider(connection, wallet, {
        commitment: "processed",
    });
    setProvider(provider);

    const program = new Program(
      ANCHOR_PROGRAM_IDL_FROM_JSON_FILE as unknown as 
         ANCHOR_PROGRAM_TYPE_FROM_TS_FILE, provider);

    await program.methods.yourFunction(arg1, arg2, ...)
        .accounts({
            signer: sender_keypair.publicKey,
            //systemProgram: SystemProgram.programId,
        })
        .signers([senderKeypair])
        .rpc();
0

How to solve this problem?

Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'size')

code: const program = new Program(HelloIDL as HelloAnchor, programId, provider);

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.