0
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.

2 Answers 2

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

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,
);

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.