3

I initialized a correct program

  const provider = getProvider();
  const program = new Program(idl, programID, provider);

When I called this function

await program.rpc.create("name", "description", {
    accounts: {
      company,
      user: provider.wallet.publicKey,
      systemProgram: SystemProgram.programId,
    },
  });

I was getting this error:

Transaction simulation…ocessing Instruction 0: custom program error: 0x0

I just swithced to use program.methods.create and it worked. So I thought this is the way how to call solana methods. Then I had another function call with `program.methods:

await program.methods.enter(new BN(0.1 * web3.LAMPORTS_PER_SOL), {
    accounts: {
      // public key that is passed to this function as argument
      company: publicKey,
      user: provider.wallet.publicKey,
      systemProgram: SystemProgram.programId,
    },
  });

this function never runs. this time I switched to program.rpc.enter and this time it worked. I do not understand what is causing this conflict.

I tried this to call create fn

await program.methods
    .create("name", "description")
    .accounts({
      company,
      user: provider.wallet.publicKey,
      systemProgram: SystemProgram.programId,
    })
    .rpc();

I still got this error

Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0

1 Answer 1

4

Try adding .rpc to the end of await program.methods to send the transaction

Example format below, (signers([]) can be excluded if there are not additional signers required):

// sends transaction
await program.methods
  .instructionName(instructionDataInputs)
  .accounts({})
  .signers([])
  .rpc()

Or you can create a transaction and then send the transaction:

// creates transaction
const transaction = await program.methods
  .instructionName(instructionDataInputs)
  .accounts({})
  .transaction()

await sendTransaction(transaction, connection)

Or you can create an instruction, and add multiple instructions to a single transaction:

// creates first instruction
const instructionOne = await program.methods
  .instructionOneName(instructionOneDataInputs)
  .accounts({})
  .instruction()

// creates second instruction
const instructionTwo = await program.methods
  .instructionTwoName(instructionTwoDataInputs)
  .accounts({})
  .instruction()

// add both instruction to one transaction
const transaction = new Transaction().add(instructionOne, instructionTwo)

// send transaction
await sendTransaction(transaction, connection)
8
  • I tried the first method but did not work. i pasted the code of how I tried.
    – Yilmaz
    Commented Jan 12, 2023 at 3:28
  • What version of Anchor are you using? Also, could it be because you are trying to initialize an account that already exists? (assuming the create instruction is initializing the company account, which uses a PDA as the address)
    – john
    Commented Jan 12, 2023 at 4:22
  • "@project-serum/anchor": "^0.26.0", anchor-lang = "0.24.2"
    – Yilmaz
    Commented Jan 12, 2023 at 22:30
  • try making sure all the anchor versions match, also run anchor --version to check which version of anchor cli you're using
    – john
    Commented Jan 12, 2023 at 22:55
  • regarding " could it be because you are trying to initialize an account that already exists?". Everytime I call create contract function new account gets initialized?
    – Yilmaz
    Commented Jan 12, 2023 at 22:56

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.