0

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 Answers2

1

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]})
Jacob Creech
  • 1,797
  • 2
  • 11
  • Thank you for your input, However, I wasn't able to solve the issue with this. I had to basically use all steps sign, decode, addSignature, partialSign, and then sendRaw to achieve the results. – Sultan Nadeem Mar 10 '22 at 06:03
0

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.

  • I encounter the same problem as you. This solution seems to work, only on the official Phantom documentation it is stated that this method is deprecated and will no longer be maintained. https://docs.phantom.app/integrating/sending-a-transaction#deprecated-methods – Mayzz Mar 26 '22 at 18:52