1

I want to call a method of solana smartcontract. I have an IDL. While I'm calling it I'm getting the following error (when I'm calling await this.program.methods.initialize() method):

Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x0

this.anchorProvider = anchor.AnchorProvider.env();
anchor.setProvider(this.anchorProvider);

const mintAccount = await createMint(
  this.anchorProvider.connection,
  this.payerKey,
  this.mintAuthorityKey.publicKey,
  null,
  DECIMALS
);

const [pdaGlobalAccount] = await anchor.web3.PublicKey.findProgramAddress(
  [Buffer.from("global")],
  this.program.programId
);
const [pdaEscrow] = await anchor.web3.PublicKey.findProgramAddress(
  [Buffer.from("escrow")],
  this.program.programId
);
// console.log('pdaGlobalAccount', pdaGlobalAccount.toBase58())

// Test initialize instruction
let init = await this.program.methods
  .initialize()
  .accounts({
    globalState: pdaGlobalAccount,
    escrowAccount: pdaEscrow,
    mint: mintAccount,
    authority: this.adminKey.publicKey,
    systemProgram: anchor.web3.SystemProgram.programId,
    tokenProgram: TOKEN_PROGRAM_ID,
    rent: SYSVAR_RENT_PUBKEY,
  })
  .signers([this.adminKey])
  .rpc();

const result = await this.confirmTransaction(init);

here is confirmTransaction method:

async confirmTransaction(tx: string) {
  const latestBlockHash = await this.anchorProvider.connection.getLatestBlockhash();

  return await this.anchorProvider.connection.confirmTransaction({
    blockhash: latestBlockHash.blockhash,
    lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
    signature: tx
  })
}

1 Answer 1

4

0x0 means you're attempting to initialize an already initialized account. What's happening here is that because your PDAs have only constant seeds, the same address is generated for them on each run of the test.

Therefore, not only does running your test for the first time initialize pdaGlobalAccount and pdaEscrow, but subsequent tests also fail because they would attempt to initialize the same accounts as the first (because constant seeds give you the exact same address each time).

5
  • ah, that seems to be the case for me as well. Now the next question is, how to reset everything so that I can initialize again? I stopped solana-test-validator and restarted it.. and then ran the tests again anchor test --skip-local-validator. But I see the same error. That means the state is persistent. How do I clear/reset it? Commented Feb 11, 2023 at 19:57
  • Also, where can I get the list of these error codes (numbers) and their meaning/explanation? Where did you learn that "0x0 means you're attempting to initialize an already initialized account"? Commented Feb 11, 2023 at 20:03
  • solana-test-validator -r resets the validator. You can also do this manually by deleting the test-ledger file. I wish I could give you a good answer to the second question. I'd recomment viewing the program logs though for more context on errors. An easy way to do this is solana logs or just try..catch in javascript
    – Ademola
    Commented Feb 11, 2023 at 20:34
  • solana-test-validator -r is exactly what I need. Thanks a lot. As for the second question, hope as the adoption increases, the docs also gets better. Commented Feb 11, 2023 at 22:42
  • 1
    Re: error codes and their meaning. You can find the codes for system errors here
    – Ademola
    Commented Feb 19, 2023 at 14:57

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.