Search code examples
solanasolana-web3jsescrow

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


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

Solution

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