Receiving "Phantom - RPC Error: Transaction creation failed" while building solana escrow with @solana/web3.js

1.9k Views Asked by At

I am trying to replicate https://github.com/dboures/solana-random-number-betting-game Although when I try to initiate my the Escrow I receive the following error:

Phantom - RPC Error: Transaction creation failed.
Uncaught (in promise) {code: -32003, message: 'Transaction creation failed.'}

I am using Phantom Wallet with Solana RPC.

const transaction = new Transaction({ feePayer: initializerKey })
  let recentBlockHash = await connection.getLatestBlockhash();
  transaction.recentBlockhash = await recentBlockHash.blockhash;
  
  const tempTokenAccount = Keypair.generate();

  // Create Temp Token X Account
  transaction.add(
    SystemProgram.createAccount({
      programId: TOKEN_PROGRAM_ID,
      fromPubkey: initializerKey,
      newAccountPubkey: tempTokenAccount.publicKey,
      space: AccountLayout.span,
      lamports: await connection.getMinimumBalanceForRentExemption(AccountLayout.span )
    })
  );

  const { signature } = await wallet.signAndSendTransaction(transaction);
  let txid = await connection.confirmTransaction(signature);
  console.log(txid);
2

There are 2 best solutions below

1
On BEST ANSWER

I was able to solve my problem by using the following code:

const signed = await wallet.request({
    method: "signTransaction",
    params: {
      message: bs58.encode(transaction.serializeMessage())
    }
  });
  const signature = bs58.decode(signed.signature)
  transaction.addSignature(initializerKey, signature);
  transaction.partialSign(...[tempTokenAccount]);

  await connection.sendRawTransaction(transaction.serialize())

instead of:

await wallet.signAndSendTransaction(transaction, {signers: [tempTokenAccount]})

Basically at first I was using one simple function to perform all the above steps, however, for some reason it was not working and throwing the subjected error. When I used this breakdown code it worked!. The cause of the error is still mysterious to me.

Thank you.

1
On

You're trying to create an account without signing with the keypair of that account to prove ownership.

You have to add the keypair as a signer like such:

await wallet.signAndSendTransaction(transaction, {signers: [tempTokenAccount]})